From 7420039a55962b7f30b81f53bb15d0a8e9654a7b Mon Sep 17 00:00:00 2001 From: dpinkevych Date: Mon, 2 Nov 2026 11:20:28 +0200 Subject: [PATCH 01/13] feat(iot-hub): the default TTN/TTI converter decodes Lansitec devices from the LoRaWAN Device Repository The Lansitec codecs wrap everything in bytes, give numbers as strings with units and get some fields wrong. The default converter unwraps them, names the keys as the Lansitec datasheets do, decodes the wrong fields from the raw bytes, and writes the Register message as attributes. The JS default reads the payload as UTF-8. --- .../src/assets/converters/js-tti-decoder.raw | 425 +++++++++++++++++- .../src/assets/converters/js-ttn-decoder.raw | 425 +++++++++++++++++- .../assets/converters/tbel-tti-decoder.raw | 378 +++++++++++++++- .../assets/converters/tbel-ttn-decoder.raw | 378 +++++++++++++++- 4 files changed, 1600 insertions(+), 6 deletions(-) diff --git a/ui-ngx/src/assets/converters/js-tti-decoder.raw b/ui-ngx/src/assets/converters/js-tti-decoder.raw index ee4314679b..ca1d911e66 100644 --- a/ui-ngx/src/assets/converters/js-tti-decoder.raw +++ b/ui-ngx/src/assets/converters/js-tti-decoder.raw @@ -2,11 +2,428 @@ // is then that JSON: its keys become telemetry. A Milesight-style history list keeps each record's time. // Such a device gets the profile -, so all devices of one model share it, // or the name the integration metadata gives it under profile.-. Any other device gets LoRaWAN Device. + +// Lansitec: the LoRaWAN Device Repository codecs wrap everything in "bytes", write numbers as strings with units +// ("85%", "-67dBm") and get some fields wrong. Key names follow the Lansitec datasheets where there are any: +// Bluetooth Gateway DataSheet 2.27, Badge Tracker DataSheet v5.30, Temperature Humidity Sensor DataSheet 1.26. +// The wrong fields are decoded from the raw bytes (metadata.data). Remove a fix when the Repository fixes the codec. +var LANSITEC_UNITS = ['dBm', 'dB', '%', 'V', 'sec', 'min', 's', '℃', 'level', ' meter']; + +var LANSITEC_GATEWAYS = ['compact-bluetooth-gateway', 'indoor-bluetooth-gateway', 'macro-bluetooth-gateway', 'solar-bluetooth-gateway']; + +// Codec key -> datasheet name, per model group. '-' drops the key. +// +// Bluetooth Gateway DataSheet 2.27, 4.4.1 Registration: ADR, Mode (current working LoRaWAN frequency sub-band plan), +// LoRaWAN frequency band, Power (LoRa TX power, dBm), Continuously BLE receiving, DR (LoRaWAN Uplink Data Rate), +// PRI (Position report interval), HB (Heartbeat interval), Bluetooth receiving duration. +// 4.4.2 Heartbeat: RSSI and SNR (LoRa), VER (Firmware version). VOL, SNR and CHGSTAT come from the raw bytes. +// number is the count of reported devices: the beacons list has them. The beacons come from the raw bytes too. +var LANSITEC_GATEWAY_KEYS = { + adr: 'adr', mode: 'mode', LoRaWANband: 'loraWanFrequencyBand', LoRaTxpower: 'loraTxPower', + ContinuousReceive: 'continuousBleReceiving', dr: 'dataRate', pos: 'positionReportInterval', + hb: 'heartbeatInterval', BLEReceivingDuration: 'bluetoothReceivingDuration', + rssi: 'loraRssi', snr: '-', ver: 'deviceFirmwareVersion', vol: '-', chgstate: '-', number: '-' +}; + +// Badge Tracker DataSheet v5.30, 5.1 Registration: ADR, LoRa TX power, DR, GNSSEN (GNSS enable status), +// POSMODE (Positioning report mode), BLEPRI (Bluetooth Position Report Interval). The codec reads it without the +// GNSSPRI field, so heartbeat, version, CFMMSG, HBCOUNT and fall detection come from the raw bytes. +// 5.2 Heartbeat: Battery (percentage), RSSI (LoRa), GNSSSTATE (Status of the GNSS module), CHGSTATE (Status of +// battery charging). SNR and VIBSTATE come from the raw bytes. +// 5.3 GNSS Position: longitude, latitude and time come from the raw bytes. +// 5.4 Beacon: bytes 1-5 are RFU, the codec reads byte 1 as Vibstate. 5.5 Alarm: 0x01 SOS, 0x02 Fall Detection. +var LANSITEC_BADGE_KEYS = { + ADR: 'adr', 'Lora Txpower': 'loraTxPower', DR: 'dataRate', GNSSEN: 'gnssEnabled', + 'Position mode': 'positioningReportMode', 'Position period': 'bluetoothPositionReportInterval', + 'Heartbeat period': '-', 'Firmware version': '-', CFMMSG: '-', HBCOUNT: '-', 'Fall threshold': '-', + 'Battery level': 'batteryLevel', RSSI: 'loraRssi', SNR: '-', 'GPS state': 'gnssState', + 'Move state': '-', 'Charge state': 'chargeState', + Longitude: '-', Latitude: '-', Time: '-', Length: '-', Vibstate: '-', 'Alarm type': 'alarm' +}; + +// Temperature Humidity Sensor DataSheet 1.26, 2.2.1 Register: ADR, MODE (current working scheme), SMODE (data +// scheme supported), POWER (configured transmit power, dBm), DR, REPTIND (report H&T if the value is the same; +// the codec gives "false" for 0 = always report), TH (period of temperature and humidity report), CRC. +// 2.2.2 Periodical temperature and humidity: VER is the version of the message type, not the firmware; +// VOL (battery left, %), RSSI (downlink, detected by the sensor). SNR, temperature and humidity come from the raw bytes. +var LANSITEC_TEMPERATURE_HUMIDITY_KEYS = { + adr: 'adr', mode: 'mode', smode: 'smode', LoraTxpower: 'loraTxPower', dr: 'dataRate', repting: 'reptind', + th: 'temperatureHumidityReportPeriod', crc: '-', + ver: '-', vol: 'batteryLevel', rssi: 'loraRssi', snr: '-', temperature: '-', humidity: '-' +}; + +// Without a datasheet only these keys are renamed: the integration writes rssi, snr and firmwareVersion itself. +// chgstate: the SocketSync gateway has the compact gateway codec byte for byte, which reads CHGSTAT 0x50 as +// "Not charging" (Bluetooth Gateway DataSheet 2.27, 4.4.2), so it is dropped. number is its count of reported +// devices: the beacons list has them. +var LANSITEC_CLASH_KEYS = { + rssi: 'loraRssi', RSSI: 'loraRssi', snr: 'loraSnr', SNR: 'loraSnr', + firmwareVersion: 'deviceFirmwareVersion', 'Firmware version': 'deviceFirmwareVersion', + type: '-', 'Message type': '-', chgstate: '-', number: '-' +}; + +// Beacon keys: Devicetype1data1, Device1data, dev1major, dev1.major, pos1minor, asset1rssi... +var LANSITEC_BEACON_PREFIXES = ['devicetype1', 'devicetype2', 'devicetype3', 'device', 'dev', 'pos', 'asset']; +var LANSITEC_BEACON_FIELDS = ['data', 'major', 'minor', 'rssi', 'id']; + +function lansitecEndsWith(s, end) { + return s.length >= end.length && s.substring(s.length - end.length) == end; +} + +// Strips the unit. Periods become seconds: the codecs give most of them in seconds, a few in minutes. +function lansitecNumber(v) { + if (typeof v != 'string') { + return v; + } + for (var i = 0; i < LANSITEC_UNITS.length; i++) { + var u = LANSITEC_UNITS[i]; + if (lansitecEndsWith(v, u)) { + var n = v.substring(0, v.length - u.length).trim(); + if (/^-?[0-9]+(\.[0-9]+)?$/.test(n)) { + return u == 'min' ? parseFloat(n) * 60 : parseFloat(n); + } + } + } + return v; +} + +function lansitecLowerStart(part) { + var n = 0; + while (n < part.length && part.charAt(n) != part.charAt(n).toLowerCase()) { + n = n + 1; + } + if (n == part.length) { + return part.toLowerCase(); + } + if (n > 1) { + return part.substring(0, n - 1).toLowerCase() + part.substring(n - 1); + } + return part.substring(0, 1).toLowerCase() + part.substring(1); +} + +function lansitecCamelCase(key) { + var parts = key.split(/[ .]+/); + var result = lansitecLowerStart(parts[0]); + for (var i = 1; i < parts.length; i++) { + var part = lansitecLowerStart(parts[i]); + result = result + part.substring(0, 1).toUpperCase() + part.substring(1); + } + return result; +} + +function lansitecBeaconKey(key) { + var k = key.toLowerCase().replace('.', ''); + for (var i = 0; i < LANSITEC_BEACON_PREFIXES.length; i++) { + var p = LANSITEC_BEACON_PREFIXES[i]; + if (k.indexOf(p) == 0) { + var rest = k.substring(p.length); + for (var j = 0; j < LANSITEC_BEACON_FIELDS.length; j++) { + var f = LANSITEC_BEACON_FIELDS[j]; + if (lansitecEndsWith(rest, f) && /^[0-9]+$/.test(rest.substring(0, rest.length - f.length))) { + return { group: p, index: rest.substring(0, rest.length - f.length), field: f }; + } + if (rest.indexOf(f) == 0 && /^[0-9]+$/.test(rest.substring(f.length))) { + return { group: p, index: rest.substring(f.length), field: f }; + } + } + } + } + return null; +} + +function lansitecKeyTable(model) { + if (LANSITEC_GATEWAYS.indexOf(model) >= 0) { + return LANSITEC_GATEWAY_KEYS; + } + if (model == 'badge-tracker') { + return LANSITEC_BADGE_KEYS; + } + if (model == 'temperature-humidity-sensor') { + return LANSITEC_TEMPERATURE_HUMIDITY_KEYS; + } + return null; +} + +function lansitecValues(raw, model) { + var table = lansitecKeyTable(model); + var values = {}; + var beacons = {}; + Object.keys(raw).forEach(function (key) { + var value = raw[key]; + var b = lansitecBeaconKey(key); + if (b != null) { + var id = b.group + b.index; + if (beacons[id] == null) { + beacons[id] = { order: parseInt(b.index, 10) }; + } + if (b.field == 'rssi') { + beacons[id].rssi = lansitecNumber(value); + } else if (b.field == 'data' || b.field == 'major' || b.field == 'minor') { + beacons[id][b.field] = value; + } + } else if (table == null && key == 'time' && typeof value == 'string') { + // Container tracker and helmet sensor, no datasheet: their codecs add 8 hours to the GNSS time and wrap + // it in quotes, as the badge tracker codec does with the UTC time of DataSheet v5.30, 5.3. + values.gnssTime = new Date(value.replace(/"/g, '')).getTime() - 8 * 60 * 60 * 1000; + } else if (table != null && table.hasOwnProperty(key)) { + if (table[key] != '-') { + values[table[key]] = lansitecNumber(value); + } + } else if (LANSITEC_CLASH_KEYS.hasOwnProperty(key)) { + if (LANSITEC_CLASH_KEYS[key] != '-') { + values[LANSITEC_CLASH_KEYS[key]] = lansitecNumber(value); + } + } else { + values[lansitecCamelCase(key)] = lansitecNumber(value); + } + }); + if (Object.keys(beacons).length > 0) { + values.beacons = lansitecBeaconList(beacons); + } + return values; +} + +function lansitecBeaconList(beacons) { + var list = []; + var ids = Object.keys(beacons); + for (var i = 1; i <= ids.length; i++) { + ids.forEach(function (id) { + var b = beacons[id]; + if (b.order == i) { + var item = {}; + if (b.data != null) { + item.data = b.data; + } + if (b.major != null) { + item.major = b.major; + item.minor = b.minor; + } + item.rssi = b.rssi; + list.push(item); + } + }); + } + // The gateway codec reads the data of every device after the first at a 5-byte step, so it is right only + // for 4-byte data (DataSheet 4.4.4 examples): with longer data only the first device is kept. The gateways + // with a datasheet get their beacons from the raw bytes instead; this is for the SocketSync gateway. + if (list.length > 1 && list[0].data != null && list[0].data.length != 8) { + return [list[0]]; + } + return list; +} + +// Decoded from the raw bytes by the datasheet where the codec gets it wrong. +function lansitecRawValues(values, b, model) { + var type = (b[0] >> 4) & 0x0F; + if (LANSITEC_GATEWAYS.indexOf(model) >= 0) { + lansitecGatewayRawValues(values, b, model, type); + } else if (model == 'badge-tracker') { + lansitecBadgeRawValues(values, b, type); + } else if (model == 'temperature-humidity-sensor' && type == 2) { + lansitecTemperatureHumidityRawValues(values, b); + } +} + +function lansitecBase64ToBytes(input) { + var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; + var bytes = []; + var bits = 0; + var count = 0; + for (var i = 0; i < input.length; i++) { + var c = chars.indexOf(input.charAt(i)); + if (c < 0) { + continue; + } + bits = (bits << 6) | c; + count = count + 6; + if (count >= 8) { + count = count - 8; + bytes.push((bits >> count) & 0xFF); + } + } + return bytes; +} + +function lansitecHex(b, from, to) { + var digits = '0123456789ABCDEF'; + var hex = ''; + for (var i = from; i < to; i++) { + hex = hex + digits.charAt((b[i] >> 4) & 0x0F) + digits.charAt(b[i] & 0x0F); + } + return hex; +} + +function lansitecUint(b, offset, length) { + var v = 0; + for (var i = 0; i < length; i++) { + v = v * 256 + b[offset + i]; + } + return v; +} + +// IEEE 754 single precision, big-endian, rounded to 6 decimals (about 0.1 m) as in the TBEL converter. +function lansitecFloat(b, offset) { + var bits = lansitecUint(b, offset, 4); + var exponent = Math.floor(bits / 8388608) % 256; + var mantissa = bits % 8388608; + var value = exponent == 0 ? mantissa * Math.pow(2, -149) : (1 + mantissa / 8388608) * Math.pow(2, exponent - 127); + value = Math.round(value * 1000000) / 1000000; + return bits >= 2147483648 ? -value : value; +} + +// SNR, unit 0.01 dB, -3000 to 3000: signed. The codecs read it unsigned. +function lansitecSnr(b, offset) { + var v = lansitecUint(b, offset, 2); + if (v > 32767) { + v = v - 65536; + } + return v / 100; +} + +// Bluetooth Gateway DataSheet 2.27. +function lansitecGatewayRawValues(values, b, model, type) { + if (type == 1 && b.length >= 9) { + // 4.4.1: Network reconnection interval, unit 5 minutes. The codec does not decode it. + values.networkReconnectionInterval = b[8] * 300; + } else if (type == 2 && b.length >= 8) { + // 4.4.2: VOL is the battery voltage (VOL x 0.01 + 1.5 V) on the macro gateway and the battery level (%) on + // the others. The codec decides by the value (<= 100 is %), not by the model. + if (model == 'macro-bluetooth-gateway') { + values.batteryVoltage = (b[1] + 150) / 100; + } else { + values.batteryLevel = b[1]; + } + values.loraSnr = lansitecSnr(b, 3); + // CHGSTAT: 0x00 Not charging, 0x50 Charging, 0x60 Charging completed on the compact and solar gateway, + // always 0x00 on the macro and indoor gateway. The codec reads the low half of the byte. + if (model == 'compact-bluetooth-gateway' || model == 'solar-bluetooth-gateway') { + if (b[7] == 0x00) { + values.chargeState = 'Not charging'; + } else if (b[7] == 0x50) { + values.chargeState = 'Charging'; + } else if (b[7] == 0x60) { + values.chargeState = 'Charging completed'; + } + } + } else if (type == 0x8 || type == 0x9 || type == 0xA || type == 0xE) { + delete values.beacons; + var beacons = lansitecGatewayBeacons(b, type); + if (beacons != null) { + values.beacons = beacons; + } + } +} + +// 4.4.4-4.4.6: Device Type 1, 2 and 3 messages: Type (bits 3-0: number of devices), then Data (1-31 bytes) and +// RSSI (value - 256, dBm) of each device. Data length is not in the message: it is set by the device report rule +// of the type (4.4.3), so all devices of one message have the same length. The codec reads every device after +// the first at a 5-byte step. +// 4.4.7: Multi-Device Type message: Device Type ID before the data of each device. The data length can differ per +// type, and the message does not say it: decoded only when all devices fit the same length. +function lansitecGatewayBeacons(b, type) { + var count = b[0] & 0x0F; + if (count == 0 || (b.length - 1) % count != 0) { + return null; + } + var step = (b.length - 1) / count; + var head = type == 0xE ? 1 : 0; + var dataLength = step - head - 1; + if (dataLength < 1) { + return null; + } + var list = []; + for (var i = 0; i < count; i++) { + var start = 1 + i * step; + var deviceType = type - 7; + if (type == 0xE) { + deviceType = b[start]; + if (deviceType < 1 || deviceType > 3) { + return null; + } + } + list.push({ + deviceType: deviceType, + data: lansitecHex(b, start + head, start + head + dataLength), + rssi: b[start + step - 1] - 256 + }); + } + return list; +} + +// Badge Tracker DataSheet v5.30. +function lansitecBadgeRawValues(values, b, type) { + if (type == 1 && b.length == 14) { + // 5.1, 14 bytes: Type, SMODE, Power, Config, BLEPRI (2), GNSSPRI (2), Heartbeat, Version (2), CFMMSG, HBCOUNT, Fall. + // BLEEN (Config bit 0) is not decoded: the field table says BLE enable status and that GNSSEN or BLEEN must + // be enabled, but the example (Config 32) has both 0 and calls bit 0 reserved. + values.gnssPositionReportInterval = lansitecUint(b, 6, 2) * 5; + values.heartbeatInterval = b[8] * 30; + // Version 0x0525 is V5.25. + var major = lansitecHex(b, 9, 10); + if (major.charAt(0) == '0') { + major = major.substring(1); + } + values.deviceFirmwareVersion = 'V' + major + '.' + lansitecHex(b, 10, 11); + values.cfmmsg = b[11]; + values.hbcount = b[12]; + // Fall detection threshold, unit 0.5 m, 0 means off. + values.fallDetectionThreshold = b[13] * 0.5; + } else if (type == 2 && b.length >= 6) { + // 5.2: the SNR field is there since firmware 0x0501. The SNREN bit (Type bits 3-0) is not used: the + // datasheet example 20 64 3E 033E 00 60 00 has SNREN 0 and the SNR 8.3 dB. + values.loraSnr = lansitecSnr(b, 3); + // VIBSTATE: moving state 0-9, 0 means no moving, 9 high acceleration. The codec turns only 0 and 1 into text. + values.movingState = b[5] & 0x0F; + } else if (type == 3 && b.length >= 13) { + // 5.3: Flag (Type bits 3-0) 0x1 means GNSS positioning failed. Longitude and latitude are IEEE 754 floats + // (the codec divides the mantissa by 0x7fffff instead of 2^23), time is UTC seconds (the codec adds 8 hours). + if ((b[0] & 0x0F) == 0) { + values.longitude = lansitecFloat(b, 1); + values.latitude = lansitecFloat(b, 5); + values.gnssTime = lansitecUint(b, 9, 4) * 1000; + } + } +} + +// Temperature Humidity Sensor DataSheet 1.26, 2.2.2: VER (Type bits 3-0) 1 means the SNR field is there, 0 means +// it is not and the message is 8 bytes. The codec always reads the SNR, so with VER 0 the next fields are shifted. +// TEMP_INT bit 7 is the sign, TEMP_FRA the hundredths. +function lansitecTemperatureHumidityRawValues(values, b) { + var offset = 3; + if ((b[0] & 0x0F) == 1) { + values.loraSnr = lansitecSnr(b, 3); + offset = 5; + } + if (b.length < offset + 3) { + return; + } + var temperature = (b[offset] & 0x7F) + b[offset + 1] / 100; + if ((b[offset] & 0x80) != 0) { + temperature = -temperature; + } + values.temperature = Math.round(temperature * 100) / 100; + values.humidity = b[offset + 2]; +} + var values = {}; +var attributes = {}; var history = []; if (metadata.payloadFormat == 'JSON') { values = decodeToJson(payload); - if (Array.isArray(values.history)) { + if (metadata.brandId == 'lansitec' && values.bytes != null) { + // The Lansitec codecs put everything in "bytes" and get some fields wrong: see the Lansitec block above. + var raw = values.bytes; + values = lansitecValues(raw, metadata.modelId); + if (metadata.data != null) { + lansitecRawValues(values, lansitecBase64ToBytes(metadata.data), metadata.modelId); + } + // Register is sent after join and holds the device settings: they are attributes, not telemetry. + var messageType = raw.type != null ? raw.type : raw['Message type']; + if (messageType == 'Register') { + attributes = values; + values = {}; + } + } else if (Array.isArray(values.history)) { values.history.forEach(function (record) { if (record.timestamp != null) { var ts = record.timestamp * 1000; @@ -26,12 +443,16 @@ if (metadata.brandId != null && metadata.modelId != null) { profile = customProfile; } } +if (Object.keys(attributes).length > 0) { + return { profile: profile, attributes: attributes }; +} if (history.length == 0) { return { profile: profile, telemetry: { ts: metadata.ts, values: values } }; } history.push({ ts: metadata.ts, values: values }); return { profile: profile, telemetry: history }; +// The payload is UTF-8: a codec value can have a unit like ℃. function decodeToJson(payload) { - return JSON.parse(String.fromCharCode.apply(String, payload)); + return JSON.parse(decodeURIComponent(escape(String.fromCharCode.apply(String, payload)))); } diff --git a/ui-ngx/src/assets/converters/js-ttn-decoder.raw b/ui-ngx/src/assets/converters/js-ttn-decoder.raw index ee4314679b..ca1d911e66 100644 --- a/ui-ngx/src/assets/converters/js-ttn-decoder.raw +++ b/ui-ngx/src/assets/converters/js-ttn-decoder.raw @@ -2,11 +2,428 @@ // is then that JSON: its keys become telemetry. A Milesight-style history list keeps each record's time. // Such a device gets the profile -, so all devices of one model share it, // or the name the integration metadata gives it under profile.-. Any other device gets LoRaWAN Device. + +// Lansitec: the LoRaWAN Device Repository codecs wrap everything in "bytes", write numbers as strings with units +// ("85%", "-67dBm") and get some fields wrong. Key names follow the Lansitec datasheets where there are any: +// Bluetooth Gateway DataSheet 2.27, Badge Tracker DataSheet v5.30, Temperature Humidity Sensor DataSheet 1.26. +// The wrong fields are decoded from the raw bytes (metadata.data). Remove a fix when the Repository fixes the codec. +var LANSITEC_UNITS = ['dBm', 'dB', '%', 'V', 'sec', 'min', 's', '℃', 'level', ' meter']; + +var LANSITEC_GATEWAYS = ['compact-bluetooth-gateway', 'indoor-bluetooth-gateway', 'macro-bluetooth-gateway', 'solar-bluetooth-gateway']; + +// Codec key -> datasheet name, per model group. '-' drops the key. +// +// Bluetooth Gateway DataSheet 2.27, 4.4.1 Registration: ADR, Mode (current working LoRaWAN frequency sub-band plan), +// LoRaWAN frequency band, Power (LoRa TX power, dBm), Continuously BLE receiving, DR (LoRaWAN Uplink Data Rate), +// PRI (Position report interval), HB (Heartbeat interval), Bluetooth receiving duration. +// 4.4.2 Heartbeat: RSSI and SNR (LoRa), VER (Firmware version). VOL, SNR and CHGSTAT come from the raw bytes. +// number is the count of reported devices: the beacons list has them. The beacons come from the raw bytes too. +var LANSITEC_GATEWAY_KEYS = { + adr: 'adr', mode: 'mode', LoRaWANband: 'loraWanFrequencyBand', LoRaTxpower: 'loraTxPower', + ContinuousReceive: 'continuousBleReceiving', dr: 'dataRate', pos: 'positionReportInterval', + hb: 'heartbeatInterval', BLEReceivingDuration: 'bluetoothReceivingDuration', + rssi: 'loraRssi', snr: '-', ver: 'deviceFirmwareVersion', vol: '-', chgstate: '-', number: '-' +}; + +// Badge Tracker DataSheet v5.30, 5.1 Registration: ADR, LoRa TX power, DR, GNSSEN (GNSS enable status), +// POSMODE (Positioning report mode), BLEPRI (Bluetooth Position Report Interval). The codec reads it without the +// GNSSPRI field, so heartbeat, version, CFMMSG, HBCOUNT and fall detection come from the raw bytes. +// 5.2 Heartbeat: Battery (percentage), RSSI (LoRa), GNSSSTATE (Status of the GNSS module), CHGSTATE (Status of +// battery charging). SNR and VIBSTATE come from the raw bytes. +// 5.3 GNSS Position: longitude, latitude and time come from the raw bytes. +// 5.4 Beacon: bytes 1-5 are RFU, the codec reads byte 1 as Vibstate. 5.5 Alarm: 0x01 SOS, 0x02 Fall Detection. +var LANSITEC_BADGE_KEYS = { + ADR: 'adr', 'Lora Txpower': 'loraTxPower', DR: 'dataRate', GNSSEN: 'gnssEnabled', + 'Position mode': 'positioningReportMode', 'Position period': 'bluetoothPositionReportInterval', + 'Heartbeat period': '-', 'Firmware version': '-', CFMMSG: '-', HBCOUNT: '-', 'Fall threshold': '-', + 'Battery level': 'batteryLevel', RSSI: 'loraRssi', SNR: '-', 'GPS state': 'gnssState', + 'Move state': '-', 'Charge state': 'chargeState', + Longitude: '-', Latitude: '-', Time: '-', Length: '-', Vibstate: '-', 'Alarm type': 'alarm' +}; + +// Temperature Humidity Sensor DataSheet 1.26, 2.2.1 Register: ADR, MODE (current working scheme), SMODE (data +// scheme supported), POWER (configured transmit power, dBm), DR, REPTIND (report H&T if the value is the same; +// the codec gives "false" for 0 = always report), TH (period of temperature and humidity report), CRC. +// 2.2.2 Periodical temperature and humidity: VER is the version of the message type, not the firmware; +// VOL (battery left, %), RSSI (downlink, detected by the sensor). SNR, temperature and humidity come from the raw bytes. +var LANSITEC_TEMPERATURE_HUMIDITY_KEYS = { + adr: 'adr', mode: 'mode', smode: 'smode', LoraTxpower: 'loraTxPower', dr: 'dataRate', repting: 'reptind', + th: 'temperatureHumidityReportPeriod', crc: '-', + ver: '-', vol: 'batteryLevel', rssi: 'loraRssi', snr: '-', temperature: '-', humidity: '-' +}; + +// Without a datasheet only these keys are renamed: the integration writes rssi, snr and firmwareVersion itself. +// chgstate: the SocketSync gateway has the compact gateway codec byte for byte, which reads CHGSTAT 0x50 as +// "Not charging" (Bluetooth Gateway DataSheet 2.27, 4.4.2), so it is dropped. number is its count of reported +// devices: the beacons list has them. +var LANSITEC_CLASH_KEYS = { + rssi: 'loraRssi', RSSI: 'loraRssi', snr: 'loraSnr', SNR: 'loraSnr', + firmwareVersion: 'deviceFirmwareVersion', 'Firmware version': 'deviceFirmwareVersion', + type: '-', 'Message type': '-', chgstate: '-', number: '-' +}; + +// Beacon keys: Devicetype1data1, Device1data, dev1major, dev1.major, pos1minor, asset1rssi... +var LANSITEC_BEACON_PREFIXES = ['devicetype1', 'devicetype2', 'devicetype3', 'device', 'dev', 'pos', 'asset']; +var LANSITEC_BEACON_FIELDS = ['data', 'major', 'minor', 'rssi', 'id']; + +function lansitecEndsWith(s, end) { + return s.length >= end.length && s.substring(s.length - end.length) == end; +} + +// Strips the unit. Periods become seconds: the codecs give most of them in seconds, a few in minutes. +function lansitecNumber(v) { + if (typeof v != 'string') { + return v; + } + for (var i = 0; i < LANSITEC_UNITS.length; i++) { + var u = LANSITEC_UNITS[i]; + if (lansitecEndsWith(v, u)) { + var n = v.substring(0, v.length - u.length).trim(); + if (/^-?[0-9]+(\.[0-9]+)?$/.test(n)) { + return u == 'min' ? parseFloat(n) * 60 : parseFloat(n); + } + } + } + return v; +} + +function lansitecLowerStart(part) { + var n = 0; + while (n < part.length && part.charAt(n) != part.charAt(n).toLowerCase()) { + n = n + 1; + } + if (n == part.length) { + return part.toLowerCase(); + } + if (n > 1) { + return part.substring(0, n - 1).toLowerCase() + part.substring(n - 1); + } + return part.substring(0, 1).toLowerCase() + part.substring(1); +} + +function lansitecCamelCase(key) { + var parts = key.split(/[ .]+/); + var result = lansitecLowerStart(parts[0]); + for (var i = 1; i < parts.length; i++) { + var part = lansitecLowerStart(parts[i]); + result = result + part.substring(0, 1).toUpperCase() + part.substring(1); + } + return result; +} + +function lansitecBeaconKey(key) { + var k = key.toLowerCase().replace('.', ''); + for (var i = 0; i < LANSITEC_BEACON_PREFIXES.length; i++) { + var p = LANSITEC_BEACON_PREFIXES[i]; + if (k.indexOf(p) == 0) { + var rest = k.substring(p.length); + for (var j = 0; j < LANSITEC_BEACON_FIELDS.length; j++) { + var f = LANSITEC_BEACON_FIELDS[j]; + if (lansitecEndsWith(rest, f) && /^[0-9]+$/.test(rest.substring(0, rest.length - f.length))) { + return { group: p, index: rest.substring(0, rest.length - f.length), field: f }; + } + if (rest.indexOf(f) == 0 && /^[0-9]+$/.test(rest.substring(f.length))) { + return { group: p, index: rest.substring(f.length), field: f }; + } + } + } + } + return null; +} + +function lansitecKeyTable(model) { + if (LANSITEC_GATEWAYS.indexOf(model) >= 0) { + return LANSITEC_GATEWAY_KEYS; + } + if (model == 'badge-tracker') { + return LANSITEC_BADGE_KEYS; + } + if (model == 'temperature-humidity-sensor') { + return LANSITEC_TEMPERATURE_HUMIDITY_KEYS; + } + return null; +} + +function lansitecValues(raw, model) { + var table = lansitecKeyTable(model); + var values = {}; + var beacons = {}; + Object.keys(raw).forEach(function (key) { + var value = raw[key]; + var b = lansitecBeaconKey(key); + if (b != null) { + var id = b.group + b.index; + if (beacons[id] == null) { + beacons[id] = { order: parseInt(b.index, 10) }; + } + if (b.field == 'rssi') { + beacons[id].rssi = lansitecNumber(value); + } else if (b.field == 'data' || b.field == 'major' || b.field == 'minor') { + beacons[id][b.field] = value; + } + } else if (table == null && key == 'time' && typeof value == 'string') { + // Container tracker and helmet sensor, no datasheet: their codecs add 8 hours to the GNSS time and wrap + // it in quotes, as the badge tracker codec does with the UTC time of DataSheet v5.30, 5.3. + values.gnssTime = new Date(value.replace(/"/g, '')).getTime() - 8 * 60 * 60 * 1000; + } else if (table != null && table.hasOwnProperty(key)) { + if (table[key] != '-') { + values[table[key]] = lansitecNumber(value); + } + } else if (LANSITEC_CLASH_KEYS.hasOwnProperty(key)) { + if (LANSITEC_CLASH_KEYS[key] != '-') { + values[LANSITEC_CLASH_KEYS[key]] = lansitecNumber(value); + } + } else { + values[lansitecCamelCase(key)] = lansitecNumber(value); + } + }); + if (Object.keys(beacons).length > 0) { + values.beacons = lansitecBeaconList(beacons); + } + return values; +} + +function lansitecBeaconList(beacons) { + var list = []; + var ids = Object.keys(beacons); + for (var i = 1; i <= ids.length; i++) { + ids.forEach(function (id) { + var b = beacons[id]; + if (b.order == i) { + var item = {}; + if (b.data != null) { + item.data = b.data; + } + if (b.major != null) { + item.major = b.major; + item.minor = b.minor; + } + item.rssi = b.rssi; + list.push(item); + } + }); + } + // The gateway codec reads the data of every device after the first at a 5-byte step, so it is right only + // for 4-byte data (DataSheet 4.4.4 examples): with longer data only the first device is kept. The gateways + // with a datasheet get their beacons from the raw bytes instead; this is for the SocketSync gateway. + if (list.length > 1 && list[0].data != null && list[0].data.length != 8) { + return [list[0]]; + } + return list; +} + +// Decoded from the raw bytes by the datasheet where the codec gets it wrong. +function lansitecRawValues(values, b, model) { + var type = (b[0] >> 4) & 0x0F; + if (LANSITEC_GATEWAYS.indexOf(model) >= 0) { + lansitecGatewayRawValues(values, b, model, type); + } else if (model == 'badge-tracker') { + lansitecBadgeRawValues(values, b, type); + } else if (model == 'temperature-humidity-sensor' && type == 2) { + lansitecTemperatureHumidityRawValues(values, b); + } +} + +function lansitecBase64ToBytes(input) { + var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; + var bytes = []; + var bits = 0; + var count = 0; + for (var i = 0; i < input.length; i++) { + var c = chars.indexOf(input.charAt(i)); + if (c < 0) { + continue; + } + bits = (bits << 6) | c; + count = count + 6; + if (count >= 8) { + count = count - 8; + bytes.push((bits >> count) & 0xFF); + } + } + return bytes; +} + +function lansitecHex(b, from, to) { + var digits = '0123456789ABCDEF'; + var hex = ''; + for (var i = from; i < to; i++) { + hex = hex + digits.charAt((b[i] >> 4) & 0x0F) + digits.charAt(b[i] & 0x0F); + } + return hex; +} + +function lansitecUint(b, offset, length) { + var v = 0; + for (var i = 0; i < length; i++) { + v = v * 256 + b[offset + i]; + } + return v; +} + +// IEEE 754 single precision, big-endian, rounded to 6 decimals (about 0.1 m) as in the TBEL converter. +function lansitecFloat(b, offset) { + var bits = lansitecUint(b, offset, 4); + var exponent = Math.floor(bits / 8388608) % 256; + var mantissa = bits % 8388608; + var value = exponent == 0 ? mantissa * Math.pow(2, -149) : (1 + mantissa / 8388608) * Math.pow(2, exponent - 127); + value = Math.round(value * 1000000) / 1000000; + return bits >= 2147483648 ? -value : value; +} + +// SNR, unit 0.01 dB, -3000 to 3000: signed. The codecs read it unsigned. +function lansitecSnr(b, offset) { + var v = lansitecUint(b, offset, 2); + if (v > 32767) { + v = v - 65536; + } + return v / 100; +} + +// Bluetooth Gateway DataSheet 2.27. +function lansitecGatewayRawValues(values, b, model, type) { + if (type == 1 && b.length >= 9) { + // 4.4.1: Network reconnection interval, unit 5 minutes. The codec does not decode it. + values.networkReconnectionInterval = b[8] * 300; + } else if (type == 2 && b.length >= 8) { + // 4.4.2: VOL is the battery voltage (VOL x 0.01 + 1.5 V) on the macro gateway and the battery level (%) on + // the others. The codec decides by the value (<= 100 is %), not by the model. + if (model == 'macro-bluetooth-gateway') { + values.batteryVoltage = (b[1] + 150) / 100; + } else { + values.batteryLevel = b[1]; + } + values.loraSnr = lansitecSnr(b, 3); + // CHGSTAT: 0x00 Not charging, 0x50 Charging, 0x60 Charging completed on the compact and solar gateway, + // always 0x00 on the macro and indoor gateway. The codec reads the low half of the byte. + if (model == 'compact-bluetooth-gateway' || model == 'solar-bluetooth-gateway') { + if (b[7] == 0x00) { + values.chargeState = 'Not charging'; + } else if (b[7] == 0x50) { + values.chargeState = 'Charging'; + } else if (b[7] == 0x60) { + values.chargeState = 'Charging completed'; + } + } + } else if (type == 0x8 || type == 0x9 || type == 0xA || type == 0xE) { + delete values.beacons; + var beacons = lansitecGatewayBeacons(b, type); + if (beacons != null) { + values.beacons = beacons; + } + } +} + +// 4.4.4-4.4.6: Device Type 1, 2 and 3 messages: Type (bits 3-0: number of devices), then Data (1-31 bytes) and +// RSSI (value - 256, dBm) of each device. Data length is not in the message: it is set by the device report rule +// of the type (4.4.3), so all devices of one message have the same length. The codec reads every device after +// the first at a 5-byte step. +// 4.4.7: Multi-Device Type message: Device Type ID before the data of each device. The data length can differ per +// type, and the message does not say it: decoded only when all devices fit the same length. +function lansitecGatewayBeacons(b, type) { + var count = b[0] & 0x0F; + if (count == 0 || (b.length - 1) % count != 0) { + return null; + } + var step = (b.length - 1) / count; + var head = type == 0xE ? 1 : 0; + var dataLength = step - head - 1; + if (dataLength < 1) { + return null; + } + var list = []; + for (var i = 0; i < count; i++) { + var start = 1 + i * step; + var deviceType = type - 7; + if (type == 0xE) { + deviceType = b[start]; + if (deviceType < 1 || deviceType > 3) { + return null; + } + } + list.push({ + deviceType: deviceType, + data: lansitecHex(b, start + head, start + head + dataLength), + rssi: b[start + step - 1] - 256 + }); + } + return list; +} + +// Badge Tracker DataSheet v5.30. +function lansitecBadgeRawValues(values, b, type) { + if (type == 1 && b.length == 14) { + // 5.1, 14 bytes: Type, SMODE, Power, Config, BLEPRI (2), GNSSPRI (2), Heartbeat, Version (2), CFMMSG, HBCOUNT, Fall. + // BLEEN (Config bit 0) is not decoded: the field table says BLE enable status and that GNSSEN or BLEEN must + // be enabled, but the example (Config 32) has both 0 and calls bit 0 reserved. + values.gnssPositionReportInterval = lansitecUint(b, 6, 2) * 5; + values.heartbeatInterval = b[8] * 30; + // Version 0x0525 is V5.25. + var major = lansitecHex(b, 9, 10); + if (major.charAt(0) == '0') { + major = major.substring(1); + } + values.deviceFirmwareVersion = 'V' + major + '.' + lansitecHex(b, 10, 11); + values.cfmmsg = b[11]; + values.hbcount = b[12]; + // Fall detection threshold, unit 0.5 m, 0 means off. + values.fallDetectionThreshold = b[13] * 0.5; + } else if (type == 2 && b.length >= 6) { + // 5.2: the SNR field is there since firmware 0x0501. The SNREN bit (Type bits 3-0) is not used: the + // datasheet example 20 64 3E 033E 00 60 00 has SNREN 0 and the SNR 8.3 dB. + values.loraSnr = lansitecSnr(b, 3); + // VIBSTATE: moving state 0-9, 0 means no moving, 9 high acceleration. The codec turns only 0 and 1 into text. + values.movingState = b[5] & 0x0F; + } else if (type == 3 && b.length >= 13) { + // 5.3: Flag (Type bits 3-0) 0x1 means GNSS positioning failed. Longitude and latitude are IEEE 754 floats + // (the codec divides the mantissa by 0x7fffff instead of 2^23), time is UTC seconds (the codec adds 8 hours). + if ((b[0] & 0x0F) == 0) { + values.longitude = lansitecFloat(b, 1); + values.latitude = lansitecFloat(b, 5); + values.gnssTime = lansitecUint(b, 9, 4) * 1000; + } + } +} + +// Temperature Humidity Sensor DataSheet 1.26, 2.2.2: VER (Type bits 3-0) 1 means the SNR field is there, 0 means +// it is not and the message is 8 bytes. The codec always reads the SNR, so with VER 0 the next fields are shifted. +// TEMP_INT bit 7 is the sign, TEMP_FRA the hundredths. +function lansitecTemperatureHumidityRawValues(values, b) { + var offset = 3; + if ((b[0] & 0x0F) == 1) { + values.loraSnr = lansitecSnr(b, 3); + offset = 5; + } + if (b.length < offset + 3) { + return; + } + var temperature = (b[offset] & 0x7F) + b[offset + 1] / 100; + if ((b[offset] & 0x80) != 0) { + temperature = -temperature; + } + values.temperature = Math.round(temperature * 100) / 100; + values.humidity = b[offset + 2]; +} + var values = {}; +var attributes = {}; var history = []; if (metadata.payloadFormat == 'JSON') { values = decodeToJson(payload); - if (Array.isArray(values.history)) { + if (metadata.brandId == 'lansitec' && values.bytes != null) { + // The Lansitec codecs put everything in "bytes" and get some fields wrong: see the Lansitec block above. + var raw = values.bytes; + values = lansitecValues(raw, metadata.modelId); + if (metadata.data != null) { + lansitecRawValues(values, lansitecBase64ToBytes(metadata.data), metadata.modelId); + } + // Register is sent after join and holds the device settings: they are attributes, not telemetry. + var messageType = raw.type != null ? raw.type : raw['Message type']; + if (messageType == 'Register') { + attributes = values; + values = {}; + } + } else if (Array.isArray(values.history)) { values.history.forEach(function (record) { if (record.timestamp != null) { var ts = record.timestamp * 1000; @@ -26,12 +443,16 @@ if (metadata.brandId != null && metadata.modelId != null) { profile = customProfile; } } +if (Object.keys(attributes).length > 0) { + return { profile: profile, attributes: attributes }; +} if (history.length == 0) { return { profile: profile, telemetry: { ts: metadata.ts, values: values } }; } history.push({ ts: metadata.ts, values: values }); return { profile: profile, telemetry: history }; +// The payload is UTF-8: a codec value can have a unit like ℃. function decodeToJson(payload) { - return JSON.parse(String.fromCharCode.apply(String, payload)); + return JSON.parse(decodeURIComponent(escape(String.fromCharCode.apply(String, payload)))); } diff --git a/ui-ngx/src/assets/converters/tbel-tti-decoder.raw b/ui-ngx/src/assets/converters/tbel-tti-decoder.raw index 1cfef4546d..2e675de454 100644 --- a/ui-ngx/src/assets/converters/tbel-tti-decoder.raw +++ b/ui-ngx/src/assets/converters/tbel-tti-decoder.raw @@ -2,11 +2,384 @@ // is then that JSON: its keys become telemetry. A Milesight-style history list keeps each record's time. // Such a device gets the profile -, so all devices of one model share it, // or the name the integration metadata gives it under profile.-. Any other device gets LoRaWAN Device. + +// Lansitec: the LoRaWAN Device Repository codecs wrap everything in "bytes", write numbers as strings with units +// ("85%", "-67dBm") and get some fields wrong. Key names follow the Lansitec datasheets where there are any: +// Bluetooth Gateway DataSheet 2.27, Badge Tracker DataSheet v5.30, Temperature Humidity Sensor DataSheet 1.26. +// The wrong fields are decoded from the raw bytes (metadata.data). Remove a fix when the Repository fixes the codec. +var LANSITEC_UNITS = ["dBm", "dB", "%", "V", "sec", "min", "s", "℃", "level", " meter"]; + +var LANSITEC_GATEWAYS = ["compact-bluetooth-gateway", "indoor-bluetooth-gateway", "macro-bluetooth-gateway", "solar-bluetooth-gateway"]; + +// Codec key -> datasheet name, per model group. "-" drops the key. +// +// Bluetooth Gateway DataSheet 2.27, 4.4.1 Registration: ADR, Mode (current working LoRaWAN frequency sub-band plan), +// LoRaWAN frequency band, Power (LoRa TX power, dBm), Continuously BLE receiving, DR (LoRaWAN Uplink Data Rate), +// PRI (Position report interval), HB (Heartbeat interval), Bluetooth receiving duration. +// 4.4.2 Heartbeat: RSSI and SNR (LoRa), VER (Firmware version). VOL, SNR and CHGSTAT come from the raw bytes. +// number is the count of reported devices: the beacons list has them. The beacons come from the raw bytes too. +var LANSITEC_GATEWAY_KEYS = { + adr: "adr", mode: "mode", LoRaWANband: "loraWanFrequencyBand", LoRaTxpower: "loraTxPower", + ContinuousReceive: "continuousBleReceiving", dr: "dataRate", pos: "positionReportInterval", + hb: "heartbeatInterval", BLEReceivingDuration: "bluetoothReceivingDuration", + rssi: "loraRssi", snr: "-", ver: "deviceFirmwareVersion", vol: "-", chgstate: "-", number: "-" +}; + +// Badge Tracker DataSheet v5.30, 5.1 Registration: ADR, LoRa TX power, DR, GNSSEN (GNSS enable status), +// POSMODE (Positioning report mode), BLEPRI (Bluetooth Position Report Interval). The codec reads it without the +// GNSSPRI field, so heartbeat, version, CFMMSG, HBCOUNT and fall detection come from the raw bytes. +// 5.2 Heartbeat: Battery (percentage), RSSI (LoRa), GNSSSTATE (Status of the GNSS module), CHGSTATE (Status of +// battery charging). SNR and VIBSTATE come from the raw bytes. +// 5.3 GNSS Position: longitude, latitude and time come from the raw bytes. +// 5.4 Beacon: bytes 1-5 are RFU, the codec reads byte 1 as Vibstate. 5.5 Alarm: 0x01 SOS, 0x02 Fall Detection. +var LANSITEC_BADGE_KEYS = { + ADR: "adr", "Lora Txpower": "loraTxPower", DR: "dataRate", GNSSEN: "gnssEnabled", + "Position mode": "positioningReportMode", "Position period": "bluetoothPositionReportInterval", + "Heartbeat period": "-", "Firmware version": "-", CFMMSG: "-", HBCOUNT: "-", "Fall threshold": "-", + "Battery level": "batteryLevel", RSSI: "loraRssi", SNR: "-", "GPS state": "gnssState", + "Move state": "-", "Charge state": "chargeState", + Longitude: "-", Latitude: "-", Time: "-", Length: "-", Vibstate: "-", "Alarm type": "alarm" +}; + +// Temperature Humidity Sensor DataSheet 1.26, 2.2.1 Register: ADR, MODE (current working scheme), SMODE (data +// scheme supported), POWER (configured transmit power, dBm), DR, REPTIND (report H&T if the value is the same; +// the codec gives "false" for 0 = always report), TH (period of temperature and humidity report), CRC. +// 2.2.2 Periodical temperature and humidity: VER is the version of the message type, not the firmware; +// VOL (battery left, %), RSSI (downlink, detected by the sensor). SNR, temperature and humidity come from the raw bytes. +var LANSITEC_TEMPERATURE_HUMIDITY_KEYS = { + adr: "adr", mode: "mode", smode: "smode", LoraTxpower: "loraTxPower", dr: "dataRate", repting: "reptind", + th: "temperatureHumidityReportPeriod", crc: "-", + ver: "-", vol: "batteryLevel", rssi: "loraRssi", snr: "-", temperature: "-", humidity: "-" +}; + +// Without a datasheet only these keys are renamed: the integration writes rssi, snr and firmwareVersion itself. +// chgstate: the SocketSync gateway has the compact gateway codec byte for byte, which reads CHGSTAT 0x50 as +// "Not charging" (Bluetooth Gateway DataSheet 2.27, 4.4.2), so it is dropped. number is its count of reported +// devices: the beacons list has them. +var LANSITEC_CLASH_KEYS = { + rssi: "loraRssi", RSSI: "loraRssi", snr: "loraSnr", SNR: "loraSnr", + firmwareVersion: "deviceFirmwareVersion", "Firmware version": "deviceFirmwareVersion", + type: "-", "Message type": "-", chgstate: "-", number: "-" +}; + +// Strips the unit. Periods become seconds: the codecs give most of them in seconds, a few in minutes. +function lansitecNumber(v) { + if (!(v instanceof String)) { + return v; + } + foreach (u : LANSITEC_UNITS) { + if (v.endsWith(u)) { + var n = v.substring(0, v.length() - u.length()).trim(); + if (n.matches("-?[0-9]+(\\.[0-9]+)?")) { + return u == "min" ? parseDouble(n) * 60 : parseDouble(n); + } + } + } + return v; +} + +function lansitecLowerStart(part) { + var n = 0; + while (n < part.length() && part.substring(n, n + 1) != part.substring(n, n + 1).toLowerCase()) { + n = n + 1; + } + if (n == part.length()) { + return part.toLowerCase(); + } + if (n > 1) { + return part.substring(0, n - 1).toLowerCase() + part.substring(n - 1); + } + return part.substring(0, 1).toLowerCase() + part.substring(1); +} + +function lansitecCamelCase(key) { + var parts = key.split("[ .]+"); + var result = lansitecLowerStart(parts[0]); + for (var i = 1; i < parts.length; i++) { + var part = lansitecLowerStart(parts[i]); + result = result + part.substring(0, 1).toUpperCase() + part.substring(1); + } + return result; +} + +// Beacon keys: Devicetype1data1, Device1data, dev1major, dev1.major, pos1minor, asset1rssi... +var LANSITEC_BEACON_PREFIXES = ["devicetype1", "devicetype2", "devicetype3", "device", "dev", "pos", "asset"]; +var LANSITEC_BEACON_FIELDS = ["data", "major", "minor", "rssi", "id"]; + +function lansitecBeaconKey(key) { + var k = key.toLowerCase().replace(".", ""); + foreach (p : LANSITEC_BEACON_PREFIXES) { + if (k.startsWith(p)) { + var rest = k.substring(p.length()); + foreach (f : LANSITEC_BEACON_FIELDS) { + if (rest.endsWith(f) && rest.substring(0, rest.length() - f.length()).matches("[0-9]+")) { + return { group: p, index: rest.substring(0, rest.length() - f.length()), field: f }; + } + if (rest.startsWith(f) && rest.substring(f.length()).matches("[0-9]+")) { + return { group: p, index: rest.substring(f.length()), field: f }; + } + } + } + } + return null; +} + +function lansitecKeyTable(model) { + if (LANSITEC_GATEWAYS.contains(model)) { + return LANSITEC_GATEWAY_KEYS; + } + if (model == "badge-tracker") { + return LANSITEC_BADGE_KEYS; + } + if (model == "temperature-humidity-sensor") { + return LANSITEC_TEMPERATURE_HUMIDITY_KEYS; + } + return null; +} + +function lansitecValues(raw, model) { + var table = lansitecKeyTable(model); + var values = {}; + var beacons = {}; + foreach (key : raw.keySet()) { + var value = raw[key]; + var b = lansitecBeaconKey(key); + if (b != null) { + var id = b.group + b.index; + if (beacons[id] == null) { + beacons[id] = { order: parseInt(b.index) }; + } + if (b.field == "rssi") { + beacons[id].rssi = lansitecNumber(value); + } else if (b.field == "data" || b.field == "major" || b.field == "minor") { + beacons[id][b.field] = value; + } + } else if (table == null && key == "time" && value instanceof String) { + // Container tracker and helmet sensor, no datasheet: their codecs add 8 hours to the GNSS time and wrap + // it in quotes, as the badge tracker codec does with the UTC time of DataSheet v5.30, 5.3. + var gnssDate = new Date(value.replace("\"", "")); + values.gnssTime = gnssDate.getTime() - 8 * 60 * 60 * 1000; + } else if (table != null && table.containsKey(key)) { + var tableName = table[key]; + if (tableName != "-") { + values[tableName] = lansitecNumber(value); + } + } else if (LANSITEC_CLASH_KEYS.containsKey(key)) { + var clashName = LANSITEC_CLASH_KEYS[key]; + if (clashName != "-") { + values[clashName] = lansitecNumber(value); + } + } else { + var codecName = lansitecCamelCase(key); + values[codecName] = lansitecNumber(value); + } + } + if (!beacons.isEmpty()) { + values.beacons = lansitecBeaconList(beacons); + } + return values; +} + +function lansitecBeaconList(beacons) { + var list = []; + for (var i = 1; i <= beacons.size(); i++) { + foreach (id : beacons.keySet()) { + var b = beacons[id]; + if (b.order == i) { + var item = {}; + if (b.data != null) { + item.data = b.data; + } + if (b.major != null) { + item.major = b.major; + item.minor = b.minor; + } + item.rssi = b.rssi; + list.add(item); + } + } + } + // The gateway codec reads the data of every device after the first at a 5-byte step, so it is right only + // for 4-byte data (DataSheet 4.4.4 examples): with longer data only the first device is kept. The gateways + // with a datasheet get their beacons from the raw bytes instead; this is for the SocketSync gateway. + if (list.size() > 1 && list[0].data != null && list[0].data.length() != 8) { + var first = []; + first.add(list[0]); + return first; + } + return list; +} + +// Decoded from the raw bytes by the datasheet where the codec gets it wrong. +function lansitecRawValues(values, b, model) { + var type = (b[0] >> 4) & 0x0F; + if (LANSITEC_GATEWAYS.contains(model)) { + lansitecGatewayRawValues(values, b, model, type); + } else if (model == "badge-tracker") { + lansitecBadgeRawValues(values, b, type); + } else if (model == "temperature-humidity-sensor" && type == 2) { + lansitecTemperatureHumidityRawValues(values, b); + } +} + +// SNR, unit 0.01 dB, -3000 to 3000: signed. The codecs read it unsigned. +function lansitecSnr(b, offset) { + var v = parseBytesToInt(b, offset, 2, true); + if (v > 32767) { + v = v - 65536; + } + return v / 100.0; +} + +// Bluetooth Gateway DataSheet 2.27. +function lansitecGatewayRawValues(values, b, model, type) { + if (type == 1 && b.length >= 9) { + // 4.4.1: Network reconnection interval, unit 5 minutes. The codec does not decode it. + values.networkReconnectionInterval = (b[8] & 0xFF) * 300; + } else if (type == 2 && b.length >= 8) { + // 4.4.2: VOL is the battery voltage (VOL x 0.01 + 1.5 V) on the macro gateway and the battery level (%) on + // the others. The codec decides by the value (<= 100 is %), not by the model. + if (model == "macro-bluetooth-gateway") { + values.batteryVoltage = ((b[1] & 0xFF) + 150) / 100.0; + } else { + values.batteryLevel = b[1] & 0xFF; + } + values.loraSnr = lansitecSnr(b, 3); + // CHGSTAT: 0x00 Not charging, 0x50 Charging, 0x60 Charging completed on the compact and solar gateway, + // always 0x00 on the macro and indoor gateway. The codec reads the low half of the byte. + if (model == "compact-bluetooth-gateway" || model == "solar-bluetooth-gateway") { + var charge = b[7] & 0xFF; + if (charge == 0x00) { + values.chargeState = "Not charging"; + } else if (charge == 0x50) { + values.chargeState = "Charging"; + } else if (charge == 0x60) { + values.chargeState = "Charging completed"; + } + } + } else if (type == 0x8 || type == 0x9 || type == 0xA || type == 0xE) { + values.remove("beacons"); + var beacons = lansitecGatewayBeacons(b, type); + if (beacons != null) { + values.beacons = beacons; + } + } +} + +// 4.4.4-4.4.6: Device Type 1, 2 and 3 messages: Type (bits 3-0: number of devices), then Data (1-31 bytes) and +// RSSI (value - 256, dBm) of each device. Data length is not in the message: it is set by the device report rule +// of the type (4.4.3), so all devices of one message have the same length. The codec reads every device after +// the first at a 5-byte step. +// 4.4.7: Multi-Device Type message: Device Type ID before the data of each device. The data length can differ per +// type, and the message does not say it: decoded only when all devices fit the same length. +function lansitecGatewayBeacons(b, type) { + var count = b[0] & 0x0F; + if (count == 0 || (b.length - 1) % count != 0) { + return null; + } + // TBEL division gives a Double: array indexes need an int. + var step = ((b.length - 1) / count).intValue(); + var head = type == 0xE ? 1 : 0; + var dataLength = step - head - 1; + if (dataLength < 1) { + return null; + } + var hex = bytesToHex(b); + var list = []; + for (var i = 0; i < count; i++) { + var start = 1 + i * step; + var deviceType = type - 7; + if (type == 0xE) { + deviceType = b[start] & 0xFF; + if (deviceType < 1 || deviceType > 3) { + return null; + } + } + var item = {}; + item.deviceType = deviceType; + item.data = hex.substring(2 * (start + head), 2 * (start + head + dataLength)); + item.rssi = (b[start + step - 1] & 0xFF) - 256; + list.add(item); + } + return list; +} + +// Badge Tracker DataSheet v5.30. +function lansitecBadgeRawValues(values, b, type) { + if (type == 1 && b.length == 14) { + // 5.1, 14 bytes: Type, SMODE, Power, Config, BLEPRI (2), GNSSPRI (2), Heartbeat, Version (2), CFMMSG, HBCOUNT, Fall. + // BLEEN (Config bit 0) is not decoded: the field table says BLE enable status and that GNSSEN or BLEEN must + // be enabled, but the example (Config 32) has both 0 and calls bit 0 reserved. + values.gnssPositionReportInterval = parseBytesToInt(b, 6, 2, true) * 5; + values.heartbeatInterval = (b[8] & 0xFF) * 30; + // Version 0x0525 is V5.25. + var version = bytesToHex(b).substring(18, 22); + var major = version.substring(0, 2); + if (major.startsWith("0")) { + major = major.substring(1); + } + values.deviceFirmwareVersion = "V" + major + "." + version.substring(2); + values.cfmmsg = b[11] & 0xFF; + values.hbcount = b[12] & 0xFF; + // Fall detection threshold, unit 0.5 m, 0 means off. + values.fallDetectionThreshold = (b[13] & 0xFF) * 0.5; + } else if (type == 2 && b.length >= 6) { + // 5.2: the SNR field is there since firmware 0x0501. The SNREN bit (Type bits 3-0) is not used: the + // datasheet example 20 64 3E 033E 00 60 00 has SNREN 0 and the SNR 8.3 dB. + values.loraSnr = lansitecSnr(b, 3); + // VIBSTATE: moving state 0-9, 0 means no moving, 9 high acceleration. The codec turns only 0 and 1 into text. + values.movingState = b[5] & 0x0F; + } else if (type == 3 && b.length >= 13) { + // 5.3: Flag (Type bits 3-0) 0x1 means GNSS positioning failed. Longitude and latitude are IEEE 754 floats + // (the codec divides the mantissa by 0x7fffff instead of 2^23), time is UTC seconds (the codec adds 8 hours). + // Rounded to 6 decimals (about 0.1 m), as a double: the float has no more precision. + if ((b[0] & 0x0F) == 0) { + values.longitude = toFixed(parseBytesToFloat(b, 1, 4, true) * 1.0, 6); + values.latitude = toFixed(parseBytesToFloat(b, 5, 4, true) * 1.0, 6); + values.gnssTime = parseBytesToLong(b, 9, 4, true) * 1000; + } + } +} + +// Temperature Humidity Sensor DataSheet 1.26, 2.2.2: VER (Type bits 3-0) 1 means the SNR field is there, 0 means +// it is not and the message is 8 bytes. The codec always reads the SNR, so with VER 0 the next fields are shifted. +// TEMP_INT bit 7 is the sign, TEMP_FRA the hundredths. +function lansitecTemperatureHumidityRawValues(values, b) { + var offset = 3; + if ((b[0] & 0x0F) == 1) { + values.loraSnr = lansitecSnr(b, 3); + offset = 5; + } + if (b.length < offset + 3) { + return; + } + var temperature = (b[offset] & 0x7F) + (b[offset + 1] & 0xFF) / 100.0; + if ((b[offset] & 0x80) != 0) { + temperature = -temperature; + } + values.temperature = toFixed(temperature, 2); + values.humidity = b[offset + 2] & 0xFF; +} + var values = {}; +var attributes = {}; var history = []; if (metadata.payloadFormat == "JSON") { values = decodeToJson(payload); - if (values.history instanceof java.util.List) { + if (metadata.brandId == "lansitec" && values.bytes != null) { + // The Lansitec codecs put everything in "bytes" and get some fields wrong: see the Lansitec block above. + var raw = values.bytes; + values = lansitecValues(raw, metadata.modelId); + if (metadata.data != null) { + lansitecRawValues(values, base64ToBytes(metadata.data), metadata.modelId); + } + // Register is sent after join and holds the device settings: they are attributes, not telemetry. + var messageType = raw.type != null ? raw.type : raw["Message type"]; + if (messageType == "Register") { + attributes = values; + values = {}; + } + } else if (values.history instanceof java.util.List) { foreach (record : values.history) { if (record.timestamp != null) { var ts = record.timestamp * 1000; @@ -26,6 +399,9 @@ if (metadata.brandId != null && metadata.modelId != null) { profile = customProfile; } } +if (!attributes.isEmpty()) { + return { profile: profile, attributes: attributes }; +} if (history.isEmpty()) { return { profile: profile, telemetry: { ts: metadata.ts, values: values } }; } diff --git a/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw b/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw index 1cfef4546d..2e675de454 100644 --- a/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw +++ b/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw @@ -2,11 +2,384 @@ // is then that JSON: its keys become telemetry. A Milesight-style history list keeps each record's time. // Such a device gets the profile -, so all devices of one model share it, // or the name the integration metadata gives it under profile.-. Any other device gets LoRaWAN Device. + +// Lansitec: the LoRaWAN Device Repository codecs wrap everything in "bytes", write numbers as strings with units +// ("85%", "-67dBm") and get some fields wrong. Key names follow the Lansitec datasheets where there are any: +// Bluetooth Gateway DataSheet 2.27, Badge Tracker DataSheet v5.30, Temperature Humidity Sensor DataSheet 1.26. +// The wrong fields are decoded from the raw bytes (metadata.data). Remove a fix when the Repository fixes the codec. +var LANSITEC_UNITS = ["dBm", "dB", "%", "V", "sec", "min", "s", "℃", "level", " meter"]; + +var LANSITEC_GATEWAYS = ["compact-bluetooth-gateway", "indoor-bluetooth-gateway", "macro-bluetooth-gateway", "solar-bluetooth-gateway"]; + +// Codec key -> datasheet name, per model group. "-" drops the key. +// +// Bluetooth Gateway DataSheet 2.27, 4.4.1 Registration: ADR, Mode (current working LoRaWAN frequency sub-band plan), +// LoRaWAN frequency band, Power (LoRa TX power, dBm), Continuously BLE receiving, DR (LoRaWAN Uplink Data Rate), +// PRI (Position report interval), HB (Heartbeat interval), Bluetooth receiving duration. +// 4.4.2 Heartbeat: RSSI and SNR (LoRa), VER (Firmware version). VOL, SNR and CHGSTAT come from the raw bytes. +// number is the count of reported devices: the beacons list has them. The beacons come from the raw bytes too. +var LANSITEC_GATEWAY_KEYS = { + adr: "adr", mode: "mode", LoRaWANband: "loraWanFrequencyBand", LoRaTxpower: "loraTxPower", + ContinuousReceive: "continuousBleReceiving", dr: "dataRate", pos: "positionReportInterval", + hb: "heartbeatInterval", BLEReceivingDuration: "bluetoothReceivingDuration", + rssi: "loraRssi", snr: "-", ver: "deviceFirmwareVersion", vol: "-", chgstate: "-", number: "-" +}; + +// Badge Tracker DataSheet v5.30, 5.1 Registration: ADR, LoRa TX power, DR, GNSSEN (GNSS enable status), +// POSMODE (Positioning report mode), BLEPRI (Bluetooth Position Report Interval). The codec reads it without the +// GNSSPRI field, so heartbeat, version, CFMMSG, HBCOUNT and fall detection come from the raw bytes. +// 5.2 Heartbeat: Battery (percentage), RSSI (LoRa), GNSSSTATE (Status of the GNSS module), CHGSTATE (Status of +// battery charging). SNR and VIBSTATE come from the raw bytes. +// 5.3 GNSS Position: longitude, latitude and time come from the raw bytes. +// 5.4 Beacon: bytes 1-5 are RFU, the codec reads byte 1 as Vibstate. 5.5 Alarm: 0x01 SOS, 0x02 Fall Detection. +var LANSITEC_BADGE_KEYS = { + ADR: "adr", "Lora Txpower": "loraTxPower", DR: "dataRate", GNSSEN: "gnssEnabled", + "Position mode": "positioningReportMode", "Position period": "bluetoothPositionReportInterval", + "Heartbeat period": "-", "Firmware version": "-", CFMMSG: "-", HBCOUNT: "-", "Fall threshold": "-", + "Battery level": "batteryLevel", RSSI: "loraRssi", SNR: "-", "GPS state": "gnssState", + "Move state": "-", "Charge state": "chargeState", + Longitude: "-", Latitude: "-", Time: "-", Length: "-", Vibstate: "-", "Alarm type": "alarm" +}; + +// Temperature Humidity Sensor DataSheet 1.26, 2.2.1 Register: ADR, MODE (current working scheme), SMODE (data +// scheme supported), POWER (configured transmit power, dBm), DR, REPTIND (report H&T if the value is the same; +// the codec gives "false" for 0 = always report), TH (period of temperature and humidity report), CRC. +// 2.2.2 Periodical temperature and humidity: VER is the version of the message type, not the firmware; +// VOL (battery left, %), RSSI (downlink, detected by the sensor). SNR, temperature and humidity come from the raw bytes. +var LANSITEC_TEMPERATURE_HUMIDITY_KEYS = { + adr: "adr", mode: "mode", smode: "smode", LoraTxpower: "loraTxPower", dr: "dataRate", repting: "reptind", + th: "temperatureHumidityReportPeriod", crc: "-", + ver: "-", vol: "batteryLevel", rssi: "loraRssi", snr: "-", temperature: "-", humidity: "-" +}; + +// Without a datasheet only these keys are renamed: the integration writes rssi, snr and firmwareVersion itself. +// chgstate: the SocketSync gateway has the compact gateway codec byte for byte, which reads CHGSTAT 0x50 as +// "Not charging" (Bluetooth Gateway DataSheet 2.27, 4.4.2), so it is dropped. number is its count of reported +// devices: the beacons list has them. +var LANSITEC_CLASH_KEYS = { + rssi: "loraRssi", RSSI: "loraRssi", snr: "loraSnr", SNR: "loraSnr", + firmwareVersion: "deviceFirmwareVersion", "Firmware version": "deviceFirmwareVersion", + type: "-", "Message type": "-", chgstate: "-", number: "-" +}; + +// Strips the unit. Periods become seconds: the codecs give most of them in seconds, a few in minutes. +function lansitecNumber(v) { + if (!(v instanceof String)) { + return v; + } + foreach (u : LANSITEC_UNITS) { + if (v.endsWith(u)) { + var n = v.substring(0, v.length() - u.length()).trim(); + if (n.matches("-?[0-9]+(\\.[0-9]+)?")) { + return u == "min" ? parseDouble(n) * 60 : parseDouble(n); + } + } + } + return v; +} + +function lansitecLowerStart(part) { + var n = 0; + while (n < part.length() && part.substring(n, n + 1) != part.substring(n, n + 1).toLowerCase()) { + n = n + 1; + } + if (n == part.length()) { + return part.toLowerCase(); + } + if (n > 1) { + return part.substring(0, n - 1).toLowerCase() + part.substring(n - 1); + } + return part.substring(0, 1).toLowerCase() + part.substring(1); +} + +function lansitecCamelCase(key) { + var parts = key.split("[ .]+"); + var result = lansitecLowerStart(parts[0]); + for (var i = 1; i < parts.length; i++) { + var part = lansitecLowerStart(parts[i]); + result = result + part.substring(0, 1).toUpperCase() + part.substring(1); + } + return result; +} + +// Beacon keys: Devicetype1data1, Device1data, dev1major, dev1.major, pos1minor, asset1rssi... +var LANSITEC_BEACON_PREFIXES = ["devicetype1", "devicetype2", "devicetype3", "device", "dev", "pos", "asset"]; +var LANSITEC_BEACON_FIELDS = ["data", "major", "minor", "rssi", "id"]; + +function lansitecBeaconKey(key) { + var k = key.toLowerCase().replace(".", ""); + foreach (p : LANSITEC_BEACON_PREFIXES) { + if (k.startsWith(p)) { + var rest = k.substring(p.length()); + foreach (f : LANSITEC_BEACON_FIELDS) { + if (rest.endsWith(f) && rest.substring(0, rest.length() - f.length()).matches("[0-9]+")) { + return { group: p, index: rest.substring(0, rest.length() - f.length()), field: f }; + } + if (rest.startsWith(f) && rest.substring(f.length()).matches("[0-9]+")) { + return { group: p, index: rest.substring(f.length()), field: f }; + } + } + } + } + return null; +} + +function lansitecKeyTable(model) { + if (LANSITEC_GATEWAYS.contains(model)) { + return LANSITEC_GATEWAY_KEYS; + } + if (model == "badge-tracker") { + return LANSITEC_BADGE_KEYS; + } + if (model == "temperature-humidity-sensor") { + return LANSITEC_TEMPERATURE_HUMIDITY_KEYS; + } + return null; +} + +function lansitecValues(raw, model) { + var table = lansitecKeyTable(model); + var values = {}; + var beacons = {}; + foreach (key : raw.keySet()) { + var value = raw[key]; + var b = lansitecBeaconKey(key); + if (b != null) { + var id = b.group + b.index; + if (beacons[id] == null) { + beacons[id] = { order: parseInt(b.index) }; + } + if (b.field == "rssi") { + beacons[id].rssi = lansitecNumber(value); + } else if (b.field == "data" || b.field == "major" || b.field == "minor") { + beacons[id][b.field] = value; + } + } else if (table == null && key == "time" && value instanceof String) { + // Container tracker and helmet sensor, no datasheet: their codecs add 8 hours to the GNSS time and wrap + // it in quotes, as the badge tracker codec does with the UTC time of DataSheet v5.30, 5.3. + var gnssDate = new Date(value.replace("\"", "")); + values.gnssTime = gnssDate.getTime() - 8 * 60 * 60 * 1000; + } else if (table != null && table.containsKey(key)) { + var tableName = table[key]; + if (tableName != "-") { + values[tableName] = lansitecNumber(value); + } + } else if (LANSITEC_CLASH_KEYS.containsKey(key)) { + var clashName = LANSITEC_CLASH_KEYS[key]; + if (clashName != "-") { + values[clashName] = lansitecNumber(value); + } + } else { + var codecName = lansitecCamelCase(key); + values[codecName] = lansitecNumber(value); + } + } + if (!beacons.isEmpty()) { + values.beacons = lansitecBeaconList(beacons); + } + return values; +} + +function lansitecBeaconList(beacons) { + var list = []; + for (var i = 1; i <= beacons.size(); i++) { + foreach (id : beacons.keySet()) { + var b = beacons[id]; + if (b.order == i) { + var item = {}; + if (b.data != null) { + item.data = b.data; + } + if (b.major != null) { + item.major = b.major; + item.minor = b.minor; + } + item.rssi = b.rssi; + list.add(item); + } + } + } + // The gateway codec reads the data of every device after the first at a 5-byte step, so it is right only + // for 4-byte data (DataSheet 4.4.4 examples): with longer data only the first device is kept. The gateways + // with a datasheet get their beacons from the raw bytes instead; this is for the SocketSync gateway. + if (list.size() > 1 && list[0].data != null && list[0].data.length() != 8) { + var first = []; + first.add(list[0]); + return first; + } + return list; +} + +// Decoded from the raw bytes by the datasheet where the codec gets it wrong. +function lansitecRawValues(values, b, model) { + var type = (b[0] >> 4) & 0x0F; + if (LANSITEC_GATEWAYS.contains(model)) { + lansitecGatewayRawValues(values, b, model, type); + } else if (model == "badge-tracker") { + lansitecBadgeRawValues(values, b, type); + } else if (model == "temperature-humidity-sensor" && type == 2) { + lansitecTemperatureHumidityRawValues(values, b); + } +} + +// SNR, unit 0.01 dB, -3000 to 3000: signed. The codecs read it unsigned. +function lansitecSnr(b, offset) { + var v = parseBytesToInt(b, offset, 2, true); + if (v > 32767) { + v = v - 65536; + } + return v / 100.0; +} + +// Bluetooth Gateway DataSheet 2.27. +function lansitecGatewayRawValues(values, b, model, type) { + if (type == 1 && b.length >= 9) { + // 4.4.1: Network reconnection interval, unit 5 minutes. The codec does not decode it. + values.networkReconnectionInterval = (b[8] & 0xFF) * 300; + } else if (type == 2 && b.length >= 8) { + // 4.4.2: VOL is the battery voltage (VOL x 0.01 + 1.5 V) on the macro gateway and the battery level (%) on + // the others. The codec decides by the value (<= 100 is %), not by the model. + if (model == "macro-bluetooth-gateway") { + values.batteryVoltage = ((b[1] & 0xFF) + 150) / 100.0; + } else { + values.batteryLevel = b[1] & 0xFF; + } + values.loraSnr = lansitecSnr(b, 3); + // CHGSTAT: 0x00 Not charging, 0x50 Charging, 0x60 Charging completed on the compact and solar gateway, + // always 0x00 on the macro and indoor gateway. The codec reads the low half of the byte. + if (model == "compact-bluetooth-gateway" || model == "solar-bluetooth-gateway") { + var charge = b[7] & 0xFF; + if (charge == 0x00) { + values.chargeState = "Not charging"; + } else if (charge == 0x50) { + values.chargeState = "Charging"; + } else if (charge == 0x60) { + values.chargeState = "Charging completed"; + } + } + } else if (type == 0x8 || type == 0x9 || type == 0xA || type == 0xE) { + values.remove("beacons"); + var beacons = lansitecGatewayBeacons(b, type); + if (beacons != null) { + values.beacons = beacons; + } + } +} + +// 4.4.4-4.4.6: Device Type 1, 2 and 3 messages: Type (bits 3-0: number of devices), then Data (1-31 bytes) and +// RSSI (value - 256, dBm) of each device. Data length is not in the message: it is set by the device report rule +// of the type (4.4.3), so all devices of one message have the same length. The codec reads every device after +// the first at a 5-byte step. +// 4.4.7: Multi-Device Type message: Device Type ID before the data of each device. The data length can differ per +// type, and the message does not say it: decoded only when all devices fit the same length. +function lansitecGatewayBeacons(b, type) { + var count = b[0] & 0x0F; + if (count == 0 || (b.length - 1) % count != 0) { + return null; + } + // TBEL division gives a Double: array indexes need an int. + var step = ((b.length - 1) / count).intValue(); + var head = type == 0xE ? 1 : 0; + var dataLength = step - head - 1; + if (dataLength < 1) { + return null; + } + var hex = bytesToHex(b); + var list = []; + for (var i = 0; i < count; i++) { + var start = 1 + i * step; + var deviceType = type - 7; + if (type == 0xE) { + deviceType = b[start] & 0xFF; + if (deviceType < 1 || deviceType > 3) { + return null; + } + } + var item = {}; + item.deviceType = deviceType; + item.data = hex.substring(2 * (start + head), 2 * (start + head + dataLength)); + item.rssi = (b[start + step - 1] & 0xFF) - 256; + list.add(item); + } + return list; +} + +// Badge Tracker DataSheet v5.30. +function lansitecBadgeRawValues(values, b, type) { + if (type == 1 && b.length == 14) { + // 5.1, 14 bytes: Type, SMODE, Power, Config, BLEPRI (2), GNSSPRI (2), Heartbeat, Version (2), CFMMSG, HBCOUNT, Fall. + // BLEEN (Config bit 0) is not decoded: the field table says BLE enable status and that GNSSEN or BLEEN must + // be enabled, but the example (Config 32) has both 0 and calls bit 0 reserved. + values.gnssPositionReportInterval = parseBytesToInt(b, 6, 2, true) * 5; + values.heartbeatInterval = (b[8] & 0xFF) * 30; + // Version 0x0525 is V5.25. + var version = bytesToHex(b).substring(18, 22); + var major = version.substring(0, 2); + if (major.startsWith("0")) { + major = major.substring(1); + } + values.deviceFirmwareVersion = "V" + major + "." + version.substring(2); + values.cfmmsg = b[11] & 0xFF; + values.hbcount = b[12] & 0xFF; + // Fall detection threshold, unit 0.5 m, 0 means off. + values.fallDetectionThreshold = (b[13] & 0xFF) * 0.5; + } else if (type == 2 && b.length >= 6) { + // 5.2: the SNR field is there since firmware 0x0501. The SNREN bit (Type bits 3-0) is not used: the + // datasheet example 20 64 3E 033E 00 60 00 has SNREN 0 and the SNR 8.3 dB. + values.loraSnr = lansitecSnr(b, 3); + // VIBSTATE: moving state 0-9, 0 means no moving, 9 high acceleration. The codec turns only 0 and 1 into text. + values.movingState = b[5] & 0x0F; + } else if (type == 3 && b.length >= 13) { + // 5.3: Flag (Type bits 3-0) 0x1 means GNSS positioning failed. Longitude and latitude are IEEE 754 floats + // (the codec divides the mantissa by 0x7fffff instead of 2^23), time is UTC seconds (the codec adds 8 hours). + // Rounded to 6 decimals (about 0.1 m), as a double: the float has no more precision. + if ((b[0] & 0x0F) == 0) { + values.longitude = toFixed(parseBytesToFloat(b, 1, 4, true) * 1.0, 6); + values.latitude = toFixed(parseBytesToFloat(b, 5, 4, true) * 1.0, 6); + values.gnssTime = parseBytesToLong(b, 9, 4, true) * 1000; + } + } +} + +// Temperature Humidity Sensor DataSheet 1.26, 2.2.2: VER (Type bits 3-0) 1 means the SNR field is there, 0 means +// it is not and the message is 8 bytes. The codec always reads the SNR, so with VER 0 the next fields are shifted. +// TEMP_INT bit 7 is the sign, TEMP_FRA the hundredths. +function lansitecTemperatureHumidityRawValues(values, b) { + var offset = 3; + if ((b[0] & 0x0F) == 1) { + values.loraSnr = lansitecSnr(b, 3); + offset = 5; + } + if (b.length < offset + 3) { + return; + } + var temperature = (b[offset] & 0x7F) + (b[offset + 1] & 0xFF) / 100.0; + if ((b[offset] & 0x80) != 0) { + temperature = -temperature; + } + values.temperature = toFixed(temperature, 2); + values.humidity = b[offset + 2] & 0xFF; +} + var values = {}; +var attributes = {}; var history = []; if (metadata.payloadFormat == "JSON") { values = decodeToJson(payload); - if (values.history instanceof java.util.List) { + if (metadata.brandId == "lansitec" && values.bytes != null) { + // The Lansitec codecs put everything in "bytes" and get some fields wrong: see the Lansitec block above. + var raw = values.bytes; + values = lansitecValues(raw, metadata.modelId); + if (metadata.data != null) { + lansitecRawValues(values, base64ToBytes(metadata.data), metadata.modelId); + } + // Register is sent after join and holds the device settings: they are attributes, not telemetry. + var messageType = raw.type != null ? raw.type : raw["Message type"]; + if (messageType == "Register") { + attributes = values; + values = {}; + } + } else if (values.history instanceof java.util.List) { foreach (record : values.history) { if (record.timestamp != null) { var ts = record.timestamp * 1000; @@ -26,6 +399,9 @@ if (metadata.brandId != null && metadata.modelId != null) { profile = customProfile; } } +if (!attributes.isEmpty()) { + return { profile: profile, attributes: attributes }; +} if (history.isEmpty()) { return { profile: profile, telemetry: { ts: metadata.ts, values: values } }; } From e57c5167f9fd6af225664e07490e93920060a226 Mon Sep 17 00:00:00 2001 From: dpinkevych Date: Mon, 2 Nov 2026 12:38:43 +0200 Subject: [PATCH 02/13] fix(iot-hub): the JS default TTN/TTI converter takes the payload as UTF-8 bytes or as characters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Nashorn passes the payload as characters, so decoding it again as UTF-8 failed on a unit like ℃ and the uplink was lost. Tested with simulated TTN uplinks of the Lansitec datasheet examples, TBEL and JS give the same values. --- ui-ngx/src/assets/converters/js-tti-decoder.raw | 10 ++++++++-- ui-ngx/src/assets/converters/js-ttn-decoder.raw | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/ui-ngx/src/assets/converters/js-tti-decoder.raw b/ui-ngx/src/assets/converters/js-tti-decoder.raw index ca1d911e66..cc7223254c 100644 --- a/ui-ngx/src/assets/converters/js-tti-decoder.raw +++ b/ui-ngx/src/assets/converters/js-tti-decoder.raw @@ -452,7 +452,13 @@ if (history.length == 0) { history.push({ ts: metadata.ts, values: values }); return { profile: profile, telemetry: history }; -// The payload is UTF-8: a codec value can have a unit like ℃. +// A codec value can have a unit like ℃. Where the payload comes as UTF-8 bytes, they are decoded; where it already +// comes as characters, the decoding fails and the text is taken as it is. function decodeToJson(payload) { - return JSON.parse(decodeURIComponent(escape(String.fromCharCode.apply(String, payload)))); + var text = String.fromCharCode.apply(String, payload); + try { + text = decodeURIComponent(escape(text)); + } catch (e) { + } + return JSON.parse(text); } diff --git a/ui-ngx/src/assets/converters/js-ttn-decoder.raw b/ui-ngx/src/assets/converters/js-ttn-decoder.raw index ca1d911e66..cc7223254c 100644 --- a/ui-ngx/src/assets/converters/js-ttn-decoder.raw +++ b/ui-ngx/src/assets/converters/js-ttn-decoder.raw @@ -452,7 +452,13 @@ if (history.length == 0) { history.push({ ts: metadata.ts, values: values }); return { profile: profile, telemetry: history }; -// The payload is UTF-8: a codec value can have a unit like ℃. +// A codec value can have a unit like ℃. Where the payload comes as UTF-8 bytes, they are decoded; where it already +// comes as characters, the decoding fails and the text is taken as it is. function decodeToJson(payload) { - return JSON.parse(decodeURIComponent(escape(String.fromCharCode.apply(String, payload)))); + var text = String.fromCharCode.apply(String, payload); + try { + text = decodeURIComponent(escape(text)); + } catch (e) { + } + return JSON.parse(text); } From b26409cae1cc133555b3afed6e684106e8e97b4d Mon Sep 17 00:00:00 2001 From: dpinkevych Date: Mon, 2 Nov 2026 13:50:06 +0200 Subject: [PATCH 03/13] test(iot-hub): the default TTN/TTI converters decode Lansitec devices, on 20 uplinks The Things Stack sent (TBEL and JS, TTN and TTI) --- .../controller/ConverterControllerTest.java | 66 + .../things_stack_lansitec_uplinks.json | 1444 +++++++++++++++++ 2 files changed, 1510 insertions(+) create mode 100644 application/src/test/resources/payload/things_stack_lansitec_uplinks.json diff --git a/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java index 023397bf0b..317508490f 100644 --- a/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java @@ -4,6 +4,7 @@ package org.thingsboard.server.controller; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.TextNode; import com.google.common.collect.Streams; @@ -40,6 +41,7 @@ import org.thingsboard.server.exception.DataValidationException; import java.io.IOException; import java.io.InputStream; +import java.math.RoundingMode; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Base64; @@ -98,6 +100,7 @@ public class ConverterControllerTest extends AbstractControllerTest { private static final String DEFAULT_V2_CONFIGURATION = "converters/default-converters-v2-configuration.json"; private static final String THINGS_STACK_REPOSITORY_DEVICE_PAYLOAD = "payload/things_stack_repository_device_payload.json"; private static final String THINGS_STACK_MANUAL_DEVICE_PAYLOAD = "payload/things_stack_manual_device_payload.json"; + private static final String THINGS_STACK_LANSITEC_UPLINKS = "payload/things_stack_lansitec_uplinks.json"; @Before public void beforeTest() throws Exception { @@ -732,6 +735,69 @@ public class ConverterControllerTest extends AbstractControllerTest { } } + // Uplinks of Lansitec devices registered from the LoRaWAN Device Repository, as The Things Stack sent them + // (Simulate uplink), and the keys the default converters decode from them. The keys the integration adds to + // every uplink (rssi, fCnt...) are not listed. TBEL gives integers as doubles, so numbers are compared by value. + private static final Set THINGS_STACK_UPLINK_KEYS = Set.of("fCnt", "data", "rssi", "snr", "channel", "consumedAirtime"); + + @Test + public void testThingsStackDefaultConvertersDecodeLansitecDevices() throws IOException { + JsonNode defaultConfigurations = JacksonUtil.toJsonNode(readResource(DEFAULT_V2_CONFIGURATION)); + JsonNode cases = JacksonUtil.toJsonNode(readResource(THINGS_STACK_LANSITEC_UPLINKS)); + for (IntegrationType integrationType : List.of(IntegrationType.TTN, IntegrationType.TTI)) { + Converter converter = createConverter(defaultConfigurations.get(integrationType.name()), integrationType, 2); + for (ScriptLanguage scriptLang : ScriptLanguage.values()) { + String decoderFileName = "converters/" + scriptLang.name().toLowerCase() + "-" + integrationType.name().toLowerCase() + "-decoder.raw"; + for (JsonNode testCase : cases) { + String description = integrationType + " " + scriptLang + " " + testCase.get("name").asText(); + JsonNode result = JacksonUtil.toJsonNode(decode(decoderFileName, testCase.get("uplink").toString(), converter, scriptLang, Map.of())); + JsonNode decoded = testCase.get("decoded"); + assertThat(result.get("profile")).as(description).isEqualTo(decoded.get("profile")); + + JsonNode telemetry = result.get("telemetry"); + if (telemetry != null && telemetry.isArray()) { + telemetry = telemetry.get(telemetry.size() - 1); + } + JsonNode telemetryValues = telemetry != null && telemetry.has("values") ? telemetry.get("values") : JacksonUtil.newObjectNode(); + ObjectNode lansitecTelemetry = JacksonUtil.newObjectNode(); + telemetryValues.fields().forEachRemaining(e -> { + if (!THINGS_STACK_UPLINK_KEYS.contains(e.getKey())) { + lansitecTelemetry.set(e.getKey(), e.getValue()); + } + }); + assertThat(numbersByValue(lansitecTelemetry)).as(description + " telemetry") + .isEqualTo(numbersByValue(decoded.has("telemetry") ? decoded.get("telemetry") : JacksonUtil.newObjectNode())); + + if (decoded.has("attributes")) { + ObjectNode attributes = JacksonUtil.newObjectNode(); + decoded.get("attributes").fieldNames().forEachRemaining(key -> attributes.set(key, result.get("attributes").get(key))); + assertThat(numbersByValue(attributes)).as(description + " attributes").isEqualTo(numbersByValue(decoded.get("attributes"))); + } + } + } + } + } + + private static JsonNode numbersByValue(JsonNode node) { + if (node == null) { + return null; + } + if (node.isNumber()) { + return JacksonUtil.toJsonNode(node.decimalValue().setScale(6, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString()); + } + if (node.isObject()) { + ObjectNode copy = JacksonUtil.newObjectNode(); + node.fields().forEachRemaining(e -> copy.set(e.getKey(), numbersByValue(e.getValue()))); + return copy; + } + if (node.isArray()) { + ArrayNode copy = JacksonUtil.newArrayNode(); + node.forEach(item -> copy.add(numbersByValue(item))); + return copy; + } + return node; + } + private String readResource(String name) throws IOException { return new String(IOUtils.toByteArray(ConverterControllerTest.class.getClassLoader().getResourceAsStream(name)), StandardCharsets.UTF_8); } diff --git a/application/src/test/resources/payload/things_stack_lansitec_uplinks.json b/application/src/test/resources/payload/things_stack_lansitec_uplinks.json new file mode 100644 index 0000000000..5b1aefd85f --- /dev/null +++ b/application/src/test/resources/payload/things_stack_lansitec_uplinks.json @@ -0,0 +1,1444 @@ +[ + { + "name": "gw Registration 4.4.1", + "uplink": { + "end_device_ids": { + "device_id": "lt-compact-bluetooth-gateway", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0000", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6DWZKQC3A6CWMNE64WPEVRC", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:96427a37-7461-40d0-94f4-6e3ea7f275e5" + ], + "received_at": "2026-11-02T10:30:49.835523566Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "FiCgMAA8eAsG", + "decoded_payload": { + "bytes": { + "BLEReceivingDuration": "11sec", + "ContinuousReceive": "Continuous Bluetooth receiving is disabled", + "LoRaTxpower": "20dBm", + "LoRaWANband": "EU868", + "adr": "OFF", + "dr": "DR3", + "hb": "3600sec", + "mode": "EU868", + "pos": "300sec", + "type": "Register" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "compact-bluetooth-gateway", + "hardware_version": "1.0", + "firmware_version": "1.0", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-compact-bluetooth-gateway", + "attributes": { + "bluetoothReceivingDuration": 11.0, + "continuousBleReceiving": "Continuous Bluetooth receiving is disabled", + "loraTxPower": 20.0, + "loraWanFrequencyBand": "EU868", + "adr": "OFF", + "dataRate": "DR3", + "heartbeatInterval": 3600.0, + "mode": "EU868", + "positionReportInterval": 300.0, + "networkReconnectionInterval": 1800 + } + } + }, + { + "name": "gw Heartbeat 4.4.2 macro", + "uplink": { + "end_device_ids": { + "device_id": "lt-macro-bluetooth-gateway", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0002", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6DWZT8XP0N2W84G07Q2MMCH", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:1af83ff0-c875-48b0-b388-dc2e2742ef61" + ], + "received_at": "2026-11-02T10:30:56.540968417Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "Ics+Az4AbAA=", + "decoded_payload": { + "bytes": { + "chgstate": "Not charging", + "rssi": "-62dBm", + "snr": "8.3dB", + "type": "Heartbeat", + "ver": "VER108", + "vol": "3.5300000000000002V" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "macro-bluetooth-gateway", + "hardware_version": "1.0", + "firmware_version": "1.10", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-macro-bluetooth-gateway", + "telemetry": { + "loraRssi": -62.0, + "deviceFirmwareVersion": "VER108", + "batteryVoltage": 3.53, + "loraSnr": 8.3 + } + } + }, + { + "name": "gw Heartbeat CHGSTAT 0x50 (built)", + "uplink": { + "end_device_ids": { + "device_id": "lt-compact-bluetooth-gateway", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0000", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6DX00PFS05VHZX420SK9SRW", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:4a7e0719-1b7b-4322-b00f-9619d3c1988b" + ], + "received_at": "2026-11-02T10:31:03.118765152Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "IWQ+Az4AbFA=", + "decoded_payload": { + "bytes": { + "chgstate": "Not charging", + "rssi": "-62dBm", + "snr": "8.3dB", + "type": "Heartbeat", + "ver": "VER108", + "vol": "100%" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "compact-bluetooth-gateway", + "hardware_version": "1.0", + "firmware_version": "1.0", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-compact-bluetooth-gateway", + "telemetry": { + "loraRssi": -62.0, + "deviceFirmwareVersion": "VER108", + "batteryLevel": 100, + "loraSnr": 8.3, + "chargeState": "Charging" + } + } + }, + { + "name": "gw Heartbeat CHGSTAT 0x60, SNR -5.00 (built)", + "uplink": { + "end_device_ids": { + "device_id": "lt-solar-bluetooth-gateway", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0003", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6DX074N1N81HG0PPEWKCC4W", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:08bbcae0-1d84-49ae-af6c-384da48f8212" + ], + "received_at": "2026-11-02T10:31:09.716547098Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "IWQ+/gwAbGA=", + "decoded_payload": { + "bytes": { + "chgstate": "Not charging", + "rssi": "-62dBm", + "snr": "650.36dB", + "type": "Heartbeat", + "ver": "VER108", + "vol": "100%" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "solar-bluetooth-gateway", + "hardware_version": "1.0", + "firmware_version": "1.10", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-solar-bluetooth-gateway", + "telemetry": { + "loraRssi": -62.0, + "deviceFirmwareVersion": "VER108", + "batteryLevel": 100, + "loraSnr": -5.0, + "chargeState": "Charging completed" + } + } + }, + { + "name": "gw Device Type 1 4.4.4 ex1", + "uplink": { + "end_device_ids": { + "device_id": "lt-indoor-bluetooth-gateway", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0001", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6DX0DHGSWGSMJN6M8JJQM4Q", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:3ff688da-df1b-4138-ac08-7d4e01ce8695" + ], + "received_at": "2026-11-02T10:31:16.271874839Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "gwYaTAACFaq7swYaTAACFaq8tAYaTAACFaq9tQ==", + "decoded_payload": { + "bytes": { + "Devicetype1data1": "061A4C000215AABB", + "Devicetype1data2": "15AABBB3061A4C00", + "Devicetype1data3": "1A4C000215AABCB4", + "Devicetype1rssi1": "-77dBm", + "Devicetype1rssi2": "-76dBm", + "Devicetype1rssi3": "-75dBm", + "number": 3, + "type": "Device Type 1 Message" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "indoor-bluetooth-gateway", + "hardware_version": "1.0", + "firmware_version": "1.0", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-indoor-bluetooth-gateway", + "telemetry": { + "beacons": [ + { + "deviceType": 1, + "data": "061A4C000215AABB", + "rssi": -77 + }, + { + "deviceType": 1, + "data": "061A4C000215AABC", + "rssi": -76 + }, + { + "deviceType": 1, + "data": "061A4C000215AABD", + "rssi": -75 + } + ] + } + } + }, + { + "name": "gw Device Type 1 4.4.4 ex2", + "uplink": { + "end_device_ids": { + "device_id": "lt-compact-bluetooth-gateway", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0000", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6DX0KYNGRQ053WBK9XC1SP2", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:d999fd28-8d4f-401c-9b7f-95c694d99f72" + ], + "received_at": "2026-11-02T10:31:22.835827277Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "gmSCFpgmU0wCFaq8tGSCFpgmVAYaFaq9tQ==", + "decoded_payload": { + "bytes": { + "Devicetype1data1": "6482169826534C0215AABC", + "Devicetype1data2": "534C0215AABCB464821698", + "Devicetype1rssi1": "-76dBm", + "Devicetype1rssi2": "-75dBm", + "number": 2, + "type": "Device Type 1 Message" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "compact-bluetooth-gateway", + "hardware_version": "1.0", + "firmware_version": "1.0", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-compact-bluetooth-gateway", + "telemetry": { + "beacons": [ + { + "deviceType": 1, + "data": "6482169826534C0215AABC", + "rssi": -76 + }, + { + "deviceType": 1, + "data": "648216982654061A15AABD", + "rssi": -75 + } + ] + } + } + }, + { + "name": "gw Device Type 2 4.4.5 ex2", + "uplink": { + "end_device_ids": { + "device_id": "lt-compact-bluetooth-gateway", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0000", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6DX0TBEF65ED7NRWQ4YFTRQ", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:7f4ac774-4027-4962-a213-e526da66fd38" + ], + "received_at": "2026-11-02T10:31:29.389983893Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "kpgmU0FNLTEwMDAxs5gmVEFNLTEwMDAytA==", + "decoded_payload": { + "bytes": { + "Devicetype2data1": "982653414D2D3130303031", + "Devicetype2data2": "2D3130303031B398265441", + "Devicetype2rssi1": "-77dBm", + "Devicetype2rssi2": "-76dBm", + "number": 2, + "type": "Device Type 2 Message" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "compact-bluetooth-gateway", + "hardware_version": "1.0", + "firmware_version": "1.0", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-compact-bluetooth-gateway", + "telemetry": { + "beacons": [ + { + "deviceType": 2, + "data": "982653414D2D3130303031", + "rssi": -77 + }, + { + "deviceType": 2, + "data": "982654414D2D3130303032", + "rssi": -76 + } + ] + } + } + }, + { + "name": "gw Device Type 3 4.4.6 ex1", + "uplink": { + "end_device_ids": { + "device_id": "lt-compact-bluetooth-gateway", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0000", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6DX10R67RVM4X2QAMR9C8JG", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:68655884-9942-4270-9536-700ee6b20dcf" + ], + "received_at": "2026-11-02T10:31:35.941440062Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "ogAQAwrCABADC8Q=", + "decoded_payload": { + "bytes": { + "Devicetype3data1": "0010030A", + "Devicetype3data2": "0010030B", + "Devicetype3rssi1": "-62dBm", + "Devicetype3rssi2": "-60dBm", + "number": 2, + "type": "Device Type 3 Message" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "compact-bluetooth-gateway", + "hardware_version": "1.0", + "firmware_version": "1.0", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-compact-bluetooth-gateway", + "telemetry": { + "beacons": [ + { + "deviceType": 3, + "data": "0010030A", + "rssi": -62 + }, + { + "deviceType": 3, + "data": "0010030B", + "rssi": -60 + } + ] + } + } + }, + { + "name": "gw Multi 4.4.7 (mixed lengths)", + "uplink": { + "end_device_ids": { + "device_id": "lt-compact-bluetooth-gateway", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0000", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6DX174RG6ZG5ZKAHAZTRVG9", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:1aff5046-26ee-42eb-9dfb-d58dafa63116" + ], + "received_at": "2026-11-02T10:31:42.487961636Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "4wEGGkwAAhWqu7MDQU0tMTAwMDGxAgAQAwvF", + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "compact-bluetooth-gateway", + "hardware_version": "1.0", + "firmware_version": "1.0", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-compact-bluetooth-gateway", + "telemetry": {} + } + }, + { + "name": "badge Registration 5.1", + "uplink": { + "end_device_ids": { + "device_id": "lt-badge-tracker", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0007", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6DX1DGR31039JQYY9Y2XQ8V", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:224ac913-2b83-4efa-8b16-93b3f4fe7a16" + ], + "received_at": "2026-11-02T10:31:49.014979356Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "EACgMgA8AWh4BSUCBQQ=", + "decoded_payload": { + "bytes": { + "ADR": "OFF", + "CFMMSG": "1 Confirmed every 5 Heartbeat", + "DR": "DR3", + "Fall threshold": "1 meter", + "Firmware version": "V68.78", + "GNSSEN": "Disable", + "HBCOUNT": "Disconnect Judgement 37", + "Heartbeat period": "30sec", + "Lora Txpower": "20dBm", + "Message type": "Register", + "Position mode": "Autonomous", + "Position period": "300sec" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "badge-tracker", + "hardware_version": "1.0", + "firmware_version": "5.25", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-badge-tracker", + "attributes": { + "adr": "OFF", + "dataRate": "DR3", + "gnssEnabled": "Disable", + "loraTxPower": 20.0, + "positioningReportMode": "Autonomous", + "bluetoothPositionReportInterval": 300.0, + "gnssPositionReportInterval": 1800, + "heartbeatInterval": 3600, + "deviceFirmwareVersion": "V5.25", + "cfmmsg": 2, + "hbcount": 5, + "fallDetectionThreshold": 2.0 + } + } + }, + { + "name": "badge Heartbeat 5.2", + "uplink": { + "end_device_ids": { + "device_id": "lt-badge-tracker", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0007", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6DX1KWZBE1ANRSZWHFSKQ89", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:29eb6310-c010-4972-897c-d743a41f1d8d" + ], + "received_at": "2026-11-02T10:31:55.550797518Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "IGQ+Az4AYAA=", + "decoded_payload": { + "bytes": { + "Battery level": "100%", + "Charge state": "Charge complete", + "GPS state": "GPS OFF", + "Message type": "Heartbeat", + "Move state": "No Move", + "RSSI": "-62dBm", + "SNR": "8.3dB" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "badge-tracker", + "hardware_version": "1.0", + "firmware_version": "5.25", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-badge-tracker", + "telemetry": { + "batteryLevel": 100.0, + "chargeState": "Charge complete", + "gnssState": "GPS OFF", + "loraRssi": -62.0, + "loraSnr": 8.3, + "movingState": 0 + } + } + }, + { + "name": "badge GNSS 5.3", + "uplink": { + "end_device_ids": { + "device_id": "lt-badge-tracker", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0007", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6DX1T9GTJ0BFJRK0D53EHQA", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:bd62cb24-c5cb-4732-a997-c5b6af5a2e47" + ], + "received_at": "2026-11-02T10:32:02.096017163Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "MELtisRB/84uZXLoqA==", + "decoded_payload": { + "bytes": { + "Latitude": 31.975675579986046, + "Longitude": 118.77103314054408, + "Message type": "GNSSPosition", + "Time": "\"2023-12-08T17:58:00.000Z\"" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "badge-tracker", + "hardware_version": "1.0", + "firmware_version": "5.25", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-badge-tracker", + "telemetry": { + "longitude": 118.771027, + "latitude": 31.975674, + "gnssTime": 1702029480000 + } + } + }, + { + "name": "badge GNSS flag failed (built)", + "uplink": { + "end_device_ids": { + "device_id": "lt-badge-tracker", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0007", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6DX20NZAK3HTDSA9K12Z4Z6", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:b498fb1f-8894-4ba9-b17c-07c7d0f4018f" + ], + "received_at": "2026-11-02T10:32:08.639160535Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "MULtisRB/84uZXLoqA==", + "decoded_payload": { + "bytes": { + "Latitude": 31.975675579986046, + "Longitude": 118.77103314054408, + "Message type": "GNSSPosition", + "Time": "\"2023-12-08T17:58:00.000Z\"" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "badge-tracker", + "hardware_version": "1.0", + "firmware_version": "5.25", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-badge-tracker", + "telemetry": {} + } + }, + { + "name": "temp-hum VER1 -5.00 dB, -12.34 C, 56 % (built)", + "uplink": { + "end_device_ids": { + "device_id": "lt-temperature-humidity-sensor", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A000A", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6DX2756RP8VNV5W4TED20QV", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:2f27538b-a738-43f7-b251-82e5b814d2f2" + ], + "received_at": "2026-11-02T10:32:15.269594659Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "IWQ+/gyMIjgSNA==", + "decoded_payload": { + "bytes": { + "crc": 4660, + "humidity": "56%", + "rssi": "-62dBm", + "snr": 650.36, + "temperature": "-12.34℃", + "type": "Heartbeat", + "ver": 1, + "vol": "100%" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "temperature-humidity-sensor", + "hardware_version": "1.0", + "firmware_version": "1.01", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-temperature-humidity-sensor", + "telemetry": { + "loraRssi": -62.0, + "batteryLevel": 100.0, + "loraSnr": -5.0, + "temperature": -12.34, + "humidity": 56 + } + } + }, + { + "name": "temp-hum VER0 21.05 C, 40 % (built)", + "uplink": { + "end_device_ids": { + "device_id": "lt-temperature-humidity-sensor", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A000A", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6DX2DHP1PVMSN9XXTFX6G9E", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:d92a9e40-19fc-4d8a-bbed-db57a3285281" + ], + "received_at": "2026-11-02T10:32:21.813476822Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "IGQ+FQUoEjQ=", + "decoded_payload": { + "bytes": { + "crc": 0, + "humidity": "52%", + "rssi": "-62dBm", + "snr": 53.81, + "temperature": "40.18℃", + "type": "Heartbeat", + "ver": 0, + "vol": "100%" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "temperature-humidity-sensor", + "hardware_version": "1.0", + "firmware_version": "1.01", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-temperature-humidity-sensor", + "telemetry": { + "loraRssi": -62.0, + "batteryLevel": 100.0, + "temperature": 21.05, + "humidity": 40 + } + } + }, + { + "name": "container Heartbeat (built from the codec)", + "uplink": { + "end_device_ids": { + "device_id": "lt-container-tracker", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0008", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6DX2KYHR69G9SQHNPG7VJ0N", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:e36b265c-edf1-4c2b-8162-4e475b776185" + ], + "received_at": "2026-11-02T10:32:28.369063597Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "Ics+Az4yABkADA==", + "decoded_payload": { + "bytes": { + "gpsstate": "located", + "movement": "60sec", + "rssi": "-62dBm", + "snr": "8.3dB", + "snrinc": "SNR field included ", + "temperature": "25℃", + "type": "Heartbeat", + "vibstate": "2level", + "vol": "3.5300000000000002V" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "container-tracker", + "hardware_version": "1.0", + "firmware_version": "1.0", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-container-tracker", + "telemetry": { + "gpsstate": "located", + "movement": 60.0, + "loraRssi": -62.0, + "loraSnr": 8.3, + "snrinc": "SNR field included ", + "temperature": 25.0, + "vibstate": 2.0, + "vol": 3.5300000000000002 + } + } + }, + { + "name": "container GNSS (badge 5.3 bytes)", + "uplink": { + "end_device_ids": { + "device_id": "lt-container-tracker", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0008", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6DX2TBAJT5T5EE3615Y9MAW", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:b64cce74-6708-4423-aa37-eca108e9f585" + ], + "received_at": "2026-11-02T10:32:34.922307414Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "MELtisRB/84uZXLoqA==", + "decoded_payload": { + "bytes": { + "latitude": 31.975675579986046, + "longitude": 118.77103314054408, + "time": "\"2023-12-08T17:58:00.000Z\"", + "type": "GNSSPosition" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "container-tracker", + "hardware_version": "1.0", + "firmware_version": "1.0", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-container-tracker", + "telemetry": { + "latitude": 31.975675579986046, + "longitude": 118.77103314054408, + "gnssTime": 1702029480000 + } + } + }, + { + "name": "helmet Heartbeat (built from the codec)", + "uplink": { + "end_device_ids": { + "device_id": "lt-helmet-sensor", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0009", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6DX30QBDSZ7MN6T27M5X0A3", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:177c9fb1-a5aa-40a5-a324-ace0f58979b7" + ], + "received_at": "2026-11-02T10:32:41.450805906Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "IWQ+Az4BAQIEIA==", + "decoded_payload": { + "bytes": { + "batteryPercentage": "100%", + "bleState": 1, + "chargeTime": "60s", + "gpsState": 1, + "moveState": "2 Move", + "rssi": "-62dBm", + "snr": "8.3dB", + "type": "Heartbeat", + "wearTime": "120s" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "helmet-sensor", + "hardware_version": "1.0", + "firmware_version": "0.25", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-helmet-sensor", + "telemetry": { + "batteryPercentage": 100.0, + "bleState": 1, + "chargeTime": 60.0, + "gpsState": 1, + "moveState": "2 Move", + "loraRssi": -62.0, + "loraSnr": 8.3, + "wearTime": 120.0 + } + } + }, + { + "name": "helmet GNSS (built from the codec)", + "uplink": { + "end_device_ids": { + "device_id": "lt-helmet-sensor", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0009", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6DX373VN3K38B2H5WR86Z6H", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:ed54ebed-8df4-46eb-a656-5769e3c37be8" + ], + "received_at": "2026-11-02T10:32:47.994303232Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "MQABhqBC7YrEQf/OLmVy6Kg=", + "decoded_payload": { + "bytes": { + "gpsState": "gps location success", + "latitude": 31.975675579986046, + "longitude": 118.77103314054408, + "pressure": 100000, + "time": "\"2023-12-08T17:58:00.000Z\"", + "type": "GNSSPosition", + "wearState": "wear" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "helmet-sensor", + "hardware_version": "1.0", + "firmware_version": "0.25", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-helmet-sensor", + "telemetry": { + "gpsState": "gps location success", + "latitude": 31.975675579986046, + "longitude": 118.77103314054408, + "pressure": 100000, + "gnssTime": 1702029480000, + "wearState": "wear" + } + } + }, + { + "name": "socketsync Heartbeat 0x50", + "uplink": { + "end_device_ids": { + "device_id": "lt-socket-sync-bluetooth-gateway", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0004", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6DX3DFWNQE3WSG9JNFTAG7B", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:ec26c3c1-e861-4d65-b6e7-1206e538a5c7" + ], + "received_at": "2026-11-02T10:32:54.523762899Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "IWQ+Az4AbFA=", + "decoded_payload": { + "bytes": { + "chgstate": "Not charging", + "rssi": "-62dBm", + "snr": "8.3dB", + "type": "Heartbeat", + "ver": "VER108", + "vol": "100%" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "socket-sync-bluetooth-gateway", + "hardware_version": "1.0", + "firmware_version": "1.0", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-socket-sync-bluetooth-gateway", + "telemetry": { + "loraRssi": -62.0, + "loraSnr": 8.3, + "ver": "VER108", + "vol": 100.0 + } + } + } +] From 846c022d9d6c6967839509fa7ac60d6c105623b4 Mon Sep 17 00:00:00 2001 From: dpinkevych Date: Mon, 2 Nov 2026 14:04:13 +0200 Subject: [PATCH 04/13] fix(iot-hub): a Lansitec frame too short for its message or bytes that are not an object no longer drop the uplink; the Lansitec test also checks the attributes --- .../controller/ConverterControllerTest.java | 103 +++++++++++++----- .../src/assets/converters/js-tti-decoder.raw | 14 ++- .../src/assets/converters/js-ttn-decoder.raw | 14 ++- .../assets/converters/tbel-tti-decoder.raw | 14 ++- .../assets/converters/tbel-ttn-decoder.raw | 14 ++- 5 files changed, 108 insertions(+), 51 deletions(-) diff --git a/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java index 317508490f..56a9237097 100644 --- a/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java @@ -737,47 +737,96 @@ public class ConverterControllerTest extends AbstractControllerTest { // Uplinks of Lansitec devices registered from the LoRaWAN Device Repository, as The Things Stack sent them // (Simulate uplink), and the keys the default converters decode from them. The keys the integration adds to - // every uplink (rssi, fCnt...) are not listed. TBEL gives integers as doubles, so numbers are compared by value. + // every uplink (rssi, fCnt, eui...) are not listed. TBEL gives integers as doubles, so numbers are compared by value. private static final Set THINGS_STACK_UPLINK_KEYS = Set.of("fCnt", "data", "rssi", "snr", "channel", "consumedAirtime"); + private static final Set THINGS_STACK_ATTRIBUTE_KEYS = Set.of("eui", "joinEui", "devAddr", "deviceId", "applicationId", "fPort", + "bandwidth", "spreadingFactor", "codeRate", "frequency", "brandId", "modelId", "hardwareVersion", "firmwareVersion", "bandId", + "netId", "tenantId", "clusterId", "clusterAddress"); @Test public void testThingsStackDefaultConvertersDecodeLansitecDevices() throws IOException { - JsonNode defaultConfigurations = JacksonUtil.toJsonNode(readResource(DEFAULT_V2_CONFIGURATION)); JsonNode cases = JacksonUtil.toJsonNode(readResource(THINGS_STACK_LANSITEC_UPLINKS)); + forEachThingsStackDefaultConverter((description, decoder) -> { + for (JsonNode testCase : cases) { + String caseDescription = description + " " + testCase.get("name").asText(); + JsonNode result = decoder.apply(testCase.get("uplink")); + JsonNode decoded = testCase.get("decoded"); + assertThat(result.get("profile")).as(caseDescription).isEqualTo(decoded.get("profile")); + assertThat(numbersByValue(withoutKeys(telemetryValues(result), THINGS_STACK_UPLINK_KEYS))).as(caseDescription + " telemetry") + .isEqualTo(numbersByValue(decoded.has("telemetry") ? decoded.get("telemetry") : JacksonUtil.newObjectNode())); + assertThat(numbersByValue(withoutKeys(result.get("attributes"), THINGS_STACK_ATTRIBUTE_KEYS))).as(caseDescription + " attributes") + .isEqualTo(numbersByValue(decoded.has("attributes") ? decoded.get("attributes") : JacksonUtil.newObjectNode())); + } + }); + } + + // A frame too short for its message type, and bytes that are not an object: the uplink is still converted + @Test + public void testThingsStackDefaultConvertersKeepMalformedLansitecUplinks() throws IOException { + JsonNode temperatureHumidity = null; + for (JsonNode testCase : JacksonUtil.toJsonNode(readResource(THINGS_STACK_LANSITEC_UPLINKS))) { + if (testCase.get("name").asText().startsWith("temp-hum VER1")) { + temperatureHumidity = testCase.get("uplink"); + } + } + ObjectNode shortFrame = temperatureHumidity.deepCopy(); + ((ObjectNode) shortFrame.get("uplink_message")).put("frm_payload", "IQ=="); + ObjectNode bytesList = temperatureHumidity.deepCopy(); + ((ObjectNode) bytesList.get("uplink_message").get("decoded_payload")).set("bytes", JacksonUtil.toJsonNode("[33, 100]")); + + forEachThingsStackDefaultConverter((description, decoder) -> { + JsonNode result = decoder.apply(shortFrame); + assertThat(result.get("profile").asText()).as(description + " short frame").isEqualTo("lansitec-temperature-humidity-sensor"); + assertThat(telemetryValues(result).has("loraSnr")).as(description + " short frame").isFalse(); + + result = decoder.apply(bytesList); + assertThat(result.get("profile").asText()).as(description + " bytes list").isEqualTo("lansitec-temperature-humidity-sensor"); + assertThat(numbersByValue(telemetryValues(result).get("bytes"))).as(description + " bytes list") + .isEqualTo(numbersByValue(JacksonUtil.toJsonNode("[33, 100]"))); + }); + } + + private interface ThingsStackDecoderCheck { + void check(String description, java.util.function.Function decoder) throws IOException; + } + + private void forEachThingsStackDefaultConverter(ThingsStackDecoderCheck check) throws IOException { + JsonNode defaultConfigurations = JacksonUtil.toJsonNode(readResource(DEFAULT_V2_CONFIGURATION)); for (IntegrationType integrationType : List.of(IntegrationType.TTN, IntegrationType.TTI)) { Converter converter = createConverter(defaultConfigurations.get(integrationType.name()), integrationType, 2); for (ScriptLanguage scriptLang : ScriptLanguage.values()) { String decoderFileName = "converters/" + scriptLang.name().toLowerCase() + "-" + integrationType.name().toLowerCase() + "-decoder.raw"; - for (JsonNode testCase : cases) { - String description = integrationType + " " + scriptLang + " " + testCase.get("name").asText(); - JsonNode result = JacksonUtil.toJsonNode(decode(decoderFileName, testCase.get("uplink").toString(), converter, scriptLang, Map.of())); - JsonNode decoded = testCase.get("decoded"); - assertThat(result.get("profile")).as(description).isEqualTo(decoded.get("profile")); - - JsonNode telemetry = result.get("telemetry"); - if (telemetry != null && telemetry.isArray()) { - telemetry = telemetry.get(telemetry.size() - 1); - } - JsonNode telemetryValues = telemetry != null && telemetry.has("values") ? telemetry.get("values") : JacksonUtil.newObjectNode(); - ObjectNode lansitecTelemetry = JacksonUtil.newObjectNode(); - telemetryValues.fields().forEachRemaining(e -> { - if (!THINGS_STACK_UPLINK_KEYS.contains(e.getKey())) { - lansitecTelemetry.set(e.getKey(), e.getValue()); - } - }); - assertThat(numbersByValue(lansitecTelemetry)).as(description + " telemetry") - .isEqualTo(numbersByValue(decoded.has("telemetry") ? decoded.get("telemetry") : JacksonUtil.newObjectNode())); - - if (decoded.has("attributes")) { - ObjectNode attributes = JacksonUtil.newObjectNode(); - decoded.get("attributes").fieldNames().forEachRemaining(key -> attributes.set(key, result.get("attributes").get(key))); - assertThat(numbersByValue(attributes)).as(description + " attributes").isEqualTo(numbersByValue(decoded.get("attributes"))); + check.check(integrationType + " " + scriptLang, uplink -> { + try { + return JacksonUtil.toJsonNode(decode(decoderFileName, uplink.toString(), converter, scriptLang, Map.of())); + } catch (IOException e) { + throw new RuntimeException(e); } - } + }); } } } + private static JsonNode telemetryValues(JsonNode result) { + JsonNode telemetry = result.get("telemetry"); + if (telemetry != null && telemetry.isArray()) { + telemetry = telemetry.get(telemetry.size() - 1); + } + return telemetry != null && telemetry.has("values") ? telemetry.get("values") : JacksonUtil.newObjectNode(); + } + + private static ObjectNode withoutKeys(JsonNode node, Set keys) { + ObjectNode copy = JacksonUtil.newObjectNode(); + if (node != null) { + node.fields().forEachRemaining(e -> { + if (!keys.contains(e.getKey())) { + copy.set(e.getKey(), e.getValue()); + } + }); + } + return copy; + } + private static JsonNode numbersByValue(JsonNode node) { if (node == null) { return null; diff --git a/ui-ngx/src/assets/converters/js-tti-decoder.raw b/ui-ngx/src/assets/converters/js-tti-decoder.raw index cc7223254c..e3257d7c4b 100644 --- a/ui-ngx/src/assets/converters/js-tti-decoder.raw +++ b/ui-ngx/src/assets/converters/js-tti-decoder.raw @@ -214,6 +214,9 @@ function lansitecBeaconList(beacons) { // Decoded from the raw bytes by the datasheet where the codec gets it wrong. function lansitecRawValues(values, b, model) { + if (b.length == 0) { + return; + } var type = (b[0] >> 4) & 0x0F; if (LANSITEC_GATEWAYS.indexOf(model) >= 0) { lansitecGatewayRawValues(values, b, model, type); @@ -389,14 +392,13 @@ function lansitecBadgeRawValues(values, b, type) { // it is not and the message is 8 bytes. The codec always reads the SNR, so with VER 0 the next fields are shifted. // TEMP_INT bit 7 is the sign, TEMP_FRA the hundredths. function lansitecTemperatureHumidityRawValues(values, b) { - var offset = 3; - if ((b[0] & 0x0F) == 1) { - values.loraSnr = lansitecSnr(b, 3); - offset = 5; - } + var offset = (b[0] & 0x0F) == 1 ? 5 : 3; if (b.length < offset + 3) { return; } + if (offset == 5) { + values.loraSnr = lansitecSnr(b, 3); + } var temperature = (b[offset] & 0x7F) + b[offset + 1] / 100; if ((b[offset] & 0x80) != 0) { temperature = -temperature; @@ -410,7 +412,7 @@ var attributes = {}; var history = []; if (metadata.payloadFormat == 'JSON') { values = decodeToJson(payload); - if (metadata.brandId == 'lansitec' && values.bytes != null) { + if (metadata.brandId == 'lansitec' && values.bytes != null && typeof values.bytes === 'object' && !Array.isArray(values.bytes)) { // The Lansitec codecs put everything in "bytes" and get some fields wrong: see the Lansitec block above. var raw = values.bytes; values = lansitecValues(raw, metadata.modelId); diff --git a/ui-ngx/src/assets/converters/js-ttn-decoder.raw b/ui-ngx/src/assets/converters/js-ttn-decoder.raw index cc7223254c..e3257d7c4b 100644 --- a/ui-ngx/src/assets/converters/js-ttn-decoder.raw +++ b/ui-ngx/src/assets/converters/js-ttn-decoder.raw @@ -214,6 +214,9 @@ function lansitecBeaconList(beacons) { // Decoded from the raw bytes by the datasheet where the codec gets it wrong. function lansitecRawValues(values, b, model) { + if (b.length == 0) { + return; + } var type = (b[0] >> 4) & 0x0F; if (LANSITEC_GATEWAYS.indexOf(model) >= 0) { lansitecGatewayRawValues(values, b, model, type); @@ -389,14 +392,13 @@ function lansitecBadgeRawValues(values, b, type) { // it is not and the message is 8 bytes. The codec always reads the SNR, so with VER 0 the next fields are shifted. // TEMP_INT bit 7 is the sign, TEMP_FRA the hundredths. function lansitecTemperatureHumidityRawValues(values, b) { - var offset = 3; - if ((b[0] & 0x0F) == 1) { - values.loraSnr = lansitecSnr(b, 3); - offset = 5; - } + var offset = (b[0] & 0x0F) == 1 ? 5 : 3; if (b.length < offset + 3) { return; } + if (offset == 5) { + values.loraSnr = lansitecSnr(b, 3); + } var temperature = (b[offset] & 0x7F) + b[offset + 1] / 100; if ((b[offset] & 0x80) != 0) { temperature = -temperature; @@ -410,7 +412,7 @@ var attributes = {}; var history = []; if (metadata.payloadFormat == 'JSON') { values = decodeToJson(payload); - if (metadata.brandId == 'lansitec' && values.bytes != null) { + if (metadata.brandId == 'lansitec' && values.bytes != null && typeof values.bytes === 'object' && !Array.isArray(values.bytes)) { // The Lansitec codecs put everything in "bytes" and get some fields wrong: see the Lansitec block above. var raw = values.bytes; values = lansitecValues(raw, metadata.modelId); diff --git a/ui-ngx/src/assets/converters/tbel-tti-decoder.raw b/ui-ngx/src/assets/converters/tbel-tti-decoder.raw index 2e675de454..6052f41f4a 100644 --- a/ui-ngx/src/assets/converters/tbel-tti-decoder.raw +++ b/ui-ngx/src/assets/converters/tbel-tti-decoder.raw @@ -212,6 +212,9 @@ function lansitecBeaconList(beacons) { // Decoded from the raw bytes by the datasheet where the codec gets it wrong. function lansitecRawValues(values, b, model) { + if (b.length == 0) { + return; + } var type = (b[0] >> 4) & 0x0F; if (LANSITEC_GATEWAYS.contains(model)) { lansitecGatewayRawValues(values, b, model, type); @@ -345,14 +348,13 @@ function lansitecBadgeRawValues(values, b, type) { // it is not and the message is 8 bytes. The codec always reads the SNR, so with VER 0 the next fields are shifted. // TEMP_INT bit 7 is the sign, TEMP_FRA the hundredths. function lansitecTemperatureHumidityRawValues(values, b) { - var offset = 3; - if ((b[0] & 0x0F) == 1) { - values.loraSnr = lansitecSnr(b, 3); - offset = 5; - } + var offset = (b[0] & 0x0F) == 1 ? 5 : 3; if (b.length < offset + 3) { return; } + if (offset == 5) { + values.loraSnr = lansitecSnr(b, 3); + } var temperature = (b[offset] & 0x7F) + (b[offset + 1] & 0xFF) / 100.0; if ((b[offset] & 0x80) != 0) { temperature = -temperature; @@ -366,7 +368,7 @@ var attributes = {}; var history = []; if (metadata.payloadFormat == "JSON") { values = decodeToJson(payload); - if (metadata.brandId == "lansitec" && values.bytes != null) { + if (metadata.brandId == "lansitec" && isMap(values.bytes)) { // The Lansitec codecs put everything in "bytes" and get some fields wrong: see the Lansitec block above. var raw = values.bytes; values = lansitecValues(raw, metadata.modelId); diff --git a/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw b/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw index 2e675de454..6052f41f4a 100644 --- a/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw +++ b/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw @@ -212,6 +212,9 @@ function lansitecBeaconList(beacons) { // Decoded from the raw bytes by the datasheet where the codec gets it wrong. function lansitecRawValues(values, b, model) { + if (b.length == 0) { + return; + } var type = (b[0] >> 4) & 0x0F; if (LANSITEC_GATEWAYS.contains(model)) { lansitecGatewayRawValues(values, b, model, type); @@ -345,14 +348,13 @@ function lansitecBadgeRawValues(values, b, type) { // it is not and the message is 8 bytes. The codec always reads the SNR, so with VER 0 the next fields are shifted. // TEMP_INT bit 7 is the sign, TEMP_FRA the hundredths. function lansitecTemperatureHumidityRawValues(values, b) { - var offset = 3; - if ((b[0] & 0x0F) == 1) { - values.loraSnr = lansitecSnr(b, 3); - offset = 5; - } + var offset = (b[0] & 0x0F) == 1 ? 5 : 3; if (b.length < offset + 3) { return; } + if (offset == 5) { + values.loraSnr = lansitecSnr(b, 3); + } var temperature = (b[offset] & 0x7F) + (b[offset + 1] & 0xFF) / 100.0; if ((b[offset] & 0x80) != 0) { temperature = -temperature; @@ -366,7 +368,7 @@ var attributes = {}; var history = []; if (metadata.payloadFormat == "JSON") { values = decodeToJson(payload); - if (metadata.brandId == "lansitec" && values.bytes != null) { + if (metadata.brandId == "lansitec" && isMap(values.bytes)) { // The Lansitec codecs put everything in "bytes" and get some fields wrong: see the Lansitec block above. var raw = values.bytes; values = lansitecValues(raw, metadata.modelId); From 105ddcfd88912e20188d15f19dedd5212ca849a5 Mon Sep 17 00:00:00 2001 From: dpinkevych Date: Mon, 2 Nov 2026 16:24:53 +0200 Subject: [PATCH 05/13] fix(iot-hub): review of the Lansitec converter: no second UTF-8 pass in JS, no position from a NaN float, signed SocketSync SNR; the test covers a Multi-Device frame and checks rssi, snr and firmwareVersion --- .../controller/ConverterControllerTest.java | 33 ++- .../things_stack_lansitec_uplinks.json | 210 ++++++++++++++++++ .../src/assets/converters/js-tti-decoder.raw | 20 +- .../src/assets/converters/js-ttn-decoder.raw | 20 +- .../assets/converters/tbel-tti-decoder.raw | 11 +- .../assets/converters/tbel-ttn-decoder.raw | 11 +- 6 files changed, 284 insertions(+), 21 deletions(-) diff --git a/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java index 56a9237097..d7999fac6d 100644 --- a/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java @@ -756,10 +756,37 @@ public class ConverterControllerTest extends AbstractControllerTest { .isEqualTo(numbersByValue(decoded.has("telemetry") ? decoded.get("telemetry") : JacksonUtil.newObjectNode())); assertThat(numbersByValue(withoutKeys(result.get("attributes"), THINGS_STACK_ATTRIBUTE_KEYS))).as(caseDescription + " attributes") .isEqualTo(numbersByValue(decoded.has("attributes") ? decoded.get("attributes") : JacksonUtil.newObjectNode())); + + // The integration's own keys keep their values: the Lansitec keys must not overwrite them + JsonNode uplinkMessage = testCase.get("uplink").get("uplink_message"); + JsonNode gateway = null; + for (JsonNode rx : uplinkMessage.get("rx_metadata")) { + if (gateway == null || rx.get("rssi").asDouble() > gateway.get("rssi").asDouble()) { + gateway = rx; + } + } + JsonNode values = telemetryValues(result); + assertThat(sameValue(values.get("rssi"), gateway.get("rssi"))).as(caseDescription + " rssi " + values.get("rssi")).isTrue(); + assertThat(sameValue(values.get("snr"), gateway.get("snr"))).as(caseDescription + " snr " + values.get("snr")).isTrue(); + JsonNode firmwareVersion = result.get("attributes").get("firmwareVersion"); + assertThat(sameValue(firmwareVersion, uplinkMessage.get("version_ids").get("firmware_version"))) + .as(caseDescription + " firmwareVersion " + firmwareVersion).isTrue(); } }); } + // Equal as numbers when both are numbers or number texts (the integration may write "1.0" as 1), else as texts + private static boolean sameValue(JsonNode actual, JsonNode expected) { + if (actual == null || expected == null) { + return actual == expected; + } + try { + return new java.math.BigDecimal(actual.asText()).compareTo(new java.math.BigDecimal(expected.asText())) == 0; + } catch (NumberFormatException e) { + return actual.asText().equals(expected.asText()); + } + } + // A frame too short for its message type, and bytes that are not an object: the uplink is still converted @Test public void testThingsStackDefaultConvertersKeepMalformedLansitecUplinks() throws IOException { @@ -812,7 +839,11 @@ public class ConverterControllerTest extends AbstractControllerTest { if (telemetry != null && telemetry.isArray()) { telemetry = telemetry.get(telemetry.size() - 1); } - return telemetry != null && telemetry.has("values") ? telemetry.get("values") : JacksonUtil.newObjectNode(); + if (telemetry == null) { + return JacksonUtil.newObjectNode(); + } + // {ts, values}, or only the integration's keys when the script gave attributes and no telemetry + return telemetry.has("values") ? telemetry.get("values") : telemetry; } private static ObjectNode withoutKeys(JsonNode node, Set keys) { diff --git a/application/src/test/resources/payload/things_stack_lansitec_uplinks.json b/application/src/test/resources/payload/things_stack_lansitec_uplinks.json index 5b1aefd85f..1f36af2b88 100644 --- a/application/src/test/resources/payload/things_stack_lansitec_uplinks.json +++ b/application/src/test/resources/payload/things_stack_lansitec_uplinks.json @@ -1440,5 +1440,215 @@ "vol": 100.0 } } + }, + { + "name": "gw Multi 4.4.7, same data length (built from 4.4.7)", + "uplink": { + "end_device_ids": { + "device_id": "lt-compact-bluetooth-gateway", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0000", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6E9MR7XDSV8F47HYQXZWNR9", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:3cb978ee-9803-4d8d-a720-f92b553f8f89" + ], + "received_at": "2026-11-02T14:12:05.501610818Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "4gEGGkwAAhWqu7MBBhpMAAIVqry0", + "decoded_payload": { + "bytes": { + "Device1ID": "Device type 1 message", + "Device1data": "061A4C000215AABB", + "Device1rssi": "-77dBm", + "Device2ID": "Device type 1 message", + "Device2data": "061A4C000215AABC", + "Device2rssi": "-76dBm", + "number": 2, + "type": "Multi-Device Type Message" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "compact-bluetooth-gateway", + "hardware_version": "1.0", + "firmware_version": "1.0", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-compact-bluetooth-gateway", + "telemetry": { + "beacons": [ + { + "deviceType": 1, + "data": "061A4C000215AABB", + "rssi": -77 + }, + { + "deviceType": 1, + "data": "061A4C000215AABC", + "rssi": -76 + } + ] + } + } + }, + { + "name": "socketsync Heartbeat SNR -5.00 (built)", + "uplink": { + "end_device_ids": { + "device_id": "lt-socket-sync-bluetooth-gateway", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0004", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6E9MXMXMTGXT2EQG0C1KR2H", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:f011a6e8-29c0-40e0-97a6-3a4c763b6a07" + ], + "received_at": "2026-11-02T14:12:11.036297935Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "IWQ+/gwAbFA=", + "decoded_payload": { + "bytes": { + "chgstate": "Not charging", + "rssi": "-62dBm", + "snr": "650.36dB", + "type": "Heartbeat", + "ver": "VER108", + "vol": "100%" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "socket-sync-bluetooth-gateway", + "hardware_version": "1.0", + "firmware_version": "1.0", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-socket-sync-bluetooth-gateway", + "telemetry": { + "loraRssi": -62, + "loraSnr": -5.0, + "ver": "VER108", + "vol": 100 + } + } + }, + { + "name": "badge GNSS longitude NaN (built)", + "uplink": { + "end_device_ids": { + "device_id": "lt-badge-tracker", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0007", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6E9N31RAQD5PHPCQR3KYFT6", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:e23ebb92-d85e-4c68-8648-1a77648991d3" + ], + "received_at": "2026-11-02T14:12:16.566781944Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "MH/AAAFB/84uZXLoqA==", + "decoded_payload": { + "bytes": { + "Latitude": 31.975675579986046, + "Longitude": 5.1042361122864376e+38, + "Message type": "GNSSPosition", + "Time": "\"2023-12-08T17:58:00.000Z\"" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "badge-tracker", + "hardware_version": "1.0", + "firmware_version": "5.25", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-badge-tracker", + "telemetry": {} + } } ] diff --git a/ui-ngx/src/assets/converters/js-tti-decoder.raw b/ui-ngx/src/assets/converters/js-tti-decoder.raw index e3257d7c4b..7736fbcbc5 100644 --- a/ui-ngx/src/assets/converters/js-tti-decoder.raw +++ b/ui-ngx/src/assets/converters/js-tti-decoder.raw @@ -224,6 +224,9 @@ function lansitecRawValues(values, b, model) { lansitecBadgeRawValues(values, b, type); } else if (model == 'temperature-humidity-sensor' && type == 2) { lansitecTemperatureHumidityRawValues(values, b); + } else if (model == 'socket-sync-bluetooth-gateway' && type == 2 && b.length >= 8) { + // No datasheet: the codec reads the heartbeat as the compact gateway's (4.4.2), SNR in bytes 3-4 unsigned. + values.loraSnr = lansitecSnr(b, 3); } } @@ -265,6 +268,11 @@ function lansitecUint(b, offset, length) { } // IEEE 754 single precision, big-endian, rounded to 6 decimals (about 0.1 m) as in the TBEL converter. +// IEEE 754 float, big-endian at offset: false when its exponent is 0xFF (NaN or infinity). +function lansitecFloatIsNumber(b, offset) { + return !((b[offset] & 0x7F) == 0x7F && (b[offset + 1] & 0x80) != 0); +} + function lansitecFloat(b, offset) { var bits = lansitecUint(b, offset, 4); var exponent = Math.floor(bits / 8388608) % 256; @@ -380,7 +388,8 @@ function lansitecBadgeRawValues(values, b, type) { } else if (type == 3 && b.length >= 13) { // 5.3: Flag (Type bits 3-0) 0x1 means GNSS positioning failed. Longitude and latitude are IEEE 754 floats // (the codec divides the mantissa by 0x7fffff instead of 2^23), time is UTC seconds (the codec adds 8 hours). - if ((b[0] & 0x0F) == 0) { + // An exponent of 0xFF is not a number (NaN or infinity): no position, as in the TBEL converter. + if ((b[0] & 0x0F) == 0 && lansitecFloatIsNumber(b, 1) && lansitecFloatIsNumber(b, 5)) { values.longitude = lansitecFloat(b, 1); values.latitude = lansitecFloat(b, 5); values.gnssTime = lansitecUint(b, 9, 4) * 1000; @@ -454,13 +463,6 @@ if (history.length == 0) { history.push({ ts: metadata.ts, values: values }); return { profile: profile, telemetry: history }; -// A codec value can have a unit like ℃. Where the payload comes as UTF-8 bytes, they are decoded; where it already -// comes as characters, the decoding fails and the text is taken as it is. function decodeToJson(payload) { - var text = String.fromCharCode.apply(String, payload); - try { - text = decodeURIComponent(escape(text)); - } catch (e) { - } - return JSON.parse(text); + return JSON.parse(String.fromCharCode.apply(String, payload)); } diff --git a/ui-ngx/src/assets/converters/js-ttn-decoder.raw b/ui-ngx/src/assets/converters/js-ttn-decoder.raw index e3257d7c4b..7736fbcbc5 100644 --- a/ui-ngx/src/assets/converters/js-ttn-decoder.raw +++ b/ui-ngx/src/assets/converters/js-ttn-decoder.raw @@ -224,6 +224,9 @@ function lansitecRawValues(values, b, model) { lansitecBadgeRawValues(values, b, type); } else if (model == 'temperature-humidity-sensor' && type == 2) { lansitecTemperatureHumidityRawValues(values, b); + } else if (model == 'socket-sync-bluetooth-gateway' && type == 2 && b.length >= 8) { + // No datasheet: the codec reads the heartbeat as the compact gateway's (4.4.2), SNR in bytes 3-4 unsigned. + values.loraSnr = lansitecSnr(b, 3); } } @@ -265,6 +268,11 @@ function lansitecUint(b, offset, length) { } // IEEE 754 single precision, big-endian, rounded to 6 decimals (about 0.1 m) as in the TBEL converter. +// IEEE 754 float, big-endian at offset: false when its exponent is 0xFF (NaN or infinity). +function lansitecFloatIsNumber(b, offset) { + return !((b[offset] & 0x7F) == 0x7F && (b[offset + 1] & 0x80) != 0); +} + function lansitecFloat(b, offset) { var bits = lansitecUint(b, offset, 4); var exponent = Math.floor(bits / 8388608) % 256; @@ -380,7 +388,8 @@ function lansitecBadgeRawValues(values, b, type) { } else if (type == 3 && b.length >= 13) { // 5.3: Flag (Type bits 3-0) 0x1 means GNSS positioning failed. Longitude and latitude are IEEE 754 floats // (the codec divides the mantissa by 0x7fffff instead of 2^23), time is UTC seconds (the codec adds 8 hours). - if ((b[0] & 0x0F) == 0) { + // An exponent of 0xFF is not a number (NaN or infinity): no position, as in the TBEL converter. + if ((b[0] & 0x0F) == 0 && lansitecFloatIsNumber(b, 1) && lansitecFloatIsNumber(b, 5)) { values.longitude = lansitecFloat(b, 1); values.latitude = lansitecFloat(b, 5); values.gnssTime = lansitecUint(b, 9, 4) * 1000; @@ -454,13 +463,6 @@ if (history.length == 0) { history.push({ ts: metadata.ts, values: values }); return { profile: profile, telemetry: history }; -// A codec value can have a unit like ℃. Where the payload comes as UTF-8 bytes, they are decoded; where it already -// comes as characters, the decoding fails and the text is taken as it is. function decodeToJson(payload) { - var text = String.fromCharCode.apply(String, payload); - try { - text = decodeURIComponent(escape(text)); - } catch (e) { - } - return JSON.parse(text); + return JSON.parse(String.fromCharCode.apply(String, payload)); } diff --git a/ui-ngx/src/assets/converters/tbel-tti-decoder.raw b/ui-ngx/src/assets/converters/tbel-tti-decoder.raw index 6052f41f4a..79fac578f8 100644 --- a/ui-ngx/src/assets/converters/tbel-tti-decoder.raw +++ b/ui-ngx/src/assets/converters/tbel-tti-decoder.raw @@ -222,9 +222,17 @@ function lansitecRawValues(values, b, model) { lansitecBadgeRawValues(values, b, type); } else if (model == "temperature-humidity-sensor" && type == 2) { lansitecTemperatureHumidityRawValues(values, b); + } else if (model == "socket-sync-bluetooth-gateway" && type == 2 && b.length >= 8) { + // No datasheet: the codec reads the heartbeat as the compact gateway's (4.4.2), SNR in bytes 3-4 unsigned. + values.loraSnr = lansitecSnr(b, 3); } } +// IEEE 754 float, big-endian at offset: false when its exponent is 0xFF (NaN or infinity). +function lansitecFloatIsNumber(b, offset) { + return !((b[offset] & 0x7F) == 0x7F && (b[offset + 1] & 0x80) != 0); +} + // SNR, unit 0.01 dB, -3000 to 3000: signed. The codecs read it unsigned. function lansitecSnr(b, offset) { var v = parseBytesToInt(b, offset, 2, true); @@ -336,7 +344,8 @@ function lansitecBadgeRawValues(values, b, type) { // 5.3: Flag (Type bits 3-0) 0x1 means GNSS positioning failed. Longitude and latitude are IEEE 754 floats // (the codec divides the mantissa by 0x7fffff instead of 2^23), time is UTC seconds (the codec adds 8 hours). // Rounded to 6 decimals (about 0.1 m), as a double: the float has no more precision. - if ((b[0] & 0x0F) == 0) { + // An exponent of 0xFF is not a number (NaN or infinity): parseBytesToFloat fails on NaN, so no position. + if ((b[0] & 0x0F) == 0 && lansitecFloatIsNumber(b, 1) && lansitecFloatIsNumber(b, 5)) { values.longitude = toFixed(parseBytesToFloat(b, 1, 4, true) * 1.0, 6); values.latitude = toFixed(parseBytesToFloat(b, 5, 4, true) * 1.0, 6); values.gnssTime = parseBytesToLong(b, 9, 4, true) * 1000; diff --git a/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw b/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw index 6052f41f4a..79fac578f8 100644 --- a/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw +++ b/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw @@ -222,9 +222,17 @@ function lansitecRawValues(values, b, model) { lansitecBadgeRawValues(values, b, type); } else if (model == "temperature-humidity-sensor" && type == 2) { lansitecTemperatureHumidityRawValues(values, b); + } else if (model == "socket-sync-bluetooth-gateway" && type == 2 && b.length >= 8) { + // No datasheet: the codec reads the heartbeat as the compact gateway's (4.4.2), SNR in bytes 3-4 unsigned. + values.loraSnr = lansitecSnr(b, 3); } } +// IEEE 754 float, big-endian at offset: false when its exponent is 0xFF (NaN or infinity). +function lansitecFloatIsNumber(b, offset) { + return !((b[offset] & 0x7F) == 0x7F && (b[offset + 1] & 0x80) != 0); +} + // SNR, unit 0.01 dB, -3000 to 3000: signed. The codecs read it unsigned. function lansitecSnr(b, offset) { var v = parseBytesToInt(b, offset, 2, true); @@ -336,7 +344,8 @@ function lansitecBadgeRawValues(values, b, type) { // 5.3: Flag (Type bits 3-0) 0x1 means GNSS positioning failed. Longitude and latitude are IEEE 754 floats // (the codec divides the mantissa by 0x7fffff instead of 2^23), time is UTC seconds (the codec adds 8 hours). // Rounded to 6 decimals (about 0.1 m), as a double: the float has no more precision. - if ((b[0] & 0x0F) == 0) { + // An exponent of 0xFF is not a number (NaN or infinity): parseBytesToFloat fails on NaN, so no position. + if ((b[0] & 0x0F) == 0 && lansitecFloatIsNumber(b, 1) && lansitecFloatIsNumber(b, 5)) { values.longitude = toFixed(parseBytesToFloat(b, 1, 4, true) * 1.0, 6); values.latitude = toFixed(parseBytesToFloat(b, 5, 4, true) * 1.0, 6); values.gnssTime = parseBytesToLong(b, 9, 4, true) * 1000; From da6ab6649c0325090a25aceb18403058c8ca352a Mon Sep 17 00:00:00 2001 From: Dmytro Pinkevych Date: Tue, 3 Nov 2026 11:46:44 +0200 Subject: [PATCH 06/13] fix(iot-hub): the rounding comment of the Lansitec float sits above lansitecFloat again --- ui-ngx/src/assets/converters/js-tti-decoder.raw | 2 +- ui-ngx/src/assets/converters/js-ttn-decoder.raw | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ui-ngx/src/assets/converters/js-tti-decoder.raw b/ui-ngx/src/assets/converters/js-tti-decoder.raw index 7736fbcbc5..f4f0d648cf 100644 --- a/ui-ngx/src/assets/converters/js-tti-decoder.raw +++ b/ui-ngx/src/assets/converters/js-tti-decoder.raw @@ -267,12 +267,12 @@ function lansitecUint(b, offset, length) { return v; } -// IEEE 754 single precision, big-endian, rounded to 6 decimals (about 0.1 m) as in the TBEL converter. // IEEE 754 float, big-endian at offset: false when its exponent is 0xFF (NaN or infinity). function lansitecFloatIsNumber(b, offset) { return !((b[offset] & 0x7F) == 0x7F && (b[offset + 1] & 0x80) != 0); } +// IEEE 754 single precision, big-endian, rounded to 6 decimals (about 0.1 m) as in the TBEL converter. function lansitecFloat(b, offset) { var bits = lansitecUint(b, offset, 4); var exponent = Math.floor(bits / 8388608) % 256; diff --git a/ui-ngx/src/assets/converters/js-ttn-decoder.raw b/ui-ngx/src/assets/converters/js-ttn-decoder.raw index 7736fbcbc5..f4f0d648cf 100644 --- a/ui-ngx/src/assets/converters/js-ttn-decoder.raw +++ b/ui-ngx/src/assets/converters/js-ttn-decoder.raw @@ -267,12 +267,12 @@ function lansitecUint(b, offset, length) { return v; } -// IEEE 754 single precision, big-endian, rounded to 6 decimals (about 0.1 m) as in the TBEL converter. // IEEE 754 float, big-endian at offset: false when its exponent is 0xFF (NaN or infinity). function lansitecFloatIsNumber(b, offset) { return !((b[offset] & 0x7F) == 0x7F && (b[offset + 1] & 0x80) != 0); } +// IEEE 754 single precision, big-endian, rounded to 6 decimals (about 0.1 m) as in the TBEL converter. function lansitecFloat(b, offset) { var bits = lansitecUint(b, offset, 4); var exponent = Math.floor(bits / 8388608) % 256; From 4fcfe6c8988c1269915049f891fafbaf131b3750 Mon Sep 17 00:00:00 2001 From: dpinkevych Date: Wed, 4 Nov 2026 11:14:18 +0200 Subject: [PATCH 07/13] fix(iot-hub): the SocketSync gateway is decoded by its own datasheet (Plug Bluetooth Gateway DataSheet 1.5): Registration, Heartbeat, Beacon, Alarm Beacon List and Acknowledgment from the raw frame; the test has an uplink of each, built from the datasheet; BigDecimal imported in the test --- .../controller/ConverterControllerTest.java | 3 +- .../things_stack_lansitec_uplinks.json | 309 +++++++++++++++--- .../src/assets/converters/js-tti-decoder.raw | 106 +++++- .../src/assets/converters/js-ttn-decoder.raw | 106 +++++- .../assets/converters/tbel-tti-decoder.raw | 116 ++++++- .../assets/converters/tbel-ttn-decoder.raw | 116 ++++++- 6 files changed, 667 insertions(+), 89 deletions(-) diff --git a/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java index d7999fac6d..1abe155e74 100644 --- a/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java @@ -41,6 +41,7 @@ import org.thingsboard.server.exception.DataValidationException; import java.io.IOException; import java.io.InputStream; +import java.math.BigDecimal; import java.math.RoundingMode; import java.nio.charset.StandardCharsets; import java.util.ArrayList; @@ -781,7 +782,7 @@ public class ConverterControllerTest extends AbstractControllerTest { return actual == expected; } try { - return new java.math.BigDecimal(actual.asText()).compareTo(new java.math.BigDecimal(expected.asText())) == 0; + return new BigDecimal(actual.asText()).compareTo(new BigDecimal(expected.asText())) == 0; } catch (NumberFormatException e) { return actual.asText().equals(expected.asText()); } diff --git a/application/src/test/resources/payload/things_stack_lansitec_uplinks.json b/application/src/test/resources/payload/things_stack_lansitec_uplinks.json index 1f36af2b88..1d5ce0b72c 100644 --- a/application/src/test/resources/payload/things_stack_lansitec_uplinks.json +++ b/application/src/test/resources/payload/things_stack_lansitec_uplinks.json @@ -1373,7 +1373,7 @@ } }, { - "name": "socketsync Heartbeat 0x50", + "name": "socketsync Registration 5.1 (built from DataSheet 1.5)", "uplink": { "end_device_ids": { "device_id": "lt-socket-sync-bluetooth-gateway", @@ -1390,15 +1390,83 @@ "received_at": "2026-11-02T10:32:54.523762899Z", "uplink_message": { "f_port": 1, - "frm_payload": "IWQ+Az4AbFA=", + "frm_payload": "RiCCFABaEAr/Onw=", + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "socket-sync-bluetooth-gateway", + "hardware_version": "1.0", + "firmware_version": "1.0", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-socket-sync-bluetooth-gateway", + "attributes": { + "adr": "OFF", + "mode": "EU868", + "loraWanFrequencyBand": "EU868", + "loraTxPower": 16, + "continuousBleReceiving": "Continuous Bluetooth receiving is enabled", + "powerDownFlag": "Normal status", + "dataRate": "DR1", + "gatewayType": "Plug gateway", + "rssiSortingMethod": "Sort by average", + "positionReportInterval": 450, + "bluetoothReceivingDuration": 16, + "heartbeatInterval": 300, + "beaconQuantityLimit": 510 + } + } + }, + { + "name": "socketsync Heartbeat 5.2, SNR -5.00, Charging (built from DataSheet 1.5)", + "uplink": { + "end_device_ids": { + "device_id": "lt-socket-sync-bluetooth-gateway", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0004", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6DX3DFWNQE3WSG9JNFTAG7B", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:ec26c3c1-e861-4d65-b6e7-1206e538a5c7" + ], + "received_at": "2026-11-02T10:32:54.523762899Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "IVA+/gwbUDp8", "decoded_payload": { "bytes": { - "chgstate": "Not charging", - "rssi": "-62dBm", - "snr": "8.3dB", "type": "Heartbeat", - "ver": "VER108", - "vol": "100%" + "vol": "80%", + "rssi": "-62dBm", + "snr": "650.36dB", + "ver": "VER6992", + "chgstate": 10 } }, "rx_metadata": [ @@ -1434,42 +1502,109 @@ "decoded": { "profile": "lansitec-socket-sync-bluetooth-gateway", "telemetry": { - "loraRssi": -62.0, - "loraSnr": 8.3, - "ver": "VER108", - "vol": 100.0 + "vol": 80, + "loraRssi": -62, + "loraSnr": -5.0, + "deviceFirmwareVersion": "1B", + "chargeState": "Charging" } } }, { - "name": "gw Multi 4.4.7, same data length (built from 4.4.7)", + "name": "socketsync Beacon 5.3 (built from DataSheet 1.5)", "uplink": { "end_device_ids": { - "device_id": "lt-compact-bluetooth-gateway", + "device_id": "lt-socket-sync-bluetooth-gateway", "application_ids": { "application_id": "ansitec-test" }, - "dev_eui": "70B3D57ED01A0000", + "dev_eui": "70B3D57ED01A0004", "join_eui": "0000000000000000" }, "correlation_ids": [ - "as:up:01M6E9MR7XDSV8F47HYQXZWNR9", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:3cb978ee-9803-4d8d-a720-f92b553f8f89" + "as:up:01M6DX3DFWNQE3WSG9JNFTAG7B", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:ec26c3c1-e861-4d65-b6e7-1206e538a5c7" ], - "received_at": "2026-11-02T14:12:05.501610818Z", + "received_at": "2026-11-02T10:32:54.523762899Z", "uplink_message": { "f_port": 1, - "frm_payload": "4gEGGkwAAhWqu7MBBhpMAAIVqry0", + "frm_payload": "YlYAAQACqlcAAwAEsA==", + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "socket-sync-bluetooth-gateway", + "hardware_version": "1.0", + "firmware_version": "1.0", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-socket-sync-bluetooth-gateway", + "telemetry": { + "beacons": [ + { + "uuid": "5C38DBDE567C4CCAB1DA40A8AD465656", + "major": 1, + "minor": 2, + "rssi": -86 + }, + { + "uuid": "5C38DBDE567C4CCAB1DA40A8AD465657", + "major": 3, + "minor": 4, + "rssi": -80 + } + ] + } + } + }, + { + "name": "socketsync Alarm Beacon List 5.4 (built from DataSheet 1.5)", + "uplink": { + "end_device_ids": { + "device_id": "lt-socket-sync-bluetooth-gateway", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0004", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6DX3DFWNQE3WSG9JNFTAG7B", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:ec26c3c1-e861-4d65-b6e7-1206e538a5c7" + ], + "received_at": "2026-11-02T10:32:54.523762899Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "4VgACgALtQ==", "decoded_payload": { "bytes": { - "Device1ID": "Device type 1 message", - "Device1data": "061A4C000215AABB", - "Device1rssi": "-77dBm", - "Device2ID": "Device type 1 message", - "Device2data": "061A4C000215AABC", - "Device2rssi": "-76dBm", - "number": 2, - "type": "Multi-Device Type Message" + "type": "Multi-Device Type Message", + "number": 1, + "Device1ID": 0, + "Device1data": "000A000B", + "Device1rssi": "-75dBm" } }, "rx_metadata": [ @@ -1494,7 +1629,7 @@ }, "version_ids": { "brand_id": "lansitec", - "model_id": "compact-bluetooth-gateway", + "model_id": "socket-sync-bluetooth-gateway", "hardware_version": "1.0", "firmware_version": "1.0", "band_id": "EU_863_870" @@ -1503,25 +1638,21 @@ "simulated": true }, "decoded": { - "profile": "lansitec-compact-bluetooth-gateway", + "profile": "lansitec-socket-sync-bluetooth-gateway", "telemetry": { - "beacons": [ - { - "deviceType": 1, - "data": "061A4C000215AABB", - "rssi": -77 - }, + "alarmBeacons": [ { - "deviceType": 1, - "data": "061A4C000215AABC", - "rssi": -76 + "uuid": "5C38DBDE567C4CCAB1DA40A8AD465658", + "major": 10, + "minor": 11, + "rssi": -75 } ] } } }, { - "name": "socketsync Heartbeat SNR -5.00 (built)", + "name": "socketsync Acknowledgment 5.5 (built from DataSheet 1.5)", "uplink": { "end_device_ids": { "device_id": "lt-socket-sync-bluetooth-gateway", @@ -1532,21 +1663,18 @@ "join_eui": "0000000000000000" }, "correlation_ids": [ - "as:up:01M6E9MXMXMTGXT2EQG0C1KR2H", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:f011a6e8-29c0-40e0-97a6-3a4c763b6a07" + "as:up:01M6DX3DFWNQE3WSG9JNFTAG7B", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:ec26c3c1-e861-4d65-b6e7-1206e538a5c7" ], - "received_at": "2026-11-02T14:12:11.036297935Z", + "received_at": "2026-11-02T10:32:54.523762899Z", "uplink_message": { "f_port": 1, - "frm_payload": "IWQ+/gwAbFA=", + "frm_payload": "8AU=", "decoded_payload": { "bytes": { - "chgstate": "Not charging", - "rssi": "-62dBm", - "snr": "650.36dB", - "type": "Heartbeat", - "ver": "VER108", - "vol": "100%" + "type": "Acknowledgment", + "result": "downlink success", + "msgid": "5" } }, "rx_metadata": [ @@ -1582,10 +1710,87 @@ "decoded": { "profile": "lansitec-socket-sync-bluetooth-gateway", "telemetry": { - "loraRssi": -62, - "loraSnr": -5.0, - "ver": "VER108", - "vol": 100 + "result": "Success", + "msgid": 5 + } + } + }, + { + "name": "gw Multi 4.4.7, same data length (built from 4.4.7)", + "uplink": { + "end_device_ids": { + "device_id": "lt-compact-bluetooth-gateway", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0000", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6E9MR7XDSV8F47HYQXZWNR9", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:3cb978ee-9803-4d8d-a720-f92b553f8f89" + ], + "received_at": "2026-11-02T14:12:05.501610818Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "4gEGGkwAAhWqu7MBBhpMAAIVqry0", + "decoded_payload": { + "bytes": { + "Device1ID": "Device type 1 message", + "Device1data": "061A4C000215AABB", + "Device1rssi": "-77dBm", + "Device2ID": "Device type 1 message", + "Device2data": "061A4C000215AABC", + "Device2rssi": "-76dBm", + "number": 2, + "type": "Multi-Device Type Message" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "compact-bluetooth-gateway", + "hardware_version": "1.0", + "firmware_version": "1.0", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-compact-bluetooth-gateway", + "telemetry": { + "beacons": [ + { + "deviceType": 1, + "data": "061A4C000215AABB", + "rssi": -77 + }, + { + "deviceType": 1, + "data": "061A4C000215AABC", + "rssi": -76 + } + ] } } }, diff --git a/ui-ngx/src/assets/converters/js-tti-decoder.raw b/ui-ngx/src/assets/converters/js-tti-decoder.raw index f4f0d648cf..634d265574 100644 --- a/ui-ngx/src/assets/converters/js-tti-decoder.raw +++ b/ui-ngx/src/assets/converters/js-tti-decoder.raw @@ -5,7 +5,8 @@ // Lansitec: the LoRaWAN Device Repository codecs wrap everything in "bytes", write numbers as strings with units // ("85%", "-67dBm") and get some fields wrong. Key names follow the Lansitec datasheets where there are any: -// Bluetooth Gateway DataSheet 2.27, Badge Tracker DataSheet v5.30, Temperature Humidity Sensor DataSheet 1.26. +// Bluetooth Gateway DataSheet 2.27, Badge Tracker DataSheet v5.30, Temperature Humidity Sensor DataSheet 1.26, +// Plug Bluetooth Gateway DataSheet 1.5 (SocketSync gateway). // The wrong fields are decoded from the raw bytes (metadata.data). Remove a fix when the Repository fixes the codec. var LANSITEC_UNITS = ['dBm', 'dB', '%', 'V', 'sec', 'min', 's', '℃', 'level', ' meter']; @@ -53,9 +54,9 @@ var LANSITEC_TEMPERATURE_HUMIDITY_KEYS = { }; // Without a datasheet only these keys are renamed: the integration writes rssi, snr and firmwareVersion itself. -// chgstate: the SocketSync gateway has the compact gateway codec byte for byte, which reads CHGSTAT 0x50 as -// "Not charging" (Bluetooth Gateway DataSheet 2.27, 4.4.2), so it is dropped. number is its count of reported -// devices: the beacons list has them. +// chgstate: the tracking label and the contact tracing badge have the gateway codec byte for byte, which reads +// CHGSTAT 0x50 as "Not charging" (Bluetooth Gateway DataSheet 2.27, 4.4.2), so it is dropped. number is the count +// of reported devices: the beacons list has them. var LANSITEC_CLASH_KEYS = { rssi: 'loraRssi', RSSI: 'loraRssi', snr: 'loraSnr', SNR: 'loraSnr', firmwareVersion: 'deviceFirmwareVersion', 'Firmware version': 'deviceFirmwareVersion', @@ -205,7 +206,7 @@ function lansitecBeaconList(beacons) { } // The gateway codec reads the data of every device after the first at a 5-byte step, so it is right only // for 4-byte data (DataSheet 4.4.4 examples): with longer data only the first device is kept. The gateways - // with a datasheet get their beacons from the raw bytes instead; this is for the SocketSync gateway. + // with a datasheet get their beacons from the raw bytes instead; this is for the models with the gateway codec. if (list.length > 1 && list[0].data != null && list[0].data.length != 8) { return [list[0]]; } @@ -224,9 +225,6 @@ function lansitecRawValues(values, b, model) { lansitecBadgeRawValues(values, b, type); } else if (model == 'temperature-humidity-sensor' && type == 2) { lansitecTemperatureHumidityRawValues(values, b); - } else if (model == 'socket-sync-bluetooth-gateway' && type == 2 && b.length >= 8) { - // No datasheet: the codec reads the heartbeat as the compact gateway's (4.4.2), SNR in bytes 3-4 unsigned. - values.loraSnr = lansitecSnr(b, 3); } } @@ -416,10 +414,100 @@ function lansitecTemperatureHumidityRawValues(values, b) { values.humidity = b[offset + 2]; } +// Plug Bluetooth Gateway DataSheet 1.5 (990-01203), the SocketSync gateway. Its codec is the compact gateway's, but +// the messages are not: the codec gives nothing for the Registration (0x4) and the Beacon message (0x6), so The +// Things Stack sends them without decoded_payload, and it reads the Heartbeat and the Alarm Beacon List (0xE) with +// the compact gateway's layout. So every message is decoded from the raw bytes, with or without decoded_payload. +var LANSITEC_SOCKET_SYNC = 'socket-sync-bluetooth-gateway'; +// 5.1 Type bits 2-0 (MODE) and SMODE: the LoRa frequency plan. +var LANSITEC_SOCKET_SYNC_MODES = ['', 'AU915', '', 'CN470', 'AS923', 'EU433', 'EU868', 'US915']; +var LANSITEC_SOCKET_SYNC_BANDS = { 1: 'AU915', 4: 'CN470', 8: 'AS923', 16: 'EU433', 32: 'EU868', 64: 'US915' }; +// 5.1 CFG bits 3-1: the gateway type. +var LANSITEC_SOCKET_SYNC_TYPES = ['Outdoor gateway', 'Indoor gateway', 'Plug gateway']; +// Table 7: UUID List. A beacon gives the last byte of its UUID, the other bytes are the same for all of them. +var LANSITEC_SOCKET_SYNC_UUID = '5C38DBDE567C4CCAB1DA40A8AD4656'; + +function lansitecSocketSyncValues(b) { + var values = {}; + if (b.length == 0) { + return values; + } + var type = (b[0] >> 4) & 0x0F; + if (type == 0x4 && b.length >= 10) { + // 5.1 Registration: Type (ADR, MODE), SMODE, POWER, CFG, POS (2), BLE Receiving Duration, HB, Beacon_QTY, CRC (2). + values.adr = (b[0] & 0x08) != 0 ? 'ON' : 'OFF'; + if (LANSITEC_SOCKET_SYNC_MODES[b[0] & 0x07] != '') { + values.mode = LANSITEC_SOCKET_SYNC_MODES[b[0] & 0x07]; + } + if (LANSITEC_SOCKET_SYNC_BANDS[b[1]] != null) { + values.loraWanFrequencyBand = LANSITEC_SOCKET_SYNC_BANDS[b[1]]; + } + values.loraTxPower = (b[2] >> 3) & 0x1F; + values.continuousBleReceiving = (b[2] & 0x02) != 0 ? 'Continuous Bluetooth receiving is enabled' : 'Continuous Bluetooth receiving is disabled'; + values.powerDownFlag = (b[2] & 0x01) != 0 ? 'Automatic shutdown status' : 'Normal status'; + values.dataRate = 'DR' + ((b[3] >> 4) & 0x0F); + if (LANSITEC_SOCKET_SYNC_TYPES[(b[3] >> 1) & 0x07] != null) { + values.gatewayType = LANSITEC_SOCKET_SYNC_TYPES[(b[3] >> 1) & 0x07]; + } + values.rssiSortingMethod = (b[3] & 0x01) != 0 ? 'Sort by the maximum value' : 'Sort by average'; + // POS unit 5 s, 0 means off. HB unit 30 s. Beacon_QTY is half of the limit. + values.positionReportInterval = lansitecUint(b, 4, 2) * 5; + values.bluetoothReceivingDuration = b[6]; + values.heartbeatInterval = b[7] * 30; + values.beaconQuantityLimit = b[8] * 2; + } else if (type == 0x2 && b.length >= 7) { + // 5.2 Heartbeat: Type, VOL (battery level, %), RSSI (unit -1 dBm), SNR (2, signed, 0.01 dB), REV (firmware, + // hex), CHGSTAT, CRC (2). The codec reads REV and CHGSTAT as the compact gateway's 2-byte version, and the + // first CRC byte as CHGSTAT. VOL keeps its name: the dashboard of the IoT Hub package reads it. + values.vol = b[1]; + values.loraRssi = -b[2]; + values.loraSnr = lansitecSnr(b, 3); + values.deviceFirmwareVersion = lansitecHex(b, 5, 6); + if (b[6] == 0x00) { + values.chargeState = 'Not charging'; + } else if (b[6] == 0x50) { + values.chargeState = 'Charging'; + } else if (b[6] == 0x60) { + values.chargeState = 'Charging completed'; + } + } else if (type == 0x6 || type == 0xE) { + // 5.3 Beacon and 5.4 Alarm Beacon List: Type (bits 3-0: number of beacons), then UUID tail number, Major (2), + // Minor (2) and RSSI (value - 256, dBm) of each beacon. The alarm list holds the beacons that trigger the alarm. + var count = b[0] & 0x0F; + if (count > 0 && b.length >= 1 + count * 6) { + var list = []; + for (var i = 0; i < count; i++) { + var start = 1 + i * 6; + list.push({ + uuid: LANSITEC_SOCKET_SYNC_UUID + lansitecHex(b, start, start + 1), + major: lansitecUint(b, start + 1, 2), + minor: lansitecUint(b, start + 3, 2), + rssi: b[start + 5] - 256 + }); + } + values[type == 0x6 ? 'beacons' : 'alarmBeacons'] = list; + } + } else if (type == 0xF && b.length >= 2) { + // 5.5 Acknowledgment: Type bits 3-0 the result, then the MSGID of the downlink. + values.result = (b[0] & 0x0F) == 0 ? 'Success' : 'Failure'; + values.msgid = b[1]; + } + return values; +} + var values = {}; var attributes = {}; var history = []; -if (metadata.payloadFormat == 'JSON') { +if (metadata.brandId == 'lansitec' && metadata.modelId == LANSITEC_SOCKET_SYNC && metadata.data != null) { + // The SocketSync gateway: decoded from the raw bytes only, see lansitecSocketSyncValues. + var socketSyncBytes = lansitecBase64ToBytes(metadata.data); + values = lansitecSocketSyncValues(socketSyncBytes); + // Registration (0x4) holds the device settings: attributes, not telemetry. + if (socketSyncBytes.length > 0 && ((socketSyncBytes[0] >> 4) & 0x0F) == 0x4) { + attributes = values; + values = {}; + } +} else if (metadata.payloadFormat == 'JSON') { values = decodeToJson(payload); if (metadata.brandId == 'lansitec' && values.bytes != null && typeof values.bytes === 'object' && !Array.isArray(values.bytes)) { // The Lansitec codecs put everything in "bytes" and get some fields wrong: see the Lansitec block above. diff --git a/ui-ngx/src/assets/converters/js-ttn-decoder.raw b/ui-ngx/src/assets/converters/js-ttn-decoder.raw index f4f0d648cf..634d265574 100644 --- a/ui-ngx/src/assets/converters/js-ttn-decoder.raw +++ b/ui-ngx/src/assets/converters/js-ttn-decoder.raw @@ -5,7 +5,8 @@ // Lansitec: the LoRaWAN Device Repository codecs wrap everything in "bytes", write numbers as strings with units // ("85%", "-67dBm") and get some fields wrong. Key names follow the Lansitec datasheets where there are any: -// Bluetooth Gateway DataSheet 2.27, Badge Tracker DataSheet v5.30, Temperature Humidity Sensor DataSheet 1.26. +// Bluetooth Gateway DataSheet 2.27, Badge Tracker DataSheet v5.30, Temperature Humidity Sensor DataSheet 1.26, +// Plug Bluetooth Gateway DataSheet 1.5 (SocketSync gateway). // The wrong fields are decoded from the raw bytes (metadata.data). Remove a fix when the Repository fixes the codec. var LANSITEC_UNITS = ['dBm', 'dB', '%', 'V', 'sec', 'min', 's', '℃', 'level', ' meter']; @@ -53,9 +54,9 @@ var LANSITEC_TEMPERATURE_HUMIDITY_KEYS = { }; // Without a datasheet only these keys are renamed: the integration writes rssi, snr and firmwareVersion itself. -// chgstate: the SocketSync gateway has the compact gateway codec byte for byte, which reads CHGSTAT 0x50 as -// "Not charging" (Bluetooth Gateway DataSheet 2.27, 4.4.2), so it is dropped. number is its count of reported -// devices: the beacons list has them. +// chgstate: the tracking label and the contact tracing badge have the gateway codec byte for byte, which reads +// CHGSTAT 0x50 as "Not charging" (Bluetooth Gateway DataSheet 2.27, 4.4.2), so it is dropped. number is the count +// of reported devices: the beacons list has them. var LANSITEC_CLASH_KEYS = { rssi: 'loraRssi', RSSI: 'loraRssi', snr: 'loraSnr', SNR: 'loraSnr', firmwareVersion: 'deviceFirmwareVersion', 'Firmware version': 'deviceFirmwareVersion', @@ -205,7 +206,7 @@ function lansitecBeaconList(beacons) { } // The gateway codec reads the data of every device after the first at a 5-byte step, so it is right only // for 4-byte data (DataSheet 4.4.4 examples): with longer data only the first device is kept. The gateways - // with a datasheet get their beacons from the raw bytes instead; this is for the SocketSync gateway. + // with a datasheet get their beacons from the raw bytes instead; this is for the models with the gateway codec. if (list.length > 1 && list[0].data != null && list[0].data.length != 8) { return [list[0]]; } @@ -224,9 +225,6 @@ function lansitecRawValues(values, b, model) { lansitecBadgeRawValues(values, b, type); } else if (model == 'temperature-humidity-sensor' && type == 2) { lansitecTemperatureHumidityRawValues(values, b); - } else if (model == 'socket-sync-bluetooth-gateway' && type == 2 && b.length >= 8) { - // No datasheet: the codec reads the heartbeat as the compact gateway's (4.4.2), SNR in bytes 3-4 unsigned. - values.loraSnr = lansitecSnr(b, 3); } } @@ -416,10 +414,100 @@ function lansitecTemperatureHumidityRawValues(values, b) { values.humidity = b[offset + 2]; } +// Plug Bluetooth Gateway DataSheet 1.5 (990-01203), the SocketSync gateway. Its codec is the compact gateway's, but +// the messages are not: the codec gives nothing for the Registration (0x4) and the Beacon message (0x6), so The +// Things Stack sends them without decoded_payload, and it reads the Heartbeat and the Alarm Beacon List (0xE) with +// the compact gateway's layout. So every message is decoded from the raw bytes, with or without decoded_payload. +var LANSITEC_SOCKET_SYNC = 'socket-sync-bluetooth-gateway'; +// 5.1 Type bits 2-0 (MODE) and SMODE: the LoRa frequency plan. +var LANSITEC_SOCKET_SYNC_MODES = ['', 'AU915', '', 'CN470', 'AS923', 'EU433', 'EU868', 'US915']; +var LANSITEC_SOCKET_SYNC_BANDS = { 1: 'AU915', 4: 'CN470', 8: 'AS923', 16: 'EU433', 32: 'EU868', 64: 'US915' }; +// 5.1 CFG bits 3-1: the gateway type. +var LANSITEC_SOCKET_SYNC_TYPES = ['Outdoor gateway', 'Indoor gateway', 'Plug gateway']; +// Table 7: UUID List. A beacon gives the last byte of its UUID, the other bytes are the same for all of them. +var LANSITEC_SOCKET_SYNC_UUID = '5C38DBDE567C4CCAB1DA40A8AD4656'; + +function lansitecSocketSyncValues(b) { + var values = {}; + if (b.length == 0) { + return values; + } + var type = (b[0] >> 4) & 0x0F; + if (type == 0x4 && b.length >= 10) { + // 5.1 Registration: Type (ADR, MODE), SMODE, POWER, CFG, POS (2), BLE Receiving Duration, HB, Beacon_QTY, CRC (2). + values.adr = (b[0] & 0x08) != 0 ? 'ON' : 'OFF'; + if (LANSITEC_SOCKET_SYNC_MODES[b[0] & 0x07] != '') { + values.mode = LANSITEC_SOCKET_SYNC_MODES[b[0] & 0x07]; + } + if (LANSITEC_SOCKET_SYNC_BANDS[b[1]] != null) { + values.loraWanFrequencyBand = LANSITEC_SOCKET_SYNC_BANDS[b[1]]; + } + values.loraTxPower = (b[2] >> 3) & 0x1F; + values.continuousBleReceiving = (b[2] & 0x02) != 0 ? 'Continuous Bluetooth receiving is enabled' : 'Continuous Bluetooth receiving is disabled'; + values.powerDownFlag = (b[2] & 0x01) != 0 ? 'Automatic shutdown status' : 'Normal status'; + values.dataRate = 'DR' + ((b[3] >> 4) & 0x0F); + if (LANSITEC_SOCKET_SYNC_TYPES[(b[3] >> 1) & 0x07] != null) { + values.gatewayType = LANSITEC_SOCKET_SYNC_TYPES[(b[3] >> 1) & 0x07]; + } + values.rssiSortingMethod = (b[3] & 0x01) != 0 ? 'Sort by the maximum value' : 'Sort by average'; + // POS unit 5 s, 0 means off. HB unit 30 s. Beacon_QTY is half of the limit. + values.positionReportInterval = lansitecUint(b, 4, 2) * 5; + values.bluetoothReceivingDuration = b[6]; + values.heartbeatInterval = b[7] * 30; + values.beaconQuantityLimit = b[8] * 2; + } else if (type == 0x2 && b.length >= 7) { + // 5.2 Heartbeat: Type, VOL (battery level, %), RSSI (unit -1 dBm), SNR (2, signed, 0.01 dB), REV (firmware, + // hex), CHGSTAT, CRC (2). The codec reads REV and CHGSTAT as the compact gateway's 2-byte version, and the + // first CRC byte as CHGSTAT. VOL keeps its name: the dashboard of the IoT Hub package reads it. + values.vol = b[1]; + values.loraRssi = -b[2]; + values.loraSnr = lansitecSnr(b, 3); + values.deviceFirmwareVersion = lansitecHex(b, 5, 6); + if (b[6] == 0x00) { + values.chargeState = 'Not charging'; + } else if (b[6] == 0x50) { + values.chargeState = 'Charging'; + } else if (b[6] == 0x60) { + values.chargeState = 'Charging completed'; + } + } else if (type == 0x6 || type == 0xE) { + // 5.3 Beacon and 5.4 Alarm Beacon List: Type (bits 3-0: number of beacons), then UUID tail number, Major (2), + // Minor (2) and RSSI (value - 256, dBm) of each beacon. The alarm list holds the beacons that trigger the alarm. + var count = b[0] & 0x0F; + if (count > 0 && b.length >= 1 + count * 6) { + var list = []; + for (var i = 0; i < count; i++) { + var start = 1 + i * 6; + list.push({ + uuid: LANSITEC_SOCKET_SYNC_UUID + lansitecHex(b, start, start + 1), + major: lansitecUint(b, start + 1, 2), + minor: lansitecUint(b, start + 3, 2), + rssi: b[start + 5] - 256 + }); + } + values[type == 0x6 ? 'beacons' : 'alarmBeacons'] = list; + } + } else if (type == 0xF && b.length >= 2) { + // 5.5 Acknowledgment: Type bits 3-0 the result, then the MSGID of the downlink. + values.result = (b[0] & 0x0F) == 0 ? 'Success' : 'Failure'; + values.msgid = b[1]; + } + return values; +} + var values = {}; var attributes = {}; var history = []; -if (metadata.payloadFormat == 'JSON') { +if (metadata.brandId == 'lansitec' && metadata.modelId == LANSITEC_SOCKET_SYNC && metadata.data != null) { + // The SocketSync gateway: decoded from the raw bytes only, see lansitecSocketSyncValues. + var socketSyncBytes = lansitecBase64ToBytes(metadata.data); + values = lansitecSocketSyncValues(socketSyncBytes); + // Registration (0x4) holds the device settings: attributes, not telemetry. + if (socketSyncBytes.length > 0 && ((socketSyncBytes[0] >> 4) & 0x0F) == 0x4) { + attributes = values; + values = {}; + } +} else if (metadata.payloadFormat == 'JSON') { values = decodeToJson(payload); if (metadata.brandId == 'lansitec' && values.bytes != null && typeof values.bytes === 'object' && !Array.isArray(values.bytes)) { // The Lansitec codecs put everything in "bytes" and get some fields wrong: see the Lansitec block above. diff --git a/ui-ngx/src/assets/converters/tbel-tti-decoder.raw b/ui-ngx/src/assets/converters/tbel-tti-decoder.raw index 79fac578f8..8adbe6983f 100644 --- a/ui-ngx/src/assets/converters/tbel-tti-decoder.raw +++ b/ui-ngx/src/assets/converters/tbel-tti-decoder.raw @@ -5,7 +5,8 @@ // Lansitec: the LoRaWAN Device Repository codecs wrap everything in "bytes", write numbers as strings with units // ("85%", "-67dBm") and get some fields wrong. Key names follow the Lansitec datasheets where there are any: -// Bluetooth Gateway DataSheet 2.27, Badge Tracker DataSheet v5.30, Temperature Humidity Sensor DataSheet 1.26. +// Bluetooth Gateway DataSheet 2.27, Badge Tracker DataSheet v5.30, Temperature Humidity Sensor DataSheet 1.26, +// Plug Bluetooth Gateway DataSheet 1.5 (SocketSync gateway). // The wrong fields are decoded from the raw bytes (metadata.data). Remove a fix when the Repository fixes the codec. var LANSITEC_UNITS = ["dBm", "dB", "%", "V", "sec", "min", "s", "℃", "level", " meter"]; @@ -53,9 +54,9 @@ var LANSITEC_TEMPERATURE_HUMIDITY_KEYS = { }; // Without a datasheet only these keys are renamed: the integration writes rssi, snr and firmwareVersion itself. -// chgstate: the SocketSync gateway has the compact gateway codec byte for byte, which reads CHGSTAT 0x50 as -// "Not charging" (Bluetooth Gateway DataSheet 2.27, 4.4.2), so it is dropped. number is its count of reported -// devices: the beacons list has them. +// chgstate: the tracking label and the contact tracing badge have the gateway codec byte for byte, which reads +// CHGSTAT 0x50 as "Not charging" (Bluetooth Gateway DataSheet 2.27, 4.4.2), so it is dropped. number is the count +// of reported devices: the beacons list has them. var LANSITEC_CLASH_KEYS = { rssi: "loraRssi", RSSI: "loraRssi", snr: "loraSnr", SNR: "loraSnr", firmwareVersion: "deviceFirmwareVersion", "Firmware version": "deviceFirmwareVersion", @@ -201,7 +202,7 @@ function lansitecBeaconList(beacons) { } // The gateway codec reads the data of every device after the first at a 5-byte step, so it is right only // for 4-byte data (DataSheet 4.4.4 examples): with longer data only the first device is kept. The gateways - // with a datasheet get their beacons from the raw bytes instead; this is for the SocketSync gateway. + // with a datasheet get their beacons from the raw bytes instead; this is for the models with the gateway codec. if (list.size() > 1 && list[0].data != null && list[0].data.length() != 8) { var first = []; first.add(list[0]); @@ -222,9 +223,6 @@ function lansitecRawValues(values, b, model) { lansitecBadgeRawValues(values, b, type); } else if (model == "temperature-humidity-sensor" && type == 2) { lansitecTemperatureHumidityRawValues(values, b); - } else if (model == "socket-sync-bluetooth-gateway" && type == 2 && b.length >= 8) { - // No datasheet: the codec reads the heartbeat as the compact gateway's (4.4.2), SNR in bytes 3-4 unsigned. - values.loraSnr = lansitecSnr(b, 3); } } @@ -372,10 +370,110 @@ function lansitecTemperatureHumidityRawValues(values, b) { values.humidity = b[offset + 2] & 0xFF; } +// Plug Bluetooth Gateway DataSheet 1.5 (990-01203), the SocketSync gateway. Its codec is the compact gateway's, but +// the messages are not: the codec gives nothing for the Registration (0x4) and the Beacon message (0x6), so The +// Things Stack sends them without decoded_payload, and it reads the Heartbeat and the Alarm Beacon List (0xE) with +// the compact gateway's layout. So every message is decoded from the raw bytes, with or without decoded_payload. +var LANSITEC_SOCKET_SYNC = "socket-sync-bluetooth-gateway"; +// 5.1 Type bits 2-0 (MODE) and SMODE: the LoRa frequency plan. +var LANSITEC_SOCKET_SYNC_MODES = ["", "AU915", "", "CN470", "AS923", "EU433", "EU868", "US915"]; +var LANSITEC_SOCKET_SYNC_SMODES = [1, 4, 8, 16, 32, 64]; +var LANSITEC_SOCKET_SYNC_BANDS = ["AU915", "CN470", "AS923", "EU433", "EU868", "US915"]; +// 5.1 CFG bits 3-1: the gateway type. +var LANSITEC_SOCKET_SYNC_TYPES = ["Outdoor gateway", "Indoor gateway", "Plug gateway"]; +// Table 7: UUID List. A beacon gives the last byte of its UUID, the other bytes are the same for all of them. +var LANSITEC_SOCKET_SYNC_UUID = "5C38DBDE567C4CCAB1DA40A8AD4656"; + +function lansitecSocketSyncValues(b) { + var values = {}; + if (b.length == 0) { + return values; + } + var type = (b[0] >> 4) & 0x0F; + if (type == 0x4 && b.length >= 10) { + // 5.1 Registration: Type (ADR, MODE), SMODE, POWER, CFG, POS (2), BLE Receiving Duration, HB, Beacon_QTY, CRC (2). + values.adr = (b[0] & 0x08) != 0 ? "ON" : "OFF"; + var mode = b[0] & 0x07; + if (LANSITEC_SOCKET_SYNC_MODES[mode] != "") { + values.mode = LANSITEC_SOCKET_SYNC_MODES[mode]; + } + var band = LANSITEC_SOCKET_SYNC_SMODES.indexOf(b[1] & 0xFF); + if (band >= 0) { + values.loraWanFrequencyBand = LANSITEC_SOCKET_SYNC_BANDS[band]; + } + values.loraTxPower = (b[2] >> 3) & 0x1F; + values.continuousBleReceiving = (b[2] & 0x02) != 0 ? "Continuous Bluetooth receiving is enabled" : "Continuous Bluetooth receiving is disabled"; + values.powerDownFlag = (b[2] & 0x01) != 0 ? "Automatic shutdown status" : "Normal status"; + values.dataRate = "DR" + ((b[3] >> 4) & 0x0F); + var gatewayType = (b[3] >> 1) & 0x07; + if (gatewayType < LANSITEC_SOCKET_SYNC_TYPES.size()) { + values.gatewayType = LANSITEC_SOCKET_SYNC_TYPES[gatewayType]; + } + values.rssiSortingMethod = (b[3] & 0x01) != 0 ? "Sort by the maximum value" : "Sort by average"; + // POS unit 5 s, 0 means off. HB unit 30 s. Beacon_QTY is half of the limit. + values.positionReportInterval = parseBytesToInt(b, 4, 2, true) * 5; + values.bluetoothReceivingDuration = b[6] & 0xFF; + values.heartbeatInterval = (b[7] & 0xFF) * 30; + values.beaconQuantityLimit = (b[8] & 0xFF) * 2; + } else if (type == 0x2 && b.length >= 7) { + // 5.2 Heartbeat: Type, VOL (battery level, %), RSSI (unit -1 dBm), SNR (2, signed, 0.01 dB), REV (firmware, + // hex), CHGSTAT, CRC (2). The codec reads REV and CHGSTAT as the compact gateway's 2-byte version, and the + // first CRC byte as CHGSTAT. VOL keeps its name: the dashboard of the IoT Hub package reads it. + values.vol = b[1] & 0xFF; + values.loraRssi = 0 - (b[2] & 0xFF); + values.loraSnr = lansitecSnr(b, 3); + values.deviceFirmwareVersion = bytesToHex(b).substring(10, 12); + var charge = b[6] & 0xFF; + if (charge == 0x00) { + values.chargeState = "Not charging"; + } else if (charge == 0x50) { + values.chargeState = "Charging"; + } else if (charge == 0x60) { + values.chargeState = "Charging completed"; + } + } else if (type == 0x6 || type == 0xE) { + // 5.3 Beacon and 5.4 Alarm Beacon List: Type (bits 3-0: number of beacons), then UUID tail number, Major (2), + // Minor (2) and RSSI (value - 256, dBm) of each beacon. The alarm list holds the beacons that trigger the alarm. + var count = b[0] & 0x0F; + if (count > 0 && b.length >= 1 + count * 6) { + var hex = bytesToHex(b); + var list = []; + for (var i = 0; i < count; i++) { + var start = 1 + i * 6; + var item = {}; + item.uuid = LANSITEC_SOCKET_SYNC_UUID + hex.substring(2 * start, 2 * start + 2); + item.major = parseBytesToInt(b, start + 1, 2, true); + item.minor = parseBytesToInt(b, start + 3, 2, true); + item.rssi = (b[start + 5] & 0xFF) - 256; + list.add(item); + } + if (type == 0x6) { + values.beacons = list; + } else { + values.alarmBeacons = list; + } + } + } else if (type == 0xF && b.length >= 2) { + // 5.5 Acknowledgment: Type bits 3-0 the result, then the MSGID of the downlink. + values.result = (b[0] & 0x0F) == 0 ? "Success" : "Failure"; + values.msgid = b[1] & 0xFF; + } + return values; +} + var values = {}; var attributes = {}; var history = []; -if (metadata.payloadFormat == "JSON") { +if (metadata.brandId == "lansitec" && metadata.modelId == LANSITEC_SOCKET_SYNC && metadata.data != null) { + // The SocketSync gateway: decoded from the raw bytes only, see lansitecSocketSyncValues. + var socketSyncBytes = base64ToBytes(metadata.data); + values = lansitecSocketSyncValues(socketSyncBytes); + // Registration (0x4) holds the device settings: attributes, not telemetry. + if (socketSyncBytes.length > 0 && ((socketSyncBytes[0] >> 4) & 0x0F) == 0x4) { + attributes = values; + values = {}; + } +} else if (metadata.payloadFormat == "JSON") { values = decodeToJson(payload); if (metadata.brandId == "lansitec" && isMap(values.bytes)) { // The Lansitec codecs put everything in "bytes" and get some fields wrong: see the Lansitec block above. diff --git a/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw b/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw index 79fac578f8..8adbe6983f 100644 --- a/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw +++ b/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw @@ -5,7 +5,8 @@ // Lansitec: the LoRaWAN Device Repository codecs wrap everything in "bytes", write numbers as strings with units // ("85%", "-67dBm") and get some fields wrong. Key names follow the Lansitec datasheets where there are any: -// Bluetooth Gateway DataSheet 2.27, Badge Tracker DataSheet v5.30, Temperature Humidity Sensor DataSheet 1.26. +// Bluetooth Gateway DataSheet 2.27, Badge Tracker DataSheet v5.30, Temperature Humidity Sensor DataSheet 1.26, +// Plug Bluetooth Gateway DataSheet 1.5 (SocketSync gateway). // The wrong fields are decoded from the raw bytes (metadata.data). Remove a fix when the Repository fixes the codec. var LANSITEC_UNITS = ["dBm", "dB", "%", "V", "sec", "min", "s", "℃", "level", " meter"]; @@ -53,9 +54,9 @@ var LANSITEC_TEMPERATURE_HUMIDITY_KEYS = { }; // Without a datasheet only these keys are renamed: the integration writes rssi, snr and firmwareVersion itself. -// chgstate: the SocketSync gateway has the compact gateway codec byte for byte, which reads CHGSTAT 0x50 as -// "Not charging" (Bluetooth Gateway DataSheet 2.27, 4.4.2), so it is dropped. number is its count of reported -// devices: the beacons list has them. +// chgstate: the tracking label and the contact tracing badge have the gateway codec byte for byte, which reads +// CHGSTAT 0x50 as "Not charging" (Bluetooth Gateway DataSheet 2.27, 4.4.2), so it is dropped. number is the count +// of reported devices: the beacons list has them. var LANSITEC_CLASH_KEYS = { rssi: "loraRssi", RSSI: "loraRssi", snr: "loraSnr", SNR: "loraSnr", firmwareVersion: "deviceFirmwareVersion", "Firmware version": "deviceFirmwareVersion", @@ -201,7 +202,7 @@ function lansitecBeaconList(beacons) { } // The gateway codec reads the data of every device after the first at a 5-byte step, so it is right only // for 4-byte data (DataSheet 4.4.4 examples): with longer data only the first device is kept. The gateways - // with a datasheet get their beacons from the raw bytes instead; this is for the SocketSync gateway. + // with a datasheet get their beacons from the raw bytes instead; this is for the models with the gateway codec. if (list.size() > 1 && list[0].data != null && list[0].data.length() != 8) { var first = []; first.add(list[0]); @@ -222,9 +223,6 @@ function lansitecRawValues(values, b, model) { lansitecBadgeRawValues(values, b, type); } else if (model == "temperature-humidity-sensor" && type == 2) { lansitecTemperatureHumidityRawValues(values, b); - } else if (model == "socket-sync-bluetooth-gateway" && type == 2 && b.length >= 8) { - // No datasheet: the codec reads the heartbeat as the compact gateway's (4.4.2), SNR in bytes 3-4 unsigned. - values.loraSnr = lansitecSnr(b, 3); } } @@ -372,10 +370,110 @@ function lansitecTemperatureHumidityRawValues(values, b) { values.humidity = b[offset + 2] & 0xFF; } +// Plug Bluetooth Gateway DataSheet 1.5 (990-01203), the SocketSync gateway. Its codec is the compact gateway's, but +// the messages are not: the codec gives nothing for the Registration (0x4) and the Beacon message (0x6), so The +// Things Stack sends them without decoded_payload, and it reads the Heartbeat and the Alarm Beacon List (0xE) with +// the compact gateway's layout. So every message is decoded from the raw bytes, with or without decoded_payload. +var LANSITEC_SOCKET_SYNC = "socket-sync-bluetooth-gateway"; +// 5.1 Type bits 2-0 (MODE) and SMODE: the LoRa frequency plan. +var LANSITEC_SOCKET_SYNC_MODES = ["", "AU915", "", "CN470", "AS923", "EU433", "EU868", "US915"]; +var LANSITEC_SOCKET_SYNC_SMODES = [1, 4, 8, 16, 32, 64]; +var LANSITEC_SOCKET_SYNC_BANDS = ["AU915", "CN470", "AS923", "EU433", "EU868", "US915"]; +// 5.1 CFG bits 3-1: the gateway type. +var LANSITEC_SOCKET_SYNC_TYPES = ["Outdoor gateway", "Indoor gateway", "Plug gateway"]; +// Table 7: UUID List. A beacon gives the last byte of its UUID, the other bytes are the same for all of them. +var LANSITEC_SOCKET_SYNC_UUID = "5C38DBDE567C4CCAB1DA40A8AD4656"; + +function lansitecSocketSyncValues(b) { + var values = {}; + if (b.length == 0) { + return values; + } + var type = (b[0] >> 4) & 0x0F; + if (type == 0x4 && b.length >= 10) { + // 5.1 Registration: Type (ADR, MODE), SMODE, POWER, CFG, POS (2), BLE Receiving Duration, HB, Beacon_QTY, CRC (2). + values.adr = (b[0] & 0x08) != 0 ? "ON" : "OFF"; + var mode = b[0] & 0x07; + if (LANSITEC_SOCKET_SYNC_MODES[mode] != "") { + values.mode = LANSITEC_SOCKET_SYNC_MODES[mode]; + } + var band = LANSITEC_SOCKET_SYNC_SMODES.indexOf(b[1] & 0xFF); + if (band >= 0) { + values.loraWanFrequencyBand = LANSITEC_SOCKET_SYNC_BANDS[band]; + } + values.loraTxPower = (b[2] >> 3) & 0x1F; + values.continuousBleReceiving = (b[2] & 0x02) != 0 ? "Continuous Bluetooth receiving is enabled" : "Continuous Bluetooth receiving is disabled"; + values.powerDownFlag = (b[2] & 0x01) != 0 ? "Automatic shutdown status" : "Normal status"; + values.dataRate = "DR" + ((b[3] >> 4) & 0x0F); + var gatewayType = (b[3] >> 1) & 0x07; + if (gatewayType < LANSITEC_SOCKET_SYNC_TYPES.size()) { + values.gatewayType = LANSITEC_SOCKET_SYNC_TYPES[gatewayType]; + } + values.rssiSortingMethod = (b[3] & 0x01) != 0 ? "Sort by the maximum value" : "Sort by average"; + // POS unit 5 s, 0 means off. HB unit 30 s. Beacon_QTY is half of the limit. + values.positionReportInterval = parseBytesToInt(b, 4, 2, true) * 5; + values.bluetoothReceivingDuration = b[6] & 0xFF; + values.heartbeatInterval = (b[7] & 0xFF) * 30; + values.beaconQuantityLimit = (b[8] & 0xFF) * 2; + } else if (type == 0x2 && b.length >= 7) { + // 5.2 Heartbeat: Type, VOL (battery level, %), RSSI (unit -1 dBm), SNR (2, signed, 0.01 dB), REV (firmware, + // hex), CHGSTAT, CRC (2). The codec reads REV and CHGSTAT as the compact gateway's 2-byte version, and the + // first CRC byte as CHGSTAT. VOL keeps its name: the dashboard of the IoT Hub package reads it. + values.vol = b[1] & 0xFF; + values.loraRssi = 0 - (b[2] & 0xFF); + values.loraSnr = lansitecSnr(b, 3); + values.deviceFirmwareVersion = bytesToHex(b).substring(10, 12); + var charge = b[6] & 0xFF; + if (charge == 0x00) { + values.chargeState = "Not charging"; + } else if (charge == 0x50) { + values.chargeState = "Charging"; + } else if (charge == 0x60) { + values.chargeState = "Charging completed"; + } + } else if (type == 0x6 || type == 0xE) { + // 5.3 Beacon and 5.4 Alarm Beacon List: Type (bits 3-0: number of beacons), then UUID tail number, Major (2), + // Minor (2) and RSSI (value - 256, dBm) of each beacon. The alarm list holds the beacons that trigger the alarm. + var count = b[0] & 0x0F; + if (count > 0 && b.length >= 1 + count * 6) { + var hex = bytesToHex(b); + var list = []; + for (var i = 0; i < count; i++) { + var start = 1 + i * 6; + var item = {}; + item.uuid = LANSITEC_SOCKET_SYNC_UUID + hex.substring(2 * start, 2 * start + 2); + item.major = parseBytesToInt(b, start + 1, 2, true); + item.minor = parseBytesToInt(b, start + 3, 2, true); + item.rssi = (b[start + 5] & 0xFF) - 256; + list.add(item); + } + if (type == 0x6) { + values.beacons = list; + } else { + values.alarmBeacons = list; + } + } + } else if (type == 0xF && b.length >= 2) { + // 5.5 Acknowledgment: Type bits 3-0 the result, then the MSGID of the downlink. + values.result = (b[0] & 0x0F) == 0 ? "Success" : "Failure"; + values.msgid = b[1] & 0xFF; + } + return values; +} + var values = {}; var attributes = {}; var history = []; -if (metadata.payloadFormat == "JSON") { +if (metadata.brandId == "lansitec" && metadata.modelId == LANSITEC_SOCKET_SYNC && metadata.data != null) { + // The SocketSync gateway: decoded from the raw bytes only, see lansitecSocketSyncValues. + var socketSyncBytes = base64ToBytes(metadata.data); + values = lansitecSocketSyncValues(socketSyncBytes); + // Registration (0x4) holds the device settings: attributes, not telemetry. + if (socketSyncBytes.length > 0 && ((socketSyncBytes[0] >> 4) & 0x0F) == 0x4) { + attributes = values; + values = {}; + } +} else if (metadata.payloadFormat == "JSON") { values = decodeToJson(payload); if (metadata.brandId == "lansitec" && isMap(values.bytes)) { // The Lansitec codecs put everything in "bytes" and get some fields wrong: see the Lansitec block above. From 9f056688f43be2aa02419900c6d0a846ead9c099 Mon Sep 17 00:00:00 2001 From: dpinkevych Date: Thu, 5 Nov 2026 12:05:01 +0200 Subject: [PATCH 08/13] fix(iot-hub): the Lansitec contact tracing badge is decoded by its own datasheet (LoRaWAN Bluetooth Badge DataSheet 2.78): Register, Heartbeat with friend badges, 10 s Instant Report, 80 s Periodic Report and Acknowledge from the raw frame; the test has 7 uplinks as The Things Stack sent them --- .../controller/ConverterControllerTest.java | 15 + .../things_stack_lansitec_uplinks.json | 514 ++++++++++++++++++ .../src/assets/converters/js-tti-decoder.raw | 172 +++++- .../src/assets/converters/js-ttn-decoder.raw | 172 +++++- .../assets/converters/tbel-tti-decoder.raw | 194 ++++++- .../assets/converters/tbel-ttn-decoder.raw | 194 ++++++- 6 files changed, 1225 insertions(+), 36 deletions(-) diff --git a/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java index 1abe155e74..78c02874bc 100644 --- a/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java @@ -792,15 +792,23 @@ public class ConverterControllerTest extends AbstractControllerTest { @Test public void testThingsStackDefaultConvertersKeepMalformedLansitecUplinks() throws IOException { JsonNode temperatureHumidity = null; + JsonNode contactBadge = null; for (JsonNode testCase : JacksonUtil.toJsonNode(readResource(THINGS_STACK_LANSITEC_UPLINKS))) { if (testCase.get("name").asText().startsWith("temp-hum VER1")) { temperatureHumidity = testCase.get("uplink"); + } else if (testCase.get("name").asText().startsWith("contact badge 80s Periodic Report 4.5.5 example 1")) { + contactBadge = testCase.get("uplink"); } } ObjectNode shortFrame = temperatureHumidity.deepCopy(); ((ObjectNode) shortFrame.get("uplink_message")).put("frm_payload", "IQ=="); ObjectNode bytesList = temperatureHumidity.deepCopy(); ((ObjectNode) bytesList.get("uplink_message").get("decoded_payload")).set("bytes", JacksonUtil.toJsonNode("[33, 100]")); + // Periodic Report with 2 badges, cut in the middle of the first one, and Instant Report with 3 beacons and 1 byte + ObjectNode shortPeriodicReport = contactBadge.deepCopy(); + ((ObjectNode) shortPeriodicReport.get("uplink_message")).put("frm_payload", "wtAR"); + ObjectNode shortInstantReport = contactBadge.deepCopy(); + ((ObjectNode) shortInstantReport.get("uplink_message")).put("frm_payload", "g9M="); forEachThingsStackDefaultConverter((description, decoder) -> { JsonNode result = decoder.apply(shortFrame); @@ -811,6 +819,13 @@ public class ConverterControllerTest extends AbstractControllerTest { assertThat(result.get("profile").asText()).as(description + " bytes list").isEqualTo("lansitec-temperature-humidity-sensor"); assertThat(numbersByValue(telemetryValues(result).get("bytes"))).as(description + " bytes list") .isEqualTo(numbersByValue(JacksonUtil.toJsonNode("[33, 100]"))); + + for (ObjectNode shortReport : List.of(shortPeriodicReport, shortInstantReport)) { + result = decoder.apply(shortReport); + String reportDescription = description + " short report " + shortReport.get("uplink_message").get("frm_payload").asText(); + assertThat(result.get("profile").asText()).as(reportDescription).isEqualTo("lansitec-contact-tracing-badge"); + assertThat(withoutKeys(telemetryValues(result), THINGS_STACK_UPLINK_KEYS).isEmpty()).as(reportDescription).isTrue(); + } }); } diff --git a/application/src/test/resources/payload/things_stack_lansitec_uplinks.json b/application/src/test/resources/payload/things_stack_lansitec_uplinks.json index 1d5ce0b72c..c9ecb72c4b 100644 --- a/application/src/test/resources/payload/things_stack_lansitec_uplinks.json +++ b/application/src/test/resources/payload/things_stack_lansitec_uplinks.json @@ -1855,5 +1855,519 @@ "profile": "lansitec-badge-tracker", "telemetry": {} } + }, + { + "name": "contact badge Register 4.5.1 (built from DataSheet 2.78)", + "uplink": { + "end_device_ids": { + "device_id": "lt-contact-tracing-badge", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0005", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6NJ57PNWP8AGXCJQD7R9BDW", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:1987636f-fbc5-417b-b6c5-43dfebb304f2" + ], + "received_at": "2026-11-05T09:55:35.252740417Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "FiBwIAACCgqKEjQ=", + "decoded_payload": { + "bytes": { + "BLEReceivingDuration": "10sec", + "ContinuousReceive": "Continuous Bluetooth receiving is disabled", + "LoRaTxpower": "14dBm", + "LoRaWANband": "EU868", + "adr": "OFF", + "dr": "DR2", + "hb": "300sec", + "mode": "EU868", + "pos": "10sec", + "type": "Register" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "contact-tracing-badge", + "hardware_version": "1.0", + "firmware_version": "1.0", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-contact-tracing-badge", + "attributes": { + "adr": "OFF", + "mode": "EU868", + "loraWanFrequencyBand": "EU868", + "loraTxPower": 14, + "dataRate": "DR2", + "positionReportInterval": 10, + "heartbeatInterval": 300, + "buzzerAndVibrator": "ON", + "bluetoothTxPower": 0, + "proximityDetection": "ON", + "bluetoothReceivingThresholdSetting": 10 + } + } + }, + { + "name": "contact badge Heartbeat 4.5.2, SNR -5.00, Charging, 2 friend badges (built from DataSheet 2.78)", + "uplink": { + "end_device_ids": { + "device_id": "lt-contact-tracing-badge", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0005", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6NJ5BCG6BN1FHSSGF67YE24", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:5168cd93-1983-479d-a3d2-cc30d548ea44" + ], + "received_at": "2026-11-05T09:55:39.023582872Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "IWQ+/gwFBRI0AADC0BEALtARAC0=", + "decoded_payload": { + "bytes": { + "chgstate": 2, + "rssi": "-62dBm", + "snr": "650.36dB", + "type": "Heartbeat", + "ver": "VER1285", + "vol": "100%" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "contact-tracing-badge", + "hardware_version": "1.0", + "firmware_version": "1.0", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-contact-tracing-badge", + "telemetry": { + "batteryLevel": 100, + "loraRssi": -62, + "loraSnr": -5, + "deviceFirmwareVersion": "05", + "chargeState": "Charging", + "vibratorBuzzerStatus": "No action", + "friends": [ + { + "major": "D011", + "minor": "002E" + }, + { + "major": "D011", + "minor": "002D" + } + ] + } + } + }, + { + "name": "contact badge Heartbeat 4.5.2, BTSTATUS (built from DataSheet 2.78)", + "uplink": { + "end_device_ids": { + "device_id": "lt-contact-tracing-badge", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0005", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6NJ5D0MNH1S774XX07286BJ", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:8c866e78-af36-4e03-8059-c9161c9f0fe8" + ], + "received_at": "2026-11-05T09:55:40.691881636Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "IWQ+/gwAABI0BiU=", + "decoded_payload": { + "bytes": { + "chgstate": 2, + "rssi": "-62dBm", + "snr": "650.36dB", + "type": "Heartbeat", + "ver": "VER0", + "vol": "100%" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "contact-tracing-badge", + "hardware_version": "1.0", + "firmware_version": "1.0", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-contact-tracing-badge", + "telemetry": { + "batteryLevel": 100, + "loraRssi": -62, + "loraSnr": -5, + "deviceFirmwareVersion": "00", + "chargeState": "Not charging", + "vibratorBuzzerStatus": "Vibrator enabled, buzzer full sound level", + "bluetoothTxPower": 0, + "bluetoothReceivingThreshold": -77 + } + } + }, + { + "name": "contact badge 10s Instant Report 4.5.3, RSSI and TOFF examples (built from DataSheet 2.78)", + "uplink": { + "end_device_ids": { + "device_id": "lt-contact-tracing-badge", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0005", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6NJ5EMKTH1Q7R7AD5WA4HB6", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:e9a53f5b-bd3b-478b-8ff9-4c3424e1337f" + ], + "received_at": "2026-11-05T09:55:42.353804675Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "g9MQAj4r0BEALqsAAQACtSQQ", + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "contact-tracing-badge", + "hardware_version": "1.0", + "firmware_version": "1.0", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-contact-tracing-badge", + "telemetry": { + "beacons": [ + { + "major": "D310", + "minor": "023E", + "type": "Badge", + "rssi": -85, + "timeOffset": 2 + }, + { + "major": "D011", + "minor": "002E", + "type": "Bluetooth beacon", + "rssi": -85, + "timeOffset": 4 + }, + { + "major": "0001", + "minor": "0002", + "type": "Bluetooth beacon", + "rssi": -75, + "timeOffset": 1 + } + ] + } + } + }, + { + "name": "contact badge 80s Periodic Report 4.5.5 example 1", + "uplink": { + "end_device_ids": { + "device_id": "lt-contact-tracing-badge", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0005", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6NJ5G8KP87MTGDZW5HZ8FK2", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:18969a7b-1571-4754-b58f-ca2faa9b417f" + ], + "received_at": "2026-11-05T09:55:44.017827879Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "wtARAC750BEALY8=", + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "contact-tracing-badge", + "hardware_version": "1.0", + "firmware_version": "1.0", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-contact-tracing-badge", + "telemetry": { + "badges": [ + { + "major": "D011", + "minor": "002E", + "timeMark": 249, + "seenTime": 60 + }, + { + "major": "D011", + "minor": "002D", + "timeMark": 143, + "seenTime": 50 + } + ], + "beacons": [] + } + } + }, + { + "name": "contact badge 80s Periodic Report 4.5.5 example 2", + "uplink": { + "end_device_ids": { + "device_id": "lt-contact-tracing-badge", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0005", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6NJ5HWK1Q3VDZS5HH0DX9ST", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:2eb35d1f-4ebc-45eb-a898-447be7a6e488" + ], + "received_at": "2026-11-05T09:55:45.681914168Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "wv//AZvfAPAg", + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "contact-tracing-badge", + "hardware_version": "1.0", + "firmware_version": "1.0", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-contact-tracing-badge", + "telemetry": { + "badges": [], + "beacons": [ + { + "minor": "019B", + "timeMark": 223, + "seenTime": 70 + }, + { + "minor": "00F0", + "timeMark": 32, + "seenTime": 10 + } + ] + } + } + }, + { + "name": "contact badge Acknowledge 4.5.4 (built from DataSheet 2.78)", + "uplink": { + "end_device_ids": { + "device_id": "lt-contact-tracing-badge", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0005", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6NJ5KGJ7QJ2B7AS9Q0VCATN", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:0aba77e7-1257-4147-9e31-31b369cc2cbe" + ], + "received_at": "2026-11-05T09:55:47.345699317Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "8Ac=", + "decoded_payload": { + "bytes": { + "msgid": "7", + "result": "downlink success", + "type": "Acknowledgment" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "contact-tracing-badge", + "hardware_version": "1.0", + "firmware_version": "1.0", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-contact-tracing-badge", + "telemetry": { + "result": "Success", + "msgid": 7 + } + } } ] diff --git a/ui-ngx/src/assets/converters/js-tti-decoder.raw b/ui-ngx/src/assets/converters/js-tti-decoder.raw index 634d265574..0723f09d7c 100644 --- a/ui-ngx/src/assets/converters/js-tti-decoder.raw +++ b/ui-ngx/src/assets/converters/js-tti-decoder.raw @@ -6,7 +6,8 @@ // Lansitec: the LoRaWAN Device Repository codecs wrap everything in "bytes", write numbers as strings with units // ("85%", "-67dBm") and get some fields wrong. Key names follow the Lansitec datasheets where there are any: // Bluetooth Gateway DataSheet 2.27, Badge Tracker DataSheet v5.30, Temperature Humidity Sensor DataSheet 1.26, -// Plug Bluetooth Gateway DataSheet 1.5 (SocketSync gateway). +// Plug Bluetooth Gateway DataSheet 1.5 (SocketSync gateway), +// LoRaWAN Bluetooth Badge DataSheet 2.78 (contact tracing badge). // The wrong fields are decoded from the raw bytes (metadata.data). Remove a fix when the Repository fixes the codec. var LANSITEC_UNITS = ['dBm', 'dB', '%', 'V', 'sec', 'min', 's', '℃', 'level', ' meter']; @@ -54,8 +55,8 @@ var LANSITEC_TEMPERATURE_HUMIDITY_KEYS = { }; // Without a datasheet only these keys are renamed: the integration writes rssi, snr and firmwareVersion itself. -// chgstate: the tracking label and the contact tracing badge have the gateway codec byte for byte, which reads -// CHGSTAT 0x50 as "Not charging" (Bluetooth Gateway DataSheet 2.27, 4.4.2), so it is dropped. number is the count +// chgstate: the tracking label has the gateway codec byte for byte, which reads CHGSTAT 0x50 as "Not charging" +// (Bluetooth Gateway DataSheet 2.27, 4.4.2), so it is dropped. number is the count // of reported devices: the beacons list has them. var LANSITEC_CLASH_KEYS = { rssi: 'loraRssi', RSSI: 'loraRssi', snr: 'loraSnr', SNR: 'loraSnr', @@ -495,15 +496,168 @@ function lansitecSocketSyncValues(b) { return values; } +// LoRaWAN Bluetooth Badge DataSheet 2.78 (990-01242), the contact tracing badge: it reports the badges and the +// Bluetooth beacons near it, it has no GNSS. Its codec is the Bluetooth gateway's, which reads these messages with +// the gateway layout, so every message is decoded from the raw bytes, as for the SocketSync gateway. +// Major and Minor are hex: a badge's are the last 4 bytes of its DevEUI (4.4). +var LANSITEC_CONTACT_BADGE = 'contact-tracing-badge'; +// 4.5.1 Type bits 2-0 (Mode) and SMODE: the LoRa frequency plan. +var LANSITEC_CONTACT_BADGE_MODES = ['', 'AU915', 'IN865', 'CN470', 'AS923', 'EU433', 'EU868', 'US915']; +var LANSITEC_CONTACT_BADGE_BANDS = { 1: 'AU915', 2: 'IN865', 4: 'CN470', 8: 'AS923', 16: 'EU433', 32: 'EU868', 64: 'US915' }; +// 4.5.1 BT bits 10-8 and 4.5.2 BTSTATUS bits 6-4: the Bluetooth transmitting power, dBm. 0 is not a power. +var LANSITEC_CONTACT_BADGE_BT_POWERS = [0, 4, 0, -4, -8, -12, -16, -20]; +// 4.5.2 BTSTATUS bits 11-8: the vibrator and buzzer. +var LANSITEC_CONTACT_BADGE_BUZZER = ['No action', 'Vibrator disabled, buzzer muted (restore)', + 'Vibrator disabled, buzzer half sound level', 'Vibrator disabled, buzzer full sound level', + 'Vibrator enabled, buzzer muted', 'Vibrator enabled, buzzer half sound level', + 'Vibrator enabled, buzzer full sound level', 'Vibrator disabled, buzzer muted (no restore)', + 'Beacon-in-tracker and proximity detection disabled']; + +// 4.5.5 TimeMark: bit n set means seen n * 10 to n * 10 + 9 seconds before the message. Seen time is 10 s per bit. +function lansitecContactBadgeSeen(item, mark) { + item.timeMark = mark; + var bits = 0; + for (var i = 0; i < 8; i++) { + bits += (mark >> i) & 1; + } + item.seenTime = bits * 10; + return item; +} + +function lansitecContactBadgeValues(b) { + var values = {}; + if (b.length == 0) { + return values; + } + var type = (b[0] >> 4) & 0x0F; + var i, start; + if (type == 0x1 && b.length >= 9) { + // 4.5.1 Register: Type (ADR, Mode), SMODE, Power, CFG, POS (2), HB, BT (2), CRC (2). + values.adr = (b[0] & 0x08) != 0 ? 'ON' : 'OFF'; + if (LANSITEC_CONTACT_BADGE_MODES[b[0] & 0x07] != '') { + values.mode = LANSITEC_CONTACT_BADGE_MODES[b[0] & 0x07]; + } + if (LANSITEC_CONTACT_BADGE_BANDS[b[1]] != null) { + values.loraWanFrequencyBand = LANSITEC_CONTACT_BADGE_BANDS[b[1]]; + } + values.loraTxPower = (b[2] >> 3) & 0x1F; + values.dataRate = 'DR' + ((b[3] >> 4) & 0x0F); + // POS unit 5 s, 0 means off. HB unit 30 s. + values.positionReportInterval = lansitecUint(b, 4, 2) * 5; + values.heartbeatInterval = b[6] * 30; + var bt = lansitecUint(b, 7, 2); + values.buzzerAndVibrator = ((bt >> 11) & 1) == 1 ? 'ON' : 'OFF'; + if (((bt >> 8) & 0x07) > 0) { + values.bluetoothTxPower = LANSITEC_CONTACT_BADGE_BT_POWERS[(bt >> 8) & 0x07]; + } + values.proximityDetection = ((bt >> 7) & 1) == 1 ? 'ON' : 'OFF'; + // 0x0-57, the datasheet gives no unit. + values.bluetoothReceivingThresholdSetting = bt & 0x7F; + } else if (type == 0x2 && b.length >= 7) { + // 4.5.2 Heartbeat: Type, VOL (battery level, %), RSSI (unit -1 dBm), SNR (2, signed, 0.01 dB), REV (software + // revision), CHGSTAT, CRC (2), BTSTATUS (2), then the friend badges. + values.batteryLevel = b[1]; + values.loraRssi = -b[2]; + values.loraSnr = lansitecSnr(b, 3); + values.deviceFirmwareVersion = lansitecHex(b, 5, 6); + if (b[6] == 0x0) { + values.chargeState = 'Not charging'; + } else if (b[6] == 0x5) { + values.chargeState = 'Charging'; + } else if (b[6] == 0x6) { + values.chargeState = 'Charging completed'; + } + if (b.length >= 11) { + var status = lansitecUint(b, 9, 2); + if (LANSITEC_CONTACT_BADGE_BUZZER[(status >> 8) & 0x0F] != null) { + values.vibratorBuzzerStatus = LANSITEC_CONTACT_BADGE_BUZZER[(status >> 8) & 0x0F]; + } + // 0 means not changed. The receiving threshold table is for range bit 7 = 0: -73 to -87 dBm. + if (((status >> 4) & 0x07) > 0) { + values.bluetoothTxPower = LANSITEC_CONTACT_BADGE_BT_POWERS[(status >> 4) & 0x07]; + } + if ((status & 0x0F) > 0 && ((status >> 7) & 1) == 0) { + values.bluetoothReceivingThreshold = -72 - (status & 0x0F); + } + } + // Friend field: Type 0xC with the number of friend badges (1-6), then Major (2) and Minor (2) of each. + if (b.length >= 12 && ((b[11] >> 4) & 0x0F) == 0xC && b.length >= 12 + (b[11] & 0x0F) * 4) { + var friends = []; + for (i = 0; i < (b[11] & 0x0F); i++) { + start = 12 + i * 4; + friends.push({ major: lansitecHex(b, start, start + 2), minor: lansitecHex(b, start + 2, start + 4) }); + } + values.friends = friends; + } + } else if (type == 0x8) { + // 4.5.3 10s Instant Report: Type (bits 3-0: number of beacons M), Major (2), Minor (2) and RSSI of each, then + // TOFF: (M + 1) / 2 bytes, 4 bits per beacon, the first beacon in the high bits: seconds before the message. + // RSSI bit 7: 1 a Bluetooth beacon, 0 a badge (beacon-in-tracker); bits 6-0 - 128 is the RSSI, dBm. + var count = b[0] & 0x0F; + if (count > 0 && b.length >= 1 + count * 5) { + var hasOffsets = b.length >= 1 + count * 5 + Math.floor((count + 1) / 2); + var list = []; + for (i = 0; i < count; i++) { + start = 1 + i * 5; + var item = { + major: lansitecHex(b, start, start + 2), + minor: lansitecHex(b, start + 2, start + 4), + type: (b[start + 4] & 0x80) != 0 ? 'Bluetooth beacon' : 'Badge', + rssi: (b[start + 4] & 0x7F) - 128 + }; + if (hasOffsets) { + var offsetByte = b[1 + count * 5 + Math.floor(i / 2)]; + item.timeOffset = i % 2 == 0 ? (offsetByte >> 4) & 0x0F : offsetByte & 0x0F; + } + list.push(item); + } + values.beacons = list; + } + } else if (type == 0xC) { + // 4.5.5 80s Periodic Report: Type (bits 3-0: number of badges and beacons), badges as Major (2), Minor (2), + // TimeMark, then the marker 0xFFFF and beacons as Minor (2), TimeMark. A beacon's Major is always FFFF. + var badges = []; + var periodBeacons = []; + var pos = 1; + var inBeacons = false; + while (pos < b.length) { + if (!inBeacons && pos + 2 <= b.length && b[pos] == 0xFF && b[pos + 1] == 0xFF) { + inBeacons = true; + pos += 2; + } else if (!inBeacons && pos + 5 <= b.length) { + badges.push(lansitecContactBadgeSeen({ major: lansitecHex(b, pos, pos + 2), minor: lansitecHex(b, pos + 2, pos + 4) }, b[pos + 4])); + pos += 5; + } else if (inBeacons && pos + 3 <= b.length) { + periodBeacons.push(lansitecContactBadgeSeen({ minor: lansitecHex(b, pos, pos + 2) }, b[pos + 2])); + pos += 3; + } else { + return values; + } + } + if (badges.length + periodBeacons.length == (b[0] & 0x0F)) { + values.badges = badges; + values.beacons = periodBeacons; + } + } else if (type == 0xF && b.length >= 2) { + // 4.5.4 Acknowledge: Type bits 3-0 the result, then the MSGID of the downlink. + values.result = (b[0] & 0x0F) == 0 ? 'Success' : 'Failure'; + values.msgid = b[1]; + } + return values; +} + var values = {}; var attributes = {}; var history = []; -if (metadata.brandId == 'lansitec' && metadata.modelId == LANSITEC_SOCKET_SYNC && metadata.data != null) { - // The SocketSync gateway: decoded from the raw bytes only, see lansitecSocketSyncValues. - var socketSyncBytes = lansitecBase64ToBytes(metadata.data); - values = lansitecSocketSyncValues(socketSyncBytes); - // Registration (0x4) holds the device settings: attributes, not telemetry. - if (socketSyncBytes.length > 0 && ((socketSyncBytes[0] >> 4) & 0x0F) == 0x4) { +if (metadata.brandId == 'lansitec' && (metadata.modelId == LANSITEC_SOCKET_SYNC || metadata.modelId == LANSITEC_CONTACT_BADGE) + && metadata.data != null) { + // The SocketSync gateway and the contact tracing badge: decoded from the raw bytes only, see + // lansitecSocketSyncValues and lansitecContactBadgeValues. + var lansitecBytes = lansitecBase64ToBytes(metadata.data); + var socketSync = metadata.modelId == LANSITEC_SOCKET_SYNC; + values = socketSync ? lansitecSocketSyncValues(lansitecBytes) : lansitecContactBadgeValues(lansitecBytes); + // Registration holds the device settings: attributes, not telemetry. + if (lansitecBytes.length > 0 && ((lansitecBytes[0] >> 4) & 0x0F) == (socketSync ? 0x4 : 0x1)) { attributes = values; values = {}; } diff --git a/ui-ngx/src/assets/converters/js-ttn-decoder.raw b/ui-ngx/src/assets/converters/js-ttn-decoder.raw index 634d265574..0723f09d7c 100644 --- a/ui-ngx/src/assets/converters/js-ttn-decoder.raw +++ b/ui-ngx/src/assets/converters/js-ttn-decoder.raw @@ -6,7 +6,8 @@ // Lansitec: the LoRaWAN Device Repository codecs wrap everything in "bytes", write numbers as strings with units // ("85%", "-67dBm") and get some fields wrong. Key names follow the Lansitec datasheets where there are any: // Bluetooth Gateway DataSheet 2.27, Badge Tracker DataSheet v5.30, Temperature Humidity Sensor DataSheet 1.26, -// Plug Bluetooth Gateway DataSheet 1.5 (SocketSync gateway). +// Plug Bluetooth Gateway DataSheet 1.5 (SocketSync gateway), +// LoRaWAN Bluetooth Badge DataSheet 2.78 (contact tracing badge). // The wrong fields are decoded from the raw bytes (metadata.data). Remove a fix when the Repository fixes the codec. var LANSITEC_UNITS = ['dBm', 'dB', '%', 'V', 'sec', 'min', 's', '℃', 'level', ' meter']; @@ -54,8 +55,8 @@ var LANSITEC_TEMPERATURE_HUMIDITY_KEYS = { }; // Without a datasheet only these keys are renamed: the integration writes rssi, snr and firmwareVersion itself. -// chgstate: the tracking label and the contact tracing badge have the gateway codec byte for byte, which reads -// CHGSTAT 0x50 as "Not charging" (Bluetooth Gateway DataSheet 2.27, 4.4.2), so it is dropped. number is the count +// chgstate: the tracking label has the gateway codec byte for byte, which reads CHGSTAT 0x50 as "Not charging" +// (Bluetooth Gateway DataSheet 2.27, 4.4.2), so it is dropped. number is the count // of reported devices: the beacons list has them. var LANSITEC_CLASH_KEYS = { rssi: 'loraRssi', RSSI: 'loraRssi', snr: 'loraSnr', SNR: 'loraSnr', @@ -495,15 +496,168 @@ function lansitecSocketSyncValues(b) { return values; } +// LoRaWAN Bluetooth Badge DataSheet 2.78 (990-01242), the contact tracing badge: it reports the badges and the +// Bluetooth beacons near it, it has no GNSS. Its codec is the Bluetooth gateway's, which reads these messages with +// the gateway layout, so every message is decoded from the raw bytes, as for the SocketSync gateway. +// Major and Minor are hex: a badge's are the last 4 bytes of its DevEUI (4.4). +var LANSITEC_CONTACT_BADGE = 'contact-tracing-badge'; +// 4.5.1 Type bits 2-0 (Mode) and SMODE: the LoRa frequency plan. +var LANSITEC_CONTACT_BADGE_MODES = ['', 'AU915', 'IN865', 'CN470', 'AS923', 'EU433', 'EU868', 'US915']; +var LANSITEC_CONTACT_BADGE_BANDS = { 1: 'AU915', 2: 'IN865', 4: 'CN470', 8: 'AS923', 16: 'EU433', 32: 'EU868', 64: 'US915' }; +// 4.5.1 BT bits 10-8 and 4.5.2 BTSTATUS bits 6-4: the Bluetooth transmitting power, dBm. 0 is not a power. +var LANSITEC_CONTACT_BADGE_BT_POWERS = [0, 4, 0, -4, -8, -12, -16, -20]; +// 4.5.2 BTSTATUS bits 11-8: the vibrator and buzzer. +var LANSITEC_CONTACT_BADGE_BUZZER = ['No action', 'Vibrator disabled, buzzer muted (restore)', + 'Vibrator disabled, buzzer half sound level', 'Vibrator disabled, buzzer full sound level', + 'Vibrator enabled, buzzer muted', 'Vibrator enabled, buzzer half sound level', + 'Vibrator enabled, buzzer full sound level', 'Vibrator disabled, buzzer muted (no restore)', + 'Beacon-in-tracker and proximity detection disabled']; + +// 4.5.5 TimeMark: bit n set means seen n * 10 to n * 10 + 9 seconds before the message. Seen time is 10 s per bit. +function lansitecContactBadgeSeen(item, mark) { + item.timeMark = mark; + var bits = 0; + for (var i = 0; i < 8; i++) { + bits += (mark >> i) & 1; + } + item.seenTime = bits * 10; + return item; +} + +function lansitecContactBadgeValues(b) { + var values = {}; + if (b.length == 0) { + return values; + } + var type = (b[0] >> 4) & 0x0F; + var i, start; + if (type == 0x1 && b.length >= 9) { + // 4.5.1 Register: Type (ADR, Mode), SMODE, Power, CFG, POS (2), HB, BT (2), CRC (2). + values.adr = (b[0] & 0x08) != 0 ? 'ON' : 'OFF'; + if (LANSITEC_CONTACT_BADGE_MODES[b[0] & 0x07] != '') { + values.mode = LANSITEC_CONTACT_BADGE_MODES[b[0] & 0x07]; + } + if (LANSITEC_CONTACT_BADGE_BANDS[b[1]] != null) { + values.loraWanFrequencyBand = LANSITEC_CONTACT_BADGE_BANDS[b[1]]; + } + values.loraTxPower = (b[2] >> 3) & 0x1F; + values.dataRate = 'DR' + ((b[3] >> 4) & 0x0F); + // POS unit 5 s, 0 means off. HB unit 30 s. + values.positionReportInterval = lansitecUint(b, 4, 2) * 5; + values.heartbeatInterval = b[6] * 30; + var bt = lansitecUint(b, 7, 2); + values.buzzerAndVibrator = ((bt >> 11) & 1) == 1 ? 'ON' : 'OFF'; + if (((bt >> 8) & 0x07) > 0) { + values.bluetoothTxPower = LANSITEC_CONTACT_BADGE_BT_POWERS[(bt >> 8) & 0x07]; + } + values.proximityDetection = ((bt >> 7) & 1) == 1 ? 'ON' : 'OFF'; + // 0x0-57, the datasheet gives no unit. + values.bluetoothReceivingThresholdSetting = bt & 0x7F; + } else if (type == 0x2 && b.length >= 7) { + // 4.5.2 Heartbeat: Type, VOL (battery level, %), RSSI (unit -1 dBm), SNR (2, signed, 0.01 dB), REV (software + // revision), CHGSTAT, CRC (2), BTSTATUS (2), then the friend badges. + values.batteryLevel = b[1]; + values.loraRssi = -b[2]; + values.loraSnr = lansitecSnr(b, 3); + values.deviceFirmwareVersion = lansitecHex(b, 5, 6); + if (b[6] == 0x0) { + values.chargeState = 'Not charging'; + } else if (b[6] == 0x5) { + values.chargeState = 'Charging'; + } else if (b[6] == 0x6) { + values.chargeState = 'Charging completed'; + } + if (b.length >= 11) { + var status = lansitecUint(b, 9, 2); + if (LANSITEC_CONTACT_BADGE_BUZZER[(status >> 8) & 0x0F] != null) { + values.vibratorBuzzerStatus = LANSITEC_CONTACT_BADGE_BUZZER[(status >> 8) & 0x0F]; + } + // 0 means not changed. The receiving threshold table is for range bit 7 = 0: -73 to -87 dBm. + if (((status >> 4) & 0x07) > 0) { + values.bluetoothTxPower = LANSITEC_CONTACT_BADGE_BT_POWERS[(status >> 4) & 0x07]; + } + if ((status & 0x0F) > 0 && ((status >> 7) & 1) == 0) { + values.bluetoothReceivingThreshold = -72 - (status & 0x0F); + } + } + // Friend field: Type 0xC with the number of friend badges (1-6), then Major (2) and Minor (2) of each. + if (b.length >= 12 && ((b[11] >> 4) & 0x0F) == 0xC && b.length >= 12 + (b[11] & 0x0F) * 4) { + var friends = []; + for (i = 0; i < (b[11] & 0x0F); i++) { + start = 12 + i * 4; + friends.push({ major: lansitecHex(b, start, start + 2), minor: lansitecHex(b, start + 2, start + 4) }); + } + values.friends = friends; + } + } else if (type == 0x8) { + // 4.5.3 10s Instant Report: Type (bits 3-0: number of beacons M), Major (2), Minor (2) and RSSI of each, then + // TOFF: (M + 1) / 2 bytes, 4 bits per beacon, the first beacon in the high bits: seconds before the message. + // RSSI bit 7: 1 a Bluetooth beacon, 0 a badge (beacon-in-tracker); bits 6-0 - 128 is the RSSI, dBm. + var count = b[0] & 0x0F; + if (count > 0 && b.length >= 1 + count * 5) { + var hasOffsets = b.length >= 1 + count * 5 + Math.floor((count + 1) / 2); + var list = []; + for (i = 0; i < count; i++) { + start = 1 + i * 5; + var item = { + major: lansitecHex(b, start, start + 2), + minor: lansitecHex(b, start + 2, start + 4), + type: (b[start + 4] & 0x80) != 0 ? 'Bluetooth beacon' : 'Badge', + rssi: (b[start + 4] & 0x7F) - 128 + }; + if (hasOffsets) { + var offsetByte = b[1 + count * 5 + Math.floor(i / 2)]; + item.timeOffset = i % 2 == 0 ? (offsetByte >> 4) & 0x0F : offsetByte & 0x0F; + } + list.push(item); + } + values.beacons = list; + } + } else if (type == 0xC) { + // 4.5.5 80s Periodic Report: Type (bits 3-0: number of badges and beacons), badges as Major (2), Minor (2), + // TimeMark, then the marker 0xFFFF and beacons as Minor (2), TimeMark. A beacon's Major is always FFFF. + var badges = []; + var periodBeacons = []; + var pos = 1; + var inBeacons = false; + while (pos < b.length) { + if (!inBeacons && pos + 2 <= b.length && b[pos] == 0xFF && b[pos + 1] == 0xFF) { + inBeacons = true; + pos += 2; + } else if (!inBeacons && pos + 5 <= b.length) { + badges.push(lansitecContactBadgeSeen({ major: lansitecHex(b, pos, pos + 2), minor: lansitecHex(b, pos + 2, pos + 4) }, b[pos + 4])); + pos += 5; + } else if (inBeacons && pos + 3 <= b.length) { + periodBeacons.push(lansitecContactBadgeSeen({ minor: lansitecHex(b, pos, pos + 2) }, b[pos + 2])); + pos += 3; + } else { + return values; + } + } + if (badges.length + periodBeacons.length == (b[0] & 0x0F)) { + values.badges = badges; + values.beacons = periodBeacons; + } + } else if (type == 0xF && b.length >= 2) { + // 4.5.4 Acknowledge: Type bits 3-0 the result, then the MSGID of the downlink. + values.result = (b[0] & 0x0F) == 0 ? 'Success' : 'Failure'; + values.msgid = b[1]; + } + return values; +} + var values = {}; var attributes = {}; var history = []; -if (metadata.brandId == 'lansitec' && metadata.modelId == LANSITEC_SOCKET_SYNC && metadata.data != null) { - // The SocketSync gateway: decoded from the raw bytes only, see lansitecSocketSyncValues. - var socketSyncBytes = lansitecBase64ToBytes(metadata.data); - values = lansitecSocketSyncValues(socketSyncBytes); - // Registration (0x4) holds the device settings: attributes, not telemetry. - if (socketSyncBytes.length > 0 && ((socketSyncBytes[0] >> 4) & 0x0F) == 0x4) { +if (metadata.brandId == 'lansitec' && (metadata.modelId == LANSITEC_SOCKET_SYNC || metadata.modelId == LANSITEC_CONTACT_BADGE) + && metadata.data != null) { + // The SocketSync gateway and the contact tracing badge: decoded from the raw bytes only, see + // lansitecSocketSyncValues and lansitecContactBadgeValues. + var lansitecBytes = lansitecBase64ToBytes(metadata.data); + var socketSync = metadata.modelId == LANSITEC_SOCKET_SYNC; + values = socketSync ? lansitecSocketSyncValues(lansitecBytes) : lansitecContactBadgeValues(lansitecBytes); + // Registration holds the device settings: attributes, not telemetry. + if (lansitecBytes.length > 0 && ((lansitecBytes[0] >> 4) & 0x0F) == (socketSync ? 0x4 : 0x1)) { attributes = values; values = {}; } diff --git a/ui-ngx/src/assets/converters/tbel-tti-decoder.raw b/ui-ngx/src/assets/converters/tbel-tti-decoder.raw index 8adbe6983f..a3f160c3dc 100644 --- a/ui-ngx/src/assets/converters/tbel-tti-decoder.raw +++ b/ui-ngx/src/assets/converters/tbel-tti-decoder.raw @@ -6,7 +6,8 @@ // Lansitec: the LoRaWAN Device Repository codecs wrap everything in "bytes", write numbers as strings with units // ("85%", "-67dBm") and get some fields wrong. Key names follow the Lansitec datasheets where there are any: // Bluetooth Gateway DataSheet 2.27, Badge Tracker DataSheet v5.30, Temperature Humidity Sensor DataSheet 1.26, -// Plug Bluetooth Gateway DataSheet 1.5 (SocketSync gateway). +// Plug Bluetooth Gateway DataSheet 1.5 (SocketSync gateway), +// LoRaWAN Bluetooth Badge DataSheet 2.78 (contact tracing badge). // The wrong fields are decoded from the raw bytes (metadata.data). Remove a fix when the Repository fixes the codec. var LANSITEC_UNITS = ["dBm", "dB", "%", "V", "sec", "min", "s", "℃", "level", " meter"]; @@ -54,8 +55,8 @@ var LANSITEC_TEMPERATURE_HUMIDITY_KEYS = { }; // Without a datasheet only these keys are renamed: the integration writes rssi, snr and firmwareVersion itself. -// chgstate: the tracking label and the contact tracing badge have the gateway codec byte for byte, which reads -// CHGSTAT 0x50 as "Not charging" (Bluetooth Gateway DataSheet 2.27, 4.4.2), so it is dropped. number is the count +// chgstate: the tracking label has the gateway codec byte for byte, which reads CHGSTAT 0x50 as "Not charging" +// (Bluetooth Gateway DataSheet 2.27, 4.4.2), so it is dropped. number is the count // of reported devices: the beacons list has them. var LANSITEC_CLASH_KEYS = { rssi: "loraRssi", RSSI: "loraRssi", snr: "loraSnr", SNR: "loraSnr", @@ -461,15 +462,190 @@ function lansitecSocketSyncValues(b) { return values; } +// LoRaWAN Bluetooth Badge DataSheet 2.78 (990-01242), the contact tracing badge: it reports the badges and the +// Bluetooth beacons near it, it has no GNSS. Its codec is the Bluetooth gateway's, which reads these messages with +// the gateway layout, so every message is decoded from the raw bytes, as for the SocketSync gateway. +// Major and Minor are hex: a badge's are the last 4 bytes of its DevEUI (4.4). +var LANSITEC_CONTACT_BADGE = "contact-tracing-badge"; +// 4.5.1 Type bits 2-0 (Mode) and SMODE: the LoRa frequency plan. +var LANSITEC_CONTACT_BADGE_MODES = ["", "AU915", "IN865", "CN470", "AS923", "EU433", "EU868", "US915"]; +var LANSITEC_CONTACT_BADGE_SMODES = [1, 2, 4, 8, 16, 32, 64]; +var LANSITEC_CONTACT_BADGE_BANDS = ["AU915", "IN865", "CN470", "AS923", "EU433", "EU868", "US915"]; +// 4.5.1 BT bits 10-8 and 4.5.2 BTSTATUS bits 6-4: the Bluetooth transmitting power, dBm. 0 is not a power. +var LANSITEC_CONTACT_BADGE_BT_POWERS = [0, 4, 0, -4, -8, -12, -16, -20]; +// 4.5.2 BTSTATUS bits 11-8: the vibrator and buzzer. +var LANSITEC_CONTACT_BADGE_BUZZER = ["No action", "Vibrator disabled, buzzer muted (restore)", + "Vibrator disabled, buzzer half sound level", "Vibrator disabled, buzzer full sound level", + "Vibrator enabled, buzzer muted", "Vibrator enabled, buzzer half sound level", + "Vibrator enabled, buzzer full sound level", "Vibrator disabled, buzzer muted (no restore)", + "Beacon-in-tracker and proximity detection disabled"]; + +// 4.5.5 TimeMark: bit n set means seen n * 10 to n * 10 + 9 seconds before the message. Seen time is 10 s per bit. +function lansitecContactBadgeSeen(item, mark) { + item.timeMark = mark; + var bits = 0; + for (var i = 0; i < 8; i++) { + if (((mark >> i) & 1) == 1) { + bits = bits + 1; + } + } + item.seenTime = bits * 10; +} + +function lansitecContactBadgeValues(b) { + var values = {}; + if (b.length == 0) { + return values; + } + var type = (b[0] >> 4) & 0x0F; + var hex = bytesToHex(b); + if (type == 0x1 && b.length >= 9) { + // 4.5.1 Register: Type (ADR, Mode), SMODE, Power, CFG, POS (2), HB, BT (2), CRC (2). + values.adr = (b[0] & 0x08) != 0 ? "ON" : "OFF"; + var mode = b[0] & 0x07; + if (LANSITEC_CONTACT_BADGE_MODES[mode] != "") { + values.mode = LANSITEC_CONTACT_BADGE_MODES[mode]; + } + var band = LANSITEC_CONTACT_BADGE_SMODES.indexOf(b[1] & 0xFF); + if (band >= 0) { + values.loraWanFrequencyBand = LANSITEC_CONTACT_BADGE_BANDS[band]; + } + values.loraTxPower = (b[2] >> 3) & 0x1F; + values.dataRate = "DR" + ((b[3] >> 4) & 0x0F); + // POS unit 5 s, 0 means off. HB unit 30 s. + values.positionReportInterval = parseBytesToInt(b, 4, 2, true) * 5; + values.heartbeatInterval = (b[6] & 0xFF) * 30; + var bt = parseBytesToInt(b, 7, 2, true); + values.buzzerAndVibrator = ((bt >> 11) & 1) == 1 ? "ON" : "OFF"; + var power = (bt >> 8) & 0x07; + if (power > 0) { + values.bluetoothTxPower = LANSITEC_CONTACT_BADGE_BT_POWERS[power]; + } + values.proximityDetection = ((bt >> 7) & 1) == 1 ? "ON" : "OFF"; + // 0x0-57, the datasheet gives no unit. + values.bluetoothReceivingThresholdSetting = bt & 0x7F; + } else if (type == 0x2 && b.length >= 7) { + // 4.5.2 Heartbeat: Type, VOL (battery level, %), RSSI (unit -1 dBm), SNR (2, signed, 0.01 dB), REV (software + // revision), CHGSTAT, CRC (2), BTSTATUS (2), then the friend badges. + values.batteryLevel = b[1] & 0xFF; + values.loraRssi = 0 - (b[2] & 0xFF); + values.loraSnr = lansitecSnr(b, 3); + values.deviceFirmwareVersion = hex.substring(10, 12); + var charge = b[6] & 0xFF; + if (charge == 0x0) { + values.chargeState = "Not charging"; + } else if (charge == 0x5) { + values.chargeState = "Charging"; + } else if (charge == 0x6) { + values.chargeState = "Charging completed"; + } + if (b.length >= 11) { + var status = parseBytesToInt(b, 9, 2, true); + var buzzer = (status >> 8) & 0x0F; + if (buzzer < LANSITEC_CONTACT_BADGE_BUZZER.size()) { + values.vibratorBuzzerStatus = LANSITEC_CONTACT_BADGE_BUZZER[buzzer]; + } + // 0 means not changed. The receiving threshold table is for range bit 7 = 0: -73 to -87 dBm. + var statusPower = (status >> 4) & 0x07; + if (statusPower > 0) { + values.bluetoothTxPower = LANSITEC_CONTACT_BADGE_BT_POWERS[statusPower]; + } + var threshold = status & 0x0F; + if (threshold > 0 && ((status >> 7) & 1) == 0) { + values.bluetoothReceivingThreshold = -72 - threshold; + } + } + // Friend field: Type 0xC with the number of friend badges (1-6), then Major (2) and Minor (2) of each. + if (b.length >= 12 && ((b[11] >> 4) & 0x0F) == 0xC) { + var friendCount = b[11] & 0x0F; + if (b.length >= 12 + friendCount * 4) { + var friends = []; + for (var f = 0; f < friendCount; f++) { + var friendStart = 12 + f * 4; + friends.add({ major: hex.substring(2 * friendStart, 2 * friendStart + 4), + minor: hex.substring(2 * friendStart + 4, 2 * friendStart + 8) }); + } + values.friends = friends; + } + } + } else if (type == 0x8) { + // 4.5.3 10s Instant Report: Type (bits 3-0: number of beacons M), Major (2), Minor (2) and RSSI of each, then + // TOFF: (M + 1) / 2 bytes, 4 bits per beacon, the first beacon in the high bits: seconds before the message. + // RSSI bit 7: 1 a Bluetooth beacon, 0 a badge (beacon-in-tracker); bits 6-0 - 128 is the RSSI, dBm. + var count = b[0] & 0x0F; + if (count > 0 && b.length >= 1 + count * 5) { + var hasOffsets = b.length >= 1 + count * 5 + (count + 1) / 2; + var list = []; + for (var i = 0; i < count; i++) { + var start = 1 + i * 5; + var item = {}; + item.major = hex.substring(2 * start, 2 * start + 4); + item.minor = hex.substring(2 * start + 4, 2 * start + 8); + var rssi = b[start + 4] & 0xFF; + item.type = (rssi & 0x80) != 0 ? "Bluetooth beacon" : "Badge"; + item.rssi = (rssi & 0x7F) - 128; + if (hasOffsets) { + var offsetByte = b[1 + count * 5 + (i / 2).intValue()] & 0xFF; + item.timeOffset = i % 2 == 0 ? (offsetByte >> 4) & 0x0F : offsetByte & 0x0F; + } + list.add(item); + } + values.beacons = list; + } + } else if (type == 0xC) { + // 4.5.5 80s Periodic Report: Type (bits 3-0: number of badges and beacons), badges as Major (2), Minor (2), + // TimeMark, then the marker 0xFFFF and beacons as Minor (2), TimeMark. A beacon's Major is always FFFF. + var badges = []; + var periodBeacons = []; + var pos = 1; + var inBeacons = false; + while (pos < b.length) { + if (!inBeacons && pos + 2 <= b.length && (b[pos] & 0xFF) == 0xFF && (b[pos + 1] & 0xFF) == 0xFF) { + inBeacons = true; + pos = pos + 2; + } else if (!inBeacons && pos + 5 <= b.length) { + var badge = { major: hex.substring(2 * pos, 2 * pos + 4), minor: hex.substring(2 * pos + 4, 2 * pos + 8) }; + lansitecContactBadgeSeen(badge, b[pos + 4] & 0xFF); + badges.add(badge); + pos = pos + 5; + } else if (inBeacons && pos + 3 <= b.length) { + var beacon = { minor: hex.substring(2 * pos, 2 * pos + 4) }; + lansitecContactBadgeSeen(beacon, b[pos + 2] & 0xFF); + periodBeacons.add(beacon); + pos = pos + 3; + } else { + return values; + } + } + if (badges.size() + periodBeacons.size() == (b[0] & 0x0F)) { + values.badges = badges; + values.beacons = periodBeacons; + } + } else if (type == 0xF && b.length >= 2) { + // 4.5.4 Acknowledge: Type bits 3-0 the result, then the MSGID of the downlink. + values.result = (b[0] & 0x0F) == 0 ? "Success" : "Failure"; + values.msgid = b[1] & 0xFF; + } + return values; +} + var values = {}; var attributes = {}; var history = []; -if (metadata.brandId == "lansitec" && metadata.modelId == LANSITEC_SOCKET_SYNC && metadata.data != null) { - // The SocketSync gateway: decoded from the raw bytes only, see lansitecSocketSyncValues. - var socketSyncBytes = base64ToBytes(metadata.data); - values = lansitecSocketSyncValues(socketSyncBytes); - // Registration (0x4) holds the device settings: attributes, not telemetry. - if (socketSyncBytes.length > 0 && ((socketSyncBytes[0] >> 4) & 0x0F) == 0x4) { +if (metadata.brandId == "lansitec" && (metadata.modelId == LANSITEC_SOCKET_SYNC || metadata.modelId == LANSITEC_CONTACT_BADGE) + && metadata.data != null) { + // The SocketSync gateway and the contact tracing badge: decoded from the raw bytes only, see + // lansitecSocketSyncValues and lansitecContactBadgeValues. + var lansitecBytes = base64ToBytes(metadata.data); + var registerType = 0x1; + if (metadata.modelId == LANSITEC_SOCKET_SYNC) { + values = lansitecSocketSyncValues(lansitecBytes); + registerType = 0x4; + } else { + values = lansitecContactBadgeValues(lansitecBytes); + } + // Registration holds the device settings: attributes, not telemetry. + if (lansitecBytes.length > 0 && ((lansitecBytes[0] >> 4) & 0x0F) == registerType) { attributes = values; values = {}; } diff --git a/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw b/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw index 8adbe6983f..a3f160c3dc 100644 --- a/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw +++ b/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw @@ -6,7 +6,8 @@ // Lansitec: the LoRaWAN Device Repository codecs wrap everything in "bytes", write numbers as strings with units // ("85%", "-67dBm") and get some fields wrong. Key names follow the Lansitec datasheets where there are any: // Bluetooth Gateway DataSheet 2.27, Badge Tracker DataSheet v5.30, Temperature Humidity Sensor DataSheet 1.26, -// Plug Bluetooth Gateway DataSheet 1.5 (SocketSync gateway). +// Plug Bluetooth Gateway DataSheet 1.5 (SocketSync gateway), +// LoRaWAN Bluetooth Badge DataSheet 2.78 (contact tracing badge). // The wrong fields are decoded from the raw bytes (metadata.data). Remove a fix when the Repository fixes the codec. var LANSITEC_UNITS = ["dBm", "dB", "%", "V", "sec", "min", "s", "℃", "level", " meter"]; @@ -54,8 +55,8 @@ var LANSITEC_TEMPERATURE_HUMIDITY_KEYS = { }; // Without a datasheet only these keys are renamed: the integration writes rssi, snr and firmwareVersion itself. -// chgstate: the tracking label and the contact tracing badge have the gateway codec byte for byte, which reads -// CHGSTAT 0x50 as "Not charging" (Bluetooth Gateway DataSheet 2.27, 4.4.2), so it is dropped. number is the count +// chgstate: the tracking label has the gateway codec byte for byte, which reads CHGSTAT 0x50 as "Not charging" +// (Bluetooth Gateway DataSheet 2.27, 4.4.2), so it is dropped. number is the count // of reported devices: the beacons list has them. var LANSITEC_CLASH_KEYS = { rssi: "loraRssi", RSSI: "loraRssi", snr: "loraSnr", SNR: "loraSnr", @@ -461,15 +462,190 @@ function lansitecSocketSyncValues(b) { return values; } +// LoRaWAN Bluetooth Badge DataSheet 2.78 (990-01242), the contact tracing badge: it reports the badges and the +// Bluetooth beacons near it, it has no GNSS. Its codec is the Bluetooth gateway's, which reads these messages with +// the gateway layout, so every message is decoded from the raw bytes, as for the SocketSync gateway. +// Major and Minor are hex: a badge's are the last 4 bytes of its DevEUI (4.4). +var LANSITEC_CONTACT_BADGE = "contact-tracing-badge"; +// 4.5.1 Type bits 2-0 (Mode) and SMODE: the LoRa frequency plan. +var LANSITEC_CONTACT_BADGE_MODES = ["", "AU915", "IN865", "CN470", "AS923", "EU433", "EU868", "US915"]; +var LANSITEC_CONTACT_BADGE_SMODES = [1, 2, 4, 8, 16, 32, 64]; +var LANSITEC_CONTACT_BADGE_BANDS = ["AU915", "IN865", "CN470", "AS923", "EU433", "EU868", "US915"]; +// 4.5.1 BT bits 10-8 and 4.5.2 BTSTATUS bits 6-4: the Bluetooth transmitting power, dBm. 0 is not a power. +var LANSITEC_CONTACT_BADGE_BT_POWERS = [0, 4, 0, -4, -8, -12, -16, -20]; +// 4.5.2 BTSTATUS bits 11-8: the vibrator and buzzer. +var LANSITEC_CONTACT_BADGE_BUZZER = ["No action", "Vibrator disabled, buzzer muted (restore)", + "Vibrator disabled, buzzer half sound level", "Vibrator disabled, buzzer full sound level", + "Vibrator enabled, buzzer muted", "Vibrator enabled, buzzer half sound level", + "Vibrator enabled, buzzer full sound level", "Vibrator disabled, buzzer muted (no restore)", + "Beacon-in-tracker and proximity detection disabled"]; + +// 4.5.5 TimeMark: bit n set means seen n * 10 to n * 10 + 9 seconds before the message. Seen time is 10 s per bit. +function lansitecContactBadgeSeen(item, mark) { + item.timeMark = mark; + var bits = 0; + for (var i = 0; i < 8; i++) { + if (((mark >> i) & 1) == 1) { + bits = bits + 1; + } + } + item.seenTime = bits * 10; +} + +function lansitecContactBadgeValues(b) { + var values = {}; + if (b.length == 0) { + return values; + } + var type = (b[0] >> 4) & 0x0F; + var hex = bytesToHex(b); + if (type == 0x1 && b.length >= 9) { + // 4.5.1 Register: Type (ADR, Mode), SMODE, Power, CFG, POS (2), HB, BT (2), CRC (2). + values.adr = (b[0] & 0x08) != 0 ? "ON" : "OFF"; + var mode = b[0] & 0x07; + if (LANSITEC_CONTACT_BADGE_MODES[mode] != "") { + values.mode = LANSITEC_CONTACT_BADGE_MODES[mode]; + } + var band = LANSITEC_CONTACT_BADGE_SMODES.indexOf(b[1] & 0xFF); + if (band >= 0) { + values.loraWanFrequencyBand = LANSITEC_CONTACT_BADGE_BANDS[band]; + } + values.loraTxPower = (b[2] >> 3) & 0x1F; + values.dataRate = "DR" + ((b[3] >> 4) & 0x0F); + // POS unit 5 s, 0 means off. HB unit 30 s. + values.positionReportInterval = parseBytesToInt(b, 4, 2, true) * 5; + values.heartbeatInterval = (b[6] & 0xFF) * 30; + var bt = parseBytesToInt(b, 7, 2, true); + values.buzzerAndVibrator = ((bt >> 11) & 1) == 1 ? "ON" : "OFF"; + var power = (bt >> 8) & 0x07; + if (power > 0) { + values.bluetoothTxPower = LANSITEC_CONTACT_BADGE_BT_POWERS[power]; + } + values.proximityDetection = ((bt >> 7) & 1) == 1 ? "ON" : "OFF"; + // 0x0-57, the datasheet gives no unit. + values.bluetoothReceivingThresholdSetting = bt & 0x7F; + } else if (type == 0x2 && b.length >= 7) { + // 4.5.2 Heartbeat: Type, VOL (battery level, %), RSSI (unit -1 dBm), SNR (2, signed, 0.01 dB), REV (software + // revision), CHGSTAT, CRC (2), BTSTATUS (2), then the friend badges. + values.batteryLevel = b[1] & 0xFF; + values.loraRssi = 0 - (b[2] & 0xFF); + values.loraSnr = lansitecSnr(b, 3); + values.deviceFirmwareVersion = hex.substring(10, 12); + var charge = b[6] & 0xFF; + if (charge == 0x0) { + values.chargeState = "Not charging"; + } else if (charge == 0x5) { + values.chargeState = "Charging"; + } else if (charge == 0x6) { + values.chargeState = "Charging completed"; + } + if (b.length >= 11) { + var status = parseBytesToInt(b, 9, 2, true); + var buzzer = (status >> 8) & 0x0F; + if (buzzer < LANSITEC_CONTACT_BADGE_BUZZER.size()) { + values.vibratorBuzzerStatus = LANSITEC_CONTACT_BADGE_BUZZER[buzzer]; + } + // 0 means not changed. The receiving threshold table is for range bit 7 = 0: -73 to -87 dBm. + var statusPower = (status >> 4) & 0x07; + if (statusPower > 0) { + values.bluetoothTxPower = LANSITEC_CONTACT_BADGE_BT_POWERS[statusPower]; + } + var threshold = status & 0x0F; + if (threshold > 0 && ((status >> 7) & 1) == 0) { + values.bluetoothReceivingThreshold = -72 - threshold; + } + } + // Friend field: Type 0xC with the number of friend badges (1-6), then Major (2) and Minor (2) of each. + if (b.length >= 12 && ((b[11] >> 4) & 0x0F) == 0xC) { + var friendCount = b[11] & 0x0F; + if (b.length >= 12 + friendCount * 4) { + var friends = []; + for (var f = 0; f < friendCount; f++) { + var friendStart = 12 + f * 4; + friends.add({ major: hex.substring(2 * friendStart, 2 * friendStart + 4), + minor: hex.substring(2 * friendStart + 4, 2 * friendStart + 8) }); + } + values.friends = friends; + } + } + } else if (type == 0x8) { + // 4.5.3 10s Instant Report: Type (bits 3-0: number of beacons M), Major (2), Minor (2) and RSSI of each, then + // TOFF: (M + 1) / 2 bytes, 4 bits per beacon, the first beacon in the high bits: seconds before the message. + // RSSI bit 7: 1 a Bluetooth beacon, 0 a badge (beacon-in-tracker); bits 6-0 - 128 is the RSSI, dBm. + var count = b[0] & 0x0F; + if (count > 0 && b.length >= 1 + count * 5) { + var hasOffsets = b.length >= 1 + count * 5 + (count + 1) / 2; + var list = []; + for (var i = 0; i < count; i++) { + var start = 1 + i * 5; + var item = {}; + item.major = hex.substring(2 * start, 2 * start + 4); + item.minor = hex.substring(2 * start + 4, 2 * start + 8); + var rssi = b[start + 4] & 0xFF; + item.type = (rssi & 0x80) != 0 ? "Bluetooth beacon" : "Badge"; + item.rssi = (rssi & 0x7F) - 128; + if (hasOffsets) { + var offsetByte = b[1 + count * 5 + (i / 2).intValue()] & 0xFF; + item.timeOffset = i % 2 == 0 ? (offsetByte >> 4) & 0x0F : offsetByte & 0x0F; + } + list.add(item); + } + values.beacons = list; + } + } else if (type == 0xC) { + // 4.5.5 80s Periodic Report: Type (bits 3-0: number of badges and beacons), badges as Major (2), Minor (2), + // TimeMark, then the marker 0xFFFF and beacons as Minor (2), TimeMark. A beacon's Major is always FFFF. + var badges = []; + var periodBeacons = []; + var pos = 1; + var inBeacons = false; + while (pos < b.length) { + if (!inBeacons && pos + 2 <= b.length && (b[pos] & 0xFF) == 0xFF && (b[pos + 1] & 0xFF) == 0xFF) { + inBeacons = true; + pos = pos + 2; + } else if (!inBeacons && pos + 5 <= b.length) { + var badge = { major: hex.substring(2 * pos, 2 * pos + 4), minor: hex.substring(2 * pos + 4, 2 * pos + 8) }; + lansitecContactBadgeSeen(badge, b[pos + 4] & 0xFF); + badges.add(badge); + pos = pos + 5; + } else if (inBeacons && pos + 3 <= b.length) { + var beacon = { minor: hex.substring(2 * pos, 2 * pos + 4) }; + lansitecContactBadgeSeen(beacon, b[pos + 2] & 0xFF); + periodBeacons.add(beacon); + pos = pos + 3; + } else { + return values; + } + } + if (badges.size() + periodBeacons.size() == (b[0] & 0x0F)) { + values.badges = badges; + values.beacons = periodBeacons; + } + } else if (type == 0xF && b.length >= 2) { + // 4.5.4 Acknowledge: Type bits 3-0 the result, then the MSGID of the downlink. + values.result = (b[0] & 0x0F) == 0 ? "Success" : "Failure"; + values.msgid = b[1] & 0xFF; + } + return values; +} + var values = {}; var attributes = {}; var history = []; -if (metadata.brandId == "lansitec" && metadata.modelId == LANSITEC_SOCKET_SYNC && metadata.data != null) { - // The SocketSync gateway: decoded from the raw bytes only, see lansitecSocketSyncValues. - var socketSyncBytes = base64ToBytes(metadata.data); - values = lansitecSocketSyncValues(socketSyncBytes); - // Registration (0x4) holds the device settings: attributes, not telemetry. - if (socketSyncBytes.length > 0 && ((socketSyncBytes[0] >> 4) & 0x0F) == 0x4) { +if (metadata.brandId == "lansitec" && (metadata.modelId == LANSITEC_SOCKET_SYNC || metadata.modelId == LANSITEC_CONTACT_BADGE) + && metadata.data != null) { + // The SocketSync gateway and the contact tracing badge: decoded from the raw bytes only, see + // lansitecSocketSyncValues and lansitecContactBadgeValues. + var lansitecBytes = base64ToBytes(metadata.data); + var registerType = 0x1; + if (metadata.modelId == LANSITEC_SOCKET_SYNC) { + values = lansitecSocketSyncValues(lansitecBytes); + registerType = 0x4; + } else { + values = lansitecContactBadgeValues(lansitecBytes); + } + // Registration holds the device settings: attributes, not telemetry. + if (lansitecBytes.length > 0 && ((lansitecBytes[0] >> 4) & 0x0F) == registerType) { attributes = values; values = {}; } From c8a3f3a7ca56f7b876f629e50679827d4e8fe5a8 Mon Sep 17 00:00:00 2001 From: dpinkevych Date: Fri, 6 Nov 2026 11:58:40 +0200 Subject: [PATCH 09/13] fix(iot-hub): review of the Lansitec converter: the main logic first, shared Lansitec helpers, short comments, and fixes by the datasheets - TBEL Instant Report of the contact tracing badge: (count + 1) / 2 as an int, so an even count gets timeOffset - SocketSync UUID: prefix ...AD4657 for tails 0x00-0x1E (Plug Bluetooth Gateway 1.5, Table 7) - beacons in the order of their numbers, gaps included (Container Tracker 5.7 Offline Cache) - time1..time8 of the container Offline Cache without the codec's 8 hours, as gnssTime1.. - badge tracker Heartbeat without the SNR field (before firmware 0x0501) decided by the length - helmet pressure / 10 (Helmet Tracker 6.3, unit 0.1 Pa) - gateway firmware V1.08 from VER, as in Bluetooth Gateway 4.4.2 - shared helpers for the charge state, Register, Heartbeat and Acknowledgment; one MODES/BANDS table - the test keeps the default TTN/TTI converters under 30000 characters; 4 more uplinks in the fixture --- .../controller/ConverterControllerTest.java | 9 + .../things_stack_lansitec_uplinks.json | 307 ++++++++++- .../src/assets/converters/js-tti-decoder.raw | 482 ++++++++--------- .../src/assets/converters/js-ttn-decoder.raw | 482 ++++++++--------- .../assets/converters/tbel-tti-decoder.raw | 506 ++++++++---------- .../assets/converters/tbel-ttn-decoder.raw | 506 ++++++++---------- 6 files changed, 1230 insertions(+), 1062 deletions(-) diff --git a/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java index 78c02874bc..4549ed5bb8 100644 --- a/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java @@ -788,6 +788,15 @@ public class ConverterControllerTest extends AbstractControllerTest { } } + // The script body limit is 50000 characters (tbel and js max_script_body_size), comments included: the default + // TTN/TTI converters stay well under it, so that a user can add decoding of their own devices + @Test + public void testThingsStackDefaultConvertersStayWellUnderScriptSizeLimit() throws IOException { + for (String decoder : List.of("tbel-ttn", "tbel-tti", "js-ttn", "js-tti")) { + assertThat(readResource("converters/" + decoder + "-decoder.raw").length()).as(decoder).isLessThan(30000); + } + } + // A frame too short for its message type, and bytes that are not an object: the uplink is still converted @Test public void testThingsStackDefaultConvertersKeepMalformedLansitecUplinks() throws IOException { diff --git a/application/src/test/resources/payload/things_stack_lansitec_uplinks.json b/application/src/test/resources/payload/things_stack_lansitec_uplinks.json index c9ecb72c4b..44921746c1 100644 --- a/application/src/test/resources/payload/things_stack_lansitec_uplinks.json +++ b/application/src/test/resources/payload/things_stack_lansitec_uplinks.json @@ -141,7 +141,7 @@ "profile": "lansitec-macro-bluetooth-gateway", "telemetry": { "loraRssi": -62.0, - "deviceFirmwareVersion": "VER108", + "deviceFirmwareVersion": "V1.08", "batteryVoltage": 3.53, "loraSnr": 8.3 } @@ -210,7 +210,7 @@ "profile": "lansitec-compact-bluetooth-gateway", "telemetry": { "loraRssi": -62.0, - "deviceFirmwareVersion": "VER108", + "deviceFirmwareVersion": "V1.08", "batteryLevel": 100, "loraSnr": 8.3, "chargeState": "Charging" @@ -280,7 +280,7 @@ "profile": "lansitec-solar-bluetooth-gateway", "telemetry": { "loraRssi": -62.0, - "deviceFirmwareVersion": "VER108", + "deviceFirmwareVersion": "V1.08", "batteryLevel": 100, "loraSnr": -5.0, "chargeState": "Charging completed" @@ -1366,7 +1366,7 @@ "gpsState": "gps location success", "latitude": 31.975675579986046, "longitude": 118.77103314054408, - "pressure": 100000, + "pressure": 10000.0, "gnssTime": 1702029480000, "wearState": "wear" } @@ -2369,5 +2369,304 @@ "msgid": 7 } } + }, + { + "name": "badge Heartbeat 5.2 without SNR, firmware before 0x0501 (built)", + "uplink": { + "end_device_ids": { + "device_id": "lt-badge-tracker", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0007", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6DX1KWZBE1ANRSZWHFSKQ89", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:29eb6310-c010-4972-897c-d743a41f1d8d" + ], + "received_at": "2026-11-02T10:31:55.550797518Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "IGQ+NWAAAA==", + "decoded_payload": { + "bytes": { + "Message type": "Heartbeat", + "Battery level": "100%", + "RSSI": "-62dBm", + "SNR": "136.64000000000001dB", + "GPS state": "GPS OFF", + "Move state": "No Move", + "Charge state": "Power cable disconnected" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "badge-tracker", + "hardware_version": "1.0", + "firmware_version": "5.25", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-badge-tracker", + "telemetry": { + "batteryLevel": 100.0, + "loraRssi": -62.0, + "gnssState": "GPS located", + "chargeState": "Charge complete", + "movingState": 5 + } + } + }, + { + "name": "container Offline Cache Position 5.7, BLE GNSS BLE (built from the codec)", + "uplink": { + "end_device_ids": { + "device_id": "lt-container-tracker", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0008", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6DX2TBAJT5T5EE3615Y9MAW", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:b64cce74-6708-4423-aa37-eca108e9f585" + ], + "received_at": "2026-11-02T10:32:34.922307414Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "s0AAAQACs0LtisRB/84uZXLoqAADAAS0", + "decoded_payload": { + "bytes": { + "type": "OfflineCachePosition", + "length": 3, + "flag": "010", + "flag1": "1 is Bluetooth position data", + "flag2": "2 is GNSS position data", + "flag3": "3 is Bluetooth position data", + "dev1major": "0001", + "dev1minor": "0002", + "dev1rssi": "-77dBm", + "longitude2": 118.77103314054408, + "latitude2": 31.975675579986046, + "time2": "\"2023-12-08T17:58:00.000Z\"", + "dev3major": "0003", + "dev3minor": "0004", + "dev3rssi": "-76dBm" + } + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "container-tracker", + "hardware_version": "1.0", + "firmware_version": "1.0", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-container-tracker", + "telemetry": { + "length": 3, + "flag": "010", + "flag1": "1 is Bluetooth position data", + "flag2": "2 is GNSS position data", + "flag3": "3 is Bluetooth position data", + "longitude2": 118.77103314054408, + "latitude2": 31.975675579986046, + "gnssTime2": 1702029480000, + "beacons": [ + { + "major": "0001", + "minor": "0002", + "rssi": -77 + }, + { + "major": "0003", + "minor": "0004", + "rssi": -76 + } + ] + } + } + }, + { + "name": "socketsync Beacon 5.3, UUID tail 0x05 (built from DataSheet 1.5)", + "uplink": { + "end_device_ids": { + "device_id": "lt-socket-sync-bluetooth-gateway", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0004", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6DX3DFWNQE3WSG9JNFTAG7B", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:ec26c3c1-e861-4d65-b6e7-1206e538a5c7" + ], + "received_at": "2026-11-02T10:32:54.523762899Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "YQUAAQACsA==", + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "socket-sync-bluetooth-gateway", + "hardware_version": "1.0", + "firmware_version": "1.0", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-socket-sync-bluetooth-gateway", + "telemetry": { + "beacons": [ + { + "uuid": "5C38DBDE567C4CCAB1DA40A8AD465705", + "major": 1, + "minor": 2, + "rssi": -80 + } + ] + } + } + }, + { + "name": "contact badge 10s Instant Report 4.5.3, 2 beacons (built from DataSheet 2.78)", + "uplink": { + "end_device_ids": { + "device_id": "lt-contact-tracing-badge", + "application_ids": { + "application_id": "ansitec-test" + }, + "dev_eui": "70B3D57ED01A0005", + "join_eui": "0000000000000000" + }, + "correlation_ids": [ + "as:up:01M6NJ5EMKTH1Q7R7AD5WA4HB6", + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:e9a53f5b-bd3b-478b-8ff9-4c3424e1337f" + ], + "received_at": "2026-11-05T09:55:42.353804675Z", + "uplink_message": { + "f_port": 1, + "frm_payload": "gtMQAj7T0BEALqsk", + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "test" + }, + "rssi": -42, + "channel_rssi": -42, + "snr": 4.2 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 7, + "coding_rate": "4/5" + } + }, + "frequency": "868100000" + }, + "version_ids": { + "brand_id": "lansitec", + "model_id": "contact-tracing-badge", + "hardware_version": "1.0", + "firmware_version": "1.0", + "band_id": "EU_863_870" + } + }, + "simulated": true + }, + "decoded": { + "profile": "lansitec-contact-tracing-badge", + "telemetry": { + "beacons": [ + { + "major": "D310", + "minor": "023E", + "type": "Bluetooth beacon", + "rssi": -45, + "timeOffset": 2 + }, + { + "major": "D011", + "minor": "002E", + "type": "Bluetooth beacon", + "rssi": -85, + "timeOffset": 4 + } + ] + } + } } ] diff --git a/ui-ngx/src/assets/converters/js-tti-decoder.raw b/ui-ngx/src/assets/converters/js-tti-decoder.raw index 0723f09d7c..1a0aa27613 100644 --- a/ui-ngx/src/assets/converters/js-tti-decoder.raw +++ b/ui-ngx/src/assets/converters/js-tti-decoder.raw @@ -2,38 +2,70 @@ // is then that JSON: its keys become telemetry. A Milesight-style history list keeps each record's time. // Such a device gets the profile -, so all devices of one model share it, // or the name the integration metadata gives it under profile.-. Any other device gets LoRaWAN Device. +// Lansitec devices are decoded by the Lansitec block below. +function decodeThingsStackUplink(payload, metadata) { + var values = {}; + var attributes = {}; + var history = []; + var lansitec = metadata.brandId == 'lansitec' ? lansitecUplink(payload, metadata) : null; + if (lansitec != null) { + values = lansitec.telemetry; + attributes = lansitec.attributes; + } else if (metadata.payloadFormat == 'JSON') { + values = decodeToJson(payload); + if (Array.isArray(values.history)) { + values.history.forEach(function (record) { + if (record.timestamp != null) { + var ts = record.timestamp * 1000; + delete record.timestamp; + history.push({ ts: ts, values: record }); + } + }); + delete values.history; + } + } + var profile = 'LoRaWAN Device'; + if (metadata.brandId != null && metadata.modelId != null) { + profile = metadata.brandId + '-' + metadata.modelId; + // The profile name chosen at install, kept in the integration metadata + var customProfile = metadata['profile.' + profile]; + if (customProfile != null && customProfile != '') { + profile = customProfile; + } + } + if (Object.keys(attributes).length > 0) { + return { profile: profile, attributes: attributes }; + } + if (history.length == 0) { + return { profile: profile, telemetry: { ts: metadata.ts, values: values } }; + } + history.push({ ts: metadata.ts, values: values }); + return { profile: profile, telemetry: history }; +} -// Lansitec: the LoRaWAN Device Repository codecs wrap everything in "bytes", write numbers as strings with units -// ("85%", "-67dBm") and get some fields wrong. Key names follow the Lansitec datasheets where there are any: -// Bluetooth Gateway DataSheet 2.27, Badge Tracker DataSheet v5.30, Temperature Humidity Sensor DataSheet 1.26, -// Plug Bluetooth Gateway DataSheet 1.5 (SocketSync gateway), -// LoRaWAN Bluetooth Badge DataSheet 2.78 (contact tracing badge). -// The wrong fields are decoded from the raw bytes (metadata.data). Remove a fix when the Repository fixes the codec. -var LANSITEC_UNITS = ['dBm', 'dB', '%', 'V', 'sec', 'min', 's', '℃', 'level', ' meter']; +function decodeToJson(payload) { + return JSON.parse(String.fromCharCode.apply(String, payload)); +} +// --------------------------------------------------------------------------------------------------------------------- +// Lansitec. The Repository codecs wrap everything in "bytes", write numbers with units ("85%", "-67dBm") and get some +// fields wrong: those are decoded from the raw bytes (metadata.data) by the datasheets. Remove a fix when the codec is fixed. +// Datasheets: Bluetooth Gateway 2.27, Badge Tracker v5.30, Temperature Humidity Sensor 1.26, Plug Bluetooth Gateway 1.5 +// (SocketSync), LoRaWAN Bluetooth Badge 2.78 (contact tracing badge), Container Tracker 0.72, Helmet Tracker v0.14. +var LANSITEC_UNITS = ['dBm', 'dB', '%', 'V', 'sec', 'min', 's', '℃', 'level', ' meter']; var LANSITEC_GATEWAYS = ['compact-bluetooth-gateway', 'indoor-bluetooth-gateway', 'macro-bluetooth-gateway', 'solar-bluetooth-gateway']; +var LANSITEC_SOCKET_SYNC = 'socket-sync-bluetooth-gateway'; +var LANSITEC_CONTACT_BADGE = 'contact-tracing-badge'; -// Codec key -> datasheet name, per model group. '-' drops the key. -// -// Bluetooth Gateway DataSheet 2.27, 4.4.1 Registration: ADR, Mode (current working LoRaWAN frequency sub-band plan), -// LoRaWAN frequency band, Power (LoRa TX power, dBm), Continuously BLE receiving, DR (LoRaWAN Uplink Data Rate), -// PRI (Position report interval), HB (Heartbeat interval), Bluetooth receiving duration. -// 4.4.2 Heartbeat: RSSI and SNR (LoRa), VER (Firmware version). VOL, SNR and CHGSTAT come from the raw bytes. -// number is the count of reported devices: the beacons list has them. The beacons come from the raw bytes too. +// Codec key -> datasheet name. '-' drops the key: it is decoded from the raw bytes or is not a value. +// Bluetooth Gateway 4.4.1 Registration, 4.4.2 Heartbeat. var LANSITEC_GATEWAY_KEYS = { adr: 'adr', mode: 'mode', LoRaWANband: 'loraWanFrequencyBand', LoRaTxpower: 'loraTxPower', ContinuousReceive: 'continuousBleReceiving', dr: 'dataRate', pos: 'positionReportInterval', hb: 'heartbeatInterval', BLEReceivingDuration: 'bluetoothReceivingDuration', - rssi: 'loraRssi', snr: '-', ver: 'deviceFirmwareVersion', vol: '-', chgstate: '-', number: '-' + rssi: 'loraRssi', snr: '-', ver: '-', vol: '-', chgstate: '-', number: '-' }; - -// Badge Tracker DataSheet v5.30, 5.1 Registration: ADR, LoRa TX power, DR, GNSSEN (GNSS enable status), -// POSMODE (Positioning report mode), BLEPRI (Bluetooth Position Report Interval). The codec reads it without the -// GNSSPRI field, so heartbeat, version, CFMMSG, HBCOUNT and fall detection come from the raw bytes. -// 5.2 Heartbeat: Battery (percentage), RSSI (LoRa), GNSSSTATE (Status of the GNSS module), CHGSTATE (Status of -// battery charging). SNR and VIBSTATE come from the raw bytes. -// 5.3 GNSS Position: longitude, latitude and time come from the raw bytes. -// 5.4 Beacon: bytes 1-5 are RFU, the codec reads byte 1 as Vibstate. 5.5 Alarm: 0x01 SOS, 0x02 Fall Detection. +// Badge Tracker 5.1 Registration (the codec skips GNSSPRI), 5.2 Heartbeat, 5.3 GNSS Position, 5.4 Beacon, 5.5 Alarm. var LANSITEC_BADGE_KEYS = { ADR: 'adr', 'Lora Txpower': 'loraTxPower', DR: 'dataRate', GNSSEN: 'gnssEnabled', 'Position mode': 'positioningReportMode', 'Position period': 'bluetoothPositionReportInterval', @@ -42,31 +74,65 @@ var LANSITEC_BADGE_KEYS = { 'Move state': '-', 'Charge state': 'chargeState', Longitude: '-', Latitude: '-', Time: '-', Length: '-', Vibstate: '-', 'Alarm type': 'alarm' }; - -// Temperature Humidity Sensor DataSheet 1.26, 2.2.1 Register: ADR, MODE (current working scheme), SMODE (data -// scheme supported), POWER (configured transmit power, dBm), DR, REPTIND (report H&T if the value is the same; -// the codec gives "false" for 0 = always report), TH (period of temperature and humidity report), CRC. -// 2.2.2 Periodical temperature and humidity: VER is the version of the message type, not the firmware; -// VOL (battery left, %), RSSI (downlink, detected by the sensor). SNR, temperature and humidity come from the raw bytes. +// Temperature Humidity Sensor 2.2.1 Register, 2.2.2 Periodical data (VER is the message version, not the firmware). var LANSITEC_TEMPERATURE_HUMIDITY_KEYS = { adr: 'adr', mode: 'mode', smode: 'smode', LoraTxpower: 'loraTxPower', dr: 'dataRate', repting: 'reptind', th: 'temperatureHumidityReportPeriod', crc: '-', ver: '-', vol: 'batteryLevel', rssi: 'loraRssi', snr: '-', temperature: '-', humidity: '-' }; - -// Without a datasheet only these keys are renamed: the integration writes rssi, snr and firmwareVersion itself. -// chgstate: the tracking label has the gateway codec byte for byte, which reads CHGSTAT 0x50 as "Not charging" -// (Bluetooth Gateway DataSheet 2.27, 4.4.2), so it is dropped. number is the count -// of reported devices: the beacons list has them. +// The other models: the integration writes rssi, snr and firmwareVersion itself. chgstate: the tracking label has the +// gateway codec, which misreads CHGSTAT (Bluetooth Gateway 4.4.2). number: the beacons list has them. var LANSITEC_CLASH_KEYS = { rssi: 'loraRssi', RSSI: 'loraRssi', snr: 'loraSnr', SNR: 'loraSnr', firmwareVersion: 'deviceFirmwareVersion', 'Firmware version': 'deviceFirmwareVersion', type: '-', 'Message type': '-', chgstate: '-', number: '-' }; - // Beacon keys: Devicetype1data1, Device1data, dev1major, dev1.major, pos1minor, asset1rssi... var LANSITEC_BEACON_PREFIXES = ['devicetype1', 'devicetype2', 'devicetype3', 'device', 'dev', 'pos', 'asset']; var LANSITEC_BEACON_FIELDS = ['data', 'major', 'minor', 'rssi', 'id']; +// Register Type bits 2-0 and SMODE: the LoRa frequency plan (Plug Bluetooth Gateway 5.1, Bluetooth Badge 4.5.1). +var LANSITEC_MODES = ['', 'AU915', 'IN865', 'CN470', 'AS923', 'EU433', 'EU868', 'US915']; +var LANSITEC_SMODES = [1, 2, 4, 8, 16, 32, 64]; +var LANSITEC_BANDS = ['AU915', 'IN865', 'CN470', 'AS923', 'EU433', 'EU868', 'US915']; +// Badge Tracker 5.2 Heartbeat, as the codec writes them. +var LANSITEC_BADGE_GNSS_STATES = ['GPS OFF', 'GPS boot', 'GPS locating', 'GPS located', '', '', '', '', '', 'GPS no signal']; +var LANSITEC_BADGE_CHARGE_STATES = ['Power cable disconnected', '', '', '', '', 'Charging', 'Charge complete']; +// Plug Bluetooth Gateway 5.1 CFG bits 3-1. +var LANSITEC_SOCKET_SYNC_TYPES = ['Outdoor gateway', 'Indoor gateway', 'Plug gateway']; +// Bluetooth Badge 4.5.1 BT bits 10-8, 4.5.2 BTSTATUS bits 6-4 (dBm, 0 is not a power) and bits 11-8. +var LANSITEC_CONTACT_BADGE_BT_POWERS = [0, 4, 0, -4, -8, -12, -16, -20]; +var LANSITEC_CONTACT_BADGE_BUZZER = ['No action', 'Vibrator disabled, buzzer muted (restore)', + 'Vibrator disabled, buzzer half sound level', 'Vibrator disabled, buzzer full sound level', + 'Vibrator enabled, buzzer muted', 'Vibrator enabled, buzzer half sound level', + 'Vibrator enabled, buzzer full sound level', 'Vibrator disabled, buzzer muted (no restore)', + 'Beacon-in-tracker and proximity detection disabled']; + +// { telemetry, attributes }, or null when the uplink is not one the Lansitec block decodes. +function lansitecUplink(payload, metadata) { + var model = metadata.modelId; + var values = null; + var register = false; + if ((model == LANSITEC_SOCKET_SYNC || model == LANSITEC_CONTACT_BADGE) && metadata.data != null) { + // Their codecs are the gateway's and misread every message: decoded from the raw bytes only. + var b = lansitecBase64ToBytes(metadata.data); + values = model == LANSITEC_SOCKET_SYNC ? lansitecSocketSyncValues(b) : lansitecContactBadgeValues(b); + register = b.length > 0 && ((b[0] >> 4) & 0x0F) == (model == LANSITEC_SOCKET_SYNC ? 0x4 : 0x1); + } else if (metadata.payloadFormat == 'JSON') { + var raw = decodeToJson(payload).bytes; + if (raw == null || typeof raw !== 'object' || Array.isArray(raw)) { + return null; + } + values = lansitecValues(raw, model); + if (metadata.data != null) { + lansitecRawValues(values, lansitecBase64ToBytes(metadata.data), model); + } + register = (raw.type != null ? raw.type : raw['Message type']) == 'Register'; + } else { + return null; + } + // Register holds the device settings: attributes, not telemetry. + return register ? { telemetry: {}, attributes: values } : { telemetry: values, attributes: {} }; +} function lansitecEndsWith(s, end) { return s.length >= end.length && s.substring(s.length - end.length) == end; @@ -163,10 +229,12 @@ function lansitecValues(raw, model) { } else if (b.field == 'data' || b.field == 'major' || b.field == 'minor') { beacons[id][b.field] = value; } - } else if (table == null && key == 'time' && typeof value == 'string') { - // Container tracker and helmet sensor, no datasheet: their codecs add 8 hours to the GNSS time and wrap - // it in quotes, as the badge tracker codec does with the UTC time of DataSheet v5.30, 5.3. - values.gnssTime = new Date(value.replace(/"/g, '')).getTime() - 8 * 60 * 60 * 1000; + } else if (table == null && /^time[1-8]?$/.test(key) && typeof value == 'string') { + // Container Tracker 5.3 and 5.7, Helmet Tracker 6.3: UTC time. The codecs add 8 hours and quote it. + values['gnssTime' + key.substring(4)] = new Date(value.replace(/"/g, '')).getTime() - 8 * 60 * 60 * 1000; + } else if (key == 'pressure' && model == 'helmet-sensor' && typeof value == 'number') { + // Helmet Tracker 6.3: the value * 10, unit 0.1 Pa. + values.pressure = value / 10; } else if (table != null && table.hasOwnProperty(key)) { if (table[key] != '-') { values[table[key]] = lansitecNumber(value); @@ -185,36 +253,30 @@ function lansitecValues(raw, model) { return values; } +// In the order of their numbers, which can have gaps (Container Tracker 5.7: dev1, dev3...). function lansitecBeaconList(beacons) { - var list = []; - var ids = Object.keys(beacons); - for (var i = 1; i <= ids.length; i++) { - ids.forEach(function (id) { - var b = beacons[id]; - if (b.order == i) { - var item = {}; - if (b.data != null) { - item.data = b.data; - } - if (b.major != null) { - item.major = b.major; - item.minor = b.minor; - } - item.rssi = b.rssi; - list.push(item); - } - }); - } - // The gateway codec reads the data of every device after the first at a 5-byte step, so it is right only - // for 4-byte data (DataSheet 4.4.4 examples): with longer data only the first device is kept. The gateways - // with a datasheet get their beacons from the raw bytes instead; this is for the models with the gateway codec. + var list = Object.keys(beacons).sort(function (a, b) { + return beacons[a].order - beacons[b].order; + }).map(function (id) { + var b = beacons[id]; + var item = {}; + if (b.data != null) { + item.data = b.data; + } + if (b.major != null) { + item.major = b.major; + item.minor = b.minor; + } + item.rssi = b.rssi; + return item; + }); + // The gateway codec reads every device after the first at a 5-byte step: right only for 4-byte data. if (list.length > 1 && list[0].data != null && list[0].data.length != 8) { return [list[0]]; } return list; } -// Decoded from the raw bytes by the datasheet where the codec gets it wrong. function lansitecRawValues(values, b, model) { if (b.length == 0) { return; @@ -281,7 +343,7 @@ function lansitecFloat(b, offset) { return bits >= 2147483648 ? -value : value; } -// SNR, unit 0.01 dB, -3000 to 3000: signed. The codecs read it unsigned. +// SNR, unit 0.01 dB, signed. The codecs read it unsigned. function lansitecSnr(b, offset) { var v = lansitecUint(b, offset, 2); if (v > 32767) { @@ -290,30 +352,67 @@ function lansitecSnr(b, offset) { return v / 100; } -// Bluetooth Gateway DataSheet 2.27. +// CHGSTAT: 0 Not charging, then the codes of Charging and Charging completed. +function lansitecChargeState(values, charge, charging, completed) { + if (charge == 0) { + values.chargeState = 'Not charging'; + } else if (charge == charging) { + values.chargeState = 'Charging'; + } else if (charge == completed) { + values.chargeState = 'Charging completed'; + } +} + +// Register of the SocketSync gateway (5.1) and the contact tracing badge (4.5.1): Type (ADR, MODE), SMODE, POWER, +// CFG, POS (2, unit 5 s), then HB (unit 30 s) at hbOffset. +function lansitecRegister(values, b, hbOffset) { + values.adr = (b[0] & 0x08) != 0 ? 'ON' : 'OFF'; + if (LANSITEC_MODES[b[0] & 0x07] != '') { + values.mode = LANSITEC_MODES[b[0] & 0x07]; + } + if (LANSITEC_SMODES.indexOf(b[1]) >= 0) { + values.loraWanFrequencyBand = LANSITEC_BANDS[LANSITEC_SMODES.indexOf(b[1])]; + } + values.loraTxPower = (b[2] >> 3) & 0x1F; + values.dataRate = 'DR' + ((b[3] >> 4) & 0x0F); + values.positionReportInterval = lansitecUint(b, 4, 2) * 5; + values.heartbeatInterval = b[hbOffset] * 30; +} + +// Heartbeat of the SocketSync gateway (5.2) and the contact tracing badge (4.5.2): Type, VOL, RSSI (unit -1 dBm), +// SNR (2), REV, CHGSTAT (0x00, 0x50, 0x60 and 0x0, 0x5, 0x6). +function lansitecHeartbeat(values, b, charging, completed) { + values.loraRssi = -b[2]; + values.loraSnr = lansitecSnr(b, 3); + values.deviceFirmwareVersion = lansitecHex(b, 5, 6); + lansitecChargeState(values, b[6], charging, completed); +} + +// Acknowledgment (Plug Bluetooth Gateway 5.5, Bluetooth Badge 4.5.4): Type bits 3-0 the result, then the MSGID. +function lansitecAcknowledgment(values, b) { + values.result = (b[0] & 0x0F) == 0 ? 'Success' : 'Failure'; + values.msgid = b[1]; +} + +// Bluetooth Gateway 2.27. function lansitecGatewayRawValues(values, b, model, type) { if (type == 1 && b.length >= 9) { - // 4.4.1: Network reconnection interval, unit 5 minutes. The codec does not decode it. + // 4.4.1: Network reconnection interval, unit 5 minutes. values.networkReconnectionInterval = b[8] * 300; } else if (type == 2 && b.length >= 8) { - // 4.4.2: VOL is the battery voltage (VOL x 0.01 + 1.5 V) on the macro gateway and the battery level (%) on - // the others. The codec decides by the value (<= 100 is %), not by the model. + // 4.4.2: VOL is the battery voltage (x 0.01 + 1.5 V) on the macro gateway, the battery level (%) on the others. if (model == 'macro-bluetooth-gateway') { values.batteryVoltage = (b[1] + 150) / 100; } else { values.batteryLevel = b[1]; } values.loraSnr = lansitecSnr(b, 3); - // CHGSTAT: 0x00 Not charging, 0x50 Charging, 0x60 Charging completed on the compact and solar gateway, - // always 0x00 on the macro and indoor gateway. The codec reads the low half of the byte. + // VER 108 is V1.08. + var version = lansitecUint(b, 5, 2); + values.deviceFirmwareVersion = 'V' + Math.floor(version / 100) + '.' + (version % 100 < 10 ? '0' : '') + version % 100; + // CHGSTAT on the compact and solar gateway only. if (model == 'compact-bluetooth-gateway' || model == 'solar-bluetooth-gateway') { - if (b[7] == 0x00) { - values.chargeState = 'Not charging'; - } else if (b[7] == 0x50) { - values.chargeState = 'Charging'; - } else if (b[7] == 0x60) { - values.chargeState = 'Charging completed'; - } + lansitecChargeState(values, b[7], 0x50, 0x60); } } else if (type == 0x8 || type == 0x9 || type == 0xA || type == 0xE) { delete values.beacons; @@ -324,12 +423,8 @@ function lansitecGatewayRawValues(values, b, model, type) { } } -// 4.4.4-4.4.6: Device Type 1, 2 and 3 messages: Type (bits 3-0: number of devices), then Data (1-31 bytes) and -// RSSI (value - 256, dBm) of each device. Data length is not in the message: it is set by the device report rule -// of the type (4.4.3), so all devices of one message have the same length. The codec reads every device after -// the first at a 5-byte step. -// 4.4.7: Multi-Device Type message: Device Type ID before the data of each device. The data length can differ per -// type, and the message does not say it: decoded only when all devices fit the same length. +// 4.4.4-4.4.6: Type (bits 3-0: number of devices), Data and RSSI (value - 256) of each device; one data length per +// message (4.4.3). 4.4.7 Multi-Device: a Device Type ID before each device, decoded when all have the same length. function lansitecGatewayBeacons(b, type) { var count = b[0] & 0x0F; if (count == 0 || (b.length - 1) % count != 0) { @@ -360,15 +455,12 @@ function lansitecGatewayBeacons(b, type) { return list; } -// Badge Tracker DataSheet v5.30. +// Badge Tracker v5.30. function lansitecBadgeRawValues(values, b, type) { if (type == 1 && b.length == 14) { - // 5.1, 14 bytes: Type, SMODE, Power, Config, BLEPRI (2), GNSSPRI (2), Heartbeat, Version (2), CFMMSG, HBCOUNT, Fall. - // BLEEN (Config bit 0) is not decoded: the field table says BLE enable status and that GNSSEN or BLEEN must - // be enabled, but the example (Config 32) has both 0 and calls bit 0 reserved. + // 5.1: GNSSPRI (2), Heartbeat, Version (2, 0x0525 is V5.25), CFMMSG, HBCOUNT, Fall (unit 0.5 m). values.gnssPositionReportInterval = lansitecUint(b, 6, 2) * 5; values.heartbeatInterval = b[8] * 30; - // Version 0x0525 is V5.25. var major = lansitecHex(b, 9, 10); if (major.charAt(0) == '0') { major = major.substring(1); @@ -376,18 +468,28 @@ function lansitecBadgeRawValues(values, b, type) { values.deviceFirmwareVersion = 'V' + major + '.' + lansitecHex(b, 10, 11); values.cfmmsg = b[11]; values.hbcount = b[12]; - // Fall detection threshold, unit 0.5 m, 0 means off. values.fallDetectionThreshold = b[13] * 0.5; } else if (type == 2 && b.length >= 6) { - // 5.2: the SNR field is there since firmware 0x0501. The SNREN bit (Type bits 3-0) is not used: the - // datasheet example 20 64 3E 033E 00 60 00 has SNREN 0 and the SNR 8.3 dB. - values.loraSnr = lansitecSnr(b, 3); - // VIBSTATE: moving state 0-9, 0 means no moving, 9 high acceleration. The codec turns only 0 and 1 into text. - values.movingState = b[5] & 0x0F; + // 5.2: SNR (2) since firmware 0x0501, decided by the length (SNREN is not reliable). Then GNSSSTAT and + // VIBSTATE (moving state 0-9), CHGSTAT. The codec always reads the SNR. + var state = 3; + if (b.length >= 8) { + values.loraSnr = lansitecSnr(b, 3); + state = 5; + } else { + delete values.gnssState; + delete values.chargeState; + if (LANSITEC_BADGE_GNSS_STATES[(b[3] >> 4) & 0x0F]) { + values.gnssState = LANSITEC_BADGE_GNSS_STATES[(b[3] >> 4) & 0x0F]; + } + if (LANSITEC_BADGE_CHARGE_STATES[(b[4] >> 4) & 0x0F]) { + values.chargeState = LANSITEC_BADGE_CHARGE_STATES[(b[4] >> 4) & 0x0F]; + } + } + values.movingState = b[state] & 0x0F; } else if (type == 3 && b.length >= 13) { - // 5.3: Flag (Type bits 3-0) 0x1 means GNSS positioning failed. Longitude and latitude are IEEE 754 floats - // (the codec divides the mantissa by 0x7fffff instead of 2^23), time is UTC seconds (the codec adds 8 hours). - // An exponent of 0xFF is not a number (NaN or infinity): no position, as in the TBEL converter. + // 5.3: Flag 0x1 is a failed fix. IEEE 754 floats (the codec divides by 0x7fffff), UTC seconds (the codec + // adds 8 hours). No position from NaN, as in the TBEL converter. if ((b[0] & 0x0F) == 0 && lansitecFloatIsNumber(b, 1) && lansitecFloatIsNumber(b, 5)) { values.longitude = lansitecFloat(b, 1); values.latitude = lansitecFloat(b, 5); @@ -396,9 +498,7 @@ function lansitecBadgeRawValues(values, b, type) { } } -// Temperature Humidity Sensor DataSheet 1.26, 2.2.2: VER (Type bits 3-0) 1 means the SNR field is there, 0 means -// it is not and the message is 8 bytes. The codec always reads the SNR, so with VER 0 the next fields are shifted. -// TEMP_INT bit 7 is the sign, TEMP_FRA the hundredths. +// Temperature Humidity Sensor 2.2.2: VER 1 has the SNR, VER 0 not (the codec always reads it). TEMP_INT bit 7 is the sign. function lansitecTemperatureHumidityRawValues(values, b) { var offset = (b[0] & 0x0F) == 1 ? 5 : 3; if (b.length < offset + 3) { @@ -415,19 +515,7 @@ function lansitecTemperatureHumidityRawValues(values, b) { values.humidity = b[offset + 2]; } -// Plug Bluetooth Gateway DataSheet 1.5 (990-01203), the SocketSync gateway. Its codec is the compact gateway's, but -// the messages are not: the codec gives nothing for the Registration (0x4) and the Beacon message (0x6), so The -// Things Stack sends them without decoded_payload, and it reads the Heartbeat and the Alarm Beacon List (0xE) with -// the compact gateway's layout. So every message is decoded from the raw bytes, with or without decoded_payload. -var LANSITEC_SOCKET_SYNC = 'socket-sync-bluetooth-gateway'; -// 5.1 Type bits 2-0 (MODE) and SMODE: the LoRa frequency plan. -var LANSITEC_SOCKET_SYNC_MODES = ['', 'AU915', '', 'CN470', 'AS923', 'EU433', 'EU868', 'US915']; -var LANSITEC_SOCKET_SYNC_BANDS = { 1: 'AU915', 4: 'CN470', 8: 'AS923', 16: 'EU433', 32: 'EU868', 64: 'US915' }; -// 5.1 CFG bits 3-1: the gateway type. -var LANSITEC_SOCKET_SYNC_TYPES = ['Outdoor gateway', 'Indoor gateway', 'Plug gateway']; -// Table 7: UUID List. A beacon gives the last byte of its UUID, the other bytes are the same for all of them. -var LANSITEC_SOCKET_SYNC_UUID = '5C38DBDE567C4CCAB1DA40A8AD4656'; - +// Plug Bluetooth Gateway 1.5, the SocketSync gateway. function lansitecSocketSyncValues(b) { var values = {}; if (b.length == 0) { @@ -435,52 +523,30 @@ function lansitecSocketSyncValues(b) { } var type = (b[0] >> 4) & 0x0F; if (type == 0x4 && b.length >= 10) { - // 5.1 Registration: Type (ADR, MODE), SMODE, POWER, CFG, POS (2), BLE Receiving Duration, HB, Beacon_QTY, CRC (2). - values.adr = (b[0] & 0x08) != 0 ? 'ON' : 'OFF'; - if (LANSITEC_SOCKET_SYNC_MODES[b[0] & 0x07] != '') { - values.mode = LANSITEC_SOCKET_SYNC_MODES[b[0] & 0x07]; - } - if (LANSITEC_SOCKET_SYNC_BANDS[b[1]] != null) { - values.loraWanFrequencyBand = LANSITEC_SOCKET_SYNC_BANDS[b[1]]; - } - values.loraTxPower = (b[2] >> 3) & 0x1F; + // 5.1 Registration: ..., BLE Receiving Duration, HB, Beacon_QTY (half of the limit), CRC (2). + lansitecRegister(values, b, 7); values.continuousBleReceiving = (b[2] & 0x02) != 0 ? 'Continuous Bluetooth receiving is enabled' : 'Continuous Bluetooth receiving is disabled'; values.powerDownFlag = (b[2] & 0x01) != 0 ? 'Automatic shutdown status' : 'Normal status'; - values.dataRate = 'DR' + ((b[3] >> 4) & 0x0F); if (LANSITEC_SOCKET_SYNC_TYPES[(b[3] >> 1) & 0x07] != null) { values.gatewayType = LANSITEC_SOCKET_SYNC_TYPES[(b[3] >> 1) & 0x07]; } values.rssiSortingMethod = (b[3] & 0x01) != 0 ? 'Sort by the maximum value' : 'Sort by average'; - // POS unit 5 s, 0 means off. HB unit 30 s. Beacon_QTY is half of the limit. - values.positionReportInterval = lansitecUint(b, 4, 2) * 5; values.bluetoothReceivingDuration = b[6]; - values.heartbeatInterval = b[7] * 30; values.beaconQuantityLimit = b[8] * 2; } else if (type == 0x2 && b.length >= 7) { - // 5.2 Heartbeat: Type, VOL (battery level, %), RSSI (unit -1 dBm), SNR (2, signed, 0.01 dB), REV (firmware, - // hex), CHGSTAT, CRC (2). The codec reads REV and CHGSTAT as the compact gateway's 2-byte version, and the - // first CRC byte as CHGSTAT. VOL keeps its name: the dashboard of the IoT Hub package reads it. + // 5.2 Heartbeat. VOL (%) keeps its name: the dashboard of the IoT Hub package reads it. values.vol = b[1]; - values.loraRssi = -b[2]; - values.loraSnr = lansitecSnr(b, 3); - values.deviceFirmwareVersion = lansitecHex(b, 5, 6); - if (b[6] == 0x00) { - values.chargeState = 'Not charging'; - } else if (b[6] == 0x50) { - values.chargeState = 'Charging'; - } else if (b[6] == 0x60) { - values.chargeState = 'Charging completed'; - } + lansitecHeartbeat(values, b, 0x50, 0x60); } else if (type == 0x6 || type == 0xE) { - // 5.3 Beacon and 5.4 Alarm Beacon List: Type (bits 3-0: number of beacons), then UUID tail number, Major (2), - // Minor (2) and RSSI (value - 256, dBm) of each beacon. The alarm list holds the beacons that trigger the alarm. + // 5.3 Beacon, 5.4 Alarm Beacon List: Type (bits 3-0: number of beacons), then UUID tail, Major (2), Minor (2), + // RSSI (value - 256) of each. Table 7: the UUID prefix ends with 56 for tails 0x56-0xFF, 57 for 0x00-0x1E. var count = b[0] & 0x0F; if (count > 0 && b.length >= 1 + count * 6) { var list = []; for (var i = 0; i < count; i++) { var start = 1 + i * 6; list.push({ - uuid: LANSITEC_SOCKET_SYNC_UUID + lansitecHex(b, start, start + 1), + uuid: '5C38DBDE567C4CCAB1DA40A8AD465' + (b[start] < 0x56 ? '7' : '6') + lansitecHex(b, start, start + 1), major: lansitecUint(b, start + 1, 2), minor: lansitecUint(b, start + 3, 2), rssi: b[start + 5] - 256 @@ -489,31 +555,12 @@ function lansitecSocketSyncValues(b) { values[type == 0x6 ? 'beacons' : 'alarmBeacons'] = list; } } else if (type == 0xF && b.length >= 2) { - // 5.5 Acknowledgment: Type bits 3-0 the result, then the MSGID of the downlink. - values.result = (b[0] & 0x0F) == 0 ? 'Success' : 'Failure'; - values.msgid = b[1]; + lansitecAcknowledgment(values, b); } return values; } -// LoRaWAN Bluetooth Badge DataSheet 2.78 (990-01242), the contact tracing badge: it reports the badges and the -// Bluetooth beacons near it, it has no GNSS. Its codec is the Bluetooth gateway's, which reads these messages with -// the gateway layout, so every message is decoded from the raw bytes, as for the SocketSync gateway. -// Major and Minor are hex: a badge's are the last 4 bytes of its DevEUI (4.4). -var LANSITEC_CONTACT_BADGE = 'contact-tracing-badge'; -// 4.5.1 Type bits 2-0 (Mode) and SMODE: the LoRa frequency plan. -var LANSITEC_CONTACT_BADGE_MODES = ['', 'AU915', 'IN865', 'CN470', 'AS923', 'EU433', 'EU868', 'US915']; -var LANSITEC_CONTACT_BADGE_BANDS = { 1: 'AU915', 2: 'IN865', 4: 'CN470', 8: 'AS923', 16: 'EU433', 32: 'EU868', 64: 'US915' }; -// 4.5.1 BT bits 10-8 and 4.5.2 BTSTATUS bits 6-4: the Bluetooth transmitting power, dBm. 0 is not a power. -var LANSITEC_CONTACT_BADGE_BT_POWERS = [0, 4, 0, -4, -8, -12, -16, -20]; -// 4.5.2 BTSTATUS bits 11-8: the vibrator and buzzer. -var LANSITEC_CONTACT_BADGE_BUZZER = ['No action', 'Vibrator disabled, buzzer muted (restore)', - 'Vibrator disabled, buzzer half sound level', 'Vibrator disabled, buzzer full sound level', - 'Vibrator enabled, buzzer muted', 'Vibrator enabled, buzzer half sound level', - 'Vibrator enabled, buzzer full sound level', 'Vibrator disabled, buzzer muted (no restore)', - 'Beacon-in-tracker and proximity detection disabled']; - -// 4.5.5 TimeMark: bit n set means seen n * 10 to n * 10 + 9 seconds before the message. Seen time is 10 s per bit. +// Bluetooth Badge 4.5.5 TimeMark: bit n set means seen n * 10 to n * 10 + 9 s before the message, 10 s per bit. function lansitecContactBadgeSeen(item, mark) { item.timeMark = mark; var bits = 0; @@ -524,6 +571,7 @@ function lansitecContactBadgeSeen(item, mark) { return item; } +// LoRaWAN Bluetooth Badge 2.78, the contact tracing badge. Major and Minor are hex (a badge's: the end of its DevEUI). function lansitecContactBadgeValues(b) { var values = {}; if (b.length == 0) { @@ -532,47 +580,25 @@ function lansitecContactBadgeValues(b) { var type = (b[0] >> 4) & 0x0F; var i, start; if (type == 0x1 && b.length >= 9) { - // 4.5.1 Register: Type (ADR, Mode), SMODE, Power, CFG, POS (2), HB, BT (2), CRC (2). - values.adr = (b[0] & 0x08) != 0 ? 'ON' : 'OFF'; - if (LANSITEC_CONTACT_BADGE_MODES[b[0] & 0x07] != '') { - values.mode = LANSITEC_CONTACT_BADGE_MODES[b[0] & 0x07]; - } - if (LANSITEC_CONTACT_BADGE_BANDS[b[1]] != null) { - values.loraWanFrequencyBand = LANSITEC_CONTACT_BADGE_BANDS[b[1]]; - } - values.loraTxPower = (b[2] >> 3) & 0x1F; - values.dataRate = 'DR' + ((b[3] >> 4) & 0x0F); - // POS unit 5 s, 0 means off. HB unit 30 s. - values.positionReportInterval = lansitecUint(b, 4, 2) * 5; - values.heartbeatInterval = b[6] * 30; + // 4.5.1 Register: ..., HB, BT (2), CRC (2). The receiving threshold setting has no unit in the datasheet. + lansitecRegister(values, b, 6); var bt = lansitecUint(b, 7, 2); values.buzzerAndVibrator = ((bt >> 11) & 1) == 1 ? 'ON' : 'OFF'; if (((bt >> 8) & 0x07) > 0) { values.bluetoothTxPower = LANSITEC_CONTACT_BADGE_BT_POWERS[(bt >> 8) & 0x07]; } values.proximityDetection = ((bt >> 7) & 1) == 1 ? 'ON' : 'OFF'; - // 0x0-57, the datasheet gives no unit. values.bluetoothReceivingThresholdSetting = bt & 0x7F; } else if (type == 0x2 && b.length >= 7) { - // 4.5.2 Heartbeat: Type, VOL (battery level, %), RSSI (unit -1 dBm), SNR (2, signed, 0.01 dB), REV (software - // revision), CHGSTAT, CRC (2), BTSTATUS (2), then the friend badges. + // 4.5.2 Heartbeat: ..., CRC (2), BTSTATUS (2), then the friend badges. values.batteryLevel = b[1]; - values.loraRssi = -b[2]; - values.loraSnr = lansitecSnr(b, 3); - values.deviceFirmwareVersion = lansitecHex(b, 5, 6); - if (b[6] == 0x0) { - values.chargeState = 'Not charging'; - } else if (b[6] == 0x5) { - values.chargeState = 'Charging'; - } else if (b[6] == 0x6) { - values.chargeState = 'Charging completed'; - } + lansitecHeartbeat(values, b, 0x5, 0x6); if (b.length >= 11) { var status = lansitecUint(b, 9, 2); if (LANSITEC_CONTACT_BADGE_BUZZER[(status >> 8) & 0x0F] != null) { values.vibratorBuzzerStatus = LANSITEC_CONTACT_BADGE_BUZZER[(status >> 8) & 0x0F]; } - // 0 means not changed. The receiving threshold table is for range bit 7 = 0: -73 to -87 dBm. + // 0 means not changed. The threshold is for range bit 7 = 0: -73 to -87 dBm. if (((status >> 4) & 0x07) > 0) { values.bluetoothTxPower = LANSITEC_CONTACT_BADGE_BT_POWERS[(status >> 4) & 0x07]; } @@ -580,7 +606,7 @@ function lansitecContactBadgeValues(b) { values.bluetoothReceivingThreshold = -72 - (status & 0x0F); } } - // Friend field: Type 0xC with the number of friend badges (1-6), then Major (2) and Minor (2) of each. + // Friend field: Type 0xC with the number of friend badges, then Major (2) and Minor (2) of each. if (b.length >= 12 && ((b[11] >> 4) & 0x0F) == 0xC && b.length >= 12 + (b[11] & 0x0F) * 4) { var friends = []; for (i = 0; i < (b[11] & 0x0F); i++) { @@ -590,9 +616,8 @@ function lansitecContactBadgeValues(b) { values.friends = friends; } } else if (type == 0x8) { - // 4.5.3 10s Instant Report: Type (bits 3-0: number of beacons M), Major (2), Minor (2) and RSSI of each, then - // TOFF: (M + 1) / 2 bytes, 4 bits per beacon, the first beacon in the high bits: seconds before the message. - // RSSI bit 7: 1 a Bluetooth beacon, 0 a badge (beacon-in-tracker); bits 6-0 - 128 is the RSSI, dBm. + // 4.5.3 10s Instant Report: Type (bits 3-0: M beacons), Major (2), Minor (2), RSSI of each, then TOFF: + // (M + 1) / 2 bytes, 4 bits per beacon, high bits first. RSSI bit 7: 1 a beacon, 0 a badge; bits 6-0 - 128. var count = b[0] & 0x0F; if (count > 0 && b.length >= 1 + count * 5) { var hasOffsets = b.length >= 1 + count * 5 + Math.floor((count + 1) / 2); @@ -614,8 +639,8 @@ function lansitecContactBadgeValues(b) { values.beacons = list; } } else if (type == 0xC) { - // 4.5.5 80s Periodic Report: Type (bits 3-0: number of badges and beacons), badges as Major (2), Minor (2), - // TimeMark, then the marker 0xFFFF and beacons as Minor (2), TimeMark. A beacon's Major is always FFFF. + // 4.5.5 80s Periodic Report: badges as Major (2), Minor (2), TimeMark, then 0xFFFF and beacons as Minor (2), + // TimeMark (a beacon's Major is FFFF). var badges = []; var periodBeacons = []; var pos = 1; @@ -639,72 +664,9 @@ function lansitecContactBadgeValues(b) { values.beacons = periodBeacons; } } else if (type == 0xF && b.length >= 2) { - // 4.5.4 Acknowledge: Type bits 3-0 the result, then the MSGID of the downlink. - values.result = (b[0] & 0x0F) == 0 ? 'Success' : 'Failure'; - values.msgid = b[1]; + lansitecAcknowledgment(values, b); } return values; } -var values = {}; -var attributes = {}; -var history = []; -if (metadata.brandId == 'lansitec' && (metadata.modelId == LANSITEC_SOCKET_SYNC || metadata.modelId == LANSITEC_CONTACT_BADGE) - && metadata.data != null) { - // The SocketSync gateway and the contact tracing badge: decoded from the raw bytes only, see - // lansitecSocketSyncValues and lansitecContactBadgeValues. - var lansitecBytes = lansitecBase64ToBytes(metadata.data); - var socketSync = metadata.modelId == LANSITEC_SOCKET_SYNC; - values = socketSync ? lansitecSocketSyncValues(lansitecBytes) : lansitecContactBadgeValues(lansitecBytes); - // Registration holds the device settings: attributes, not telemetry. - if (lansitecBytes.length > 0 && ((lansitecBytes[0] >> 4) & 0x0F) == (socketSync ? 0x4 : 0x1)) { - attributes = values; - values = {}; - } -} else if (metadata.payloadFormat == 'JSON') { - values = decodeToJson(payload); - if (metadata.brandId == 'lansitec' && values.bytes != null && typeof values.bytes === 'object' && !Array.isArray(values.bytes)) { - // The Lansitec codecs put everything in "bytes" and get some fields wrong: see the Lansitec block above. - var raw = values.bytes; - values = lansitecValues(raw, metadata.modelId); - if (metadata.data != null) { - lansitecRawValues(values, lansitecBase64ToBytes(metadata.data), metadata.modelId); - } - // Register is sent after join and holds the device settings: they are attributes, not telemetry. - var messageType = raw.type != null ? raw.type : raw['Message type']; - if (messageType == 'Register') { - attributes = values; - values = {}; - } - } else if (Array.isArray(values.history)) { - values.history.forEach(function (record) { - if (record.timestamp != null) { - var ts = record.timestamp * 1000; - delete record.timestamp; - history.push({ ts: ts, values: record }); - } - }); - delete values.history; - } -} -var profile = 'LoRaWAN Device'; -if (metadata.brandId != null && metadata.modelId != null) { - profile = metadata.brandId + '-' + metadata.modelId; - // The profile name chosen at install, kept in the integration metadata - var customProfile = metadata['profile.' + profile]; - if (customProfile != null && customProfile != '') { - profile = customProfile; - } -} -if (Object.keys(attributes).length > 0) { - return { profile: profile, attributes: attributes }; -} -if (history.length == 0) { - return { profile: profile, telemetry: { ts: metadata.ts, values: values } }; -} -history.push({ ts: metadata.ts, values: values }); -return { profile: profile, telemetry: history }; - -function decodeToJson(payload) { - return JSON.parse(String.fromCharCode.apply(String, payload)); -} +return decodeThingsStackUplink(payload, metadata); diff --git a/ui-ngx/src/assets/converters/js-ttn-decoder.raw b/ui-ngx/src/assets/converters/js-ttn-decoder.raw index 0723f09d7c..1a0aa27613 100644 --- a/ui-ngx/src/assets/converters/js-ttn-decoder.raw +++ b/ui-ngx/src/assets/converters/js-ttn-decoder.raw @@ -2,38 +2,70 @@ // is then that JSON: its keys become telemetry. A Milesight-style history list keeps each record's time. // Such a device gets the profile -, so all devices of one model share it, // or the name the integration metadata gives it under profile.-. Any other device gets LoRaWAN Device. +// Lansitec devices are decoded by the Lansitec block below. +function decodeThingsStackUplink(payload, metadata) { + var values = {}; + var attributes = {}; + var history = []; + var lansitec = metadata.brandId == 'lansitec' ? lansitecUplink(payload, metadata) : null; + if (lansitec != null) { + values = lansitec.telemetry; + attributes = lansitec.attributes; + } else if (metadata.payloadFormat == 'JSON') { + values = decodeToJson(payload); + if (Array.isArray(values.history)) { + values.history.forEach(function (record) { + if (record.timestamp != null) { + var ts = record.timestamp * 1000; + delete record.timestamp; + history.push({ ts: ts, values: record }); + } + }); + delete values.history; + } + } + var profile = 'LoRaWAN Device'; + if (metadata.brandId != null && metadata.modelId != null) { + profile = metadata.brandId + '-' + metadata.modelId; + // The profile name chosen at install, kept in the integration metadata + var customProfile = metadata['profile.' + profile]; + if (customProfile != null && customProfile != '') { + profile = customProfile; + } + } + if (Object.keys(attributes).length > 0) { + return { profile: profile, attributes: attributes }; + } + if (history.length == 0) { + return { profile: profile, telemetry: { ts: metadata.ts, values: values } }; + } + history.push({ ts: metadata.ts, values: values }); + return { profile: profile, telemetry: history }; +} -// Lansitec: the LoRaWAN Device Repository codecs wrap everything in "bytes", write numbers as strings with units -// ("85%", "-67dBm") and get some fields wrong. Key names follow the Lansitec datasheets where there are any: -// Bluetooth Gateway DataSheet 2.27, Badge Tracker DataSheet v5.30, Temperature Humidity Sensor DataSheet 1.26, -// Plug Bluetooth Gateway DataSheet 1.5 (SocketSync gateway), -// LoRaWAN Bluetooth Badge DataSheet 2.78 (contact tracing badge). -// The wrong fields are decoded from the raw bytes (metadata.data). Remove a fix when the Repository fixes the codec. -var LANSITEC_UNITS = ['dBm', 'dB', '%', 'V', 'sec', 'min', 's', '℃', 'level', ' meter']; +function decodeToJson(payload) { + return JSON.parse(String.fromCharCode.apply(String, payload)); +} +// --------------------------------------------------------------------------------------------------------------------- +// Lansitec. The Repository codecs wrap everything in "bytes", write numbers with units ("85%", "-67dBm") and get some +// fields wrong: those are decoded from the raw bytes (metadata.data) by the datasheets. Remove a fix when the codec is fixed. +// Datasheets: Bluetooth Gateway 2.27, Badge Tracker v5.30, Temperature Humidity Sensor 1.26, Plug Bluetooth Gateway 1.5 +// (SocketSync), LoRaWAN Bluetooth Badge 2.78 (contact tracing badge), Container Tracker 0.72, Helmet Tracker v0.14. +var LANSITEC_UNITS = ['dBm', 'dB', '%', 'V', 'sec', 'min', 's', '℃', 'level', ' meter']; var LANSITEC_GATEWAYS = ['compact-bluetooth-gateway', 'indoor-bluetooth-gateway', 'macro-bluetooth-gateway', 'solar-bluetooth-gateway']; +var LANSITEC_SOCKET_SYNC = 'socket-sync-bluetooth-gateway'; +var LANSITEC_CONTACT_BADGE = 'contact-tracing-badge'; -// Codec key -> datasheet name, per model group. '-' drops the key. -// -// Bluetooth Gateway DataSheet 2.27, 4.4.1 Registration: ADR, Mode (current working LoRaWAN frequency sub-band plan), -// LoRaWAN frequency band, Power (LoRa TX power, dBm), Continuously BLE receiving, DR (LoRaWAN Uplink Data Rate), -// PRI (Position report interval), HB (Heartbeat interval), Bluetooth receiving duration. -// 4.4.2 Heartbeat: RSSI and SNR (LoRa), VER (Firmware version). VOL, SNR and CHGSTAT come from the raw bytes. -// number is the count of reported devices: the beacons list has them. The beacons come from the raw bytes too. +// Codec key -> datasheet name. '-' drops the key: it is decoded from the raw bytes or is not a value. +// Bluetooth Gateway 4.4.1 Registration, 4.4.2 Heartbeat. var LANSITEC_GATEWAY_KEYS = { adr: 'adr', mode: 'mode', LoRaWANband: 'loraWanFrequencyBand', LoRaTxpower: 'loraTxPower', ContinuousReceive: 'continuousBleReceiving', dr: 'dataRate', pos: 'positionReportInterval', hb: 'heartbeatInterval', BLEReceivingDuration: 'bluetoothReceivingDuration', - rssi: 'loraRssi', snr: '-', ver: 'deviceFirmwareVersion', vol: '-', chgstate: '-', number: '-' + rssi: 'loraRssi', snr: '-', ver: '-', vol: '-', chgstate: '-', number: '-' }; - -// Badge Tracker DataSheet v5.30, 5.1 Registration: ADR, LoRa TX power, DR, GNSSEN (GNSS enable status), -// POSMODE (Positioning report mode), BLEPRI (Bluetooth Position Report Interval). The codec reads it without the -// GNSSPRI field, so heartbeat, version, CFMMSG, HBCOUNT and fall detection come from the raw bytes. -// 5.2 Heartbeat: Battery (percentage), RSSI (LoRa), GNSSSTATE (Status of the GNSS module), CHGSTATE (Status of -// battery charging). SNR and VIBSTATE come from the raw bytes. -// 5.3 GNSS Position: longitude, latitude and time come from the raw bytes. -// 5.4 Beacon: bytes 1-5 are RFU, the codec reads byte 1 as Vibstate. 5.5 Alarm: 0x01 SOS, 0x02 Fall Detection. +// Badge Tracker 5.1 Registration (the codec skips GNSSPRI), 5.2 Heartbeat, 5.3 GNSS Position, 5.4 Beacon, 5.5 Alarm. var LANSITEC_BADGE_KEYS = { ADR: 'adr', 'Lora Txpower': 'loraTxPower', DR: 'dataRate', GNSSEN: 'gnssEnabled', 'Position mode': 'positioningReportMode', 'Position period': 'bluetoothPositionReportInterval', @@ -42,31 +74,65 @@ var LANSITEC_BADGE_KEYS = { 'Move state': '-', 'Charge state': 'chargeState', Longitude: '-', Latitude: '-', Time: '-', Length: '-', Vibstate: '-', 'Alarm type': 'alarm' }; - -// Temperature Humidity Sensor DataSheet 1.26, 2.2.1 Register: ADR, MODE (current working scheme), SMODE (data -// scheme supported), POWER (configured transmit power, dBm), DR, REPTIND (report H&T if the value is the same; -// the codec gives "false" for 0 = always report), TH (period of temperature and humidity report), CRC. -// 2.2.2 Periodical temperature and humidity: VER is the version of the message type, not the firmware; -// VOL (battery left, %), RSSI (downlink, detected by the sensor). SNR, temperature and humidity come from the raw bytes. +// Temperature Humidity Sensor 2.2.1 Register, 2.2.2 Periodical data (VER is the message version, not the firmware). var LANSITEC_TEMPERATURE_HUMIDITY_KEYS = { adr: 'adr', mode: 'mode', smode: 'smode', LoraTxpower: 'loraTxPower', dr: 'dataRate', repting: 'reptind', th: 'temperatureHumidityReportPeriod', crc: '-', ver: '-', vol: 'batteryLevel', rssi: 'loraRssi', snr: '-', temperature: '-', humidity: '-' }; - -// Without a datasheet only these keys are renamed: the integration writes rssi, snr and firmwareVersion itself. -// chgstate: the tracking label has the gateway codec byte for byte, which reads CHGSTAT 0x50 as "Not charging" -// (Bluetooth Gateway DataSheet 2.27, 4.4.2), so it is dropped. number is the count -// of reported devices: the beacons list has them. +// The other models: the integration writes rssi, snr and firmwareVersion itself. chgstate: the tracking label has the +// gateway codec, which misreads CHGSTAT (Bluetooth Gateway 4.4.2). number: the beacons list has them. var LANSITEC_CLASH_KEYS = { rssi: 'loraRssi', RSSI: 'loraRssi', snr: 'loraSnr', SNR: 'loraSnr', firmwareVersion: 'deviceFirmwareVersion', 'Firmware version': 'deviceFirmwareVersion', type: '-', 'Message type': '-', chgstate: '-', number: '-' }; - // Beacon keys: Devicetype1data1, Device1data, dev1major, dev1.major, pos1minor, asset1rssi... var LANSITEC_BEACON_PREFIXES = ['devicetype1', 'devicetype2', 'devicetype3', 'device', 'dev', 'pos', 'asset']; var LANSITEC_BEACON_FIELDS = ['data', 'major', 'minor', 'rssi', 'id']; +// Register Type bits 2-0 and SMODE: the LoRa frequency plan (Plug Bluetooth Gateway 5.1, Bluetooth Badge 4.5.1). +var LANSITEC_MODES = ['', 'AU915', 'IN865', 'CN470', 'AS923', 'EU433', 'EU868', 'US915']; +var LANSITEC_SMODES = [1, 2, 4, 8, 16, 32, 64]; +var LANSITEC_BANDS = ['AU915', 'IN865', 'CN470', 'AS923', 'EU433', 'EU868', 'US915']; +// Badge Tracker 5.2 Heartbeat, as the codec writes them. +var LANSITEC_BADGE_GNSS_STATES = ['GPS OFF', 'GPS boot', 'GPS locating', 'GPS located', '', '', '', '', '', 'GPS no signal']; +var LANSITEC_BADGE_CHARGE_STATES = ['Power cable disconnected', '', '', '', '', 'Charging', 'Charge complete']; +// Plug Bluetooth Gateway 5.1 CFG bits 3-1. +var LANSITEC_SOCKET_SYNC_TYPES = ['Outdoor gateway', 'Indoor gateway', 'Plug gateway']; +// Bluetooth Badge 4.5.1 BT bits 10-8, 4.5.2 BTSTATUS bits 6-4 (dBm, 0 is not a power) and bits 11-8. +var LANSITEC_CONTACT_BADGE_BT_POWERS = [0, 4, 0, -4, -8, -12, -16, -20]; +var LANSITEC_CONTACT_BADGE_BUZZER = ['No action', 'Vibrator disabled, buzzer muted (restore)', + 'Vibrator disabled, buzzer half sound level', 'Vibrator disabled, buzzer full sound level', + 'Vibrator enabled, buzzer muted', 'Vibrator enabled, buzzer half sound level', + 'Vibrator enabled, buzzer full sound level', 'Vibrator disabled, buzzer muted (no restore)', + 'Beacon-in-tracker and proximity detection disabled']; + +// { telemetry, attributes }, or null when the uplink is not one the Lansitec block decodes. +function lansitecUplink(payload, metadata) { + var model = metadata.modelId; + var values = null; + var register = false; + if ((model == LANSITEC_SOCKET_SYNC || model == LANSITEC_CONTACT_BADGE) && metadata.data != null) { + // Their codecs are the gateway's and misread every message: decoded from the raw bytes only. + var b = lansitecBase64ToBytes(metadata.data); + values = model == LANSITEC_SOCKET_SYNC ? lansitecSocketSyncValues(b) : lansitecContactBadgeValues(b); + register = b.length > 0 && ((b[0] >> 4) & 0x0F) == (model == LANSITEC_SOCKET_SYNC ? 0x4 : 0x1); + } else if (metadata.payloadFormat == 'JSON') { + var raw = decodeToJson(payload).bytes; + if (raw == null || typeof raw !== 'object' || Array.isArray(raw)) { + return null; + } + values = lansitecValues(raw, model); + if (metadata.data != null) { + lansitecRawValues(values, lansitecBase64ToBytes(metadata.data), model); + } + register = (raw.type != null ? raw.type : raw['Message type']) == 'Register'; + } else { + return null; + } + // Register holds the device settings: attributes, not telemetry. + return register ? { telemetry: {}, attributes: values } : { telemetry: values, attributes: {} }; +} function lansitecEndsWith(s, end) { return s.length >= end.length && s.substring(s.length - end.length) == end; @@ -163,10 +229,12 @@ function lansitecValues(raw, model) { } else if (b.field == 'data' || b.field == 'major' || b.field == 'minor') { beacons[id][b.field] = value; } - } else if (table == null && key == 'time' && typeof value == 'string') { - // Container tracker and helmet sensor, no datasheet: their codecs add 8 hours to the GNSS time and wrap - // it in quotes, as the badge tracker codec does with the UTC time of DataSheet v5.30, 5.3. - values.gnssTime = new Date(value.replace(/"/g, '')).getTime() - 8 * 60 * 60 * 1000; + } else if (table == null && /^time[1-8]?$/.test(key) && typeof value == 'string') { + // Container Tracker 5.3 and 5.7, Helmet Tracker 6.3: UTC time. The codecs add 8 hours and quote it. + values['gnssTime' + key.substring(4)] = new Date(value.replace(/"/g, '')).getTime() - 8 * 60 * 60 * 1000; + } else if (key == 'pressure' && model == 'helmet-sensor' && typeof value == 'number') { + // Helmet Tracker 6.3: the value * 10, unit 0.1 Pa. + values.pressure = value / 10; } else if (table != null && table.hasOwnProperty(key)) { if (table[key] != '-') { values[table[key]] = lansitecNumber(value); @@ -185,36 +253,30 @@ function lansitecValues(raw, model) { return values; } +// In the order of their numbers, which can have gaps (Container Tracker 5.7: dev1, dev3...). function lansitecBeaconList(beacons) { - var list = []; - var ids = Object.keys(beacons); - for (var i = 1; i <= ids.length; i++) { - ids.forEach(function (id) { - var b = beacons[id]; - if (b.order == i) { - var item = {}; - if (b.data != null) { - item.data = b.data; - } - if (b.major != null) { - item.major = b.major; - item.minor = b.minor; - } - item.rssi = b.rssi; - list.push(item); - } - }); - } - // The gateway codec reads the data of every device after the first at a 5-byte step, so it is right only - // for 4-byte data (DataSheet 4.4.4 examples): with longer data only the first device is kept. The gateways - // with a datasheet get their beacons from the raw bytes instead; this is for the models with the gateway codec. + var list = Object.keys(beacons).sort(function (a, b) { + return beacons[a].order - beacons[b].order; + }).map(function (id) { + var b = beacons[id]; + var item = {}; + if (b.data != null) { + item.data = b.data; + } + if (b.major != null) { + item.major = b.major; + item.minor = b.minor; + } + item.rssi = b.rssi; + return item; + }); + // The gateway codec reads every device after the first at a 5-byte step: right only for 4-byte data. if (list.length > 1 && list[0].data != null && list[0].data.length != 8) { return [list[0]]; } return list; } -// Decoded from the raw bytes by the datasheet where the codec gets it wrong. function lansitecRawValues(values, b, model) { if (b.length == 0) { return; @@ -281,7 +343,7 @@ function lansitecFloat(b, offset) { return bits >= 2147483648 ? -value : value; } -// SNR, unit 0.01 dB, -3000 to 3000: signed. The codecs read it unsigned. +// SNR, unit 0.01 dB, signed. The codecs read it unsigned. function lansitecSnr(b, offset) { var v = lansitecUint(b, offset, 2); if (v > 32767) { @@ -290,30 +352,67 @@ function lansitecSnr(b, offset) { return v / 100; } -// Bluetooth Gateway DataSheet 2.27. +// CHGSTAT: 0 Not charging, then the codes of Charging and Charging completed. +function lansitecChargeState(values, charge, charging, completed) { + if (charge == 0) { + values.chargeState = 'Not charging'; + } else if (charge == charging) { + values.chargeState = 'Charging'; + } else if (charge == completed) { + values.chargeState = 'Charging completed'; + } +} + +// Register of the SocketSync gateway (5.1) and the contact tracing badge (4.5.1): Type (ADR, MODE), SMODE, POWER, +// CFG, POS (2, unit 5 s), then HB (unit 30 s) at hbOffset. +function lansitecRegister(values, b, hbOffset) { + values.adr = (b[0] & 0x08) != 0 ? 'ON' : 'OFF'; + if (LANSITEC_MODES[b[0] & 0x07] != '') { + values.mode = LANSITEC_MODES[b[0] & 0x07]; + } + if (LANSITEC_SMODES.indexOf(b[1]) >= 0) { + values.loraWanFrequencyBand = LANSITEC_BANDS[LANSITEC_SMODES.indexOf(b[1])]; + } + values.loraTxPower = (b[2] >> 3) & 0x1F; + values.dataRate = 'DR' + ((b[3] >> 4) & 0x0F); + values.positionReportInterval = lansitecUint(b, 4, 2) * 5; + values.heartbeatInterval = b[hbOffset] * 30; +} + +// Heartbeat of the SocketSync gateway (5.2) and the contact tracing badge (4.5.2): Type, VOL, RSSI (unit -1 dBm), +// SNR (2), REV, CHGSTAT (0x00, 0x50, 0x60 and 0x0, 0x5, 0x6). +function lansitecHeartbeat(values, b, charging, completed) { + values.loraRssi = -b[2]; + values.loraSnr = lansitecSnr(b, 3); + values.deviceFirmwareVersion = lansitecHex(b, 5, 6); + lansitecChargeState(values, b[6], charging, completed); +} + +// Acknowledgment (Plug Bluetooth Gateway 5.5, Bluetooth Badge 4.5.4): Type bits 3-0 the result, then the MSGID. +function lansitecAcknowledgment(values, b) { + values.result = (b[0] & 0x0F) == 0 ? 'Success' : 'Failure'; + values.msgid = b[1]; +} + +// Bluetooth Gateway 2.27. function lansitecGatewayRawValues(values, b, model, type) { if (type == 1 && b.length >= 9) { - // 4.4.1: Network reconnection interval, unit 5 minutes. The codec does not decode it. + // 4.4.1: Network reconnection interval, unit 5 minutes. values.networkReconnectionInterval = b[8] * 300; } else if (type == 2 && b.length >= 8) { - // 4.4.2: VOL is the battery voltage (VOL x 0.01 + 1.5 V) on the macro gateway and the battery level (%) on - // the others. The codec decides by the value (<= 100 is %), not by the model. + // 4.4.2: VOL is the battery voltage (x 0.01 + 1.5 V) on the macro gateway, the battery level (%) on the others. if (model == 'macro-bluetooth-gateway') { values.batteryVoltage = (b[1] + 150) / 100; } else { values.batteryLevel = b[1]; } values.loraSnr = lansitecSnr(b, 3); - // CHGSTAT: 0x00 Not charging, 0x50 Charging, 0x60 Charging completed on the compact and solar gateway, - // always 0x00 on the macro and indoor gateway. The codec reads the low half of the byte. + // VER 108 is V1.08. + var version = lansitecUint(b, 5, 2); + values.deviceFirmwareVersion = 'V' + Math.floor(version / 100) + '.' + (version % 100 < 10 ? '0' : '') + version % 100; + // CHGSTAT on the compact and solar gateway only. if (model == 'compact-bluetooth-gateway' || model == 'solar-bluetooth-gateway') { - if (b[7] == 0x00) { - values.chargeState = 'Not charging'; - } else if (b[7] == 0x50) { - values.chargeState = 'Charging'; - } else if (b[7] == 0x60) { - values.chargeState = 'Charging completed'; - } + lansitecChargeState(values, b[7], 0x50, 0x60); } } else if (type == 0x8 || type == 0x9 || type == 0xA || type == 0xE) { delete values.beacons; @@ -324,12 +423,8 @@ function lansitecGatewayRawValues(values, b, model, type) { } } -// 4.4.4-4.4.6: Device Type 1, 2 and 3 messages: Type (bits 3-0: number of devices), then Data (1-31 bytes) and -// RSSI (value - 256, dBm) of each device. Data length is not in the message: it is set by the device report rule -// of the type (4.4.3), so all devices of one message have the same length. The codec reads every device after -// the first at a 5-byte step. -// 4.4.7: Multi-Device Type message: Device Type ID before the data of each device. The data length can differ per -// type, and the message does not say it: decoded only when all devices fit the same length. +// 4.4.4-4.4.6: Type (bits 3-0: number of devices), Data and RSSI (value - 256) of each device; one data length per +// message (4.4.3). 4.4.7 Multi-Device: a Device Type ID before each device, decoded when all have the same length. function lansitecGatewayBeacons(b, type) { var count = b[0] & 0x0F; if (count == 0 || (b.length - 1) % count != 0) { @@ -360,15 +455,12 @@ function lansitecGatewayBeacons(b, type) { return list; } -// Badge Tracker DataSheet v5.30. +// Badge Tracker v5.30. function lansitecBadgeRawValues(values, b, type) { if (type == 1 && b.length == 14) { - // 5.1, 14 bytes: Type, SMODE, Power, Config, BLEPRI (2), GNSSPRI (2), Heartbeat, Version (2), CFMMSG, HBCOUNT, Fall. - // BLEEN (Config bit 0) is not decoded: the field table says BLE enable status and that GNSSEN or BLEEN must - // be enabled, but the example (Config 32) has both 0 and calls bit 0 reserved. + // 5.1: GNSSPRI (2), Heartbeat, Version (2, 0x0525 is V5.25), CFMMSG, HBCOUNT, Fall (unit 0.5 m). values.gnssPositionReportInterval = lansitecUint(b, 6, 2) * 5; values.heartbeatInterval = b[8] * 30; - // Version 0x0525 is V5.25. var major = lansitecHex(b, 9, 10); if (major.charAt(0) == '0') { major = major.substring(1); @@ -376,18 +468,28 @@ function lansitecBadgeRawValues(values, b, type) { values.deviceFirmwareVersion = 'V' + major + '.' + lansitecHex(b, 10, 11); values.cfmmsg = b[11]; values.hbcount = b[12]; - // Fall detection threshold, unit 0.5 m, 0 means off. values.fallDetectionThreshold = b[13] * 0.5; } else if (type == 2 && b.length >= 6) { - // 5.2: the SNR field is there since firmware 0x0501. The SNREN bit (Type bits 3-0) is not used: the - // datasheet example 20 64 3E 033E 00 60 00 has SNREN 0 and the SNR 8.3 dB. - values.loraSnr = lansitecSnr(b, 3); - // VIBSTATE: moving state 0-9, 0 means no moving, 9 high acceleration. The codec turns only 0 and 1 into text. - values.movingState = b[5] & 0x0F; + // 5.2: SNR (2) since firmware 0x0501, decided by the length (SNREN is not reliable). Then GNSSSTAT and + // VIBSTATE (moving state 0-9), CHGSTAT. The codec always reads the SNR. + var state = 3; + if (b.length >= 8) { + values.loraSnr = lansitecSnr(b, 3); + state = 5; + } else { + delete values.gnssState; + delete values.chargeState; + if (LANSITEC_BADGE_GNSS_STATES[(b[3] >> 4) & 0x0F]) { + values.gnssState = LANSITEC_BADGE_GNSS_STATES[(b[3] >> 4) & 0x0F]; + } + if (LANSITEC_BADGE_CHARGE_STATES[(b[4] >> 4) & 0x0F]) { + values.chargeState = LANSITEC_BADGE_CHARGE_STATES[(b[4] >> 4) & 0x0F]; + } + } + values.movingState = b[state] & 0x0F; } else if (type == 3 && b.length >= 13) { - // 5.3: Flag (Type bits 3-0) 0x1 means GNSS positioning failed. Longitude and latitude are IEEE 754 floats - // (the codec divides the mantissa by 0x7fffff instead of 2^23), time is UTC seconds (the codec adds 8 hours). - // An exponent of 0xFF is not a number (NaN or infinity): no position, as in the TBEL converter. + // 5.3: Flag 0x1 is a failed fix. IEEE 754 floats (the codec divides by 0x7fffff), UTC seconds (the codec + // adds 8 hours). No position from NaN, as in the TBEL converter. if ((b[0] & 0x0F) == 0 && lansitecFloatIsNumber(b, 1) && lansitecFloatIsNumber(b, 5)) { values.longitude = lansitecFloat(b, 1); values.latitude = lansitecFloat(b, 5); @@ -396,9 +498,7 @@ function lansitecBadgeRawValues(values, b, type) { } } -// Temperature Humidity Sensor DataSheet 1.26, 2.2.2: VER (Type bits 3-0) 1 means the SNR field is there, 0 means -// it is not and the message is 8 bytes. The codec always reads the SNR, so with VER 0 the next fields are shifted. -// TEMP_INT bit 7 is the sign, TEMP_FRA the hundredths. +// Temperature Humidity Sensor 2.2.2: VER 1 has the SNR, VER 0 not (the codec always reads it). TEMP_INT bit 7 is the sign. function lansitecTemperatureHumidityRawValues(values, b) { var offset = (b[0] & 0x0F) == 1 ? 5 : 3; if (b.length < offset + 3) { @@ -415,19 +515,7 @@ function lansitecTemperatureHumidityRawValues(values, b) { values.humidity = b[offset + 2]; } -// Plug Bluetooth Gateway DataSheet 1.5 (990-01203), the SocketSync gateway. Its codec is the compact gateway's, but -// the messages are not: the codec gives nothing for the Registration (0x4) and the Beacon message (0x6), so The -// Things Stack sends them without decoded_payload, and it reads the Heartbeat and the Alarm Beacon List (0xE) with -// the compact gateway's layout. So every message is decoded from the raw bytes, with or without decoded_payload. -var LANSITEC_SOCKET_SYNC = 'socket-sync-bluetooth-gateway'; -// 5.1 Type bits 2-0 (MODE) and SMODE: the LoRa frequency plan. -var LANSITEC_SOCKET_SYNC_MODES = ['', 'AU915', '', 'CN470', 'AS923', 'EU433', 'EU868', 'US915']; -var LANSITEC_SOCKET_SYNC_BANDS = { 1: 'AU915', 4: 'CN470', 8: 'AS923', 16: 'EU433', 32: 'EU868', 64: 'US915' }; -// 5.1 CFG bits 3-1: the gateway type. -var LANSITEC_SOCKET_SYNC_TYPES = ['Outdoor gateway', 'Indoor gateway', 'Plug gateway']; -// Table 7: UUID List. A beacon gives the last byte of its UUID, the other bytes are the same for all of them. -var LANSITEC_SOCKET_SYNC_UUID = '5C38DBDE567C4CCAB1DA40A8AD4656'; - +// Plug Bluetooth Gateway 1.5, the SocketSync gateway. function lansitecSocketSyncValues(b) { var values = {}; if (b.length == 0) { @@ -435,52 +523,30 @@ function lansitecSocketSyncValues(b) { } var type = (b[0] >> 4) & 0x0F; if (type == 0x4 && b.length >= 10) { - // 5.1 Registration: Type (ADR, MODE), SMODE, POWER, CFG, POS (2), BLE Receiving Duration, HB, Beacon_QTY, CRC (2). - values.adr = (b[0] & 0x08) != 0 ? 'ON' : 'OFF'; - if (LANSITEC_SOCKET_SYNC_MODES[b[0] & 0x07] != '') { - values.mode = LANSITEC_SOCKET_SYNC_MODES[b[0] & 0x07]; - } - if (LANSITEC_SOCKET_SYNC_BANDS[b[1]] != null) { - values.loraWanFrequencyBand = LANSITEC_SOCKET_SYNC_BANDS[b[1]]; - } - values.loraTxPower = (b[2] >> 3) & 0x1F; + // 5.1 Registration: ..., BLE Receiving Duration, HB, Beacon_QTY (half of the limit), CRC (2). + lansitecRegister(values, b, 7); values.continuousBleReceiving = (b[2] & 0x02) != 0 ? 'Continuous Bluetooth receiving is enabled' : 'Continuous Bluetooth receiving is disabled'; values.powerDownFlag = (b[2] & 0x01) != 0 ? 'Automatic shutdown status' : 'Normal status'; - values.dataRate = 'DR' + ((b[3] >> 4) & 0x0F); if (LANSITEC_SOCKET_SYNC_TYPES[(b[3] >> 1) & 0x07] != null) { values.gatewayType = LANSITEC_SOCKET_SYNC_TYPES[(b[3] >> 1) & 0x07]; } values.rssiSortingMethod = (b[3] & 0x01) != 0 ? 'Sort by the maximum value' : 'Sort by average'; - // POS unit 5 s, 0 means off. HB unit 30 s. Beacon_QTY is half of the limit. - values.positionReportInterval = lansitecUint(b, 4, 2) * 5; values.bluetoothReceivingDuration = b[6]; - values.heartbeatInterval = b[7] * 30; values.beaconQuantityLimit = b[8] * 2; } else if (type == 0x2 && b.length >= 7) { - // 5.2 Heartbeat: Type, VOL (battery level, %), RSSI (unit -1 dBm), SNR (2, signed, 0.01 dB), REV (firmware, - // hex), CHGSTAT, CRC (2). The codec reads REV and CHGSTAT as the compact gateway's 2-byte version, and the - // first CRC byte as CHGSTAT. VOL keeps its name: the dashboard of the IoT Hub package reads it. + // 5.2 Heartbeat. VOL (%) keeps its name: the dashboard of the IoT Hub package reads it. values.vol = b[1]; - values.loraRssi = -b[2]; - values.loraSnr = lansitecSnr(b, 3); - values.deviceFirmwareVersion = lansitecHex(b, 5, 6); - if (b[6] == 0x00) { - values.chargeState = 'Not charging'; - } else if (b[6] == 0x50) { - values.chargeState = 'Charging'; - } else if (b[6] == 0x60) { - values.chargeState = 'Charging completed'; - } + lansitecHeartbeat(values, b, 0x50, 0x60); } else if (type == 0x6 || type == 0xE) { - // 5.3 Beacon and 5.4 Alarm Beacon List: Type (bits 3-0: number of beacons), then UUID tail number, Major (2), - // Minor (2) and RSSI (value - 256, dBm) of each beacon. The alarm list holds the beacons that trigger the alarm. + // 5.3 Beacon, 5.4 Alarm Beacon List: Type (bits 3-0: number of beacons), then UUID tail, Major (2), Minor (2), + // RSSI (value - 256) of each. Table 7: the UUID prefix ends with 56 for tails 0x56-0xFF, 57 for 0x00-0x1E. var count = b[0] & 0x0F; if (count > 0 && b.length >= 1 + count * 6) { var list = []; for (var i = 0; i < count; i++) { var start = 1 + i * 6; list.push({ - uuid: LANSITEC_SOCKET_SYNC_UUID + lansitecHex(b, start, start + 1), + uuid: '5C38DBDE567C4CCAB1DA40A8AD465' + (b[start] < 0x56 ? '7' : '6') + lansitecHex(b, start, start + 1), major: lansitecUint(b, start + 1, 2), minor: lansitecUint(b, start + 3, 2), rssi: b[start + 5] - 256 @@ -489,31 +555,12 @@ function lansitecSocketSyncValues(b) { values[type == 0x6 ? 'beacons' : 'alarmBeacons'] = list; } } else if (type == 0xF && b.length >= 2) { - // 5.5 Acknowledgment: Type bits 3-0 the result, then the MSGID of the downlink. - values.result = (b[0] & 0x0F) == 0 ? 'Success' : 'Failure'; - values.msgid = b[1]; + lansitecAcknowledgment(values, b); } return values; } -// LoRaWAN Bluetooth Badge DataSheet 2.78 (990-01242), the contact tracing badge: it reports the badges and the -// Bluetooth beacons near it, it has no GNSS. Its codec is the Bluetooth gateway's, which reads these messages with -// the gateway layout, so every message is decoded from the raw bytes, as for the SocketSync gateway. -// Major and Minor are hex: a badge's are the last 4 bytes of its DevEUI (4.4). -var LANSITEC_CONTACT_BADGE = 'contact-tracing-badge'; -// 4.5.1 Type bits 2-0 (Mode) and SMODE: the LoRa frequency plan. -var LANSITEC_CONTACT_BADGE_MODES = ['', 'AU915', 'IN865', 'CN470', 'AS923', 'EU433', 'EU868', 'US915']; -var LANSITEC_CONTACT_BADGE_BANDS = { 1: 'AU915', 2: 'IN865', 4: 'CN470', 8: 'AS923', 16: 'EU433', 32: 'EU868', 64: 'US915' }; -// 4.5.1 BT bits 10-8 and 4.5.2 BTSTATUS bits 6-4: the Bluetooth transmitting power, dBm. 0 is not a power. -var LANSITEC_CONTACT_BADGE_BT_POWERS = [0, 4, 0, -4, -8, -12, -16, -20]; -// 4.5.2 BTSTATUS bits 11-8: the vibrator and buzzer. -var LANSITEC_CONTACT_BADGE_BUZZER = ['No action', 'Vibrator disabled, buzzer muted (restore)', - 'Vibrator disabled, buzzer half sound level', 'Vibrator disabled, buzzer full sound level', - 'Vibrator enabled, buzzer muted', 'Vibrator enabled, buzzer half sound level', - 'Vibrator enabled, buzzer full sound level', 'Vibrator disabled, buzzer muted (no restore)', - 'Beacon-in-tracker and proximity detection disabled']; - -// 4.5.5 TimeMark: bit n set means seen n * 10 to n * 10 + 9 seconds before the message. Seen time is 10 s per bit. +// Bluetooth Badge 4.5.5 TimeMark: bit n set means seen n * 10 to n * 10 + 9 s before the message, 10 s per bit. function lansitecContactBadgeSeen(item, mark) { item.timeMark = mark; var bits = 0; @@ -524,6 +571,7 @@ function lansitecContactBadgeSeen(item, mark) { return item; } +// LoRaWAN Bluetooth Badge 2.78, the contact tracing badge. Major and Minor are hex (a badge's: the end of its DevEUI). function lansitecContactBadgeValues(b) { var values = {}; if (b.length == 0) { @@ -532,47 +580,25 @@ function lansitecContactBadgeValues(b) { var type = (b[0] >> 4) & 0x0F; var i, start; if (type == 0x1 && b.length >= 9) { - // 4.5.1 Register: Type (ADR, Mode), SMODE, Power, CFG, POS (2), HB, BT (2), CRC (2). - values.adr = (b[0] & 0x08) != 0 ? 'ON' : 'OFF'; - if (LANSITEC_CONTACT_BADGE_MODES[b[0] & 0x07] != '') { - values.mode = LANSITEC_CONTACT_BADGE_MODES[b[0] & 0x07]; - } - if (LANSITEC_CONTACT_BADGE_BANDS[b[1]] != null) { - values.loraWanFrequencyBand = LANSITEC_CONTACT_BADGE_BANDS[b[1]]; - } - values.loraTxPower = (b[2] >> 3) & 0x1F; - values.dataRate = 'DR' + ((b[3] >> 4) & 0x0F); - // POS unit 5 s, 0 means off. HB unit 30 s. - values.positionReportInterval = lansitecUint(b, 4, 2) * 5; - values.heartbeatInterval = b[6] * 30; + // 4.5.1 Register: ..., HB, BT (2), CRC (2). The receiving threshold setting has no unit in the datasheet. + lansitecRegister(values, b, 6); var bt = lansitecUint(b, 7, 2); values.buzzerAndVibrator = ((bt >> 11) & 1) == 1 ? 'ON' : 'OFF'; if (((bt >> 8) & 0x07) > 0) { values.bluetoothTxPower = LANSITEC_CONTACT_BADGE_BT_POWERS[(bt >> 8) & 0x07]; } values.proximityDetection = ((bt >> 7) & 1) == 1 ? 'ON' : 'OFF'; - // 0x0-57, the datasheet gives no unit. values.bluetoothReceivingThresholdSetting = bt & 0x7F; } else if (type == 0x2 && b.length >= 7) { - // 4.5.2 Heartbeat: Type, VOL (battery level, %), RSSI (unit -1 dBm), SNR (2, signed, 0.01 dB), REV (software - // revision), CHGSTAT, CRC (2), BTSTATUS (2), then the friend badges. + // 4.5.2 Heartbeat: ..., CRC (2), BTSTATUS (2), then the friend badges. values.batteryLevel = b[1]; - values.loraRssi = -b[2]; - values.loraSnr = lansitecSnr(b, 3); - values.deviceFirmwareVersion = lansitecHex(b, 5, 6); - if (b[6] == 0x0) { - values.chargeState = 'Not charging'; - } else if (b[6] == 0x5) { - values.chargeState = 'Charging'; - } else if (b[6] == 0x6) { - values.chargeState = 'Charging completed'; - } + lansitecHeartbeat(values, b, 0x5, 0x6); if (b.length >= 11) { var status = lansitecUint(b, 9, 2); if (LANSITEC_CONTACT_BADGE_BUZZER[(status >> 8) & 0x0F] != null) { values.vibratorBuzzerStatus = LANSITEC_CONTACT_BADGE_BUZZER[(status >> 8) & 0x0F]; } - // 0 means not changed. The receiving threshold table is for range bit 7 = 0: -73 to -87 dBm. + // 0 means not changed. The threshold is for range bit 7 = 0: -73 to -87 dBm. if (((status >> 4) & 0x07) > 0) { values.bluetoothTxPower = LANSITEC_CONTACT_BADGE_BT_POWERS[(status >> 4) & 0x07]; } @@ -580,7 +606,7 @@ function lansitecContactBadgeValues(b) { values.bluetoothReceivingThreshold = -72 - (status & 0x0F); } } - // Friend field: Type 0xC with the number of friend badges (1-6), then Major (2) and Minor (2) of each. + // Friend field: Type 0xC with the number of friend badges, then Major (2) and Minor (2) of each. if (b.length >= 12 && ((b[11] >> 4) & 0x0F) == 0xC && b.length >= 12 + (b[11] & 0x0F) * 4) { var friends = []; for (i = 0; i < (b[11] & 0x0F); i++) { @@ -590,9 +616,8 @@ function lansitecContactBadgeValues(b) { values.friends = friends; } } else if (type == 0x8) { - // 4.5.3 10s Instant Report: Type (bits 3-0: number of beacons M), Major (2), Minor (2) and RSSI of each, then - // TOFF: (M + 1) / 2 bytes, 4 bits per beacon, the first beacon in the high bits: seconds before the message. - // RSSI bit 7: 1 a Bluetooth beacon, 0 a badge (beacon-in-tracker); bits 6-0 - 128 is the RSSI, dBm. + // 4.5.3 10s Instant Report: Type (bits 3-0: M beacons), Major (2), Minor (2), RSSI of each, then TOFF: + // (M + 1) / 2 bytes, 4 bits per beacon, high bits first. RSSI bit 7: 1 a beacon, 0 a badge; bits 6-0 - 128. var count = b[0] & 0x0F; if (count > 0 && b.length >= 1 + count * 5) { var hasOffsets = b.length >= 1 + count * 5 + Math.floor((count + 1) / 2); @@ -614,8 +639,8 @@ function lansitecContactBadgeValues(b) { values.beacons = list; } } else if (type == 0xC) { - // 4.5.5 80s Periodic Report: Type (bits 3-0: number of badges and beacons), badges as Major (2), Minor (2), - // TimeMark, then the marker 0xFFFF and beacons as Minor (2), TimeMark. A beacon's Major is always FFFF. + // 4.5.5 80s Periodic Report: badges as Major (2), Minor (2), TimeMark, then 0xFFFF and beacons as Minor (2), + // TimeMark (a beacon's Major is FFFF). var badges = []; var periodBeacons = []; var pos = 1; @@ -639,72 +664,9 @@ function lansitecContactBadgeValues(b) { values.beacons = periodBeacons; } } else if (type == 0xF && b.length >= 2) { - // 4.5.4 Acknowledge: Type bits 3-0 the result, then the MSGID of the downlink. - values.result = (b[0] & 0x0F) == 0 ? 'Success' : 'Failure'; - values.msgid = b[1]; + lansitecAcknowledgment(values, b); } return values; } -var values = {}; -var attributes = {}; -var history = []; -if (metadata.brandId == 'lansitec' && (metadata.modelId == LANSITEC_SOCKET_SYNC || metadata.modelId == LANSITEC_CONTACT_BADGE) - && metadata.data != null) { - // The SocketSync gateway and the contact tracing badge: decoded from the raw bytes only, see - // lansitecSocketSyncValues and lansitecContactBadgeValues. - var lansitecBytes = lansitecBase64ToBytes(metadata.data); - var socketSync = metadata.modelId == LANSITEC_SOCKET_SYNC; - values = socketSync ? lansitecSocketSyncValues(lansitecBytes) : lansitecContactBadgeValues(lansitecBytes); - // Registration holds the device settings: attributes, not telemetry. - if (lansitecBytes.length > 0 && ((lansitecBytes[0] >> 4) & 0x0F) == (socketSync ? 0x4 : 0x1)) { - attributes = values; - values = {}; - } -} else if (metadata.payloadFormat == 'JSON') { - values = decodeToJson(payload); - if (metadata.brandId == 'lansitec' && values.bytes != null && typeof values.bytes === 'object' && !Array.isArray(values.bytes)) { - // The Lansitec codecs put everything in "bytes" and get some fields wrong: see the Lansitec block above. - var raw = values.bytes; - values = lansitecValues(raw, metadata.modelId); - if (metadata.data != null) { - lansitecRawValues(values, lansitecBase64ToBytes(metadata.data), metadata.modelId); - } - // Register is sent after join and holds the device settings: they are attributes, not telemetry. - var messageType = raw.type != null ? raw.type : raw['Message type']; - if (messageType == 'Register') { - attributes = values; - values = {}; - } - } else if (Array.isArray(values.history)) { - values.history.forEach(function (record) { - if (record.timestamp != null) { - var ts = record.timestamp * 1000; - delete record.timestamp; - history.push({ ts: ts, values: record }); - } - }); - delete values.history; - } -} -var profile = 'LoRaWAN Device'; -if (metadata.brandId != null && metadata.modelId != null) { - profile = metadata.brandId + '-' + metadata.modelId; - // The profile name chosen at install, kept in the integration metadata - var customProfile = metadata['profile.' + profile]; - if (customProfile != null && customProfile != '') { - profile = customProfile; - } -} -if (Object.keys(attributes).length > 0) { - return { profile: profile, attributes: attributes }; -} -if (history.length == 0) { - return { profile: profile, telemetry: { ts: metadata.ts, values: values } }; -} -history.push({ ts: metadata.ts, values: values }); -return { profile: profile, telemetry: history }; - -function decodeToJson(payload) { - return JSON.parse(String.fromCharCode.apply(String, payload)); -} +return decodeThingsStackUplink(payload, metadata); diff --git a/ui-ngx/src/assets/converters/tbel-tti-decoder.raw b/ui-ngx/src/assets/converters/tbel-tti-decoder.raw index a3f160c3dc..be3d8f9098 100644 --- a/ui-ngx/src/assets/converters/tbel-tti-decoder.raw +++ b/ui-ngx/src/assets/converters/tbel-tti-decoder.raw @@ -2,38 +2,69 @@ // is then that JSON: its keys become telemetry. A Milesight-style history list keeps each record's time. // Such a device gets the profile -, so all devices of one model share it, // or the name the integration metadata gives it under profile.-. Any other device gets LoRaWAN Device. +// Lansitec devices are decoded by the Lansitec block below. +function decodeThingsStackUplink(payload, metadata) { + var values = {}; + var attributes = {}; + var history = []; + var lansitec = null; + if (metadata.brandId == "lansitec") { + lansitec = lansitecUplink(payload, metadata); + } + if (lansitec != null) { + values = lansitec.telemetry; + attributes = lansitec.attributes; + } else if (metadata.payloadFormat == "JSON") { + values = decodeToJson(payload); + if (values.history instanceof java.util.List) { + foreach (record : values.history) { + if (record.timestamp != null) { + var ts = record.timestamp * 1000; + record.remove("timestamp"); + history.add({ ts: ts, values: record }); + } + } + values.remove("history"); + } + } + var profile = "LoRaWAN Device"; + if (metadata.brandId != null && metadata.modelId != null) { + profile = metadata.brandId + "-" + metadata.modelId; + // The profile name chosen at install, kept in the integration metadata + var customProfile = metadata["profile." + profile]; + if (customProfile != null && customProfile != "") { + profile = customProfile; + } + } + if (!attributes.isEmpty()) { + return { profile: profile, attributes: attributes }; + } + if (history.isEmpty()) { + return { profile: profile, telemetry: { ts: metadata.ts, values: values } }; + } + history.add({ ts: metadata.ts, values: values }); + return { profile: profile, telemetry: history }; +} -// Lansitec: the LoRaWAN Device Repository codecs wrap everything in "bytes", write numbers as strings with units -// ("85%", "-67dBm") and get some fields wrong. Key names follow the Lansitec datasheets where there are any: -// Bluetooth Gateway DataSheet 2.27, Badge Tracker DataSheet v5.30, Temperature Humidity Sensor DataSheet 1.26, -// Plug Bluetooth Gateway DataSheet 1.5 (SocketSync gateway), -// LoRaWAN Bluetooth Badge DataSheet 2.78 (contact tracing badge). -// The wrong fields are decoded from the raw bytes (metadata.data). Remove a fix when the Repository fixes the codec. +// --------------------------------------------------------------------------------------------------------------------- +// Lansitec. The Repository codecs wrap everything in "bytes", write numbers with units ("85%", "-67dBm") and get some +// fields wrong: those are decoded from the raw bytes (metadata.data) by the datasheets. Remove a fix when the codec is fixed. +// Datasheets: Bluetooth Gateway 2.27, Badge Tracker v5.30, Temperature Humidity Sensor 1.26, Plug Bluetooth Gateway 1.5 +// (SocketSync), LoRaWAN Bluetooth Badge 2.78 (contact tracing badge), Container Tracker 0.72, Helmet Tracker v0.14. var LANSITEC_UNITS = ["dBm", "dB", "%", "V", "sec", "min", "s", "℃", "level", " meter"]; - var LANSITEC_GATEWAYS = ["compact-bluetooth-gateway", "indoor-bluetooth-gateway", "macro-bluetooth-gateway", "solar-bluetooth-gateway"]; +var LANSITEC_SOCKET_SYNC = "socket-sync-bluetooth-gateway"; +var LANSITEC_CONTACT_BADGE = "contact-tracing-badge"; -// Codec key -> datasheet name, per model group. "-" drops the key. -// -// Bluetooth Gateway DataSheet 2.27, 4.4.1 Registration: ADR, Mode (current working LoRaWAN frequency sub-band plan), -// LoRaWAN frequency band, Power (LoRa TX power, dBm), Continuously BLE receiving, DR (LoRaWAN Uplink Data Rate), -// PRI (Position report interval), HB (Heartbeat interval), Bluetooth receiving duration. -// 4.4.2 Heartbeat: RSSI and SNR (LoRa), VER (Firmware version). VOL, SNR and CHGSTAT come from the raw bytes. -// number is the count of reported devices: the beacons list has them. The beacons come from the raw bytes too. +// Codec key -> datasheet name. "-" drops the key: it is decoded from the raw bytes or is not a value. +// Bluetooth Gateway 4.4.1 Registration, 4.4.2 Heartbeat. var LANSITEC_GATEWAY_KEYS = { adr: "adr", mode: "mode", LoRaWANband: "loraWanFrequencyBand", LoRaTxpower: "loraTxPower", ContinuousReceive: "continuousBleReceiving", dr: "dataRate", pos: "positionReportInterval", hb: "heartbeatInterval", BLEReceivingDuration: "bluetoothReceivingDuration", - rssi: "loraRssi", snr: "-", ver: "deviceFirmwareVersion", vol: "-", chgstate: "-", number: "-" + rssi: "loraRssi", snr: "-", ver: "-", vol: "-", chgstate: "-", number: "-" }; - -// Badge Tracker DataSheet v5.30, 5.1 Registration: ADR, LoRa TX power, DR, GNSSEN (GNSS enable status), -// POSMODE (Positioning report mode), BLEPRI (Bluetooth Position Report Interval). The codec reads it without the -// GNSSPRI field, so heartbeat, version, CFMMSG, HBCOUNT and fall detection come from the raw bytes. -// 5.2 Heartbeat: Battery (percentage), RSSI (LoRa), GNSSSTATE (Status of the GNSS module), CHGSTATE (Status of -// battery charging). SNR and VIBSTATE come from the raw bytes. -// 5.3 GNSS Position: longitude, latitude and time come from the raw bytes. -// 5.4 Beacon: bytes 1-5 are RFU, the codec reads byte 1 as Vibstate. 5.5 Alarm: 0x01 SOS, 0x02 Fall Detection. +// Badge Tracker 5.1 Registration (the codec skips GNSSPRI), 5.2 Heartbeat, 5.3 GNSS Position, 5.4 Beacon, 5.5 Alarm. var LANSITEC_BADGE_KEYS = { ADR: "adr", "Lora Txpower": "loraTxPower", DR: "dataRate", GNSSEN: "gnssEnabled", "Position mode": "positioningReportMode", "Position period": "bluetoothPositionReportInterval", @@ -42,27 +73,70 @@ var LANSITEC_BADGE_KEYS = { "Move state": "-", "Charge state": "chargeState", Longitude: "-", Latitude: "-", Time: "-", Length: "-", Vibstate: "-", "Alarm type": "alarm" }; - -// Temperature Humidity Sensor DataSheet 1.26, 2.2.1 Register: ADR, MODE (current working scheme), SMODE (data -// scheme supported), POWER (configured transmit power, dBm), DR, REPTIND (report H&T if the value is the same; -// the codec gives "false" for 0 = always report), TH (period of temperature and humidity report), CRC. -// 2.2.2 Periodical temperature and humidity: VER is the version of the message type, not the firmware; -// VOL (battery left, %), RSSI (downlink, detected by the sensor). SNR, temperature and humidity come from the raw bytes. +// Temperature Humidity Sensor 2.2.1 Register, 2.2.2 Periodical data (VER is the message version, not the firmware). var LANSITEC_TEMPERATURE_HUMIDITY_KEYS = { adr: "adr", mode: "mode", smode: "smode", LoraTxpower: "loraTxPower", dr: "dataRate", repting: "reptind", th: "temperatureHumidityReportPeriod", crc: "-", ver: "-", vol: "batteryLevel", rssi: "loraRssi", snr: "-", temperature: "-", humidity: "-" }; - -// Without a datasheet only these keys are renamed: the integration writes rssi, snr and firmwareVersion itself. -// chgstate: the tracking label has the gateway codec byte for byte, which reads CHGSTAT 0x50 as "Not charging" -// (Bluetooth Gateway DataSheet 2.27, 4.4.2), so it is dropped. number is the count -// of reported devices: the beacons list has them. +// The other models: the integration writes rssi, snr and firmwareVersion itself. chgstate: the tracking label has the +// gateway codec, which misreads CHGSTAT (Bluetooth Gateway 4.4.2). number: the beacons list has them. var LANSITEC_CLASH_KEYS = { rssi: "loraRssi", RSSI: "loraRssi", snr: "loraSnr", SNR: "loraSnr", firmwareVersion: "deviceFirmwareVersion", "Firmware version": "deviceFirmwareVersion", type: "-", "Message type": "-", chgstate: "-", number: "-" }; +// Beacon keys: Devicetype1data1, Device1data, dev1major, dev1.major, pos1minor, asset1rssi... +var LANSITEC_BEACON_PREFIXES = ["devicetype1", "devicetype2", "devicetype3", "device", "dev", "pos", "asset"]; +var LANSITEC_BEACON_FIELDS = ["data", "major", "minor", "rssi", "id"]; +// Register Type bits 2-0 and SMODE: the LoRa frequency plan (Plug Bluetooth Gateway 5.1, Bluetooth Badge 4.5.1). +var LANSITEC_MODES = ["", "AU915", "IN865", "CN470", "AS923", "EU433", "EU868", "US915"]; +var LANSITEC_SMODES = [1, 2, 4, 8, 16, 32, 64]; +var LANSITEC_BANDS = ["AU915", "IN865", "CN470", "AS923", "EU433", "EU868", "US915"]; +// Badge Tracker 5.2 Heartbeat, as the codec writes them. +var LANSITEC_BADGE_GNSS_STATES = ["GPS OFF", "GPS boot", "GPS locating", "GPS located", "", "", "", "", "", "GPS no signal"]; +var LANSITEC_BADGE_CHARGE_STATES = ["Power cable disconnected", "", "", "", "", "Charging", "Charge complete"]; +// Plug Bluetooth Gateway 5.1 CFG bits 3-1. +var LANSITEC_SOCKET_SYNC_TYPES = ["Outdoor gateway", "Indoor gateway", "Plug gateway"]; +// Bluetooth Badge 4.5.1 BT bits 10-8, 4.5.2 BTSTATUS bits 6-4 (dBm, 0 is not a power) and bits 11-8. +var LANSITEC_CONTACT_BADGE_BT_POWERS = [0, 4, 0, -4, -8, -12, -16, -20]; +var LANSITEC_CONTACT_BADGE_BUZZER = ["No action", "Vibrator disabled, buzzer muted (restore)", + "Vibrator disabled, buzzer half sound level", "Vibrator disabled, buzzer full sound level", + "Vibrator enabled, buzzer muted", "Vibrator enabled, buzzer half sound level", + "Vibrator enabled, buzzer full sound level", "Vibrator disabled, buzzer muted (no restore)", + "Beacon-in-tracker and proximity detection disabled"]; + +// { telemetry, attributes }, or null when the uplink is not one the Lansitec block decodes. +function lansitecUplink(payload, metadata) { + var model = metadata.modelId; + var values = null; + var register = false; + if ((model == LANSITEC_SOCKET_SYNC || model == LANSITEC_CONTACT_BADGE) && metadata.data != null) { + // Their codecs are the gateway's and misread every message: decoded from the raw bytes only. + var b = base64ToBytes(metadata.data); + values = model == LANSITEC_SOCKET_SYNC ? lansitecSocketSyncValues(b) : lansitecContactBadgeValues(b); + register = b.length > 0 && ((b[0] >> 4) & 0x0F) == (model == LANSITEC_SOCKET_SYNC ? 0x4 : 0x1); + } else if (metadata.payloadFormat == "JSON") { + var json = decodeToJson(payload); + if (!isMap(json.bytes)) { + return null; + } + var raw = json.bytes; + values = lansitecValues(raw, model); + if (metadata.data != null) { + lansitecRawValues(values, base64ToBytes(metadata.data), model); + } + var messageType = raw.type != null ? raw.type : raw["Message type"]; + register = messageType == "Register"; + } else { + return null; + } + // Register holds the device settings: attributes, not telemetry. + if (register) { + return { telemetry: {}, attributes: values }; + } + return { telemetry: values, attributes: {} }; +} // Strips the unit. Periods become seconds: the codecs give most of them in seconds, a few in minutes. function lansitecNumber(v) { @@ -104,10 +178,6 @@ function lansitecCamelCase(key) { return result; } -// Beacon keys: Devicetype1data1, Device1data, dev1major, dev1.major, pos1minor, asset1rssi... -var LANSITEC_BEACON_PREFIXES = ["devicetype1", "devicetype2", "devicetype3", "device", "dev", "pos", "asset"]; -var LANSITEC_BEACON_FIELDS = ["data", "major", "minor", "rssi", "id"]; - function lansitecBeaconKey(key) { var k = key.toLowerCase().replace(".", ""); foreach (p : LANSITEC_BEACON_PREFIXES) { @@ -156,11 +226,13 @@ function lansitecValues(raw, model) { } else if (b.field == "data" || b.field == "major" || b.field == "minor") { beacons[id][b.field] = value; } - } else if (table == null && key == "time" && value instanceof String) { - // Container tracker and helmet sensor, no datasheet: their codecs add 8 hours to the GNSS time and wrap - // it in quotes, as the badge tracker codec does with the UTC time of DataSheet v5.30, 5.3. + } else if (table == null && key.matches("time[1-8]?") && value instanceof String) { + // Container Tracker 5.3 and 5.7, Helmet Tracker 6.3: UTC time. The codecs add 8 hours and quote it. var gnssDate = new Date(value.replace("\"", "")); - values.gnssTime = gnssDate.getTime() - 8 * 60 * 60 * 1000; + values["gnssTime" + key.substring(4)] = gnssDate.getTime() - 8 * 60 * 60 * 1000; + } else if (key == "pressure" && model == "helmet-sensor" && !(value instanceof String)) { + // Helmet Tracker 6.3: the value * 10, unit 0.1 Pa. + values.pressure = value / 10.0; } else if (table != null && table.containsKey(key)) { var tableName = table[key]; if (tableName != "-") { @@ -182,28 +254,31 @@ function lansitecValues(raw, model) { return values; } +// In the order of their numbers, which can have gaps (Container Tracker 5.7: dev1, dev3...). function lansitecBeaconList(beacons) { var list = []; - for (var i = 1; i <= beacons.size(); i++) { + var added = {}; + while (list.size() < beacons.size()) { + var next = null; foreach (id : beacons.keySet()) { - var b = beacons[id]; - if (b.order == i) { - var item = {}; - if (b.data != null) { - item.data = b.data; - } - if (b.major != null) { - item.major = b.major; - item.minor = b.minor; - } - item.rssi = b.rssi; - list.add(item); + if (added[id] == null && (next == null || beacons[id].order < beacons[next].order)) { + next = id; } } + added[next] = true; + var b = beacons[next]; + var item = {}; + if (b.data != null) { + item.data = b.data; + } + if (b.major != null) { + item.major = b.major; + item.minor = b.minor; + } + item.rssi = b.rssi; + list.add(item); } - // The gateway codec reads the data of every device after the first at a 5-byte step, so it is right only - // for 4-byte data (DataSheet 4.4.4 examples): with longer data only the first device is kept. The gateways - // with a datasheet get their beacons from the raw bytes instead; this is for the models with the gateway codec. + // The gateway codec reads every device after the first at a 5-byte step: right only for 4-byte data. if (list.size() > 1 && list[0].data != null && list[0].data.length() != 8) { var first = []; first.add(list[0]); @@ -212,7 +287,6 @@ function lansitecBeaconList(beacons) { return list; } -// Decoded from the raw bytes by the datasheet where the codec gets it wrong. function lansitecRawValues(values, b, model) { if (b.length == 0) { return; @@ -232,7 +306,7 @@ function lansitecFloatIsNumber(b, offset) { return !((b[offset] & 0x7F) == 0x7F && (b[offset + 1] & 0x80) != 0); } -// SNR, unit 0.01 dB, -3000 to 3000: signed. The codecs read it unsigned. +// SNR, unit 0.01 dB, signed. The codecs read it unsigned. function lansitecSnr(b, offset) { var v = parseBytesToInt(b, offset, 2, true); if (v > 32767) { @@ -241,31 +315,70 @@ function lansitecSnr(b, offset) { return v / 100.0; } -// Bluetooth Gateway DataSheet 2.27. +// CHGSTAT: 0 Not charging, then the codes of Charging and Charging completed. +function lansitecChargeState(values, charge, charging, completed) { + if (charge == 0) { + values.chargeState = "Not charging"; + } else if (charge == charging) { + values.chargeState = "Charging"; + } else if (charge == completed) { + values.chargeState = "Charging completed"; + } +} + +// Register of the SocketSync gateway (5.1) and the contact tracing badge (4.5.1): Type (ADR, MODE), SMODE, POWER, +// CFG, POS (2, unit 5 s), then HB (unit 30 s) at hbOffset. +function lansitecRegister(values, b, hbOffset) { + values.adr = (b[0] & 0x08) != 0 ? "ON" : "OFF"; + var mode = b[0] & 0x07; + if (LANSITEC_MODES[mode] != "") { + values.mode = LANSITEC_MODES[mode]; + } + var band = LANSITEC_SMODES.indexOf(b[1] & 0xFF); + if (band >= 0) { + values.loraWanFrequencyBand = LANSITEC_BANDS[band]; + } + values.loraTxPower = (b[2] >> 3) & 0x1F; + values.dataRate = "DR" + ((b[3] >> 4) & 0x0F); + values.positionReportInterval = parseBytesToInt(b, 4, 2, true) * 5; + values.heartbeatInterval = (b[hbOffset] & 0xFF) * 30; +} + +// Heartbeat of the SocketSync gateway (5.2) and the contact tracing badge (4.5.2): Type, VOL, RSSI (unit -1 dBm), +// SNR (2), REV, CHGSTAT (0x00, 0x50, 0x60 and 0x0, 0x5, 0x6). +function lansitecHeartbeat(values, b, charging, completed) { + values.loraRssi = 0 - (b[2] & 0xFF); + values.loraSnr = lansitecSnr(b, 3); + values.deviceFirmwareVersion = bytesToHex(b).substring(10, 12); + lansitecChargeState(values, b[6] & 0xFF, charging, completed); +} + +// Acknowledgment (Plug Bluetooth Gateway 5.5, Bluetooth Badge 4.5.4): Type bits 3-0 the result, then the MSGID. +function lansitecAcknowledgment(values, b) { + values.result = (b[0] & 0x0F) == 0 ? "Success" : "Failure"; + values.msgid = b[1] & 0xFF; +} + +// Bluetooth Gateway 2.27. function lansitecGatewayRawValues(values, b, model, type) { if (type == 1 && b.length >= 9) { - // 4.4.1: Network reconnection interval, unit 5 minutes. The codec does not decode it. + // 4.4.1: Network reconnection interval, unit 5 minutes. values.networkReconnectionInterval = (b[8] & 0xFF) * 300; } else if (type == 2 && b.length >= 8) { - // 4.4.2: VOL is the battery voltage (VOL x 0.01 + 1.5 V) on the macro gateway and the battery level (%) on - // the others. The codec decides by the value (<= 100 is %), not by the model. + // 4.4.2: VOL is the battery voltage (x 0.01 + 1.5 V) on the macro gateway, the battery level (%) on the others. if (model == "macro-bluetooth-gateway") { values.batteryVoltage = ((b[1] & 0xFF) + 150) / 100.0; } else { values.batteryLevel = b[1] & 0xFF; } values.loraSnr = lansitecSnr(b, 3); - // CHGSTAT: 0x00 Not charging, 0x50 Charging, 0x60 Charging completed on the compact and solar gateway, - // always 0x00 on the macro and indoor gateway. The codec reads the low half of the byte. + // VER 108 is V1.08. + var version = parseBytesToInt(b, 5, 2, true); + var minor = (version % 100).intValue(); + values.deviceFirmwareVersion = "V" + (version / 100).intValue() + "." + (minor < 10 ? "0" : "") + minor; + // CHGSTAT on the compact and solar gateway only. if (model == "compact-bluetooth-gateway" || model == "solar-bluetooth-gateway") { - var charge = b[7] & 0xFF; - if (charge == 0x00) { - values.chargeState = "Not charging"; - } else if (charge == 0x50) { - values.chargeState = "Charging"; - } else if (charge == 0x60) { - values.chargeState = "Charging completed"; - } + lansitecChargeState(values, b[7] & 0xFF, 0x50, 0x60); } } else if (type == 0x8 || type == 0x9 || type == 0xA || type == 0xE) { values.remove("beacons"); @@ -276,12 +389,8 @@ function lansitecGatewayRawValues(values, b, model, type) { } } -// 4.4.4-4.4.6: Device Type 1, 2 and 3 messages: Type (bits 3-0: number of devices), then Data (1-31 bytes) and -// RSSI (value - 256, dBm) of each device. Data length is not in the message: it is set by the device report rule -// of the type (4.4.3), so all devices of one message have the same length. The codec reads every device after -// the first at a 5-byte step. -// 4.4.7: Multi-Device Type message: Device Type ID before the data of each device. The data length can differ per -// type, and the message does not say it: decoded only when all devices fit the same length. +// 4.4.4-4.4.6: Type (bits 3-0: number of devices), Data and RSSI (value - 256) of each device; one data length per +// message (4.4.3). 4.4.7 Multi-Device: a Device Type ID before each device, decoded when all have the same length. function lansitecGatewayBeacons(b, type) { var count = b[0] & 0x0F; if (count == 0 || (b.length - 1) % count != 0) { @@ -314,15 +423,12 @@ function lansitecGatewayBeacons(b, type) { return list; } -// Badge Tracker DataSheet v5.30. +// Badge Tracker v5.30. function lansitecBadgeRawValues(values, b, type) { if (type == 1 && b.length == 14) { - // 5.1, 14 bytes: Type, SMODE, Power, Config, BLEPRI (2), GNSSPRI (2), Heartbeat, Version (2), CFMMSG, HBCOUNT, Fall. - // BLEEN (Config bit 0) is not decoded: the field table says BLE enable status and that GNSSEN or BLEEN must - // be enabled, but the example (Config 32) has both 0 and calls bit 0 reserved. + // 5.1: GNSSPRI (2), Heartbeat, Version (2, 0x0525 is V5.25), CFMMSG, HBCOUNT, Fall (unit 0.5 m). values.gnssPositionReportInterval = parseBytesToInt(b, 6, 2, true) * 5; values.heartbeatInterval = (b[8] & 0xFF) * 30; - // Version 0x0525 is V5.25. var version = bytesToHex(b).substring(18, 22); var major = version.substring(0, 2); if (major.startsWith("0")) { @@ -331,19 +437,30 @@ function lansitecBadgeRawValues(values, b, type) { values.deviceFirmwareVersion = "V" + major + "." + version.substring(2); values.cfmmsg = b[11] & 0xFF; values.hbcount = b[12] & 0xFF; - // Fall detection threshold, unit 0.5 m, 0 means off. values.fallDetectionThreshold = (b[13] & 0xFF) * 0.5; } else if (type == 2 && b.length >= 6) { - // 5.2: the SNR field is there since firmware 0x0501. The SNREN bit (Type bits 3-0) is not used: the - // datasheet example 20 64 3E 033E 00 60 00 has SNREN 0 and the SNR 8.3 dB. - values.loraSnr = lansitecSnr(b, 3); - // VIBSTATE: moving state 0-9, 0 means no moving, 9 high acceleration. The codec turns only 0 and 1 into text. - values.movingState = b[5] & 0x0F; + // 5.2: SNR (2) since firmware 0x0501, decided by the length (SNREN is not reliable). Then GNSSSTAT and + // VIBSTATE (moving state 0-9), CHGSTAT. The codec always reads the SNR. + var state = 3; + if (b.length >= 8) { + values.loraSnr = lansitecSnr(b, 3); + state = 5; + } else { + var gnss = (b[3] >> 4) & 0x0F; + var charge = (b[4] >> 4) & 0x0F; + values.remove("gnssState"); + values.remove("chargeState"); + if (gnss < LANSITEC_BADGE_GNSS_STATES.size() && LANSITEC_BADGE_GNSS_STATES[gnss] != "") { + values.gnssState = LANSITEC_BADGE_GNSS_STATES[gnss]; + } + if (charge < LANSITEC_BADGE_CHARGE_STATES.size() && LANSITEC_BADGE_CHARGE_STATES[charge] != "") { + values.chargeState = LANSITEC_BADGE_CHARGE_STATES[charge]; + } + } + values.movingState = b[state] & 0x0F; } else if (type == 3 && b.length >= 13) { - // 5.3: Flag (Type bits 3-0) 0x1 means GNSS positioning failed. Longitude and latitude are IEEE 754 floats - // (the codec divides the mantissa by 0x7fffff instead of 2^23), time is UTC seconds (the codec adds 8 hours). - // Rounded to 6 decimals (about 0.1 m), as a double: the float has no more precision. - // An exponent of 0xFF is not a number (NaN or infinity): parseBytesToFloat fails on NaN, so no position. + // 5.3: Flag 0x1 is a failed fix. IEEE 754 floats (the codec divides by 0x7fffff) rounded to 6 decimals, + // UTC seconds (the codec adds 8 hours). No position from NaN: parseBytesToFloat fails on it. if ((b[0] & 0x0F) == 0 && lansitecFloatIsNumber(b, 1) && lansitecFloatIsNumber(b, 5)) { values.longitude = toFixed(parseBytesToFloat(b, 1, 4, true) * 1.0, 6); values.latitude = toFixed(parseBytesToFloat(b, 5, 4, true) * 1.0, 6); @@ -352,9 +469,7 @@ function lansitecBadgeRawValues(values, b, type) { } } -// Temperature Humidity Sensor DataSheet 1.26, 2.2.2: VER (Type bits 3-0) 1 means the SNR field is there, 0 means -// it is not and the message is 8 bytes. The codec always reads the SNR, so with VER 0 the next fields are shifted. -// TEMP_INT bit 7 is the sign, TEMP_FRA the hundredths. +// Temperature Humidity Sensor 2.2.2: VER 1 has the SNR, VER 0 not (the codec always reads it). TEMP_INT bit 7 is the sign. function lansitecTemperatureHumidityRawValues(values, b) { var offset = (b[0] & 0x0F) == 1 ? 5 : 3; if (b.length < offset + 3) { @@ -371,20 +486,7 @@ function lansitecTemperatureHumidityRawValues(values, b) { values.humidity = b[offset + 2] & 0xFF; } -// Plug Bluetooth Gateway DataSheet 1.5 (990-01203), the SocketSync gateway. Its codec is the compact gateway's, but -// the messages are not: the codec gives nothing for the Registration (0x4) and the Beacon message (0x6), so The -// Things Stack sends them without decoded_payload, and it reads the Heartbeat and the Alarm Beacon List (0xE) with -// the compact gateway's layout. So every message is decoded from the raw bytes, with or without decoded_payload. -var LANSITEC_SOCKET_SYNC = "socket-sync-bluetooth-gateway"; -// 5.1 Type bits 2-0 (MODE) and SMODE: the LoRa frequency plan. -var LANSITEC_SOCKET_SYNC_MODES = ["", "AU915", "", "CN470", "AS923", "EU433", "EU868", "US915"]; -var LANSITEC_SOCKET_SYNC_SMODES = [1, 4, 8, 16, 32, 64]; -var LANSITEC_SOCKET_SYNC_BANDS = ["AU915", "CN470", "AS923", "EU433", "EU868", "US915"]; -// 5.1 CFG bits 3-1: the gateway type. -var LANSITEC_SOCKET_SYNC_TYPES = ["Outdoor gateway", "Indoor gateway", "Plug gateway"]; -// Table 7: UUID List. A beacon gives the last byte of its UUID, the other bytes are the same for all of them. -var LANSITEC_SOCKET_SYNC_UUID = "5C38DBDE567C4CCAB1DA40A8AD4656"; - +// Plug Bluetooth Gateway 1.5, the SocketSync gateway. function lansitecSocketSyncValues(b) { var values = {}; if (b.length == 0) { @@ -392,49 +494,24 @@ function lansitecSocketSyncValues(b) { } var type = (b[0] >> 4) & 0x0F; if (type == 0x4 && b.length >= 10) { - // 5.1 Registration: Type (ADR, MODE), SMODE, POWER, CFG, POS (2), BLE Receiving Duration, HB, Beacon_QTY, CRC (2). - values.adr = (b[0] & 0x08) != 0 ? "ON" : "OFF"; - var mode = b[0] & 0x07; - if (LANSITEC_SOCKET_SYNC_MODES[mode] != "") { - values.mode = LANSITEC_SOCKET_SYNC_MODES[mode]; - } - var band = LANSITEC_SOCKET_SYNC_SMODES.indexOf(b[1] & 0xFF); - if (band >= 0) { - values.loraWanFrequencyBand = LANSITEC_SOCKET_SYNC_BANDS[band]; - } - values.loraTxPower = (b[2] >> 3) & 0x1F; + // 5.1 Registration: ..., BLE Receiving Duration, HB, Beacon_QTY (half of the limit), CRC (2). + lansitecRegister(values, b, 7); values.continuousBleReceiving = (b[2] & 0x02) != 0 ? "Continuous Bluetooth receiving is enabled" : "Continuous Bluetooth receiving is disabled"; values.powerDownFlag = (b[2] & 0x01) != 0 ? "Automatic shutdown status" : "Normal status"; - values.dataRate = "DR" + ((b[3] >> 4) & 0x0F); var gatewayType = (b[3] >> 1) & 0x07; if (gatewayType < LANSITEC_SOCKET_SYNC_TYPES.size()) { values.gatewayType = LANSITEC_SOCKET_SYNC_TYPES[gatewayType]; } values.rssiSortingMethod = (b[3] & 0x01) != 0 ? "Sort by the maximum value" : "Sort by average"; - // POS unit 5 s, 0 means off. HB unit 30 s. Beacon_QTY is half of the limit. - values.positionReportInterval = parseBytesToInt(b, 4, 2, true) * 5; values.bluetoothReceivingDuration = b[6] & 0xFF; - values.heartbeatInterval = (b[7] & 0xFF) * 30; values.beaconQuantityLimit = (b[8] & 0xFF) * 2; } else if (type == 0x2 && b.length >= 7) { - // 5.2 Heartbeat: Type, VOL (battery level, %), RSSI (unit -1 dBm), SNR (2, signed, 0.01 dB), REV (firmware, - // hex), CHGSTAT, CRC (2). The codec reads REV and CHGSTAT as the compact gateway's 2-byte version, and the - // first CRC byte as CHGSTAT. VOL keeps its name: the dashboard of the IoT Hub package reads it. + // 5.2 Heartbeat. VOL (%) keeps its name: the dashboard of the IoT Hub package reads it. values.vol = b[1] & 0xFF; - values.loraRssi = 0 - (b[2] & 0xFF); - values.loraSnr = lansitecSnr(b, 3); - values.deviceFirmwareVersion = bytesToHex(b).substring(10, 12); - var charge = b[6] & 0xFF; - if (charge == 0x00) { - values.chargeState = "Not charging"; - } else if (charge == 0x50) { - values.chargeState = "Charging"; - } else if (charge == 0x60) { - values.chargeState = "Charging completed"; - } + lansitecHeartbeat(values, b, 0x50, 0x60); } else if (type == 0x6 || type == 0xE) { - // 5.3 Beacon and 5.4 Alarm Beacon List: Type (bits 3-0: number of beacons), then UUID tail number, Major (2), - // Minor (2) and RSSI (value - 256, dBm) of each beacon. The alarm list holds the beacons that trigger the alarm. + // 5.3 Beacon, 5.4 Alarm Beacon List: Type (bits 3-0: number of beacons), then UUID tail, Major (2), Minor (2), + // RSSI (value - 256) of each. Table 7: the UUID prefix ends with 56 for tails 0x56-0xFF, 57 for 0x00-0x1E. var count = b[0] & 0x0F; if (count > 0 && b.length >= 1 + count * 6) { var hex = bytesToHex(b); @@ -442,7 +519,7 @@ function lansitecSocketSyncValues(b) { for (var i = 0; i < count; i++) { var start = 1 + i * 6; var item = {}; - item.uuid = LANSITEC_SOCKET_SYNC_UUID + hex.substring(2 * start, 2 * start + 2); + item.uuid = "5C38DBDE567C4CCAB1DA40A8AD465" + ((b[start] & 0xFF) < 0x56 ? "7" : "6") + hex.substring(2 * start, 2 * start + 2); item.major = parseBytesToInt(b, start + 1, 2, true); item.minor = parseBytesToInt(b, start + 3, 2, true); item.rssi = (b[start + 5] & 0xFF) - 256; @@ -455,32 +532,12 @@ function lansitecSocketSyncValues(b) { } } } else if (type == 0xF && b.length >= 2) { - // 5.5 Acknowledgment: Type bits 3-0 the result, then the MSGID of the downlink. - values.result = (b[0] & 0x0F) == 0 ? "Success" : "Failure"; - values.msgid = b[1] & 0xFF; + lansitecAcknowledgment(values, b); } return values; } -// LoRaWAN Bluetooth Badge DataSheet 2.78 (990-01242), the contact tracing badge: it reports the badges and the -// Bluetooth beacons near it, it has no GNSS. Its codec is the Bluetooth gateway's, which reads these messages with -// the gateway layout, so every message is decoded from the raw bytes, as for the SocketSync gateway. -// Major and Minor are hex: a badge's are the last 4 bytes of its DevEUI (4.4). -var LANSITEC_CONTACT_BADGE = "contact-tracing-badge"; -// 4.5.1 Type bits 2-0 (Mode) and SMODE: the LoRa frequency plan. -var LANSITEC_CONTACT_BADGE_MODES = ["", "AU915", "IN865", "CN470", "AS923", "EU433", "EU868", "US915"]; -var LANSITEC_CONTACT_BADGE_SMODES = [1, 2, 4, 8, 16, 32, 64]; -var LANSITEC_CONTACT_BADGE_BANDS = ["AU915", "IN865", "CN470", "AS923", "EU433", "EU868", "US915"]; -// 4.5.1 BT bits 10-8 and 4.5.2 BTSTATUS bits 6-4: the Bluetooth transmitting power, dBm. 0 is not a power. -var LANSITEC_CONTACT_BADGE_BT_POWERS = [0, 4, 0, -4, -8, -12, -16, -20]; -// 4.5.2 BTSTATUS bits 11-8: the vibrator and buzzer. -var LANSITEC_CONTACT_BADGE_BUZZER = ["No action", "Vibrator disabled, buzzer muted (restore)", - "Vibrator disabled, buzzer half sound level", "Vibrator disabled, buzzer full sound level", - "Vibrator enabled, buzzer muted", "Vibrator enabled, buzzer half sound level", - "Vibrator enabled, buzzer full sound level", "Vibrator disabled, buzzer muted (no restore)", - "Beacon-in-tracker and proximity detection disabled"]; - -// 4.5.5 TimeMark: bit n set means seen n * 10 to n * 10 + 9 seconds before the message. Seen time is 10 s per bit. +// Bluetooth Badge 4.5.5 TimeMark: bit n set means seen n * 10 to n * 10 + 9 s before the message, 10 s per bit. function lansitecContactBadgeSeen(item, mark) { item.timeMark = mark; var bits = 0; @@ -492,6 +549,7 @@ function lansitecContactBadgeSeen(item, mark) { item.seenTime = bits * 10; } +// LoRaWAN Bluetooth Badge 2.78, the contact tracing badge. Major and Minor are hex (a badge's: the end of its DevEUI). function lansitecContactBadgeValues(b) { var values = {}; if (b.length == 0) { @@ -500,21 +558,8 @@ function lansitecContactBadgeValues(b) { var type = (b[0] >> 4) & 0x0F; var hex = bytesToHex(b); if (type == 0x1 && b.length >= 9) { - // 4.5.1 Register: Type (ADR, Mode), SMODE, Power, CFG, POS (2), HB, BT (2), CRC (2). - values.adr = (b[0] & 0x08) != 0 ? "ON" : "OFF"; - var mode = b[0] & 0x07; - if (LANSITEC_CONTACT_BADGE_MODES[mode] != "") { - values.mode = LANSITEC_CONTACT_BADGE_MODES[mode]; - } - var band = LANSITEC_CONTACT_BADGE_SMODES.indexOf(b[1] & 0xFF); - if (band >= 0) { - values.loraWanFrequencyBand = LANSITEC_CONTACT_BADGE_BANDS[band]; - } - values.loraTxPower = (b[2] >> 3) & 0x1F; - values.dataRate = "DR" + ((b[3] >> 4) & 0x0F); - // POS unit 5 s, 0 means off. HB unit 30 s. - values.positionReportInterval = parseBytesToInt(b, 4, 2, true) * 5; - values.heartbeatInterval = (b[6] & 0xFF) * 30; + // 4.5.1 Register: ..., HB, BT (2), CRC (2). The receiving threshold setting has no unit in the datasheet. + lansitecRegister(values, b, 6); var bt = parseBytesToInt(b, 7, 2, true); values.buzzerAndVibrator = ((bt >> 11) & 1) == 1 ? "ON" : "OFF"; var power = (bt >> 8) & 0x07; @@ -522,30 +567,18 @@ function lansitecContactBadgeValues(b) { values.bluetoothTxPower = LANSITEC_CONTACT_BADGE_BT_POWERS[power]; } values.proximityDetection = ((bt >> 7) & 1) == 1 ? "ON" : "OFF"; - // 0x0-57, the datasheet gives no unit. values.bluetoothReceivingThresholdSetting = bt & 0x7F; } else if (type == 0x2 && b.length >= 7) { - // 4.5.2 Heartbeat: Type, VOL (battery level, %), RSSI (unit -1 dBm), SNR (2, signed, 0.01 dB), REV (software - // revision), CHGSTAT, CRC (2), BTSTATUS (2), then the friend badges. + // 4.5.2 Heartbeat: ..., CRC (2), BTSTATUS (2), then the friend badges. values.batteryLevel = b[1] & 0xFF; - values.loraRssi = 0 - (b[2] & 0xFF); - values.loraSnr = lansitecSnr(b, 3); - values.deviceFirmwareVersion = hex.substring(10, 12); - var charge = b[6] & 0xFF; - if (charge == 0x0) { - values.chargeState = "Not charging"; - } else if (charge == 0x5) { - values.chargeState = "Charging"; - } else if (charge == 0x6) { - values.chargeState = "Charging completed"; - } + lansitecHeartbeat(values, b, 0x5, 0x6); if (b.length >= 11) { var status = parseBytesToInt(b, 9, 2, true); var buzzer = (status >> 8) & 0x0F; if (buzzer < LANSITEC_CONTACT_BADGE_BUZZER.size()) { values.vibratorBuzzerStatus = LANSITEC_CONTACT_BADGE_BUZZER[buzzer]; } - // 0 means not changed. The receiving threshold table is for range bit 7 = 0: -73 to -87 dBm. + // 0 means not changed. The threshold is for range bit 7 = 0: -73 to -87 dBm. var statusPower = (status >> 4) & 0x07; if (statusPower > 0) { values.bluetoothTxPower = LANSITEC_CONTACT_BADGE_BT_POWERS[statusPower]; @@ -555,7 +588,7 @@ function lansitecContactBadgeValues(b) { values.bluetoothReceivingThreshold = -72 - threshold; } } - // Friend field: Type 0xC with the number of friend badges (1-6), then Major (2) and Minor (2) of each. + // Friend field: Type 0xC with the number of friend badges, then Major (2) and Minor (2) of each. if (b.length >= 12 && ((b[11] >> 4) & 0x0F) == 0xC) { var friendCount = b[11] & 0x0F; if (b.length >= 12 + friendCount * 4) { @@ -569,12 +602,11 @@ function lansitecContactBadgeValues(b) { } } } else if (type == 0x8) { - // 4.5.3 10s Instant Report: Type (bits 3-0: number of beacons M), Major (2), Minor (2) and RSSI of each, then - // TOFF: (M + 1) / 2 bytes, 4 bits per beacon, the first beacon in the high bits: seconds before the message. - // RSSI bit 7: 1 a Bluetooth beacon, 0 a badge (beacon-in-tracker); bits 6-0 - 128 is the RSSI, dBm. + // 4.5.3 10s Instant Report: Type (bits 3-0: M beacons), Major (2), Minor (2), RSSI of each, then TOFF: + // (M + 1) / 2 bytes, 4 bits per beacon, high bits first. RSSI bit 7: 1 a beacon, 0 a badge; bits 6-0 - 128. var count = b[0] & 0x0F; if (count > 0 && b.length >= 1 + count * 5) { - var hasOffsets = b.length >= 1 + count * 5 + (count + 1) / 2; + var hasOffsets = b.length >= 1 + count * 5 + ((count + 1) / 2).intValue(); var list = []; for (var i = 0; i < count; i++) { var start = 1 + i * 5; @@ -593,8 +625,8 @@ function lansitecContactBadgeValues(b) { values.beacons = list; } } else if (type == 0xC) { - // 4.5.5 80s Periodic Report: Type (bits 3-0: number of badges and beacons), badges as Major (2), Minor (2), - // TimeMark, then the marker 0xFFFF and beacons as Minor (2), TimeMark. A beacon's Major is always FFFF. + // 4.5.5 80s Periodic Report: badges as Major (2), Minor (2), TimeMark, then 0xFFFF and beacons as Minor (2), + // TimeMark (a beacon's Major is FFFF). var badges = []; var periodBeacons = []; var pos = 1; @@ -622,73 +654,9 @@ function lansitecContactBadgeValues(b) { values.beacons = periodBeacons; } } else if (type == 0xF && b.length >= 2) { - // 4.5.4 Acknowledge: Type bits 3-0 the result, then the MSGID of the downlink. - values.result = (b[0] & 0x0F) == 0 ? "Success" : "Failure"; - values.msgid = b[1] & 0xFF; + lansitecAcknowledgment(values, b); } return values; } -var values = {}; -var attributes = {}; -var history = []; -if (metadata.brandId == "lansitec" && (metadata.modelId == LANSITEC_SOCKET_SYNC || metadata.modelId == LANSITEC_CONTACT_BADGE) - && metadata.data != null) { - // The SocketSync gateway and the contact tracing badge: decoded from the raw bytes only, see - // lansitecSocketSyncValues and lansitecContactBadgeValues. - var lansitecBytes = base64ToBytes(metadata.data); - var registerType = 0x1; - if (metadata.modelId == LANSITEC_SOCKET_SYNC) { - values = lansitecSocketSyncValues(lansitecBytes); - registerType = 0x4; - } else { - values = lansitecContactBadgeValues(lansitecBytes); - } - // Registration holds the device settings: attributes, not telemetry. - if (lansitecBytes.length > 0 && ((lansitecBytes[0] >> 4) & 0x0F) == registerType) { - attributes = values; - values = {}; - } -} else if (metadata.payloadFormat == "JSON") { - values = decodeToJson(payload); - if (metadata.brandId == "lansitec" && isMap(values.bytes)) { - // The Lansitec codecs put everything in "bytes" and get some fields wrong: see the Lansitec block above. - var raw = values.bytes; - values = lansitecValues(raw, metadata.modelId); - if (metadata.data != null) { - lansitecRawValues(values, base64ToBytes(metadata.data), metadata.modelId); - } - // Register is sent after join and holds the device settings: they are attributes, not telemetry. - var messageType = raw.type != null ? raw.type : raw["Message type"]; - if (messageType == "Register") { - attributes = values; - values = {}; - } - } else if (values.history instanceof java.util.List) { - foreach (record : values.history) { - if (record.timestamp != null) { - var ts = record.timestamp * 1000; - record.remove("timestamp"); - history.add({ ts: ts, values: record }); - } - } - values.remove("history"); - } -} -var profile = "LoRaWAN Device"; -if (metadata.brandId != null && metadata.modelId != null) { - profile = metadata.brandId + "-" + metadata.modelId; - // The profile name chosen at install, kept in the integration metadata - var customProfile = metadata["profile." + profile]; - if (customProfile != null && customProfile != "") { - profile = customProfile; - } -} -if (!attributes.isEmpty()) { - return { profile: profile, attributes: attributes }; -} -if (history.isEmpty()) { - return { profile: profile, telemetry: { ts: metadata.ts, values: values } }; -} -history.add({ ts: metadata.ts, values: values }); -return { profile: profile, telemetry: history }; +return decodeThingsStackUplink(payload, metadata); diff --git a/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw b/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw index a3f160c3dc..be3d8f9098 100644 --- a/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw +++ b/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw @@ -2,38 +2,69 @@ // is then that JSON: its keys become telemetry. A Milesight-style history list keeps each record's time. // Such a device gets the profile -, so all devices of one model share it, // or the name the integration metadata gives it under profile.-. Any other device gets LoRaWAN Device. +// Lansitec devices are decoded by the Lansitec block below. +function decodeThingsStackUplink(payload, metadata) { + var values = {}; + var attributes = {}; + var history = []; + var lansitec = null; + if (metadata.brandId == "lansitec") { + lansitec = lansitecUplink(payload, metadata); + } + if (lansitec != null) { + values = lansitec.telemetry; + attributes = lansitec.attributes; + } else if (metadata.payloadFormat == "JSON") { + values = decodeToJson(payload); + if (values.history instanceof java.util.List) { + foreach (record : values.history) { + if (record.timestamp != null) { + var ts = record.timestamp * 1000; + record.remove("timestamp"); + history.add({ ts: ts, values: record }); + } + } + values.remove("history"); + } + } + var profile = "LoRaWAN Device"; + if (metadata.brandId != null && metadata.modelId != null) { + profile = metadata.brandId + "-" + metadata.modelId; + // The profile name chosen at install, kept in the integration metadata + var customProfile = metadata["profile." + profile]; + if (customProfile != null && customProfile != "") { + profile = customProfile; + } + } + if (!attributes.isEmpty()) { + return { profile: profile, attributes: attributes }; + } + if (history.isEmpty()) { + return { profile: profile, telemetry: { ts: metadata.ts, values: values } }; + } + history.add({ ts: metadata.ts, values: values }); + return { profile: profile, telemetry: history }; +} -// Lansitec: the LoRaWAN Device Repository codecs wrap everything in "bytes", write numbers as strings with units -// ("85%", "-67dBm") and get some fields wrong. Key names follow the Lansitec datasheets where there are any: -// Bluetooth Gateway DataSheet 2.27, Badge Tracker DataSheet v5.30, Temperature Humidity Sensor DataSheet 1.26, -// Plug Bluetooth Gateway DataSheet 1.5 (SocketSync gateway), -// LoRaWAN Bluetooth Badge DataSheet 2.78 (contact tracing badge). -// The wrong fields are decoded from the raw bytes (metadata.data). Remove a fix when the Repository fixes the codec. +// --------------------------------------------------------------------------------------------------------------------- +// Lansitec. The Repository codecs wrap everything in "bytes", write numbers with units ("85%", "-67dBm") and get some +// fields wrong: those are decoded from the raw bytes (metadata.data) by the datasheets. Remove a fix when the codec is fixed. +// Datasheets: Bluetooth Gateway 2.27, Badge Tracker v5.30, Temperature Humidity Sensor 1.26, Plug Bluetooth Gateway 1.5 +// (SocketSync), LoRaWAN Bluetooth Badge 2.78 (contact tracing badge), Container Tracker 0.72, Helmet Tracker v0.14. var LANSITEC_UNITS = ["dBm", "dB", "%", "V", "sec", "min", "s", "℃", "level", " meter"]; - var LANSITEC_GATEWAYS = ["compact-bluetooth-gateway", "indoor-bluetooth-gateway", "macro-bluetooth-gateway", "solar-bluetooth-gateway"]; +var LANSITEC_SOCKET_SYNC = "socket-sync-bluetooth-gateway"; +var LANSITEC_CONTACT_BADGE = "contact-tracing-badge"; -// Codec key -> datasheet name, per model group. "-" drops the key. -// -// Bluetooth Gateway DataSheet 2.27, 4.4.1 Registration: ADR, Mode (current working LoRaWAN frequency sub-band plan), -// LoRaWAN frequency band, Power (LoRa TX power, dBm), Continuously BLE receiving, DR (LoRaWAN Uplink Data Rate), -// PRI (Position report interval), HB (Heartbeat interval), Bluetooth receiving duration. -// 4.4.2 Heartbeat: RSSI and SNR (LoRa), VER (Firmware version). VOL, SNR and CHGSTAT come from the raw bytes. -// number is the count of reported devices: the beacons list has them. The beacons come from the raw bytes too. +// Codec key -> datasheet name. "-" drops the key: it is decoded from the raw bytes or is not a value. +// Bluetooth Gateway 4.4.1 Registration, 4.4.2 Heartbeat. var LANSITEC_GATEWAY_KEYS = { adr: "adr", mode: "mode", LoRaWANband: "loraWanFrequencyBand", LoRaTxpower: "loraTxPower", ContinuousReceive: "continuousBleReceiving", dr: "dataRate", pos: "positionReportInterval", hb: "heartbeatInterval", BLEReceivingDuration: "bluetoothReceivingDuration", - rssi: "loraRssi", snr: "-", ver: "deviceFirmwareVersion", vol: "-", chgstate: "-", number: "-" + rssi: "loraRssi", snr: "-", ver: "-", vol: "-", chgstate: "-", number: "-" }; - -// Badge Tracker DataSheet v5.30, 5.1 Registration: ADR, LoRa TX power, DR, GNSSEN (GNSS enable status), -// POSMODE (Positioning report mode), BLEPRI (Bluetooth Position Report Interval). The codec reads it without the -// GNSSPRI field, so heartbeat, version, CFMMSG, HBCOUNT and fall detection come from the raw bytes. -// 5.2 Heartbeat: Battery (percentage), RSSI (LoRa), GNSSSTATE (Status of the GNSS module), CHGSTATE (Status of -// battery charging). SNR and VIBSTATE come from the raw bytes. -// 5.3 GNSS Position: longitude, latitude and time come from the raw bytes. -// 5.4 Beacon: bytes 1-5 are RFU, the codec reads byte 1 as Vibstate. 5.5 Alarm: 0x01 SOS, 0x02 Fall Detection. +// Badge Tracker 5.1 Registration (the codec skips GNSSPRI), 5.2 Heartbeat, 5.3 GNSS Position, 5.4 Beacon, 5.5 Alarm. var LANSITEC_BADGE_KEYS = { ADR: "adr", "Lora Txpower": "loraTxPower", DR: "dataRate", GNSSEN: "gnssEnabled", "Position mode": "positioningReportMode", "Position period": "bluetoothPositionReportInterval", @@ -42,27 +73,70 @@ var LANSITEC_BADGE_KEYS = { "Move state": "-", "Charge state": "chargeState", Longitude: "-", Latitude: "-", Time: "-", Length: "-", Vibstate: "-", "Alarm type": "alarm" }; - -// Temperature Humidity Sensor DataSheet 1.26, 2.2.1 Register: ADR, MODE (current working scheme), SMODE (data -// scheme supported), POWER (configured transmit power, dBm), DR, REPTIND (report H&T if the value is the same; -// the codec gives "false" for 0 = always report), TH (period of temperature and humidity report), CRC. -// 2.2.2 Periodical temperature and humidity: VER is the version of the message type, not the firmware; -// VOL (battery left, %), RSSI (downlink, detected by the sensor). SNR, temperature and humidity come from the raw bytes. +// Temperature Humidity Sensor 2.2.1 Register, 2.2.2 Periodical data (VER is the message version, not the firmware). var LANSITEC_TEMPERATURE_HUMIDITY_KEYS = { adr: "adr", mode: "mode", smode: "smode", LoraTxpower: "loraTxPower", dr: "dataRate", repting: "reptind", th: "temperatureHumidityReportPeriod", crc: "-", ver: "-", vol: "batteryLevel", rssi: "loraRssi", snr: "-", temperature: "-", humidity: "-" }; - -// Without a datasheet only these keys are renamed: the integration writes rssi, snr and firmwareVersion itself. -// chgstate: the tracking label has the gateway codec byte for byte, which reads CHGSTAT 0x50 as "Not charging" -// (Bluetooth Gateway DataSheet 2.27, 4.4.2), so it is dropped. number is the count -// of reported devices: the beacons list has them. +// The other models: the integration writes rssi, snr and firmwareVersion itself. chgstate: the tracking label has the +// gateway codec, which misreads CHGSTAT (Bluetooth Gateway 4.4.2). number: the beacons list has them. var LANSITEC_CLASH_KEYS = { rssi: "loraRssi", RSSI: "loraRssi", snr: "loraSnr", SNR: "loraSnr", firmwareVersion: "deviceFirmwareVersion", "Firmware version": "deviceFirmwareVersion", type: "-", "Message type": "-", chgstate: "-", number: "-" }; +// Beacon keys: Devicetype1data1, Device1data, dev1major, dev1.major, pos1minor, asset1rssi... +var LANSITEC_BEACON_PREFIXES = ["devicetype1", "devicetype2", "devicetype3", "device", "dev", "pos", "asset"]; +var LANSITEC_BEACON_FIELDS = ["data", "major", "minor", "rssi", "id"]; +// Register Type bits 2-0 and SMODE: the LoRa frequency plan (Plug Bluetooth Gateway 5.1, Bluetooth Badge 4.5.1). +var LANSITEC_MODES = ["", "AU915", "IN865", "CN470", "AS923", "EU433", "EU868", "US915"]; +var LANSITEC_SMODES = [1, 2, 4, 8, 16, 32, 64]; +var LANSITEC_BANDS = ["AU915", "IN865", "CN470", "AS923", "EU433", "EU868", "US915"]; +// Badge Tracker 5.2 Heartbeat, as the codec writes them. +var LANSITEC_BADGE_GNSS_STATES = ["GPS OFF", "GPS boot", "GPS locating", "GPS located", "", "", "", "", "", "GPS no signal"]; +var LANSITEC_BADGE_CHARGE_STATES = ["Power cable disconnected", "", "", "", "", "Charging", "Charge complete"]; +// Plug Bluetooth Gateway 5.1 CFG bits 3-1. +var LANSITEC_SOCKET_SYNC_TYPES = ["Outdoor gateway", "Indoor gateway", "Plug gateway"]; +// Bluetooth Badge 4.5.1 BT bits 10-8, 4.5.2 BTSTATUS bits 6-4 (dBm, 0 is not a power) and bits 11-8. +var LANSITEC_CONTACT_BADGE_BT_POWERS = [0, 4, 0, -4, -8, -12, -16, -20]; +var LANSITEC_CONTACT_BADGE_BUZZER = ["No action", "Vibrator disabled, buzzer muted (restore)", + "Vibrator disabled, buzzer half sound level", "Vibrator disabled, buzzer full sound level", + "Vibrator enabled, buzzer muted", "Vibrator enabled, buzzer half sound level", + "Vibrator enabled, buzzer full sound level", "Vibrator disabled, buzzer muted (no restore)", + "Beacon-in-tracker and proximity detection disabled"]; + +// { telemetry, attributes }, or null when the uplink is not one the Lansitec block decodes. +function lansitecUplink(payload, metadata) { + var model = metadata.modelId; + var values = null; + var register = false; + if ((model == LANSITEC_SOCKET_SYNC || model == LANSITEC_CONTACT_BADGE) && metadata.data != null) { + // Their codecs are the gateway's and misread every message: decoded from the raw bytes only. + var b = base64ToBytes(metadata.data); + values = model == LANSITEC_SOCKET_SYNC ? lansitecSocketSyncValues(b) : lansitecContactBadgeValues(b); + register = b.length > 0 && ((b[0] >> 4) & 0x0F) == (model == LANSITEC_SOCKET_SYNC ? 0x4 : 0x1); + } else if (metadata.payloadFormat == "JSON") { + var json = decodeToJson(payload); + if (!isMap(json.bytes)) { + return null; + } + var raw = json.bytes; + values = lansitecValues(raw, model); + if (metadata.data != null) { + lansitecRawValues(values, base64ToBytes(metadata.data), model); + } + var messageType = raw.type != null ? raw.type : raw["Message type"]; + register = messageType == "Register"; + } else { + return null; + } + // Register holds the device settings: attributes, not telemetry. + if (register) { + return { telemetry: {}, attributes: values }; + } + return { telemetry: values, attributes: {} }; +} // Strips the unit. Periods become seconds: the codecs give most of them in seconds, a few in minutes. function lansitecNumber(v) { @@ -104,10 +178,6 @@ function lansitecCamelCase(key) { return result; } -// Beacon keys: Devicetype1data1, Device1data, dev1major, dev1.major, pos1minor, asset1rssi... -var LANSITEC_BEACON_PREFIXES = ["devicetype1", "devicetype2", "devicetype3", "device", "dev", "pos", "asset"]; -var LANSITEC_BEACON_FIELDS = ["data", "major", "minor", "rssi", "id"]; - function lansitecBeaconKey(key) { var k = key.toLowerCase().replace(".", ""); foreach (p : LANSITEC_BEACON_PREFIXES) { @@ -156,11 +226,13 @@ function lansitecValues(raw, model) { } else if (b.field == "data" || b.field == "major" || b.field == "minor") { beacons[id][b.field] = value; } - } else if (table == null && key == "time" && value instanceof String) { - // Container tracker and helmet sensor, no datasheet: their codecs add 8 hours to the GNSS time and wrap - // it in quotes, as the badge tracker codec does with the UTC time of DataSheet v5.30, 5.3. + } else if (table == null && key.matches("time[1-8]?") && value instanceof String) { + // Container Tracker 5.3 and 5.7, Helmet Tracker 6.3: UTC time. The codecs add 8 hours and quote it. var gnssDate = new Date(value.replace("\"", "")); - values.gnssTime = gnssDate.getTime() - 8 * 60 * 60 * 1000; + values["gnssTime" + key.substring(4)] = gnssDate.getTime() - 8 * 60 * 60 * 1000; + } else if (key == "pressure" && model == "helmet-sensor" && !(value instanceof String)) { + // Helmet Tracker 6.3: the value * 10, unit 0.1 Pa. + values.pressure = value / 10.0; } else if (table != null && table.containsKey(key)) { var tableName = table[key]; if (tableName != "-") { @@ -182,28 +254,31 @@ function lansitecValues(raw, model) { return values; } +// In the order of their numbers, which can have gaps (Container Tracker 5.7: dev1, dev3...). function lansitecBeaconList(beacons) { var list = []; - for (var i = 1; i <= beacons.size(); i++) { + var added = {}; + while (list.size() < beacons.size()) { + var next = null; foreach (id : beacons.keySet()) { - var b = beacons[id]; - if (b.order == i) { - var item = {}; - if (b.data != null) { - item.data = b.data; - } - if (b.major != null) { - item.major = b.major; - item.minor = b.minor; - } - item.rssi = b.rssi; - list.add(item); + if (added[id] == null && (next == null || beacons[id].order < beacons[next].order)) { + next = id; } } + added[next] = true; + var b = beacons[next]; + var item = {}; + if (b.data != null) { + item.data = b.data; + } + if (b.major != null) { + item.major = b.major; + item.minor = b.minor; + } + item.rssi = b.rssi; + list.add(item); } - // The gateway codec reads the data of every device after the first at a 5-byte step, so it is right only - // for 4-byte data (DataSheet 4.4.4 examples): with longer data only the first device is kept. The gateways - // with a datasheet get their beacons from the raw bytes instead; this is for the models with the gateway codec. + // The gateway codec reads every device after the first at a 5-byte step: right only for 4-byte data. if (list.size() > 1 && list[0].data != null && list[0].data.length() != 8) { var first = []; first.add(list[0]); @@ -212,7 +287,6 @@ function lansitecBeaconList(beacons) { return list; } -// Decoded from the raw bytes by the datasheet where the codec gets it wrong. function lansitecRawValues(values, b, model) { if (b.length == 0) { return; @@ -232,7 +306,7 @@ function lansitecFloatIsNumber(b, offset) { return !((b[offset] & 0x7F) == 0x7F && (b[offset + 1] & 0x80) != 0); } -// SNR, unit 0.01 dB, -3000 to 3000: signed. The codecs read it unsigned. +// SNR, unit 0.01 dB, signed. The codecs read it unsigned. function lansitecSnr(b, offset) { var v = parseBytesToInt(b, offset, 2, true); if (v > 32767) { @@ -241,31 +315,70 @@ function lansitecSnr(b, offset) { return v / 100.0; } -// Bluetooth Gateway DataSheet 2.27. +// CHGSTAT: 0 Not charging, then the codes of Charging and Charging completed. +function lansitecChargeState(values, charge, charging, completed) { + if (charge == 0) { + values.chargeState = "Not charging"; + } else if (charge == charging) { + values.chargeState = "Charging"; + } else if (charge == completed) { + values.chargeState = "Charging completed"; + } +} + +// Register of the SocketSync gateway (5.1) and the contact tracing badge (4.5.1): Type (ADR, MODE), SMODE, POWER, +// CFG, POS (2, unit 5 s), then HB (unit 30 s) at hbOffset. +function lansitecRegister(values, b, hbOffset) { + values.adr = (b[0] & 0x08) != 0 ? "ON" : "OFF"; + var mode = b[0] & 0x07; + if (LANSITEC_MODES[mode] != "") { + values.mode = LANSITEC_MODES[mode]; + } + var band = LANSITEC_SMODES.indexOf(b[1] & 0xFF); + if (band >= 0) { + values.loraWanFrequencyBand = LANSITEC_BANDS[band]; + } + values.loraTxPower = (b[2] >> 3) & 0x1F; + values.dataRate = "DR" + ((b[3] >> 4) & 0x0F); + values.positionReportInterval = parseBytesToInt(b, 4, 2, true) * 5; + values.heartbeatInterval = (b[hbOffset] & 0xFF) * 30; +} + +// Heartbeat of the SocketSync gateway (5.2) and the contact tracing badge (4.5.2): Type, VOL, RSSI (unit -1 dBm), +// SNR (2), REV, CHGSTAT (0x00, 0x50, 0x60 and 0x0, 0x5, 0x6). +function lansitecHeartbeat(values, b, charging, completed) { + values.loraRssi = 0 - (b[2] & 0xFF); + values.loraSnr = lansitecSnr(b, 3); + values.deviceFirmwareVersion = bytesToHex(b).substring(10, 12); + lansitecChargeState(values, b[6] & 0xFF, charging, completed); +} + +// Acknowledgment (Plug Bluetooth Gateway 5.5, Bluetooth Badge 4.5.4): Type bits 3-0 the result, then the MSGID. +function lansitecAcknowledgment(values, b) { + values.result = (b[0] & 0x0F) == 0 ? "Success" : "Failure"; + values.msgid = b[1] & 0xFF; +} + +// Bluetooth Gateway 2.27. function lansitecGatewayRawValues(values, b, model, type) { if (type == 1 && b.length >= 9) { - // 4.4.1: Network reconnection interval, unit 5 minutes. The codec does not decode it. + // 4.4.1: Network reconnection interval, unit 5 minutes. values.networkReconnectionInterval = (b[8] & 0xFF) * 300; } else if (type == 2 && b.length >= 8) { - // 4.4.2: VOL is the battery voltage (VOL x 0.01 + 1.5 V) on the macro gateway and the battery level (%) on - // the others. The codec decides by the value (<= 100 is %), not by the model. + // 4.4.2: VOL is the battery voltage (x 0.01 + 1.5 V) on the macro gateway, the battery level (%) on the others. if (model == "macro-bluetooth-gateway") { values.batteryVoltage = ((b[1] & 0xFF) + 150) / 100.0; } else { values.batteryLevel = b[1] & 0xFF; } values.loraSnr = lansitecSnr(b, 3); - // CHGSTAT: 0x00 Not charging, 0x50 Charging, 0x60 Charging completed on the compact and solar gateway, - // always 0x00 on the macro and indoor gateway. The codec reads the low half of the byte. + // VER 108 is V1.08. + var version = parseBytesToInt(b, 5, 2, true); + var minor = (version % 100).intValue(); + values.deviceFirmwareVersion = "V" + (version / 100).intValue() + "." + (minor < 10 ? "0" : "") + minor; + // CHGSTAT on the compact and solar gateway only. if (model == "compact-bluetooth-gateway" || model == "solar-bluetooth-gateway") { - var charge = b[7] & 0xFF; - if (charge == 0x00) { - values.chargeState = "Not charging"; - } else if (charge == 0x50) { - values.chargeState = "Charging"; - } else if (charge == 0x60) { - values.chargeState = "Charging completed"; - } + lansitecChargeState(values, b[7] & 0xFF, 0x50, 0x60); } } else if (type == 0x8 || type == 0x9 || type == 0xA || type == 0xE) { values.remove("beacons"); @@ -276,12 +389,8 @@ function lansitecGatewayRawValues(values, b, model, type) { } } -// 4.4.4-4.4.6: Device Type 1, 2 and 3 messages: Type (bits 3-0: number of devices), then Data (1-31 bytes) and -// RSSI (value - 256, dBm) of each device. Data length is not in the message: it is set by the device report rule -// of the type (4.4.3), so all devices of one message have the same length. The codec reads every device after -// the first at a 5-byte step. -// 4.4.7: Multi-Device Type message: Device Type ID before the data of each device. The data length can differ per -// type, and the message does not say it: decoded only when all devices fit the same length. +// 4.4.4-4.4.6: Type (bits 3-0: number of devices), Data and RSSI (value - 256) of each device; one data length per +// message (4.4.3). 4.4.7 Multi-Device: a Device Type ID before each device, decoded when all have the same length. function lansitecGatewayBeacons(b, type) { var count = b[0] & 0x0F; if (count == 0 || (b.length - 1) % count != 0) { @@ -314,15 +423,12 @@ function lansitecGatewayBeacons(b, type) { return list; } -// Badge Tracker DataSheet v5.30. +// Badge Tracker v5.30. function lansitecBadgeRawValues(values, b, type) { if (type == 1 && b.length == 14) { - // 5.1, 14 bytes: Type, SMODE, Power, Config, BLEPRI (2), GNSSPRI (2), Heartbeat, Version (2), CFMMSG, HBCOUNT, Fall. - // BLEEN (Config bit 0) is not decoded: the field table says BLE enable status and that GNSSEN or BLEEN must - // be enabled, but the example (Config 32) has both 0 and calls bit 0 reserved. + // 5.1: GNSSPRI (2), Heartbeat, Version (2, 0x0525 is V5.25), CFMMSG, HBCOUNT, Fall (unit 0.5 m). values.gnssPositionReportInterval = parseBytesToInt(b, 6, 2, true) * 5; values.heartbeatInterval = (b[8] & 0xFF) * 30; - // Version 0x0525 is V5.25. var version = bytesToHex(b).substring(18, 22); var major = version.substring(0, 2); if (major.startsWith("0")) { @@ -331,19 +437,30 @@ function lansitecBadgeRawValues(values, b, type) { values.deviceFirmwareVersion = "V" + major + "." + version.substring(2); values.cfmmsg = b[11] & 0xFF; values.hbcount = b[12] & 0xFF; - // Fall detection threshold, unit 0.5 m, 0 means off. values.fallDetectionThreshold = (b[13] & 0xFF) * 0.5; } else if (type == 2 && b.length >= 6) { - // 5.2: the SNR field is there since firmware 0x0501. The SNREN bit (Type bits 3-0) is not used: the - // datasheet example 20 64 3E 033E 00 60 00 has SNREN 0 and the SNR 8.3 dB. - values.loraSnr = lansitecSnr(b, 3); - // VIBSTATE: moving state 0-9, 0 means no moving, 9 high acceleration. The codec turns only 0 and 1 into text. - values.movingState = b[5] & 0x0F; + // 5.2: SNR (2) since firmware 0x0501, decided by the length (SNREN is not reliable). Then GNSSSTAT and + // VIBSTATE (moving state 0-9), CHGSTAT. The codec always reads the SNR. + var state = 3; + if (b.length >= 8) { + values.loraSnr = lansitecSnr(b, 3); + state = 5; + } else { + var gnss = (b[3] >> 4) & 0x0F; + var charge = (b[4] >> 4) & 0x0F; + values.remove("gnssState"); + values.remove("chargeState"); + if (gnss < LANSITEC_BADGE_GNSS_STATES.size() && LANSITEC_BADGE_GNSS_STATES[gnss] != "") { + values.gnssState = LANSITEC_BADGE_GNSS_STATES[gnss]; + } + if (charge < LANSITEC_BADGE_CHARGE_STATES.size() && LANSITEC_BADGE_CHARGE_STATES[charge] != "") { + values.chargeState = LANSITEC_BADGE_CHARGE_STATES[charge]; + } + } + values.movingState = b[state] & 0x0F; } else if (type == 3 && b.length >= 13) { - // 5.3: Flag (Type bits 3-0) 0x1 means GNSS positioning failed. Longitude and latitude are IEEE 754 floats - // (the codec divides the mantissa by 0x7fffff instead of 2^23), time is UTC seconds (the codec adds 8 hours). - // Rounded to 6 decimals (about 0.1 m), as a double: the float has no more precision. - // An exponent of 0xFF is not a number (NaN or infinity): parseBytesToFloat fails on NaN, so no position. + // 5.3: Flag 0x1 is a failed fix. IEEE 754 floats (the codec divides by 0x7fffff) rounded to 6 decimals, + // UTC seconds (the codec adds 8 hours). No position from NaN: parseBytesToFloat fails on it. if ((b[0] & 0x0F) == 0 && lansitecFloatIsNumber(b, 1) && lansitecFloatIsNumber(b, 5)) { values.longitude = toFixed(parseBytesToFloat(b, 1, 4, true) * 1.0, 6); values.latitude = toFixed(parseBytesToFloat(b, 5, 4, true) * 1.0, 6); @@ -352,9 +469,7 @@ function lansitecBadgeRawValues(values, b, type) { } } -// Temperature Humidity Sensor DataSheet 1.26, 2.2.2: VER (Type bits 3-0) 1 means the SNR field is there, 0 means -// it is not and the message is 8 bytes. The codec always reads the SNR, so with VER 0 the next fields are shifted. -// TEMP_INT bit 7 is the sign, TEMP_FRA the hundredths. +// Temperature Humidity Sensor 2.2.2: VER 1 has the SNR, VER 0 not (the codec always reads it). TEMP_INT bit 7 is the sign. function lansitecTemperatureHumidityRawValues(values, b) { var offset = (b[0] & 0x0F) == 1 ? 5 : 3; if (b.length < offset + 3) { @@ -371,20 +486,7 @@ function lansitecTemperatureHumidityRawValues(values, b) { values.humidity = b[offset + 2] & 0xFF; } -// Plug Bluetooth Gateway DataSheet 1.5 (990-01203), the SocketSync gateway. Its codec is the compact gateway's, but -// the messages are not: the codec gives nothing for the Registration (0x4) and the Beacon message (0x6), so The -// Things Stack sends them without decoded_payload, and it reads the Heartbeat and the Alarm Beacon List (0xE) with -// the compact gateway's layout. So every message is decoded from the raw bytes, with or without decoded_payload. -var LANSITEC_SOCKET_SYNC = "socket-sync-bluetooth-gateway"; -// 5.1 Type bits 2-0 (MODE) and SMODE: the LoRa frequency plan. -var LANSITEC_SOCKET_SYNC_MODES = ["", "AU915", "", "CN470", "AS923", "EU433", "EU868", "US915"]; -var LANSITEC_SOCKET_SYNC_SMODES = [1, 4, 8, 16, 32, 64]; -var LANSITEC_SOCKET_SYNC_BANDS = ["AU915", "CN470", "AS923", "EU433", "EU868", "US915"]; -// 5.1 CFG bits 3-1: the gateway type. -var LANSITEC_SOCKET_SYNC_TYPES = ["Outdoor gateway", "Indoor gateway", "Plug gateway"]; -// Table 7: UUID List. A beacon gives the last byte of its UUID, the other bytes are the same for all of them. -var LANSITEC_SOCKET_SYNC_UUID = "5C38DBDE567C4CCAB1DA40A8AD4656"; - +// Plug Bluetooth Gateway 1.5, the SocketSync gateway. function lansitecSocketSyncValues(b) { var values = {}; if (b.length == 0) { @@ -392,49 +494,24 @@ function lansitecSocketSyncValues(b) { } var type = (b[0] >> 4) & 0x0F; if (type == 0x4 && b.length >= 10) { - // 5.1 Registration: Type (ADR, MODE), SMODE, POWER, CFG, POS (2), BLE Receiving Duration, HB, Beacon_QTY, CRC (2). - values.adr = (b[0] & 0x08) != 0 ? "ON" : "OFF"; - var mode = b[0] & 0x07; - if (LANSITEC_SOCKET_SYNC_MODES[mode] != "") { - values.mode = LANSITEC_SOCKET_SYNC_MODES[mode]; - } - var band = LANSITEC_SOCKET_SYNC_SMODES.indexOf(b[1] & 0xFF); - if (band >= 0) { - values.loraWanFrequencyBand = LANSITEC_SOCKET_SYNC_BANDS[band]; - } - values.loraTxPower = (b[2] >> 3) & 0x1F; + // 5.1 Registration: ..., BLE Receiving Duration, HB, Beacon_QTY (half of the limit), CRC (2). + lansitecRegister(values, b, 7); values.continuousBleReceiving = (b[2] & 0x02) != 0 ? "Continuous Bluetooth receiving is enabled" : "Continuous Bluetooth receiving is disabled"; values.powerDownFlag = (b[2] & 0x01) != 0 ? "Automatic shutdown status" : "Normal status"; - values.dataRate = "DR" + ((b[3] >> 4) & 0x0F); var gatewayType = (b[3] >> 1) & 0x07; if (gatewayType < LANSITEC_SOCKET_SYNC_TYPES.size()) { values.gatewayType = LANSITEC_SOCKET_SYNC_TYPES[gatewayType]; } values.rssiSortingMethod = (b[3] & 0x01) != 0 ? "Sort by the maximum value" : "Sort by average"; - // POS unit 5 s, 0 means off. HB unit 30 s. Beacon_QTY is half of the limit. - values.positionReportInterval = parseBytesToInt(b, 4, 2, true) * 5; values.bluetoothReceivingDuration = b[6] & 0xFF; - values.heartbeatInterval = (b[7] & 0xFF) * 30; values.beaconQuantityLimit = (b[8] & 0xFF) * 2; } else if (type == 0x2 && b.length >= 7) { - // 5.2 Heartbeat: Type, VOL (battery level, %), RSSI (unit -1 dBm), SNR (2, signed, 0.01 dB), REV (firmware, - // hex), CHGSTAT, CRC (2). The codec reads REV and CHGSTAT as the compact gateway's 2-byte version, and the - // first CRC byte as CHGSTAT. VOL keeps its name: the dashboard of the IoT Hub package reads it. + // 5.2 Heartbeat. VOL (%) keeps its name: the dashboard of the IoT Hub package reads it. values.vol = b[1] & 0xFF; - values.loraRssi = 0 - (b[2] & 0xFF); - values.loraSnr = lansitecSnr(b, 3); - values.deviceFirmwareVersion = bytesToHex(b).substring(10, 12); - var charge = b[6] & 0xFF; - if (charge == 0x00) { - values.chargeState = "Not charging"; - } else if (charge == 0x50) { - values.chargeState = "Charging"; - } else if (charge == 0x60) { - values.chargeState = "Charging completed"; - } + lansitecHeartbeat(values, b, 0x50, 0x60); } else if (type == 0x6 || type == 0xE) { - // 5.3 Beacon and 5.4 Alarm Beacon List: Type (bits 3-0: number of beacons), then UUID tail number, Major (2), - // Minor (2) and RSSI (value - 256, dBm) of each beacon. The alarm list holds the beacons that trigger the alarm. + // 5.3 Beacon, 5.4 Alarm Beacon List: Type (bits 3-0: number of beacons), then UUID tail, Major (2), Minor (2), + // RSSI (value - 256) of each. Table 7: the UUID prefix ends with 56 for tails 0x56-0xFF, 57 for 0x00-0x1E. var count = b[0] & 0x0F; if (count > 0 && b.length >= 1 + count * 6) { var hex = bytesToHex(b); @@ -442,7 +519,7 @@ function lansitecSocketSyncValues(b) { for (var i = 0; i < count; i++) { var start = 1 + i * 6; var item = {}; - item.uuid = LANSITEC_SOCKET_SYNC_UUID + hex.substring(2 * start, 2 * start + 2); + item.uuid = "5C38DBDE567C4CCAB1DA40A8AD465" + ((b[start] & 0xFF) < 0x56 ? "7" : "6") + hex.substring(2 * start, 2 * start + 2); item.major = parseBytesToInt(b, start + 1, 2, true); item.minor = parseBytesToInt(b, start + 3, 2, true); item.rssi = (b[start + 5] & 0xFF) - 256; @@ -455,32 +532,12 @@ function lansitecSocketSyncValues(b) { } } } else if (type == 0xF && b.length >= 2) { - // 5.5 Acknowledgment: Type bits 3-0 the result, then the MSGID of the downlink. - values.result = (b[0] & 0x0F) == 0 ? "Success" : "Failure"; - values.msgid = b[1] & 0xFF; + lansitecAcknowledgment(values, b); } return values; } -// LoRaWAN Bluetooth Badge DataSheet 2.78 (990-01242), the contact tracing badge: it reports the badges and the -// Bluetooth beacons near it, it has no GNSS. Its codec is the Bluetooth gateway's, which reads these messages with -// the gateway layout, so every message is decoded from the raw bytes, as for the SocketSync gateway. -// Major and Minor are hex: a badge's are the last 4 bytes of its DevEUI (4.4). -var LANSITEC_CONTACT_BADGE = "contact-tracing-badge"; -// 4.5.1 Type bits 2-0 (Mode) and SMODE: the LoRa frequency plan. -var LANSITEC_CONTACT_BADGE_MODES = ["", "AU915", "IN865", "CN470", "AS923", "EU433", "EU868", "US915"]; -var LANSITEC_CONTACT_BADGE_SMODES = [1, 2, 4, 8, 16, 32, 64]; -var LANSITEC_CONTACT_BADGE_BANDS = ["AU915", "IN865", "CN470", "AS923", "EU433", "EU868", "US915"]; -// 4.5.1 BT bits 10-8 and 4.5.2 BTSTATUS bits 6-4: the Bluetooth transmitting power, dBm. 0 is not a power. -var LANSITEC_CONTACT_BADGE_BT_POWERS = [0, 4, 0, -4, -8, -12, -16, -20]; -// 4.5.2 BTSTATUS bits 11-8: the vibrator and buzzer. -var LANSITEC_CONTACT_BADGE_BUZZER = ["No action", "Vibrator disabled, buzzer muted (restore)", - "Vibrator disabled, buzzer half sound level", "Vibrator disabled, buzzer full sound level", - "Vibrator enabled, buzzer muted", "Vibrator enabled, buzzer half sound level", - "Vibrator enabled, buzzer full sound level", "Vibrator disabled, buzzer muted (no restore)", - "Beacon-in-tracker and proximity detection disabled"]; - -// 4.5.5 TimeMark: bit n set means seen n * 10 to n * 10 + 9 seconds before the message. Seen time is 10 s per bit. +// Bluetooth Badge 4.5.5 TimeMark: bit n set means seen n * 10 to n * 10 + 9 s before the message, 10 s per bit. function lansitecContactBadgeSeen(item, mark) { item.timeMark = mark; var bits = 0; @@ -492,6 +549,7 @@ function lansitecContactBadgeSeen(item, mark) { item.seenTime = bits * 10; } +// LoRaWAN Bluetooth Badge 2.78, the contact tracing badge. Major and Minor are hex (a badge's: the end of its DevEUI). function lansitecContactBadgeValues(b) { var values = {}; if (b.length == 0) { @@ -500,21 +558,8 @@ function lansitecContactBadgeValues(b) { var type = (b[0] >> 4) & 0x0F; var hex = bytesToHex(b); if (type == 0x1 && b.length >= 9) { - // 4.5.1 Register: Type (ADR, Mode), SMODE, Power, CFG, POS (2), HB, BT (2), CRC (2). - values.adr = (b[0] & 0x08) != 0 ? "ON" : "OFF"; - var mode = b[0] & 0x07; - if (LANSITEC_CONTACT_BADGE_MODES[mode] != "") { - values.mode = LANSITEC_CONTACT_BADGE_MODES[mode]; - } - var band = LANSITEC_CONTACT_BADGE_SMODES.indexOf(b[1] & 0xFF); - if (band >= 0) { - values.loraWanFrequencyBand = LANSITEC_CONTACT_BADGE_BANDS[band]; - } - values.loraTxPower = (b[2] >> 3) & 0x1F; - values.dataRate = "DR" + ((b[3] >> 4) & 0x0F); - // POS unit 5 s, 0 means off. HB unit 30 s. - values.positionReportInterval = parseBytesToInt(b, 4, 2, true) * 5; - values.heartbeatInterval = (b[6] & 0xFF) * 30; + // 4.5.1 Register: ..., HB, BT (2), CRC (2). The receiving threshold setting has no unit in the datasheet. + lansitecRegister(values, b, 6); var bt = parseBytesToInt(b, 7, 2, true); values.buzzerAndVibrator = ((bt >> 11) & 1) == 1 ? "ON" : "OFF"; var power = (bt >> 8) & 0x07; @@ -522,30 +567,18 @@ function lansitecContactBadgeValues(b) { values.bluetoothTxPower = LANSITEC_CONTACT_BADGE_BT_POWERS[power]; } values.proximityDetection = ((bt >> 7) & 1) == 1 ? "ON" : "OFF"; - // 0x0-57, the datasheet gives no unit. values.bluetoothReceivingThresholdSetting = bt & 0x7F; } else if (type == 0x2 && b.length >= 7) { - // 4.5.2 Heartbeat: Type, VOL (battery level, %), RSSI (unit -1 dBm), SNR (2, signed, 0.01 dB), REV (software - // revision), CHGSTAT, CRC (2), BTSTATUS (2), then the friend badges. + // 4.5.2 Heartbeat: ..., CRC (2), BTSTATUS (2), then the friend badges. values.batteryLevel = b[1] & 0xFF; - values.loraRssi = 0 - (b[2] & 0xFF); - values.loraSnr = lansitecSnr(b, 3); - values.deviceFirmwareVersion = hex.substring(10, 12); - var charge = b[6] & 0xFF; - if (charge == 0x0) { - values.chargeState = "Not charging"; - } else if (charge == 0x5) { - values.chargeState = "Charging"; - } else if (charge == 0x6) { - values.chargeState = "Charging completed"; - } + lansitecHeartbeat(values, b, 0x5, 0x6); if (b.length >= 11) { var status = parseBytesToInt(b, 9, 2, true); var buzzer = (status >> 8) & 0x0F; if (buzzer < LANSITEC_CONTACT_BADGE_BUZZER.size()) { values.vibratorBuzzerStatus = LANSITEC_CONTACT_BADGE_BUZZER[buzzer]; } - // 0 means not changed. The receiving threshold table is for range bit 7 = 0: -73 to -87 dBm. + // 0 means not changed. The threshold is for range bit 7 = 0: -73 to -87 dBm. var statusPower = (status >> 4) & 0x07; if (statusPower > 0) { values.bluetoothTxPower = LANSITEC_CONTACT_BADGE_BT_POWERS[statusPower]; @@ -555,7 +588,7 @@ function lansitecContactBadgeValues(b) { values.bluetoothReceivingThreshold = -72 - threshold; } } - // Friend field: Type 0xC with the number of friend badges (1-6), then Major (2) and Minor (2) of each. + // Friend field: Type 0xC with the number of friend badges, then Major (2) and Minor (2) of each. if (b.length >= 12 && ((b[11] >> 4) & 0x0F) == 0xC) { var friendCount = b[11] & 0x0F; if (b.length >= 12 + friendCount * 4) { @@ -569,12 +602,11 @@ function lansitecContactBadgeValues(b) { } } } else if (type == 0x8) { - // 4.5.3 10s Instant Report: Type (bits 3-0: number of beacons M), Major (2), Minor (2) and RSSI of each, then - // TOFF: (M + 1) / 2 bytes, 4 bits per beacon, the first beacon in the high bits: seconds before the message. - // RSSI bit 7: 1 a Bluetooth beacon, 0 a badge (beacon-in-tracker); bits 6-0 - 128 is the RSSI, dBm. + // 4.5.3 10s Instant Report: Type (bits 3-0: M beacons), Major (2), Minor (2), RSSI of each, then TOFF: + // (M + 1) / 2 bytes, 4 bits per beacon, high bits first. RSSI bit 7: 1 a beacon, 0 a badge; bits 6-0 - 128. var count = b[0] & 0x0F; if (count > 0 && b.length >= 1 + count * 5) { - var hasOffsets = b.length >= 1 + count * 5 + (count + 1) / 2; + var hasOffsets = b.length >= 1 + count * 5 + ((count + 1) / 2).intValue(); var list = []; for (var i = 0; i < count; i++) { var start = 1 + i * 5; @@ -593,8 +625,8 @@ function lansitecContactBadgeValues(b) { values.beacons = list; } } else if (type == 0xC) { - // 4.5.5 80s Periodic Report: Type (bits 3-0: number of badges and beacons), badges as Major (2), Minor (2), - // TimeMark, then the marker 0xFFFF and beacons as Minor (2), TimeMark. A beacon's Major is always FFFF. + // 4.5.5 80s Periodic Report: badges as Major (2), Minor (2), TimeMark, then 0xFFFF and beacons as Minor (2), + // TimeMark (a beacon's Major is FFFF). var badges = []; var periodBeacons = []; var pos = 1; @@ -622,73 +654,9 @@ function lansitecContactBadgeValues(b) { values.beacons = periodBeacons; } } else if (type == 0xF && b.length >= 2) { - // 4.5.4 Acknowledge: Type bits 3-0 the result, then the MSGID of the downlink. - values.result = (b[0] & 0x0F) == 0 ? "Success" : "Failure"; - values.msgid = b[1] & 0xFF; + lansitecAcknowledgment(values, b); } return values; } -var values = {}; -var attributes = {}; -var history = []; -if (metadata.brandId == "lansitec" && (metadata.modelId == LANSITEC_SOCKET_SYNC || metadata.modelId == LANSITEC_CONTACT_BADGE) - && metadata.data != null) { - // The SocketSync gateway and the contact tracing badge: decoded from the raw bytes only, see - // lansitecSocketSyncValues and lansitecContactBadgeValues. - var lansitecBytes = base64ToBytes(metadata.data); - var registerType = 0x1; - if (metadata.modelId == LANSITEC_SOCKET_SYNC) { - values = lansitecSocketSyncValues(lansitecBytes); - registerType = 0x4; - } else { - values = lansitecContactBadgeValues(lansitecBytes); - } - // Registration holds the device settings: attributes, not telemetry. - if (lansitecBytes.length > 0 && ((lansitecBytes[0] >> 4) & 0x0F) == registerType) { - attributes = values; - values = {}; - } -} else if (metadata.payloadFormat == "JSON") { - values = decodeToJson(payload); - if (metadata.brandId == "lansitec" && isMap(values.bytes)) { - // The Lansitec codecs put everything in "bytes" and get some fields wrong: see the Lansitec block above. - var raw = values.bytes; - values = lansitecValues(raw, metadata.modelId); - if (metadata.data != null) { - lansitecRawValues(values, base64ToBytes(metadata.data), metadata.modelId); - } - // Register is sent after join and holds the device settings: they are attributes, not telemetry. - var messageType = raw.type != null ? raw.type : raw["Message type"]; - if (messageType == "Register") { - attributes = values; - values = {}; - } - } else if (values.history instanceof java.util.List) { - foreach (record : values.history) { - if (record.timestamp != null) { - var ts = record.timestamp * 1000; - record.remove("timestamp"); - history.add({ ts: ts, values: record }); - } - } - values.remove("history"); - } -} -var profile = "LoRaWAN Device"; -if (metadata.brandId != null && metadata.modelId != null) { - profile = metadata.brandId + "-" + metadata.modelId; - // The profile name chosen at install, kept in the integration metadata - var customProfile = metadata["profile." + profile]; - if (customProfile != null && customProfile != "") { - profile = customProfile; - } -} -if (!attributes.isEmpty()) { - return { profile: profile, attributes: attributes }; -} -if (history.isEmpty()) { - return { profile: profile, telemetry: { ts: metadata.ts, values: values } }; -} -history.add({ ts: metadata.ts, values: values }); -return { profile: profile, telemetry: history }; +return decodeThingsStackUplink(payload, metadata); From ed1978b4f5d518e41c5b41814425576ead899e40 Mon Sep 17 00:00:00 2001 From: dpinkevych Date: Mon, 9 Nov 2026 14:59:42 +0200 Subject: [PATCH 10/13] fix(iot-hub): the default Things Stack converters take decoded_payload as it is, and a payload formatter can name the model in tbVersionIds Lansitec devices are decoded by our payload formatter in The Things Stack (Custom Javascript formatter on the end device), so the Lansitec block leaves the TBEL and JS converters, with its test and uplinks. A device that is not in the LoRaWAN Device Repository has no version_ids: its formatter can add tbVersionIds { brand_id, model_id } to decoded_payload, the converter takes the profile - from it (or the name in the integration metadata), and drops the field from the telemetry. version_ids come first. Co-Authored-By: Claude Opus 5.5 --- .../controller/ConverterControllerTest.java | 197 +- ...things_stack_formatter_device_payload.json | 42 + .../things_stack_lansitec_uplinks.json | 2672 ----------------- .../src/assets/converters/js-tti-decoder.raw | 694 +---- .../src/assets/converters/js-ttn-decoder.raw | 694 +---- .../assets/converters/tbel-tti-decoder.raw | 702 +---- .../assets/converters/tbel-ttn-decoder.raw | 702 +---- 7 files changed, 234 insertions(+), 5469 deletions(-) create mode 100644 application/src/test/resources/payload/things_stack_formatter_device_payload.json delete mode 100644 application/src/test/resources/payload/things_stack_lansitec_uplinks.json diff --git a/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java index 4549ed5bb8..1c42645271 100644 --- a/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java @@ -4,7 +4,6 @@ package org.thingsboard.server.controller; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.TextNode; import com.google.common.collect.Streams; @@ -41,8 +40,6 @@ import org.thingsboard.server.exception.DataValidationException; import java.io.IOException; import java.io.InputStream; -import java.math.BigDecimal; -import java.math.RoundingMode; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Base64; @@ -101,7 +98,7 @@ public class ConverterControllerTest extends AbstractControllerTest { private static final String DEFAULT_V2_CONFIGURATION = "converters/default-converters-v2-configuration.json"; private static final String THINGS_STACK_REPOSITORY_DEVICE_PAYLOAD = "payload/things_stack_repository_device_payload.json"; private static final String THINGS_STACK_MANUAL_DEVICE_PAYLOAD = "payload/things_stack_manual_device_payload.json"; - private static final String THINGS_STACK_LANSITEC_UPLINKS = "payload/things_stack_lansitec_uplinks.json"; + private static final String THINGS_STACK_FORMATTER_DEVICE_PAYLOAD = "payload/things_stack_formatter_device_payload.json"; @Before public void beforeTest() throws Exception { @@ -721,188 +718,62 @@ public class ConverterControllerTest extends AbstractControllerTest { testThingsStackDefaultConverters(THINGS_STACK_MANUAL_DEVICE_PAYLOAD, Map.of("profile.milesight-iot-am308", "AM308"), expectedDecodedMessage); } - // The TBEL and JS defaults are two copies of one script, so each of them must give this result for TTN and TTI - private void testThingsStackDefaultConverters(String payloadFile, Map integrationMetadata, String expectedResult) throws IOException { - JsonNode defaultConfigurations = JacksonUtil.toJsonNode(readResource(DEFAULT_V2_CONFIGURATION)); - String payload = readResource(payloadFile); - for (IntegrationType integrationType : List.of(IntegrationType.TTN, IntegrationType.TTI)) { - Converter converter = createConverter(defaultConfigurations.get(integrationType.name()), integrationType, 2); - for (ScriptLanguage scriptLang : ScriptLanguage.values()) { - String decoderFileName = "converters/" + scriptLang.name().toLowerCase() + "-" + integrationType.name().toLowerCase() + "-decoder.raw"; - assertThat(JacksonUtil.toJsonNode(decode(decoderFileName, payload, converter, scriptLang, integrationMetadata))) - .as("%s %s", integrationType, scriptLang) - .isEqualTo(JacksonUtil.toJsonNode(expectedResult)); - } - } - } - - // Uplinks of Lansitec devices registered from the LoRaWAN Device Repository, as The Things Stack sent them - // (Simulate uplink), and the keys the default converters decode from them. The keys the integration adds to - // every uplink (rssi, fCnt, eui...) are not listed. TBEL gives integers as doubles, so numbers are compared by value. - private static final Set THINGS_STACK_UPLINK_KEYS = Set.of("fCnt", "data", "rssi", "snr", "channel", "consumedAirtime"); - private static final Set THINGS_STACK_ATTRIBUTE_KEYS = Set.of("eui", "joinEui", "devAddr", "deviceId", "applicationId", "fPort", - "bandwidth", "spreadingFactor", "codeRate", "frequency", "brandId", "modelId", "hardwareVersion", "firmwareVersion", "bandId", - "netId", "tenantId", "clusterId", "clusterAddress"); + private static final String THINGS_STACK_FORMATTER_DEVICE_RESULT = "{\"entityType\":\"DEVICE\",\"name\":\"macro-tracker-1\",\"profile\":\"lansitec-macro-tracker\",\"label\":\"macro-tracker-1\",\"group\":\"my-app\"," + + "\"telemetry\":{\"ts\":1794304800100,\"values\":{\"batteryLevel\":100,\"chargeState\":\"Charging\",\"fCnt\":5,\"data\":\"IWQ+Az4AbFA=\",\"rssi\":-70,\"snr\":7.0}}," + + "\"attributes\":{\"eui\":\"70B3D57ED006F371\",\"deviceId\":\"macro-tracker-1\",\"applicationId\":\"my-app\",\"fPort\":1,\"bandwidth\":125000," + + "\"spreadingFactor\":9,\"frequency\":\"868300000\"}}"; + // A device added without the Repository, whose payload formatter names its model in tbVersionIds @Test - public void testThingsStackDefaultConvertersDecodeLansitecDevices() throws IOException { - JsonNode cases = JacksonUtil.toJsonNode(readResource(THINGS_STACK_LANSITEC_UPLINKS)); - forEachThingsStackDefaultConverter((description, decoder) -> { - for (JsonNode testCase : cases) { - String caseDescription = description + " " + testCase.get("name").asText(); - JsonNode result = decoder.apply(testCase.get("uplink")); - JsonNode decoded = testCase.get("decoded"); - assertThat(result.get("profile")).as(caseDescription).isEqualTo(decoded.get("profile")); - assertThat(numbersByValue(withoutKeys(telemetryValues(result), THINGS_STACK_UPLINK_KEYS))).as(caseDescription + " telemetry") - .isEqualTo(numbersByValue(decoded.has("telemetry") ? decoded.get("telemetry") : JacksonUtil.newObjectNode())); - assertThat(numbersByValue(withoutKeys(result.get("attributes"), THINGS_STACK_ATTRIBUTE_KEYS))).as(caseDescription + " attributes") - .isEqualTo(numbersByValue(decoded.has("attributes") ? decoded.get("attributes") : JacksonUtil.newObjectNode())); - - // The integration's own keys keep their values: the Lansitec keys must not overwrite them - JsonNode uplinkMessage = testCase.get("uplink").get("uplink_message"); - JsonNode gateway = null; - for (JsonNode rx : uplinkMessage.get("rx_metadata")) { - if (gateway == null || rx.get("rssi").asDouble() > gateway.get("rssi").asDouble()) { - gateway = rx; - } - } - JsonNode values = telemetryValues(result); - assertThat(sameValue(values.get("rssi"), gateway.get("rssi"))).as(caseDescription + " rssi " + values.get("rssi")).isTrue(); - assertThat(sameValue(values.get("snr"), gateway.get("snr"))).as(caseDescription + " snr " + values.get("snr")).isTrue(); - JsonNode firmwareVersion = result.get("attributes").get("firmwareVersion"); - assertThat(sameValue(firmwareVersion, uplinkMessage.get("version_ids").get("firmware_version"))) - .as(caseDescription + " firmwareVersion " + firmwareVersion).isTrue(); - } - }); + public void testThingsStackDefaultConvertersGiveFormatterDeviceTheModelProfileOfItsDecodedPayload() throws IOException { + testThingsStackDefaultConverters(THINGS_STACK_FORMATTER_DEVICE_PAYLOAD, Map.of(), THINGS_STACK_FORMATTER_DEVICE_RESULT); } - // Equal as numbers when both are numbers or number texts (the integration may write "1.0" as 1), else as texts - private static boolean sameValue(JsonNode actual, JsonNode expected) { - if (actual == null || expected == null) { - return actual == expected; - } - try { - return new BigDecimal(actual.asText()).compareTo(new BigDecimal(expected.asText())) == 0; - } catch (NumberFormatException e) { - return actual.asText().equals(expected.asText()); - } + @Test + public void testThingsStackDefaultConvertersTakeProfileNameOfFormatterDeviceFromIntegrationMetadata() throws IOException { + String expectedDecodedMessage = THINGS_STACK_FORMATTER_DEVICE_RESULT.replace("\"profile\":\"lansitec-macro-tracker\"", "\"profile\":\"Macro tracker\""); + + testThingsStackDefaultConverters(THINGS_STACK_FORMATTER_DEVICE_PAYLOAD, Map.of("profile.lansitec-macro-tracker", "Macro tracker"), expectedDecodedMessage); } - // The script body limit is 50000 characters (tbel and js max_script_body_size), comments included: the default - // TTN/TTI converters stay well under it, so that a user can add decoding of their own devices + // version_ids come first: tbVersionIds of a Repository device does not change its profile, and is not telemetry either @Test - public void testThingsStackDefaultConvertersStayWellUnderScriptSizeLimit() throws IOException { - for (String decoder : List.of("tbel-ttn", "tbel-tti", "js-ttn", "js-tti")) { - assertThat(readResource("converters/" + decoder + "-decoder.raw").length()).as(decoder).isLessThan(30000); - } + public void testThingsStackDefaultConvertersPreferRepositoryModelToTbVersionIds() throws IOException { + ObjectNode payload = (ObjectNode) JacksonUtil.toJsonNode(readResource(THINGS_STACK_REPOSITORY_DEVICE_PAYLOAD)); + ((ObjectNode) payload.get("uplink_message").get("decoded_payload")) + .set("tbVersionIds", JacksonUtil.toJsonNode("{\"brand_id\":\"lansitec\",\"model_id\":\"macro-tracker\"}")); + + testThingsStackDefaultConvertersOnPayload(payload.toString(), Map.of(), THINGS_STACK_REPOSITORY_DEVICE_RESULT); } - // A frame too short for its message type, and bytes that are not an object: the uplink is still converted + // tbVersionIds that is not an object names no model @Test - public void testThingsStackDefaultConvertersKeepMalformedLansitecUplinks() throws IOException { - JsonNode temperatureHumidity = null; - JsonNode contactBadge = null; - for (JsonNode testCase : JacksonUtil.toJsonNode(readResource(THINGS_STACK_LANSITEC_UPLINKS))) { - if (testCase.get("name").asText().startsWith("temp-hum VER1")) { - temperatureHumidity = testCase.get("uplink"); - } else if (testCase.get("name").asText().startsWith("contact badge 80s Periodic Report 4.5.5 example 1")) { - contactBadge = testCase.get("uplink"); - } - } - ObjectNode shortFrame = temperatureHumidity.deepCopy(); - ((ObjectNode) shortFrame.get("uplink_message")).put("frm_payload", "IQ=="); - ObjectNode bytesList = temperatureHumidity.deepCopy(); - ((ObjectNode) bytesList.get("uplink_message").get("decoded_payload")).set("bytes", JacksonUtil.toJsonNode("[33, 100]")); - // Periodic Report with 2 badges, cut in the middle of the first one, and Instant Report with 3 beacons and 1 byte - ObjectNode shortPeriodicReport = contactBadge.deepCopy(); - ((ObjectNode) shortPeriodicReport.get("uplink_message")).put("frm_payload", "wtAR"); - ObjectNode shortInstantReport = contactBadge.deepCopy(); - ((ObjectNode) shortInstantReport.get("uplink_message")).put("frm_payload", "g9M="); - - forEachThingsStackDefaultConverter((description, decoder) -> { - JsonNode result = decoder.apply(shortFrame); - assertThat(result.get("profile").asText()).as(description + " short frame").isEqualTo("lansitec-temperature-humidity-sensor"); - assertThat(telemetryValues(result).has("loraSnr")).as(description + " short frame").isFalse(); - - result = decoder.apply(bytesList); - assertThat(result.get("profile").asText()).as(description + " bytes list").isEqualTo("lansitec-temperature-humidity-sensor"); - assertThat(numbersByValue(telemetryValues(result).get("bytes"))).as(description + " bytes list") - .isEqualTo(numbersByValue(JacksonUtil.toJsonNode("[33, 100]"))); - - for (ObjectNode shortReport : List.of(shortPeriodicReport, shortInstantReport)) { - result = decoder.apply(shortReport); - String reportDescription = description + " short report " + shortReport.get("uplink_message").get("frm_payload").asText(); - assertThat(result.get("profile").asText()).as(reportDescription).isEqualTo("lansitec-contact-tracing-badge"); - assertThat(withoutKeys(telemetryValues(result), THINGS_STACK_UPLINK_KEYS).isEmpty()).as(reportDescription).isTrue(); - } - }); + public void testThingsStackDefaultConvertersIgnoreTbVersionIdsThatIsNotAnObject() throws IOException { + ObjectNode payload = (ObjectNode) JacksonUtil.toJsonNode(readResource(THINGS_STACK_FORMATTER_DEVICE_PAYLOAD)); + ((ObjectNode) payload.get("uplink_message").get("decoded_payload")).put("tbVersionIds", "lansitec-macro-tracker"); + String expectedDecodedMessage = THINGS_STACK_FORMATTER_DEVICE_RESULT.replace("\"profile\":\"lansitec-macro-tracker\"", "\"profile\":\"LoRaWAN Device\""); + + testThingsStackDefaultConvertersOnPayload(payload.toString(), Map.of(), expectedDecodedMessage); } - private interface ThingsStackDecoderCheck { - void check(String description, java.util.function.Function decoder) throws IOException; + // The TBEL and JS defaults are two copies of one script, so each of them must give this result for TTN and TTI + private void testThingsStackDefaultConverters(String payloadFile, Map integrationMetadata, String expectedResult) throws IOException { + testThingsStackDefaultConvertersOnPayload(readResource(payloadFile), integrationMetadata, expectedResult); } - private void forEachThingsStackDefaultConverter(ThingsStackDecoderCheck check) throws IOException { + private void testThingsStackDefaultConvertersOnPayload(String payload, Map integrationMetadata, String expectedResult) throws IOException { JsonNode defaultConfigurations = JacksonUtil.toJsonNode(readResource(DEFAULT_V2_CONFIGURATION)); for (IntegrationType integrationType : List.of(IntegrationType.TTN, IntegrationType.TTI)) { Converter converter = createConverter(defaultConfigurations.get(integrationType.name()), integrationType, 2); for (ScriptLanguage scriptLang : ScriptLanguage.values()) { String decoderFileName = "converters/" + scriptLang.name().toLowerCase() + "-" + integrationType.name().toLowerCase() + "-decoder.raw"; - check.check(integrationType + " " + scriptLang, uplink -> { - try { - return JacksonUtil.toJsonNode(decode(decoderFileName, uplink.toString(), converter, scriptLang, Map.of())); - } catch (IOException e) { - throw new RuntimeException(e); - } - }); + assertThat(JacksonUtil.toJsonNode(decode(decoderFileName, payload, converter, scriptLang, integrationMetadata))) + .as("%s %s", integrationType, scriptLang) + .isEqualTo(JacksonUtil.toJsonNode(expectedResult)); } } } - private static JsonNode telemetryValues(JsonNode result) { - JsonNode telemetry = result.get("telemetry"); - if (telemetry != null && telemetry.isArray()) { - telemetry = telemetry.get(telemetry.size() - 1); - } - if (telemetry == null) { - return JacksonUtil.newObjectNode(); - } - // {ts, values}, or only the integration's keys when the script gave attributes and no telemetry - return telemetry.has("values") ? telemetry.get("values") : telemetry; - } - - private static ObjectNode withoutKeys(JsonNode node, Set keys) { - ObjectNode copy = JacksonUtil.newObjectNode(); - if (node != null) { - node.fields().forEachRemaining(e -> { - if (!keys.contains(e.getKey())) { - copy.set(e.getKey(), e.getValue()); - } - }); - } - return copy; - } - - private static JsonNode numbersByValue(JsonNode node) { - if (node == null) { - return null; - } - if (node.isNumber()) { - return JacksonUtil.toJsonNode(node.decimalValue().setScale(6, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString()); - } - if (node.isObject()) { - ObjectNode copy = JacksonUtil.newObjectNode(); - node.fields().forEachRemaining(e -> copy.set(e.getKey(), numbersByValue(e.getValue()))); - return copy; - } - if (node.isArray()) { - ArrayNode copy = JacksonUtil.newArrayNode(); - node.forEach(item -> copy.add(numbersByValue(item))); - return copy; - } - return node; - } - private String readResource(String name) throws IOException { return new String(IOUtils.toByteArray(ConverterControllerTest.class.getClassLoader().getResourceAsStream(name)), StandardCharsets.UTF_8); } diff --git a/application/src/test/resources/payload/things_stack_formatter_device_payload.json b/application/src/test/resources/payload/things_stack_formatter_device_payload.json new file mode 100644 index 0000000000..0cdf374bec --- /dev/null +++ b/application/src/test/resources/payload/things_stack_formatter_device_payload.json @@ -0,0 +1,42 @@ +{ + "end_device_ids": { + "device_id": "macro-tracker-1", + "application_ids": { + "application_id": "my-app" + }, + "dev_eui": "70B3D57ED006F371" + }, + "received_at": "2026-11-10T10:00:00.123Z", + "uplink_message": { + "f_port": 1, + "f_cnt": 5, + "frm_payload": "IWQ+Az4AbFA=", + "decoded_payload": { + "tbVersionIds": { + "brand_id": "lansitec", + "model_id": "macro-tracker" + }, + "batteryLevel": 100, + "chargeState": "Charging" + }, + "rx_metadata": [ + { + "gateway_ids": { + "gateway_id": "ug56" + }, + "rssi": -70, + "snr": 7 + } + ], + "settings": { + "data_rate": { + "lora": { + "bandwidth": 125000, + "spreading_factor": 9 + } + }, + "frequency": "868300000" + }, + "received_at": "2026-11-10T10:00:00.100Z" + } +} diff --git a/application/src/test/resources/payload/things_stack_lansitec_uplinks.json b/application/src/test/resources/payload/things_stack_lansitec_uplinks.json deleted file mode 100644 index 44921746c1..0000000000 --- a/application/src/test/resources/payload/things_stack_lansitec_uplinks.json +++ /dev/null @@ -1,2672 +0,0 @@ -[ - { - "name": "gw Registration 4.4.1", - "uplink": { - "end_device_ids": { - "device_id": "lt-compact-bluetooth-gateway", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0000", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DWZKQC3A6CWMNE64WPEVRC", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:96427a37-7461-40d0-94f4-6e3ea7f275e5" - ], - "received_at": "2026-11-02T10:30:49.835523566Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "FiCgMAA8eAsG", - "decoded_payload": { - "bytes": { - "BLEReceivingDuration": "11sec", - "ContinuousReceive": "Continuous Bluetooth receiving is disabled", - "LoRaTxpower": "20dBm", - "LoRaWANband": "EU868", - "adr": "OFF", - "dr": "DR3", - "hb": "3600sec", - "mode": "EU868", - "pos": "300sec", - "type": "Register" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "compact-bluetooth-gateway", - "hardware_version": "1.0", - "firmware_version": "1.0", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-compact-bluetooth-gateway", - "attributes": { - "bluetoothReceivingDuration": 11.0, - "continuousBleReceiving": "Continuous Bluetooth receiving is disabled", - "loraTxPower": 20.0, - "loraWanFrequencyBand": "EU868", - "adr": "OFF", - "dataRate": "DR3", - "heartbeatInterval": 3600.0, - "mode": "EU868", - "positionReportInterval": 300.0, - "networkReconnectionInterval": 1800 - } - } - }, - { - "name": "gw Heartbeat 4.4.2 macro", - "uplink": { - "end_device_ids": { - "device_id": "lt-macro-bluetooth-gateway", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0002", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DWZT8XP0N2W84G07Q2MMCH", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:1af83ff0-c875-48b0-b388-dc2e2742ef61" - ], - "received_at": "2026-11-02T10:30:56.540968417Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "Ics+Az4AbAA=", - "decoded_payload": { - "bytes": { - "chgstate": "Not charging", - "rssi": "-62dBm", - "snr": "8.3dB", - "type": "Heartbeat", - "ver": "VER108", - "vol": "3.5300000000000002V" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "macro-bluetooth-gateway", - "hardware_version": "1.0", - "firmware_version": "1.10", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-macro-bluetooth-gateway", - "telemetry": { - "loraRssi": -62.0, - "deviceFirmwareVersion": "V1.08", - "batteryVoltage": 3.53, - "loraSnr": 8.3 - } - } - }, - { - "name": "gw Heartbeat CHGSTAT 0x50 (built)", - "uplink": { - "end_device_ids": { - "device_id": "lt-compact-bluetooth-gateway", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0000", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DX00PFS05VHZX420SK9SRW", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:4a7e0719-1b7b-4322-b00f-9619d3c1988b" - ], - "received_at": "2026-11-02T10:31:03.118765152Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "IWQ+Az4AbFA=", - "decoded_payload": { - "bytes": { - "chgstate": "Not charging", - "rssi": "-62dBm", - "snr": "8.3dB", - "type": "Heartbeat", - "ver": "VER108", - "vol": "100%" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "compact-bluetooth-gateway", - "hardware_version": "1.0", - "firmware_version": "1.0", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-compact-bluetooth-gateway", - "telemetry": { - "loraRssi": -62.0, - "deviceFirmwareVersion": "V1.08", - "batteryLevel": 100, - "loraSnr": 8.3, - "chargeState": "Charging" - } - } - }, - { - "name": "gw Heartbeat CHGSTAT 0x60, SNR -5.00 (built)", - "uplink": { - "end_device_ids": { - "device_id": "lt-solar-bluetooth-gateway", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0003", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DX074N1N81HG0PPEWKCC4W", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:08bbcae0-1d84-49ae-af6c-384da48f8212" - ], - "received_at": "2026-11-02T10:31:09.716547098Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "IWQ+/gwAbGA=", - "decoded_payload": { - "bytes": { - "chgstate": "Not charging", - "rssi": "-62dBm", - "snr": "650.36dB", - "type": "Heartbeat", - "ver": "VER108", - "vol": "100%" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "solar-bluetooth-gateway", - "hardware_version": "1.0", - "firmware_version": "1.10", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-solar-bluetooth-gateway", - "telemetry": { - "loraRssi": -62.0, - "deviceFirmwareVersion": "V1.08", - "batteryLevel": 100, - "loraSnr": -5.0, - "chargeState": "Charging completed" - } - } - }, - { - "name": "gw Device Type 1 4.4.4 ex1", - "uplink": { - "end_device_ids": { - "device_id": "lt-indoor-bluetooth-gateway", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0001", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DX0DHGSWGSMJN6M8JJQM4Q", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:3ff688da-df1b-4138-ac08-7d4e01ce8695" - ], - "received_at": "2026-11-02T10:31:16.271874839Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "gwYaTAACFaq7swYaTAACFaq8tAYaTAACFaq9tQ==", - "decoded_payload": { - "bytes": { - "Devicetype1data1": "061A4C000215AABB", - "Devicetype1data2": "15AABBB3061A4C00", - "Devicetype1data3": "1A4C000215AABCB4", - "Devicetype1rssi1": "-77dBm", - "Devicetype1rssi2": "-76dBm", - "Devicetype1rssi3": "-75dBm", - "number": 3, - "type": "Device Type 1 Message" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "indoor-bluetooth-gateway", - "hardware_version": "1.0", - "firmware_version": "1.0", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-indoor-bluetooth-gateway", - "telemetry": { - "beacons": [ - { - "deviceType": 1, - "data": "061A4C000215AABB", - "rssi": -77 - }, - { - "deviceType": 1, - "data": "061A4C000215AABC", - "rssi": -76 - }, - { - "deviceType": 1, - "data": "061A4C000215AABD", - "rssi": -75 - } - ] - } - } - }, - { - "name": "gw Device Type 1 4.4.4 ex2", - "uplink": { - "end_device_ids": { - "device_id": "lt-compact-bluetooth-gateway", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0000", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DX0KYNGRQ053WBK9XC1SP2", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:d999fd28-8d4f-401c-9b7f-95c694d99f72" - ], - "received_at": "2026-11-02T10:31:22.835827277Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "gmSCFpgmU0wCFaq8tGSCFpgmVAYaFaq9tQ==", - "decoded_payload": { - "bytes": { - "Devicetype1data1": "6482169826534C0215AABC", - "Devicetype1data2": "534C0215AABCB464821698", - "Devicetype1rssi1": "-76dBm", - "Devicetype1rssi2": "-75dBm", - "number": 2, - "type": "Device Type 1 Message" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "compact-bluetooth-gateway", - "hardware_version": "1.0", - "firmware_version": "1.0", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-compact-bluetooth-gateway", - "telemetry": { - "beacons": [ - { - "deviceType": 1, - "data": "6482169826534C0215AABC", - "rssi": -76 - }, - { - "deviceType": 1, - "data": "648216982654061A15AABD", - "rssi": -75 - } - ] - } - } - }, - { - "name": "gw Device Type 2 4.4.5 ex2", - "uplink": { - "end_device_ids": { - "device_id": "lt-compact-bluetooth-gateway", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0000", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DX0TBEF65ED7NRWQ4YFTRQ", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:7f4ac774-4027-4962-a213-e526da66fd38" - ], - "received_at": "2026-11-02T10:31:29.389983893Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "kpgmU0FNLTEwMDAxs5gmVEFNLTEwMDAytA==", - "decoded_payload": { - "bytes": { - "Devicetype2data1": "982653414D2D3130303031", - "Devicetype2data2": "2D3130303031B398265441", - "Devicetype2rssi1": "-77dBm", - "Devicetype2rssi2": "-76dBm", - "number": 2, - "type": "Device Type 2 Message" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "compact-bluetooth-gateway", - "hardware_version": "1.0", - "firmware_version": "1.0", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-compact-bluetooth-gateway", - "telemetry": { - "beacons": [ - { - "deviceType": 2, - "data": "982653414D2D3130303031", - "rssi": -77 - }, - { - "deviceType": 2, - "data": "982654414D2D3130303032", - "rssi": -76 - } - ] - } - } - }, - { - "name": "gw Device Type 3 4.4.6 ex1", - "uplink": { - "end_device_ids": { - "device_id": "lt-compact-bluetooth-gateway", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0000", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DX10R67RVM4X2QAMR9C8JG", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:68655884-9942-4270-9536-700ee6b20dcf" - ], - "received_at": "2026-11-02T10:31:35.941440062Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "ogAQAwrCABADC8Q=", - "decoded_payload": { - "bytes": { - "Devicetype3data1": "0010030A", - "Devicetype3data2": "0010030B", - "Devicetype3rssi1": "-62dBm", - "Devicetype3rssi2": "-60dBm", - "number": 2, - "type": "Device Type 3 Message" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "compact-bluetooth-gateway", - "hardware_version": "1.0", - "firmware_version": "1.0", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-compact-bluetooth-gateway", - "telemetry": { - "beacons": [ - { - "deviceType": 3, - "data": "0010030A", - "rssi": -62 - }, - { - "deviceType": 3, - "data": "0010030B", - "rssi": -60 - } - ] - } - } - }, - { - "name": "gw Multi 4.4.7 (mixed lengths)", - "uplink": { - "end_device_ids": { - "device_id": "lt-compact-bluetooth-gateway", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0000", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DX174RG6ZG5ZKAHAZTRVG9", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:1aff5046-26ee-42eb-9dfb-d58dafa63116" - ], - "received_at": "2026-11-02T10:31:42.487961636Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "4wEGGkwAAhWqu7MDQU0tMTAwMDGxAgAQAwvF", - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "compact-bluetooth-gateway", - "hardware_version": "1.0", - "firmware_version": "1.0", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-compact-bluetooth-gateway", - "telemetry": {} - } - }, - { - "name": "badge Registration 5.1", - "uplink": { - "end_device_ids": { - "device_id": "lt-badge-tracker", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0007", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DX1DGR31039JQYY9Y2XQ8V", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:224ac913-2b83-4efa-8b16-93b3f4fe7a16" - ], - "received_at": "2026-11-02T10:31:49.014979356Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "EACgMgA8AWh4BSUCBQQ=", - "decoded_payload": { - "bytes": { - "ADR": "OFF", - "CFMMSG": "1 Confirmed every 5 Heartbeat", - "DR": "DR3", - "Fall threshold": "1 meter", - "Firmware version": "V68.78", - "GNSSEN": "Disable", - "HBCOUNT": "Disconnect Judgement 37", - "Heartbeat period": "30sec", - "Lora Txpower": "20dBm", - "Message type": "Register", - "Position mode": "Autonomous", - "Position period": "300sec" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "badge-tracker", - "hardware_version": "1.0", - "firmware_version": "5.25", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-badge-tracker", - "attributes": { - "adr": "OFF", - "dataRate": "DR3", - "gnssEnabled": "Disable", - "loraTxPower": 20.0, - "positioningReportMode": "Autonomous", - "bluetoothPositionReportInterval": 300.0, - "gnssPositionReportInterval": 1800, - "heartbeatInterval": 3600, - "deviceFirmwareVersion": "V5.25", - "cfmmsg": 2, - "hbcount": 5, - "fallDetectionThreshold": 2.0 - } - } - }, - { - "name": "badge Heartbeat 5.2", - "uplink": { - "end_device_ids": { - "device_id": "lt-badge-tracker", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0007", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DX1KWZBE1ANRSZWHFSKQ89", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:29eb6310-c010-4972-897c-d743a41f1d8d" - ], - "received_at": "2026-11-02T10:31:55.550797518Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "IGQ+Az4AYAA=", - "decoded_payload": { - "bytes": { - "Battery level": "100%", - "Charge state": "Charge complete", - "GPS state": "GPS OFF", - "Message type": "Heartbeat", - "Move state": "No Move", - "RSSI": "-62dBm", - "SNR": "8.3dB" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "badge-tracker", - "hardware_version": "1.0", - "firmware_version": "5.25", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-badge-tracker", - "telemetry": { - "batteryLevel": 100.0, - "chargeState": "Charge complete", - "gnssState": "GPS OFF", - "loraRssi": -62.0, - "loraSnr": 8.3, - "movingState": 0 - } - } - }, - { - "name": "badge GNSS 5.3", - "uplink": { - "end_device_ids": { - "device_id": "lt-badge-tracker", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0007", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DX1T9GTJ0BFJRK0D53EHQA", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:bd62cb24-c5cb-4732-a997-c5b6af5a2e47" - ], - "received_at": "2026-11-02T10:32:02.096017163Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "MELtisRB/84uZXLoqA==", - "decoded_payload": { - "bytes": { - "Latitude": 31.975675579986046, - "Longitude": 118.77103314054408, - "Message type": "GNSSPosition", - "Time": "\"2023-12-08T17:58:00.000Z\"" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "badge-tracker", - "hardware_version": "1.0", - "firmware_version": "5.25", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-badge-tracker", - "telemetry": { - "longitude": 118.771027, - "latitude": 31.975674, - "gnssTime": 1702029480000 - } - } - }, - { - "name": "badge GNSS flag failed (built)", - "uplink": { - "end_device_ids": { - "device_id": "lt-badge-tracker", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0007", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DX20NZAK3HTDSA9K12Z4Z6", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:b498fb1f-8894-4ba9-b17c-07c7d0f4018f" - ], - "received_at": "2026-11-02T10:32:08.639160535Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "MULtisRB/84uZXLoqA==", - "decoded_payload": { - "bytes": { - "Latitude": 31.975675579986046, - "Longitude": 118.77103314054408, - "Message type": "GNSSPosition", - "Time": "\"2023-12-08T17:58:00.000Z\"" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "badge-tracker", - "hardware_version": "1.0", - "firmware_version": "5.25", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-badge-tracker", - "telemetry": {} - } - }, - { - "name": "temp-hum VER1 -5.00 dB, -12.34 C, 56 % (built)", - "uplink": { - "end_device_ids": { - "device_id": "lt-temperature-humidity-sensor", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A000A", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DX2756RP8VNV5W4TED20QV", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:2f27538b-a738-43f7-b251-82e5b814d2f2" - ], - "received_at": "2026-11-02T10:32:15.269594659Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "IWQ+/gyMIjgSNA==", - "decoded_payload": { - "bytes": { - "crc": 4660, - "humidity": "56%", - "rssi": "-62dBm", - "snr": 650.36, - "temperature": "-12.34℃", - "type": "Heartbeat", - "ver": 1, - "vol": "100%" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "temperature-humidity-sensor", - "hardware_version": "1.0", - "firmware_version": "1.01", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-temperature-humidity-sensor", - "telemetry": { - "loraRssi": -62.0, - "batteryLevel": 100.0, - "loraSnr": -5.0, - "temperature": -12.34, - "humidity": 56 - } - } - }, - { - "name": "temp-hum VER0 21.05 C, 40 % (built)", - "uplink": { - "end_device_ids": { - "device_id": "lt-temperature-humidity-sensor", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A000A", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DX2DHP1PVMSN9XXTFX6G9E", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:d92a9e40-19fc-4d8a-bbed-db57a3285281" - ], - "received_at": "2026-11-02T10:32:21.813476822Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "IGQ+FQUoEjQ=", - "decoded_payload": { - "bytes": { - "crc": 0, - "humidity": "52%", - "rssi": "-62dBm", - "snr": 53.81, - "temperature": "40.18℃", - "type": "Heartbeat", - "ver": 0, - "vol": "100%" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "temperature-humidity-sensor", - "hardware_version": "1.0", - "firmware_version": "1.01", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-temperature-humidity-sensor", - "telemetry": { - "loraRssi": -62.0, - "batteryLevel": 100.0, - "temperature": 21.05, - "humidity": 40 - } - } - }, - { - "name": "container Heartbeat (built from the codec)", - "uplink": { - "end_device_ids": { - "device_id": "lt-container-tracker", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0008", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DX2KYHR69G9SQHNPG7VJ0N", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:e36b265c-edf1-4c2b-8162-4e475b776185" - ], - "received_at": "2026-11-02T10:32:28.369063597Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "Ics+Az4yABkADA==", - "decoded_payload": { - "bytes": { - "gpsstate": "located", - "movement": "60sec", - "rssi": "-62dBm", - "snr": "8.3dB", - "snrinc": "SNR field included ", - "temperature": "25℃", - "type": "Heartbeat", - "vibstate": "2level", - "vol": "3.5300000000000002V" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "container-tracker", - "hardware_version": "1.0", - "firmware_version": "1.0", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-container-tracker", - "telemetry": { - "gpsstate": "located", - "movement": 60.0, - "loraRssi": -62.0, - "loraSnr": 8.3, - "snrinc": "SNR field included ", - "temperature": 25.0, - "vibstate": 2.0, - "vol": 3.5300000000000002 - } - } - }, - { - "name": "container GNSS (badge 5.3 bytes)", - "uplink": { - "end_device_ids": { - "device_id": "lt-container-tracker", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0008", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DX2TBAJT5T5EE3615Y9MAW", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:b64cce74-6708-4423-aa37-eca108e9f585" - ], - "received_at": "2026-11-02T10:32:34.922307414Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "MELtisRB/84uZXLoqA==", - "decoded_payload": { - "bytes": { - "latitude": 31.975675579986046, - "longitude": 118.77103314054408, - "time": "\"2023-12-08T17:58:00.000Z\"", - "type": "GNSSPosition" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "container-tracker", - "hardware_version": "1.0", - "firmware_version": "1.0", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-container-tracker", - "telemetry": { - "latitude": 31.975675579986046, - "longitude": 118.77103314054408, - "gnssTime": 1702029480000 - } - } - }, - { - "name": "helmet Heartbeat (built from the codec)", - "uplink": { - "end_device_ids": { - "device_id": "lt-helmet-sensor", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0009", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DX30QBDSZ7MN6T27M5X0A3", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:177c9fb1-a5aa-40a5-a324-ace0f58979b7" - ], - "received_at": "2026-11-02T10:32:41.450805906Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "IWQ+Az4BAQIEIA==", - "decoded_payload": { - "bytes": { - "batteryPercentage": "100%", - "bleState": 1, - "chargeTime": "60s", - "gpsState": 1, - "moveState": "2 Move", - "rssi": "-62dBm", - "snr": "8.3dB", - "type": "Heartbeat", - "wearTime": "120s" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "helmet-sensor", - "hardware_version": "1.0", - "firmware_version": "0.25", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-helmet-sensor", - "telemetry": { - "batteryPercentage": 100.0, - "bleState": 1, - "chargeTime": 60.0, - "gpsState": 1, - "moveState": "2 Move", - "loraRssi": -62.0, - "loraSnr": 8.3, - "wearTime": 120.0 - } - } - }, - { - "name": "helmet GNSS (built from the codec)", - "uplink": { - "end_device_ids": { - "device_id": "lt-helmet-sensor", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0009", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DX373VN3K38B2H5WR86Z6H", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:ed54ebed-8df4-46eb-a656-5769e3c37be8" - ], - "received_at": "2026-11-02T10:32:47.994303232Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "MQABhqBC7YrEQf/OLmVy6Kg=", - "decoded_payload": { - "bytes": { - "gpsState": "gps location success", - "latitude": 31.975675579986046, - "longitude": 118.77103314054408, - "pressure": 100000, - "time": "\"2023-12-08T17:58:00.000Z\"", - "type": "GNSSPosition", - "wearState": "wear" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "helmet-sensor", - "hardware_version": "1.0", - "firmware_version": "0.25", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-helmet-sensor", - "telemetry": { - "gpsState": "gps location success", - "latitude": 31.975675579986046, - "longitude": 118.77103314054408, - "pressure": 10000.0, - "gnssTime": 1702029480000, - "wearState": "wear" - } - } - }, - { - "name": "socketsync Registration 5.1 (built from DataSheet 1.5)", - "uplink": { - "end_device_ids": { - "device_id": "lt-socket-sync-bluetooth-gateway", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0004", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DX3DFWNQE3WSG9JNFTAG7B", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:ec26c3c1-e861-4d65-b6e7-1206e538a5c7" - ], - "received_at": "2026-11-02T10:32:54.523762899Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "RiCCFABaEAr/Onw=", - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "socket-sync-bluetooth-gateway", - "hardware_version": "1.0", - "firmware_version": "1.0", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-socket-sync-bluetooth-gateway", - "attributes": { - "adr": "OFF", - "mode": "EU868", - "loraWanFrequencyBand": "EU868", - "loraTxPower": 16, - "continuousBleReceiving": "Continuous Bluetooth receiving is enabled", - "powerDownFlag": "Normal status", - "dataRate": "DR1", - "gatewayType": "Plug gateway", - "rssiSortingMethod": "Sort by average", - "positionReportInterval": 450, - "bluetoothReceivingDuration": 16, - "heartbeatInterval": 300, - "beaconQuantityLimit": 510 - } - } - }, - { - "name": "socketsync Heartbeat 5.2, SNR -5.00, Charging (built from DataSheet 1.5)", - "uplink": { - "end_device_ids": { - "device_id": "lt-socket-sync-bluetooth-gateway", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0004", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DX3DFWNQE3WSG9JNFTAG7B", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:ec26c3c1-e861-4d65-b6e7-1206e538a5c7" - ], - "received_at": "2026-11-02T10:32:54.523762899Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "IVA+/gwbUDp8", - "decoded_payload": { - "bytes": { - "type": "Heartbeat", - "vol": "80%", - "rssi": "-62dBm", - "snr": "650.36dB", - "ver": "VER6992", - "chgstate": 10 - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "socket-sync-bluetooth-gateway", - "hardware_version": "1.0", - "firmware_version": "1.0", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-socket-sync-bluetooth-gateway", - "telemetry": { - "vol": 80, - "loraRssi": -62, - "loraSnr": -5.0, - "deviceFirmwareVersion": "1B", - "chargeState": "Charging" - } - } - }, - { - "name": "socketsync Beacon 5.3 (built from DataSheet 1.5)", - "uplink": { - "end_device_ids": { - "device_id": "lt-socket-sync-bluetooth-gateway", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0004", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DX3DFWNQE3WSG9JNFTAG7B", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:ec26c3c1-e861-4d65-b6e7-1206e538a5c7" - ], - "received_at": "2026-11-02T10:32:54.523762899Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "YlYAAQACqlcAAwAEsA==", - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "socket-sync-bluetooth-gateway", - "hardware_version": "1.0", - "firmware_version": "1.0", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-socket-sync-bluetooth-gateway", - "telemetry": { - "beacons": [ - { - "uuid": "5C38DBDE567C4CCAB1DA40A8AD465656", - "major": 1, - "minor": 2, - "rssi": -86 - }, - { - "uuid": "5C38DBDE567C4CCAB1DA40A8AD465657", - "major": 3, - "minor": 4, - "rssi": -80 - } - ] - } - } - }, - { - "name": "socketsync Alarm Beacon List 5.4 (built from DataSheet 1.5)", - "uplink": { - "end_device_ids": { - "device_id": "lt-socket-sync-bluetooth-gateway", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0004", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DX3DFWNQE3WSG9JNFTAG7B", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:ec26c3c1-e861-4d65-b6e7-1206e538a5c7" - ], - "received_at": "2026-11-02T10:32:54.523762899Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "4VgACgALtQ==", - "decoded_payload": { - "bytes": { - "type": "Multi-Device Type Message", - "number": 1, - "Device1ID": 0, - "Device1data": "000A000B", - "Device1rssi": "-75dBm" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "socket-sync-bluetooth-gateway", - "hardware_version": "1.0", - "firmware_version": "1.0", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-socket-sync-bluetooth-gateway", - "telemetry": { - "alarmBeacons": [ - { - "uuid": "5C38DBDE567C4CCAB1DA40A8AD465658", - "major": 10, - "minor": 11, - "rssi": -75 - } - ] - } - } - }, - { - "name": "socketsync Acknowledgment 5.5 (built from DataSheet 1.5)", - "uplink": { - "end_device_ids": { - "device_id": "lt-socket-sync-bluetooth-gateway", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0004", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DX3DFWNQE3WSG9JNFTAG7B", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:ec26c3c1-e861-4d65-b6e7-1206e538a5c7" - ], - "received_at": "2026-11-02T10:32:54.523762899Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "8AU=", - "decoded_payload": { - "bytes": { - "type": "Acknowledgment", - "result": "downlink success", - "msgid": "5" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "socket-sync-bluetooth-gateway", - "hardware_version": "1.0", - "firmware_version": "1.0", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-socket-sync-bluetooth-gateway", - "telemetry": { - "result": "Success", - "msgid": 5 - } - } - }, - { - "name": "gw Multi 4.4.7, same data length (built from 4.4.7)", - "uplink": { - "end_device_ids": { - "device_id": "lt-compact-bluetooth-gateway", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0000", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6E9MR7XDSV8F47HYQXZWNR9", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:3cb978ee-9803-4d8d-a720-f92b553f8f89" - ], - "received_at": "2026-11-02T14:12:05.501610818Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "4gEGGkwAAhWqu7MBBhpMAAIVqry0", - "decoded_payload": { - "bytes": { - "Device1ID": "Device type 1 message", - "Device1data": "061A4C000215AABB", - "Device1rssi": "-77dBm", - "Device2ID": "Device type 1 message", - "Device2data": "061A4C000215AABC", - "Device2rssi": "-76dBm", - "number": 2, - "type": "Multi-Device Type Message" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "compact-bluetooth-gateway", - "hardware_version": "1.0", - "firmware_version": "1.0", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-compact-bluetooth-gateway", - "telemetry": { - "beacons": [ - { - "deviceType": 1, - "data": "061A4C000215AABB", - "rssi": -77 - }, - { - "deviceType": 1, - "data": "061A4C000215AABC", - "rssi": -76 - } - ] - } - } - }, - { - "name": "badge GNSS longitude NaN (built)", - "uplink": { - "end_device_ids": { - "device_id": "lt-badge-tracker", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0007", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6E9N31RAQD5PHPCQR3KYFT6", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:e23ebb92-d85e-4c68-8648-1a77648991d3" - ], - "received_at": "2026-11-02T14:12:16.566781944Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "MH/AAAFB/84uZXLoqA==", - "decoded_payload": { - "bytes": { - "Latitude": 31.975675579986046, - "Longitude": 5.1042361122864376e+38, - "Message type": "GNSSPosition", - "Time": "\"2023-12-08T17:58:00.000Z\"" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "badge-tracker", - "hardware_version": "1.0", - "firmware_version": "5.25", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-badge-tracker", - "telemetry": {} - } - }, - { - "name": "contact badge Register 4.5.1 (built from DataSheet 2.78)", - "uplink": { - "end_device_ids": { - "device_id": "lt-contact-tracing-badge", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0005", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6NJ57PNWP8AGXCJQD7R9BDW", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:1987636f-fbc5-417b-b6c5-43dfebb304f2" - ], - "received_at": "2026-11-05T09:55:35.252740417Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "FiBwIAACCgqKEjQ=", - "decoded_payload": { - "bytes": { - "BLEReceivingDuration": "10sec", - "ContinuousReceive": "Continuous Bluetooth receiving is disabled", - "LoRaTxpower": "14dBm", - "LoRaWANband": "EU868", - "adr": "OFF", - "dr": "DR2", - "hb": "300sec", - "mode": "EU868", - "pos": "10sec", - "type": "Register" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "contact-tracing-badge", - "hardware_version": "1.0", - "firmware_version": "1.0", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-contact-tracing-badge", - "attributes": { - "adr": "OFF", - "mode": "EU868", - "loraWanFrequencyBand": "EU868", - "loraTxPower": 14, - "dataRate": "DR2", - "positionReportInterval": 10, - "heartbeatInterval": 300, - "buzzerAndVibrator": "ON", - "bluetoothTxPower": 0, - "proximityDetection": "ON", - "bluetoothReceivingThresholdSetting": 10 - } - } - }, - { - "name": "contact badge Heartbeat 4.5.2, SNR -5.00, Charging, 2 friend badges (built from DataSheet 2.78)", - "uplink": { - "end_device_ids": { - "device_id": "lt-contact-tracing-badge", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0005", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6NJ5BCG6BN1FHSSGF67YE24", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:5168cd93-1983-479d-a3d2-cc30d548ea44" - ], - "received_at": "2026-11-05T09:55:39.023582872Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "IWQ+/gwFBRI0AADC0BEALtARAC0=", - "decoded_payload": { - "bytes": { - "chgstate": 2, - "rssi": "-62dBm", - "snr": "650.36dB", - "type": "Heartbeat", - "ver": "VER1285", - "vol": "100%" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "contact-tracing-badge", - "hardware_version": "1.0", - "firmware_version": "1.0", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-contact-tracing-badge", - "telemetry": { - "batteryLevel": 100, - "loraRssi": -62, - "loraSnr": -5, - "deviceFirmwareVersion": "05", - "chargeState": "Charging", - "vibratorBuzzerStatus": "No action", - "friends": [ - { - "major": "D011", - "minor": "002E" - }, - { - "major": "D011", - "minor": "002D" - } - ] - } - } - }, - { - "name": "contact badge Heartbeat 4.5.2, BTSTATUS (built from DataSheet 2.78)", - "uplink": { - "end_device_ids": { - "device_id": "lt-contact-tracing-badge", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0005", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6NJ5D0MNH1S774XX07286BJ", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:8c866e78-af36-4e03-8059-c9161c9f0fe8" - ], - "received_at": "2026-11-05T09:55:40.691881636Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "IWQ+/gwAABI0BiU=", - "decoded_payload": { - "bytes": { - "chgstate": 2, - "rssi": "-62dBm", - "snr": "650.36dB", - "type": "Heartbeat", - "ver": "VER0", - "vol": "100%" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "contact-tracing-badge", - "hardware_version": "1.0", - "firmware_version": "1.0", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-contact-tracing-badge", - "telemetry": { - "batteryLevel": 100, - "loraRssi": -62, - "loraSnr": -5, - "deviceFirmwareVersion": "00", - "chargeState": "Not charging", - "vibratorBuzzerStatus": "Vibrator enabled, buzzer full sound level", - "bluetoothTxPower": 0, - "bluetoothReceivingThreshold": -77 - } - } - }, - { - "name": "contact badge 10s Instant Report 4.5.3, RSSI and TOFF examples (built from DataSheet 2.78)", - "uplink": { - "end_device_ids": { - "device_id": "lt-contact-tracing-badge", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0005", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6NJ5EMKTH1Q7R7AD5WA4HB6", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:e9a53f5b-bd3b-478b-8ff9-4c3424e1337f" - ], - "received_at": "2026-11-05T09:55:42.353804675Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "g9MQAj4r0BEALqsAAQACtSQQ", - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "contact-tracing-badge", - "hardware_version": "1.0", - "firmware_version": "1.0", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-contact-tracing-badge", - "telemetry": { - "beacons": [ - { - "major": "D310", - "minor": "023E", - "type": "Badge", - "rssi": -85, - "timeOffset": 2 - }, - { - "major": "D011", - "minor": "002E", - "type": "Bluetooth beacon", - "rssi": -85, - "timeOffset": 4 - }, - { - "major": "0001", - "minor": "0002", - "type": "Bluetooth beacon", - "rssi": -75, - "timeOffset": 1 - } - ] - } - } - }, - { - "name": "contact badge 80s Periodic Report 4.5.5 example 1", - "uplink": { - "end_device_ids": { - "device_id": "lt-contact-tracing-badge", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0005", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6NJ5G8KP87MTGDZW5HZ8FK2", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:18969a7b-1571-4754-b58f-ca2faa9b417f" - ], - "received_at": "2026-11-05T09:55:44.017827879Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "wtARAC750BEALY8=", - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "contact-tracing-badge", - "hardware_version": "1.0", - "firmware_version": "1.0", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-contact-tracing-badge", - "telemetry": { - "badges": [ - { - "major": "D011", - "minor": "002E", - "timeMark": 249, - "seenTime": 60 - }, - { - "major": "D011", - "minor": "002D", - "timeMark": 143, - "seenTime": 50 - } - ], - "beacons": [] - } - } - }, - { - "name": "contact badge 80s Periodic Report 4.5.5 example 2", - "uplink": { - "end_device_ids": { - "device_id": "lt-contact-tracing-badge", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0005", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6NJ5HWK1Q3VDZS5HH0DX9ST", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:2eb35d1f-4ebc-45eb-a898-447be7a6e488" - ], - "received_at": "2026-11-05T09:55:45.681914168Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "wv//AZvfAPAg", - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "contact-tracing-badge", - "hardware_version": "1.0", - "firmware_version": "1.0", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-contact-tracing-badge", - "telemetry": { - "badges": [], - "beacons": [ - { - "minor": "019B", - "timeMark": 223, - "seenTime": 70 - }, - { - "minor": "00F0", - "timeMark": 32, - "seenTime": 10 - } - ] - } - } - }, - { - "name": "contact badge Acknowledge 4.5.4 (built from DataSheet 2.78)", - "uplink": { - "end_device_ids": { - "device_id": "lt-contact-tracing-badge", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0005", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6NJ5KGJ7QJ2B7AS9Q0VCATN", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:0aba77e7-1257-4147-9e31-31b369cc2cbe" - ], - "received_at": "2026-11-05T09:55:47.345699317Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "8Ac=", - "decoded_payload": { - "bytes": { - "msgid": "7", - "result": "downlink success", - "type": "Acknowledgment" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "contact-tracing-badge", - "hardware_version": "1.0", - "firmware_version": "1.0", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-contact-tracing-badge", - "telemetry": { - "result": "Success", - "msgid": 7 - } - } - }, - { - "name": "badge Heartbeat 5.2 without SNR, firmware before 0x0501 (built)", - "uplink": { - "end_device_ids": { - "device_id": "lt-badge-tracker", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0007", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DX1KWZBE1ANRSZWHFSKQ89", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:29eb6310-c010-4972-897c-d743a41f1d8d" - ], - "received_at": "2026-11-02T10:31:55.550797518Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "IGQ+NWAAAA==", - "decoded_payload": { - "bytes": { - "Message type": "Heartbeat", - "Battery level": "100%", - "RSSI": "-62dBm", - "SNR": "136.64000000000001dB", - "GPS state": "GPS OFF", - "Move state": "No Move", - "Charge state": "Power cable disconnected" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "badge-tracker", - "hardware_version": "1.0", - "firmware_version": "5.25", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-badge-tracker", - "telemetry": { - "batteryLevel": 100.0, - "loraRssi": -62.0, - "gnssState": "GPS located", - "chargeState": "Charge complete", - "movingState": 5 - } - } - }, - { - "name": "container Offline Cache Position 5.7, BLE GNSS BLE (built from the codec)", - "uplink": { - "end_device_ids": { - "device_id": "lt-container-tracker", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0008", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DX2TBAJT5T5EE3615Y9MAW", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:b64cce74-6708-4423-aa37-eca108e9f585" - ], - "received_at": "2026-11-02T10:32:34.922307414Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "s0AAAQACs0LtisRB/84uZXLoqAADAAS0", - "decoded_payload": { - "bytes": { - "type": "OfflineCachePosition", - "length": 3, - "flag": "010", - "flag1": "1 is Bluetooth position data", - "flag2": "2 is GNSS position data", - "flag3": "3 is Bluetooth position data", - "dev1major": "0001", - "dev1minor": "0002", - "dev1rssi": "-77dBm", - "longitude2": 118.77103314054408, - "latitude2": 31.975675579986046, - "time2": "\"2023-12-08T17:58:00.000Z\"", - "dev3major": "0003", - "dev3minor": "0004", - "dev3rssi": "-76dBm" - } - }, - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "container-tracker", - "hardware_version": "1.0", - "firmware_version": "1.0", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-container-tracker", - "telemetry": { - "length": 3, - "flag": "010", - "flag1": "1 is Bluetooth position data", - "flag2": "2 is GNSS position data", - "flag3": "3 is Bluetooth position data", - "longitude2": 118.77103314054408, - "latitude2": 31.975675579986046, - "gnssTime2": 1702029480000, - "beacons": [ - { - "major": "0001", - "minor": "0002", - "rssi": -77 - }, - { - "major": "0003", - "minor": "0004", - "rssi": -76 - } - ] - } - } - }, - { - "name": "socketsync Beacon 5.3, UUID tail 0x05 (built from DataSheet 1.5)", - "uplink": { - "end_device_ids": { - "device_id": "lt-socket-sync-bluetooth-gateway", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0004", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6DX3DFWNQE3WSG9JNFTAG7B", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:ec26c3c1-e861-4d65-b6e7-1206e538a5c7" - ], - "received_at": "2026-11-02T10:32:54.523762899Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "YQUAAQACsA==", - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "socket-sync-bluetooth-gateway", - "hardware_version": "1.0", - "firmware_version": "1.0", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-socket-sync-bluetooth-gateway", - "telemetry": { - "beacons": [ - { - "uuid": "5C38DBDE567C4CCAB1DA40A8AD465705", - "major": 1, - "minor": 2, - "rssi": -80 - } - ] - } - } - }, - { - "name": "contact badge 10s Instant Report 4.5.3, 2 beacons (built from DataSheet 2.78)", - "uplink": { - "end_device_ids": { - "device_id": "lt-contact-tracing-badge", - "application_ids": { - "application_id": "ansitec-test" - }, - "dev_eui": "70B3D57ED01A0005", - "join_eui": "0000000000000000" - }, - "correlation_ids": [ - "as:up:01M6NJ5EMKTH1Q7R7AD5WA4HB6", - "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:e9a53f5b-bd3b-478b-8ff9-4c3424e1337f" - ], - "received_at": "2026-11-05T09:55:42.353804675Z", - "uplink_message": { - "f_port": 1, - "frm_payload": "gtMQAj7T0BEALqsk", - "rx_metadata": [ - { - "gateway_ids": { - "gateway_id": "test" - }, - "rssi": -42, - "channel_rssi": -42, - "snr": 4.2 - } - ], - "settings": { - "data_rate": { - "lora": { - "bandwidth": 125000, - "spreading_factor": 7, - "coding_rate": "4/5" - } - }, - "frequency": "868100000" - }, - "version_ids": { - "brand_id": "lansitec", - "model_id": "contact-tracing-badge", - "hardware_version": "1.0", - "firmware_version": "1.0", - "band_id": "EU_863_870" - } - }, - "simulated": true - }, - "decoded": { - "profile": "lansitec-contact-tracing-badge", - "telemetry": { - "beacons": [ - { - "major": "D310", - "minor": "023E", - "type": "Bluetooth beacon", - "rssi": -45, - "timeOffset": 2 - }, - { - "major": "D011", - "minor": "002E", - "type": "Bluetooth beacon", - "rssi": -85, - "timeOffset": 4 - } - ] - } - } - } -] diff --git a/ui-ngx/src/assets/converters/js-tti-decoder.raw b/ui-ngx/src/assets/converters/js-tti-decoder.raw index 1a0aa27613..b7c6fb0354 100644 --- a/ui-ngx/src/assets/converters/js-tti-decoder.raw +++ b/ui-ngx/src/assets/converters/js-tti-decoder.raw @@ -1,672 +1,50 @@ // The Things Stack adds decoded_payload for a device registered from the LoRaWAN Device Repository, and the payload // is then that JSON: its keys become telemetry. A Milesight-style history list keeps each record's time. // Such a device gets the profile -, so all devices of one model share it, -// or the name the integration metadata gives it under profile.-. Any other device gets LoRaWAN Device. -// Lansitec devices are decoded by the Lansitec block below. -function decodeThingsStackUplink(payload, metadata) { - var values = {}; - var attributes = {}; - var history = []; - var lansitec = metadata.brandId == 'lansitec' ? lansitecUplink(payload, metadata) : null; - if (lansitec != null) { - values = lansitec.telemetry; - attributes = lansitec.attributes; - } else if (metadata.payloadFormat == 'JSON') { - values = decodeToJson(payload); - if (Array.isArray(values.history)) { - values.history.forEach(function (record) { - if (record.timestamp != null) { - var ts = record.timestamp * 1000; - delete record.timestamp; - history.push({ ts: ts, values: record }); - } - }); - delete values.history; - } - } - var profile = 'LoRaWAN Device'; - if (metadata.brandId != null && metadata.modelId != null) { - profile = metadata.brandId + '-' + metadata.modelId; - // The profile name chosen at install, kept in the integration metadata - var customProfile = metadata['profile.' + profile]; - if (customProfile != null && customProfile != '') { - profile = customProfile; - } - } - if (Object.keys(attributes).length > 0) { - return { profile: profile, attributes: attributes }; - } - if (history.length == 0) { - return { profile: profile, telemetry: { ts: metadata.ts, values: values } }; - } - history.push({ ts: metadata.ts, values: values }); - return { profile: profile, telemetry: history }; -} - -function decodeToJson(payload) { - return JSON.parse(String.fromCharCode.apply(String, payload)); -} - -// --------------------------------------------------------------------------------------------------------------------- -// Lansitec. The Repository codecs wrap everything in "bytes", write numbers with units ("85%", "-67dBm") and get some -// fields wrong: those are decoded from the raw bytes (metadata.data) by the datasheets. Remove a fix when the codec is fixed. -// Datasheets: Bluetooth Gateway 2.27, Badge Tracker v5.30, Temperature Humidity Sensor 1.26, Plug Bluetooth Gateway 1.5 -// (SocketSync), LoRaWAN Bluetooth Badge 2.78 (contact tracing badge), Container Tracker 0.72, Helmet Tracker v0.14. -var LANSITEC_UNITS = ['dBm', 'dB', '%', 'V', 'sec', 'min', 's', '℃', 'level', ' meter']; -var LANSITEC_GATEWAYS = ['compact-bluetooth-gateway', 'indoor-bluetooth-gateway', 'macro-bluetooth-gateway', 'solar-bluetooth-gateway']; -var LANSITEC_SOCKET_SYNC = 'socket-sync-bluetooth-gateway'; -var LANSITEC_CONTACT_BADGE = 'contact-tracing-badge'; - -// Codec key -> datasheet name. '-' drops the key: it is decoded from the raw bytes or is not a value. -// Bluetooth Gateway 4.4.1 Registration, 4.4.2 Heartbeat. -var LANSITEC_GATEWAY_KEYS = { - adr: 'adr', mode: 'mode', LoRaWANband: 'loraWanFrequencyBand', LoRaTxpower: 'loraTxPower', - ContinuousReceive: 'continuousBleReceiving', dr: 'dataRate', pos: 'positionReportInterval', - hb: 'heartbeatInterval', BLEReceivingDuration: 'bluetoothReceivingDuration', - rssi: 'loraRssi', snr: '-', ver: '-', vol: '-', chgstate: '-', number: '-' -}; -// Badge Tracker 5.1 Registration (the codec skips GNSSPRI), 5.2 Heartbeat, 5.3 GNSS Position, 5.4 Beacon, 5.5 Alarm. -var LANSITEC_BADGE_KEYS = { - ADR: 'adr', 'Lora Txpower': 'loraTxPower', DR: 'dataRate', GNSSEN: 'gnssEnabled', - 'Position mode': 'positioningReportMode', 'Position period': 'bluetoothPositionReportInterval', - 'Heartbeat period': '-', 'Firmware version': '-', CFMMSG: '-', HBCOUNT: '-', 'Fall threshold': '-', - 'Battery level': 'batteryLevel', RSSI: 'loraRssi', SNR: '-', 'GPS state': 'gnssState', - 'Move state': '-', 'Charge state': 'chargeState', - Longitude: '-', Latitude: '-', Time: '-', Length: '-', Vibstate: '-', 'Alarm type': 'alarm' -}; -// Temperature Humidity Sensor 2.2.1 Register, 2.2.2 Periodical data (VER is the message version, not the firmware). -var LANSITEC_TEMPERATURE_HUMIDITY_KEYS = { - adr: 'adr', mode: 'mode', smode: 'smode', LoraTxpower: 'loraTxPower', dr: 'dataRate', repting: 'reptind', - th: 'temperatureHumidityReportPeriod', crc: '-', - ver: '-', vol: 'batteryLevel', rssi: 'loraRssi', snr: '-', temperature: '-', humidity: '-' -}; -// The other models: the integration writes rssi, snr and firmwareVersion itself. chgstate: the tracking label has the -// gateway codec, which misreads CHGSTAT (Bluetooth Gateway 4.4.2). number: the beacons list has them. -var LANSITEC_CLASH_KEYS = { - rssi: 'loraRssi', RSSI: 'loraRssi', snr: 'loraSnr', SNR: 'loraSnr', - firmwareVersion: 'deviceFirmwareVersion', 'Firmware version': 'deviceFirmwareVersion', - type: '-', 'Message type': '-', chgstate: '-', number: '-' -}; -// Beacon keys: Devicetype1data1, Device1data, dev1major, dev1.major, pos1minor, asset1rssi... -var LANSITEC_BEACON_PREFIXES = ['devicetype1', 'devicetype2', 'devicetype3', 'device', 'dev', 'pos', 'asset']; -var LANSITEC_BEACON_FIELDS = ['data', 'major', 'minor', 'rssi', 'id']; -// Register Type bits 2-0 and SMODE: the LoRa frequency plan (Plug Bluetooth Gateway 5.1, Bluetooth Badge 4.5.1). -var LANSITEC_MODES = ['', 'AU915', 'IN865', 'CN470', 'AS923', 'EU433', 'EU868', 'US915']; -var LANSITEC_SMODES = [1, 2, 4, 8, 16, 32, 64]; -var LANSITEC_BANDS = ['AU915', 'IN865', 'CN470', 'AS923', 'EU433', 'EU868', 'US915']; -// Badge Tracker 5.2 Heartbeat, as the codec writes them. -var LANSITEC_BADGE_GNSS_STATES = ['GPS OFF', 'GPS boot', 'GPS locating', 'GPS located', '', '', '', '', '', 'GPS no signal']; -var LANSITEC_BADGE_CHARGE_STATES = ['Power cable disconnected', '', '', '', '', 'Charging', 'Charge complete']; -// Plug Bluetooth Gateway 5.1 CFG bits 3-1. -var LANSITEC_SOCKET_SYNC_TYPES = ['Outdoor gateway', 'Indoor gateway', 'Plug gateway']; -// Bluetooth Badge 4.5.1 BT bits 10-8, 4.5.2 BTSTATUS bits 6-4 (dBm, 0 is not a power) and bits 11-8. -var LANSITEC_CONTACT_BADGE_BT_POWERS = [0, 4, 0, -4, -8, -12, -16, -20]; -var LANSITEC_CONTACT_BADGE_BUZZER = ['No action', 'Vibrator disabled, buzzer muted (restore)', - 'Vibrator disabled, buzzer half sound level', 'Vibrator disabled, buzzer full sound level', - 'Vibrator enabled, buzzer muted', 'Vibrator enabled, buzzer half sound level', - 'Vibrator enabled, buzzer full sound level', 'Vibrator disabled, buzzer muted (no restore)', - 'Beacon-in-tracker and proximity detection disabled']; - -// { telemetry, attributes }, or null when the uplink is not one the Lansitec block decodes. -function lansitecUplink(payload, metadata) { - var model = metadata.modelId; - var values = null; - var register = false; - if ((model == LANSITEC_SOCKET_SYNC || model == LANSITEC_CONTACT_BADGE) && metadata.data != null) { - // Their codecs are the gateway's and misread every message: decoded from the raw bytes only. - var b = lansitecBase64ToBytes(metadata.data); - values = model == LANSITEC_SOCKET_SYNC ? lansitecSocketSyncValues(b) : lansitecContactBadgeValues(b); - register = b.length > 0 && ((b[0] >> 4) & 0x0F) == (model == LANSITEC_SOCKET_SYNC ? 0x4 : 0x1); - } else if (metadata.payloadFormat == 'JSON') { - var raw = decodeToJson(payload).bytes; - if (raw == null || typeof raw !== 'object' || Array.isArray(raw)) { - return null; - } - values = lansitecValues(raw, model); - if (metadata.data != null) { - lansitecRawValues(values, lansitecBase64ToBytes(metadata.data), model); - } - register = (raw.type != null ? raw.type : raw['Message type']) == 'Register'; - } else { - return null; - } - // Register holds the device settings: attributes, not telemetry. - return register ? { telemetry: {}, attributes: values } : { telemetry: values, attributes: {} }; -} - -function lansitecEndsWith(s, end) { - return s.length >= end.length && s.substring(s.length - end.length) == end; -} - -// Strips the unit. Periods become seconds: the codecs give most of them in seconds, a few in minutes. -function lansitecNumber(v) { - if (typeof v != 'string') { - return v; - } - for (var i = 0; i < LANSITEC_UNITS.length; i++) { - var u = LANSITEC_UNITS[i]; - if (lansitecEndsWith(v, u)) { - var n = v.substring(0, v.length - u.length).trim(); - if (/^-?[0-9]+(\.[0-9]+)?$/.test(n)) { - return u == 'min' ? parseFloat(n) * 60 : parseFloat(n); +// or the name the integration metadata gives it under profile.-. +// A device that is not in the Repository can name its model in decoded_payload: its payload formatter adds +// tbVersionIds: { brand_id, model_id }, which is not telemetry. Any other device gets LoRaWAN Device. +var values = {}; +var history = []; +var versionIds = null; +if (metadata.payloadFormat == 'JSON') { + values = decodeToJson(payload); + if (Array.isArray(values.history)) { + values.history.forEach(function (record) { + if (record.timestamp != null) { + var ts = record.timestamp * 1000; + delete record.timestamp; + history.push({ ts: ts, values: record }); } - } - } - return v; -} - -function lansitecLowerStart(part) { - var n = 0; - while (n < part.length && part.charAt(n) != part.charAt(n).toLowerCase()) { - n = n + 1; - } - if (n == part.length) { - return part.toLowerCase(); - } - if (n > 1) { - return part.substring(0, n - 1).toLowerCase() + part.substring(n - 1); - } - return part.substring(0, 1).toLowerCase() + part.substring(1); -} - -function lansitecCamelCase(key) { - var parts = key.split(/[ .]+/); - var result = lansitecLowerStart(parts[0]); - for (var i = 1; i < parts.length; i++) { - var part = lansitecLowerStart(parts[i]); - result = result + part.substring(0, 1).toUpperCase() + part.substring(1); - } - return result; -} - -function lansitecBeaconKey(key) { - var k = key.toLowerCase().replace('.', ''); - for (var i = 0; i < LANSITEC_BEACON_PREFIXES.length; i++) { - var p = LANSITEC_BEACON_PREFIXES[i]; - if (k.indexOf(p) == 0) { - var rest = k.substring(p.length); - for (var j = 0; j < LANSITEC_BEACON_FIELDS.length; j++) { - var f = LANSITEC_BEACON_FIELDS[j]; - if (lansitecEndsWith(rest, f) && /^[0-9]+$/.test(rest.substring(0, rest.length - f.length))) { - return { group: p, index: rest.substring(0, rest.length - f.length), field: f }; - } - if (rest.indexOf(f) == 0 && /^[0-9]+$/.test(rest.substring(f.length))) { - return { group: p, index: rest.substring(f.length), field: f }; - } - } - } - } - return null; -} - -function lansitecKeyTable(model) { - if (LANSITEC_GATEWAYS.indexOf(model) >= 0) { - return LANSITEC_GATEWAY_KEYS; - } - if (model == 'badge-tracker') { - return LANSITEC_BADGE_KEYS; - } - if (model == 'temperature-humidity-sensor') { - return LANSITEC_TEMPERATURE_HUMIDITY_KEYS; - } - return null; -} - -function lansitecValues(raw, model) { - var table = lansitecKeyTable(model); - var values = {}; - var beacons = {}; - Object.keys(raw).forEach(function (key) { - var value = raw[key]; - var b = lansitecBeaconKey(key); - if (b != null) { - var id = b.group + b.index; - if (beacons[id] == null) { - beacons[id] = { order: parseInt(b.index, 10) }; - } - if (b.field == 'rssi') { - beacons[id].rssi = lansitecNumber(value); - } else if (b.field == 'data' || b.field == 'major' || b.field == 'minor') { - beacons[id][b.field] = value; - } - } else if (table == null && /^time[1-8]?$/.test(key) && typeof value == 'string') { - // Container Tracker 5.3 and 5.7, Helmet Tracker 6.3: UTC time. The codecs add 8 hours and quote it. - values['gnssTime' + key.substring(4)] = new Date(value.replace(/"/g, '')).getTime() - 8 * 60 * 60 * 1000; - } else if (key == 'pressure' && model == 'helmet-sensor' && typeof value == 'number') { - // Helmet Tracker 6.3: the value * 10, unit 0.1 Pa. - values.pressure = value / 10; - } else if (table != null && table.hasOwnProperty(key)) { - if (table[key] != '-') { - values[table[key]] = lansitecNumber(value); - } - } else if (LANSITEC_CLASH_KEYS.hasOwnProperty(key)) { - if (LANSITEC_CLASH_KEYS[key] != '-') { - values[LANSITEC_CLASH_KEYS[key]] = lansitecNumber(value); - } - } else { - values[lansitecCamelCase(key)] = lansitecNumber(value); - } - }); - if (Object.keys(beacons).length > 0) { - values.beacons = lansitecBeaconList(beacons); - } - return values; -} - -// In the order of their numbers, which can have gaps (Container Tracker 5.7: dev1, dev3...). -function lansitecBeaconList(beacons) { - var list = Object.keys(beacons).sort(function (a, b) { - return beacons[a].order - beacons[b].order; - }).map(function (id) { - var b = beacons[id]; - var item = {}; - if (b.data != null) { - item.data = b.data; - } - if (b.major != null) { - item.major = b.major; - item.minor = b.minor; - } - item.rssi = b.rssi; - return item; - }); - // The gateway codec reads every device after the first at a 5-byte step: right only for 4-byte data. - if (list.length > 1 && list[0].data != null && list[0].data.length != 8) { - return [list[0]]; - } - return list; -} - -function lansitecRawValues(values, b, model) { - if (b.length == 0) { - return; - } - var type = (b[0] >> 4) & 0x0F; - if (LANSITEC_GATEWAYS.indexOf(model) >= 0) { - lansitecGatewayRawValues(values, b, model, type); - } else if (model == 'badge-tracker') { - lansitecBadgeRawValues(values, b, type); - } else if (model == 'temperature-humidity-sensor' && type == 2) { - lansitecTemperatureHumidityRawValues(values, b); - } -} - -function lansitecBase64ToBytes(input) { - var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; - var bytes = []; - var bits = 0; - var count = 0; - for (var i = 0; i < input.length; i++) { - var c = chars.indexOf(input.charAt(i)); - if (c < 0) { - continue; - } - bits = (bits << 6) | c; - count = count + 6; - if (count >= 8) { - count = count - 8; - bytes.push((bits >> count) & 0xFF); - } - } - return bytes; -} - -function lansitecHex(b, from, to) { - var digits = '0123456789ABCDEF'; - var hex = ''; - for (var i = from; i < to; i++) { - hex = hex + digits.charAt((b[i] >> 4) & 0x0F) + digits.charAt(b[i] & 0x0F); - } - return hex; -} - -function lansitecUint(b, offset, length) { - var v = 0; - for (var i = 0; i < length; i++) { - v = v * 256 + b[offset + i]; - } - return v; -} - -// IEEE 754 float, big-endian at offset: false when its exponent is 0xFF (NaN or infinity). -function lansitecFloatIsNumber(b, offset) { - return !((b[offset] & 0x7F) == 0x7F && (b[offset + 1] & 0x80) != 0); -} - -// IEEE 754 single precision, big-endian, rounded to 6 decimals (about 0.1 m) as in the TBEL converter. -function lansitecFloat(b, offset) { - var bits = lansitecUint(b, offset, 4); - var exponent = Math.floor(bits / 8388608) % 256; - var mantissa = bits % 8388608; - var value = exponent == 0 ? mantissa * Math.pow(2, -149) : (1 + mantissa / 8388608) * Math.pow(2, exponent - 127); - value = Math.round(value * 1000000) / 1000000; - return bits >= 2147483648 ? -value : value; -} - -// SNR, unit 0.01 dB, signed. The codecs read it unsigned. -function lansitecSnr(b, offset) { - var v = lansitecUint(b, offset, 2); - if (v > 32767) { - v = v - 65536; - } - return v / 100; -} - -// CHGSTAT: 0 Not charging, then the codes of Charging and Charging completed. -function lansitecChargeState(values, charge, charging, completed) { - if (charge == 0) { - values.chargeState = 'Not charging'; - } else if (charge == charging) { - values.chargeState = 'Charging'; - } else if (charge == completed) { - values.chargeState = 'Charging completed'; - } -} - -// Register of the SocketSync gateway (5.1) and the contact tracing badge (4.5.1): Type (ADR, MODE), SMODE, POWER, -// CFG, POS (2, unit 5 s), then HB (unit 30 s) at hbOffset. -function lansitecRegister(values, b, hbOffset) { - values.adr = (b[0] & 0x08) != 0 ? 'ON' : 'OFF'; - if (LANSITEC_MODES[b[0] & 0x07] != '') { - values.mode = LANSITEC_MODES[b[0] & 0x07]; - } - if (LANSITEC_SMODES.indexOf(b[1]) >= 0) { - values.loraWanFrequencyBand = LANSITEC_BANDS[LANSITEC_SMODES.indexOf(b[1])]; - } - values.loraTxPower = (b[2] >> 3) & 0x1F; - values.dataRate = 'DR' + ((b[3] >> 4) & 0x0F); - values.positionReportInterval = lansitecUint(b, 4, 2) * 5; - values.heartbeatInterval = b[hbOffset] * 30; -} - -// Heartbeat of the SocketSync gateway (5.2) and the contact tracing badge (4.5.2): Type, VOL, RSSI (unit -1 dBm), -// SNR (2), REV, CHGSTAT (0x00, 0x50, 0x60 and 0x0, 0x5, 0x6). -function lansitecHeartbeat(values, b, charging, completed) { - values.loraRssi = -b[2]; - values.loraSnr = lansitecSnr(b, 3); - values.deviceFirmwareVersion = lansitecHex(b, 5, 6); - lansitecChargeState(values, b[6], charging, completed); -} - -// Acknowledgment (Plug Bluetooth Gateway 5.5, Bluetooth Badge 4.5.4): Type bits 3-0 the result, then the MSGID. -function lansitecAcknowledgment(values, b) { - values.result = (b[0] & 0x0F) == 0 ? 'Success' : 'Failure'; - values.msgid = b[1]; -} - -// Bluetooth Gateway 2.27. -function lansitecGatewayRawValues(values, b, model, type) { - if (type == 1 && b.length >= 9) { - // 4.4.1: Network reconnection interval, unit 5 minutes. - values.networkReconnectionInterval = b[8] * 300; - } else if (type == 2 && b.length >= 8) { - // 4.4.2: VOL is the battery voltage (x 0.01 + 1.5 V) on the macro gateway, the battery level (%) on the others. - if (model == 'macro-bluetooth-gateway') { - values.batteryVoltage = (b[1] + 150) / 100; - } else { - values.batteryLevel = b[1]; - } - values.loraSnr = lansitecSnr(b, 3); - // VER 108 is V1.08. - var version = lansitecUint(b, 5, 2); - values.deviceFirmwareVersion = 'V' + Math.floor(version / 100) + '.' + (version % 100 < 10 ? '0' : '') + version % 100; - // CHGSTAT on the compact and solar gateway only. - if (model == 'compact-bluetooth-gateway' || model == 'solar-bluetooth-gateway') { - lansitecChargeState(values, b[7], 0x50, 0x60); - } - } else if (type == 0x8 || type == 0x9 || type == 0xA || type == 0xE) { - delete values.beacons; - var beacons = lansitecGatewayBeacons(b, type); - if (beacons != null) { - values.beacons = beacons; - } - } -} - -// 4.4.4-4.4.6: Type (bits 3-0: number of devices), Data and RSSI (value - 256) of each device; one data length per -// message (4.4.3). 4.4.7 Multi-Device: a Device Type ID before each device, decoded when all have the same length. -function lansitecGatewayBeacons(b, type) { - var count = b[0] & 0x0F; - if (count == 0 || (b.length - 1) % count != 0) { - return null; - } - var step = (b.length - 1) / count; - var head = type == 0xE ? 1 : 0; - var dataLength = step - head - 1; - if (dataLength < 1) { - return null; - } - var list = []; - for (var i = 0; i < count; i++) { - var start = 1 + i * step; - var deviceType = type - 7; - if (type == 0xE) { - deviceType = b[start]; - if (deviceType < 1 || deviceType > 3) { - return null; - } - } - list.push({ - deviceType: deviceType, - data: lansitecHex(b, start + head, start + head + dataLength), - rssi: b[start + step - 1] - 256 }); + delete values.history; } - return list; -} - -// Badge Tracker v5.30. -function lansitecBadgeRawValues(values, b, type) { - if (type == 1 && b.length == 14) { - // 5.1: GNSSPRI (2), Heartbeat, Version (2, 0x0525 is V5.25), CFMMSG, HBCOUNT, Fall (unit 0.5 m). - values.gnssPositionReportInterval = lansitecUint(b, 6, 2) * 5; - values.heartbeatInterval = b[8] * 30; - var major = lansitecHex(b, 9, 10); - if (major.charAt(0) == '0') { - major = major.substring(1); - } - values.deviceFirmwareVersion = 'V' + major + '.' + lansitecHex(b, 10, 11); - values.cfmmsg = b[11]; - values.hbcount = b[12]; - values.fallDetectionThreshold = b[13] * 0.5; - } else if (type == 2 && b.length >= 6) { - // 5.2: SNR (2) since firmware 0x0501, decided by the length (SNREN is not reliable). Then GNSSSTAT and - // VIBSTATE (moving state 0-9), CHGSTAT. The codec always reads the SNR. - var state = 3; - if (b.length >= 8) { - values.loraSnr = lansitecSnr(b, 3); - state = 5; - } else { - delete values.gnssState; - delete values.chargeState; - if (LANSITEC_BADGE_GNSS_STATES[(b[3] >> 4) & 0x0F]) { - values.gnssState = LANSITEC_BADGE_GNSS_STATES[(b[3] >> 4) & 0x0F]; - } - if (LANSITEC_BADGE_CHARGE_STATES[(b[4] >> 4) & 0x0F]) { - values.chargeState = LANSITEC_BADGE_CHARGE_STATES[(b[4] >> 4) & 0x0F]; - } - } - values.movingState = b[state] & 0x0F; - } else if (type == 3 && b.length >= 13) { - // 5.3: Flag 0x1 is a failed fix. IEEE 754 floats (the codec divides by 0x7fffff), UTC seconds (the codec - // adds 8 hours). No position from NaN, as in the TBEL converter. - if ((b[0] & 0x0F) == 0 && lansitecFloatIsNumber(b, 1) && lansitecFloatIsNumber(b, 5)) { - values.longitude = lansitecFloat(b, 1); - values.latitude = lansitecFloat(b, 5); - values.gnssTime = lansitecUint(b, 9, 4) * 1000; - } + if (values.tbVersionIds != null) { + versionIds = values.tbVersionIds; + delete values.tbVersionIds; } } - -// Temperature Humidity Sensor 2.2.2: VER 1 has the SNR, VER 0 not (the codec always reads it). TEMP_INT bit 7 is the sign. -function lansitecTemperatureHumidityRawValues(values, b) { - var offset = (b[0] & 0x0F) == 1 ? 5 : 3; - if (b.length < offset + 3) { - return; - } - if (offset == 5) { - values.loraSnr = lansitecSnr(b, 3); - } - var temperature = (b[offset] & 0x7F) + b[offset + 1] / 100; - if ((b[offset] & 0x80) != 0) { - temperature = -temperature; - } - values.temperature = Math.round(temperature * 100) / 100; - values.humidity = b[offset + 2]; +var brandId = metadata.brandId; +var modelId = metadata.modelId; +if ((brandId == null || modelId == null) && versionIds != null) { + brandId = versionIds.brand_id; + modelId = versionIds.model_id; } - -// Plug Bluetooth Gateway 1.5, the SocketSync gateway. -function lansitecSocketSyncValues(b) { - var values = {}; - if (b.length == 0) { - return values; - } - var type = (b[0] >> 4) & 0x0F; - if (type == 0x4 && b.length >= 10) { - // 5.1 Registration: ..., BLE Receiving Duration, HB, Beacon_QTY (half of the limit), CRC (2). - lansitecRegister(values, b, 7); - values.continuousBleReceiving = (b[2] & 0x02) != 0 ? 'Continuous Bluetooth receiving is enabled' : 'Continuous Bluetooth receiving is disabled'; - values.powerDownFlag = (b[2] & 0x01) != 0 ? 'Automatic shutdown status' : 'Normal status'; - if (LANSITEC_SOCKET_SYNC_TYPES[(b[3] >> 1) & 0x07] != null) { - values.gatewayType = LANSITEC_SOCKET_SYNC_TYPES[(b[3] >> 1) & 0x07]; - } - values.rssiSortingMethod = (b[3] & 0x01) != 0 ? 'Sort by the maximum value' : 'Sort by average'; - values.bluetoothReceivingDuration = b[6]; - values.beaconQuantityLimit = b[8] * 2; - } else if (type == 0x2 && b.length >= 7) { - // 5.2 Heartbeat. VOL (%) keeps its name: the dashboard of the IoT Hub package reads it. - values.vol = b[1]; - lansitecHeartbeat(values, b, 0x50, 0x60); - } else if (type == 0x6 || type == 0xE) { - // 5.3 Beacon, 5.4 Alarm Beacon List: Type (bits 3-0: number of beacons), then UUID tail, Major (2), Minor (2), - // RSSI (value - 256) of each. Table 7: the UUID prefix ends with 56 for tails 0x56-0xFF, 57 for 0x00-0x1E. - var count = b[0] & 0x0F; - if (count > 0 && b.length >= 1 + count * 6) { - var list = []; - for (var i = 0; i < count; i++) { - var start = 1 + i * 6; - list.push({ - uuid: '5C38DBDE567C4CCAB1DA40A8AD465' + (b[start] < 0x56 ? '7' : '6') + lansitecHex(b, start, start + 1), - major: lansitecUint(b, start + 1, 2), - minor: lansitecUint(b, start + 3, 2), - rssi: b[start + 5] - 256 - }); - } - values[type == 0x6 ? 'beacons' : 'alarmBeacons'] = list; - } - } else if (type == 0xF && b.length >= 2) { - lansitecAcknowledgment(values, b); +var profile = 'LoRaWAN Device'; +if (brandId != null && modelId != null) { + profile = brandId + '-' + modelId; + // The profile name chosen at install, kept in the integration metadata + var customProfile = metadata['profile.' + profile]; + if (customProfile != null && customProfile != '') { + profile = customProfile; } - return values; } - -// Bluetooth Badge 4.5.5 TimeMark: bit n set means seen n * 10 to n * 10 + 9 s before the message, 10 s per bit. -function lansitecContactBadgeSeen(item, mark) { - item.timeMark = mark; - var bits = 0; - for (var i = 0; i < 8; i++) { - bits += (mark >> i) & 1; - } - item.seenTime = bits * 10; - return item; +if (history.length == 0) { + return { profile: profile, telemetry: { ts: metadata.ts, values: values } }; } +history.push({ ts: metadata.ts, values: values }); +return { profile: profile, telemetry: history }; -// LoRaWAN Bluetooth Badge 2.78, the contact tracing badge. Major and Minor are hex (a badge's: the end of its DevEUI). -function lansitecContactBadgeValues(b) { - var values = {}; - if (b.length == 0) { - return values; - } - var type = (b[0] >> 4) & 0x0F; - var i, start; - if (type == 0x1 && b.length >= 9) { - // 4.5.1 Register: ..., HB, BT (2), CRC (2). The receiving threshold setting has no unit in the datasheet. - lansitecRegister(values, b, 6); - var bt = lansitecUint(b, 7, 2); - values.buzzerAndVibrator = ((bt >> 11) & 1) == 1 ? 'ON' : 'OFF'; - if (((bt >> 8) & 0x07) > 0) { - values.bluetoothTxPower = LANSITEC_CONTACT_BADGE_BT_POWERS[(bt >> 8) & 0x07]; - } - values.proximityDetection = ((bt >> 7) & 1) == 1 ? 'ON' : 'OFF'; - values.bluetoothReceivingThresholdSetting = bt & 0x7F; - } else if (type == 0x2 && b.length >= 7) { - // 4.5.2 Heartbeat: ..., CRC (2), BTSTATUS (2), then the friend badges. - values.batteryLevel = b[1]; - lansitecHeartbeat(values, b, 0x5, 0x6); - if (b.length >= 11) { - var status = lansitecUint(b, 9, 2); - if (LANSITEC_CONTACT_BADGE_BUZZER[(status >> 8) & 0x0F] != null) { - values.vibratorBuzzerStatus = LANSITEC_CONTACT_BADGE_BUZZER[(status >> 8) & 0x0F]; - } - // 0 means not changed. The threshold is for range bit 7 = 0: -73 to -87 dBm. - if (((status >> 4) & 0x07) > 0) { - values.bluetoothTxPower = LANSITEC_CONTACT_BADGE_BT_POWERS[(status >> 4) & 0x07]; - } - if ((status & 0x0F) > 0 && ((status >> 7) & 1) == 0) { - values.bluetoothReceivingThreshold = -72 - (status & 0x0F); - } - } - // Friend field: Type 0xC with the number of friend badges, then Major (2) and Minor (2) of each. - if (b.length >= 12 && ((b[11] >> 4) & 0x0F) == 0xC && b.length >= 12 + (b[11] & 0x0F) * 4) { - var friends = []; - for (i = 0; i < (b[11] & 0x0F); i++) { - start = 12 + i * 4; - friends.push({ major: lansitecHex(b, start, start + 2), minor: lansitecHex(b, start + 2, start + 4) }); - } - values.friends = friends; - } - } else if (type == 0x8) { - // 4.5.3 10s Instant Report: Type (bits 3-0: M beacons), Major (2), Minor (2), RSSI of each, then TOFF: - // (M + 1) / 2 bytes, 4 bits per beacon, high bits first. RSSI bit 7: 1 a beacon, 0 a badge; bits 6-0 - 128. - var count = b[0] & 0x0F; - if (count > 0 && b.length >= 1 + count * 5) { - var hasOffsets = b.length >= 1 + count * 5 + Math.floor((count + 1) / 2); - var list = []; - for (i = 0; i < count; i++) { - start = 1 + i * 5; - var item = { - major: lansitecHex(b, start, start + 2), - minor: lansitecHex(b, start + 2, start + 4), - type: (b[start + 4] & 0x80) != 0 ? 'Bluetooth beacon' : 'Badge', - rssi: (b[start + 4] & 0x7F) - 128 - }; - if (hasOffsets) { - var offsetByte = b[1 + count * 5 + Math.floor(i / 2)]; - item.timeOffset = i % 2 == 0 ? (offsetByte >> 4) & 0x0F : offsetByte & 0x0F; - } - list.push(item); - } - values.beacons = list; - } - } else if (type == 0xC) { - // 4.5.5 80s Periodic Report: badges as Major (2), Minor (2), TimeMark, then 0xFFFF and beacons as Minor (2), - // TimeMark (a beacon's Major is FFFF). - var badges = []; - var periodBeacons = []; - var pos = 1; - var inBeacons = false; - while (pos < b.length) { - if (!inBeacons && pos + 2 <= b.length && b[pos] == 0xFF && b[pos + 1] == 0xFF) { - inBeacons = true; - pos += 2; - } else if (!inBeacons && pos + 5 <= b.length) { - badges.push(lansitecContactBadgeSeen({ major: lansitecHex(b, pos, pos + 2), minor: lansitecHex(b, pos + 2, pos + 4) }, b[pos + 4])); - pos += 5; - } else if (inBeacons && pos + 3 <= b.length) { - periodBeacons.push(lansitecContactBadgeSeen({ minor: lansitecHex(b, pos, pos + 2) }, b[pos + 2])); - pos += 3; - } else { - return values; - } - } - if (badges.length + periodBeacons.length == (b[0] & 0x0F)) { - values.badges = badges; - values.beacons = periodBeacons; - } - } else if (type == 0xF && b.length >= 2) { - lansitecAcknowledgment(values, b); - } - return values; +function decodeToJson(payload) { + return JSON.parse(String.fromCharCode.apply(String, payload)); } - -return decodeThingsStackUplink(payload, metadata); diff --git a/ui-ngx/src/assets/converters/js-ttn-decoder.raw b/ui-ngx/src/assets/converters/js-ttn-decoder.raw index 1a0aa27613..b7c6fb0354 100644 --- a/ui-ngx/src/assets/converters/js-ttn-decoder.raw +++ b/ui-ngx/src/assets/converters/js-ttn-decoder.raw @@ -1,672 +1,50 @@ // The Things Stack adds decoded_payload for a device registered from the LoRaWAN Device Repository, and the payload // is then that JSON: its keys become telemetry. A Milesight-style history list keeps each record's time. // Such a device gets the profile -, so all devices of one model share it, -// or the name the integration metadata gives it under profile.-. Any other device gets LoRaWAN Device. -// Lansitec devices are decoded by the Lansitec block below. -function decodeThingsStackUplink(payload, metadata) { - var values = {}; - var attributes = {}; - var history = []; - var lansitec = metadata.brandId == 'lansitec' ? lansitecUplink(payload, metadata) : null; - if (lansitec != null) { - values = lansitec.telemetry; - attributes = lansitec.attributes; - } else if (metadata.payloadFormat == 'JSON') { - values = decodeToJson(payload); - if (Array.isArray(values.history)) { - values.history.forEach(function (record) { - if (record.timestamp != null) { - var ts = record.timestamp * 1000; - delete record.timestamp; - history.push({ ts: ts, values: record }); - } - }); - delete values.history; - } - } - var profile = 'LoRaWAN Device'; - if (metadata.brandId != null && metadata.modelId != null) { - profile = metadata.brandId + '-' + metadata.modelId; - // The profile name chosen at install, kept in the integration metadata - var customProfile = metadata['profile.' + profile]; - if (customProfile != null && customProfile != '') { - profile = customProfile; - } - } - if (Object.keys(attributes).length > 0) { - return { profile: profile, attributes: attributes }; - } - if (history.length == 0) { - return { profile: profile, telemetry: { ts: metadata.ts, values: values } }; - } - history.push({ ts: metadata.ts, values: values }); - return { profile: profile, telemetry: history }; -} - -function decodeToJson(payload) { - return JSON.parse(String.fromCharCode.apply(String, payload)); -} - -// --------------------------------------------------------------------------------------------------------------------- -// Lansitec. The Repository codecs wrap everything in "bytes", write numbers with units ("85%", "-67dBm") and get some -// fields wrong: those are decoded from the raw bytes (metadata.data) by the datasheets. Remove a fix when the codec is fixed. -// Datasheets: Bluetooth Gateway 2.27, Badge Tracker v5.30, Temperature Humidity Sensor 1.26, Plug Bluetooth Gateway 1.5 -// (SocketSync), LoRaWAN Bluetooth Badge 2.78 (contact tracing badge), Container Tracker 0.72, Helmet Tracker v0.14. -var LANSITEC_UNITS = ['dBm', 'dB', '%', 'V', 'sec', 'min', 's', '℃', 'level', ' meter']; -var LANSITEC_GATEWAYS = ['compact-bluetooth-gateway', 'indoor-bluetooth-gateway', 'macro-bluetooth-gateway', 'solar-bluetooth-gateway']; -var LANSITEC_SOCKET_SYNC = 'socket-sync-bluetooth-gateway'; -var LANSITEC_CONTACT_BADGE = 'contact-tracing-badge'; - -// Codec key -> datasheet name. '-' drops the key: it is decoded from the raw bytes or is not a value. -// Bluetooth Gateway 4.4.1 Registration, 4.4.2 Heartbeat. -var LANSITEC_GATEWAY_KEYS = { - adr: 'adr', mode: 'mode', LoRaWANband: 'loraWanFrequencyBand', LoRaTxpower: 'loraTxPower', - ContinuousReceive: 'continuousBleReceiving', dr: 'dataRate', pos: 'positionReportInterval', - hb: 'heartbeatInterval', BLEReceivingDuration: 'bluetoothReceivingDuration', - rssi: 'loraRssi', snr: '-', ver: '-', vol: '-', chgstate: '-', number: '-' -}; -// Badge Tracker 5.1 Registration (the codec skips GNSSPRI), 5.2 Heartbeat, 5.3 GNSS Position, 5.4 Beacon, 5.5 Alarm. -var LANSITEC_BADGE_KEYS = { - ADR: 'adr', 'Lora Txpower': 'loraTxPower', DR: 'dataRate', GNSSEN: 'gnssEnabled', - 'Position mode': 'positioningReportMode', 'Position period': 'bluetoothPositionReportInterval', - 'Heartbeat period': '-', 'Firmware version': '-', CFMMSG: '-', HBCOUNT: '-', 'Fall threshold': '-', - 'Battery level': 'batteryLevel', RSSI: 'loraRssi', SNR: '-', 'GPS state': 'gnssState', - 'Move state': '-', 'Charge state': 'chargeState', - Longitude: '-', Latitude: '-', Time: '-', Length: '-', Vibstate: '-', 'Alarm type': 'alarm' -}; -// Temperature Humidity Sensor 2.2.1 Register, 2.2.2 Periodical data (VER is the message version, not the firmware). -var LANSITEC_TEMPERATURE_HUMIDITY_KEYS = { - adr: 'adr', mode: 'mode', smode: 'smode', LoraTxpower: 'loraTxPower', dr: 'dataRate', repting: 'reptind', - th: 'temperatureHumidityReportPeriod', crc: '-', - ver: '-', vol: 'batteryLevel', rssi: 'loraRssi', snr: '-', temperature: '-', humidity: '-' -}; -// The other models: the integration writes rssi, snr and firmwareVersion itself. chgstate: the tracking label has the -// gateway codec, which misreads CHGSTAT (Bluetooth Gateway 4.4.2). number: the beacons list has them. -var LANSITEC_CLASH_KEYS = { - rssi: 'loraRssi', RSSI: 'loraRssi', snr: 'loraSnr', SNR: 'loraSnr', - firmwareVersion: 'deviceFirmwareVersion', 'Firmware version': 'deviceFirmwareVersion', - type: '-', 'Message type': '-', chgstate: '-', number: '-' -}; -// Beacon keys: Devicetype1data1, Device1data, dev1major, dev1.major, pos1minor, asset1rssi... -var LANSITEC_BEACON_PREFIXES = ['devicetype1', 'devicetype2', 'devicetype3', 'device', 'dev', 'pos', 'asset']; -var LANSITEC_BEACON_FIELDS = ['data', 'major', 'minor', 'rssi', 'id']; -// Register Type bits 2-0 and SMODE: the LoRa frequency plan (Plug Bluetooth Gateway 5.1, Bluetooth Badge 4.5.1). -var LANSITEC_MODES = ['', 'AU915', 'IN865', 'CN470', 'AS923', 'EU433', 'EU868', 'US915']; -var LANSITEC_SMODES = [1, 2, 4, 8, 16, 32, 64]; -var LANSITEC_BANDS = ['AU915', 'IN865', 'CN470', 'AS923', 'EU433', 'EU868', 'US915']; -// Badge Tracker 5.2 Heartbeat, as the codec writes them. -var LANSITEC_BADGE_GNSS_STATES = ['GPS OFF', 'GPS boot', 'GPS locating', 'GPS located', '', '', '', '', '', 'GPS no signal']; -var LANSITEC_BADGE_CHARGE_STATES = ['Power cable disconnected', '', '', '', '', 'Charging', 'Charge complete']; -// Plug Bluetooth Gateway 5.1 CFG bits 3-1. -var LANSITEC_SOCKET_SYNC_TYPES = ['Outdoor gateway', 'Indoor gateway', 'Plug gateway']; -// Bluetooth Badge 4.5.1 BT bits 10-8, 4.5.2 BTSTATUS bits 6-4 (dBm, 0 is not a power) and bits 11-8. -var LANSITEC_CONTACT_BADGE_BT_POWERS = [0, 4, 0, -4, -8, -12, -16, -20]; -var LANSITEC_CONTACT_BADGE_BUZZER = ['No action', 'Vibrator disabled, buzzer muted (restore)', - 'Vibrator disabled, buzzer half sound level', 'Vibrator disabled, buzzer full sound level', - 'Vibrator enabled, buzzer muted', 'Vibrator enabled, buzzer half sound level', - 'Vibrator enabled, buzzer full sound level', 'Vibrator disabled, buzzer muted (no restore)', - 'Beacon-in-tracker and proximity detection disabled']; - -// { telemetry, attributes }, or null when the uplink is not one the Lansitec block decodes. -function lansitecUplink(payload, metadata) { - var model = metadata.modelId; - var values = null; - var register = false; - if ((model == LANSITEC_SOCKET_SYNC || model == LANSITEC_CONTACT_BADGE) && metadata.data != null) { - // Their codecs are the gateway's and misread every message: decoded from the raw bytes only. - var b = lansitecBase64ToBytes(metadata.data); - values = model == LANSITEC_SOCKET_SYNC ? lansitecSocketSyncValues(b) : lansitecContactBadgeValues(b); - register = b.length > 0 && ((b[0] >> 4) & 0x0F) == (model == LANSITEC_SOCKET_SYNC ? 0x4 : 0x1); - } else if (metadata.payloadFormat == 'JSON') { - var raw = decodeToJson(payload).bytes; - if (raw == null || typeof raw !== 'object' || Array.isArray(raw)) { - return null; - } - values = lansitecValues(raw, model); - if (metadata.data != null) { - lansitecRawValues(values, lansitecBase64ToBytes(metadata.data), model); - } - register = (raw.type != null ? raw.type : raw['Message type']) == 'Register'; - } else { - return null; - } - // Register holds the device settings: attributes, not telemetry. - return register ? { telemetry: {}, attributes: values } : { telemetry: values, attributes: {} }; -} - -function lansitecEndsWith(s, end) { - return s.length >= end.length && s.substring(s.length - end.length) == end; -} - -// Strips the unit. Periods become seconds: the codecs give most of them in seconds, a few in minutes. -function lansitecNumber(v) { - if (typeof v != 'string') { - return v; - } - for (var i = 0; i < LANSITEC_UNITS.length; i++) { - var u = LANSITEC_UNITS[i]; - if (lansitecEndsWith(v, u)) { - var n = v.substring(0, v.length - u.length).trim(); - if (/^-?[0-9]+(\.[0-9]+)?$/.test(n)) { - return u == 'min' ? parseFloat(n) * 60 : parseFloat(n); +// or the name the integration metadata gives it under profile.-. +// A device that is not in the Repository can name its model in decoded_payload: its payload formatter adds +// tbVersionIds: { brand_id, model_id }, which is not telemetry. Any other device gets LoRaWAN Device. +var values = {}; +var history = []; +var versionIds = null; +if (metadata.payloadFormat == 'JSON') { + values = decodeToJson(payload); + if (Array.isArray(values.history)) { + values.history.forEach(function (record) { + if (record.timestamp != null) { + var ts = record.timestamp * 1000; + delete record.timestamp; + history.push({ ts: ts, values: record }); } - } - } - return v; -} - -function lansitecLowerStart(part) { - var n = 0; - while (n < part.length && part.charAt(n) != part.charAt(n).toLowerCase()) { - n = n + 1; - } - if (n == part.length) { - return part.toLowerCase(); - } - if (n > 1) { - return part.substring(0, n - 1).toLowerCase() + part.substring(n - 1); - } - return part.substring(0, 1).toLowerCase() + part.substring(1); -} - -function lansitecCamelCase(key) { - var parts = key.split(/[ .]+/); - var result = lansitecLowerStart(parts[0]); - for (var i = 1; i < parts.length; i++) { - var part = lansitecLowerStart(parts[i]); - result = result + part.substring(0, 1).toUpperCase() + part.substring(1); - } - return result; -} - -function lansitecBeaconKey(key) { - var k = key.toLowerCase().replace('.', ''); - for (var i = 0; i < LANSITEC_BEACON_PREFIXES.length; i++) { - var p = LANSITEC_BEACON_PREFIXES[i]; - if (k.indexOf(p) == 0) { - var rest = k.substring(p.length); - for (var j = 0; j < LANSITEC_BEACON_FIELDS.length; j++) { - var f = LANSITEC_BEACON_FIELDS[j]; - if (lansitecEndsWith(rest, f) && /^[0-9]+$/.test(rest.substring(0, rest.length - f.length))) { - return { group: p, index: rest.substring(0, rest.length - f.length), field: f }; - } - if (rest.indexOf(f) == 0 && /^[0-9]+$/.test(rest.substring(f.length))) { - return { group: p, index: rest.substring(f.length), field: f }; - } - } - } - } - return null; -} - -function lansitecKeyTable(model) { - if (LANSITEC_GATEWAYS.indexOf(model) >= 0) { - return LANSITEC_GATEWAY_KEYS; - } - if (model == 'badge-tracker') { - return LANSITEC_BADGE_KEYS; - } - if (model == 'temperature-humidity-sensor') { - return LANSITEC_TEMPERATURE_HUMIDITY_KEYS; - } - return null; -} - -function lansitecValues(raw, model) { - var table = lansitecKeyTable(model); - var values = {}; - var beacons = {}; - Object.keys(raw).forEach(function (key) { - var value = raw[key]; - var b = lansitecBeaconKey(key); - if (b != null) { - var id = b.group + b.index; - if (beacons[id] == null) { - beacons[id] = { order: parseInt(b.index, 10) }; - } - if (b.field == 'rssi') { - beacons[id].rssi = lansitecNumber(value); - } else if (b.field == 'data' || b.field == 'major' || b.field == 'minor') { - beacons[id][b.field] = value; - } - } else if (table == null && /^time[1-8]?$/.test(key) && typeof value == 'string') { - // Container Tracker 5.3 and 5.7, Helmet Tracker 6.3: UTC time. The codecs add 8 hours and quote it. - values['gnssTime' + key.substring(4)] = new Date(value.replace(/"/g, '')).getTime() - 8 * 60 * 60 * 1000; - } else if (key == 'pressure' && model == 'helmet-sensor' && typeof value == 'number') { - // Helmet Tracker 6.3: the value * 10, unit 0.1 Pa. - values.pressure = value / 10; - } else if (table != null && table.hasOwnProperty(key)) { - if (table[key] != '-') { - values[table[key]] = lansitecNumber(value); - } - } else if (LANSITEC_CLASH_KEYS.hasOwnProperty(key)) { - if (LANSITEC_CLASH_KEYS[key] != '-') { - values[LANSITEC_CLASH_KEYS[key]] = lansitecNumber(value); - } - } else { - values[lansitecCamelCase(key)] = lansitecNumber(value); - } - }); - if (Object.keys(beacons).length > 0) { - values.beacons = lansitecBeaconList(beacons); - } - return values; -} - -// In the order of their numbers, which can have gaps (Container Tracker 5.7: dev1, dev3...). -function lansitecBeaconList(beacons) { - var list = Object.keys(beacons).sort(function (a, b) { - return beacons[a].order - beacons[b].order; - }).map(function (id) { - var b = beacons[id]; - var item = {}; - if (b.data != null) { - item.data = b.data; - } - if (b.major != null) { - item.major = b.major; - item.minor = b.minor; - } - item.rssi = b.rssi; - return item; - }); - // The gateway codec reads every device after the first at a 5-byte step: right only for 4-byte data. - if (list.length > 1 && list[0].data != null && list[0].data.length != 8) { - return [list[0]]; - } - return list; -} - -function lansitecRawValues(values, b, model) { - if (b.length == 0) { - return; - } - var type = (b[0] >> 4) & 0x0F; - if (LANSITEC_GATEWAYS.indexOf(model) >= 0) { - lansitecGatewayRawValues(values, b, model, type); - } else if (model == 'badge-tracker') { - lansitecBadgeRawValues(values, b, type); - } else if (model == 'temperature-humidity-sensor' && type == 2) { - lansitecTemperatureHumidityRawValues(values, b); - } -} - -function lansitecBase64ToBytes(input) { - var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; - var bytes = []; - var bits = 0; - var count = 0; - for (var i = 0; i < input.length; i++) { - var c = chars.indexOf(input.charAt(i)); - if (c < 0) { - continue; - } - bits = (bits << 6) | c; - count = count + 6; - if (count >= 8) { - count = count - 8; - bytes.push((bits >> count) & 0xFF); - } - } - return bytes; -} - -function lansitecHex(b, from, to) { - var digits = '0123456789ABCDEF'; - var hex = ''; - for (var i = from; i < to; i++) { - hex = hex + digits.charAt((b[i] >> 4) & 0x0F) + digits.charAt(b[i] & 0x0F); - } - return hex; -} - -function lansitecUint(b, offset, length) { - var v = 0; - for (var i = 0; i < length; i++) { - v = v * 256 + b[offset + i]; - } - return v; -} - -// IEEE 754 float, big-endian at offset: false when its exponent is 0xFF (NaN or infinity). -function lansitecFloatIsNumber(b, offset) { - return !((b[offset] & 0x7F) == 0x7F && (b[offset + 1] & 0x80) != 0); -} - -// IEEE 754 single precision, big-endian, rounded to 6 decimals (about 0.1 m) as in the TBEL converter. -function lansitecFloat(b, offset) { - var bits = lansitecUint(b, offset, 4); - var exponent = Math.floor(bits / 8388608) % 256; - var mantissa = bits % 8388608; - var value = exponent == 0 ? mantissa * Math.pow(2, -149) : (1 + mantissa / 8388608) * Math.pow(2, exponent - 127); - value = Math.round(value * 1000000) / 1000000; - return bits >= 2147483648 ? -value : value; -} - -// SNR, unit 0.01 dB, signed. The codecs read it unsigned. -function lansitecSnr(b, offset) { - var v = lansitecUint(b, offset, 2); - if (v > 32767) { - v = v - 65536; - } - return v / 100; -} - -// CHGSTAT: 0 Not charging, then the codes of Charging and Charging completed. -function lansitecChargeState(values, charge, charging, completed) { - if (charge == 0) { - values.chargeState = 'Not charging'; - } else if (charge == charging) { - values.chargeState = 'Charging'; - } else if (charge == completed) { - values.chargeState = 'Charging completed'; - } -} - -// Register of the SocketSync gateway (5.1) and the contact tracing badge (4.5.1): Type (ADR, MODE), SMODE, POWER, -// CFG, POS (2, unit 5 s), then HB (unit 30 s) at hbOffset. -function lansitecRegister(values, b, hbOffset) { - values.adr = (b[0] & 0x08) != 0 ? 'ON' : 'OFF'; - if (LANSITEC_MODES[b[0] & 0x07] != '') { - values.mode = LANSITEC_MODES[b[0] & 0x07]; - } - if (LANSITEC_SMODES.indexOf(b[1]) >= 0) { - values.loraWanFrequencyBand = LANSITEC_BANDS[LANSITEC_SMODES.indexOf(b[1])]; - } - values.loraTxPower = (b[2] >> 3) & 0x1F; - values.dataRate = 'DR' + ((b[3] >> 4) & 0x0F); - values.positionReportInterval = lansitecUint(b, 4, 2) * 5; - values.heartbeatInterval = b[hbOffset] * 30; -} - -// Heartbeat of the SocketSync gateway (5.2) and the contact tracing badge (4.5.2): Type, VOL, RSSI (unit -1 dBm), -// SNR (2), REV, CHGSTAT (0x00, 0x50, 0x60 and 0x0, 0x5, 0x6). -function lansitecHeartbeat(values, b, charging, completed) { - values.loraRssi = -b[2]; - values.loraSnr = lansitecSnr(b, 3); - values.deviceFirmwareVersion = lansitecHex(b, 5, 6); - lansitecChargeState(values, b[6], charging, completed); -} - -// Acknowledgment (Plug Bluetooth Gateway 5.5, Bluetooth Badge 4.5.4): Type bits 3-0 the result, then the MSGID. -function lansitecAcknowledgment(values, b) { - values.result = (b[0] & 0x0F) == 0 ? 'Success' : 'Failure'; - values.msgid = b[1]; -} - -// Bluetooth Gateway 2.27. -function lansitecGatewayRawValues(values, b, model, type) { - if (type == 1 && b.length >= 9) { - // 4.4.1: Network reconnection interval, unit 5 minutes. - values.networkReconnectionInterval = b[8] * 300; - } else if (type == 2 && b.length >= 8) { - // 4.4.2: VOL is the battery voltage (x 0.01 + 1.5 V) on the macro gateway, the battery level (%) on the others. - if (model == 'macro-bluetooth-gateway') { - values.batteryVoltage = (b[1] + 150) / 100; - } else { - values.batteryLevel = b[1]; - } - values.loraSnr = lansitecSnr(b, 3); - // VER 108 is V1.08. - var version = lansitecUint(b, 5, 2); - values.deviceFirmwareVersion = 'V' + Math.floor(version / 100) + '.' + (version % 100 < 10 ? '0' : '') + version % 100; - // CHGSTAT on the compact and solar gateway only. - if (model == 'compact-bluetooth-gateway' || model == 'solar-bluetooth-gateway') { - lansitecChargeState(values, b[7], 0x50, 0x60); - } - } else if (type == 0x8 || type == 0x9 || type == 0xA || type == 0xE) { - delete values.beacons; - var beacons = lansitecGatewayBeacons(b, type); - if (beacons != null) { - values.beacons = beacons; - } - } -} - -// 4.4.4-4.4.6: Type (bits 3-0: number of devices), Data and RSSI (value - 256) of each device; one data length per -// message (4.4.3). 4.4.7 Multi-Device: a Device Type ID before each device, decoded when all have the same length. -function lansitecGatewayBeacons(b, type) { - var count = b[0] & 0x0F; - if (count == 0 || (b.length - 1) % count != 0) { - return null; - } - var step = (b.length - 1) / count; - var head = type == 0xE ? 1 : 0; - var dataLength = step - head - 1; - if (dataLength < 1) { - return null; - } - var list = []; - for (var i = 0; i < count; i++) { - var start = 1 + i * step; - var deviceType = type - 7; - if (type == 0xE) { - deviceType = b[start]; - if (deviceType < 1 || deviceType > 3) { - return null; - } - } - list.push({ - deviceType: deviceType, - data: lansitecHex(b, start + head, start + head + dataLength), - rssi: b[start + step - 1] - 256 }); + delete values.history; } - return list; -} - -// Badge Tracker v5.30. -function lansitecBadgeRawValues(values, b, type) { - if (type == 1 && b.length == 14) { - // 5.1: GNSSPRI (2), Heartbeat, Version (2, 0x0525 is V5.25), CFMMSG, HBCOUNT, Fall (unit 0.5 m). - values.gnssPositionReportInterval = lansitecUint(b, 6, 2) * 5; - values.heartbeatInterval = b[8] * 30; - var major = lansitecHex(b, 9, 10); - if (major.charAt(0) == '0') { - major = major.substring(1); - } - values.deviceFirmwareVersion = 'V' + major + '.' + lansitecHex(b, 10, 11); - values.cfmmsg = b[11]; - values.hbcount = b[12]; - values.fallDetectionThreshold = b[13] * 0.5; - } else if (type == 2 && b.length >= 6) { - // 5.2: SNR (2) since firmware 0x0501, decided by the length (SNREN is not reliable). Then GNSSSTAT and - // VIBSTATE (moving state 0-9), CHGSTAT. The codec always reads the SNR. - var state = 3; - if (b.length >= 8) { - values.loraSnr = lansitecSnr(b, 3); - state = 5; - } else { - delete values.gnssState; - delete values.chargeState; - if (LANSITEC_BADGE_GNSS_STATES[(b[3] >> 4) & 0x0F]) { - values.gnssState = LANSITEC_BADGE_GNSS_STATES[(b[3] >> 4) & 0x0F]; - } - if (LANSITEC_BADGE_CHARGE_STATES[(b[4] >> 4) & 0x0F]) { - values.chargeState = LANSITEC_BADGE_CHARGE_STATES[(b[4] >> 4) & 0x0F]; - } - } - values.movingState = b[state] & 0x0F; - } else if (type == 3 && b.length >= 13) { - // 5.3: Flag 0x1 is a failed fix. IEEE 754 floats (the codec divides by 0x7fffff), UTC seconds (the codec - // adds 8 hours). No position from NaN, as in the TBEL converter. - if ((b[0] & 0x0F) == 0 && lansitecFloatIsNumber(b, 1) && lansitecFloatIsNumber(b, 5)) { - values.longitude = lansitecFloat(b, 1); - values.latitude = lansitecFloat(b, 5); - values.gnssTime = lansitecUint(b, 9, 4) * 1000; - } + if (values.tbVersionIds != null) { + versionIds = values.tbVersionIds; + delete values.tbVersionIds; } } - -// Temperature Humidity Sensor 2.2.2: VER 1 has the SNR, VER 0 not (the codec always reads it). TEMP_INT bit 7 is the sign. -function lansitecTemperatureHumidityRawValues(values, b) { - var offset = (b[0] & 0x0F) == 1 ? 5 : 3; - if (b.length < offset + 3) { - return; - } - if (offset == 5) { - values.loraSnr = lansitecSnr(b, 3); - } - var temperature = (b[offset] & 0x7F) + b[offset + 1] / 100; - if ((b[offset] & 0x80) != 0) { - temperature = -temperature; - } - values.temperature = Math.round(temperature * 100) / 100; - values.humidity = b[offset + 2]; +var brandId = metadata.brandId; +var modelId = metadata.modelId; +if ((brandId == null || modelId == null) && versionIds != null) { + brandId = versionIds.brand_id; + modelId = versionIds.model_id; } - -// Plug Bluetooth Gateway 1.5, the SocketSync gateway. -function lansitecSocketSyncValues(b) { - var values = {}; - if (b.length == 0) { - return values; - } - var type = (b[0] >> 4) & 0x0F; - if (type == 0x4 && b.length >= 10) { - // 5.1 Registration: ..., BLE Receiving Duration, HB, Beacon_QTY (half of the limit), CRC (2). - lansitecRegister(values, b, 7); - values.continuousBleReceiving = (b[2] & 0x02) != 0 ? 'Continuous Bluetooth receiving is enabled' : 'Continuous Bluetooth receiving is disabled'; - values.powerDownFlag = (b[2] & 0x01) != 0 ? 'Automatic shutdown status' : 'Normal status'; - if (LANSITEC_SOCKET_SYNC_TYPES[(b[3] >> 1) & 0x07] != null) { - values.gatewayType = LANSITEC_SOCKET_SYNC_TYPES[(b[3] >> 1) & 0x07]; - } - values.rssiSortingMethod = (b[3] & 0x01) != 0 ? 'Sort by the maximum value' : 'Sort by average'; - values.bluetoothReceivingDuration = b[6]; - values.beaconQuantityLimit = b[8] * 2; - } else if (type == 0x2 && b.length >= 7) { - // 5.2 Heartbeat. VOL (%) keeps its name: the dashboard of the IoT Hub package reads it. - values.vol = b[1]; - lansitecHeartbeat(values, b, 0x50, 0x60); - } else if (type == 0x6 || type == 0xE) { - // 5.3 Beacon, 5.4 Alarm Beacon List: Type (bits 3-0: number of beacons), then UUID tail, Major (2), Minor (2), - // RSSI (value - 256) of each. Table 7: the UUID prefix ends with 56 for tails 0x56-0xFF, 57 for 0x00-0x1E. - var count = b[0] & 0x0F; - if (count > 0 && b.length >= 1 + count * 6) { - var list = []; - for (var i = 0; i < count; i++) { - var start = 1 + i * 6; - list.push({ - uuid: '5C38DBDE567C4CCAB1DA40A8AD465' + (b[start] < 0x56 ? '7' : '6') + lansitecHex(b, start, start + 1), - major: lansitecUint(b, start + 1, 2), - minor: lansitecUint(b, start + 3, 2), - rssi: b[start + 5] - 256 - }); - } - values[type == 0x6 ? 'beacons' : 'alarmBeacons'] = list; - } - } else if (type == 0xF && b.length >= 2) { - lansitecAcknowledgment(values, b); +var profile = 'LoRaWAN Device'; +if (brandId != null && modelId != null) { + profile = brandId + '-' + modelId; + // The profile name chosen at install, kept in the integration metadata + var customProfile = metadata['profile.' + profile]; + if (customProfile != null && customProfile != '') { + profile = customProfile; } - return values; } - -// Bluetooth Badge 4.5.5 TimeMark: bit n set means seen n * 10 to n * 10 + 9 s before the message, 10 s per bit. -function lansitecContactBadgeSeen(item, mark) { - item.timeMark = mark; - var bits = 0; - for (var i = 0; i < 8; i++) { - bits += (mark >> i) & 1; - } - item.seenTime = bits * 10; - return item; +if (history.length == 0) { + return { profile: profile, telemetry: { ts: metadata.ts, values: values } }; } +history.push({ ts: metadata.ts, values: values }); +return { profile: profile, telemetry: history }; -// LoRaWAN Bluetooth Badge 2.78, the contact tracing badge. Major and Minor are hex (a badge's: the end of its DevEUI). -function lansitecContactBadgeValues(b) { - var values = {}; - if (b.length == 0) { - return values; - } - var type = (b[0] >> 4) & 0x0F; - var i, start; - if (type == 0x1 && b.length >= 9) { - // 4.5.1 Register: ..., HB, BT (2), CRC (2). The receiving threshold setting has no unit in the datasheet. - lansitecRegister(values, b, 6); - var bt = lansitecUint(b, 7, 2); - values.buzzerAndVibrator = ((bt >> 11) & 1) == 1 ? 'ON' : 'OFF'; - if (((bt >> 8) & 0x07) > 0) { - values.bluetoothTxPower = LANSITEC_CONTACT_BADGE_BT_POWERS[(bt >> 8) & 0x07]; - } - values.proximityDetection = ((bt >> 7) & 1) == 1 ? 'ON' : 'OFF'; - values.bluetoothReceivingThresholdSetting = bt & 0x7F; - } else if (type == 0x2 && b.length >= 7) { - // 4.5.2 Heartbeat: ..., CRC (2), BTSTATUS (2), then the friend badges. - values.batteryLevel = b[1]; - lansitecHeartbeat(values, b, 0x5, 0x6); - if (b.length >= 11) { - var status = lansitecUint(b, 9, 2); - if (LANSITEC_CONTACT_BADGE_BUZZER[(status >> 8) & 0x0F] != null) { - values.vibratorBuzzerStatus = LANSITEC_CONTACT_BADGE_BUZZER[(status >> 8) & 0x0F]; - } - // 0 means not changed. The threshold is for range bit 7 = 0: -73 to -87 dBm. - if (((status >> 4) & 0x07) > 0) { - values.bluetoothTxPower = LANSITEC_CONTACT_BADGE_BT_POWERS[(status >> 4) & 0x07]; - } - if ((status & 0x0F) > 0 && ((status >> 7) & 1) == 0) { - values.bluetoothReceivingThreshold = -72 - (status & 0x0F); - } - } - // Friend field: Type 0xC with the number of friend badges, then Major (2) and Minor (2) of each. - if (b.length >= 12 && ((b[11] >> 4) & 0x0F) == 0xC && b.length >= 12 + (b[11] & 0x0F) * 4) { - var friends = []; - for (i = 0; i < (b[11] & 0x0F); i++) { - start = 12 + i * 4; - friends.push({ major: lansitecHex(b, start, start + 2), minor: lansitecHex(b, start + 2, start + 4) }); - } - values.friends = friends; - } - } else if (type == 0x8) { - // 4.5.3 10s Instant Report: Type (bits 3-0: M beacons), Major (2), Minor (2), RSSI of each, then TOFF: - // (M + 1) / 2 bytes, 4 bits per beacon, high bits first. RSSI bit 7: 1 a beacon, 0 a badge; bits 6-0 - 128. - var count = b[0] & 0x0F; - if (count > 0 && b.length >= 1 + count * 5) { - var hasOffsets = b.length >= 1 + count * 5 + Math.floor((count + 1) / 2); - var list = []; - for (i = 0; i < count; i++) { - start = 1 + i * 5; - var item = { - major: lansitecHex(b, start, start + 2), - minor: lansitecHex(b, start + 2, start + 4), - type: (b[start + 4] & 0x80) != 0 ? 'Bluetooth beacon' : 'Badge', - rssi: (b[start + 4] & 0x7F) - 128 - }; - if (hasOffsets) { - var offsetByte = b[1 + count * 5 + Math.floor(i / 2)]; - item.timeOffset = i % 2 == 0 ? (offsetByte >> 4) & 0x0F : offsetByte & 0x0F; - } - list.push(item); - } - values.beacons = list; - } - } else if (type == 0xC) { - // 4.5.5 80s Periodic Report: badges as Major (2), Minor (2), TimeMark, then 0xFFFF and beacons as Minor (2), - // TimeMark (a beacon's Major is FFFF). - var badges = []; - var periodBeacons = []; - var pos = 1; - var inBeacons = false; - while (pos < b.length) { - if (!inBeacons && pos + 2 <= b.length && b[pos] == 0xFF && b[pos + 1] == 0xFF) { - inBeacons = true; - pos += 2; - } else if (!inBeacons && pos + 5 <= b.length) { - badges.push(lansitecContactBadgeSeen({ major: lansitecHex(b, pos, pos + 2), minor: lansitecHex(b, pos + 2, pos + 4) }, b[pos + 4])); - pos += 5; - } else if (inBeacons && pos + 3 <= b.length) { - periodBeacons.push(lansitecContactBadgeSeen({ minor: lansitecHex(b, pos, pos + 2) }, b[pos + 2])); - pos += 3; - } else { - return values; - } - } - if (badges.length + periodBeacons.length == (b[0] & 0x0F)) { - values.badges = badges; - values.beacons = periodBeacons; - } - } else if (type == 0xF && b.length >= 2) { - lansitecAcknowledgment(values, b); - } - return values; +function decodeToJson(payload) { + return JSON.parse(String.fromCharCode.apply(String, payload)); } - -return decodeThingsStackUplink(payload, metadata); diff --git a/ui-ngx/src/assets/converters/tbel-tti-decoder.raw b/ui-ngx/src/assets/converters/tbel-tti-decoder.raw index be3d8f9098..b4aed0a400 100644 --- a/ui-ngx/src/assets/converters/tbel-tti-decoder.raw +++ b/ui-ngx/src/assets/converters/tbel-tti-decoder.raw @@ -1,662 +1,46 @@ // The Things Stack adds decoded_payload for a device registered from the LoRaWAN Device Repository, and the payload // is then that JSON: its keys become telemetry. A Milesight-style history list keeps each record's time. // Such a device gets the profile -, so all devices of one model share it, -// or the name the integration metadata gives it under profile.-. Any other device gets LoRaWAN Device. -// Lansitec devices are decoded by the Lansitec block below. -function decodeThingsStackUplink(payload, metadata) { - var values = {}; - var attributes = {}; - var history = []; - var lansitec = null; - if (metadata.brandId == "lansitec") { - lansitec = lansitecUplink(payload, metadata); - } - if (lansitec != null) { - values = lansitec.telemetry; - attributes = lansitec.attributes; - } else if (metadata.payloadFormat == "JSON") { - values = decodeToJson(payload); - if (values.history instanceof java.util.List) { - foreach (record : values.history) { - if (record.timestamp != null) { - var ts = record.timestamp * 1000; - record.remove("timestamp"); - history.add({ ts: ts, values: record }); - } - } - values.remove("history"); - } - } - var profile = "LoRaWAN Device"; - if (metadata.brandId != null && metadata.modelId != null) { - profile = metadata.brandId + "-" + metadata.modelId; - // The profile name chosen at install, kept in the integration metadata - var customProfile = metadata["profile." + profile]; - if (customProfile != null && customProfile != "") { - profile = customProfile; - } - } - if (!attributes.isEmpty()) { - return { profile: profile, attributes: attributes }; - } - if (history.isEmpty()) { - return { profile: profile, telemetry: { ts: metadata.ts, values: values } }; - } - history.add({ ts: metadata.ts, values: values }); - return { profile: profile, telemetry: history }; -} - -// --------------------------------------------------------------------------------------------------------------------- -// Lansitec. The Repository codecs wrap everything in "bytes", write numbers with units ("85%", "-67dBm") and get some -// fields wrong: those are decoded from the raw bytes (metadata.data) by the datasheets. Remove a fix when the codec is fixed. -// Datasheets: Bluetooth Gateway 2.27, Badge Tracker v5.30, Temperature Humidity Sensor 1.26, Plug Bluetooth Gateway 1.5 -// (SocketSync), LoRaWAN Bluetooth Badge 2.78 (contact tracing badge), Container Tracker 0.72, Helmet Tracker v0.14. -var LANSITEC_UNITS = ["dBm", "dB", "%", "V", "sec", "min", "s", "℃", "level", " meter"]; -var LANSITEC_GATEWAYS = ["compact-bluetooth-gateway", "indoor-bluetooth-gateway", "macro-bluetooth-gateway", "solar-bluetooth-gateway"]; -var LANSITEC_SOCKET_SYNC = "socket-sync-bluetooth-gateway"; -var LANSITEC_CONTACT_BADGE = "contact-tracing-badge"; - -// Codec key -> datasheet name. "-" drops the key: it is decoded from the raw bytes or is not a value. -// Bluetooth Gateway 4.4.1 Registration, 4.4.2 Heartbeat. -var LANSITEC_GATEWAY_KEYS = { - adr: "adr", mode: "mode", LoRaWANband: "loraWanFrequencyBand", LoRaTxpower: "loraTxPower", - ContinuousReceive: "continuousBleReceiving", dr: "dataRate", pos: "positionReportInterval", - hb: "heartbeatInterval", BLEReceivingDuration: "bluetoothReceivingDuration", - rssi: "loraRssi", snr: "-", ver: "-", vol: "-", chgstate: "-", number: "-" -}; -// Badge Tracker 5.1 Registration (the codec skips GNSSPRI), 5.2 Heartbeat, 5.3 GNSS Position, 5.4 Beacon, 5.5 Alarm. -var LANSITEC_BADGE_KEYS = { - ADR: "adr", "Lora Txpower": "loraTxPower", DR: "dataRate", GNSSEN: "gnssEnabled", - "Position mode": "positioningReportMode", "Position period": "bluetoothPositionReportInterval", - "Heartbeat period": "-", "Firmware version": "-", CFMMSG: "-", HBCOUNT: "-", "Fall threshold": "-", - "Battery level": "batteryLevel", RSSI: "loraRssi", SNR: "-", "GPS state": "gnssState", - "Move state": "-", "Charge state": "chargeState", - Longitude: "-", Latitude: "-", Time: "-", Length: "-", Vibstate: "-", "Alarm type": "alarm" -}; -// Temperature Humidity Sensor 2.2.1 Register, 2.2.2 Periodical data (VER is the message version, not the firmware). -var LANSITEC_TEMPERATURE_HUMIDITY_KEYS = { - adr: "adr", mode: "mode", smode: "smode", LoraTxpower: "loraTxPower", dr: "dataRate", repting: "reptind", - th: "temperatureHumidityReportPeriod", crc: "-", - ver: "-", vol: "batteryLevel", rssi: "loraRssi", snr: "-", temperature: "-", humidity: "-" -}; -// The other models: the integration writes rssi, snr and firmwareVersion itself. chgstate: the tracking label has the -// gateway codec, which misreads CHGSTAT (Bluetooth Gateway 4.4.2). number: the beacons list has them. -var LANSITEC_CLASH_KEYS = { - rssi: "loraRssi", RSSI: "loraRssi", snr: "loraSnr", SNR: "loraSnr", - firmwareVersion: "deviceFirmwareVersion", "Firmware version": "deviceFirmwareVersion", - type: "-", "Message type": "-", chgstate: "-", number: "-" -}; -// Beacon keys: Devicetype1data1, Device1data, dev1major, dev1.major, pos1minor, asset1rssi... -var LANSITEC_BEACON_PREFIXES = ["devicetype1", "devicetype2", "devicetype3", "device", "dev", "pos", "asset"]; -var LANSITEC_BEACON_FIELDS = ["data", "major", "minor", "rssi", "id"]; -// Register Type bits 2-0 and SMODE: the LoRa frequency plan (Plug Bluetooth Gateway 5.1, Bluetooth Badge 4.5.1). -var LANSITEC_MODES = ["", "AU915", "IN865", "CN470", "AS923", "EU433", "EU868", "US915"]; -var LANSITEC_SMODES = [1, 2, 4, 8, 16, 32, 64]; -var LANSITEC_BANDS = ["AU915", "IN865", "CN470", "AS923", "EU433", "EU868", "US915"]; -// Badge Tracker 5.2 Heartbeat, as the codec writes them. -var LANSITEC_BADGE_GNSS_STATES = ["GPS OFF", "GPS boot", "GPS locating", "GPS located", "", "", "", "", "", "GPS no signal"]; -var LANSITEC_BADGE_CHARGE_STATES = ["Power cable disconnected", "", "", "", "", "Charging", "Charge complete"]; -// Plug Bluetooth Gateway 5.1 CFG bits 3-1. -var LANSITEC_SOCKET_SYNC_TYPES = ["Outdoor gateway", "Indoor gateway", "Plug gateway"]; -// Bluetooth Badge 4.5.1 BT bits 10-8, 4.5.2 BTSTATUS bits 6-4 (dBm, 0 is not a power) and bits 11-8. -var LANSITEC_CONTACT_BADGE_BT_POWERS = [0, 4, 0, -4, -8, -12, -16, -20]; -var LANSITEC_CONTACT_BADGE_BUZZER = ["No action", "Vibrator disabled, buzzer muted (restore)", - "Vibrator disabled, buzzer half sound level", "Vibrator disabled, buzzer full sound level", - "Vibrator enabled, buzzer muted", "Vibrator enabled, buzzer half sound level", - "Vibrator enabled, buzzer full sound level", "Vibrator disabled, buzzer muted (no restore)", - "Beacon-in-tracker and proximity detection disabled"]; - -// { telemetry, attributes }, or null when the uplink is not one the Lansitec block decodes. -function lansitecUplink(payload, metadata) { - var model = metadata.modelId; - var values = null; - var register = false; - if ((model == LANSITEC_SOCKET_SYNC || model == LANSITEC_CONTACT_BADGE) && metadata.data != null) { - // Their codecs are the gateway's and misread every message: decoded from the raw bytes only. - var b = base64ToBytes(metadata.data); - values = model == LANSITEC_SOCKET_SYNC ? lansitecSocketSyncValues(b) : lansitecContactBadgeValues(b); - register = b.length > 0 && ((b[0] >> 4) & 0x0F) == (model == LANSITEC_SOCKET_SYNC ? 0x4 : 0x1); - } else if (metadata.payloadFormat == "JSON") { - var json = decodeToJson(payload); - if (!isMap(json.bytes)) { - return null; - } - var raw = json.bytes; - values = lansitecValues(raw, model); - if (metadata.data != null) { - lansitecRawValues(values, base64ToBytes(metadata.data), model); - } - var messageType = raw.type != null ? raw.type : raw["Message type"]; - register = messageType == "Register"; - } else { - return null; - } - // Register holds the device settings: attributes, not telemetry. - if (register) { - return { telemetry: {}, attributes: values }; - } - return { telemetry: values, attributes: {} }; -} - -// Strips the unit. Periods become seconds: the codecs give most of them in seconds, a few in minutes. -function lansitecNumber(v) { - if (!(v instanceof String)) { - return v; - } - foreach (u : LANSITEC_UNITS) { - if (v.endsWith(u)) { - var n = v.substring(0, v.length() - u.length()).trim(); - if (n.matches("-?[0-9]+(\\.[0-9]+)?")) { - return u == "min" ? parseDouble(n) * 60 : parseDouble(n); - } - } - } - return v; -} - -function lansitecLowerStart(part) { - var n = 0; - while (n < part.length() && part.substring(n, n + 1) != part.substring(n, n + 1).toLowerCase()) { - n = n + 1; - } - if (n == part.length()) { - return part.toLowerCase(); - } - if (n > 1) { - return part.substring(0, n - 1).toLowerCase() + part.substring(n - 1); - } - return part.substring(0, 1).toLowerCase() + part.substring(1); -} - -function lansitecCamelCase(key) { - var parts = key.split("[ .]+"); - var result = lansitecLowerStart(parts[0]); - for (var i = 1; i < parts.length; i++) { - var part = lansitecLowerStart(parts[i]); - result = result + part.substring(0, 1).toUpperCase() + part.substring(1); - } - return result; -} - -function lansitecBeaconKey(key) { - var k = key.toLowerCase().replace(".", ""); - foreach (p : LANSITEC_BEACON_PREFIXES) { - if (k.startsWith(p)) { - var rest = k.substring(p.length()); - foreach (f : LANSITEC_BEACON_FIELDS) { - if (rest.endsWith(f) && rest.substring(0, rest.length() - f.length()).matches("[0-9]+")) { - return { group: p, index: rest.substring(0, rest.length() - f.length()), field: f }; - } - if (rest.startsWith(f) && rest.substring(f.length()).matches("[0-9]+")) { - return { group: p, index: rest.substring(f.length()), field: f }; - } - } - } - } - return null; -} - -function lansitecKeyTable(model) { - if (LANSITEC_GATEWAYS.contains(model)) { - return LANSITEC_GATEWAY_KEYS; - } - if (model == "badge-tracker") { - return LANSITEC_BADGE_KEYS; - } - if (model == "temperature-humidity-sensor") { - return LANSITEC_TEMPERATURE_HUMIDITY_KEYS; - } - return null; -} - -function lansitecValues(raw, model) { - var table = lansitecKeyTable(model); - var values = {}; - var beacons = {}; - foreach (key : raw.keySet()) { - var value = raw[key]; - var b = lansitecBeaconKey(key); - if (b != null) { - var id = b.group + b.index; - if (beacons[id] == null) { - beacons[id] = { order: parseInt(b.index) }; - } - if (b.field == "rssi") { - beacons[id].rssi = lansitecNumber(value); - } else if (b.field == "data" || b.field == "major" || b.field == "minor") { - beacons[id][b.field] = value; - } - } else if (table == null && key.matches("time[1-8]?") && value instanceof String) { - // Container Tracker 5.3 and 5.7, Helmet Tracker 6.3: UTC time. The codecs add 8 hours and quote it. - var gnssDate = new Date(value.replace("\"", "")); - values["gnssTime" + key.substring(4)] = gnssDate.getTime() - 8 * 60 * 60 * 1000; - } else if (key == "pressure" && model == "helmet-sensor" && !(value instanceof String)) { - // Helmet Tracker 6.3: the value * 10, unit 0.1 Pa. - values.pressure = value / 10.0; - } else if (table != null && table.containsKey(key)) { - var tableName = table[key]; - if (tableName != "-") { - values[tableName] = lansitecNumber(value); - } - } else if (LANSITEC_CLASH_KEYS.containsKey(key)) { - var clashName = LANSITEC_CLASH_KEYS[key]; - if (clashName != "-") { - values[clashName] = lansitecNumber(value); - } - } else { - var codecName = lansitecCamelCase(key); - values[codecName] = lansitecNumber(value); - } - } - if (!beacons.isEmpty()) { - values.beacons = lansitecBeaconList(beacons); - } - return values; -} - -// In the order of their numbers, which can have gaps (Container Tracker 5.7: dev1, dev3...). -function lansitecBeaconList(beacons) { - var list = []; - var added = {}; - while (list.size() < beacons.size()) { - var next = null; - foreach (id : beacons.keySet()) { - if (added[id] == null && (next == null || beacons[id].order < beacons[next].order)) { - next = id; - } - } - added[next] = true; - var b = beacons[next]; - var item = {}; - if (b.data != null) { - item.data = b.data; - } - if (b.major != null) { - item.major = b.major; - item.minor = b.minor; - } - item.rssi = b.rssi; - list.add(item); - } - // The gateway codec reads every device after the first at a 5-byte step: right only for 4-byte data. - if (list.size() > 1 && list[0].data != null && list[0].data.length() != 8) { - var first = []; - first.add(list[0]); - return first; - } - return list; -} - -function lansitecRawValues(values, b, model) { - if (b.length == 0) { - return; - } - var type = (b[0] >> 4) & 0x0F; - if (LANSITEC_GATEWAYS.contains(model)) { - lansitecGatewayRawValues(values, b, model, type); - } else if (model == "badge-tracker") { - lansitecBadgeRawValues(values, b, type); - } else if (model == "temperature-humidity-sensor" && type == 2) { - lansitecTemperatureHumidityRawValues(values, b); - } -} - -// IEEE 754 float, big-endian at offset: false when its exponent is 0xFF (NaN or infinity). -function lansitecFloatIsNumber(b, offset) { - return !((b[offset] & 0x7F) == 0x7F && (b[offset + 1] & 0x80) != 0); -} - -// SNR, unit 0.01 dB, signed. The codecs read it unsigned. -function lansitecSnr(b, offset) { - var v = parseBytesToInt(b, offset, 2, true); - if (v > 32767) { - v = v - 65536; - } - return v / 100.0; -} - -// CHGSTAT: 0 Not charging, then the codes of Charging and Charging completed. -function lansitecChargeState(values, charge, charging, completed) { - if (charge == 0) { - values.chargeState = "Not charging"; - } else if (charge == charging) { - values.chargeState = "Charging"; - } else if (charge == completed) { - values.chargeState = "Charging completed"; - } -} - -// Register of the SocketSync gateway (5.1) and the contact tracing badge (4.5.1): Type (ADR, MODE), SMODE, POWER, -// CFG, POS (2, unit 5 s), then HB (unit 30 s) at hbOffset. -function lansitecRegister(values, b, hbOffset) { - values.adr = (b[0] & 0x08) != 0 ? "ON" : "OFF"; - var mode = b[0] & 0x07; - if (LANSITEC_MODES[mode] != "") { - values.mode = LANSITEC_MODES[mode]; - } - var band = LANSITEC_SMODES.indexOf(b[1] & 0xFF); - if (band >= 0) { - values.loraWanFrequencyBand = LANSITEC_BANDS[band]; - } - values.loraTxPower = (b[2] >> 3) & 0x1F; - values.dataRate = "DR" + ((b[3] >> 4) & 0x0F); - values.positionReportInterval = parseBytesToInt(b, 4, 2, true) * 5; - values.heartbeatInterval = (b[hbOffset] & 0xFF) * 30; -} - -// Heartbeat of the SocketSync gateway (5.2) and the contact tracing badge (4.5.2): Type, VOL, RSSI (unit -1 dBm), -// SNR (2), REV, CHGSTAT (0x00, 0x50, 0x60 and 0x0, 0x5, 0x6). -function lansitecHeartbeat(values, b, charging, completed) { - values.loraRssi = 0 - (b[2] & 0xFF); - values.loraSnr = lansitecSnr(b, 3); - values.deviceFirmwareVersion = bytesToHex(b).substring(10, 12); - lansitecChargeState(values, b[6] & 0xFF, charging, completed); -} - -// Acknowledgment (Plug Bluetooth Gateway 5.5, Bluetooth Badge 4.5.4): Type bits 3-0 the result, then the MSGID. -function lansitecAcknowledgment(values, b) { - values.result = (b[0] & 0x0F) == 0 ? "Success" : "Failure"; - values.msgid = b[1] & 0xFF; -} - -// Bluetooth Gateway 2.27. -function lansitecGatewayRawValues(values, b, model, type) { - if (type == 1 && b.length >= 9) { - // 4.4.1: Network reconnection interval, unit 5 minutes. - values.networkReconnectionInterval = (b[8] & 0xFF) * 300; - } else if (type == 2 && b.length >= 8) { - // 4.4.2: VOL is the battery voltage (x 0.01 + 1.5 V) on the macro gateway, the battery level (%) on the others. - if (model == "macro-bluetooth-gateway") { - values.batteryVoltage = ((b[1] & 0xFF) + 150) / 100.0; - } else { - values.batteryLevel = b[1] & 0xFF; - } - values.loraSnr = lansitecSnr(b, 3); - // VER 108 is V1.08. - var version = parseBytesToInt(b, 5, 2, true); - var minor = (version % 100).intValue(); - values.deviceFirmwareVersion = "V" + (version / 100).intValue() + "." + (minor < 10 ? "0" : "") + minor; - // CHGSTAT on the compact and solar gateway only. - if (model == "compact-bluetooth-gateway" || model == "solar-bluetooth-gateway") { - lansitecChargeState(values, b[7] & 0xFF, 0x50, 0x60); - } - } else if (type == 0x8 || type == 0x9 || type == 0xA || type == 0xE) { - values.remove("beacons"); - var beacons = lansitecGatewayBeacons(b, type); - if (beacons != null) { - values.beacons = beacons; - } - } -} - -// 4.4.4-4.4.6: Type (bits 3-0: number of devices), Data and RSSI (value - 256) of each device; one data length per -// message (4.4.3). 4.4.7 Multi-Device: a Device Type ID before each device, decoded when all have the same length. -function lansitecGatewayBeacons(b, type) { - var count = b[0] & 0x0F; - if (count == 0 || (b.length - 1) % count != 0) { - return null; - } - // TBEL division gives a Double: array indexes need an int. - var step = ((b.length - 1) / count).intValue(); - var head = type == 0xE ? 1 : 0; - var dataLength = step - head - 1; - if (dataLength < 1) { - return null; - } - var hex = bytesToHex(b); - var list = []; - for (var i = 0; i < count; i++) { - var start = 1 + i * step; - var deviceType = type - 7; - if (type == 0xE) { - deviceType = b[start] & 0xFF; - if (deviceType < 1 || deviceType > 3) { - return null; - } - } - var item = {}; - item.deviceType = deviceType; - item.data = hex.substring(2 * (start + head), 2 * (start + head + dataLength)); - item.rssi = (b[start + step - 1] & 0xFF) - 256; - list.add(item); - } - return list; -} - -// Badge Tracker v5.30. -function lansitecBadgeRawValues(values, b, type) { - if (type == 1 && b.length == 14) { - // 5.1: GNSSPRI (2), Heartbeat, Version (2, 0x0525 is V5.25), CFMMSG, HBCOUNT, Fall (unit 0.5 m). - values.gnssPositionReportInterval = parseBytesToInt(b, 6, 2, true) * 5; - values.heartbeatInterval = (b[8] & 0xFF) * 30; - var version = bytesToHex(b).substring(18, 22); - var major = version.substring(0, 2); - if (major.startsWith("0")) { - major = major.substring(1); - } - values.deviceFirmwareVersion = "V" + major + "." + version.substring(2); - values.cfmmsg = b[11] & 0xFF; - values.hbcount = b[12] & 0xFF; - values.fallDetectionThreshold = (b[13] & 0xFF) * 0.5; - } else if (type == 2 && b.length >= 6) { - // 5.2: SNR (2) since firmware 0x0501, decided by the length (SNREN is not reliable). Then GNSSSTAT and - // VIBSTATE (moving state 0-9), CHGSTAT. The codec always reads the SNR. - var state = 3; - if (b.length >= 8) { - values.loraSnr = lansitecSnr(b, 3); - state = 5; - } else { - var gnss = (b[3] >> 4) & 0x0F; - var charge = (b[4] >> 4) & 0x0F; - values.remove("gnssState"); - values.remove("chargeState"); - if (gnss < LANSITEC_BADGE_GNSS_STATES.size() && LANSITEC_BADGE_GNSS_STATES[gnss] != "") { - values.gnssState = LANSITEC_BADGE_GNSS_STATES[gnss]; - } - if (charge < LANSITEC_BADGE_CHARGE_STATES.size() && LANSITEC_BADGE_CHARGE_STATES[charge] != "") { - values.chargeState = LANSITEC_BADGE_CHARGE_STATES[charge]; - } - } - values.movingState = b[state] & 0x0F; - } else if (type == 3 && b.length >= 13) { - // 5.3: Flag 0x1 is a failed fix. IEEE 754 floats (the codec divides by 0x7fffff) rounded to 6 decimals, - // UTC seconds (the codec adds 8 hours). No position from NaN: parseBytesToFloat fails on it. - if ((b[0] & 0x0F) == 0 && lansitecFloatIsNumber(b, 1) && lansitecFloatIsNumber(b, 5)) { - values.longitude = toFixed(parseBytesToFloat(b, 1, 4, true) * 1.0, 6); - values.latitude = toFixed(parseBytesToFloat(b, 5, 4, true) * 1.0, 6); - values.gnssTime = parseBytesToLong(b, 9, 4, true) * 1000; - } - } -} - -// Temperature Humidity Sensor 2.2.2: VER 1 has the SNR, VER 0 not (the codec always reads it). TEMP_INT bit 7 is the sign. -function lansitecTemperatureHumidityRawValues(values, b) { - var offset = (b[0] & 0x0F) == 1 ? 5 : 3; - if (b.length < offset + 3) { - return; - } - if (offset == 5) { - values.loraSnr = lansitecSnr(b, 3); - } - var temperature = (b[offset] & 0x7F) + (b[offset + 1] & 0xFF) / 100.0; - if ((b[offset] & 0x80) != 0) { - temperature = -temperature; - } - values.temperature = toFixed(temperature, 2); - values.humidity = b[offset + 2] & 0xFF; -} - -// Plug Bluetooth Gateway 1.5, the SocketSync gateway. -function lansitecSocketSyncValues(b) { - var values = {}; - if (b.length == 0) { - return values; - } - var type = (b[0] >> 4) & 0x0F; - if (type == 0x4 && b.length >= 10) { - // 5.1 Registration: ..., BLE Receiving Duration, HB, Beacon_QTY (half of the limit), CRC (2). - lansitecRegister(values, b, 7); - values.continuousBleReceiving = (b[2] & 0x02) != 0 ? "Continuous Bluetooth receiving is enabled" : "Continuous Bluetooth receiving is disabled"; - values.powerDownFlag = (b[2] & 0x01) != 0 ? "Automatic shutdown status" : "Normal status"; - var gatewayType = (b[3] >> 1) & 0x07; - if (gatewayType < LANSITEC_SOCKET_SYNC_TYPES.size()) { - values.gatewayType = LANSITEC_SOCKET_SYNC_TYPES[gatewayType]; - } - values.rssiSortingMethod = (b[3] & 0x01) != 0 ? "Sort by the maximum value" : "Sort by average"; - values.bluetoothReceivingDuration = b[6] & 0xFF; - values.beaconQuantityLimit = (b[8] & 0xFF) * 2; - } else if (type == 0x2 && b.length >= 7) { - // 5.2 Heartbeat. VOL (%) keeps its name: the dashboard of the IoT Hub package reads it. - values.vol = b[1] & 0xFF; - lansitecHeartbeat(values, b, 0x50, 0x60); - } else if (type == 0x6 || type == 0xE) { - // 5.3 Beacon, 5.4 Alarm Beacon List: Type (bits 3-0: number of beacons), then UUID tail, Major (2), Minor (2), - // RSSI (value - 256) of each. Table 7: the UUID prefix ends with 56 for tails 0x56-0xFF, 57 for 0x00-0x1E. - var count = b[0] & 0x0F; - if (count > 0 && b.length >= 1 + count * 6) { - var hex = bytesToHex(b); - var list = []; - for (var i = 0; i < count; i++) { - var start = 1 + i * 6; - var item = {}; - item.uuid = "5C38DBDE567C4CCAB1DA40A8AD465" + ((b[start] & 0xFF) < 0x56 ? "7" : "6") + hex.substring(2 * start, 2 * start + 2); - item.major = parseBytesToInt(b, start + 1, 2, true); - item.minor = parseBytesToInt(b, start + 3, 2, true); - item.rssi = (b[start + 5] & 0xFF) - 256; - list.add(item); - } - if (type == 0x6) { - values.beacons = list; - } else { - values.alarmBeacons = list; - } - } - } else if (type == 0xF && b.length >= 2) { - lansitecAcknowledgment(values, b); - } - return values; -} - -// Bluetooth Badge 4.5.5 TimeMark: bit n set means seen n * 10 to n * 10 + 9 s before the message, 10 s per bit. -function lansitecContactBadgeSeen(item, mark) { - item.timeMark = mark; - var bits = 0; - for (var i = 0; i < 8; i++) { - if (((mark >> i) & 1) == 1) { - bits = bits + 1; - } - } - item.seenTime = bits * 10; -} - -// LoRaWAN Bluetooth Badge 2.78, the contact tracing badge. Major and Minor are hex (a badge's: the end of its DevEUI). -function lansitecContactBadgeValues(b) { - var values = {}; - if (b.length == 0) { - return values; - } - var type = (b[0] >> 4) & 0x0F; - var hex = bytesToHex(b); - if (type == 0x1 && b.length >= 9) { - // 4.5.1 Register: ..., HB, BT (2), CRC (2). The receiving threshold setting has no unit in the datasheet. - lansitecRegister(values, b, 6); - var bt = parseBytesToInt(b, 7, 2, true); - values.buzzerAndVibrator = ((bt >> 11) & 1) == 1 ? "ON" : "OFF"; - var power = (bt >> 8) & 0x07; - if (power > 0) { - values.bluetoothTxPower = LANSITEC_CONTACT_BADGE_BT_POWERS[power]; - } - values.proximityDetection = ((bt >> 7) & 1) == 1 ? "ON" : "OFF"; - values.bluetoothReceivingThresholdSetting = bt & 0x7F; - } else if (type == 0x2 && b.length >= 7) { - // 4.5.2 Heartbeat: ..., CRC (2), BTSTATUS (2), then the friend badges. - values.batteryLevel = b[1] & 0xFF; - lansitecHeartbeat(values, b, 0x5, 0x6); - if (b.length >= 11) { - var status = parseBytesToInt(b, 9, 2, true); - var buzzer = (status >> 8) & 0x0F; - if (buzzer < LANSITEC_CONTACT_BADGE_BUZZER.size()) { - values.vibratorBuzzerStatus = LANSITEC_CONTACT_BADGE_BUZZER[buzzer]; - } - // 0 means not changed. The threshold is for range bit 7 = 0: -73 to -87 dBm. - var statusPower = (status >> 4) & 0x07; - if (statusPower > 0) { - values.bluetoothTxPower = LANSITEC_CONTACT_BADGE_BT_POWERS[statusPower]; - } - var threshold = status & 0x0F; - if (threshold > 0 && ((status >> 7) & 1) == 0) { - values.bluetoothReceivingThreshold = -72 - threshold; - } - } - // Friend field: Type 0xC with the number of friend badges, then Major (2) and Minor (2) of each. - if (b.length >= 12 && ((b[11] >> 4) & 0x0F) == 0xC) { - var friendCount = b[11] & 0x0F; - if (b.length >= 12 + friendCount * 4) { - var friends = []; - for (var f = 0; f < friendCount; f++) { - var friendStart = 12 + f * 4; - friends.add({ major: hex.substring(2 * friendStart, 2 * friendStart + 4), - minor: hex.substring(2 * friendStart + 4, 2 * friendStart + 8) }); - } - values.friends = friends; - } - } - } else if (type == 0x8) { - // 4.5.3 10s Instant Report: Type (bits 3-0: M beacons), Major (2), Minor (2), RSSI of each, then TOFF: - // (M + 1) / 2 bytes, 4 bits per beacon, high bits first. RSSI bit 7: 1 a beacon, 0 a badge; bits 6-0 - 128. - var count = b[0] & 0x0F; - if (count > 0 && b.length >= 1 + count * 5) { - var hasOffsets = b.length >= 1 + count * 5 + ((count + 1) / 2).intValue(); - var list = []; - for (var i = 0; i < count; i++) { - var start = 1 + i * 5; - var item = {}; - item.major = hex.substring(2 * start, 2 * start + 4); - item.minor = hex.substring(2 * start + 4, 2 * start + 8); - var rssi = b[start + 4] & 0xFF; - item.type = (rssi & 0x80) != 0 ? "Bluetooth beacon" : "Badge"; - item.rssi = (rssi & 0x7F) - 128; - if (hasOffsets) { - var offsetByte = b[1 + count * 5 + (i / 2).intValue()] & 0xFF; - item.timeOffset = i % 2 == 0 ? (offsetByte >> 4) & 0x0F : offsetByte & 0x0F; - } - list.add(item); - } - values.beacons = list; - } - } else if (type == 0xC) { - // 4.5.5 80s Periodic Report: badges as Major (2), Minor (2), TimeMark, then 0xFFFF and beacons as Minor (2), - // TimeMark (a beacon's Major is FFFF). - var badges = []; - var periodBeacons = []; - var pos = 1; - var inBeacons = false; - while (pos < b.length) { - if (!inBeacons && pos + 2 <= b.length && (b[pos] & 0xFF) == 0xFF && (b[pos + 1] & 0xFF) == 0xFF) { - inBeacons = true; - pos = pos + 2; - } else if (!inBeacons && pos + 5 <= b.length) { - var badge = { major: hex.substring(2 * pos, 2 * pos + 4), minor: hex.substring(2 * pos + 4, 2 * pos + 8) }; - lansitecContactBadgeSeen(badge, b[pos + 4] & 0xFF); - badges.add(badge); - pos = pos + 5; - } else if (inBeacons && pos + 3 <= b.length) { - var beacon = { minor: hex.substring(2 * pos, 2 * pos + 4) }; - lansitecContactBadgeSeen(beacon, b[pos + 2] & 0xFF); - periodBeacons.add(beacon); - pos = pos + 3; - } else { - return values; - } - } - if (badges.size() + periodBeacons.size() == (b[0] & 0x0F)) { - values.badges = badges; - values.beacons = periodBeacons; - } - } else if (type == 0xF && b.length >= 2) { - lansitecAcknowledgment(values, b); - } - return values; -} - -return decodeThingsStackUplink(payload, metadata); +// or the name the integration metadata gives it under profile.-. +// A device that is not in the Repository can name its model in decoded_payload: its payload formatter adds +// tbVersionIds: { brand_id, model_id }, which is not telemetry. Any other device gets LoRaWAN Device. +var values = {}; +var history = []; +var versionIds = null; +if (metadata.payloadFormat == "JSON") { + values = decodeToJson(payload); + if (values.history instanceof java.util.List) { + foreach (record : values.history) { + if (record.timestamp != null) { + var ts = record.timestamp * 1000; + record.remove("timestamp"); + history.add({ ts: ts, values: record }); + } + } + values.remove("history"); + } + if (values.containsKey("tbVersionIds")) { + versionIds = values.tbVersionIds; + values.remove("tbVersionIds"); + } +} +var brandId = metadata.brandId; +var modelId = metadata.modelId; +if ((brandId == null || modelId == null) && versionIds instanceof java.util.Map) { + brandId = versionIds.brand_id; + modelId = versionIds.model_id; +} +var profile = "LoRaWAN Device"; +if (brandId != null && modelId != null) { + profile = brandId + "-" + modelId; + // The profile name chosen at install, kept in the integration metadata + var customProfile = metadata["profile." + profile]; + if (customProfile != null && customProfile != "") { + profile = customProfile; + } +} +if (history.isEmpty()) { + return { profile: profile, telemetry: { ts: metadata.ts, values: values } }; +} +history.add({ ts: metadata.ts, values: values }); +return { profile: profile, telemetry: history }; diff --git a/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw b/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw index be3d8f9098..b4aed0a400 100644 --- a/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw +++ b/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw @@ -1,662 +1,46 @@ // The Things Stack adds decoded_payload for a device registered from the LoRaWAN Device Repository, and the payload // is then that JSON: its keys become telemetry. A Milesight-style history list keeps each record's time. // Such a device gets the profile -, so all devices of one model share it, -// or the name the integration metadata gives it under profile.-. Any other device gets LoRaWAN Device. -// Lansitec devices are decoded by the Lansitec block below. -function decodeThingsStackUplink(payload, metadata) { - var values = {}; - var attributes = {}; - var history = []; - var lansitec = null; - if (metadata.brandId == "lansitec") { - lansitec = lansitecUplink(payload, metadata); - } - if (lansitec != null) { - values = lansitec.telemetry; - attributes = lansitec.attributes; - } else if (metadata.payloadFormat == "JSON") { - values = decodeToJson(payload); - if (values.history instanceof java.util.List) { - foreach (record : values.history) { - if (record.timestamp != null) { - var ts = record.timestamp * 1000; - record.remove("timestamp"); - history.add({ ts: ts, values: record }); - } - } - values.remove("history"); - } - } - var profile = "LoRaWAN Device"; - if (metadata.brandId != null && metadata.modelId != null) { - profile = metadata.brandId + "-" + metadata.modelId; - // The profile name chosen at install, kept in the integration metadata - var customProfile = metadata["profile." + profile]; - if (customProfile != null && customProfile != "") { - profile = customProfile; - } - } - if (!attributes.isEmpty()) { - return { profile: profile, attributes: attributes }; - } - if (history.isEmpty()) { - return { profile: profile, telemetry: { ts: metadata.ts, values: values } }; - } - history.add({ ts: metadata.ts, values: values }); - return { profile: profile, telemetry: history }; -} - -// --------------------------------------------------------------------------------------------------------------------- -// Lansitec. The Repository codecs wrap everything in "bytes", write numbers with units ("85%", "-67dBm") and get some -// fields wrong: those are decoded from the raw bytes (metadata.data) by the datasheets. Remove a fix when the codec is fixed. -// Datasheets: Bluetooth Gateway 2.27, Badge Tracker v5.30, Temperature Humidity Sensor 1.26, Plug Bluetooth Gateway 1.5 -// (SocketSync), LoRaWAN Bluetooth Badge 2.78 (contact tracing badge), Container Tracker 0.72, Helmet Tracker v0.14. -var LANSITEC_UNITS = ["dBm", "dB", "%", "V", "sec", "min", "s", "℃", "level", " meter"]; -var LANSITEC_GATEWAYS = ["compact-bluetooth-gateway", "indoor-bluetooth-gateway", "macro-bluetooth-gateway", "solar-bluetooth-gateway"]; -var LANSITEC_SOCKET_SYNC = "socket-sync-bluetooth-gateway"; -var LANSITEC_CONTACT_BADGE = "contact-tracing-badge"; - -// Codec key -> datasheet name. "-" drops the key: it is decoded from the raw bytes or is not a value. -// Bluetooth Gateway 4.4.1 Registration, 4.4.2 Heartbeat. -var LANSITEC_GATEWAY_KEYS = { - adr: "adr", mode: "mode", LoRaWANband: "loraWanFrequencyBand", LoRaTxpower: "loraTxPower", - ContinuousReceive: "continuousBleReceiving", dr: "dataRate", pos: "positionReportInterval", - hb: "heartbeatInterval", BLEReceivingDuration: "bluetoothReceivingDuration", - rssi: "loraRssi", snr: "-", ver: "-", vol: "-", chgstate: "-", number: "-" -}; -// Badge Tracker 5.1 Registration (the codec skips GNSSPRI), 5.2 Heartbeat, 5.3 GNSS Position, 5.4 Beacon, 5.5 Alarm. -var LANSITEC_BADGE_KEYS = { - ADR: "adr", "Lora Txpower": "loraTxPower", DR: "dataRate", GNSSEN: "gnssEnabled", - "Position mode": "positioningReportMode", "Position period": "bluetoothPositionReportInterval", - "Heartbeat period": "-", "Firmware version": "-", CFMMSG: "-", HBCOUNT: "-", "Fall threshold": "-", - "Battery level": "batteryLevel", RSSI: "loraRssi", SNR: "-", "GPS state": "gnssState", - "Move state": "-", "Charge state": "chargeState", - Longitude: "-", Latitude: "-", Time: "-", Length: "-", Vibstate: "-", "Alarm type": "alarm" -}; -// Temperature Humidity Sensor 2.2.1 Register, 2.2.2 Periodical data (VER is the message version, not the firmware). -var LANSITEC_TEMPERATURE_HUMIDITY_KEYS = { - adr: "adr", mode: "mode", smode: "smode", LoraTxpower: "loraTxPower", dr: "dataRate", repting: "reptind", - th: "temperatureHumidityReportPeriod", crc: "-", - ver: "-", vol: "batteryLevel", rssi: "loraRssi", snr: "-", temperature: "-", humidity: "-" -}; -// The other models: the integration writes rssi, snr and firmwareVersion itself. chgstate: the tracking label has the -// gateway codec, which misreads CHGSTAT (Bluetooth Gateway 4.4.2). number: the beacons list has them. -var LANSITEC_CLASH_KEYS = { - rssi: "loraRssi", RSSI: "loraRssi", snr: "loraSnr", SNR: "loraSnr", - firmwareVersion: "deviceFirmwareVersion", "Firmware version": "deviceFirmwareVersion", - type: "-", "Message type": "-", chgstate: "-", number: "-" -}; -// Beacon keys: Devicetype1data1, Device1data, dev1major, dev1.major, pos1minor, asset1rssi... -var LANSITEC_BEACON_PREFIXES = ["devicetype1", "devicetype2", "devicetype3", "device", "dev", "pos", "asset"]; -var LANSITEC_BEACON_FIELDS = ["data", "major", "minor", "rssi", "id"]; -// Register Type bits 2-0 and SMODE: the LoRa frequency plan (Plug Bluetooth Gateway 5.1, Bluetooth Badge 4.5.1). -var LANSITEC_MODES = ["", "AU915", "IN865", "CN470", "AS923", "EU433", "EU868", "US915"]; -var LANSITEC_SMODES = [1, 2, 4, 8, 16, 32, 64]; -var LANSITEC_BANDS = ["AU915", "IN865", "CN470", "AS923", "EU433", "EU868", "US915"]; -// Badge Tracker 5.2 Heartbeat, as the codec writes them. -var LANSITEC_BADGE_GNSS_STATES = ["GPS OFF", "GPS boot", "GPS locating", "GPS located", "", "", "", "", "", "GPS no signal"]; -var LANSITEC_BADGE_CHARGE_STATES = ["Power cable disconnected", "", "", "", "", "Charging", "Charge complete"]; -// Plug Bluetooth Gateway 5.1 CFG bits 3-1. -var LANSITEC_SOCKET_SYNC_TYPES = ["Outdoor gateway", "Indoor gateway", "Plug gateway"]; -// Bluetooth Badge 4.5.1 BT bits 10-8, 4.5.2 BTSTATUS bits 6-4 (dBm, 0 is not a power) and bits 11-8. -var LANSITEC_CONTACT_BADGE_BT_POWERS = [0, 4, 0, -4, -8, -12, -16, -20]; -var LANSITEC_CONTACT_BADGE_BUZZER = ["No action", "Vibrator disabled, buzzer muted (restore)", - "Vibrator disabled, buzzer half sound level", "Vibrator disabled, buzzer full sound level", - "Vibrator enabled, buzzer muted", "Vibrator enabled, buzzer half sound level", - "Vibrator enabled, buzzer full sound level", "Vibrator disabled, buzzer muted (no restore)", - "Beacon-in-tracker and proximity detection disabled"]; - -// { telemetry, attributes }, or null when the uplink is not one the Lansitec block decodes. -function lansitecUplink(payload, metadata) { - var model = metadata.modelId; - var values = null; - var register = false; - if ((model == LANSITEC_SOCKET_SYNC || model == LANSITEC_CONTACT_BADGE) && metadata.data != null) { - // Their codecs are the gateway's and misread every message: decoded from the raw bytes only. - var b = base64ToBytes(metadata.data); - values = model == LANSITEC_SOCKET_SYNC ? lansitecSocketSyncValues(b) : lansitecContactBadgeValues(b); - register = b.length > 0 && ((b[0] >> 4) & 0x0F) == (model == LANSITEC_SOCKET_SYNC ? 0x4 : 0x1); - } else if (metadata.payloadFormat == "JSON") { - var json = decodeToJson(payload); - if (!isMap(json.bytes)) { - return null; - } - var raw = json.bytes; - values = lansitecValues(raw, model); - if (metadata.data != null) { - lansitecRawValues(values, base64ToBytes(metadata.data), model); - } - var messageType = raw.type != null ? raw.type : raw["Message type"]; - register = messageType == "Register"; - } else { - return null; - } - // Register holds the device settings: attributes, not telemetry. - if (register) { - return { telemetry: {}, attributes: values }; - } - return { telemetry: values, attributes: {} }; -} - -// Strips the unit. Periods become seconds: the codecs give most of them in seconds, a few in minutes. -function lansitecNumber(v) { - if (!(v instanceof String)) { - return v; - } - foreach (u : LANSITEC_UNITS) { - if (v.endsWith(u)) { - var n = v.substring(0, v.length() - u.length()).trim(); - if (n.matches("-?[0-9]+(\\.[0-9]+)?")) { - return u == "min" ? parseDouble(n) * 60 : parseDouble(n); - } - } - } - return v; -} - -function lansitecLowerStart(part) { - var n = 0; - while (n < part.length() && part.substring(n, n + 1) != part.substring(n, n + 1).toLowerCase()) { - n = n + 1; - } - if (n == part.length()) { - return part.toLowerCase(); - } - if (n > 1) { - return part.substring(0, n - 1).toLowerCase() + part.substring(n - 1); - } - return part.substring(0, 1).toLowerCase() + part.substring(1); -} - -function lansitecCamelCase(key) { - var parts = key.split("[ .]+"); - var result = lansitecLowerStart(parts[0]); - for (var i = 1; i < parts.length; i++) { - var part = lansitecLowerStart(parts[i]); - result = result + part.substring(0, 1).toUpperCase() + part.substring(1); - } - return result; -} - -function lansitecBeaconKey(key) { - var k = key.toLowerCase().replace(".", ""); - foreach (p : LANSITEC_BEACON_PREFIXES) { - if (k.startsWith(p)) { - var rest = k.substring(p.length()); - foreach (f : LANSITEC_BEACON_FIELDS) { - if (rest.endsWith(f) && rest.substring(0, rest.length() - f.length()).matches("[0-9]+")) { - return { group: p, index: rest.substring(0, rest.length() - f.length()), field: f }; - } - if (rest.startsWith(f) && rest.substring(f.length()).matches("[0-9]+")) { - return { group: p, index: rest.substring(f.length()), field: f }; - } - } - } - } - return null; -} - -function lansitecKeyTable(model) { - if (LANSITEC_GATEWAYS.contains(model)) { - return LANSITEC_GATEWAY_KEYS; - } - if (model == "badge-tracker") { - return LANSITEC_BADGE_KEYS; - } - if (model == "temperature-humidity-sensor") { - return LANSITEC_TEMPERATURE_HUMIDITY_KEYS; - } - return null; -} - -function lansitecValues(raw, model) { - var table = lansitecKeyTable(model); - var values = {}; - var beacons = {}; - foreach (key : raw.keySet()) { - var value = raw[key]; - var b = lansitecBeaconKey(key); - if (b != null) { - var id = b.group + b.index; - if (beacons[id] == null) { - beacons[id] = { order: parseInt(b.index) }; - } - if (b.field == "rssi") { - beacons[id].rssi = lansitecNumber(value); - } else if (b.field == "data" || b.field == "major" || b.field == "minor") { - beacons[id][b.field] = value; - } - } else if (table == null && key.matches("time[1-8]?") && value instanceof String) { - // Container Tracker 5.3 and 5.7, Helmet Tracker 6.3: UTC time. The codecs add 8 hours and quote it. - var gnssDate = new Date(value.replace("\"", "")); - values["gnssTime" + key.substring(4)] = gnssDate.getTime() - 8 * 60 * 60 * 1000; - } else if (key == "pressure" && model == "helmet-sensor" && !(value instanceof String)) { - // Helmet Tracker 6.3: the value * 10, unit 0.1 Pa. - values.pressure = value / 10.0; - } else if (table != null && table.containsKey(key)) { - var tableName = table[key]; - if (tableName != "-") { - values[tableName] = lansitecNumber(value); - } - } else if (LANSITEC_CLASH_KEYS.containsKey(key)) { - var clashName = LANSITEC_CLASH_KEYS[key]; - if (clashName != "-") { - values[clashName] = lansitecNumber(value); - } - } else { - var codecName = lansitecCamelCase(key); - values[codecName] = lansitecNumber(value); - } - } - if (!beacons.isEmpty()) { - values.beacons = lansitecBeaconList(beacons); - } - return values; -} - -// In the order of their numbers, which can have gaps (Container Tracker 5.7: dev1, dev3...). -function lansitecBeaconList(beacons) { - var list = []; - var added = {}; - while (list.size() < beacons.size()) { - var next = null; - foreach (id : beacons.keySet()) { - if (added[id] == null && (next == null || beacons[id].order < beacons[next].order)) { - next = id; - } - } - added[next] = true; - var b = beacons[next]; - var item = {}; - if (b.data != null) { - item.data = b.data; - } - if (b.major != null) { - item.major = b.major; - item.minor = b.minor; - } - item.rssi = b.rssi; - list.add(item); - } - // The gateway codec reads every device after the first at a 5-byte step: right only for 4-byte data. - if (list.size() > 1 && list[0].data != null && list[0].data.length() != 8) { - var first = []; - first.add(list[0]); - return first; - } - return list; -} - -function lansitecRawValues(values, b, model) { - if (b.length == 0) { - return; - } - var type = (b[0] >> 4) & 0x0F; - if (LANSITEC_GATEWAYS.contains(model)) { - lansitecGatewayRawValues(values, b, model, type); - } else if (model == "badge-tracker") { - lansitecBadgeRawValues(values, b, type); - } else if (model == "temperature-humidity-sensor" && type == 2) { - lansitecTemperatureHumidityRawValues(values, b); - } -} - -// IEEE 754 float, big-endian at offset: false when its exponent is 0xFF (NaN or infinity). -function lansitecFloatIsNumber(b, offset) { - return !((b[offset] & 0x7F) == 0x7F && (b[offset + 1] & 0x80) != 0); -} - -// SNR, unit 0.01 dB, signed. The codecs read it unsigned. -function lansitecSnr(b, offset) { - var v = parseBytesToInt(b, offset, 2, true); - if (v > 32767) { - v = v - 65536; - } - return v / 100.0; -} - -// CHGSTAT: 0 Not charging, then the codes of Charging and Charging completed. -function lansitecChargeState(values, charge, charging, completed) { - if (charge == 0) { - values.chargeState = "Not charging"; - } else if (charge == charging) { - values.chargeState = "Charging"; - } else if (charge == completed) { - values.chargeState = "Charging completed"; - } -} - -// Register of the SocketSync gateway (5.1) and the contact tracing badge (4.5.1): Type (ADR, MODE), SMODE, POWER, -// CFG, POS (2, unit 5 s), then HB (unit 30 s) at hbOffset. -function lansitecRegister(values, b, hbOffset) { - values.adr = (b[0] & 0x08) != 0 ? "ON" : "OFF"; - var mode = b[0] & 0x07; - if (LANSITEC_MODES[mode] != "") { - values.mode = LANSITEC_MODES[mode]; - } - var band = LANSITEC_SMODES.indexOf(b[1] & 0xFF); - if (band >= 0) { - values.loraWanFrequencyBand = LANSITEC_BANDS[band]; - } - values.loraTxPower = (b[2] >> 3) & 0x1F; - values.dataRate = "DR" + ((b[3] >> 4) & 0x0F); - values.positionReportInterval = parseBytesToInt(b, 4, 2, true) * 5; - values.heartbeatInterval = (b[hbOffset] & 0xFF) * 30; -} - -// Heartbeat of the SocketSync gateway (5.2) and the contact tracing badge (4.5.2): Type, VOL, RSSI (unit -1 dBm), -// SNR (2), REV, CHGSTAT (0x00, 0x50, 0x60 and 0x0, 0x5, 0x6). -function lansitecHeartbeat(values, b, charging, completed) { - values.loraRssi = 0 - (b[2] & 0xFF); - values.loraSnr = lansitecSnr(b, 3); - values.deviceFirmwareVersion = bytesToHex(b).substring(10, 12); - lansitecChargeState(values, b[6] & 0xFF, charging, completed); -} - -// Acknowledgment (Plug Bluetooth Gateway 5.5, Bluetooth Badge 4.5.4): Type bits 3-0 the result, then the MSGID. -function lansitecAcknowledgment(values, b) { - values.result = (b[0] & 0x0F) == 0 ? "Success" : "Failure"; - values.msgid = b[1] & 0xFF; -} - -// Bluetooth Gateway 2.27. -function lansitecGatewayRawValues(values, b, model, type) { - if (type == 1 && b.length >= 9) { - // 4.4.1: Network reconnection interval, unit 5 minutes. - values.networkReconnectionInterval = (b[8] & 0xFF) * 300; - } else if (type == 2 && b.length >= 8) { - // 4.4.2: VOL is the battery voltage (x 0.01 + 1.5 V) on the macro gateway, the battery level (%) on the others. - if (model == "macro-bluetooth-gateway") { - values.batteryVoltage = ((b[1] & 0xFF) + 150) / 100.0; - } else { - values.batteryLevel = b[1] & 0xFF; - } - values.loraSnr = lansitecSnr(b, 3); - // VER 108 is V1.08. - var version = parseBytesToInt(b, 5, 2, true); - var minor = (version % 100).intValue(); - values.deviceFirmwareVersion = "V" + (version / 100).intValue() + "." + (minor < 10 ? "0" : "") + minor; - // CHGSTAT on the compact and solar gateway only. - if (model == "compact-bluetooth-gateway" || model == "solar-bluetooth-gateway") { - lansitecChargeState(values, b[7] & 0xFF, 0x50, 0x60); - } - } else if (type == 0x8 || type == 0x9 || type == 0xA || type == 0xE) { - values.remove("beacons"); - var beacons = lansitecGatewayBeacons(b, type); - if (beacons != null) { - values.beacons = beacons; - } - } -} - -// 4.4.4-4.4.6: Type (bits 3-0: number of devices), Data and RSSI (value - 256) of each device; one data length per -// message (4.4.3). 4.4.7 Multi-Device: a Device Type ID before each device, decoded when all have the same length. -function lansitecGatewayBeacons(b, type) { - var count = b[0] & 0x0F; - if (count == 0 || (b.length - 1) % count != 0) { - return null; - } - // TBEL division gives a Double: array indexes need an int. - var step = ((b.length - 1) / count).intValue(); - var head = type == 0xE ? 1 : 0; - var dataLength = step - head - 1; - if (dataLength < 1) { - return null; - } - var hex = bytesToHex(b); - var list = []; - for (var i = 0; i < count; i++) { - var start = 1 + i * step; - var deviceType = type - 7; - if (type == 0xE) { - deviceType = b[start] & 0xFF; - if (deviceType < 1 || deviceType > 3) { - return null; - } - } - var item = {}; - item.deviceType = deviceType; - item.data = hex.substring(2 * (start + head), 2 * (start + head + dataLength)); - item.rssi = (b[start + step - 1] & 0xFF) - 256; - list.add(item); - } - return list; -} - -// Badge Tracker v5.30. -function lansitecBadgeRawValues(values, b, type) { - if (type == 1 && b.length == 14) { - // 5.1: GNSSPRI (2), Heartbeat, Version (2, 0x0525 is V5.25), CFMMSG, HBCOUNT, Fall (unit 0.5 m). - values.gnssPositionReportInterval = parseBytesToInt(b, 6, 2, true) * 5; - values.heartbeatInterval = (b[8] & 0xFF) * 30; - var version = bytesToHex(b).substring(18, 22); - var major = version.substring(0, 2); - if (major.startsWith("0")) { - major = major.substring(1); - } - values.deviceFirmwareVersion = "V" + major + "." + version.substring(2); - values.cfmmsg = b[11] & 0xFF; - values.hbcount = b[12] & 0xFF; - values.fallDetectionThreshold = (b[13] & 0xFF) * 0.5; - } else if (type == 2 && b.length >= 6) { - // 5.2: SNR (2) since firmware 0x0501, decided by the length (SNREN is not reliable). Then GNSSSTAT and - // VIBSTATE (moving state 0-9), CHGSTAT. The codec always reads the SNR. - var state = 3; - if (b.length >= 8) { - values.loraSnr = lansitecSnr(b, 3); - state = 5; - } else { - var gnss = (b[3] >> 4) & 0x0F; - var charge = (b[4] >> 4) & 0x0F; - values.remove("gnssState"); - values.remove("chargeState"); - if (gnss < LANSITEC_BADGE_GNSS_STATES.size() && LANSITEC_BADGE_GNSS_STATES[gnss] != "") { - values.gnssState = LANSITEC_BADGE_GNSS_STATES[gnss]; - } - if (charge < LANSITEC_BADGE_CHARGE_STATES.size() && LANSITEC_BADGE_CHARGE_STATES[charge] != "") { - values.chargeState = LANSITEC_BADGE_CHARGE_STATES[charge]; - } - } - values.movingState = b[state] & 0x0F; - } else if (type == 3 && b.length >= 13) { - // 5.3: Flag 0x1 is a failed fix. IEEE 754 floats (the codec divides by 0x7fffff) rounded to 6 decimals, - // UTC seconds (the codec adds 8 hours). No position from NaN: parseBytesToFloat fails on it. - if ((b[0] & 0x0F) == 0 && lansitecFloatIsNumber(b, 1) && lansitecFloatIsNumber(b, 5)) { - values.longitude = toFixed(parseBytesToFloat(b, 1, 4, true) * 1.0, 6); - values.latitude = toFixed(parseBytesToFloat(b, 5, 4, true) * 1.0, 6); - values.gnssTime = parseBytesToLong(b, 9, 4, true) * 1000; - } - } -} - -// Temperature Humidity Sensor 2.2.2: VER 1 has the SNR, VER 0 not (the codec always reads it). TEMP_INT bit 7 is the sign. -function lansitecTemperatureHumidityRawValues(values, b) { - var offset = (b[0] & 0x0F) == 1 ? 5 : 3; - if (b.length < offset + 3) { - return; - } - if (offset == 5) { - values.loraSnr = lansitecSnr(b, 3); - } - var temperature = (b[offset] & 0x7F) + (b[offset + 1] & 0xFF) / 100.0; - if ((b[offset] & 0x80) != 0) { - temperature = -temperature; - } - values.temperature = toFixed(temperature, 2); - values.humidity = b[offset + 2] & 0xFF; -} - -// Plug Bluetooth Gateway 1.5, the SocketSync gateway. -function lansitecSocketSyncValues(b) { - var values = {}; - if (b.length == 0) { - return values; - } - var type = (b[0] >> 4) & 0x0F; - if (type == 0x4 && b.length >= 10) { - // 5.1 Registration: ..., BLE Receiving Duration, HB, Beacon_QTY (half of the limit), CRC (2). - lansitecRegister(values, b, 7); - values.continuousBleReceiving = (b[2] & 0x02) != 0 ? "Continuous Bluetooth receiving is enabled" : "Continuous Bluetooth receiving is disabled"; - values.powerDownFlag = (b[2] & 0x01) != 0 ? "Automatic shutdown status" : "Normal status"; - var gatewayType = (b[3] >> 1) & 0x07; - if (gatewayType < LANSITEC_SOCKET_SYNC_TYPES.size()) { - values.gatewayType = LANSITEC_SOCKET_SYNC_TYPES[gatewayType]; - } - values.rssiSortingMethod = (b[3] & 0x01) != 0 ? "Sort by the maximum value" : "Sort by average"; - values.bluetoothReceivingDuration = b[6] & 0xFF; - values.beaconQuantityLimit = (b[8] & 0xFF) * 2; - } else if (type == 0x2 && b.length >= 7) { - // 5.2 Heartbeat. VOL (%) keeps its name: the dashboard of the IoT Hub package reads it. - values.vol = b[1] & 0xFF; - lansitecHeartbeat(values, b, 0x50, 0x60); - } else if (type == 0x6 || type == 0xE) { - // 5.3 Beacon, 5.4 Alarm Beacon List: Type (bits 3-0: number of beacons), then UUID tail, Major (2), Minor (2), - // RSSI (value - 256) of each. Table 7: the UUID prefix ends with 56 for tails 0x56-0xFF, 57 for 0x00-0x1E. - var count = b[0] & 0x0F; - if (count > 0 && b.length >= 1 + count * 6) { - var hex = bytesToHex(b); - var list = []; - for (var i = 0; i < count; i++) { - var start = 1 + i * 6; - var item = {}; - item.uuid = "5C38DBDE567C4CCAB1DA40A8AD465" + ((b[start] & 0xFF) < 0x56 ? "7" : "6") + hex.substring(2 * start, 2 * start + 2); - item.major = parseBytesToInt(b, start + 1, 2, true); - item.minor = parseBytesToInt(b, start + 3, 2, true); - item.rssi = (b[start + 5] & 0xFF) - 256; - list.add(item); - } - if (type == 0x6) { - values.beacons = list; - } else { - values.alarmBeacons = list; - } - } - } else if (type == 0xF && b.length >= 2) { - lansitecAcknowledgment(values, b); - } - return values; -} - -// Bluetooth Badge 4.5.5 TimeMark: bit n set means seen n * 10 to n * 10 + 9 s before the message, 10 s per bit. -function lansitecContactBadgeSeen(item, mark) { - item.timeMark = mark; - var bits = 0; - for (var i = 0; i < 8; i++) { - if (((mark >> i) & 1) == 1) { - bits = bits + 1; - } - } - item.seenTime = bits * 10; -} - -// LoRaWAN Bluetooth Badge 2.78, the contact tracing badge. Major and Minor are hex (a badge's: the end of its DevEUI). -function lansitecContactBadgeValues(b) { - var values = {}; - if (b.length == 0) { - return values; - } - var type = (b[0] >> 4) & 0x0F; - var hex = bytesToHex(b); - if (type == 0x1 && b.length >= 9) { - // 4.5.1 Register: ..., HB, BT (2), CRC (2). The receiving threshold setting has no unit in the datasheet. - lansitecRegister(values, b, 6); - var bt = parseBytesToInt(b, 7, 2, true); - values.buzzerAndVibrator = ((bt >> 11) & 1) == 1 ? "ON" : "OFF"; - var power = (bt >> 8) & 0x07; - if (power > 0) { - values.bluetoothTxPower = LANSITEC_CONTACT_BADGE_BT_POWERS[power]; - } - values.proximityDetection = ((bt >> 7) & 1) == 1 ? "ON" : "OFF"; - values.bluetoothReceivingThresholdSetting = bt & 0x7F; - } else if (type == 0x2 && b.length >= 7) { - // 4.5.2 Heartbeat: ..., CRC (2), BTSTATUS (2), then the friend badges. - values.batteryLevel = b[1] & 0xFF; - lansitecHeartbeat(values, b, 0x5, 0x6); - if (b.length >= 11) { - var status = parseBytesToInt(b, 9, 2, true); - var buzzer = (status >> 8) & 0x0F; - if (buzzer < LANSITEC_CONTACT_BADGE_BUZZER.size()) { - values.vibratorBuzzerStatus = LANSITEC_CONTACT_BADGE_BUZZER[buzzer]; - } - // 0 means not changed. The threshold is for range bit 7 = 0: -73 to -87 dBm. - var statusPower = (status >> 4) & 0x07; - if (statusPower > 0) { - values.bluetoothTxPower = LANSITEC_CONTACT_BADGE_BT_POWERS[statusPower]; - } - var threshold = status & 0x0F; - if (threshold > 0 && ((status >> 7) & 1) == 0) { - values.bluetoothReceivingThreshold = -72 - threshold; - } - } - // Friend field: Type 0xC with the number of friend badges, then Major (2) and Minor (2) of each. - if (b.length >= 12 && ((b[11] >> 4) & 0x0F) == 0xC) { - var friendCount = b[11] & 0x0F; - if (b.length >= 12 + friendCount * 4) { - var friends = []; - for (var f = 0; f < friendCount; f++) { - var friendStart = 12 + f * 4; - friends.add({ major: hex.substring(2 * friendStart, 2 * friendStart + 4), - minor: hex.substring(2 * friendStart + 4, 2 * friendStart + 8) }); - } - values.friends = friends; - } - } - } else if (type == 0x8) { - // 4.5.3 10s Instant Report: Type (bits 3-0: M beacons), Major (2), Minor (2), RSSI of each, then TOFF: - // (M + 1) / 2 bytes, 4 bits per beacon, high bits first. RSSI bit 7: 1 a beacon, 0 a badge; bits 6-0 - 128. - var count = b[0] & 0x0F; - if (count > 0 && b.length >= 1 + count * 5) { - var hasOffsets = b.length >= 1 + count * 5 + ((count + 1) / 2).intValue(); - var list = []; - for (var i = 0; i < count; i++) { - var start = 1 + i * 5; - var item = {}; - item.major = hex.substring(2 * start, 2 * start + 4); - item.minor = hex.substring(2 * start + 4, 2 * start + 8); - var rssi = b[start + 4] & 0xFF; - item.type = (rssi & 0x80) != 0 ? "Bluetooth beacon" : "Badge"; - item.rssi = (rssi & 0x7F) - 128; - if (hasOffsets) { - var offsetByte = b[1 + count * 5 + (i / 2).intValue()] & 0xFF; - item.timeOffset = i % 2 == 0 ? (offsetByte >> 4) & 0x0F : offsetByte & 0x0F; - } - list.add(item); - } - values.beacons = list; - } - } else if (type == 0xC) { - // 4.5.5 80s Periodic Report: badges as Major (2), Minor (2), TimeMark, then 0xFFFF and beacons as Minor (2), - // TimeMark (a beacon's Major is FFFF). - var badges = []; - var periodBeacons = []; - var pos = 1; - var inBeacons = false; - while (pos < b.length) { - if (!inBeacons && pos + 2 <= b.length && (b[pos] & 0xFF) == 0xFF && (b[pos + 1] & 0xFF) == 0xFF) { - inBeacons = true; - pos = pos + 2; - } else if (!inBeacons && pos + 5 <= b.length) { - var badge = { major: hex.substring(2 * pos, 2 * pos + 4), minor: hex.substring(2 * pos + 4, 2 * pos + 8) }; - lansitecContactBadgeSeen(badge, b[pos + 4] & 0xFF); - badges.add(badge); - pos = pos + 5; - } else if (inBeacons && pos + 3 <= b.length) { - var beacon = { minor: hex.substring(2 * pos, 2 * pos + 4) }; - lansitecContactBadgeSeen(beacon, b[pos + 2] & 0xFF); - periodBeacons.add(beacon); - pos = pos + 3; - } else { - return values; - } - } - if (badges.size() + periodBeacons.size() == (b[0] & 0x0F)) { - values.badges = badges; - values.beacons = periodBeacons; - } - } else if (type == 0xF && b.length >= 2) { - lansitecAcknowledgment(values, b); - } - return values; -} - -return decodeThingsStackUplink(payload, metadata); +// or the name the integration metadata gives it under profile.-. +// A device that is not in the Repository can name its model in decoded_payload: its payload formatter adds +// tbVersionIds: { brand_id, model_id }, which is not telemetry. Any other device gets LoRaWAN Device. +var values = {}; +var history = []; +var versionIds = null; +if (metadata.payloadFormat == "JSON") { + values = decodeToJson(payload); + if (values.history instanceof java.util.List) { + foreach (record : values.history) { + if (record.timestamp != null) { + var ts = record.timestamp * 1000; + record.remove("timestamp"); + history.add({ ts: ts, values: record }); + } + } + values.remove("history"); + } + if (values.containsKey("tbVersionIds")) { + versionIds = values.tbVersionIds; + values.remove("tbVersionIds"); + } +} +var brandId = metadata.brandId; +var modelId = metadata.modelId; +if ((brandId == null || modelId == null) && versionIds instanceof java.util.Map) { + brandId = versionIds.brand_id; + modelId = versionIds.model_id; +} +var profile = "LoRaWAN Device"; +if (brandId != null && modelId != null) { + profile = brandId + "-" + modelId; + // The profile name chosen at install, kept in the integration metadata + var customProfile = metadata["profile." + profile]; + if (customProfile != null && customProfile != "") { + profile = customProfile; + } +} +if (history.isEmpty()) { + return { profile: profile, telemetry: { ts: metadata.ts, values: values } }; +} +history.add({ ts: metadata.ts, values: values }); +return { profile: profile, telemetry: history }; From 1b1d7ba5f58bf3f78cb9887680dd8cd4d5d6e496 Mon Sep 17 00:00:00 2001 From: dpinkevych Date: Mon, 9 Nov 2026 15:11:19 +0200 Subject: [PATCH 11/13] test(iot-hub): the Things Stack converter tests expect frequency as a number, as the integration writes it since #16534 Co-Authored-By: Claude Opus 5.5 --- .../server/controller/ConverterControllerTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java index 1c42645271..9502eeefa6 100644 --- a/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java @@ -691,7 +691,7 @@ public class ConverterControllerTest extends AbstractControllerTest { "\"telemetry\":[{\"ts\":1793170000000,\"values\":{\"temperature\":24.1,\"co2\":550}},{\"ts\":1793181600100,\"values\":{\"battery\":95,\"temperature\":24.5,\"co2\":560," + "\"fCnt\":12,\"data\":\"AXVk\",\"rssi\":-60,\"snr\":9.5,\"channel\":2,\"consumedAirtime\":\"0.061696s\"}}]," + "\"attributes\":{\"eui\":\"24E124707C481005\",\"joinEui\":\"24E124C0002A0001\",\"devAddr\":\"260B1234\",\"deviceId\":\"am308-office\",\"applicationId\":\"my-app\"," + - "\"fPort\":85,\"bandwidth\":125000,\"spreadingFactor\":7,\"codeRate\":\"4/5\",\"frequency\":\"868100000\",\"brandId\":\"milesight-iot\",\"modelId\":\"am308\"," + + "\"fPort\":85,\"bandwidth\":125000,\"spreadingFactor\":7,\"codeRate\":\"4/5\",\"frequency\":868100000,\"brandId\":\"milesight-iot\",\"modelId\":\"am308\"," + "\"hardwareVersion\":\"1.0\",\"firmwareVersion\":\"1.0\",\"bandId\":\"EU_863_870\",\"netId\":\"000013\",\"tenantId\":\"ttn\",\"clusterId\":\"eu1\"," + "\"clusterAddress\":\"eu1.cloud.thethings.network\"}}"; @@ -713,7 +713,7 @@ public class ConverterControllerTest extends AbstractControllerTest { String expectedDecodedMessage = "{\"entityType\":\"DEVICE\",\"name\":\"manual-node\",\"profile\":\"LoRaWAN Device\",\"label\":\"manual-node\",\"group\":\"my-app\"," + "\"telemetry\":{\"ts\":1793181600100,\"values\":{\"fCnt\":3,\"data\":\"Ae0DM18OTGM=\",\"rssi\":-70,\"snr\":7.0}}," + "\"attributes\":{\"eui\":\"70B3D57ED006F370\",\"deviceId\":\"manual-node\",\"applicationId\":\"my-app\",\"fPort\":1,\"bandwidth\":125000," + - "\"spreadingFactor\":9,\"frequency\":\"868300000\"}}"; + "\"spreadingFactor\":9,\"frequency\":868300000}}"; testThingsStackDefaultConverters(THINGS_STACK_MANUAL_DEVICE_PAYLOAD, Map.of("profile.milesight-iot-am308", "AM308"), expectedDecodedMessage); } @@ -721,7 +721,7 @@ public class ConverterControllerTest extends AbstractControllerTest { private static final String THINGS_STACK_FORMATTER_DEVICE_RESULT = "{\"entityType\":\"DEVICE\",\"name\":\"macro-tracker-1\",\"profile\":\"lansitec-macro-tracker\",\"label\":\"macro-tracker-1\",\"group\":\"my-app\"," + "\"telemetry\":{\"ts\":1794304800100,\"values\":{\"batteryLevel\":100,\"chargeState\":\"Charging\",\"fCnt\":5,\"data\":\"IWQ+Az4AbFA=\",\"rssi\":-70,\"snr\":7.0}}," + "\"attributes\":{\"eui\":\"70B3D57ED006F371\",\"deviceId\":\"macro-tracker-1\",\"applicationId\":\"my-app\",\"fPort\":1,\"bandwidth\":125000," + - "\"spreadingFactor\":9,\"frequency\":\"868300000\"}}"; + "\"spreadingFactor\":9,\"frequency\":868300000}}"; // A device added without the Repository, whose payload formatter names its model in tbVersionIds @Test From f511edcce2190b0e4940ba26866f5143f7c4e469 Mon Sep 17 00:00:00 2001 From: dpinkevych Date: Mon, 9 Nov 2026 19:05:52 +0200 Subject: [PATCH 12/13] fix(iot-hub): the default Things Stack converters save the keys the integration metadata lists for the model as attributes, and the brandId and modelId of tbVersionIds attributes.- in the integration metadata (comma-separated, written by the device package's INTEGRATION step, like profile.*) lists the keys of decoded_payload that are device information and settings: they become attributes, the rest stays telemetry. No list, everything stays telemetry. The model comes from version_ids, else from tbVersionIds, which now also gives the brandId and modelId attributes, as version_ids does through the integration. Tests: the listed keys of a formatter device, the list of a Repository device by its model with a renamed profile (history records stay telemetry), the list of another model changes nothing. Co-Authored-By: Claude Opus 5.5 --- .../controller/ConverterControllerTest.java | 39 +++++++++++++++++-- .../src/assets/converters/js-tti-decoder.raw | 37 ++++++++++++++---- .../src/assets/converters/js-ttn-decoder.raw | 37 ++++++++++++++---- .../assets/converters/tbel-tti-decoder.raw | 37 ++++++++++++++---- .../assets/converters/tbel-ttn-decoder.raw | 37 ++++++++++++++---- 5 files changed, 156 insertions(+), 31 deletions(-) diff --git a/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java index 9502eeefa6..2a01c73492 100644 --- a/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java @@ -720,10 +720,11 @@ public class ConverterControllerTest extends AbstractControllerTest { private static final String THINGS_STACK_FORMATTER_DEVICE_RESULT = "{\"entityType\":\"DEVICE\",\"name\":\"macro-tracker-1\",\"profile\":\"lansitec-macro-tracker\",\"label\":\"macro-tracker-1\",\"group\":\"my-app\"," + "\"telemetry\":{\"ts\":1794304800100,\"values\":{\"batteryLevel\":100,\"chargeState\":\"Charging\",\"fCnt\":5,\"data\":\"IWQ+Az4AbFA=\",\"rssi\":-70,\"snr\":7.0}}," + - "\"attributes\":{\"eui\":\"70B3D57ED006F371\",\"deviceId\":\"macro-tracker-1\",\"applicationId\":\"my-app\",\"fPort\":1,\"bandwidth\":125000," + + "\"attributes\":{\"brandId\":\"lansitec\",\"modelId\":\"macro-tracker\",\"eui\":\"70B3D57ED006F371\",\"deviceId\":\"macro-tracker-1\",\"applicationId\":\"my-app\",\"fPort\":1,\"bandwidth\":125000," + "\"spreadingFactor\":9,\"frequency\":868300000}}"; - // A device added without the Repository, whose payload formatter names its model in tbVersionIds + // A device added without the Repository, whose payload formatter names its model in tbVersionIds: the profile of the + // model, and the brandId and modelId attributes, as a Repository device gets them from version_ids @Test public void testThingsStackDefaultConvertersGiveFormatterDeviceTheModelProfileOfItsDecodedPayload() throws IOException { testThingsStackDefaultConverters(THINGS_STACK_FORMATTER_DEVICE_PAYLOAD, Map.of(), THINGS_STACK_FORMATTER_DEVICE_RESULT); @@ -751,11 +752,43 @@ public class ConverterControllerTest extends AbstractControllerTest { public void testThingsStackDefaultConvertersIgnoreTbVersionIdsThatIsNotAnObject() throws IOException { ObjectNode payload = (ObjectNode) JacksonUtil.toJsonNode(readResource(THINGS_STACK_FORMATTER_DEVICE_PAYLOAD)); ((ObjectNode) payload.get("uplink_message").get("decoded_payload")).put("tbVersionIds", "lansitec-macro-tracker"); - String expectedDecodedMessage = THINGS_STACK_FORMATTER_DEVICE_RESULT.replace("\"profile\":\"lansitec-macro-tracker\"", "\"profile\":\"LoRaWAN Device\""); + String expectedDecodedMessage = THINGS_STACK_FORMATTER_DEVICE_RESULT.replace("\"profile\":\"lansitec-macro-tracker\"", "\"profile\":\"LoRaWAN Device\"") + .replace("\"brandId\":\"lansitec\",\"modelId\":\"macro-tracker\",", ""); testThingsStackDefaultConvertersOnPayload(payload.toString(), Map.of(), expectedDecodedMessage); } + // The keys the integration metadata lists for the model are attributes, not telemetry; spaces around them are ignored, + // and a listed key the uplink doesn't have is skipped. The model may come from tbVersionIds + @Test + public void testThingsStackDefaultConvertersSaveTheModelsListedKeysAsAttributes() throws IOException { + String expectedDecodedMessage = THINGS_STACK_FORMATTER_DEVICE_RESULT + .replace("\"chargeState\":\"Charging\",", "") + .replace("\"attributes\":{", "\"attributes\":{\"chargeState\":\"Charging\","); + + testThingsStackDefaultConverters(THINGS_STACK_FORMATTER_DEVICE_PAYLOAD, + Map.of("attributes.lansitec-macro-tracker", " chargeState , firmwareVersionOfTheTracker"), expectedDecodedMessage); + } + + // The list is per brand and model, not per profile name: a renamed profile keeps it. History records stay telemetry + @Test + public void testThingsStackDefaultConvertersTakeTheListOfARepositoryDeviceByItsModel() throws IOException { + String expectedDecodedMessage = THINGS_STACK_REPOSITORY_DEVICE_RESULT + .replace("\"profile\":\"milesight-iot-am308\"", "\"profile\":\"AM308\"") + .replace("{\"battery\":95,", "{") + .replace("\"attributes\":{", "\"attributes\":{\"battery\":95,"); + + testThingsStackDefaultConverters(THINGS_STACK_REPOSITORY_DEVICE_PAYLOAD, + Map.of("profile.milesight-iot-am308", "AM308", "attributes.milesight-iot-am308", "battery"), expectedDecodedMessage); + } + + // The list of another model changes nothing + @Test + public void testThingsStackDefaultConvertersIgnoreTheListOfAnotherModel() throws IOException { + testThingsStackDefaultConverters(THINGS_STACK_FORMATTER_DEVICE_PAYLOAD, + Map.of("attributes.lansitec-solar-tracker", "chargeState"), THINGS_STACK_FORMATTER_DEVICE_RESULT); + } + // The TBEL and JS defaults are two copies of one script, so each of them must give this result for TTN and TTI private void testThingsStackDefaultConverters(String payloadFile, Map integrationMetadata, String expectedResult) throws IOException { testThingsStackDefaultConvertersOnPayload(readResource(payloadFile), integrationMetadata, expectedResult); diff --git a/ui-ngx/src/assets/converters/js-tti-decoder.raw b/ui-ngx/src/assets/converters/js-tti-decoder.raw index b7c6fb0354..5b944841f1 100644 --- a/ui-ngx/src/assets/converters/js-tti-decoder.raw +++ b/ui-ngx/src/assets/converters/js-tti-decoder.raw @@ -3,8 +3,12 @@ // Such a device gets the profile -, so all devices of one model share it, // or the name the integration metadata gives it under profile.-. // A device that is not in the Repository can name its model in decoded_payload: its payload formatter adds -// tbVersionIds: { brand_id, model_id }, which is not telemetry. Any other device gets LoRaWAN Device. +// tbVersionIds: { brand_id, model_id }, which is not telemetry; they become the attributes brandId and modelId, as the +// integration writes them from version_ids. Any other device gets LoRaWAN Device. +// The keys the integration metadata lists under attributes.- (comma-separated) are saved as +// attributes, not telemetry: device information and settings. var values = {}; +var attributes = {}; var history = []; var versionIds = null; if (metadata.payloadFormat == 'JSON') { @@ -29,21 +33,40 @@ var modelId = metadata.modelId; if ((brandId == null || modelId == null) && versionIds != null) { brandId = versionIds.brand_id; modelId = versionIds.model_id; + if (brandId != null && modelId != null) { + attributes.brandId = brandId; + attributes.modelId = modelId; + } } var profile = 'LoRaWAN Device'; if (brandId != null && modelId != null) { - profile = brandId + '-' + modelId; + var model = brandId + '-' + modelId; + profile = model; // The profile name chosen at install, kept in the integration metadata - var customProfile = metadata['profile.' + profile]; + var customProfile = metadata['profile.' + model]; if (customProfile != null && customProfile != '') { profile = customProfile; } + var attributeKeys = metadata['attributes.' + model]; + if (attributeKeys != null && attributeKeys != '') { + attributeKeys.split(',').forEach(function (key) { + key = key.trim(); + if (key != '' && values.hasOwnProperty(key)) { + attributes[key] = values[key]; + delete values[key]; + } + }); + } +} +var result = { profile: profile, telemetry: { ts: metadata.ts, values: values } }; +if (history.length > 0) { + history.push({ ts: metadata.ts, values: values }); + result.telemetry = history; } -if (history.length == 0) { - return { profile: profile, telemetry: { ts: metadata.ts, values: values } }; +if (Object.keys(attributes).length > 0) { + result.attributes = attributes; } -history.push({ ts: metadata.ts, values: values }); -return { profile: profile, telemetry: history }; +return result; function decodeToJson(payload) { return JSON.parse(String.fromCharCode.apply(String, payload)); diff --git a/ui-ngx/src/assets/converters/js-ttn-decoder.raw b/ui-ngx/src/assets/converters/js-ttn-decoder.raw index b7c6fb0354..5b944841f1 100644 --- a/ui-ngx/src/assets/converters/js-ttn-decoder.raw +++ b/ui-ngx/src/assets/converters/js-ttn-decoder.raw @@ -3,8 +3,12 @@ // Such a device gets the profile -, so all devices of one model share it, // or the name the integration metadata gives it under profile.-. // A device that is not in the Repository can name its model in decoded_payload: its payload formatter adds -// tbVersionIds: { brand_id, model_id }, which is not telemetry. Any other device gets LoRaWAN Device. +// tbVersionIds: { brand_id, model_id }, which is not telemetry; they become the attributes brandId and modelId, as the +// integration writes them from version_ids. Any other device gets LoRaWAN Device. +// The keys the integration metadata lists under attributes.- (comma-separated) are saved as +// attributes, not telemetry: device information and settings. var values = {}; +var attributes = {}; var history = []; var versionIds = null; if (metadata.payloadFormat == 'JSON') { @@ -29,21 +33,40 @@ var modelId = metadata.modelId; if ((brandId == null || modelId == null) && versionIds != null) { brandId = versionIds.brand_id; modelId = versionIds.model_id; + if (brandId != null && modelId != null) { + attributes.brandId = brandId; + attributes.modelId = modelId; + } } var profile = 'LoRaWAN Device'; if (brandId != null && modelId != null) { - profile = brandId + '-' + modelId; + var model = brandId + '-' + modelId; + profile = model; // The profile name chosen at install, kept in the integration metadata - var customProfile = metadata['profile.' + profile]; + var customProfile = metadata['profile.' + model]; if (customProfile != null && customProfile != '') { profile = customProfile; } + var attributeKeys = metadata['attributes.' + model]; + if (attributeKeys != null && attributeKeys != '') { + attributeKeys.split(',').forEach(function (key) { + key = key.trim(); + if (key != '' && values.hasOwnProperty(key)) { + attributes[key] = values[key]; + delete values[key]; + } + }); + } +} +var result = { profile: profile, telemetry: { ts: metadata.ts, values: values } }; +if (history.length > 0) { + history.push({ ts: metadata.ts, values: values }); + result.telemetry = history; } -if (history.length == 0) { - return { profile: profile, telemetry: { ts: metadata.ts, values: values } }; +if (Object.keys(attributes).length > 0) { + result.attributes = attributes; } -history.push({ ts: metadata.ts, values: values }); -return { profile: profile, telemetry: history }; +return result; function decodeToJson(payload) { return JSON.parse(String.fromCharCode.apply(String, payload)); diff --git a/ui-ngx/src/assets/converters/tbel-tti-decoder.raw b/ui-ngx/src/assets/converters/tbel-tti-decoder.raw index b4aed0a400..ff5744a55e 100644 --- a/ui-ngx/src/assets/converters/tbel-tti-decoder.raw +++ b/ui-ngx/src/assets/converters/tbel-tti-decoder.raw @@ -3,8 +3,12 @@ // Such a device gets the profile -, so all devices of one model share it, // or the name the integration metadata gives it under profile.-. // A device that is not in the Repository can name its model in decoded_payload: its payload formatter adds -// tbVersionIds: { brand_id, model_id }, which is not telemetry. Any other device gets LoRaWAN Device. +// tbVersionIds: { brand_id, model_id }, which is not telemetry; they become the attributes brandId and modelId, as the +// integration writes them from version_ids. Any other device gets LoRaWAN Device. +// The keys the integration metadata lists under attributes.- (comma-separated) are saved as +// attributes, not telemetry: device information and settings. var values = {}; +var attributes = {}; var history = []; var versionIds = null; if (metadata.payloadFormat == "JSON") { @@ -29,18 +33,37 @@ var modelId = metadata.modelId; if ((brandId == null || modelId == null) && versionIds instanceof java.util.Map) { brandId = versionIds.brand_id; modelId = versionIds.model_id; + if (brandId != null && modelId != null) { + attributes.put("brandId", brandId); + attributes.put("modelId", modelId); + } } var profile = "LoRaWAN Device"; if (brandId != null && modelId != null) { - profile = brandId + "-" + modelId; + var model = brandId + "-" + modelId; + profile = model; // The profile name chosen at install, kept in the integration metadata - var customProfile = metadata["profile." + profile]; + var customProfile = metadata["profile." + model]; if (customProfile != null && customProfile != "") { profile = customProfile; } + var attributeKeys = metadata["attributes." + model]; + if (attributeKeys != null && attributeKeys != "") { + foreach (item : attributeKeys.split(",")) { + var key = item.trim(); + if (key != "" && values.containsKey(key)) { + attributes.put(key, values.get(key)); + values.remove(key); + } + } + } +} +var result = { profile: profile, telemetry: { ts: metadata.ts, values: values } }; +if (!history.isEmpty()) { + history.add({ ts: metadata.ts, values: values }); + result.telemetry = history; } -if (history.isEmpty()) { - return { profile: profile, telemetry: { ts: metadata.ts, values: values } }; +if (!attributes.isEmpty()) { + result.attributes = attributes; } -history.add({ ts: metadata.ts, values: values }); -return { profile: profile, telemetry: history }; +return result; diff --git a/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw b/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw index b4aed0a400..ff5744a55e 100644 --- a/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw +++ b/ui-ngx/src/assets/converters/tbel-ttn-decoder.raw @@ -3,8 +3,12 @@ // Such a device gets the profile -, so all devices of one model share it, // or the name the integration metadata gives it under profile.-. // A device that is not in the Repository can name its model in decoded_payload: its payload formatter adds -// tbVersionIds: { brand_id, model_id }, which is not telemetry. Any other device gets LoRaWAN Device. +// tbVersionIds: { brand_id, model_id }, which is not telemetry; they become the attributes brandId and modelId, as the +// integration writes them from version_ids. Any other device gets LoRaWAN Device. +// The keys the integration metadata lists under attributes.- (comma-separated) are saved as +// attributes, not telemetry: device information and settings. var values = {}; +var attributes = {}; var history = []; var versionIds = null; if (metadata.payloadFormat == "JSON") { @@ -29,18 +33,37 @@ var modelId = metadata.modelId; if ((brandId == null || modelId == null) && versionIds instanceof java.util.Map) { brandId = versionIds.brand_id; modelId = versionIds.model_id; + if (brandId != null && modelId != null) { + attributes.put("brandId", brandId); + attributes.put("modelId", modelId); + } } var profile = "LoRaWAN Device"; if (brandId != null && modelId != null) { - profile = brandId + "-" + modelId; + var model = brandId + "-" + modelId; + profile = model; // The profile name chosen at install, kept in the integration metadata - var customProfile = metadata["profile." + profile]; + var customProfile = metadata["profile." + model]; if (customProfile != null && customProfile != "") { profile = customProfile; } + var attributeKeys = metadata["attributes." + model]; + if (attributeKeys != null && attributeKeys != "") { + foreach (item : attributeKeys.split(",")) { + var key = item.trim(); + if (key != "" && values.containsKey(key)) { + attributes.put(key, values.get(key)); + values.remove(key); + } + } + } +} +var result = { profile: profile, telemetry: { ts: metadata.ts, values: values } }; +if (!history.isEmpty()) { + history.add({ ts: metadata.ts, values: values }); + result.telemetry = history; } -if (history.isEmpty()) { - return { profile: profile, telemetry: { ts: metadata.ts, values: values } }; +if (!attributes.isEmpty()) { + result.attributes = attributes; } -history.add({ ts: metadata.ts, values: values }); -return { profile: profile, telemetry: history }; +return result; From 70c5eae81cbb9dd45bf8b484b58add76fd215aa7 Mon Sep 17 00:00:00 2001 From: dpinkevych Date: Tue, 10 Nov 2026 11:10:14 +0200 Subject: [PATCH 13/13] fix(iot-hub): the JS Things Stack converters check tbVersionIds as the TBEL ones do The key is removed whenever the decoded payload has it, and it names a model only when it is an object, so tbVersionIds: null or a list gives the same telemetry in JS and TBEL. The test of a tbVersionIds that is not an object covers a text, null and a list. The test of the renamed profile lists temperature, which is in the history record and in the latest values, so it fails if a listed key leaves a history record. --- .../controller/ConverterControllerTest.java | 23 ++++++++++++------- .../src/assets/converters/js-tti-decoder.raw | 4 ++-- .../src/assets/converters/js-ttn-decoder.raw | 4 ++-- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java index 2a01c73492..c4309b3add 100644 --- a/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java @@ -47,6 +47,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.regex.Pattern; import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.containsString; @@ -747,15 +748,17 @@ public class ConverterControllerTest extends AbstractControllerTest { testThingsStackDefaultConvertersOnPayload(payload.toString(), Map.of(), THINGS_STACK_REPOSITORY_DEVICE_RESULT); } - // tbVersionIds that is not an object names no model + // tbVersionIds that is not an object (a text, null, a list) names no model, and is not telemetry either @Test public void testThingsStackDefaultConvertersIgnoreTbVersionIdsThatIsNotAnObject() throws IOException { - ObjectNode payload = (ObjectNode) JacksonUtil.toJsonNode(readResource(THINGS_STACK_FORMATTER_DEVICE_PAYLOAD)); - ((ObjectNode) payload.get("uplink_message").get("decoded_payload")).put("tbVersionIds", "lansitec-macro-tracker"); String expectedDecodedMessage = THINGS_STACK_FORMATTER_DEVICE_RESULT.replace("\"profile\":\"lansitec-macro-tracker\"", "\"profile\":\"LoRaWAN Device\"") .replace("\"brandId\":\"lansitec\",\"modelId\":\"macro-tracker\",", ""); + for (String tbVersionIds : List.of("\"lansitec-macro-tracker\"", "null", "[\"lansitec\",\"macro-tracker\"]")) { + ObjectNode payload = (ObjectNode) JacksonUtil.toJsonNode(readResource(THINGS_STACK_FORMATTER_DEVICE_PAYLOAD)); + ((ObjectNode) payload.get("uplink_message").get("decoded_payload")).set("tbVersionIds", JacksonUtil.toJsonNode(tbVersionIds)); - testThingsStackDefaultConvertersOnPayload(payload.toString(), Map.of(), expectedDecodedMessage); + testThingsStackDefaultConvertersOnPayload(payload.toString(), Map.of(), expectedDecodedMessage); + } } // The keys the integration metadata lists for the model are attributes, not telemetry; spaces around them are ignored, @@ -770,16 +773,20 @@ public class ConverterControllerTest extends AbstractControllerTest { Map.of("attributes.lansitec-macro-tracker", " chargeState , firmwareVersionOfTheTracker"), expectedDecodedMessage); } - // The list is per brand and model, not per profile name: a renamed profile keeps it. History records stay telemetry + // The list is per brand and model, not per profile name: a renamed profile keeps it. History records stay telemetry: + // temperature is in the history record (24.1) and in the latest values (24.5), and only the latest one is an attribute @Test public void testThingsStackDefaultConvertersTakeTheListOfARepositoryDeviceByItsModel() throws IOException { + String latestTemperature = "\"temperature\":24.5,"; + assertThat(THINGS_STACK_REPOSITORY_DEVICE_RESULT.split(Pattern.quote(latestTemperature), -1)).hasSize(2); + assertThat(THINGS_STACK_REPOSITORY_DEVICE_RESULT).contains("{\"temperature\":24.1,\"co2\":550}"); String expectedDecodedMessage = THINGS_STACK_REPOSITORY_DEVICE_RESULT .replace("\"profile\":\"milesight-iot-am308\"", "\"profile\":\"AM308\"") - .replace("{\"battery\":95,", "{") - .replace("\"attributes\":{", "\"attributes\":{\"battery\":95,"); + .replace(latestTemperature, "") + .replace("\"attributes\":{", "\"attributes\":{\"temperature\":24.5,"); testThingsStackDefaultConverters(THINGS_STACK_REPOSITORY_DEVICE_PAYLOAD, - Map.of("profile.milesight-iot-am308", "AM308", "attributes.milesight-iot-am308", "battery"), expectedDecodedMessage); + Map.of("profile.milesight-iot-am308", "AM308", "attributes.milesight-iot-am308", "temperature"), expectedDecodedMessage); } // The list of another model changes nothing diff --git a/ui-ngx/src/assets/converters/js-tti-decoder.raw b/ui-ngx/src/assets/converters/js-tti-decoder.raw index 5b944841f1..9cc99a41a9 100644 --- a/ui-ngx/src/assets/converters/js-tti-decoder.raw +++ b/ui-ngx/src/assets/converters/js-tti-decoder.raw @@ -23,14 +23,14 @@ if (metadata.payloadFormat == 'JSON') { }); delete values.history; } - if (values.tbVersionIds != null) { + if (Object.prototype.hasOwnProperty.call(values, 'tbVersionIds')) { versionIds = values.tbVersionIds; delete values.tbVersionIds; } } var brandId = metadata.brandId; var modelId = metadata.modelId; -if ((brandId == null || modelId == null) && versionIds != null) { +if ((brandId == null || modelId == null) && typeof versionIds === 'object' && versionIds !== null && !Array.isArray(versionIds)) { brandId = versionIds.brand_id; modelId = versionIds.model_id; if (brandId != null && modelId != null) { diff --git a/ui-ngx/src/assets/converters/js-ttn-decoder.raw b/ui-ngx/src/assets/converters/js-ttn-decoder.raw index 5b944841f1..9cc99a41a9 100644 --- a/ui-ngx/src/assets/converters/js-ttn-decoder.raw +++ b/ui-ngx/src/assets/converters/js-ttn-decoder.raw @@ -23,14 +23,14 @@ if (metadata.payloadFormat == 'JSON') { }); delete values.history; } - if (values.tbVersionIds != null) { + if (Object.prototype.hasOwnProperty.call(values, 'tbVersionIds')) { versionIds = values.tbVersionIds; delete values.tbVersionIds; } } var brandId = metadata.brandId; var modelId = metadata.modelId; -if ((brandId == null || modelId == null) && versionIds != null) { +if ((brandId == null || modelId == null) && typeof versionIds === 'object' && versionIds !== null && !Array.isArray(versionIds)) { brandId = versionIds.brand_id; modelId = versionIds.model_id; if (brandId != null && modelId != null) {