Skip to content

Commit

Permalink
Merge pull request #155 from artem-barysh-dev/em320-tilt
Browse files Browse the repository at this point in the history
EM320-TILT
  • Loading branch information
ashvayka authored Dec 11, 2024
2 parents ed149dc + f6b1dad commit 9343a07
Show file tree
Hide file tree
Showing 19 changed files with 509 additions and 0 deletions.
39 changes: 39 additions & 0 deletions VENDORS/Milesight/EM320-TILT/ChirpStack/uplink/converter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "ChirpStack Uplink Decoder for EM320-TILT",
"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 = \"EM320-TILT\";\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 for (var i = 0; i < input.length - 2 ; ) {\n var channel_id = input[i++] & 0xff;\n var channel_type = input[i++] & 0xff;\n \n // BATTERY\n if (channel_id === 0x01 && channel_type === 0x75) {\n decoded.battery = input[i];\n i += 1;\n }\n // ANGLE\n else if (channel_id === 0x03 && channel_type === 0xd4) {\n decoded.angle_x = (((input[i] & 0xFF) | (input[i + 1] << 8)) >> 1) / 100;\n decoded.angle_y = (((input[i + 2] & 0xFF) | (input[i + 3] << 8)) >> 1) / 100;\n decoded.angle_z = (((input[i + 4] & 0xFF) | (input[i + 5] << 8)) >> 1) / 100;\n decoded.threshold_x = (input[i] & 0x01) === 0x01 ? \"trigger\" : \"normal\";\n decoded.threshold_y = (input[i + 2] & 0x01) === 0x01 ? \"trigger\" : \"normal\";\n decoded.threshold_z = (input[i + 4] & 0x01) === 0x01 ? \"trigger\" : \"normal\";\n i += 6;\n }\n }\n\n output.telemetry = [{\n ts: timestamp,\n values: decoded\n }];\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/Milesight/EM320-TILT/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/Milesight/EM320-TILT/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": 85,
"confirmed": false,
"data": "AXVkA9QAAAEAUEY=",
"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"
}
}
}
}
31 changes: 31 additions & 0 deletions VENDORS/Milesight/EM320-TILT/ChirpStack/uplink/result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"deviceName": "Device name 1000000000000001",
"deviceType": "EM320-TILT",
"attributes": {
"eui": "1000000000000001",
"devAddr": "20000001",
"fPort": 85,
"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": {
"battery": 100,
"angle_x": 0,
"angle_y": 0,
"angle_z": 90,
"threshold_x": "normal",
"threshold_y": "trigger",
"threshold_z": "normal"
}
}]
}
29 changes: 29 additions & 0 deletions VENDORS/Milesight/EM320-TILT/LORIOT/uplink/converter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "Loriot Uplink Decoder for EM320-TILT",
"type": "UPLINK",
"debugMode": false,
"debugSettings": {
"failuresEnabled": true,
"allEnabled": false,
"allEnabledUntil": 1733331880270
},
"configuration": {
"scriptLang": "TBEL",
"decoder": "// Decode an uplink message from a buffer\n// payload - array of bytes\n// metadata - key/value object\n\n/** Decoder **/\n\n// decode payload to string\nvar payloadStr = decodeToString(payload);\n\n// decode payload to JSON\n// var data = decodeToJson(payload);\n\nvar deviceName = 'Device A';\nvar deviceType = 'thermostat';\nvar customerName = 'Customer C';\nvar groupName = 'thermostat devices';\nvar manufacturer = 'Example corporation';\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// Result object with device/asset attributes/telemetry data\nvar result = {\n// Use deviceName and deviceType or assetName and assetType, but not both.\n deviceName: deviceName,\n deviceType: deviceType,\n// assetName: assetName,\n// assetType: assetType,\n// customerName: customerName,\n groupName: groupName,\n attributes: {\n model: 'Model A',\n serialNumber: 'SN111',\n integrationName: metadata['integrationName'],\n manufacturer: manufacturer\n },\n telemetry: {\n temperature: 42,\n humidity: 80,\n rawData: payloadStr\n }\n};\n\n/** Helper functions **/\n\nfunction decodeToString(payload) {\n return String.fromCharCode.apply(String, payload);\n}\n\nfunction decodeToJson(payload) {\n // covert payload to string.\n var str = decodeToString(payload);\n\n // parse string to JSON\n var data = JSON.parse(str);\n return data;\n}\n\nreturn result;",
"tbelDecoder": "var data = decodeToJson(payload);\nvar deviceName = data.EUI;\nvar deviceType = \"EM320-TILT\";\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\": {\"telemetryKey\": \"telemetryValue\"}\n// }\n\nfunction decodePayload(input) {\n var output = { attributes: {}, telemetry: []};\n \n var decoded = {};\n for (var i = 0; i < input.length - 2 ; ) {\n var channel_id = input[i++] & 0xff;\n var channel_type = input[i++] & 0xff;\n\n // BATTERY\n if (channel_id === 0x01 && channel_type === 0x75) {\n decoded.battery = input[i];\n i += 1;\n }\n // ANGLE\n else if (channel_id === 0x03 && channel_type === 0xd4) {\n decoded.angle_x = (((input[i] & 0xFF) | (input[i + 1] << 8)) >> 1) / 100;\n decoded.angle_y = (((input[i + 2] & 0xFF) | (input[i + 3] << 8)) >> 1) / 100;\n decoded.angle_z = (((input[i + 4] & 0xFF) | (input[i + 5] << 8)) >> 1) / 100;\n decoded.threshold_x = (input[i] & 0x01) === 0x01 ? \"trigger\" : \"normal\";\n decoded.threshold_y = (input[i + 2] & 0x01) === 0x01 ? \"trigger\" : \"normal\";\n decoded.threshold_z = (input[i + 4] & 0x01) === 0x01 ? \"trigger\" : \"normal\";\n i += 6;\n }\n }\n \n output.telemetry = [{\n ts: timestamp,\n values: decoded\n }];\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\ntimestamp = data.ts;\n// --- Timestamp parsing\n\n// Message parsing\n// To avoid paths in the decoded objects we passing false value to function as \"pathInKey\" argument.\n// Warning: pathInKey can cause already found fields to be overwritten with the last value found.\n\nvar uplinkDataList = [];\n\n// Passing incoming bytes to decodePayload function, to get custom decoding\nvar customDecoding = decodePayload(hexToBytes(data.data));\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.EUI;\nattributes.fPort = data.port;\nattributes.frequency = data.freq;\n\nvar isIncludeGatewayInfo = metadata[\"includeGatewayInfo\"];\nif(isIncludeGatewayInfo == true) {\n var addDataToTelemetry = {};\n addDataToTelemetry.rssi = data.rssi;\n addDataToTelemetry.seqno = data.seqno;\n addDataToTelemetry.snr = data.snr;\n addDataToTelemetry.ack = data.ack;\n addDataToTelemetry.toa = data.toa;\n addDataToTelemetry.fCnt = data.fcnt;\n \n telemetry = processTelemetryData(telemetry, addDataToTelemetry);\n}\n\nvar deviceInfo = {\n deviceName: deviceName,\n deviceType: deviceType,\n// assetName: assetName,\n// assetType: assetType,\n attributes: attributes,\n telemetry: telemetry, \n};\n\naddAdditionalInfoForDeviceMsg(deviceInfo, customerName, groupName);\n\nuplinkDataList.add(deviceInfo);\n\nvar gatewayDeviceNamePrefix = \"Gateway \";\nvar gatewayDeviceType = \"Lora gateway\";\nvar gatewayGroupName = null; // If gatewayGroupName is not null - created device will be added to the entity group with such name.\n\nif (data.cmd == \"gw\") {\n foreach( gatewayInfo : data.gws ) {\n var addGatewayInfo = {};\n\n // You can add some keys manually telemetry\n addGatewayInfo.rssi = gatewayInfo.rssi;\n addGatewayInfo.snr = gatewayInfo.snr;\n // You can add some keys manually telemetry\n \n var gatewayInfoMsg = {\n deviceName: gatewayDeviceNamePrefix + gatewayInfo.gweui,\n deviceType: gatewayDeviceType,\n telemetry: [{\n \"ts\": gatewayInfo.ts,\n \"values\": addGatewayInfo\n }],\n attributes: {\n eui: gatewayInfo.gweui\n }\n };\n addAdditionalInfoForDeviceMsg(gatewayInfoMsg, customerName, gatewayGroupName);\n uplinkDataList.add(gatewayInfoMsg);\n }\n}\n\nreturn uplinkDataList;\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 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": [
"fPort",
"ack",
"eui",
"frequency",
"dr",
"battery"
]
},
"additionalInfo": {
"description": ""
},
"edgeTemplate": false
}
4 changes: 4 additions & 0 deletions VENDORS/Milesight/EM320-TILT/LORIOT/uplink/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"integrationName": "Loriot integration",
"includeGatewayInfo": "false"
}
17 changes: 17 additions & 0 deletions VENDORS/Milesight/EM320-TILT/LORIOT/uplink/payload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"cmd": "rx",
"seqno": 3040,
"EUI": "1000000000000001",
"ts": 1684478801936,
"fcnt": 2,
"port": 85,
"freq": 867500000,
"rssi": -21,
"snr": 10,
"toa": 206,
"dr": "SF9 BW125 4/5",
"ack": false,
"bat": 94,
"offline": false,
"data": "01756403D4000001005046"
}
21 changes: 21 additions & 0 deletions VENDORS/Milesight/EM320-TILT/LORIOT/uplink/result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[{
"deviceName": "1000000000000001",
"deviceType": "EM320-TILT",
"attributes": {
"eui": "1000000000000001",
"fPort": 85,
"frequency": 867500000
},
"telemetry": [{
"ts": 1684478801936,
"values": {
"battery": 100,
"angle_x": 0,
"angle_y": 0,
"angle_z": 90,
"threshold_x": "normal",
"threshold_y": "trigger",
"threshold_z": "normal"
}
}]
}]
Loading

0 comments on commit 9343a07

Please sign in to comment.