fromPacket static method

ArPacket fromPacket(
  1. String raw
)
override

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

Implementation

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

  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 ArPacket(reason: parts[0]);
}