diff --git a/dingtek-dc520/plugin.json b/dingtek-dc520/plugin.json index 420764e5a..3d94e9671 100644 --- a/dingtek-dc520/plugin.json +++ b/dingtek-dc520/plugin.json @@ -1,134 +1,768 @@ { - "name": "dingtek_dc520", - "version": "1.0.0", - "description": "The CNDingtek dc520 is a microwave radar bidirection people counter for indoor using, which calculate the people number for in/out respectively.", - "author": "Thinger.io", - "license": "MIT", - "repository": { - "type": "git", - "url": "https://github.com/thinger-io/plugins.git", - "directory": "dingtek-dc520" - }, - "metadata": { - "name": "Dingtek DC520", + "name": "dingtek-dc520", + "version": "1.0.0", "description": "The CNDingtek dc520 is a microwave radar bidirection people counter for indoor using, which calculate the people number for in/out respectively.", + "author": "Thinger.io", + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/thinger-io/plugins.git", + "directory": "dingtek-dc520" + }, + "metadata": { + "name": "Dingtek DC520", + "description": "The CNDingtek dc520 is a microwave radar bidirection people counter for indoor using, which calculate the people number for in/out respectively.", "image": "assets/dc520.png", "category": "devices", "vendor": "dingtek" - }, - "resources": { - "products": [ - { - "description": "The CNDingtek dc520 is a microwave radar bidirection people counter for indoor using, which calculate the people number for in/out respectively.", - "enabled": true, - "name": "Dingtek DC520", - "product": "dingtek_dc520", - "profile": { - "api": { - "downlink": { - "enabled": true, - "handle_connectivity": false, - "request": { - "data": { - "path": "/downlink", - "payload": "{\n \"data\" : \"{{payload.data=\"\"}}\",\n \"port\" : {{payload.port=3}},\n \"priority\": {{payload.priority=3}},\n \"confirmed\" : {{payload.confirmed=false}},\n \"uplink\" : {{property.uplink}} \n}", - "payload_function": "", - "payload_type": "", - "plugin": "{{property.uplink.source}}", - "target": "plugin_endpoint" - } - } - }, - "uplink": { - "device_id_resolver": "getId", - "enabled": true, - "handle_connectivity": true, - "request": { - "data": { - "payload": "{{payload}}", - "payload_function": "", - "payload_type": "source_payload", - "resource_stream": "uplink", - "target": "resource_stream" + }, + "resources": { + "products": [ + { + "config": { + "icons": [] + }, + "description": "The CNDingtek dc520 is a microwave radar bidirection people counter for indoor using, which calculate the people number for in/out respectively.", + "enabled": true, + "name": "Dingtek DC520", + "product": "dingtek_dc520", + "profile": { + "api": { + "downlink": { + "enabled": true, + "handle_connectivity": false, + "request": { + "data": { + "path": "/downlink", + "payload": "{\n \"data\" : \"{{payload.data=\"\"}}\",\n \"port\" : {{payload.port=3}},\n \"priority\": {{payload.priority=3}},\n \"confirmed\" : {{payload.confirmed=false}},\n \"uplink\" : {{property.uplink}} \n}", + "payload_function": "", + "payload_type": "", + "plugin": "{{property.uplink.source}}", + "target": "plugin_endpoint" + } + } + }, + "uplink": { + "device_id_resolver": "getId", + "enabled": true, + "handle_connectivity": true, + "request": { + "data": { + "payload": "{{payload}}", + "payload_function": "", + "payload_type": "source_payload", + "resource_stream": "uplink", + "target": "resource_stream" + } + } + } + }, + "autoprovisions": { + "device_autoprovisioning": { + "config": { + "mode": "pattern", + "pattern": "dc520_.*" + }, + "enabled": true + } + }, + "buckets": { + "dingtek_dc520_data": { + "backend": "mongodb", + "data": { + "payload": "{{payload}}", + "payload_function": "", + "payload_type": "source_payload", + "resource_stream": "uplink_decoded", + "source": "resource_stream" + }, + "enabled": true, + "retention": { + "period": 3, + "unit": "months" + }, + "tags": [] + } + }, + "code": { + "code": "function decodeThingerUplink(thingerData) {\n // 0. If data has already been decoded, we will return it\n if (thingerData.decodedPayload) return thingerData.decodedPayload;\n \n // 1. Extract and Validate Input\n // We need 'payload' (hex string) and 'fPort' (integer)\n const hexPayload = thingerData.payload || \"\";\n const port = thingerData.fPort || 1;\n\n // 2. Convert Hex String to Byte Array\n const bytes = [];\n for (let i = 0; i < hexPayload.length; i += 2) {\n bytes.push(parseInt(hexPayload.substr(i, 2), 16));\n }\n\n // 3. Dynamic Function Detection and Execution\n \n // CASE A: (The Things Stack v3)\n if (typeof decodeUplink === 'function') {\n try {\n const input = {\n bytes: bytes,\n fPort: port\n };\n var result = decodeUplink(input);\n \n if (result.data) return result.data;\n\n return result; \n } catch (e) {\n console.error(\"Error inside decodeUplink:\", e);\n throw e;\n }\n }\n\n // CASE B: Legacy TTN (v2)\n else if (typeof Decoder === 'function') {\n try {\n return Decoder(bytes, port);\n } catch (e) {\n console.error(\"Error inside Decoder:\", e);\n throw e;\n }\n }\n\n // CASE C: No decoder found\n else {\n throw new Error(\"No compatible TTN decoder function (decodeUplink or Decoder) found in scope.\");\n }\n}\n\n\n// TTN decoder\n//IEEE754 hex to float convert\nfunction hex2float(num) {\n var sign = num & 0x80000000 ? -1 : 1;\n var exponent = ((num >> 23) & 0xff) - 127;\n var mantissa = 1 + (num & 0x7fffff) / 0x7fffff;\n return sign * mantissa * Math.pow(2, exponent);\n}\n\nfunction decodeUplink(input) {\n if (input.fPort != 3) {\n return {\n errors: ['unknown FPort'],\n };\n }\n switch (input.bytes.length) {\n case 19:\n return {\n // Decoded data\n data: {\n balanceCounter: ((input.bytes[5] << 8) + input.bytes[6]),\n inCounter: ((input.bytes[7] << 8) + input.bytes[8]),\n outCounter: ((input.bytes[9] << 8) + input.bytes[10]),\n alarmBalance: input.bytes[11] >> 4,\n alarmIn: input.bytes[11] & 0x0F,\n alarmOut: input.bytes[12] >> 4,\n alarmBattery: input.bytes[12] & 0x0F,\n volt: ((input.bytes[13] << 8) + input.bytes[14]),\n errorCode: input.bytes[15],\n frameCounter: (input.bytes[16] << 8) + input.bytes[17],\n },\n };\n case 18:\n return {\n // Decoded data\n data: {\n firmware: input.bytes[5] + \".\" + input.bytes[6],\n uploadInterval: input.bytes[7],\n originalBalance: (input.bytes[8] << 8) + input.bytes[9],\n balanceThreshold: (input.bytes[10] << 8) + input.bytes[11],\n inThreshold: (input.bytes[12] << 8) + input.bytes[13],\n outThreshold: (input.bytes[14] << 8) + input.bytes[15],\n batteryThreshold: input.bytes[16]\n\n },\n };\n default:\n return {\n errors: ['wrong length'],\n };\n }\n}\n\n// Encode downlink function.\n//\n// Input is an object with the following fields:\n// - data = Object representing the payload that must be encoded.\n// - variables = Object containing the configured device variables.\n//\n// Output must be an object with the following fields:\n// - bytes = Byte array containing the downlink payload.\nfunction encodeDownlink(input) {\n if (input.data.uploadInterval !== null && !isNaN(input.data.uploadInterval)) {\n var uploadInterval = input.data.uploadInterval;\n var uploadInterval_high = uploadInterval.toString(16).padStart(2, '0').toUpperCase()[0].charCodeAt(0);\n var uploadInterval_low = uploadInterval.toString(16).padStart(2, '0').toUpperCase()[1].charCodeAt(0);\n if (uploadInterval > 168 || uploadInterval < 1) {\n return {\n errors: ['upload interval range 1-168 hours.'],\n };\n } else {\n return {\n // LoRaWAN FPort used for the downlink message\n fPort: 3,\n // Encoded bytes\n bytes: [0x38, 0x30, 0x30, 0x32, 0x39, 0x39, 0x39, 0x39, 0x30, 0x31, uploadInterval_high, uploadInterval_low, 0x38, 0x31],\n };\n }\n }\n if (input.data.originalBalance !== null && !isNaN(input.data.originalBalance)) {\n var originalBalance = input.data.originalBalance;\n var originalBalance_1st = originalBalance.toString(16).padStart(4, '0').toUpperCase()[0].charCodeAt(0);\n var originalBalance_2nd = originalBalance.toString(16).padStart(4, '0').toUpperCase()[1].charCodeAt(0);\n var originalBalance_3rd = originalBalance.toString(16).padStart(4, '0').toUpperCase()[2].charCodeAt(0);\n var originalBalance_4th = originalBalance.toString(16).padStart(4, '0').toUpperCase()[3].charCodeAt(0);\n if (originalBalance > 65535 || originalBalance < 1) {\n return {\n errors: ['original balance range 1-65535.'],\n };\n } else {\n return {\n // LoRaWAN FPort used for the downlink message\n fPort: 3,\n // Encoded bytes\n bytes: [0x38, 0x30, 0x30, 0x32, 0x39, 0x39, 0x39, 0x39, 0x30, 0x41, originalBalance_1st, originalBalance_2nd, originalBalance_3rd, originalBalance_4th, 0x38, 0x31],\n };\n }\n }\n if (input.data.balanceThreshold !== null && !isNaN(input.data.balanceThreshold)) {\n var balanceThreshold = input.data.balanceThreshold;\n var balanceThreshold_1st = balanceThreshold.toString(16).padStart(4, '0').toUpperCase()[0].charCodeAt(0);\n var balanceThreshold_2nd = balanceThreshold.toString(16).padStart(4, '0').toUpperCase()[1].charCodeAt(0);\n var balanceThreshold_3rd = balanceThreshold.toString(16).padStart(4, '0').toUpperCase()[2].charCodeAt(0);\n var balanceThreshold_4th = balanceThreshold.toString(16).padStart(4, '0').toUpperCase()[3].charCodeAt(0);\n if (balanceThreshold > 65535 || balanceThreshold < 1) {\n return {\n errors: ['balance threshold range 1-65535.'],\n };\n } else {\n return {\n // LoRaWAN FPort used for the downlink message\n fPort: 3,\n // Encoded bytes\n bytes: [0x38, 0x30, 0x30, 0x32, 0x39, 0x39, 0x39, 0x39, 0x30, 0x32, balanceThreshold_1st, balanceThreshold_2nd, balanceThreshold_3rd, balanceThreshold_4th, 0x38, 0x31],\n };\n }\n }\n if (input.data.inThreshold !== null && !isNaN(input.data.inThreshold)) {\n var inThreshold = input.data.inThreshold;\n var inThreshold_1st = inThreshold.toString(16).padStart(4, '0').toUpperCase()[0].charCodeAt(0);\n var inThreshold_2nd = inThreshold.toString(16).padStart(4, '0').toUpperCase()[1].charCodeAt(0);\n var inThreshold_3rd = inThreshold.toString(16).padStart(4, '0').toUpperCase()[2].charCodeAt(0);\n var inThreshold_4th = inThreshold.toString(16).padStart(4, '0').toUpperCase()[3].charCodeAt(0);\n if (inThreshold > 65535 || inThreshold < 1) {\n return {\n errors: ['In threshold range 1-65535.'],\n };\n } else {\n return {\n // LoRaWAN FPort used for the downlink message\n fPort: 3,\n // Encoded bytes\n bytes: [0x38, 0x30, 0x30, 0x32, 0x39, 0x39, 0x39, 0x39, 0x30, 0x33, inThreshold_1st, inThreshold_2nd, inThreshold_3rd, inThreshold_4th, 0x38, 0x31],\n };\n }\n }\n if (input.data.outThreshold !== null && !isNaN(input.data.outThreshold)) {\n var outThreshold = input.data.outThreshold;\n var outThreshold_1st = outThreshold.toString(16).padStart(4, '0').toUpperCase()[0].charCodeAt(0);\n var outThreshold_2nd = outThreshold.toString(16).padStart(4, '0').toUpperCase()[1].charCodeAt(0);\n var outThreshold_3rd = outThreshold.toString(16).padStart(4, '0').toUpperCase()[2].charCodeAt(0);\n var outThreshold_4th = outThreshold.toString(16).padStart(4, '0').toUpperCase()[3].charCodeAt(0);\n if (outThreshold > 65535 || outThreshold < 1) {\n return {\n errors: ['In threshold range 1-65535.'],\n };\n } else {\n return {\n // LoRaWAN FPort used for the downlink message\n fPort: 3,\n // Encoded bytes\n bytes: [0x38, 0x30, 0x30, 0x32, 0x39, 0x39, 0x39, 0x39, 0x30, 0x34, outThreshold_1st, outThreshold_2nd, outThreshold_3rd, outThreshold_4th, 0x38, 0x31],\n };\n }\n }\n if (input.data.batteryThreshold !== null && !isNaN(input.data.batteryThreshold)) {\n var batteryThreshold = input.data.batteryThreshold;\n var batteryThreshold_high = batteryThreshold.toString(16).padStart(2, '0').toUpperCase()[0].charCodeAt(0);\n var batteryThreshold_low = batteryThreshold.toString(16).padStart(2, '0').toUpperCase()[1].charCodeAt(0);\n if (batteryThreshold > 99 || batteryThreshold < 5) {\n return {\n errors: ['battery alarm threshold range 5-99.'],\n };\n } else {\n return {\n // LoRaWAN FPort used for the downlink message\n fPort: 3,\n // Encoded bytes\n bytes: [0x38, 0x30, 0x30, 0x32, 0x39, 0x39, 0x39, 0x39, 0x30, 0x35, batteryThreshold_high, batteryThreshold_low, 0x38, 0x31],\n };\n }\n }\n\n\n return {\n errors: ['invalid downlink parameter.'],\n };\n}\n\nfunction decodeDownlink(input) {\n var input_length = input.bytes.length;\n if (input.fPort != 3) {\n return {\n errors: ['invalid FPort.'],\n };\n }\n\n if (\n input_length < 12 ||\n input.bytes[0] != 0x38 ||\n input.bytes[1] != 0x30 ||\n input.bytes[2] != 0x30 ||\n input.bytes[3] != 0x32 ||\n input.bytes[4] != 0x39 ||\n input.bytes[5] != 0x39 ||\n input.bytes[6] != 0x39 ||\n input.bytes[7] != 0x39 ||\n input.bytes[input_length - 2] != 0x38 ||\n input.bytes[input_length - 1] != 0x31\n ) {\n return {\n errors: ['invalid format.'],\n };\n }\n var option = parseInt(String.fromCharCode(input.bytes[8]) + String.fromCharCode(input.bytes[9]), 16);\n if (input_length == 16)\n var value = parseInt(String.fromCharCode(input.bytes[10]) + String.fromCharCode(input.bytes[11]) + String.fromCharCode(input.bytes[12]) + String.fromCharCode(input.bytes[13]), 16);\n else\n var value = parseInt(String.fromCharCode(input.bytes[10]) + String.fromCharCode(input.bytes[11]), 16);\n switch (option) {\n case 1:\n return {\n data: {\n uploadInterval: value,\n },\n };\n case 2:\n return {\n data: {\n balanceThreshold: value,\n },\n };\n case 3:\n return {\n data: {\n inThreshold: value,\n },\n };\n case 4:\n return {\n data: {\n outThreshold: value,\n },\n };\n case 5:\n return {\n data: {\n batteryThreshold: value,\n },\n };\n case 10:\n return {\n data: {\n originalBalance: value,\n },\n };\n\n default:\n return {\n errors: ['invalid parameter key.'],\n };\n }\n}", + "environment": "javascript", + "storage": "", + "version": "1.0" + }, + "flows": { + "dingtek_dc520_decoder": { + "data": { + "payload": "{{payload}}", + "payload_function": "decodeThingerUplink", + "payload_type": "source_payload", + "resource": "uplink", + "source": "resource", + "update": "events" + }, + "enabled": true, + "sink": { + "payload": "{{payload}}", + "payload_function": "", + "payload_type": "source_payload", + "resource_stream": "uplink_decoded", + "target": "resource_stream" + }, + "split_data": false + } + }, + "properties": { + "uplink": { + "data": { + "payload": "{{payload}}", + "payload_function": "", + "payload_type": "source_payload", + "resource": "uplink", + "source": "resource", + "update": "events" + }, + "default": { + "source": "value" + }, + "enabled": true + } + } + }, + "_resources": { + "properties": [ + { + "property": "dashboard", + "value": { + "tabs": [ + { + "name": "Overview", + "widgets": [ + { + "layout": { + "col": 0, + "row": 0, + "sizeX": 2, + "sizeY": 6 + }, + "panel": { + "color": "#ffffff", + "currentColor": "#ffffff", + "showOffline": { + "type": "none" + }, + "title": "Balance Counter (Net Occupancy)" + }, + "properties": { + "color": "#3498db", + "max": 1000, + "min": 0, + "unit": "people" + }, + "sources": [ + { + "bucket": { + "backend": "mongodb", + "id": "dingtek_dc520_data", + "mapping": "balanceCounter", + "tags": { + "device": [], + "group": [] + } + }, + "color": "#3498db", + "name": "Balance", + "source": "bucket", + "timespan": { + "mode": "latest" + } + } + ], + "type": "donutchart" + }, + { + "layout": { + "col": 2, + "row": 0, + "sizeX": 1, + "sizeY": 6 + }, + "panel": { + "color": "#ffffff", + "currentColor": "#ffffff", + "showOffline": { + "type": "none" + }, + "title": "IN Counter" + }, + "properties": { + "color": "#2ecc71", + "max": 1000, + "min": 0, + "unit": "people" + }, + "sources": [ + { + "bucket": { + "backend": "mongodb", + "id": "dingtek_dc520_data", + "mapping": "inCounter", + "tags": { + "device": [], + "group": [] + } + }, + "color": "#2ecc71", + "name": "In Counter", + "source": "bucket", + "timespan": { + "mode": "latest" + } + } + ], + "type": "donutchart" + }, + { + "layout": { + "col": 3, + "row": 0, + "sizeX": 1, + "sizeY": 6 + }, + "panel": { + "color": "#ffffff", + "currentColor": "#ffffff", + "showOffline": { + "type": "none" + }, + "title": "OUT Counter" + }, + "properties": { + "color": "#e74c3c", + "max": 1000, + "min": 0, + "unit": "people" + }, + "sources": [ + { + "bucket": { + "backend": "mongodb", + "id": "dingtek_dc520_data", + "mapping": "outCounter", + "tags": { + "device": [], + "group": [] + } + }, + "color": "#e74c3c", + "name": "Out Counter", + "source": "bucket", + "timespan": { + "mode": "latest" + } + } + ], + "type": "donutchart" + }, + { + "layout": { + "col": 4, + "row": 0, + "sizeX": 2, + "sizeY": 6 + }, + "panel": { + "color": "#ffffff", + "currentColor": "#ffffff", + "showOffline": { + "type": "none" + }, + "title": "Device Status" + }, + "properties": { + "source": "code", + "template": "
\n
\n
\n
{{ (value && value[0] && value[0].alarmBalance) ? '🚨' : '✅' }}
\n
Balance
\n
{{ (value && value[0] && value[0].alarmBalance) ? 'ALARM' : 'OK' }}
\n
\n
\n
{{ (value && value[0] && value[0].alarmIn) ? 'âš ī¸' : '✅' }}
\n
IN Alarm
\n
{{ (value && value[0] && value[0].alarmIn) ? 'ALARM' : 'OK' }}
\n
\n
\n
{{ (value && value[0] && value[0].alarmOut) ? 'âš ī¸' : '✅' }}
\n
OUT Alarm
\n
{{ (value && value[0] && value[0].alarmOut) ? 'ALARM' : 'OK' }}
\n
\n
\n
{{ (value && value[0] && value[0].alarmBattery) ? 'đŸĒĢ' : '🔋' }}
\n
Battery
\n
{{ (value && value[0] && value[0].alarmBattery) ? 'Low' : 'OK' }}
\n
\n
\n
Last update: {{ (value && value[0] && value[0].ts) | date:'medium' }}
\n
\n" + }, + "sources": [ + { + "bucket": { + "backend": "mongodb", + "id": "dingtek_dc520_data", + "mapping": "alarmBalance", + "tags": { + "device": [], + "group": [] + } + }, + "color": "#e74c3c", + "name": "alarmBalance", + "source": "bucket", + "timespan": { + "mode": "latest" + } + }, + { + "bucket": { + "backend": "mongodb", + "id": "dingtek_dc520_data", + "mapping": "alarmIn", + "tags": { + "device": [], + "group": [] + } + }, + "color": "#e67e22", + "name": "alarmIn", + "source": "bucket", + "timespan": { + "mode": "latest" + } + }, + { + "bucket": { + "backend": "mongodb", + "id": "dingtek_dc520_data", + "mapping": "alarmOut", + "tags": { + "device": [], + "group": [] + } + }, + "color": "#9b59b6", + "name": "alarmOut", + "source": "bucket", + "timespan": { + "mode": "latest" + } + }, + { + "bucket": { + "backend": "mongodb", + "id": "dingtek_dc520_data", + "mapping": "alarmBattery", + "tags": { + "device": [], + "group": [] + } + }, + "color": "#f39c12", + "name": "alarmBattery", + "source": "bucket", + "timespan": { + "mode": "latest" + } + }, + { + "bucket": { + "backend": "mongodb", + "id": "dingtek_dc520_data", + "mapping": "ts", + "tags": { + "device": [], + "group": [] + } + }, + "color": "#95a5a6", + "name": "ts", + "source": "bucket", + "timespan": { + "mode": "latest" + } + } + ], + "type": "html_time" + }, + { + "layout": { + "col": 0, + "row": 6, + "sizeX": 4, + "sizeY": 8 + }, + "panel": { + "color": "#ffffff", + "currentColor": "#ffffff", + "showOffline": { + "type": "none" + }, + "title": "IN / OUT / Balance History" + }, + "properties": { + "axis": true, + "fill": false, + "legend": true, + "multiple_axes": false + }, + "sources": [ + { + "bucket": { + "backend": "mongodb", + "id": "dingtek_dc520_data", + "mapping": "balanceCounter", + "tags": { + "device": [], + "group": [] + } + }, + "color": "#3498db", + "name": "Balance (Net)", + "source": "bucket", + "timespan": { + "magnitude": "hour", + "mode": "relative", + "period": "latest", + "value": 24 + } + }, + { + "bucket": { + "backend": "mongodb", + "id": "dingtek_dc520_data", + "mapping": "inCounter", + "tags": { + "device": [], + "group": [] + } + }, + "color": "#2ecc71", + "name": "IN", + "source": "bucket", + "timespan": { + "magnitude": "hour", + "mode": "relative", + "period": "latest", + "value": 24 + } + }, + { + "bucket": { + "backend": "mongodb", + "id": "dingtek_dc520_data", + "mapping": "outCounter", + "tags": { + "device": [], + "group": [] + } + }, + "color": "#e74c3c", + "name": "OUT", + "source": "bucket", + "timespan": { + "magnitude": "hour", + "mode": "relative", + "period": "latest", + "value": 24 + } + } + ], + "type": "chart" + }, + { + "layout": { + "col": 4, + "row": 6, + "sizeX": 2, + "sizeY": 8 + }, + "panel": { + "color": "#ffffff", + "currentColor": "#ffffff", + "showOffline": { + "type": "none" + }, + "title": "Battery Voltage History" + }, + "properties": { + "axis": true, + "fill": false, + "legend": true, + "multiple_axes": false + }, + "sources": [ + { + "bucket": { + "backend": "mongodb", + "id": "dingtek_dc520_data", + "mapping": "volt", + "tags": { + "device": [], + "group": [] + } + }, + "color": "#f1c40f", + "name": "Voltage (mV)", + "source": "bucket", + "timespan": { + "magnitude": "hour", + "mode": "relative", + "period": "latest", + "value": 24 + } + } + ], + "type": "chart" + }, + { + "layout": { + "col": 0, + "row": 14, + "sizeX": 6, + "sizeY": 8 + }, + "panel": { + "color": "#ffffff", + "currentColor": "#ffffff", + "showOffline": { + "type": "none" + }, + "title": "Last 24h Records" + }, + "properties": { + "source": "code", + "template": "
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
DateBalanceINOUTVolt (mV)Balance AlarmIN AlarmOUT AlarmBatteryErrorFrame #
{{ entry.ts | date:'dd/MM/yy HH:mm' }}{{ entry.balanceCounter !== undefined ? entry.balanceCounter : '—' }}{{ entry.inCounter !== undefined ? entry.inCounter : '—' }}{{ entry.outCounter !== undefined ? entry.outCounter : '—' }}{{ entry.volt !== undefined ? entry.volt : '—' }}{{ entry.alarmBalance ? '🚨 YES' : '✅ NO' }}{{ entry.alarmIn ? 'âš ī¸ YES' : '✅ NO' }}{{ entry.alarmOut ? 'âš ī¸ YES' : '✅ NO' }}{{ entry.alarmBattery ? 'đŸĒĢ Low' : '🔋 OK' }}{{ entry.errorCode !== undefined ? entry.errorCode : '—' }}{{ entry.frameCounter !== undefined ? entry.frameCounter : '—' }}
\n
\n" + }, + "sources": [ + { + "aggregation": {}, + "bucket": { + "backend": "mongodb", + "id": "dingtek_dc520_data", + "mapping": "ts", + "tags": { + "device": [], + "group": [] + } + }, + "color": "#95a5a6", + "name": "ts", + "source": "bucket", + "timespan": { + "magnitude": "hour", + "mode": "relative", + "period": "latest", + "value": 24 + } + }, + { + "aggregation": {}, + "bucket": { + "backend": "mongodb", + "id": "dingtek_dc520_data", + "mapping": "balanceCounter", + "tags": { + "device": [], + "group": [] + } + }, + "color": "#3498db", + "name": "balanceCounter", + "source": "bucket", + "timespan": { + "magnitude": "hour", + "mode": "relative", + "period": "latest", + "value": 24 + } + }, + { + "aggregation": {}, + "bucket": { + "backend": "mongodb", + "id": "dingtek_dc520_data", + "mapping": "inCounter", + "tags": { + "device": [], + "group": [] + } + }, + "color": "#2ecc71", + "name": "inCounter", + "source": "bucket", + "timespan": { + "magnitude": "hour", + "mode": "relative", + "period": "latest", + "value": 24 + } + }, + { + "aggregation": {}, + "bucket": { + "backend": "mongodb", + "id": "dingtek_dc520_data", + "mapping": "outCounter", + "tags": { + "device": [], + "group": [] + } + }, + "color": "#e74c3c", + "name": "outCounter", + "source": "bucket", + "timespan": { + "magnitude": "hour", + "mode": "relative", + "period": "latest", + "value": 24 + } + }, + { + "aggregation": {}, + "bucket": { + "backend": "mongodb", + "id": "dingtek_dc520_data", + "mapping": "volt", + "tags": { + "device": [], + "group": [] + } + }, + "color": "#f1c40f", + "name": "volt", + "source": "bucket", + "timespan": { + "magnitude": "hour", + "mode": "relative", + "period": "latest", + "value": 24 + } + }, + { + "aggregation": {}, + "bucket": { + "backend": "mongodb", + "id": "dingtek_dc520_data", + "mapping": "alarmBalance", + "tags": { + "device": [], + "group": [] + } + }, + "color": "#e74c3c", + "name": "alarmBalance", + "source": "bucket", + "timespan": { + "magnitude": "hour", + "mode": "relative", + "period": "latest", + "value": 24 + } + }, + { + "aggregation": {}, + "bucket": { + "backend": "mongodb", + "id": "dingtek_dc520_data", + "mapping": "alarmIn", + "tags": { + "device": [], + "group": [] + } + }, + "color": "#e67e22", + "name": "alarmIn", + "source": "bucket", + "timespan": { + "magnitude": "hour", + "mode": "relative", + "period": "latest", + "value": 24 + } + }, + { + "aggregation": {}, + "bucket": { + "backend": "mongodb", + "id": "dingtek_dc520_data", + "mapping": "alarmOut", + "tags": { + "device": [], + "group": [] + } + }, + "color": "#9b59b6", + "name": "alarmOut", + "source": "bucket", + "timespan": { + "magnitude": "hour", + "mode": "relative", + "period": "latest", + "value": 24 + } + }, + { + "aggregation": {}, + "bucket": { + "backend": "mongodb", + "id": "dingtek_dc520_data", + "mapping": "alarmBattery", + "tags": { + "device": [], + "group": [] + } + }, + "color": "#f39c12", + "name": "alarmBattery", + "source": "bucket", + "timespan": { + "magnitude": "hour", + "mode": "relative", + "period": "latest", + "value": 24 + } + }, + { + "aggregation": {}, + "bucket": { + "backend": "mongodb", + "id": "dingtek_dc520_data", + "mapping": "errorCode", + "tags": { + "device": [], + "group": [] + } + }, + "color": "#e74c3c", + "name": "errorCode", + "source": "bucket", + "timespan": { + "magnitude": "hour", + "mode": "relative", + "period": "latest", + "value": 24 + } + }, + { + "aggregation": {}, + "bucket": { + "backend": "mongodb", + "id": "dingtek_dc520_data", + "mapping": "frameCounter", + "tags": { + "device": [], + "group": [] + } + }, + "color": "#1abc9c", + "name": "frameCounter", + "source": "bucket", + "timespan": { + "magnitude": "hour", + "mode": "relative", + "period": "latest", + "value": 24 + } + } + ], + "type": "html_time" + } + ] + } + ] + } + } + ] } - } - } - }, - "autoprovisions": { - "device_autoprovisioning": { - "config": { - "mode": "pattern", - "pattern": "dc520_.*" - }, - "enabled": true - } - }, - "buckets": { - "dingtek_dc520_data": { - "backend": "mongodb", - "data": { - "payload": "{{payload}}", - "payload_function": "", - "payload_type": "source_payload", - "resource_stream": "uplink_decoded", - "source": "resource_stream" - }, - "enabled": true, - "retention": { - "period": 3, - "unit": "months" - }, - "tags": [] - } - }, - "code": { - "code": "function decodeThingerUplink(thingerData) {\n // 0. If data has already been decoded, we will return it\n if (thingerData.decodedPayload) return thingerData.decodedPayload;\n \n // 1. Extract and Validate Input\n // We need 'payload' (hex string) and 'fPort' (integer)\n const hexPayload = thingerData.payload || \"\";\n const port = thingerData.fPort || 1;\n\n // 2. Convert Hex String to Byte Array\n const bytes = [];\n for (let i = 0; i < hexPayload.length; i += 2) {\n bytes.push(parseInt(hexPayload.substr(i, 2), 16));\n }\n\n // 3. Dynamic Function Detection and Execution\n \n // CASE A: (The Things Stack v3)\n if (typeof decodeUplink === 'function') {\n try {\n const input = {\n bytes: bytes,\n fPort: port\n };\n var result = decodeUplink(input);\n \n if (result.data) return result.data;\n\n return result; \n } catch (e) {\n console.error(\"Error inside decodeUplink:\", e);\n throw e;\n }\n }\n\n // CASE B: Legacy TTN (v2)\n else if (typeof Decoder === 'function') {\n try {\n return Decoder(bytes, port);\n } catch (e) {\n console.error(\"Error inside Decoder:\", e);\n throw e;\n }\n }\n\n // CASE C: No decoder found\n else {\n throw new Error(\"No compatible TTN decoder function (decodeUplink or Decoder) found in scope.\");\n }\n}\n\n\n// TTN decoder\n//IEEE754 hex to float convert\nfunction hex2float(num) {\n var sign = num & 0x80000000 ? -1 : 1;\n var exponent = ((num >> 23) & 0xff) - 127;\n var mantissa = 1 + (num & 0x7fffff) / 0x7fffff;\n return sign * mantissa * Math.pow(2, exponent);\n}\n\nfunction decodeUplink(input) {\n if (input.fPort != 3) {\n return {\n errors: ['unknown FPort'],\n };\n }\n switch (input.bytes.length) {\n case 19:\n return {\n // Decoded data\n data: {\n balanceCounter: ((input.bytes[5] << 8) + input.bytes[6]),\n inCounter: ((input.bytes[7] << 8) + input.bytes[8]),\n outCounter: ((input.bytes[9] << 8) + input.bytes[10]),\n alarmBalance: input.bytes[11] >> 4,\n alarmIn: input.bytes[11] & 0x0F,\n alarmOut: input.bytes[12] >> 4,\n alarmBattery: input.bytes[12] & 0x0F,\n volt: ((input.bytes[13] << 8) + input.bytes[14]),\n errorCode: input.bytes[15],\n frameCounter: (input.bytes[16] << 8) + input.bytes[17],\n },\n };\n case 18:\n return {\n // Decoded data\n data: {\n firmware: input.bytes[5] + \".\" + input.bytes[6],\n uploadInterval: input.bytes[7],\n originalBalance: (input.bytes[8] << 8) + input.bytes[9],\n balanceThreshold: (input.bytes[10] << 8) + input.bytes[11],\n inThreshold: (input.bytes[12] << 8) + input.bytes[13],\n outThreshold: (input.bytes[14] << 8) + input.bytes[15],\n batteryThreshold: input.bytes[16]\n\n },\n };\n default:\n return {\n errors: ['wrong length'],\n };\n }\n}\n\n// Encode downlink function.\n//\n// Input is an object with the following fields:\n// - data = Object representing the payload that must be encoded.\n// - variables = Object containing the configured device variables.\n//\n// Output must be an object with the following fields:\n// - bytes = Byte array containing the downlink payload.\nfunction encodeDownlink(input) {\n if (input.data.uploadInterval !== null && !isNaN(input.data.uploadInterval)) {\n var uploadInterval = input.data.uploadInterval;\n var uploadInterval_high = uploadInterval.toString(16).padStart(2, '0').toUpperCase()[0].charCodeAt(0);\n var uploadInterval_low = uploadInterval.toString(16).padStart(2, '0').toUpperCase()[1].charCodeAt(0);\n if (uploadInterval > 168 || uploadInterval < 1) {\n return {\n errors: ['upload interval range 1-168 hours.'],\n };\n } else {\n return {\n // LoRaWAN FPort used for the downlink message\n fPort: 3,\n // Encoded bytes\n bytes: [0x38, 0x30, 0x30, 0x32, 0x39, 0x39, 0x39, 0x39, 0x30, 0x31, uploadInterval_high, uploadInterval_low, 0x38, 0x31],\n };\n }\n }\n if (input.data.originalBalance !== null && !isNaN(input.data.originalBalance)) {\n var originalBalance = input.data.originalBalance;\n var originalBalance_1st = originalBalance.toString(16).padStart(4, '0').toUpperCase()[0].charCodeAt(0);\n var originalBalance_2nd = originalBalance.toString(16).padStart(4, '0').toUpperCase()[1].charCodeAt(0);\n var originalBalance_3rd = originalBalance.toString(16).padStart(4, '0').toUpperCase()[2].charCodeAt(0);\n var originalBalance_4th = originalBalance.toString(16).padStart(4, '0').toUpperCase()[3].charCodeAt(0);\n if (originalBalance > 65535 || originalBalance < 1) {\n return {\n errors: ['original balance range 1-65535.'],\n };\n } else {\n return {\n // LoRaWAN FPort used for the downlink message\n fPort: 3,\n // Encoded bytes\n bytes: [0x38, 0x30, 0x30, 0x32, 0x39, 0x39, 0x39, 0x39, 0x30, 0x41, originalBalance_1st, originalBalance_2nd, originalBalance_3rd, originalBalance_4th, 0x38, 0x31],\n };\n }\n }\n if (input.data.balanceThreshold !== null && !isNaN(input.data.balanceThreshold)) {\n var balanceThreshold = input.data.balanceThreshold;\n var balanceThreshold_1st = balanceThreshold.toString(16).padStart(4, '0').toUpperCase()[0].charCodeAt(0);\n var balanceThreshold_2nd = balanceThreshold.toString(16).padStart(4, '0').toUpperCase()[1].charCodeAt(0);\n var balanceThreshold_3rd = balanceThreshold.toString(16).padStart(4, '0').toUpperCase()[2].charCodeAt(0);\n var balanceThreshold_4th = balanceThreshold.toString(16).padStart(4, '0').toUpperCase()[3].charCodeAt(0);\n if (balanceThreshold > 65535 || balanceThreshold < 1) {\n return {\n errors: ['balance threshold range 1-65535.'],\n };\n } else {\n return {\n // LoRaWAN FPort used for the downlink message\n fPort: 3,\n // Encoded bytes\n bytes: [0x38, 0x30, 0x30, 0x32, 0x39, 0x39, 0x39, 0x39, 0x30, 0x32, balanceThreshold_1st, balanceThreshold_2nd, balanceThreshold_3rd, balanceThreshold_4th, 0x38, 0x31],\n };\n }\n }\n if (input.data.inThreshold !== null && !isNaN(input.data.inThreshold)) {\n var inThreshold = input.data.inThreshold;\n var inThreshold_1st = inThreshold.toString(16).padStart(4, '0').toUpperCase()[0].charCodeAt(0);\n var inThreshold_2nd = inThreshold.toString(16).padStart(4, '0').toUpperCase()[1].charCodeAt(0);\n var inThreshold_3rd = inThreshold.toString(16).padStart(4, '0').toUpperCase()[2].charCodeAt(0);\n var inThreshold_4th = inThreshold.toString(16).padStart(4, '0').toUpperCase()[3].charCodeAt(0);\n if (inThreshold > 65535 || inThreshold < 1) {\n return {\n errors: ['In threshold range 1-65535.'],\n };\n } else {\n return {\n // LoRaWAN FPort used for the downlink message\n fPort: 3,\n // Encoded bytes\n bytes: [0x38, 0x30, 0x30, 0x32, 0x39, 0x39, 0x39, 0x39, 0x30, 0x33, inThreshold_1st, inThreshold_2nd, inThreshold_3rd, inThreshold_4th, 0x38, 0x31],\n };\n }\n }\n if (input.data.outThreshold !== null && !isNaN(input.data.outThreshold)) {\n var outThreshold = input.data.outThreshold;\n var outThreshold_1st = outThreshold.toString(16).padStart(4, '0').toUpperCase()[0].charCodeAt(0);\n var outThreshold_2nd = outThreshold.toString(16).padStart(4, '0').toUpperCase()[1].charCodeAt(0);\n var outThreshold_3rd = outThreshold.toString(16).padStart(4, '0').toUpperCase()[2].charCodeAt(0);\n var outThreshold_4th = outThreshold.toString(16).padStart(4, '0').toUpperCase()[3].charCodeAt(0);\n if (outThreshold > 65535 || outThreshold < 1) {\n return {\n errors: ['In threshold range 1-65535.'],\n };\n } else {\n return {\n // LoRaWAN FPort used for the downlink message\n fPort: 3,\n // Encoded bytes\n bytes: [0x38, 0x30, 0x30, 0x32, 0x39, 0x39, 0x39, 0x39, 0x30, 0x34, outThreshold_1st, outThreshold_2nd, outThreshold_3rd, outThreshold_4th, 0x38, 0x31],\n };\n }\n }\n if (input.data.batteryThreshold !== null && !isNaN(input.data.batteryThreshold)) {\n var batteryThreshold = input.data.batteryThreshold;\n var batteryThreshold_high = batteryThreshold.toString(16).padStart(2, '0').toUpperCase()[0].charCodeAt(0);\n var batteryThreshold_low = batteryThreshold.toString(16).padStart(2, '0').toUpperCase()[1].charCodeAt(0);\n if (batteryThreshold > 99 || batteryThreshold < 5) {\n return {\n errors: ['battery alarm threshold range 5-99.'],\n };\n } else {\n return {\n // LoRaWAN FPort used for the downlink message\n fPort: 3,\n // Encoded bytes\n bytes: [0x38, 0x30, 0x30, 0x32, 0x39, 0x39, 0x39, 0x39, 0x30, 0x35, batteryThreshold_high, batteryThreshold_low, 0x38, 0x31],\n };\n }\n }\n\n\n return {\n errors: ['invalid downlink parameter.'],\n };\n}\n\nfunction decodeDownlink(input) {\n var input_length = input.bytes.length;\n if (input.fPort != 3) {\n return {\n errors: ['invalid FPort.'],\n };\n }\n\n if (\n input_length < 12 ||\n input.bytes[0] != 0x38 ||\n input.bytes[1] != 0x30 ||\n input.bytes[2] != 0x30 ||\n input.bytes[3] != 0x32 ||\n input.bytes[4] != 0x39 ||\n input.bytes[5] != 0x39 ||\n input.bytes[6] != 0x39 ||\n input.bytes[7] != 0x39 ||\n input.bytes[input_length - 2] != 0x38 ||\n input.bytes[input_length - 1] != 0x31\n ) {\n return {\n errors: ['invalid format.'],\n };\n }\n var option = parseInt(String.fromCharCode(input.bytes[8]) + String.fromCharCode(input.bytes[9]), 16);\n if (input_length == 16)\n var value = parseInt(String.fromCharCode(input.bytes[10]) + String.fromCharCode(input.bytes[11]) + String.fromCharCode(input.bytes[12]) + String.fromCharCode(input.bytes[13]), 16);\n else\n var value = parseInt(String.fromCharCode(input.bytes[10]) + String.fromCharCode(input.bytes[11]), 16);\n switch (option) {\n case 1:\n return {\n data: {\n uploadInterval: value,\n },\n };\n case 2:\n return {\n data: {\n balanceThreshold: value,\n },\n };\n case 3:\n return {\n data: {\n inThreshold: value,\n },\n };\n case 4:\n return {\n data: {\n outThreshold: value,\n },\n };\n case 5:\n return {\n data: {\n batteryThreshold: value,\n },\n };\n case 10:\n return {\n data: {\n originalBalance: value,\n },\n };\n\n default:\n return {\n errors: ['invalid parameter key.'],\n };\n }\n}", - "environment": "javascript", - "storage": "", - "version": "1.0" - }, - "flows": { - "dingtek_dc520_decoder": { - "data": { - "payload": "{{payload}}", - "payload_function": "decodeThingerUplink", - "payload_type": "source_payload", - "resource": "uplink", - "source": "resource", - "update": "events" - }, - "enabled": true, - "sink": { - "payload": "{{payload}}", - "payload_function": "", - "payload_type": "source_payload", - "resource_stream": "uplink_decoded", - "target": "resource_stream" - }, - "split_data": false - } - }, - "properties": { - "uplink": { - "data": { - "payload": "{{payload}}", - "payload_function": "", - "payload_type": "source_payload", - "resource": "uplink", - "source": "resource", - "update": "events" - }, - "default": { - "source": "value" - }, - "enabled": true } - } - }, - "_resources": { - "properties": [] - } - } - ] - } -} \ No newline at end of file + ] + } +}