diff --git a/ui-ngx/src/app/shared/models/converter.models.ts b/ui-ngx/src/app/shared/models/converter.models.ts index 98187efe93..e953cd1fb1 100644 --- a/ui-ngx/src/app/shared/models/converter.models.ts +++ b/ui-ngx/src/app/shared/models/converter.models.ts @@ -15,7 +15,7 @@ export enum ConverterType { DOWNLINK = 'DOWNLINK' } -export const IntegrationTbelDefaultConvertersUrl = new Map([ +export const IntegrationTbelDefaultDecodersUrl = new Map([ [IntegrationType.CHIRPSTACK, '/assets/converters/tbel-chirpstack-decoder.raw'], [IntegrationType.LORIOT, '/assets/converters/tbel-loriot-decoder.raw'], [IntegrationType.TTI,'/assets/converters/tbel-tti-decoder.raw'], @@ -31,7 +31,7 @@ export const IntegrationTbelDefaultConvertersUrl = new Map([ +export const IntegrationJSDefaultDecodersUrl = new Map([ [IntegrationType.CHIRPSTACK, '/assets/converters/js-chirpstack-decoder.raw'], [IntegrationType.LORIOT, '/assets/converters/js-loriot-decoder.raw'], [IntegrationType.TTI,'/assets/converters/js-tti-decoder.raw'], @@ -42,7 +42,20 @@ export const IntegrationJSDefaultConvertersUrl = new Map([ - [IntegrationType.OPC_UA, '/assets/converters/tbel-opc-ua-encoder.raw'] + [IntegrationType.OPC_UA, '/assets/converters/tbel-opc-ua-encoder.raw'], + [IntegrationType.TTI, '/assets/converters/tbel-ttn-tti-encoder.raw'], + [IntegrationType.TTN, '/assets/converters/tbel-ttn-tti-encoder.raw'], + [IntegrationType.MQTT, '/assets/converters/tbel-mqtt-encoder.raw'], + [IntegrationType.CHIRPSTACK, '/assets/converters/tbel-chirpstack-encoder.raw'], + [IntegrationType.LORIOT, '/assets/converters/tbel-loriot-encoder.raw'] +]) + +export const IntegrationJSDefaultEncodersUrl = new Map([ + [IntegrationType.TTI, '/assets/converters/js-ttn-tti-encoder.raw'], + [IntegrationType.TTN, '/assets/converters/js-ttn-tti-encoder.raw'], + [IntegrationType.MQTT, '/assets/converters/js-mqtt-encoder.raw'], + [IntegrationType.CHIRPSTACK, '/assets/converters/js-chirpstack-encoder.raw'], + [IntegrationType.LORIOT, '/assets/converters/js-loriot-encoder.raw'] ]) export const jsDefaultConvertersUrl = new Map([ @@ -262,16 +275,19 @@ export const getTargetField = export const getTargetTemplateUrl = (converterType: ConverterType, scriptLang: ScriptLanguage, integrationType: IntegrationType, converterVersion: ConverterVersion = 1): string => { - if (scriptLang === ScriptLanguage.JS && converterType === ConverterType.UPLINK && IntegrationJSDefaultConvertersUrl.has(integrationType)) { - return IntegrationJSDefaultConvertersUrl.get(integrationType) - } else if (scriptLang === ScriptLanguage.JS) { - return getJsTemplateUrl(converterType, converterVersion); - } else if (converterType === ConverterType.UPLINK && IntegrationTbelDefaultConvertersUrl.has(integrationType)) { - return IntegrationTbelDefaultConvertersUrl.get(integrationType); - } else if (converterType === ConverterType.DOWNLINK && IntegrationTbelDefaultEncodersUrl.has(integrationType)) { - return IntegrationTbelDefaultEncodersUrl.get(integrationType); + const isJs = scriptLang === ScriptLanguage.JS; + const isUplink = converterType === ConverterType.UPLINK; + // Integration-specific template map for this (language, direction); UPLINK -> decoders, DOWNLINK -> encoders. + const integrationUrls = isJs + ? (isUplink ? IntegrationJSDefaultDecodersUrl : IntegrationJSDefaultEncodersUrl) + : (isUplink ? IntegrationTbelDefaultDecodersUrl : IntegrationTbelDefaultEncodersUrl); + if (integrationUrls.has(integrationType)) { + return integrationUrls.get(integrationType); } - return getTbelTemplateUrl(converterType, converterVersion); + // Fall back to the generic template for the script language. + return isJs + ? getJsTemplateUrl(converterType, converterVersion) + : getTbelTemplateUrl(converterType, converterVersion); } const getTbelConverterFunctionHeldId = (converterType: ConverterType, converterVersion: ConverterVersion): string => { diff --git a/ui-ngx/src/assets/converters/js-chirpstack-encoder.raw b/ui-ngx/src/assets/converters/js-chirpstack-encoder.raw new file mode 100644 index 0000000000..3e8ec1d4b1 --- /dev/null +++ b/ui-ngx/src/assets/converters/js-chirpstack-encoder.raw @@ -0,0 +1,37 @@ +// Encode downlink data from incoming Rule Engine message + +// msg - JSON message payload downlink message json +// msgType - type of message, for ex. 'ATTRIBUTES_UPDATED', 'POST_TELEMETRY_REQUEST', etc. +// metadata - list of key-value pairs with additional data about the message +// ChirpStack reads DevEUI and fPort from the metadata below to enqueue the downlink (POST .../devices/{DevEUI}/queue). +// Both are client-side device attributes that could be populated via an Originator attributes rule node. +// ThingsBoard prefixes client-scope attributes with "cs_": +// metadata.cs_eui — ChirpStack device EUI stored on first uplink (e.g. "466ECD510CF7B021") +// metadata.cs_fPort — LoRaWAN frame port stored on first uplink (e.g. "1") +// integrationMetadata - list of key-value pairs with additional data defined in Integration executing this converter + +/** Encoder **/ + +// EXAMPLE — replace this: it encodes a single field ('powerState') from the downlink message. +// Swap 'powerState' (and the 'unspecified' fallback) for the field(s) and encoding your device expects; +// as written, any downlink without a 'powerState' field sends the constant value 'unspecified'. +var base64Payload = (msg.powerState != null) + ? btoa(msg.powerState) // if powerState is present, encode it to base64 + : btoa('unspecified'); // fall back to 'unspecified' when absent + +// Result object with encoded downlink payload +var result = { + // downlink data content type: JSON, TEXT or BINARY (base64 format) + contentType: "TEXT", + // downlink data + data: base64Payload, + // Metadata required by ChirpStack for downlink delivery. + metadata: { + // Target device EUI; ChirpStack rejects the downlink if this is missing. + DevEUI: metadata.cs_eui, + // LoRaWAN frame port (1-223); ChirpStack parses this as an integer. + fPort: metadata.cs_fPort + } +}; + +return result; diff --git a/ui-ngx/src/assets/converters/js-loriot-decoder.raw b/ui-ngx/src/assets/converters/js-loriot-decoder.raw index 8021611b56..b0bcab08a2 100644 --- a/ui-ngx/src/assets/converters/js-loriot-decoder.raw +++ b/ui-ngx/src/assets/converters/js-loriot-decoder.raw @@ -52,6 +52,13 @@ function decodePayload(input) { // }; } +// Process only LORIOT uplink messages: cmd is 'rx', or 'gw' when gateway info is included. +// Skip downlink confirmations (e.g. cmd == 'txd') and other events so they +// are not stored as telemetry. Returning an empty array yields no uplink data. +if (metadata.cmd != 'rx' && metadata.cmd != 'gw') { + return []; +} + var result = decodePayload(payload); // Uncomment this code block to overwrite values set in the main configuration window. Useful if you extract device/asset/customer/group names from the payload; // result.type = 'DEVICE'; // Entity type allows you to choose type of created entity. Can be 'DEVICE' or 'ASSET'. diff --git a/ui-ngx/src/assets/converters/js-loriot-encoder.raw b/ui-ngx/src/assets/converters/js-loriot-encoder.raw new file mode 100644 index 0000000000..879469edec --- /dev/null +++ b/ui-ngx/src/assets/converters/js-loriot-encoder.raw @@ -0,0 +1,37 @@ +// Encode downlink data from incoming Rule Engine message. +// msg - JSON message payload (e.g. { "state": "on" }) +// msgType - message type, e.g. 'ATTRIBUTES_UPDATED' +// metadata - enriched by the Originator attributes node with device client attributes. +// ThingsBoard prefixes client-scope attributes with "cs_": +// metadata.cs_eui — LORIOT device EUI stored on first uplink (e.g. "466ECD510CF7B021") +// metadata.cs_fPort — LoRaWAN frame port stored on first uplink (e.g. "1") + +var eui = metadata.cs_eui; + +// Default port to 1 when the device fPort attribute is missing or empty. +var port = (metadata.cs_fPort != null && metadata.cs_fPort != '') + ? parseInt(metadata.cs_fPort) + : 1; + +// EXAMPLE — replace this: it sends a single field ('powerState') from the downlink message. +// Swap 'powerState' (and the 'unspecified' fallback) for the field your device expects; +// as written, any downlink without a 'powerState' field sends the constant value 'unspecified'. +var payload = (msg.powerState != null) + ? msg.powerState + : 'unspecified'; + +var result = { + // Downlink data content type: JSON, TEXT, or BINARY (base64 format). + contentType: "TEXT", + + // Downlink data — the command value; LORIOT hex-encodes it before delivery. + data: payload, + + // Metadata required by LORIOT for downlink delivery. + metadata: { + "EUI": eui, + "port": port + } +}; + +return result; diff --git a/ui-ngx/src/assets/converters/js-mqtt-encoder.raw b/ui-ngx/src/assets/converters/js-mqtt-encoder.raw new file mode 100644 index 0000000000..71739f2029 --- /dev/null +++ b/ui-ngx/src/assets/converters/js-mqtt-encoder.raw @@ -0,0 +1,20 @@ +// Encode downlink data from incoming Rule Engine message + +// msg - JSON message payload downlink message json +// msgType - type of message, for ex. 'ATTRIBUTES_UPDATED', 'POST_TELEMETRY_REQUEST', etc. +// metadata - list of key-value pairs with additional data about the message +// integrationMetadata - list of key-value pairs with additional data defined in Integration executing this converter + +/** Encoder **/ + +// Publish the ThingsBoard message as JSON to the device upload topic. +// metadata.topic is read by the integration when Downlink topic pattern is ${topic}. +var result = { + contentType: "JSON", + data: JSON.stringify(msg), + metadata: { + topic: metadata['deviceType'] + '/' + metadata['deviceName'] + '/upload' + } +}; + +return result; diff --git a/ui-ngx/src/assets/converters/js-ttn-tti-encoder.raw b/ui-ngx/src/assets/converters/js-ttn-tti-encoder.raw new file mode 100644 index 0000000000..e30c1c75ef --- /dev/null +++ b/ui-ngx/src/assets/converters/js-ttn-tti-encoder.raw @@ -0,0 +1,49 @@ +// Encode downlink data from incoming Rule Engine message +// Shared by the TTN and TTI integrations (The Things Stack). + +// msg - JSON message payload downlink message json +// msgType - type of message, for ex. 'ATTRIBUTES_UPDATED', 'POST_TELEMETRY_REQUEST', etc. +// metadata - list of key-value pairs with additional data about the message +// integrationMetadata - list of key-value pairs with additional data defined in Integration executing this converter + +/** Encoder **/ + +// Target device identifier used to build the TTN/TTI downlink topic (devId is assumed to be added via Originator fields rule node) +var devId = metadata.devId; +// LoRaWAN frame port the downlink is scheduled on (cs_fPort is assumed to be added via Originator attributes rule node, prefix 'cs_' (client-side) is added by rule node) +// Default port to 1 when the device fPort attribute is missing or empty. parseInt keeps f_port a number, as the TTN/TTI API expects. +var fPort = (metadata.cs_fPort != null && metadata.cs_fPort != '') + ? parseInt(metadata.cs_fPort) + : 1; + +// EXAMPLE — replace this: it encodes a single field ('powerState') from the downlink message. +// Swap 'powerState' (and the 'unspecified' fallback) for the field(s) and encoding your device expects; +// as written, any downlink without a 'powerState' field sends the constant value 'unspecified'. +var base64Payload = (msg.powerState != null) + ? btoa(msg.powerState) // if powerState is present, encode it to base64 + : btoa('unspecified'); // fall back to 'unspecified' when absent + +// The Things Stack (TTN/TTI) expects a 'downlinks' array; each entry is a single scheduled downlink. +var data = { + downlinks: [{ + // Frame port to deliver the payload on (must match the device's expected port). + f_port: fPort, + // false - best-effort downlink; set to true to request an acknowledgement from the device. + confirmed: false, + // Downlink payload, base64-encoded as required by the TTN/TTI API. + frm_payload: base64Payload, + // Scheduling priority for the network server: LOWEST, LOW, BELOW_NORMAL, NORMAL, ABOVE_NORMAL, HIGH, HIGHEST. + priority: "NORMAL" + }] +}; + +// Result object with encoded downlink payload +var result = { + contentType: "JSON", + data: JSON.stringify(data), + metadata: { + devId: devId + } +}; + +return result; diff --git a/ui-ngx/src/assets/converters/tbel-chirpstack-encoder.raw b/ui-ngx/src/assets/converters/tbel-chirpstack-encoder.raw new file mode 100644 index 0000000000..3e8ec1d4b1 --- /dev/null +++ b/ui-ngx/src/assets/converters/tbel-chirpstack-encoder.raw @@ -0,0 +1,37 @@ +// Encode downlink data from incoming Rule Engine message + +// msg - JSON message payload downlink message json +// msgType - type of message, for ex. 'ATTRIBUTES_UPDATED', 'POST_TELEMETRY_REQUEST', etc. +// metadata - list of key-value pairs with additional data about the message +// ChirpStack reads DevEUI and fPort from the metadata below to enqueue the downlink (POST .../devices/{DevEUI}/queue). +// Both are client-side device attributes that could be populated via an Originator attributes rule node. +// ThingsBoard prefixes client-scope attributes with "cs_": +// metadata.cs_eui — ChirpStack device EUI stored on first uplink (e.g. "466ECD510CF7B021") +// metadata.cs_fPort — LoRaWAN frame port stored on first uplink (e.g. "1") +// integrationMetadata - list of key-value pairs with additional data defined in Integration executing this converter + +/** Encoder **/ + +// EXAMPLE — replace this: it encodes a single field ('powerState') from the downlink message. +// Swap 'powerState' (and the 'unspecified' fallback) for the field(s) and encoding your device expects; +// as written, any downlink without a 'powerState' field sends the constant value 'unspecified'. +var base64Payload = (msg.powerState != null) + ? btoa(msg.powerState) // if powerState is present, encode it to base64 + : btoa('unspecified'); // fall back to 'unspecified' when absent + +// Result object with encoded downlink payload +var result = { + // downlink data content type: JSON, TEXT or BINARY (base64 format) + contentType: "TEXT", + // downlink data + data: base64Payload, + // Metadata required by ChirpStack for downlink delivery. + metadata: { + // Target device EUI; ChirpStack rejects the downlink if this is missing. + DevEUI: metadata.cs_eui, + // LoRaWAN frame port (1-223); ChirpStack parses this as an integer. + fPort: metadata.cs_fPort + } +}; + +return result; diff --git a/ui-ngx/src/assets/converters/tbel-loriot-decoder.raw b/ui-ngx/src/assets/converters/tbel-loriot-decoder.raw index 5628a2f6c4..248015eb7c 100644 --- a/ui-ngx/src/assets/converters/tbel-loriot-decoder.raw +++ b/ui-ngx/src/assets/converters/tbel-loriot-decoder.raw @@ -52,6 +52,13 @@ function decodePayload(input) { // }; } +// Process only LORIOT uplink messages: cmd is 'rx', or 'gw' when gateway info is included. +// Skip downlink confirmations (e.g. cmd == 'txd') and other events so they +// are not stored as telemetry. Returning an empty array yields no uplink data. +if (metadata.cmd != 'rx' && metadata.cmd != 'gw') { + return []; +} + var result = decodePayload(payload); // Uncomment this code block to overwrite values set in the main configuration window. Useful if you extract device/asset/customer/group names from the payload; // result.type = 'DEVICE'; // Entity type allows you to choose type of created entity. Can be 'DEVICE' or 'ASSET'. diff --git a/ui-ngx/src/assets/converters/tbel-loriot-encoder.raw b/ui-ngx/src/assets/converters/tbel-loriot-encoder.raw new file mode 100644 index 0000000000..879469edec --- /dev/null +++ b/ui-ngx/src/assets/converters/tbel-loriot-encoder.raw @@ -0,0 +1,37 @@ +// Encode downlink data from incoming Rule Engine message. +// msg - JSON message payload (e.g. { "state": "on" }) +// msgType - message type, e.g. 'ATTRIBUTES_UPDATED' +// metadata - enriched by the Originator attributes node with device client attributes. +// ThingsBoard prefixes client-scope attributes with "cs_": +// metadata.cs_eui — LORIOT device EUI stored on first uplink (e.g. "466ECD510CF7B021") +// metadata.cs_fPort — LoRaWAN frame port stored on first uplink (e.g. "1") + +var eui = metadata.cs_eui; + +// Default port to 1 when the device fPort attribute is missing or empty. +var port = (metadata.cs_fPort != null && metadata.cs_fPort != '') + ? parseInt(metadata.cs_fPort) + : 1; + +// EXAMPLE — replace this: it sends a single field ('powerState') from the downlink message. +// Swap 'powerState' (and the 'unspecified' fallback) for the field your device expects; +// as written, any downlink without a 'powerState' field sends the constant value 'unspecified'. +var payload = (msg.powerState != null) + ? msg.powerState + : 'unspecified'; + +var result = { + // Downlink data content type: JSON, TEXT, or BINARY (base64 format). + contentType: "TEXT", + + // Downlink data — the command value; LORIOT hex-encodes it before delivery. + data: payload, + + // Metadata required by LORIOT for downlink delivery. + metadata: { + "EUI": eui, + "port": port + } +}; + +return result; diff --git a/ui-ngx/src/assets/converters/tbel-mqtt-encoder.raw b/ui-ngx/src/assets/converters/tbel-mqtt-encoder.raw new file mode 100644 index 0000000000..71739f2029 --- /dev/null +++ b/ui-ngx/src/assets/converters/tbel-mqtt-encoder.raw @@ -0,0 +1,20 @@ +// Encode downlink data from incoming Rule Engine message + +// msg - JSON message payload downlink message json +// msgType - type of message, for ex. 'ATTRIBUTES_UPDATED', 'POST_TELEMETRY_REQUEST', etc. +// metadata - list of key-value pairs with additional data about the message +// integrationMetadata - list of key-value pairs with additional data defined in Integration executing this converter + +/** Encoder **/ + +// Publish the ThingsBoard message as JSON to the device upload topic. +// metadata.topic is read by the integration when Downlink topic pattern is ${topic}. +var result = { + contentType: "JSON", + data: JSON.stringify(msg), + metadata: { + topic: metadata['deviceType'] + '/' + metadata['deviceName'] + '/upload' + } +}; + +return result; diff --git a/ui-ngx/src/assets/converters/tbel-ttn-tti-encoder.raw b/ui-ngx/src/assets/converters/tbel-ttn-tti-encoder.raw new file mode 100644 index 0000000000..a8306cddab --- /dev/null +++ b/ui-ngx/src/assets/converters/tbel-ttn-tti-encoder.raw @@ -0,0 +1,49 @@ +// Encode downlink data from incoming Rule Engine message +// Shared by the TTN and TTI integrations (The Things Stack). + +// msg - JSON message payload downlink message json +// msgType - type of message, for ex. 'ATTRIBUTES_UPDATED', 'POST_TELEMETRY_REQUEST', etc. +// metadata - list of key-value pairs with additional data about the message +// integrationMetadata - list of key-value pairs with additional data defined in Integration executing this converter + +/** Encoder **/ + +// Target device identifier used to build the TTN/TTI downlink topic (devId is assumed to be added via Originator fields rule node) +var devId = metadata.devId; +// LoRaWAN frame port the downlink is scheduled on (cs_fPort is assumed to be added via Originator attributes rule node, prefix 'cs_' (client-side) is added by rule node) +// Default port to 1 when the device fPort attribute is missing or empty. parseInt keeps f_port a number, as the TTN/TTI API expects. +var fPort = (metadata.cs_fPort != null && metadata.cs_fPort != '') + ? parseInt(metadata.cs_fPort) + : 1; + +// EXAMPLE — replace this: it encodes a single field ('powerState') from the downlink message. +// Swap 'powerState' (and the 'unspecified' fallback) for the field(s) and encoding your device expects; +// as written, any downlink without a 'powerState' field sends the constant value 'unspecified'. +var base64Payload = (msg.powerState != null) + ? btoa(msg.powerState) // if powerState is present, encode it to base64 + : btoa('unspecified'); // fall back to 'unspecified' when absent + +// The Things Stack (TTN/TTI) expects a 'downlinks' array; each entry is a single scheduled downlink. +var data = { + downlinks: [{ + // Frame port to deliver the payload on (must match the device's expected port). + f_port: fPort, + // false - best-effort downlink; set to true to request an acknowledgement from the device. + confirmed: false, + // Downlink payload, base64-encoded as required by the TTN/TTI API. + frm_payload: base64Payload, + // Scheduling priority for the network server: LOWEST, LOW, BELOW_NORMAL, NORMAL, ABOVE_NORMAL, HIGH, HIGHEST. + priority: "NORMAL" + }] +}; + +// Result object with encoded downlink payload +var result = { + contentType: "JSON", + data: JSON.stringify(data), + metadata: { + devId: devId + } +}; + +return result;