committed by
GitHub
11 changed files with 454 additions and 406 deletions
@ -0,0 +1,34 @@ |
|||
{ |
|||
"end_device_ids": { |
|||
"device_id": "manual-node", |
|||
"application_ids": { |
|||
"application_id": "my-app" |
|||
}, |
|||
"dev_eui": "70B3D57ED006F370" |
|||
}, |
|||
"received_at": "2026-10-28T10:00:00.123Z", |
|||
"uplink_message": { |
|||
"f_port": 1, |
|||
"f_cnt": 3, |
|||
"frm_payload": "Ae0DM18OTGM=", |
|||
"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-10-28T10:00:00.100Z" |
|||
} |
|||
} |
|||
@ -0,0 +1,64 @@ |
|||
{ |
|||
"end_device_ids": { |
|||
"device_id": "am308-office", |
|||
"application_ids": { |
|||
"application_id": "my-app" |
|||
}, |
|||
"dev_eui": "24E124707C481005", |
|||
"join_eui": "24E124C0002A0001", |
|||
"dev_addr": "260B1234" |
|||
}, |
|||
"received_at": "2026-10-28T10:00:00.123Z", |
|||
"uplink_message": { |
|||
"f_port": 85, |
|||
"f_cnt": 12, |
|||
"frm_payload": "AXVk", |
|||
"decoded_payload": { |
|||
"battery": 95, |
|||
"temperature": 24.5, |
|||
"co2": 560, |
|||
"history": [ |
|||
{ |
|||
"timestamp": 1793170000, |
|||
"temperature": 24.1, |
|||
"co2": 550 |
|||
} |
|||
] |
|||
}, |
|||
"rx_metadata": [ |
|||
{ |
|||
"gateway_ids": { |
|||
"gateway_id": "ug56" |
|||
}, |
|||
"rssi": -60, |
|||
"snr": 9.5, |
|||
"channel_index": 2 |
|||
} |
|||
], |
|||
"settings": { |
|||
"data_rate": { |
|||
"lora": { |
|||
"bandwidth": 125000, |
|||
"spreading_factor": 7, |
|||
"coding_rate": "4/5" |
|||
} |
|||
}, |
|||
"frequency": "868100000" |
|||
}, |
|||
"received_at": "2026-10-28T10:00:00.100Z", |
|||
"consumed_airtime": "0.061696s", |
|||
"version_ids": { |
|||
"brand_id": "milesight-iot", |
|||
"model_id": "am308", |
|||
"hardware_version": "1.0", |
|||
"firmware_version": "1.0", |
|||
"band_id": "EU_863_870" |
|||
}, |
|||
"network_ids": { |
|||
"net_id": "000013", |
|||
"tenant_id": "ttn", |
|||
"cluster_id": "eu1", |
|||
"cluster_address": "eu1.cloud.thethings.network" |
|||
} |
|||
} |
|||
} |
|||
@ -1,79 +1,31 @@ |
|||
/** |
|||
* Decodes the incoming payload and returns a structured object containing telemetry data and attributes. |
|||
* |
|||
* @param {byte[]} input - The raw payload received as an array of bytes. |
|||
* @returns {Object} output - The structured output with decoded telemetry and attributes. |
|||
*/ |
|||
|
|||
function decodePayload(input) { |
|||
// Initialize the output object with empty attributes and telemetry for clarity. |
|||
var result = { attributes: {}, telemetry: {}}; |
|||
|
|||
// Detect input type and normalize to byte array |
|||
var bytes; |
|||
|
|||
if (metadata.payloadFormat == 'JSON') { |
|||
// payloadFormat is 'JSON' when The Things Industries payload formatters are configured on the application or device level. |
|||
// In that case, decoded_payload is a JSON object produced by your formatter. |
|||
// This template assumes the formatter returns { "bytes": [...] } with the raw byte array. |
|||
// If your formatter returns pre-decoded values in a different structure, |
|||
// update this section to extract the values directly from 'parsed' instead. |
|||
var parsed = decodeToJson(input); |
|||
if (parsed.bytes == null) { |
|||
throw new Error("Payload formatter output does not contain a 'bytes' field. " + |
|||
"Update this decoder to match your formatter's output structure."); |
|||
// 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 <brand_id>-<model_id>, so all devices of one model share it. Any other device gets LoRaWAN Device. |
|||
var values = {}; |
|||
var history = []; |
|||
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 }); |
|||
} |
|||
} |
|||
bytes = parsed.bytes; |
|||
} else { |
|||
// payloadFormat is 'BINARY' when no payload formatter is configured — raw bytes from frm_payload are passed directly. |
|||
bytes = input; |
|||
values.remove("history"); |
|||
} |
|||
|
|||
// Decode serial number (SN) from the first 4 bytes of the payload. |
|||
// Press '?' icon in the top right corner to learn more about built in helper functions and capabilities. |
|||
result.attributes.sn = parseBytesToInt(bytes, 0, 4); |
|||
|
|||
// Extract the timestamp from metadata (represented in milliseconds). |
|||
var timestamp = metadata.ts; // ts is the timestamp parsed from the incoming message's time, or returns the current time if it cannot be parsed. |
|||
|
|||
// Combine the timestamp with decoded values and add it to the telemetry. |
|||
result.telemetry = { |
|||
ts: timestamp, |
|||
values: { |
|||
// Decode battery level from the 5th byte of the payload. |
|||
battery: parseBytesToInt(bytes, 4, 1), |
|||
// Decode temperature from the 6th and 7th bytes of the payload (divided by 100). |
|||
temperature: parseBytesToInt(bytes, 5, 2) / 100.0, |
|||
// Decode saturation from the 8th byte of the payload. |
|||
saturation: parseBytesToInt(bytes, 7, 1) |
|||
} |
|||
}; |
|||
|
|||
// Return the fully constructed output object. |
|||
return result; |
|||
// Same logic, less code: |
|||
// return { |
|||
// attributes: { |
|||
// sn: parseBytesToInt(bytes, 0, 4) |
|||
// }, |
|||
// telemetry: { |
|||
// ts: convertDateToTimestamp(extractDateFromMetadata()), |
|||
// values: { |
|||
// battery: parseBytesToInt(bytes, 4, 1), |
|||
// temperature: parseBytesToInt(bytes, 5, 2) / 100.0, |
|||
// saturation: parseBytesToInt(bytes, 7, 1) |
|||
// } |
|||
// } |
|||
// }; |
|||
} |
|||
|
|||
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'. |
|||
// result.name = 'Temperature Sensor'; // Device or asset name (the value must be unique) |
|||
// result.profile = 'IndustrialSensorProfile'; // Device or asset profile name. |
|||
// result.customer = 'MyCustomer'; // If customer is not null - created entity will be assigned to customer with such name. |
|||
// result.group = 'SensorsGroup'; // If group is not null - created entity will be added to the entity group with such name. |
|||
|
|||
// Return the final result object. |
|||
return result; |
|||
var profile = "LoRaWAN Device"; |
|||
if (metadata.brandId != null && metadata.modelId != null) { |
|||
profile = metadata.brandId + "-" + metadata.modelId; |
|||
} |
|||
// To give a model its own profile name, map it here, for example: |
|||
// if (profile == "milesight-iot-am308") { |
|||
// profile = "AM308"; |
|||
// } |
|||
if (history.isEmpty()) { |
|||
return { profile: profile, telemetry: { ts: metadata.ts, values: values } }; |
|||
} |
|||
history.add({ ts: metadata.ts, values: values }); |
|||
return { profile: profile, telemetry: history }; |
|||
|
|||
@ -1,79 +1,31 @@ |
|||
/** |
|||
* Decodes the incoming payload and returns a structured object containing telemetry data and attributes. |
|||
* |
|||
* @param {byte[]} input - The raw payload received as an array of bytes. |
|||
* @returns {Object} output - The structured output with decoded telemetry and attributes. |
|||
*/ |
|||
|
|||
function decodePayload(input) { |
|||
// Initialize the output object with empty attributes and telemetry for clarity. |
|||
var result = { attributes: {}, telemetry: {}}; |
|||
|
|||
// Detect input type and normalize to byte array |
|||
var bytes; |
|||
|
|||
if (metadata.payloadFormat == 'JSON') { |
|||
// payloadFormat is 'JSON' when The Things Network payload formatters are configured on the application or device level. |
|||
// In that case, decoded_payload is a JSON object produced by your formatter. |
|||
// This template assumes the formatter returns { "bytes": [...] } with the raw byte array. |
|||
// If your formatter returns pre-decoded values in a different structure, |
|||
// update this section to extract the values directly from 'parsed' instead. |
|||
var parsed = decodeToJson(input); |
|||
if (parsed.bytes == null) { |
|||
throw new Error("Payload formatter output does not contain a 'bytes' field. " + |
|||
"Update this decoder to match your formatter's output structure."); |
|||
// 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 <brand_id>-<model_id>, so all devices of one model share it. Any other device gets LoRaWAN Device. |
|||
var values = {}; |
|||
var history = []; |
|||
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 }); |
|||
} |
|||
} |
|||
bytes = parsed.bytes; |
|||
} else { |
|||
// payloadFormat is 'BINARY' when no payload formatter is configured — raw bytes from frm_payload are passed directly. |
|||
bytes = input; |
|||
values.remove("history"); |
|||
} |
|||
|
|||
// Decode serial number (SN) from the first 4 bytes of the payload. |
|||
// Press '?' icon in the top right corner to learn more about built in helper functions and capabilities. |
|||
result.attributes.sn = parseBytesToInt(bytes, 0, 4); |
|||
|
|||
// Extract the timestamp from metadata (represented in milliseconds). |
|||
var timestamp = metadata.ts; // ts is the timestamp parsed from the incoming message's time, or returns the current time if it cannot be parsed. |
|||
|
|||
// Combine the timestamp with decoded values and add it to the telemetry. |
|||
result.telemetry = { |
|||
ts: timestamp, |
|||
values: { |
|||
// Decode battery level from the 5th byte of the payload. |
|||
battery: parseBytesToInt(bytes, 4, 1), |
|||
// Decode temperature from the 6th and 7th bytes of the payload (divided by 100). |
|||
temperature: parseBytesToInt(bytes, 5, 2) / 100.0, |
|||
// Decode saturation from the 8th byte of the payload. |
|||
saturation: parseBytesToInt(bytes, 7, 1) |
|||
} |
|||
}; |
|||
|
|||
// Return the fully constructed output object. |
|||
return result; |
|||
// Same logic, less code: |
|||
// return { |
|||
// attributes: { |
|||
// sn: parseBytesToInt(bytes, 0, 4) |
|||
// }, |
|||
// telemetry: { |
|||
// ts: convertDateToTimestamp(extractDateFromMetadata()), |
|||
// values: { |
|||
// battery: parseBytesToInt(bytes, 4, 1), |
|||
// temperature: parseBytesToInt(bytes, 5, 2) / 100.0, |
|||
// saturation: parseBytesToInt(bytes, 7, 1) |
|||
// } |
|||
// } |
|||
// }; |
|||
} |
|||
|
|||
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'. |
|||
// result.name = 'Temperature Sensor'; // Device or asset name (the value must be unique) |
|||
// result.profile = 'IndustrialSensorProfile'; // Device or asset profile name. |
|||
// result.customer = 'MyCustomer'; // If customer is not null - created entity will be assigned to customer with such name. |
|||
// result.group = 'SensorsGroup'; // If group is not null - created entity will be added to the entity group with such name. |
|||
|
|||
// Return the final result object. |
|||
return result; |
|||
var profile = "LoRaWAN Device"; |
|||
if (metadata.brandId != null && metadata.modelId != null) { |
|||
profile = metadata.brandId + "-" + metadata.modelId; |
|||
} |
|||
// To give a model its own profile name, map it here, for example: |
|||
// if (profile == "milesight-iot-am308") { |
|||
// profile = "AM308"; |
|||
// } |
|||
if (history.isEmpty()) { |
|||
return { profile: profile, telemetry: { ts: metadata.ts, values: values } }; |
|||
} |
|||
history.add({ ts: metadata.ts, values: values }); |
|||
return { profile: profile, telemetry: history }; |
|||
|
|||
Loading…
Reference in new issue