Browse Source
Added integration-specific encoders for TTN/TTI/MQTT/LORIOT integrationspull/16313/head
committed by
GitHub
11 changed files with 328 additions and 12 deletions
@ -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; |
|||
@ -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; |
|||
@ -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; |
|||
@ -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; |
|||
@ -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; |
|||
@ -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; |
|||
@ -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; |
|||
@ -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; |
|||
Loading…
Reference in new issue