fromPacket static method

AcPacket fromPacket(
  1. String raw
)
override

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

Implementation

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

  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 AcPacket(commands: Command.fromPackets(parts.sublist(0, parts.length - 1).join(';')));
}