Skip to content

Commit

Permalink
Merge pull request #174 from thingsboard/aura
Browse files Browse the repository at this point in the history
Aura
ViacheslavKlimov authored Dec 24, 2024
2 parents f16f3a3 + 230ed70 commit 963218e
Showing 18 changed files with 464 additions and 0 deletions.
39 changes: 39 additions & 0 deletions VENDORS/Tektelic/Aura/ChirpStack/uplink/converter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "ChirpStack Uplink Decoder for Aura",
"type": "UPLINK",
"debugMode": false,
"debugSettings": {
"failuresEnabled": true,
"allEnabled": false,
"allEnabledUntil": 1733331880270
},
"configuration": {
"scriptLang": "TBEL",
"decoder": null,
"tbelDecoder": "var data = decodeToJson(payload);\nvar deviceName = data.deviceInfo.deviceName + \" \" + data.deviceInfo.devEui;\nvar deviceType = \"Aura\";\nvar groupName = null; // If groupName is not null - created device will be added to the entity group with such name.\nvar customerName = null; // If customerName is not null - created devices will be assigned to customer with such name. \n\n// use assetName and assetType instead of deviceName and deviceType\n// to automatically create assets instead of devices.\n// var assetName = 'Asset A';\n// var assetType = 'building';\n\n// If you want to parse incoming data somehow, you can add your code to this function.\n// input: bytes\n// expected output:\n// {\n// \"attributes\": {\"attributeKey\": \"attributeValue\"},\n// \"telemetry\": [{\"ts\": 1...1, \"values\": {\"telemetryKey\":\"telemetryValue\"}, {\"ts\": 1...2, \"values\": {\"telemetryKey\":\"telemetryValue\"}}]\n// }\n\nfunction decodePayload(input) {\n var output = {\n attributes: {},\n telemetry: []\n };\n \n // --- Decoding code --- //\n var decoded = {};\n var fPort = data.fPort;\n if(fPort == 10) {\n for(var i = 0; i < input.length - 2; ) {\n var key_1 = input[i++] & 0xff;\n var key_2 = input[i++] & 0xff;\n \n if(key_1 == 0x00 && key_2 == 0xFE) {\n decoded.energy_consumption_meter_elapsed = parseBytesToInt(input, i, 4);\n decoded.energy_consumption_meter_consumed = parseBytesToInt(input, i + 4, 4);\n i += 8;\n }\n else if(key_1 == 0x00 && key_2 == 0x00) {\n decoded.energy_consumption_meter_status = parseBytesToInt(input, i, 1);\n if(decoded.energy_consumption_meter_status != 0) {\n decoded.energy_consumption_meter_status != 1;\n }\n }\n else if(key_1 == 0x00 && key_2 == 0x74) {\n decoded.voltmeter = (input[i] << 8) | (input[i + 1] & 0xFF);\n if (decoded.voltmeter != 65535) {\n decoded.voltmeter = decoded.voltmeter * 10;\n }\n \n i += 2;\n }\n else if(key_1 == 0x00 && key_2 == 0x75) {\n decoded.voltmeter = (input[i] << 8) | (input[i + 1] & 0xFF);\n if (decoded.ammeter != 65535) {\n decoded.ammeter = decoded.ammeter * 10;\n }\n \n i += 2;\n }\n else if(key_1 == 0x00 && key_2 == 0x80) {\n decoded.real_power = (input[i] << 8) | (input[i + 1] & 0xFF);\n if (decoded.real_power != 65535) {\n decoded.real_power = decoded.real_power * 10;\n }\n \n i += 2;\n }\n else if(key_1 == 0x01 && key_2 == 0x80) {\n decoded.apparent_power = (input[i] << 8) | (input[i + 1] & 0xFF);\n if (decoded.apparent_power != 65535) {\n decoded.apparent_power = decoded.apparent_power * 10;\n }\n \n i += 2;\n }\n else if(key_1 == 0x00 && key_2 == 0x81) {\n decoded.power_factor_meter = input[i] & 0xFF;\n if (decoded.power_factor_meter != 65535) {\n decoded.power_factor_meter = decoded.power_factor_meter * 100;\n }\n \n i += 1;\n }\n else if(key_1 == 0x00 && key_2 == 0x01) {\n decoded.relay_status = input[i] & 0xFF;\n \n if (decoded.relay_status != 0) {\n decoded.relay_status = 1;\n }\n \n i += 1;\n }\n }\n }\n else if (fPort == 100) {\n for(var i = 0; i < input.length -1; ) {\n var key = input[i++] & 0xff;\n \n if(key == 0x00) {\n output.attributes.eui = bytesToHex(java.util.Arrays.copyRange(input, i, i + 8));\n i += 8;\n }\n else if(key == 0x01) {\n output.attributes.app_eui = bytesToHex(java.util.Arrays.copyRange(input, i, i + 8));\n\t\t\t\ti += 8;\n }\n else if (key == 0x02) {\n output.attributes.app_key = bytesToHex(java.util.Arrays.copyRange(input, i, i + 16));\n i += 16;\n }\n else if (key == 0x03) {\n output.attributes.devAddr = bytesToHex(java.util.Arrays.copyRange(input, i, i + 4));\n i += 4;\n }\n else if(key == 0x04) {\n output.attributes.network_session_key = bytesToHex(java.util.Arrays.copyRange(input, i, i + 16));\n i += 16;\n }\n else if(key == 0x05) {\n output.attributes.app_session_key = bytesToHex(java.util.Arrays.copyRange(input, i, i + 16));\n i += 16;\n }\n else if(key == 0x10) {\n output.attributes.loramac_join_mode = (input[i] >> 7) & 1;\n i += 2;\n }\n else if (key == 0x11) {\n var val = (input[i] >> 4) & 0x0F;\n \n if(val == 0x0C) {\n output.attributes.lora_class = \"Class C\";\n }\n else if(val == 0x0B) {\n output.attributes.lora_class = \"Class B\";\n }\n else if(val == 0x0A) {\n output.attributes.lora_class = \"Class F\";\n }\n else {\n output.attributes.lora_class = \"Invalid\";\n }\n\t\t\t\t\n\t\t\t\toutput.attributes.confirm_mode = (input[i] >> 8) & 1; \n\t\t\t\toutput.attributes.sync_word = (input[i] >> 9) & 1;\n\t\t\t\toutput.attributes.duty_cycle = (input[i] >> 10) & 1;\n\t\t\t\toutput.attributes.adr = (input[i] >> 11) & 1; \n\t\t\t\t\n\t\t\t\ti += 2;\n }\n else if (key == 0x12) {\n output.attributes.dr_number = (((input[i] << 8) | input[i + 1]) >> 8) & 0xF;\n output.attributes.tx_power_number = ((input[i] << 8) | input[i + 1]) & 0xF;\n \n i +=2;\n }\n else if (key == 0x13) {\n output.attributes.frequency = (((input[i] << 32) | (input[i + 1] << 24) | (input[i + 2] << 16) | (input[i + 3] << 8) | input[i + 4]) >> 8) & 0xFFFFFFFF;\n output.attributes.dr_number_rx2 = input[i + 4] & 0xFF;\n \n i += 5;\n }\n else if(key == 0x19) {\n output.attributes.netid_msb = parseBytesToInt(input, i, 2);\n i += 2;\n }\n else if (key == 0x20) {\n output.attributes.seconds_per_core_tick = parseBytesToInt(input, i, 4);\n i += 4;\n }\n else if(key == 0x21) {\n output.attributes.tick_per_battery = parseBytesToInt(input, i, 2);\n i += 2;\n }\n else if(key == 0x71) {\n output.attributes.app_major_version = input[i];\n\t\t\t\toutput.attributes.app_minor_version = input[i + 1];\n\t\t\t\toutput.attributes.app_revision = input[i + 2];\n\t\t\t\toutput.attributes.loramac_major_version = input[i + 3];\n\t\t\t\toutput.attributes.loramac_minor_version = input[i + 4];\n\t\t\t\toutput.attributes.loramac_revision = input[i + 5];\n\t\t\t\toutput.attributes.region = input[i + 7];\n\t\t\t\ti += 7;\n }\n }\n }\n\n output.telemetry = [{\n ts: timestamp,\n values: decoded\n }];\n // --- Decoding code --- //\n return output;\n}\n\n// --- attributes and telemetry objects ---\nvar telemetry = [];\nvar attributes = {};\n// --- attributes and telemetry objects ---\n\n// --- Timestamp parsing\nvar dateString = data.time;\ntimestamp = parseDateToTimestamp(dateString);\n// --- Timestamp parsing\n\n// Passing incoming bytes to decodePayload function, to get custom decoding\nvar customDecoding = decodePayload(base64ToBytes(data.data));\n\n\nattributes.eui = data.deviceInfo.devEui;\n\n// Collecting data to result\nif (customDecoding.?telemetry.size() > 0) {\n if (customDecoding.telemetry instanceof java.util.ArrayList) {\n foreach(telemetryObj: customDecoding.telemetry) {\n if (telemetryObj.ts != null && telemetryObj.values != null) {\n telemetry.add(telemetryObj);\n }\n }\n } else {\n telemetry.putAll(customDecoding.telemetry);\n }\n}\n\nif (customDecoding.?attributes.size() > 0) {\n attributes.putAll(customDecoding.attributes);\n}\n\n// You can add some keys manually to attributes or telemetry\nattributes.eui = data.deviceInfo.?devEui;\nattributes.devAddr = data.devAddr;\nattributes.fPort = data.fPort;\nattributes.applicationId = data.deviceInfo.?applicationId;\nattributes.applicationName = data.deviceInfo.?applicationName;\nattributes.tenantId = data.deviceInfo.?tenantId;\nattributes.tenantName = data.deviceInfo.?tenantName;\nattributes.deviceProfileId = data.deviceInfo.?deviceProfileId;\nattributes.deviceProfileName = data.deviceInfo.?deviceProfileName;\nattributes.frequency = data.txInfo.?frequency;\nattributes.bandwidth = data.txInfo.?modulation.?lora.?bandwidth;\nattributes.spreadingFactor = data.txInfo.?modulation.?lora.?spreadingFactor;\nattributes.codeRate = data.txInfo.?modulation.?lora.?codeRate;\n\nif(Boolean.parseBoolean(metadata[\"includeGatewayInfo\"])) {\n var gatewayInfo = getGatewayInfo();\n var addDataToTelemetry = {};\n addDataToTelemetry.snr = gatewayInfo.snr;\n addDataToTelemetry.rssi = gatewayInfo.rssi;\n addDataToTelemetry.channel = gatewayInfo.channel;\n addDataToTelemetry.rfChain = gatewayInfo.rfChain;\n addDataToTelemetry.fCnt = data.fCnt;\n \n telemetry = processTelemetryData(telemetry, addDataToTelemetry);\n}\n\nvar result = {\n deviceName: deviceName,\n deviceType: deviceType,\n // assetName: assetName,\n // assetType: assetType,\n attributes: attributes,\n telemetry: telemetry\n};\n\naddAdditionalInfoForDeviceMsg(result, customerName, groupName);\n\nreturn result;\n\nfunction addAdditionalInfoForDeviceMsg(deviceInfo, customerName, groupName) {\n if (customerName != null) {\n deviceInfo.customerName = customerName;\n }\n if (groupName != null) {\n deviceInfo.groupName = groupName;\n }\n}\n\nfunction parseDateToTimestamp(dateString) {\n var date = new Date(dateString);\n var timestamp = date.getTime();\n \n // If we cannot parse timestamp - we will use the current timestamp\n if (timestamp == -1) {\n timestamp = Date.now();\n }\n \n return timestamp;\n}\n\nfunction getGatewayInfo() {\n var gatewayList = data.rxInfo;\n var maxRssi = Integer.MIN_VALUE;\n var gatewayInfo = {};\n \n foreach (gateway : gatewayList) {\n if(gateway.rssi > maxRssi) {\n maxRssi = gateway.rssi;\n gatewayInfo = gateway;\n }\n }\n \n return gatewayInfo;\n}\n\nfunction processTelemetryData(telemetry, addDataToTelemetry) {\n if (telemetry.size >= 1) {\n telemetry = addDataToTelemetries(telemetry, addDataToTelemetry);\n }\n else {\n telemetry.add(addDataToTelemetry);\n }\n \n return telemetry;\n}\n\nfunction addDataToTelemetries(telemetries, addDataToTelemetry) {\n foreach(telemetry : telemetries) {\n foreach(element : addDataToTelemetry.entrySet()) {\n if(!telemetry[\"values\"].keys.contains(element.key)) {\n telemetry[\"values\"][element.key] = element.value;\n }\n } \n }\n \n return telemetries;\n}",
"encoder": null,
"tbelEncoder": null,
"updateOnlyKeys": [
"tenantId",
"tenantName",
"applicationId",
"applicationName",
"deviceProfileId",
"deviceProfileName",
"devAddr",
"fPort",
"frequency",
"bandwidth",
"spreadingFactor",
"codeRate",
"channel",
"rfChain",
"eui",
"battery"
]
},
"additionalInfo": {
"description": ""
},
"edgeTemplate": false
}
4 changes: 4 additions & 0 deletions VENDORS/Tektelic/Aura/ChirpStack/uplink/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"integrationName": "ChirpStack integration",
"includeGatewayInfo": "false"
}
48 changes: 48 additions & 0 deletions VENDORS/Tektelic/Aura/ChirpStack/uplink/payload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"deduplicationId": "57433366-50a6-4dc2-8145-2df1bbc70d9e",
"time": "2023-05-22T07:47:05.404859+00:00",
"deviceInfo": {
"tenantId": "52f14cd4-c6f1-4fbd-8f87-4025e1d49242",
"tenantName": "ChirpStack",
"applicationId": "ca739e26-7b67-4f14-b69e-d568c22a5a75",
"applicationName": "Chirpstack application",
"deviceProfileId": "605d08d4-65f5-4d2c-8a5a-3d2457662f79",
"deviceProfileName": "Chirpstack default device profile",
"deviceName": "Device name",
"devEui": "1000000000000001",
"tags": {}
},
"devAddr": "20000001",
"adr": true,
"dr": 5,
"fCnt": 4,
"fPort": 10,
"confirmed": false,
"data": "AP4AAVGAAABqUAAAAA==",
"rxInfo": [{
"gatewayId": "6a7e111a10000000",
"uplinkId": 24022,
"time": "2023-05-22T07:47:05.404859+00:00",
"rssi": -35,
"snr": 11.5,
"channel": 2,
"rfChain": 1,
"location": {},
"context": "EFwMtA==",
"metadata": {
"region_common_name": "EU868",
"region_config_id": "eu868"
},
"crcStatus": "CRC_OK"
}],
"txInfo": {
"frequency": 868500000,
"modulation": {
"lora": {
"bandwidth": 125000,
"spreadingFactor": 7,
"codeRate": "CR_4_5"
}
}
}
}
27 changes: 27 additions & 0 deletions VENDORS/Tektelic/Aura/ChirpStack/uplink/result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"deviceName": "Device name 1000000000000001",
"deviceType": "Aura",
"attributes": {
"eui": "1000000000000001",
"devAddr": "20000001",
"fPort": 10,
"applicationId": "ca739e26-7b67-4f14-b69e-d568c22a5a75",
"applicationName": "Chirpstack application",
"tenantId": "52f14cd4-c6f1-4fbd-8f87-4025e1d49242",
"tenantName": "ChirpStack",
"deviceProfileId": "605d08d4-65f5-4d2c-8a5a-3d2457662f79",
"deviceProfileName": "Chirpstack default device profile",
"frequency": 868500000,
"bandwidth": 125000,
"spreadingFactor": 7,
"codeRate": "CR_4_5"
},
"telemetry": [{
"ts": 1684741625404,
"values": {
"energy_consumption_meter_elapsed": 86400,
"energy_consumption_meter_consumed": 27216,
"energy_consumption_meter_status": 0
}
}]
}
Loading

0 comments on commit 963218e

Please sign in to comment.