fromPacket static method
fromPacket creates a PiPacket from a string packet in the format of Layrz Protocol v3.
Implementation
static PiPacket fromPacket(String raw) {
if (!raw.startsWith('<Pi>') || !raw.endsWith('</Pi>')) {
throw ParseException('Invalid identification packet, should be <Pi>...</Pi>');
}
final parts = raw.substring(4, raw.length - 5).split(';');
if (parts.length != 9) {
throw MalformedException('Invalid packet parts, should have 6 parts');
}
int? receivedCrc = int.tryParse(parts[8], radix: 16);
int? calculatedCrc = calculateCrc("${parts.sublist(0, 8).join(';')};".codeUnits);
if (receivedCrc != calculatedCrc) {
throw CrcException('Invalid CRC, received: $receivedCrc, calculated: $calculatedCrc');
}
int? firmwareBuild = int.tryParse(parts[2]);
if (firmwareBuild == null) {
throw MalformedException('Invalid firmware build number');
}
bool fotaEnabled = parts[7].toLowerCase() == 'true' || parts[7] == '1';
return PiPacket(
ident: parts[0],
firmwareId: parts[1],
firmwareBuild: firmwareBuild,
deviceId: parts[3],
hardwareId: parts[4],
modelId: parts[5],
firmwareBranch: parts[6] == '1' ? FirmwareBranch.development : FirmwareBranch.stable,
fotaEnabled: fotaEnabled,
);
}