forked from Di-Ny/chirpstack2domoticz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.py
357 lines (320 loc) · 15.3 KB
/
plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# Chirpstack 2 domoticz : a plugin to connect LoRa device to Domoticz
#
# Author: DiNy
#
# Goal :
# - Log LoRa devices from Chirpstack App Server data into Domoticz
#
# Concept :
# - ChirpStack is organized with Applications regrouping several Nodes. This plugin is ment to have one Hardware by Application ID.
# - The LoRaWAN message use the Cayenne LPP format. Each Node can then have several channels.
# - The user should declare the Application ID in the parameters of the Hardware
# - A MQTT listner will then start with topic application/ID/#
# - New devices will automatically be added to the hardware with a unique ID, and an incremented UNIT. Thus there is a max of 255 |node-channel-sensor| can be used.
# - A unique device ID will be associated : "eui-[Chirpstack APPEUI]-[CayenneLPP-CHANNEL]-[SENSORTYPE]
#
#
# TODO :
# - Create merged sensor : temp+hum, temp+hum+baro into One sensor if associated to the same channel
# - Extend the Cayenne LPP possibilities by allowing the user to define its own decoding protocol ( CS_USER-Defined_to_DZ={} )
# - Place the unsupported Cayenne LPP datatype into text sensors
# -
#
# Credits :
# Uses the zigbee2mqtt domoticz plugin https://github.com/stas-demydiuk/domoticz-zigbee2mqtt-plugin
# Based on https://github.com/emontnemery/domoticz_mqtt_discovery
# Touched files :
# - plugin.py
# - API/api.py
# License : MIT
"""
<plugin key="ChirpStack2Dz" name="Chirpstack MQTT to Domoticz" author="DiNy" version="0.0.1" externallink="https://github.com/Di-Ny/chirpstack2domoticz">
<description>
<h3>Chirpstack 2 Domoticz notice </h3><br/>
A plugin to connect LoRa device to Domoticz.
<br/>
<b>Requirements:</b>
<ul >
<li>Already installed MQTT Server : https://www.domoticz.com/wiki/MQTT</li>
<li>Working ChirpStack Application Server : https://www.chirpstack.io/</li>
<li>A physical LoRa device configured to work on Chirpstack App Server, with <b>Cayenne LPP decoding</b></li>
<li>For now only the following Cayenne LPP are supported : digital Input, Analog Input (0-255), illuminance, presence, temperature, humidity, barometer </li>
</ul>
<br/>
<b>Description of parameters :</b><br/>
<ul>
<li>MQTT - Only tested on local hosted server, unencrypted. Port is filled by \"Connection\"</li>
<li>Application ID - You find this in the ChirpStack Application Server. It will subscribe to the corresponding MQTT topic application/[ID]/#</li>
<li>Automatically create new devices - Must be False to prevent the app adding new devices. Also Domoticz setting should allow new hardware</li>
<li>Debug - Will display all MQTT data ofr the concerned Topic</li>
</ul>
</description>
<params>
<param field="Address" label="MQTT IP" width="200px" required="true" default="127.0.0.1"/>
<param field="Port" label="Connection" required="true" width="200px">
<options>
<option label="Unencrypted" value="1883" default="true" />
<option label="Encrypted (Client Certificate)" value="8883" />
</options>
</param>
<param field="Username" label="MQTT Username (optional)" width="300px" required="false" default=""/>
<param field="Password" label="MQTT Password (optional)" width="300px" required="false" default="" password="true"/>
<param field="Mode3" label="MQTT Client ID (optional)" width="300px" required="false" default=""/>
<param field="Mode1" label="Application ID" default="" required="false" width="200px"/>
<param field="Mode2" label="Create devices" required="true" width="75px">
<options>
<option label="True" value="True" default="true"/>
<option label="False" value="False"/>
</options>
</param>
<param field="Mode6" label="Debug" width="75px">
<options>
<option label="Verbose" value="Verbose"/>
<option label="True" value="Debug"/>
<option label="False" value="Normal" default="true" />
</options>
</param>
</params>
</plugin>
"""
import Domoticz
import json
import time
import re
import os
from shutil import copy2, rmtree
from mqtt import MqttClient
from api import API
import requests
# from devices_manager import DevicesManager
# from groups_manager impocdrt GroupsManager
from mqtt_globals import *
class BasePlugin:
mqttClient = None
def CS_2_DZ_decoder(self,topic, message):
success = 0
Domoticz.Debug("CS_Decoder")
EUI = str(topic.split("device/")[1].split("/")[0])
Domoticz.Debug("EUI : "+EUI)
############
## Devices
payload = json.loads(message)
Domoticz.Debug("Payload:\n"+str(payload))
if not 'loRaSNR' in str(payload):
Domoticz.Log("Decoding LoRa message problem. Check ChirpStack application.")
return success
RSSI=payload['rxInfo'][0]['loRaSNR']
RSSI = int(round(mapFromTo(int(RSSI),-20,0,0,10), 0))#Domoticz : between 0 and 10
Domoticz.Debug("RSSI : "+str(RSSI))
CS_objects = json.loads(payload['objectJSON'])
if(len(CS_objects) == 0):
Domoticz.Log("Decoding cayenne message problem. Check ChirpStack application.")
return success
#Domoticz.Debug(str(CS_objects))#{'temperatureSensor': {'1': 12}, 'humiditySensor': {'1': 80}}
#Reorganize data
data = []
for obj in CS_objects :
if(str(obj) in CS_cayenne_to_DZ):
for channel in CS_objects[str(obj)]:
#Get the value of the sensor
value=str(CS_objects[str(obj)][str(channel)])
#build DATA : [Channel, value, [Corresp.DZ_TypeName, Corresp.DZ_pType,DZ_sType, Corresp.FancyName, Corresp.UNIT_ID] ]
data.append([channel, value, CS_cayenne_to_DZ[str(obj)]])
else:
Domoticz.Log("Unfound/Unproccessable Cayenne Data Type : "+str(obj))
Domoticz.Debug("DATA structure:"+str(data))
#[['1', '12', ['Temperature', 243, 17, 'temp']], ['2', '14', ['Temperature', 243, 17, 'temp']], ['3', '15', ['Temperature', 243, 17, 'temp']], ['1', '80', ['Humidity', 243, 3, 'hum']]]
# #aggregate several sensor types into one sensor. Decide whi one is nvalue, which one is svalue
# # TODO
data_ag = data
#Devices ID
d_ids=[]
d_units=[]
for x in Devices:
d_ids.append([str(x),str(Devices[x].ID),str(Devices[x].Name),str(Devices[x].DeviceID)])
d_units.append(int(x))
Domoticz.Debug(str(d_ids))
Domoticz.Debug(str(d_units))
#Device
for d in range(0,len(data_ag)):
# Case there is Type_Name assigned
if data_ag[d][2][0] != "":
#Shape the values
device_fancyname = str(data_ag[d][2][3])
device_id="eui-"+EUI+"-"+str(data_ag[d][0])+"-"+device_fancyname#eui-a8d4fdf51239f322-1-hum
device_Name="Unknown "+device_fancyname
device_TypeName=str(data_ag[d][2][0])
Domoticz.Debug("===================")
Domoticz.Debug("device_id: "+str(device_id))
#If not exist, create
if(device_id not in str(d_ids)):
#Find a new unit ID
device_Unit = 0
for u in range(1, 255):
if u not in d_units:
device_Unit = u
d_units.append(u)
break
if device_Unit == 0:
Domoticz.Log("ERROR - No more UNIT ID to allocate. Device not created")
return 0
Domoticz.Debug("device_Name: "+str(device_Name))
Domoticz.Debug("device_TypeName: "+str(device_TypeName))
if self.create_new_devices == "True":
Domoticz.Log("ChirpStack Plugin - Create NEW device with ID="+str(device_id)+", Name="+str(device_Name)+", Type="+str(device_TypeName))
#Create
Domoticz.Device(Name=device_Name, TypeName=device_TypeName, Unit=device_Unit, DeviceID=device_id).Create()
else:
Domoticz.Log("ChirpStack Plugin - DISBALED [Create device "+str(device_id)+"]")
for x in Devices:
#Check if now exists, and update
if(str(Devices[x].DeviceID) == device_id):
device_svalue = str(data_ag[d][1])
#Update
Domoticz.Log("Chirpstack Plugin - Update device (ID="+str(Devices[x].ID)+") value ="+data_ag[d][1])
Devices[x].Update(nValue=int(device_svalue), sValue=str(device_svalue), SignalLevel=RSSI)
Domoticz.Debug("===================")
return success
def onStart(self):
self.debugging = Parameters["Mode6"]
if self.debugging == "Verbose":
Domoticz.Debugging(2+4+8+16+64)
if self.debugging == "Debug":
Domoticz.Debugging(2)
DumpConfigToLog()
Domoticz.Log("Chirpstack2Domoticz Plugin started")
#App ID Base TOPIC
if(Parameters["Mode1"].strip() != ""):
self.base_topic = "application/"+Parameters["Mode1"].strip()
else:
Domoticz.Log("Warning : No Application ID submitted. It will subscribe to all application topics")
self.base_topic = "application"
self.subscribed_for_devices = False
self.create_new_devices = Parameters["Mode2"].strip()
mqtt_server_address = Parameters["Address"].strip()
mqtt_server_port = Parameters["Port"].strip()
mqtt_client_id = Parameters["Mode3"].strip()
self.mqttClient = MqttClient(mqtt_server_address, mqtt_server_port, mqtt_client_id, self.onMQTTConnected, self.onMQTTDisconnected, self.onMQTTPublish, self.onMQTTSubscribed)
self.api = API(Devices, self.publishToMqtt)
def checkDevices(self):
Domoticz.Debug("checkDevices called")
def onStop(self):
Domoticz.Debug("onStop called")
#self.uninstall()
def onCommand(self, Unit, Command, Level, Color):
Domoticz.Debug("onCommand: " + Command + ", level (" + str(Level) + ") Color:" + Color)
message = None
device = Devices[Unit] #Devices is Domoticz collection of devices for this hardware
device_params = device.DeviceID.split('_')
entity_id = device_params[0]
Domoticz.Debug("OnCommand --> Partie desctivee")
# if (self.devices_manager.get_device_by_id(entity_id) != None):
# message = self.devices_manager.handle_command(Devices, device, Command, Level, Color)
# elif(self.groups_manager.get_group_by_deviceid(device.DeviceID) != None):
# message = self.groups_manager.handle_command(device, Command, Level, Color)
# else:
# Domoticz.Log('Can\'t process command from device "' + device.Name + '"')
# if (message != None):
# self.publishToMqtt(message['topic'], message['payload'])
def publishToMqtt(self, topic, payload):
self.mqttClient.publish(self.base_topic + '/' + topic, payload)
def onConnect(self, Connection, Status, Description):
Domoticz.Debug("onConnect called")
self.mqttClient.onConnect(Connection, Status, Description)
def onDisconnect(self, Connection):
self.mqttClient.onDisconnect(Connection)
def onDeviceModified(self, unit):
if (unit == 255):
self.api.handle_request(Devices[unit].sValue)
return
def onMessage(self, Connection, Data):
self.mqttClient.onMessage(Connection, Data)
def onHeartbeat(self):
self.mqttClient.onHeartbeat()
def onMQTTConnected(self):
#if connected subscirbe to topic
self.mqttClient.subscribe([self.base_topic+"/#"])
Domoticz.Log("MQTT subscribed to : "+self.base_topic+"/#")
def onMQTTDisconnected(self):
self.subscribed_for_devices = False
def onMQTTSubscribed(self):
Domoticz.Debug("onMQTTSubscribed")
def onMQTTPublish(self, topic, message):
Domoticz.Debug("On MQTT Publish")
payload = json.dumps(message)
Domoticz.Debug("MQTT message: " + topic + " " + str(payload))
topic = topic.replace(self.base_topic + '/', '')
#self.api.handle_mqtt_message(topic, message)
#Decode the pyaload and message. --> Will automatically add sensors
self.CS_2_DZ_decoder(topic, payload)
global _plugin
_plugin = BasePlugin()
def onStart():
global _plugin
_plugin.onStart()
def onConnect(Connection, Status, Description):
global _plugin
_plugin.onConnect(Connection, Status, Description)
def onMessage(Connection, Data):
global _plugin
_plugin.onMessage(Connection, Data)
def onDisconnect(Connection):
global _plugin
_plugin.onDisconnect(Connection)
def onHeartbeat():
global _plugin
_plugin.onHeartbeat()
# Generic helper functions
def DumpConfigToLog():
for x in Parameters:
if Parameters[x] != "":
Domoticz.Debug( "'" + x + "':'" + str(Parameters[x]) + "'")
Domoticz.Debug("Device count: " + str(len(Devices)))
for x in Devices:
Domoticz.Debug("Device: " + str(x) + " - " + str(Devices[x]))
Domoticz.Debug("Device ID: '" + str(Devices[x].ID) + "'")
Domoticz.Debug("Device Name: '" + Devices[x].Name + "'")
Domoticz.Debug("Device nValue: " + str(Devices[x].nValue))
Domoticz.Debug("Device sValue: '" + Devices[x].sValue + "'")
Domoticz.Debug("Device LastLevel: " + str(Devices[x].LastLevel))
return
def DumpDictionaryToLog(theDict, Depth=""):
if isinstance(theDict, dict):
for x in theDict:
if isinstance(theDict[x], dict):
Domoticz.Log(Depth+"> Dict '"+x+"' ("+str(len(theDict[x]))+"):")
DumpDictionaryToLog(theDict[x], Depth+"---")
elif isinstance(theDict[x], list):
Domoticz.Log(Depth+"> List '"+x+"' ("+str(len(theDict[x]))+"):")
DumpListToLog(theDict[x], Depth+"---")
elif isinstance(theDict[x], str):
Domoticz.Log(Depth+">'" + x + "':'" + str(theDict[x]) + "'")
else:
Domoticz.Log(Depth+">'" + x + "': " + str(theDict[x]))
def DumpListToLog(theList, Depth):
if isinstance(theList, list):
for x in theList:
if isinstance(x, dict):
Domoticz.Log(Depth+"> Dict ("+str(len(x))+"):")
DumpDictionaryToLog(x, Depth+"---")
elif isinstance(x, list):
Domoticz.Log(Depth+"> List ("+str(len(theList))+"):")
DumpListToLog(x, Depth+"---")
elif isinstance(x, str):
Domoticz.Log(Depth+">'" + x + "':'" + str(theList[x]) + "'")
else:
Domoticz.Log(Depth+">'" + x + "': " + str(theList[x]))
def mapFromTo(x,a,b,c,d):
#
if a <b :
if x < a:
x=a
if x > b :
x=b
else:
if x > a:
x=a
if x <b:
x=b
y=(x-a)/(b-a)*(d-c)+c
return y