Skip to content

Plugin file

Plugin configuration file
{
  "name": "dragino_ldds04",
  "version": "1.0.0",
  "description": "The Dragino LDDS04 is a 4-channel LoRaWAN Ultrasonic Distance Sensor.",
  "author": "Thinger.io",
  "license": "MIT",
  "repository": {
    "type": "git",
    "url": "https://github.com/thinger-io/plugins.git",
    "directory": "dragino-ldds04"
  },
  "metadata": {
    "name": "Dragino LDDS04",
    "description": "The Dragino LDDS04 is a 4-channel LoRaWAN Ultrasonic Distance Sensor.",
    "image": "assets/ldds04.png",
    "category": "devices",
    "vendor": "dragino"
  },
  "resources": {
    "products": [
      {
        "description": "The Dragino LDDS04 is a 4-channel LoRaWAN Ultrasonic Distance Sensor.",
        "enabled": true,
        "name": "Dragino LDDS04",
        "product": "dragino_ldds04",
        "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"
                }
              },
              "response": {
                "data": {
                  "source": "request_response"
                }
              }
            },
            "uplink": {
              "enabled": true,
              "handle_connectivity": true,
              "request": {
                "data": {
                  "payload": "{{payload}}",
                  "payload_function": "",
                  "payload_type": "source_payload",
                  "resource_stream": "uplink",
                  "target": "resource_stream"
                }
              },
              "response": {
                "data": {
                  "source": "request_response"
                }
              }
            }
          },
          "autoprovisions": {
            "device_autoprovisioning": {
              "config": {
                "mode": "pattern",
                "pattern": "ldds04_.*"
              },
              "enabled": true
            }
          },
          "buckets": {
            "dragino_ldds04_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 decodeUplink(input) {\n  var port = input.fPort;\n  var bytes = input.bytes;\n  var value = ((bytes[0] << 8) | bytes[1]) & 0x3fff;\n  var data = {};\n  switch (input.fPort) {\n    case 2:\n      if (!(bytes[0] == 0x03 && bytes[10] == 0x02)) {\n        data.BatV = value / 1000;\n        data.EXTI_Trigger = bytes[0] & 0x80 ? 'TRUE' : 'FALSE';\n        data.distance1_cm = ((bytes[2] << 8) | bytes[3]) / 10;\n        data.distance2_cm = ((bytes[4] << 8) | bytes[5]) / 10;\n        data.distance3_cm = ((bytes[6] << 8) | bytes[7]) / 10;\n        data.distance4_cm = ((bytes[8] << 8) | bytes[9]) / 10;\n        data.mes_type = bytes[10];\n      }\n      return {\n        data: data,\n      };\n    default:\n      return {\n        errors: ['unknown FPort'],\n      };\n  }\n}\n",
            "environment": "javascript",
            "storage": "",
            "version": "1.0"
          },
          "flows": {
            "dragino_ldds04_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": "LDDS04 Distance",
                    "widgets": [
                      {
                        "layout": {
                          "col": 0,
                          "row": 0,
                          "sizeX": 2,
                          "sizeY": 5
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Battery Voltage"
                        },
                        "properties": {
                          "color": "#27ae60",
                          "gradient": false,
                          "icon": "fas fa-battery-full",
                          "iconColor": "#30a638",
                          "iconSize": "",
                          "majorTicks": 10,
                          "max": 3.6,
                          "min": 2.5,
                          "plateColor": "#ffffff",
                          "showValue": true,
                          "textColor": "#1E313E",
                          "tickColor": "#000000",
                          "unit": "V"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "dragino_ldds04_data",
                              "mapping": "BatV",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#27ae60",
                            "name": "Battery",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          }
                        ],
                        "type": "gauge"
                      },
                      {
                        "layout": {
                          "col": 2,
                          "row": 0,
                          "sizeX": 4,
                          "sizeY": 5
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Battery History (24h)"
                        },
                        "properties": {
                          "alignTimeSeries": false,
                          "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            allowMouseWheelZoom: false\n        },\n        toolbar: {\n            autoSelected: 'zoom'\n        }\n    },\n    stroke: {\n        curve: 'smooth',\n        width: 3\n    },\n    markers: {\n        size: 4,\n        hover: {\n            sizeOffset: 2\n        }\n    },\n    grid: {\n        row: {\n            colors: ['#f3f3f3', 'transparent'],\n            opacity: 0.5\n        }\n    },\n    xaxis: {\n        type: 'datetime',\n        tooltip: {\n            enabled: false\n        },\n        labels: {\n            datetimeUTC: 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_ldds04_data",
                              "mapping": "BatV",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#27ae60",
                            "name": "Battery (V)",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "hour",
                              "mode": "relative",
                              "period": "latest",
                              "value": 24
                            }
                          }
                        ],
                        "type": "apex_charts"
                      },
                      {
                        "layout": {
                          "col": 0,
                          "row": 5,
                          "sizeX": 1,
                          "sizeY": 6
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Distance 1"
                        },
                        "properties": {
                          "color": "#3498db",
                          "max": 1000,
                          "min": 0,
                          "unit": "cm"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "dragino_ldds04_data",
                              "mapping": "distance1_cm",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#3498db",
                            "name": "Distance 1",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          }
                        ],
                        "type": "donutchart"
                      },
                      {
                        "layout": {
                          "col": 1,
                          "row": 5,
                          "sizeX": 1,
                          "sizeY": 6
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Distance 2"
                        },
                        "properties": {
                          "color": "#e74c3c",
                          "max": 1000,
                          "min": 0,
                          "unit": "cm"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "dragino_ldds04_data",
                              "mapping": "distance2_cm",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#e74c3c",
                            "name": "Distance 2",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          }
                        ],
                        "type": "donutchart"
                      },
                      {
                        "layout": {
                          "col": 2,
                          "row": 5,
                          "sizeX": 2,
                          "sizeY": 6
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Distance 3"
                        },
                        "properties": {
                          "color": "#f39c12",
                          "max": 1000,
                          "min": 0,
                          "unit": "cm"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "dragino_ldds04_data",
                              "mapping": "distance3_cm",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#f39c12",
                            "name": "Distance 3",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          }
                        ],
                        "type": "donutchart"
                      },
                      {
                        "layout": {
                          "col": 4,
                          "row": 5,
                          "sizeX": 2,
                          "sizeY": 6
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Distance 4"
                        },
                        "properties": {
                          "color": "#9b59b6",
                          "max": 1000,
                          "min": 0,
                          "unit": "cm"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "dragino_ldds04_data",
                              "mapping": "distance4_cm",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#9b59b6",
                            "name": "Distance 4",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          }
                        ],
                        "type": "donutchart"
                      },
                      {
                        "layout": {
                          "col": 0,
                          "row": 11,
                          "sizeX": 6,
                          "sizeY": 8
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Distance History (24h)"
                        },
                        "properties": {
                          "alignTimeSeries": false,
                          "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            allowMouseWheelZoom: false\n        },\n        toolbar: {\n            autoSelected: 'zoom'\n        }\n    },\n    stroke: {\n        curve: 'smooth',\n        width: 3\n    },\n    markers: {\n        size: 4,\n        hover: {\n            sizeOffset: 2\n        }\n    },\n    grid: {\n        row: {\n            colors: ['#f3f3f3', 'transparent'],\n            opacity: 0.5\n        }\n    },\n    xaxis: {\n        type: 'datetime',\n        tooltip: {\n            enabled: false\n        },\n        labels: {\n            datetimeUTC: false\n        }\n    },\n    yaxis: {\n        labels: {\n            formatter: function (val) {\n                if (val !== null && typeof val !== 'undefined')\n                    return val.toFixed(1);\n            }\n        }\n    },\n    tooltip: {\n        x: {\n            format: 'dd/MM/yyyy HH:mm:ss'\n        }\n    }\n};"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "dragino_ldds04_data",
                              "mapping": "distance1_cm",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#3498db",
                            "name": "Distance 1 (cm)",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "hour",
                              "mode": "relative",
                              "period": "latest",
                              "value": 24
                            }
                          },
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "dragino_ldds04_data",
                              "mapping": "distance2_cm",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#e74c3c",
                            "name": "Distance 2 (cm)",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "hour",
                              "mode": "relative",
                              "period": "latest",
                              "value": 24
                            }
                          },
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "dragino_ldds04_data",
                              "mapping": "distance3_cm",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#f39c12",
                            "name": "Distance 3 (cm)",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "hour",
                              "mode": "relative",
                              "period": "latest",
                              "value": 24
                            }
                          },
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "dragino_ldds04_data",
                              "mapping": "distance4_cm",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#9b59b6",
                            "name": "Distance 4 (cm)",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "hour",
                              "mode": "relative",
                              "period": "latest",
                              "value": 24
                            }
                          }
                        ],
                        "type": "apex_charts"
                      },
                      {
                        "layout": {
                          "col": 0,
                          "row": 19,
                          "sizeX": 3,
                          "sizeY": 4
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "External Trigger"
                        },
                        "properties": {
                          "decimalPlaces": 0,
                          "enableExtraTextColor": false,
                          "enableIconColor": true,
                          "enableIconSize": false,
                          "extraText": "",
                          "extraTextColor": "#1E313E",
                          "extraTextColorConditions": [],
                          "extraTextConditions": [],
                          "extraTextPosition": "above-value",
                          "extraTextSize": "20px",
                          "extraTextWeight": "font-light",
                          "icon": "fas fa-bolt",
                          "iconColor": "#95a5a6",
                          "iconColorConditions": [
                            {
                              "color": "#27ae60",
                              "operator": "eq",
                              "value": "TRUE"
                            },
                            {
                              "color": "#e74c3c",
                              "operator": "eq",
                              "value": "FALSE"
                            }
                          ],
                          "iconConditions": [],
                          "iconGap": "8px",
                          "iconPosition": "before-value",
                          "iconSize": "50px",
                          "iconVerticalOffset": "0px",
                          "link": "",
                          "textAlign": "center",
                          "textColor": "#1E313E",
                          "textColorConditions": [
                            {
                              "color": "#27ae60",
                              "operator": "eq",
                              "value": "TRUE"
                            },
                            {
                              "color": "#e74c3c",
                              "operator": "eq",
                              "value": "FALSE"
                            }
                          ],
                          "textSize": "40px",
                          "textWeight": "font-semibold",
                          "unit": "",
                          "unitSize": "20px"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "dragino_ldds04_data",
                              "mapping": "EXTI_Trigger",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#34495e",
                            "name": "Trigger Status",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          }
                        ],
                        "type": "text"
                      },
                      {
                        "layout": {
                          "col": 3,
                          "row": 19,
                          "sizeX": 3,
                          "sizeY": 4
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Message Type"
                        },
                        "properties": {
                          "decimalPlaces": 0,
                          "enableExtraTextColor": false,
                          "enableIconColor": false,
                          "enableIconSize": false,
                          "extraText": "",
                          "extraTextColor": "#1E313E",
                          "extraTextColorConditions": [],
                          "extraTextConditions": [],
                          "extraTextPosition": "above-value",
                          "extraTextSize": "20px",
                          "extraTextWeight": "font-light",
                          "icon": "fas fa-info-circle",
                          "iconColor": "#3498db",
                          "iconColorConditions": [],
                          "iconConditions": [],
                          "iconGap": "8px",
                          "iconPosition": "before-value",
                          "iconSize": "50px",
                          "iconVerticalOffset": "0px",
                          "link": "",
                          "textAlign": "center",
                          "textColor": "#1E313E",
                          "textColorConditions": [],
                          "textSize": "40px",
                          "textWeight": "font-semibold",
                          "unit": "",
                          "unitSize": "20px"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "dragino_ldds04_data",
                              "mapping": "mes_type",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#34495e",
                            "name": "Message Type",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          }
                        ],
                        "type": "text"
                      }
                    ]
                  }
                ]
              }
            }
          ]
        }
      }
    ]
  }
}