Plugin file
Plugin configuration file
{
"name": "milesight_iot_uc1152",
"version": "1.0.0",
"description": "The Milesight UC1152 is a professional LoRaWAN serial controller designed to bridge the gap between legacy industrial assets and the IoT ecosystem.",
"author": "Thinger.io",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/thinger-io/plugins.git",
"directory": "milesight-iot-uc1152"
},
"metadata": {
"name": "Milesight-Iot UC1152",
"description": "The Milesight UC1152 is a professional LoRaWAN serial controller designed to bridge the gap between legacy industrial assets and the IoT ecosystem.",
"image": "assets/uc1152.png",
"category": "devices",
"vendor": "milesight-iot"
},
"resources": {
"products": [
{
"description": "The Milesight UC1152 is a professional LoRaWAN serial controller designed to bridge the gap between legacy industrial assets and the IoT ecosystem.",
"enabled": true,
"name": "Milesight-Iot UC1152",
"product": "milesight_iot_uc1152",
"profile": {
"api": {
"downlink": {
"enabled": true,
"handle_connectivity": false,
"request": {
"data": {
"path": "/downlink",
"payload": "{\r\n \"data\" : \"{{payload.data=\"\"}}\",\r\n \"port\" : {{payload.port=2}},\r\n \"priority\": {{payload.priority=3}},\r\n \"confirmed\" : {{payload.confirmed=false}},\r\n \"uplink\" : {{property.uplink}} \r\n}",
"payload_function": "",
"payload_type": "",
"plugin": "{{property.uplink.source}}",
"target": "plugin_endpoint"
}
}
},
"uplink": {
"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": "uc1152_.*"
},
"enabled": true
}
},
"buckets": {
"milesight_uc1152_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\nfunction decodeUplink(input) {\n var res = Decoder(input.bytes, input.fPort);\n if (res.error) {\n return {\n errors: [res.error],\n };\n }\n return {\n data: res,\n };\n}\n/**\n * Payload Decoder for The Things Network\n * \n * Copyright 2021 Milesight IoT\n * \n * @product UC11 series\n */\nfunction Decoder(bytes, port) {\n var decoded = {};\n\n for (i = 0; i < bytes.length;) {\n var channel_id = bytes[i++];\n var channel_type = bytes[i++];\n\n // Digital Input 1\n if (channel_id === 0x01 && channel_type !== 0xc8) {\n decoded.digital_input = bytes[i] === 0 ? \"off\" : \"on\";\n i += 1;\n }\n // Digital Output 1\n else if (channel_id === 0x09) {\n decoded.digital_output = bytes[i] === 0 ? \"off\" : \"on\";\n i += 1;\n }\n // MODBUS\n else if (channel_id === 0xFF && channel_type === 0x0E) {\n var modbus_chn_id = bytes[i++];\n var package_type = bytes[i++];\n var data_type = package_type & 7;\n var date_length = package_type >> 3;\n var chn = modbus_chn_id - 0x18;\n decoded.channels = [];\n var channel = {};\n switch (data_type) {\n case 0:\n channel['index'] = chn;\n channel['reading'] = bytes[i] ? \"on\" : \"off\";\n decoded.channels.push(channel);\n i += 1;\n break;\n case 1:\n channel['index'] = chn;\n channel['reading'] = bytes[i];\n decoded.channels.push(channel);\n i += 1;\n break;\n case 2:\n case 3:\n channel['index'] = chn;\n channel['reading'] = readUInt16LE(bytes.slice(i, i + 2));\n decoded.channels.push(channel);\n i += 2;\n break;\n case 4:\n case 6:\n channel['index'] = chn;\n channel['reading'] = readUInt32LE(bytes.slice(i, i + 4));\n decoded.channels.push(channel);\n i += 4;\n break;\n case 5:\n case 7:\n channel['index'] = chn;\n channel['reading'] = readFloatLE(bytes.slice(i, i + 4));\n decoded.channels.push(channel);\n i += 4;\n break;\n }\n }\n }\n\n return decoded;\n}\n\n/* ******************************************\n * bytes to number\n ********************************************/\nfunction readUInt8LE(bytes) {\n return (bytes & 0xFF);\n}\n\nfunction readInt8LE(bytes) {\n var ref = readUInt8LE(bytes);\n return (ref > 0x7F) ? ref - 0x100 : ref;\n}\n\nfunction readUInt16LE(bytes) {\n var value = (bytes[1] << 8) + bytes[0];\n return (value & 0xFFFF);\n}\n\nfunction readInt16LE(bytes) {\n var ref = readUInt16LE(bytes);\n return (ref > 0x7FFF) ? ref - 0x10000 : ref;\n}\n\nfunction readUInt32LE(bytes) {\n var value = (bytes[3] << 24) + (bytes[2] << 16) + (bytes[1] << 8) + bytes[0];\n return (value & 0xFFFFFFFF);\n}\n\nfunction readInt32LE(bytes) {\n var ref = readUInt32LE(bytes);\n return (ref > 0x7FFFFFFF) ? ref - 0x100000000 : ref;\n}\n\nfunction readFloatLE(bytes) {\n // JavaScript bitwise operators yield a 32 bits integer, not a float.\n // Assume LSB (least significant byte first).\n var bits = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];\n var sign = (bits >>> 31 === 0) ? 1.0 : -1.0;\n var e = bits >>> 23 & 0xff;\n var m = (e === 0) ? (bits & 0x7fffff) << 1 : (bits & 0x7fffff) | 0x800000;\n var f = sign * m * Math.pow(2, e - 150);\n return f;\n}\n",
"environment": "javascript",
"storage": "",
"version": "1.0"
},
"flows": {
"milesight_uc1152_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": "UC1152 Controller",
"widgets": [
{
"layout": {
"col": 0,
"row": 0,
"sizeX": 2,
"sizeY": 5
},
"panel": {
"color": "#ffffff",
"currentColor": "#ffffff",
"showOffline": {
"type": "none"
},
"title": "Digital Input"
},
"properties": {
"decimalPlaces": 2,
"enableExtraTextColor": false,
"enableIconColor": false,
"enableIconSize": false,
"extraText": "",
"extraTextColor": "#1E313E",
"extraTextColorConditions": [],
"extraTextConditions": [],
"extraTextPosition": "above-value",
"extraTextSize": "20px",
"extraTextWeight": "font-light",
"icon": "",
"iconColor": "#1E313E",
"iconColorConditions": [],
"iconConditions": [],
"iconGap": "8px",
"iconPosition": "before-value",
"iconSize": "75px",
"iconVerticalOffset": "0px",
"link": "",
"textAlign": "center",
"textColor": "#1E313E",
"textColorConditions": [],
"textSize": "75px",
"textWeight": "font-light",
"unit": "",
"unitSize": "20px"
},
"sources": [
{
"bucket": {
"backend": "mongodb",
"id": "milesight_uc1152_data",
"mapping": "digital_input",
"tags": {
"device": [],
"group": []
}
},
"color": "#1abc9c",
"name": "Digital Input",
"source": "bucket",
"timespan": {
"mode": "latest"
}
}
],
"type": "text"
},
{
"layout": {
"col": 2,
"row": 0,
"sizeX": 2,
"sizeY": 5
},
"panel": {
"color": "#ffffff",
"currentColor": "#ffffff",
"showOffline": {
"type": "none"
},
"title": "Digital Output"
},
"properties": {
"decimalPlaces": 2,
"enableExtraTextColor": false,
"enableIconColor": false,
"enableIconSize": false,
"extraText": "",
"extraTextColor": "#1E313E",
"extraTextColorConditions": [],
"extraTextConditions": [],
"extraTextPosition": "above-value",
"extraTextSize": "20px",
"extraTextWeight": "font-light",
"icon": "",
"iconColor": "#1E313E",
"iconColorConditions": [],
"iconConditions": [],
"iconGap": "8px",
"iconPosition": "before-value",
"iconSize": "75px",
"iconVerticalOffset": "0px",
"link": "",
"textAlign": "center",
"textColor": "#1E313E",
"textColorConditions": [],
"textSize": "75px",
"textWeight": "font-light",
"unit": "",
"unitSize": "20px"
},
"sources": [
{
"bucket": {
"backend": "mongodb",
"id": "milesight_uc1152_data",
"mapping": "digital_output",
"tags": {
"device": [],
"group": []
}
},
"color": "#e74c3c",
"name": "Digital Output",
"source": "bucket",
"timespan": {
"mode": "latest"
}
}
],
"type": "text"
},
{
"layout": {
"col": 0,
"row": 5,
"sizeX": 4,
"sizeY": 10
},
"panel": {
"color": "#ffffff",
"currentColor": "#ffffff",
"showOffline": {
"type": "none"
},
"title": "Modbus Channel Readings (24h)"
},
"properties": {
"alignTimeSeries": false,
"dataAppend": false,
"options": "var options = {\n chart: {\n type: 'line',\n stacked: false\n },\n dataLabels: {\n enabled: false\n },\n stroke: {\n curve: 'smooth',\n width: 2\n },\n xaxis: {\n type: 'datetime',\n labels: {\n datetimeUTC: false\n },\n tooltip: {\n enabled: false\n }\n },\n yaxis: {\n labels: {\n formatter: function (val) {\n if ( val !== null && typeof val !== 'undefined' )\n return val.toFixed(2);\n }\n }\n },\n tooltip: {\n x: {\n format: 'dd/MM/yyyy HH:mm:ss'\n }\n }\n};\n",
"realTimeUpdate": true
},
"sources": [
{
"aggregation": {},
"bucket": {
"backend": "mongodb",
"id": "milesight_uc1152_data",
"mapping": "channels",
"tags": {
"device": [],
"group": []
}
},
"color": "#3498db",
"name": "Channel Readings",
"source": "bucket",
"timespan": {
"magnitude": "hour",
"mode": "relative",
"period": "latest",
"value": 24
}
}
],
"type": "apex_charts"
},
{
"layout": {
"col": 4,
"row": 0,
"sizeX": 2,
"sizeY": 15
},
"panel": {
"color": "#ffffff",
"currentColor": "#ffffff",
"showOffline": {
"type": "none"
},
"title": "Latest Modbus Readings"
},
"properties": {
"source": "code",
"template": "<div style=\"width:100%; height:100%; overflow-y:auto; padding: 10px;\">\n <table class=\"table table-striped table-condensed\">\n <thead>\n <tr>\n <th>Channel</th>\n <th>Reading</th>\n </tr>\n </thead>\n <tbody>\n <tr ng-repeat=\"channel in value[0].channels track by $index\" ng-if=\"value[0].channels\">\n <td>{{ channel.index }}</td>\n <td>{{ channel.reading }}</td>\n </tr>\n <tr ng-if=\"!value[0].channels || value[0].channels.length === 0\">\n <td colspan=\"2\" style=\"text-align:center; color:#999;\">No channel data available</td>\n </tr>\n </tbody>\n </table>\n</div>"
},
"sources": [
{
"bucket": {
"backend": "mongodb",
"id": "milesight_uc1152_data",
"mapping": "channels",
"tags": {
"device": [],
"group": []
}
},
"color": "#1abc9c",
"name": "Latest Channels",
"source": "bucket",
"timespan": {
"mode": "latest"
}
}
],
"type": "html_time"
}
]
}
]
}
}
]
}
}
]
}
}