Skip to content

Plugin file

Plugin configuration file
{
  "name": "edc_communication_module",
  "version": "1.0.0",
  "description": "The EDC communication module is an add-on module for water meters for secure remote reading and for integrating water meters into smart metering systems",
  "author": "Thinger.io",
  "license": "MIT",
  "repository": {
    "type": "git",
    "url": "https://github.com/thinger-io/plugins.git",
    "directory": "minol-zenner-edc-communication-module"
  },
  "metadata": {
    "name": "EDC-COMMUNICATION-MODULE",
    "description": "The EDC communication module is an add-on module for water meters for secure remote reading and for integrating water meters into smart metering systems",
    "image": "assets/edc_communication.png",
    "category": "devices",
    "vendor": "minol-zenner"
  },
  "resources": {
    "products": [
      {
        "description": "The EDC communication module is an add-on module for water meters for secure remote reading and for integrating water meters into smart metering systems",
        "enabled": true,
        "name": "EDC-COMMUNICATION-MODULE",
        "product": "edc_communication_module",
        "profile": {
          "api": {
            "downlink": {
              "enabled": true,
              "handle_connectivity": false,
              "request": {
                "data": {
                  "path": "/downlink",
                  "payload": "{\n    \"data\"    : \"{{payload.data=\"\"}}\",\n    \"port\"    :  {{payload.port=85}},\n    \"priority\":  {{payload.priority=3}},\n    \"confirmed\" :  {{payload.confirmed=false}},\n    \"uplink\"  :  {{property.uplink}} \n}",
                  "payload_function": "",
                  "payload_type": "source_payload",
                  "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": "edc-.*"
              },
              "enabled": true
            }
          },
          "buckets": {
            "edc_data_bucket": {
              "backend": "mongodb",
              "data": {
                "payload": "{{payload}}",
                "payload_function": "parseOrDecodeIncomingData",
                "payload_type": "source_payload",
                "resource": "uplink",
                "source": "resource",
                "update": "events"
              },
              "enabled": true,
              "retention": {
                "period": 3,
                "unit": "months"
              },
              "tags": []
            }
          },
          "code": {
            "code": "// Device Identifier Resolver configured in \"uplink\" API resource.\nfunction getId(payload) {\n    return payload.deviceId;\n}\n\n// Custom payload processing function configured in \"edc_communication_module_data_bucket\" bucket.\nfunction parseOrDecodeIncomingData(payload) {\n    return decodeThingerUplink(payload);\n}\n\nfunction 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 bytes = input.bytes;\n  var status = bytes[0]; // Assuming the first byte contains the status summary bits\n  var data = {\n    removal_detection: (status & 0x01) >> 0,\n    battery_low: (status & 0x02) >> 1,\n    battery_end_of_life: (status & 0x04) >> 2,\n    hw_error: (status & 0x08) >> 3,\n    coil_manipulation: (status & 0x20) >> 5,\n    // ... other status bits\n  };\n\n  // Assuming the next bytes contain meter values in SP12 format\n  var meterValues = [];\n  for (var i = 1; i < bytes.length; i += 4) {\n    // Combining four bytes to form a meter value\n    var meterValue = (bytes[i] << 24) | (bytes[i + 1] << 16) | (bytes[i + 2] << 8) | bytes[i + 3];\n    meterValues.push(meterValue);\n  }\n\n  data.meter_values = meterValues;\n\n  return {\n    data: data,\n  };\n}",
            "environment": "javascript",
            "storage": "",
            "version": "1.0"
          },
          "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": "Water Meter Dashboard",
                    "widgets": [
                      {
                        "layout": {
                          "col": 0,
                          "row": 0,
                          "sizeX": 4,
                          "sizeY": 12
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Water Consumption History"
                        },
                        "properties": {
                          "axis": true,
                          "fill": false,
                          "legend": true,
                          "multiple_axes": false
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "edc_data_bucket",
                              "mapping": "meter_values[0]",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#3498db",
                            "name": "Water Consumption",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "day",
                              "mode": "relative",
                              "period": "latest",
                              "value": 7
                            }
                          }
                        ],
                        "type": "chart"
                      },
                      {
                        "layout": {
                          "col": 4,
                          "row": 0,
                          "sizeX": 2,
                          "sizeY": 6
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Current Reading"
                        },
                        "properties": {
                          "color": "#3498db",
                          "decimal_places": 2,
                          "icon": "",
                          "unit": "m³",
                          "unit_text": "Water Volume"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "edc_data_bucket",
                              "mapping": "meter_values[0]",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#1abc9c",
                            "name": "Current Meter Value",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          }
                        ],
                        "type": "text"
                      },
                      {
                        "layout": {
                          "col": 4,
                          "row": 6,
                          "sizeX": 2,
                          "sizeY": 12
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Device Status & Alerts"
                        },
                        "properties": {
                          "source": "code",
                          "template": "<div style=\"width:100%; height:100%; padding:20px; overflow-y:auto\">\r\n  <div ng-repeat=\"entry in value | limitTo:1\">\r\n    <h4>Status Summary</h4>\r\n    <div style=\"margin: 15px 0;\">\r\n      <div style=\"padding: 8px; margin: 5px 0; border-radius: 4px;\" ng-style=\"{'background-color': entry.removal_detection ? '#e74c3c' : '#2ecc71', 'color': 'white'}\">\r\n        <strong>Removal Detection:</strong> {{ entry.removal_detection ? 'ALERT' : 'Normal' }}\r\n      </div>\r\n      <div style=\"padding: 8px; margin: 5px 0; border-radius: 4px;\" ng-style=\"{'background-color': entry.battery_low ? '#f39c12' : '#2ecc71', 'color': 'white'}\">\r\n        <strong>Battery Level:</strong> {{ entry.battery_low ? 'Low' : 'Normal' }}\r\n      </div>\r\n      <div style=\"padding: 8px; margin: 5px 0; border-radius: 4px;\" ng-style=\"{'background-color': entry.battery_end_of_life ? '#e74c3c' : '#2ecc71', 'color': 'white'}\">\r\n        <strong>Battery EOL:</strong> {{ entry.battery_end_of_life ? 'CRITICAL' : 'Normal' }}\r\n      </div>\r\n      <div style=\"padding: 8px; margin: 5px 0; border-radius: 4px;\" ng-style=\"{'background-color': entry.hw_error ? '#e74c3c' : '#2ecc71', 'color': 'white'}\">\r\n        <strong>Hardware:</strong> {{ entry.hw_error ? 'Error' : 'Normal' }}\r\n      </div>\r\n      <div style=\"padding: 8px; margin: 5px 0; border-radius: 4px;\" ng-style=\"{'background-color': entry.coil_manipulation ? '#e74c3c' : '#2ecc71', 'color': 'white'}\">\r\n        <strong>Coil Status:</strong> {{ entry.coil_manipulation ? 'Manipulation Detected' : 'Normal' }}\r\n      </div>\r\n    </div>\r\n    <p style=\"margin-top: 15px; font-size: 11px; color: #7f8c8d;\">\r\n      Last Update: {{ entry.ts | date:'medium' }}\r\n    </p>\r\n  </div>\r\n</div>"
                        },
                        "sources": [
                          {
                            "aggregation": {},
                            "bucket": {
                              "backend": "mongodb",
                              "id": "edc_data_bucket",
                              "mapping": "ts",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#1abc9c",
                            "name": "timestamp",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          },
                          {
                            "aggregation": {},
                            "bucket": {
                              "backend": "mongodb",
                              "id": "edc_data_bucket",
                              "mapping": "removal_detection",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#e74c3c",
                            "name": "removal_detection",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          },
                          {
                            "aggregation": {},
                            "bucket": {
                              "backend": "mongodb",
                              "id": "edc_data_bucket",
                              "mapping": "battery_low",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#f39c12",
                            "name": "battery_low",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          },
                          {
                            "aggregation": {},
                            "bucket": {
                              "backend": "mongodb",
                              "id": "edc_data_bucket",
                              "mapping": "battery_end_of_life",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#e74c3c",
                            "name": "battery_end_of_life",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          },
                          {
                            "aggregation": {},
                            "bucket": {
                              "backend": "mongodb",
                              "id": "edc_data_bucket",
                              "mapping": "hw_error",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#e74c3c",
                            "name": "hw_error",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          },
                          {
                            "aggregation": {},
                            "bucket": {
                              "backend": "mongodb",
                              "id": "edc_data_bucket",
                              "mapping": "coil_manipulation",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#e74c3c",
                            "name": "coil_manipulation",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          }
                        ],
                        "type": "html_time"
                      },
                      {
                        "layout": {
                          "col": 0,
                          "row": 12,
                          "sizeX": 4,
                          "sizeY": 12
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Recent Readings"
                        },
                        "properties": {
                          "source": "code",
                          "template": "<div style=\"width:100%; height:100%; overflow-y:auto\">\r\n  <table class=\"table table-striped table-condensed\">\r\n    <thead>\r\n      <tr>\r\n        <th>Date</th>\r\n        <th>Meter Reading</th>\r\n        <th>Status</th>\r\n      </tr>\r\n    </thead>\r\n    <tbody>\r\n      <tr ng-repeat=\"entry in value\">\r\n        <td>{{ entry.ts | date:'medium' }}</td>\r\n        <td>{{ (entry.meter_values && entry.meter_values[0]) || '—' }} m³</td>\r\n        <td>\r\n          <span ng-if=\"entry.battery_low || entry.battery_end_of_life || entry.hw_error || entry.removal_detection || entry.coil_manipulation\" style=\"color: #e74c3c; font-weight: bold;\">⚠ Alert</span>\r\n          <span ng-if=\"!entry.battery_low && !entry.battery_end_of_life && !entry.hw_error && !entry.removal_detection && !entry.coil_manipulation\" style=\"color: #2ecc71;\">✓ OK</span>\r\n        </td>\r\n      </tr>\r\n    </tbody>\r\n  </table>\r\n</div>"
                        },
                        "sources": [
                          {
                            "aggregation": {},
                            "bucket": {
                              "backend": "mongodb",
                              "id": "edc_data_bucket",
                              "mapping": "ts",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#1abc9c",
                            "name": "timestamp",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "day",
                              "mode": "relative",
                              "period": "latest",
                              "value": 7
                            }
                          },
                          {
                            "aggregation": {},
                            "bucket": {
                              "backend": "mongodb",
                              "id": "edc_data_bucket",
                              "mapping": "meter_values",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#3498db",
                            "name": "meter_values",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "day",
                              "mode": "relative",
                              "period": "latest",
                              "value": 7
                            }
                          },
                          {
                            "aggregation": {},
                            "bucket": {
                              "backend": "mongodb",
                              "id": "edc_data_bucket",
                              "mapping": "battery_low",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#f39c12",
                            "name": "battery_low",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "day",
                              "mode": "relative",
                              "period": "latest",
                              "value": 7
                            }
                          },
                          {
                            "aggregation": {},
                            "bucket": {
                              "backend": "mongodb",
                              "id": "edc_data_bucket",
                              "mapping": "battery_end_of_life",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#e74c3c",
                            "name": "battery_end_of_life",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "day",
                              "mode": "relative",
                              "period": "latest",
                              "value": 7
                            }
                          },
                          {
                            "aggregation": {},
                            "bucket": {
                              "backend": "mongodb",
                              "id": "edc_data_bucket",
                              "mapping": "hw_error",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#e74c3c",
                            "name": "hw_error",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "day",
                              "mode": "relative",
                              "period": "latest",
                              "value": 7
                            }
                          },
                          {
                            "aggregation": {},
                            "bucket": {
                              "backend": "mongodb",
                              "id": "edc_data_bucket",
                              "mapping": "removal_detection",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#e74c3c",
                            "name": "removal_detection",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "day",
                              "mode": "relative",
                              "period": "latest",
                              "value": 7
                            }
                          },
                          {
                            "aggregation": {},
                            "bucket": {
                              "backend": "mongodb",
                              "id": "edc_data_bucket",
                              "mapping": "coil_manipulation",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#e74c3c",
                            "name": "coil_manipulation",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "day",
                              "mode": "relative",
                              "period": "latest",
                              "value": 7
                            }
                          }
                        ],
                        "type": "html_time"
                      }
                    ]
                  }
                ]
              }
            }
          ]
        }
      }
    ]
  }
}