From f511edcce2190b0e4940ba26866f5143f7c4e469 Mon Sep 17 00:00:00 2001 From: dpinkevych Date: Mon, 9 Nov 2026 19:05:52 +0200 Subject: [PATCH] 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;