-
Notifications
You must be signed in to change notification settings - Fork 0
/
UdpBroker.be
529 lines (415 loc) · 13.3 KB
/
UdpBroker.be
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#-----------------------------------
Name UdpBroker
Task UDP message broker
public methods
--------------
publish(topic,value) publish the value to the given topic
subscribe(topic,closure) susbscribe to a specific topic with callback-function(topic,payload)
unsubscribe(topic) unsubscribe from specific topic
deinit() unitialize the broker
init(name) create an initialize the broker
Tasmota Commands
----------------
udppub <topic> <payload> publish a message with topic/payload
udpsub <topic> subscribe for a topic; on receive perfrom command : event <topic>=<payload>
udpunsub <topic> unsubscribe topic
-----------------------------------#
import string
import json
import undefined
class UdpTopic
var topic
var closure
var isCmd
end
class UdpBroker
static PORT = 12233
static IP = "224.3.0.1"
var paketCounter
var tickCounter
var udp
var topics
var name
var infoEnable
var lastLogInfo
var lastWarnInfo
var lastLogProc
var sensorTopic
var publishSensorEnable
var lastTele
#-
callback if broker is started
para (self)
-#
var onStarted
#-
callback if broker is stopped
para (self)
-#
var onStopped
# log with level 'INFO'
def info(proc,info)
self.lastLogProc = proc
self.lastLogInfo = info
if self.infoEnable print("INFO "+self.name+"."+proc+" - "+info) end
end
# log with level 'WARN'
def warn(proc,info)
self.lastLogProc = proc
self.lastWarnInfo = info
print("WARN "+self.name+"."+proc+" - "+info)
end
#-
function publish the value(string) to the given topic(string)
return true if successful, false otherwise
-#
def publish(topic,value)
var cproc="publish"
if type(topic)!="string" || size(topic)==0
self.warn(cproc,"topic must be a non-null-string")
return false
end
if value==nil
self.warn(cproc,"value must not be null")
return false
end
var msg = DynClass()
msg.topic = topic
msg.payload = value
# convert message to json and send via UDP broadcast
var tele = msg.toJson()
if self.udp self.udp.send_multicast(bytes().fromstring(tele)) end
self.info(cproc,tele)
return true
end
#-
function susbscribe to a specific topic
return true, if all checks are ok, false otherwise
-#
def subscribe(topic,closure,isCmd)
var cproc="subscribe"
# closure must be a function
if type(closure) != 'function'
self.warn(cproc," closure is not a function")
return false
end
for m : self.topics
var found=false
if isCmd
found=m.topic == topic && m.isCmd
else
found=m.topic == topic && !m.isCmd
end
if found
self.info(cproc,"topic already subscribed:"+str(topic))
return false
end
end
var xtopic = UdpTopic()
xtopic.topic = topic
xtopic.closure = closure
xtopic.isCmd = isCmd==true
self.topics.push(xtopic)
self.info(cproc,"subscribe to topic:"+str(topic))
return true
end
#-
function unsubscribe a specific topic
return true, if all checks are ok, false otherwise
-#
def unsubscribe(topic,isCmd)
if self.topics == nil return end
## convert to boolean
isCmd = isCmd == true
var i = 0
while i < size(self.topics)
var xtopic = self.topics[i]
if xtopic.topic == topic && xtopic.isCmd == isCmd
self.topics.remove(i) # remove and don't increment
else
i += 1
end
end
end
# forward the message to all subscribers for given topic
def process(topic,payload)
var cproc="process"
# loop over all subscribed topics
for m : self.topics
# on match, execute the associated closure and implement error handling
if m.topic == topic
try
m.closure(topic,payload)
except .. as exname, exmsg
self.warn(cproc, str(exname) + " - " + str(exmsg))
end
end
end
end
def every_hour()
var cproc="every_hour"
self.info(cproc,"begin")
self.restart()
end
#-
- checks perodically for incomming udp-messages
- checks the validity
- forwards the message to the subscribers
-#
def every_100ms()
var cproc="every_poll"
# shortcut, if listener is not started
if self.udp==nil
return
end
# only for debugging issues
self.tickCounter=self.tickCounter+1
if self.tickCounter>=1000
self.tickCounter=0
end
# try to read the udp packet
var packet = self.udp.read()
if packet == nil
return
end
# packet counter runs till 1000
self.paketCounter=self.paketCounter+1
if self.paketCounter>=1000
self.paketCounter=0
end
# get paket as string
var ss = packet.asstring()
# udp message must be a json-string
if !tool.isJson(ss)
self.warn(cproc,"got a non-json-message:"+ss)
return
end
# map json to a dynamic class; property topic must exist
var msg = DynClass()
msg.loadJson(ss)
if msg.topic == undefined
self.warn(cproc,"missing member topic:"+ss)
return
end
# property payload must exist
if msg.payload == undefined
self.warn(cproc,"missing member payload:"+ss)
return
end
if self.infoEnable
self.info(cproc,"got valid message:"+ss)
end
# further processing of the message
self.process(msg.topic,msg.payload)
end
def restart()
var cproc="restart"
self.stop(true)
self.start(true)
self.info(cproc,"done")
end
# start the udp-listener
def start(isRestart)
var cproc="start"
if (self.udp != nil)
self.warn(cproc,"udp client not null")
return
end
isRestart = isRestart==true
self.udp = udp()
var result = self.udp.begin_multicast(self.IP, self.PORT)
self.warn(cproc,"broker started with result:"+str(result)+ " and isRestart="+ str(isRestart))
if self.onStarted && !isRestart
try
self.onStarted(self)
except .. as exname, exmsg
self.warn(cproc, str(exname) + " - " + str(exmsg))
end
end
return result
end
# stops the udp-listener
def stop(isRestart)
var cproc="stop"
if (self.udp==nil)
return
end
isRestart = isRestart==true
self.udp.close()
self.udp = nil
self.warn(cproc,"broker stopped isRestart=" + str(isRestart))
if self.onStopped && !isRestart
try
self.onStopped(self)
except .. as exname, exmsg
self.warn(cproc, str(exname) + " - " + str(exmsg))
end
end
end
# get command parts
def getParts(payload)
var parts = string.split(payload," ")
# remove empty parts
var i=0
while i < size(parts)
if size(parts[i]) == 0
parts.remove(i)
else
i += 1
end
end
return parts
end
# the handler for the command udppub
def cmdHandlerPub(cmd, idx, payload, payload_json)
var cproc="cmdHandlerPub"
if self.infoEnable self.info(cproc,"cmd:" + cmd +" payload:"+payload) end
var parts = self.getParts(payload)
# we need at least 2 non-null args
if size(parts)<2
tasmota.resp_cmnd_failed()
self.warn(cproc,"need at least 2 arguments")
return
end
# get the payload part of the command, wich is all after the topic
var xtopic = parts[0]
var xpayload = string.split(payload,size(xtopic)+1)[1]
# publish message using broker
self.publish(xtopic ,xpayload)
# return OK result to tasmota
tasmota.resp_cmnd_done()
end
# triggers the rules-event
def triggerEvent(topic, payload)
var cproc="triggerEvent"
var ss=nil
if tool.isJson(payload)
# use the map instead of string
if self.infoEnable self.info(cproc,"payload is json") end
payload=tool.lastJsonResult
end
# create nested json object like {"udpBroker":{"SM.PowerAV":"2917.8"}}
var nested = DynClass()
nested[topic]=payload
dyn = DynClass()
dyn.udpBroker = nested.toMap()
ss = dyn.toJson()
if self.infoEnable self.info(cproc,"fire event "+ss) end
# feed the rules engine
tasmota.publish_rule(ss)
end
# the handler for the command udpsub
def cmdHandlerSub(cmd, idx, payload, payload_json)
var cproc="cmdHandlerSub"
self.info(cproc,"cmd:" + cmd +" payload:"+payload)
# get all arguments
var parts = self.getParts(payload)
# we need at least 1 non-null args
if size(parts)<1
tasmota.resp_cmnd_failed()
self.warn(cproc,"need at least 1 arguments")
return
end
var xtopic = parts[0]
# publish message using broker
self.subscribe(xtopic,def(topic,payload) self.triggerEvent(topic,payload) end,true)
# return OK result to tasmota
tasmota.resp_cmnd_done()
end
# the handler for the command udpunsub
def cmdHandlerUnsub(cmd, idx, payload, payload_json)
var cproc="cmdHandlerUnsub"
self.info(cproc,"cmd:" + cmd +" payload:"+payload)
# get all arguments
var parts = self.getParts(payload)
# we need at least 1 non-null args
if size(parts)<1
tasmota.resp_cmnd_failed()
self.warn(cproc,"need at least 1 arguments")
return
end
var xtopic = parts[0]
# unsubscribe topic
self.unsubscribe(xtopic,true)
# return OK result to tasmota
tasmota.resp_cmnd_done()
end
# add a new tasmota commands
def addCommands()
var cproc="addCommands"
var cmd="udppub"
tasmota.remove_cmd(cmd)
tasmota.add_cmd(cmd,
def (cmd, idx, payload, payload_json) self.cmdHandlerPub(cmd, idx, payload, payload_json) end
)
self.info(cproc,"added command:"+cmd)
cmd="udpsub"
tasmota.remove_cmd(cmd)
tasmota.add_cmd(cmd,
def (cmd, idx, payload, payload_json) self.cmdHandlerSub(cmd, idx, payload, payload_json) end
)
self.info(cproc,"added command:"+cmd)
cmd="udpunsub"
tasmota.remove_cmd(cmd)
tasmota.add_cmd(cmd,
def (cmd, idx, payload, payload_json) self.cmdHandlerUnsub(cmd, idx, payload, payload_json) end
)
self.info(cproc,"added command:"+cmd)
end
def teleHandler(value, trigger)
# print ("teleHandler.01")
self.lastTele = value
if !value.contains("Wifi") && string.find(str(self.lastTele),"Time")==2
# print ("publish sensor:",value)
self.publish(self.sensorTopic,json.dump(value))
end
end
def publishSensorMsg(value)
self.publishSensorEnable = value == true
tasmota.remove_rule("tele")
if self.publishSensorEnable
tasmota.add_rule("tele",def(value,trigger) self.teleHandler(value,trigger) end)
end
end
# destructor
def deinit()
var cproc="deinit"
self.stop()
tasmota.remove_driver(self)
tasmota.remove_cron("udpBroker")
self.warn(cproc,"deinit done")
end
# constructor
def init(name)
var cproc="init"
self.publishSensorEnable = false
# -------- create sensor topic
var fullTopic = tasmota.cmd('FullTopic')['FullTopic']
# tasmota_boiler_pm
var topic = tasmota.cmd('Topic')['Topic']
# tele
var prefix = tasmota.cmd('Prefix')['Prefix3']
# %prefix%/tasmota_boiler_pm/
var udpSensorTopic = string.replace(fullTopic, '%topic%', topic)
udpSensorTopic = string.replace(udpSensorTopic, '%prefix%', prefix)
udpSensorTopic += 'SENSOR'
self.sensorTopic = udpSensorTopic
# --------
self.paketCounter=0
self.tickCounter=0
self.topics=[]
self.name = name
self.infoEnable = true
self.info(cproc,"udp-broker created using IP:" + self.IP + " Port:"+ str(self.PORT))
self.infoEnable = false
self.addCommands()
tasmota.add_driver(self)
tasmota.add_cron("0 0 * * * *", /-> self.every_hour(),"udpBroker") # each hour
tasmota.add_rule("Wifi#Connected", / -> self.start())
tasmota.add_rule("Wifi#Disconnected", / -> self.stop())
# in case of manual creation, follow the wifi state
if tasmota.wifi()["up"]
self.start()
end
end
end