Skip to content

Plugin file

Plugin configuration file
{
  "name": "dragino_lds03a",
  "version": "1.0.0",
  "description": "The Dragino LDS03A is an outdoor-ready LoRaWAN Door/Window Sensor designed to detect open/close status and count events.",
  "author": "Thinger.io",
  "license": "MIT",
  "repository": {
    "type": "git",
    "url": "https://github.com/thinger-io/plugins.git",
    "directory": "dragino-lds03a"
  },
  "metadata": {
    "name": "Dragino LDS03A",
    "description": "The Dragino LDS03A is an outdoor-ready LoRaWAN Door/Window Sensor designed to detect open/close status and count events.",
    "image": "assets/lds03a.png",
    "category": "devices",
    "vendor": "dragino"
  },
  "resources": {
    "products": [
      {
        "description": "The Dragino LDS03A is an outdoor-ready LoRaWAN Door/Window Sensor designed to detect open/close status and count events.",
        "enabled": true,
        "name": "Dragino LDS03A",
        "product": "dragino_lds03a",
        "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": "lds03a_.*"
              },
              "enabled": true
            }
          },
          "buckets": {
            "dragino_lds03a_data": {
              "backend": "mongodb",
              "data": {
                "payload": "{{payload}}",
                "payload_function": "",
                "payload_type": "source_payload",
                "resource": "uplink_decoded",
                "source": "resource",
                "update": "events"
              },
              "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 datalog(i, bytes) {\n  var aa = bytes[0 + i] & 0x02 ? 'TRUE' : 'FALSE';\n  var bb = bytes[0 + i] & 0x01 ? 'OPEN' : 'CLOSE';\n  var cc = ((bytes[1 + i] << 16) | (bytes[2 + i] << 8) | bytes[3 + i]).toString(10);\n  var dd = ((bytes[4 + i] << 16) | (bytes[5 + i] << 8) | bytes[6 + i]).toString(10);\n  var ee = getMyDate(((bytes[7 + i] << 24) | (bytes[8 + i] << 16) | (bytes[9 + i] << 8) | bytes[10 + i]).toString(10));\n  var string = '[' + aa + ',' + bb + ',' + cc + ',' + dd + ',' + ee + ']' + ',';\n\n  return string;\n}\n\nfunction getzf(c_num) {\n  if (parseInt(c_num) < 10) c_num = '0' + c_num;\n\n  return c_num;\n}\n\nfunction getMyDate(str) {\n  var c_Date;\n  if (str > 9999999999) c_Date = new Date(parseInt(str));\n  else c_Date = new Date(parseInt(str) * 1000);\n\n  var c_Year = c_Date.getFullYear(),\n    c_Month = c_Date.getMonth() + 1,\n    c_Day = c_Date.getDate(),\n    c_Hour = c_Date.getHours(),\n    c_Min = c_Date.getMinutes(),\n    c_Sen = c_Date.getSeconds();\n  var c_Time = c_Year + '-' + getzf(c_Month) + '-' + getzf(c_Day) + ' ' + getzf(c_Hour) + ':' + getzf(c_Min) + ':' + getzf(c_Sen);\n\n  return c_Time;\n}\n\nfunction decodeUplink(input) {\n  var port = input.fPort;\n  var bytes = input.bytes;\n  var data = {};\n  switch (input.fPort) {\n    case 2:\n      {\n        data.ALARM = bytes[0] & 0x02 ? 'TRUE' : 'FALSE';\n        data.DOOR_OPEN_STATUS = bytes[0] & 0x01 ? 'OPEN' : 'CLOSE';\n        data.DOOR_OPEN_TIMES = (bytes[1] << 16) | (bytes[2] << 8) | bytes[3];\n        data.LAST_DOOR_OPEN_DURATION = (bytes[4] << 16) | (bytes[5] << 8) | bytes[6];\n        data.TIME = getMyDate(((bytes[7] << 24) | (bytes[8] << 16) | (bytes[9] << 8) | bytes[10]).toString(10));\n      }\n      return {\n        data: data,\n      };\n      break;\n\n    case 3:\n      {\n        for (var i = 0; i < bytes.length; i = i + 11) {\n          var da = datalog(i, bytes);\n          if (i == '0') data.DATALOG = da;\n          else data.DATALOG += da;\n        }\n      }\n      return {\n        data: data,\n      };\n      break;\n\n    case 4:\n      {\n        data.TDC = (bytes[0] << 16) | (bytes[1] << 8) | bytes[2];\n        data.DISALARM = bytes[3] & 0x01;\n        data.KEEP_STATUS = bytes[4] & 0x01;\n        data.KEEP_TIME = (bytes[5] << 8) | bytes[6];\n      }\n      return {\n        data: data,\n      };\n      break;\n\n    case 5:\n      {\n        if (bytes[0] == 0x0a) data.SENSOR_MODEL = 'LDS03A';\n\n        if (bytes[4] == 0xff) data.SUB_BAND = 'NULL';\n        else data.SUB_BAND = bytes[4];\n\n        if (bytes[3] == 0x01) data.FREQUENCY_BAND = 'EU868';\n        else if (bytes[3] == 0x02) data.FREQUENCY_BAND = 'US915';\n        else if (bytes[3] == 0x03) data.FREQUENCY_BAND = 'IN865';\n        else if (bytes[3] == 0x04) data.FREQUENCY_BAND = 'AU915';\n        else if (bytes[3] == 0x05) data.FREQUENCY_BAND = 'KZ865';\n        else if (bytes[3] == 0x06) data.FREQUENCY_BAND = 'RU864';\n        else if (bytes[3] == 0x07) data.FREQUENCY_BAND = 'AS923';\n        else if (bytes[3] == 0x08) data.FREQUENCY_BAND = 'AS923_1';\n        else if (bytes[3] == 0x09) data.FREQUENCY_BAND = 'AS923_2';\n        else if (bytes[3] == 0x0a) data.FREQUENCY_BAND = 'AS923_3';\n        else if (bytes[3] == 0x0b) data.FREQUENCY_BAND = 'CN470';\n        else if (bytes[3] == 0x0c) data.FREQUENCY_BAND = 'EU433';\n        else if (bytes[3] == 0x0d) data.FREQUENCY_BAND = 'KR920';\n        else if (bytes[3] == 0x0e) data.FREQUENCY_BAND = 'MA869';\n\n        data.FIRMWARE_VERSION = (bytes[1] & 0x0f) + '.' + ((bytes[2] >> 4) & 0x0f) + '.' + (bytes[2] & 0x0f);\n        data.BAT = ((bytes[5] << 8) | bytes[6]) / 1000;\n      }\n      return {\n        data: data,\n      };\n      break;\n\n    default:\n      return {\n        errors: ['unknown FPort'],\n      };\n  }\n}\n",
            "environment": "javascript",
            "storage": "",
            "version": "1.0"
          },
          "flows": {
            "dragino_lds03a_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": "LDS03A Door Sensor",
                    "widgets": [
                      {
                        "layout": {
                          "col": 0,
                          "row": 0,
                          "sizeX": 2,
                          "sizeY": 6
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Door Status"
                        },
                        "properties": {
                          "color": "#95a5a6",
                          "colorCondition": {
                            "conditions": [
                              {
                                "color": "#e74c3c",
                                "operator": "eq",
                                "value": "OPEN"
                              },
                              {
                                "color": "#27ae60",
                                "operator": "eq",
                                "value": "CLOSE"
                              }
                            ],
                            "enabled": true
                          },
                          "size": "100px"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "dragino_lds03a_data",
                              "mapping": "DOOR_OPEN_STATUS",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#3498db",
                            "name": "Door Status",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          }
                        ],
                        "type": "led"
                      },
                      {
                        "layout": {
                          "col": 2,
                          "row": 0,
                          "sizeX": 2,
                          "sizeY": 6
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Alarm Status"
                        },
                        "properties": {
                          "color": "#95a5a6",
                          "colorCondition": {
                            "conditions": [
                              {
                                "color": "#e74c3c",
                                "operator": "eq",
                                "value": "TRUE"
                              },
                              {
                                "color": "#27ae60",
                                "operator": "eq",
                                "value": "FALSE"
                              }
                            ],
                            "enabled": true
                          },
                          "size": "100px"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "dragino_lds03a_data",
                              "mapping": "ALARM",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#e74c3c",
                            "name": "Alarm",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          }
                        ],
                        "type": "led"
                      },
                      {
                        "layout": {
                          "col": 4,
                          "row": 0,
                          "sizeX": 2,
                          "sizeY": 6
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Battery"
                        },
                        "properties": {
                          "color": "#27ae60",
                          "gradient": false,
                          "icon": "fas fa-battery-full",
                          "iconColor": "#30a638",
                          "majorTicks": 10,
                          "max": 3.6,
                          "min": 2.5,
                          "plateColor": "#ffffff",
                          "showValue": true,
                          "textColor": "#1E313E",
                          "tickColor": "#000000",
                          "unit": "V"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "dragino_lds03a_data",
                              "mapping": "BAT",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#27ae60",
                            "name": "Battery",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          }
                        ],
                        "type": "gauge"
                      },
                      {
                        "layout": {
                          "col": 0,
                          "row": 6,
                          "sizeX": 2,
                          "sizeY": 6
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Door Open Times"
                        },
                        "properties": {
                          "color": "#3498db",
                          "max": 1000,
                          "min": 0,
                          "unit": "times"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "dragino_lds03a_data",
                              "mapping": "DOOR_OPEN_TIMES",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#3498db",
                            "name": "Open Times",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          }
                        ],
                        "type": "donutchart"
                      },
                      {
                        "layout": {
                          "col": 2,
                          "row": 6,
                          "sizeX": 2,
                          "sizeY": 6
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Last Door Open Duration"
                        },
                        "properties": {
                          "color": "#f39c12",
                          "max": 300,
                          "min": 0,
                          "unit": "sec"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "dragino_lds03a_data",
                              "mapping": "LAST_DOOR_OPEN_DURATION",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#f39c12",
                            "name": "Duration",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          }
                        ],
                        "type": "donutchart"
                      },
                      {
                        "layout": {
                          "col": 4,
                          "row": 6,
                          "sizeX": 2,
                          "sizeY": 6
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Last Event Time"
                        },
                        "properties": {
                          "decimalPlaces": 2,
                          "icon": "fas fa-clock",
                          "iconColor": "#1E313E",
                          "iconGap": "8px",
                          "iconPosition": "above-value",
                          "iconSize": "30px",
                          "textAlign": "center",
                          "textColor": "#1E313E",
                          "textSize": "24px",
                          "textWeight": "font-light"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "dragino_lds03a_data",
                              "mapping": "TIME",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#34495e",
                            "name": "Event Time",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          }
                        ],
                        "type": "text"
                      },
                      {
                        "layout": {
                          "col": 0,
                          "row": 12,
                          "sizeX": 3,
                          "sizeY": 8
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Door Open Times History (24h)"
                        },
                        "properties": {
                          "axis": true,
                          "fill": false,
                          "legend": true,
                          "multiple_axes": false,
                          "options": "var options = {\n    chart: {\n        type: 'line',\n        zoom: {\n            type: 'x',\n            enabled: true,\n            autoScaleYaxis: true\n        },\n        toolbar: {\n            autoSelected: 'zoom'\n        }\n    },\n    stroke: {\n        curve: 'stepline',\n        width: 3\n    },\n    markers: {\n        size: 5,\n        hover: {\n            sizeOffset: 2\n        }\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(0);\n            }\n        }\n    },\n    tooltip: {\n        x: {\n            format: 'dd/MM/yyyy HH:mm:ss'\n        }\n    }\n};"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "dragino_lds03a_data",
                              "mapping": "DOOR_OPEN_TIMES",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#3498db",
                            "name": "Open Times",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "hour",
                              "mode": "relative",
                              "period": "latest",
                              "value": 24
                            }
                          }
                        ],
                        "type": "apex_charts"
                      },
                      {
                        "layout": {
                          "col": 3,
                          "row": 12,
                          "sizeX": 3,
                          "sizeY": 8
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Door Open Duration History (24h)"
                        },
                        "properties": {
                          "axis": true,
                          "fill": false,
                          "legend": true,
                          "multiple_axes": false,
                          "options": "var options = {\n    chart: {\n        type: 'area'\n    },\n    dataLabels: {\n        enabled: false\n    },\n    stroke: {\n        curve: 'smooth',\n        width: 3\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(0);\n            }\n        }\n    },\n    tooltip: {\n        x: {\n            format: 'dd/MM/yyyy HH:mm:ss'\n        }\n    }\n};"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "dragino_lds03a_data",
                              "mapping": "LAST_DOOR_OPEN_DURATION",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#f39c12",
                            "name": "Duration (sec)",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "hour",
                              "mode": "relative",
                              "period": "latest",
                              "value": 24
                            }
                          }
                        ],
                        "type": "apex_charts"
                      },
                      {
                        "layout": {
                          "col": 0,
                          "row": 20,
                          "sizeX": 6,
                          "sizeY": 8
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Battery History (24h)"
                        },
                        "properties": {
                          "axis": true,
                          "fill": true,
                          "legend": true,
                          "multiple_axes": false,
                          "options": "var options = {\n    chart: {\n        type: 'area'\n    },\n    dataLabels: {\n        enabled: false\n    },\n    stroke: {\n        curve: 'smooth',\n        width: 3\n    },\n    fill: {\n        type: 'gradient',\n        gradient: {\n            shadeIntensity: 1,\n            opacityFrom: 0.7,\n            opacityTo: 0.2\n        }\n    },\n    xaxis: {\n        type: 'datetime',\n        labels: {\n            datetimeUTC: false\n        },\n        tooltip: {\n            enabled: false\n        }\n    },\n    yaxis: {\n        min: 2.5,\n        max: 3.6,\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};"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "dragino_lds03a_data",
                              "mapping": "BAT",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#27ae60",
                            "name": "Battery (V)",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "hour",
                              "mode": "relative",
                              "period": "latest",
                              "value": 24
                            }
                          }
                        ],
                        "type": "apex_charts"
                      }
                    ]
                  }
                ]
              }
            }
          ]
        }
      }
    ]
  }
}