Browse Source

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

pull/16535/head
dpinkevych 1 month ago
parent
commit
105ddcfd88
Failed to extract signature
  1. 33
      application/src/test/java/org/thingsboard/server/controller/ConverterControllerTest.java
  2. 210
      application/src/test/resources/payload/things_stack_lansitec_uplinks.json
  3. 20
      ui-ngx/src/assets/converters/js-tti-decoder.raw
  4. 20
      ui-ngx/src/assets/converters/js-ttn-decoder.raw
  5. 11
      ui-ngx/src/assets/converters/tbel-tti-decoder.raw
  6. 11
      ui-ngx/src/assets/converters/tbel-ttn-decoder.raw

33
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<String> keys) {

210
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": {}
}
}
]

20
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));
}

20
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));
}

11
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;

11
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;

Loading…
Cancel
Save