toPacket method
toPacket returns the packet in the format of Layrz Protocol v3.
Implementation
@override
String toPacket() {
String payload = '${timestamp.millisecondsSinceEpoch ~/ 1000};';
payload += '${position.latitude ?? ''};';
payload += '${position.longitude ?? ''};';
payload += '${position.altitude ?? ''};';
payload += '${position.speed ?? ''};';
payload += '${position.direction ?? ''};';
payload += '${position.satellites ?? ''};';
payload += '${position.hdop ?? ''};';
List<String> extraList = [];
for (String key in extra.keys) {
// Escape colons in key and value so they don't collide with the `key:value` separator on the
// wire (e.g. a MAC-like identifier `a4:c1:...`). Reversed by Packet.parseExtraArgs (`___` -> `:`).
final escapedKey = key.replaceAll(':', '___');
final escapedValue = '${extra[key]}'.replaceAll(':', '___');
extraList.add('$escapedKey:$escapedValue');
}
payload += '${extraList.join(',')};';
String crc = calculateCrc(payload.codeUnits).toRadixString(16).padLeft(4, '0').toUpperCase();
return '<Pd>$payload$crc</Pd>';
}