fromPacket static method
fromPacket creates a AuPacket from a string packet in the format of Layrz Protocol v3.
Implementation
static AuPacket fromPacket(String raw) {
if (!raw.startsWith('<Au>') || !raw.endsWith('</Au>')) {
throw ParseException('Invalid identification packet, should be <Au>...</Au>');
}
final parts = raw.substring(4, raw.length - 5).split(';');
if (parts.length != 2) {
throw MalformedException('Invalid packet parts, should have 2 parts');
}
int? receivedCrc = int.tryParse(parts[1], radix: 16);
int? calculatedCrc = calculateCrc("${parts.sublist(0, 1).join(';')};".codeUnits);
if (receivedCrc != calculatedCrc) {
throw CrcException('Invalid CRC, received: $receivedCrc, calculated: $calculatedCrc');
}
return AuPacket();
}