UNPKG

packet-decoder

Version:

SenRa Packet Decoder For IOT devices

106 lines (97 loc) 4.19 kB
class PlacePod { constructor(packet) { packet = packet.toUpperCase(); this.packet_type = this.getPacketType(packet.substring(0, 2)); this.payload = this.getPayload(packet.substring(2, 26)); this.time_stamp = Buffer.from(packet.substring(26, 34), 'hex').readUInt32LE(); } comparePacket(prev_packet) { if (this.payload.car_status !== prev_packet.payload.car_status) return false; return true; } getPacketType(packet_type) { switch (packet_type) { case '15': return 'CAR_DETECTOR_SENSOR'; case '20': return 'CAR_DETECTOR_DATA_SENSOR'; case '21': return 'CAR_DRIVE_THROUGH_SENSOR'; case '30': return 'CONFIGURATION_STATE'; case '31': return 'VERSION_INFO'; case '32': return '16_BIT_CAR_MAG_DATA'; case '33': return 'PONG'; case '34': return 'VERSION'; case '35': return 'CONFIGURATION_PARAMETER'; case '36': return 'RADIO_STATS'; case '37': return 'SELF_REPORT'; case '38': return 'TIME_SYNC'; case '39': return 'ALGORITHM_DEBUG'; case 'FE': return 'META_EVENT'; } return null; } getPayload(payload) { switch (this.packet_type) { case 'CAR_DETECTOR_SENSOR': return { car_status: this.getResult(payload.substring(0, 2)), // flag: this.getFlag(payload.substring(2, 4)), temperature: Buffer.from(payload.substring(4, 12).padEnd(8, '0'), 'hex').readFloatLE(), battery_status: Buffer.from(payload.substring(12, 20).padEnd(8, '0'), 'hex').readFloatLE(), pkt_counter: Buffer.from(payload.substring(22, 24).padEnd(8, '0'), 'hex').readUInt8() }; case 'SELF_REPORT': return { car_status: this.getResult(payload.substring(0, 4)), temperature: Buffer.from(payload.substring(8, 12).padEnd(16, '0'), 'hex').readUInt16LE() / 10, battery_status: Buffer.from(payload.substring(4, 8).padEnd(16, '0'), 'hex').readUInt16LE() / 1000, pkt_counter: Buffer.from(payload.substring(22, 24).padEnd(8, '0'), 'hex').readUInt8(), }; } } getFlag(flag) { const buffer = Buffer.from(flag, 'hex'); flag = buffer.readUInt8().toString(2).padStart(8, '0'); return { confidence: Buffer.from(parseInt(flag.substring(5, 8), 2).toString(16).padStart(2, '0'), 'hex').readUInt8(), calibration_complete: Buffer.from(parseInt(flag.substring(4, 5), 2).toString(16).padStart(2, '0'), 'hex').readUInt8(), ls_mode: Buffer.from(parseInt(flag.substring(3, 4), 2).toString(16).padStart(2, '0'), 'hex').readUInt8(), mag_signature: Buffer.from(parseInt(flag.substring(1, 3), 2).toString(16).padStart(2, '0'), 'hex').readUInt8(), rfu: Buffer.from(parseInt(flag.substring(0, 1), 2).toString(16).padStart(2, '0'), 'hex').readUInt8() }; } getResult(result) { const buffer = Buffer.from(result, 'hex'); if (result.length === 2) { result = Number(buffer.readUInt8()).toString(16).padStart(2, '0'); } else if (result.length === 4) { result = Number(buffer.readUInt16LE()).toString(16).padStart(2, '0'); } switch (result) { case '00': return 'UNINITIALIZED'; case '01': return 'NO_CAR'; case '02': return 'CAR_ENTERING'; case '03': return 'CAR_PARKED'; case '04': return 'CAR_LEAVING'; } return null; } } module.exports = PlacePod;