fromPacket static method

PaPacket fromPacket(
  1. String raw
)
override

fromPacket creates a PaPacket from a string packet in the format of Layrz Protocol v3.

Implementation

static PaPacket fromPacket(String raw) {
  if (!raw.startsWith('<Pa>') || !raw.endsWith('</Pa>')) {
    throw ParseException('Invalid identification packet, should be <Pa>...</Pa>');
  }

  final parts = raw.substring(4, raw.length - 5).split(';');
  int? receivedCrc = int.tryParse(parts[parts.length - 1], radix: 16);
  int? calculatedCrc = calculateCrc("${parts.sublist(0, parts.length - 1).join(';')};".codeUnits);

  if (receivedCrc != calculatedCrc) {
    throw CrcException('Invalid CRC, received: $receivedCrc, calculated: $calculatedCrc');
  }

  return PaPacket(ident: parts[0], password: parts[1]);
}