forked from Dan-in-CA/SIP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_red.py
844 lines (743 loc) · 24.9 KB
/
node_red.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
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# Node-RED SIP extension plugin
# standard library imports
import json # for working with data file
from time import sleep
# local module imports
from blinker import signal
import gpio_pins
import gv # Get access to SIP's settings
from helpers import * # provides utility functions
import requests
from sip import template_render
from urls import urls # Get access to SIP's URLs
import web
import webpages
from webpages import ProtectedPage # Needed for security
from webpages import report_option_change, report_value_change
from webpages import change_options
# Add new URLs to access classes in this plugin.
# fmt: off
urls.extend([
"/node-red-sp", "plugins.node_red.settings",
"/node-red-save", "plugins.node_red.save_settings",
"/jsin", "plugins.node_red.handle_requests"
])
# fmt: on
# Add this plugin to the PLUGINS menu ["Menu Name", "URL"], (Optional)
gv.plugin_menu.append([_("Node-red Settings"), "/node-red-sp"])
##########################
#### Global variables ####
prior_srvals = [0] * len(gv.srvals)
# prior_progs = sorted(gv.pd)
nr_settings = {}
list_vars = [
"ps",
"rovals",
"rs",
"snames",
"srvals",
"output_srvals",
"lrun",
"pd",
"pnames",
"sbits",
"plugin_menu",
]
not_writable = [
"cputemp",
"day_ord",
"lang",
"now",
"nowt",
"npw",
"output_srvals",
"output_srvals_lock",
"passphrase",
"plugin_data",
"plugin_menu",
"pw",
"upas",
"ver_str",
"ver_date",
]
danger_list = [
"nst",
"nopts",
"nprogs",
]
prog_keys = [
"cycle_min",
"day_mask",
"duration_sec",
"enabled",
"interval_base_day",
"name",
"start_min",
"station_mask",
"stop_min",
"type",
]
list_vars = [
"ps",
"rovals",
"rs",
"snames",
"srvals",
"output_srvals",
"lrun",
"pd",
"pnames",
"sbits",
"plugin_menu",
] # these return lists
###################
#### Functions ####
def set_rd(val):
if "chng-rd" in nr_settings:
gv.sd["rd"] = val
if val:
gv.sd["rdst"] = round(gv.now + (val * 3600))
stop_onrain()
msg = f"Rain delay set to {val}"
else:
gv.sd["rdst"] = 0
msg = f"Rain delay off"
jsave(gv.sd, "sd")
# to_node_red(msg)
report_rain_delay_change()
report_option_change()
def set_mm(val):
if "chng-sd" in nr_settings:
if val == 1:
gv.sd["mm"] = 1
jsave(gv.sd, "sd")
msg = "Manual mode on"
elif val == 0:
clear_mm()
gv.sd["mm"] = 0
jsave(gv.sd, "sd")
msg = "Manual mode off"
else:
msg = "invalid request, must be 0 or 1"
to_node_red(msg)
def set_rsn(val):
if "stop-stn" in nr_settings:
if val == 1: stop_stations()
def set_wl(val):
if "chng-wl" in nr_settings:
gv.sd["wl"] = val
jsave(gv.sd, "sd")
msg = f"water level set to {val}"
to_node_red(msg)
report_option_change()
def set_nbrd(val):
if "chng-sd" in nr_settings:
if val != gv.sd["nbrd"]: # number of boards has changed
if val == 0: val = 1
brd_chng = val - gv.sd["nbrd"]
change_options.update_scount(brd_chng)
gv.sd["nbrd"] = gv.sd["nbrd"] + brd_chng
gv.sd["nst"] = gv.sd["nbrd"] * 8
change_options.update_prog_lists("nbrd")
sn_count = gv.sd["nst"]
msg = f"Station count set to {sn_count}"
to_node_red(msg)
def set_htp(val):
if "chng-sd" in nr_settings:
gv.sd["htp"] = val
jsave(gv.sd, "sd")
msg = f"htp changed to {val}"
def set_idd(val):
if "chng-sd" in nr_settings:
if val != gv.sd["idd"] and (val == 0 or val == 1):
gv.sd["idd"] = val
change_options.update_prog_lists("idd")
jsave(gv.sd, "sd")
setting = "on" if val else "off"
msg = f"Individual durations {setting}"
else:
msg = "Error val must be 0 or 1"
to_node_red(msg)
def set_mton(val):
if "chng-sd" in nr_settings:
if val < -60 or val > 60:
msg = "Error val must be -60 to +60"
to_node_red(msg)
return
else:
gv.sd["mton"] = val
jsave(gv.sd, "sd")
def set_mtoff(val):
if "chng-sd" in nr_settings:
if val < -60 or val > 60:
mag = "Error val must be -60 to 60"
to_node_red(msg)
return
else:
gv.sd["mtoff"] = val
jsave(gv.sd, "sd")
def set_rbt(val):
if "chng-sd" in nr_settings:
if val == 1: reboot()
def set_rstrt(val):
if "chng-sd" in nr_settings:
if val == 1: restart()
def set_rst(val):
if "chng-sd" in nr_settings:
if val == 0 or val == 1:
gv.sd["rst"] = val
msg = "Rain detected" if val else "no rain"
to_node_red(msg)
else:
msg = "invalid input, must be 0 or 1"
to_node_red(msg)
return
jsave(gv.sd, "sd")
def set_rs(val):
if "chng-sd" in nr_settings:
if val == 0 or val == 1:
gv.sd["rs"] = val
else:
return "invalid input, must be 0 or 1"
jsave(gv.sd, "sd")
def set_urs(val):
if "chng-sd" in nr_settings:
if val == 0 or val == 1:
gv.sd["urs"] = val
else:
msg = "invalid input, must be 0 or 1"
to_node_red(msg)
return
jsave(gv.sd, "sd")
# def skip(): # - test
# pass
def bit_read(byts, read_lst):
"""Read bits in bytes.
Return dict of bit values per input read list
"""
res_lst = []
# print("byts: ", byts) # - test
# print("read_lst:", read_lst) # - test
for i in read_lst:
idx = int(i) - 1
bid = idx // 8
bit = (byts[bid] >> (idx % 8)) & 1
res_lst.append(bit)
res_dict = dict(zip(read_lst, res_lst))
return res_dict
def bit_write(bytes, bit_dict):
"""Turn bits on or off."""
for key, value in bit_dict.items():
idx = int(key) - 1
byte_idx = idx // 8
if value: # turn bits on
gv.sd[bytes][byte_idx] |= 1 << (idx % 8)
else: # Turn bita off
gv.sd[bytes][byte_idx] &= ~(1 << (idx % 8))
def load_settings():
global nr_settings # , base_url
try:
with open(
"./data/node_red.json", "r"
) as f: # Read nr_settings from json file if it exists
nr_settings = json.load(f)
except IOError: # If file does not exist save default values
nr_settings = {
"station-on-off": "on",
"chng-gv": "off",
"chng-sd": "off",
"chng-rd": "on",
"chng-ro": "on",
"chng-rn": "on",
"nr-url": "http://localhost:1880/node-red",
"chng-stn": "on",
"stop-stn": "on",
"chng-prog": "on",
"chng-wl": "on",
"blinker-signals": "on",
}
jsave(nr_settings, "node_red")
return
load_settings()
def to_node_red(msg):
url = nr_settings["nr-url"]
try:
resp = requests.post(url, data = msg)
except Exception:
pass
# def nr_run_once(list, pre):
# """-
# Start a run once program from node-red
# Optionally disable preemption of running program.
# """
# # print("nr list: ", list) # - test
# if not gv.sd["en"]: # check if SIP is enabled
# return
# # if pre:
# # stop_stations() # preempt any running program.
# # dur_sum = 0
# # stations = [0] * gv.sd["nbrd"]
# for s in list:
# ident = s[0]
# try:
# if isinstance(ident, int):
# sid = ident - 1
# elif ident.isnumeric(): # quoted number
# sid = int(ident) - 1
# else:
# sid = gv.snames.index(ident)
# except Exception as e:
# print("Error: ", e) # name not found
# return e
# dur = s[1]
# gv.rovals[sid] = dur
# run_once(pnum = 100, bump = pre)
# #
# # gv.rs[sid][0] = gv.now + dur_sum
# # dur_sum += dur
# # gv.rs[sid][1] = gv.now + dur_sum
# # gv.rs[sid][2] = dur
# # gv.rs[sid][3] = 100
# # gv.ps[sid][0] = 100
# # gv.ps[sid][1] = dur
# # stations[sid // 8] += 2 ** (sid % 8)
# # if not gv.sd["bsy"]:
# # schedule_stations(stations)
def program_on_off(data):
"""Disable or enable a SIP program.
Optionally end the program if running.
"""
if "prog" in data:
program = data["prog"]
elif "program" in data:
program = data["program"]
state = int(data["set"])
for p in range(len(program)):
prog_p = program[p]
if isinstance(prog_p, int):
pid = prog_p - 1
elif isinstance(prog_p, str):
if prog_p.isnumeric(): # quoted number
pid = int(prog_p) - 1
elif prog_p.lower() == "all":
for i in range(len(gv.pd)):
gv.pd[i]["enabled"] = state
return
else:
try:
pid = gv.pnames.index(prog_p)
except ValueError as e:
return e
if (
"end" in data
and int(data["end"]) == 1
and state == 0
# and gv.pon - 1 == pid # The program to be disabled is running
):
stop_stations()
gv.pd[pid]["enabled"] = state
jsave(gv.pd, "programData")
def station_on_off(data):
"""Enable or disable a station.
Called by "station switch" node.
"""
if "sn" in data:
station = data["sn"]
elif "station" in data:
station = data["station"]
state = data["set"]
masid = gv.sd["mas"] - 1
if "preempt" in data:
pre = data["preempt"]
else:
pre = 0
for sn in range(len(station)): # number of stations in data
stn = station[sn]
if isinstance(stn, int):
sid = stn - 1 # station index
elif isinstance(stn, str):
if stn.isnumeric(): # quoted number
sid = int(stn) - 1
else:
try:
sid = gv.snames.index(stn)
except ValueError as e:
return e
bid = sid // 8
if (
not pre # preempt is not set
and gv.pon # a program is running
and (gv.pd[gv.pon - 1]["station_mask"][bid])
& 1 << sid # station is in the program
and gv.rs[sid][2] # station has a duration in running program
):
continue # Skip if station is controlled by a running program
if state: # set == 1 in Node-red - applies to all stations in data
gv.rs[sid] = [gv.now, float("inf"), 0, 100]
gv.srvals[sid] = 1
gv.sbits[bid] |= 1 << (sid % 8) # Set sbits for this station
gv.ps[sid][0] = 100
gv.ps[sid][1] = float("inf")
else:
gv.lrun = [sid, gv.rs[sid][3], gv.now - gv.rs[sid][0], gv.now]
log_run()
gv.rs[sid] = [0, 0, 0, 0]
gv.srvals[sid] = 0
gv.sbits[bid] &= ~(1 << (sid % 8))
gv.ps[sid] = [0, 0]
gpio_pins.set_output()
def run_now(ident):
"""Start an existing program.
ident can be a program name or number.
"""
try:
if isinstance(ident, int):
pid = ident - 1
elif ident.isnumeric(): # quoted number
pid = int(ident) - 1
else:
pid = gv.pnames.index(str(ident))
except Exception as e:
print("Error: ", e) # name not found
return e
run_program(pid)
### send blinker signals ###
def send_zone_change(name, **kw):
"""Send notification to node-red
when core program signals a change in station state.
"""
global prior_srvals
if len(gv.srvals) > len(prior_srvals):
prior_srvals += [0] * (len(gv.srvals) - len(prior_srvals))
if not "station-on-off" in nr_settings:
# to_node_red("Station status is disabled") # - test
return
if gv.srvals != prior_srvals: # check for a change
for i in range(len(gv.srvals)):
if (
gv.srvals[i] != prior_srvals[i]
and True # Send station on/off: option ####
): # this station has changed
if not gv.srvals[i]: # station is off
if gv.sd["mas"] and gv.sd["mas"] == i + 1:
name = "master"
else:
name = gv.snames[i]
msg = {"station": i + 1, "name": name, "state": 0}
to_node_red(msg)
else:
if gv.sd["mas"] and gv.sd["mas"] == i + 1:
name = "master"
else:
name = gv.snames[i]
msg = {"station": i + 1, "name": name, "state": 1}
# print("sending message to NR") # - test
to_node_red(msg)
prior_srvals = gv.srvals[:]
zones = signal("zone_change")
zones.connect(send_zone_change)
### rain delay ###
def send_rain_delay_change(name, **kw):
"""Send rain delay state change to node-red"""
if gv.sd["rd"]: # just switched on
state = 1
else: # Just switched off
state = 0
msg = {"rd_state": state}
to_node_red(msg)
rd_change = signal("rain_delay_change")
rd_change.connect(send_rain_delay_change)
### new day ###
def send_new_day(name, **kw):
msg = "A new day has started"
to_node_red(msg)
new_day = signal("new_day")
new_day.connect(send_new_day)
### logged in ###
def send_login(name, **kw):
msg = "User logged in"
to_node_red(msg)
loggedin = signal("loggedin")
loggedin.connect(send_login)
###############################
#### blinker signals ##########
"alarm"
"new_day" # working
"loggedin" # done
"option_change"
"program_change"
"program_added"
"program_deleted"
"program_toggled"
"rain_changed" # need new state
"rain_delay_change" # included with "value_change"
"rebooted"
"restarting"
"running_program_change" # to do
"station_completed"
"station_names"
"stations_scheduled"
"value_change" # done - may need modification
"zone_change" # working
###############################
#### function dicts ####
get_gv = {
}
set_GV = {
"sn": "",
"station": "",
"item": "",
"index": "",
}
set_sd = {
"rd": set_rd,
"mm": set_mm,
"rsn": set_rsn,
"wl": set_wl,
"nbrd": set_nbrd,
"htp": set_htp,
"idd": set_idd,
"mton": set_mton,
"mtoff": set_mtoff,
"rbt": set_rbt,
"rstrt": set_rstrt,
"rs": set_rs,
"rst": set_rst,
"urs": set_urs
}
#################
###########################
#### Class definitions ####
class settings(ProtectedPage):
"""
Load an html page for entering plugin settings.
"""
def GET(self):
try:
with open(
"./data/node_red.json", "r"
) as f: # Read settings from json file if it exists
nr_settings = json.load(f)
except IOError: # If file does not exist return empty value
nr_settings = {} # Default settings. can be list, dictionary, etc.
return template_render.node_red(nr_settings) # open settings page
class save_settings(ProtectedPage):
"""
Save user input to json file.
Will create or update file when SUBMIT button is clicked
CheckBoxes only appear in qdict if they are checked.
"""
def GET(self):
global nr_settings
qdict = (
web.input()
) # Dictionary of values returned as query string from settings page.
with open("./data/node_red.json", "w") as f:
json.dump(qdict, f, indent=4) # save to file
nr_settings = dict(qdict)
raise web.seeother("/") # Return user to home page.
class handle_requests(object):
"""
parse request messages from node-red
"""
def GET(self):
"""return a value from get request."""
qdict = dict(web.input()) # Dictionary of JSON values
if "gv" in qdict:
attr = str(qdict["gv"])
try:
if attr in list_vars:
gv_lst = getattr(gv, attr)
sel_lst = [] # empty list to hold elements from list in qdict
if "sn" in qdict or "station" in qdict:
sn_lst = []
if "sn" in qdict:
sn_lst = json.loads(qdict["sn"])
elif "station" in qdict:
sn_lst = json.loads(qdict["station"])
for i in sn_lst:
sel_lst.append(gv_lst[i - 1])
res_dict = dict(zip(sn_lst, sel_lst))
return res_dict
elif "item" in qdict:
item_lst = json.loads(qdict["item"])
try:
for i in item_lst:
sel_lst.append(gv_lst[i - 1])
except TypeError:
return "Error, item value must be an array in double quotes"
except IndexError:
pass
res_dict = dict(zip(item_lst, sel_lst))
return res_dict
elif "index" in qdict:
index_lst = json.loads(qdict["index"])
try:
for i in index_lst:
sel_lst.append(gv_lst[i])
except TypeError:
return "Error, item value must be an array in double quotes"
except IndexError:
pass
res_dict = dict(zip(index_lst, sel_lst))
return res_dict
elif "bit" in qdict:
bit_dict = bit_read(getattr(gv, attr), json.loads(qdict["bit"]))
return json.dumps(bit_dict)
else:
return json.dumps(getattr(gv, attr))
except Exception as e:
return e
elif "sd" in qdict:
try:
if "bit" in qdict:
bit_dict = bit_read(gv.sd[qdict["sd"]], json.loads(qdict["bit"]))
return json.dumps(bit_dict)
else:
return json.dumps(gv.sd[qdict["sd"]])
except Exception as e:
return e
else:
return "Unknown request"
def POST(self):
"""Update SIP with value sent from node-red."""
data = web.data()
data = json.loads(data.decode("utf-8"))
#######################
#### Set gv values ####
if "gv" in data:
if "chng-gv" in nr_settings:
attr = data["gv"]
if attr in not_writable:
return "gv." + attr + " is not writable"
try:
if attr in list_vars:
gv_lst = getattr(gv, attr)
if "sn" in data or "station" in data:
if "sn" in data:
sn_dict = data["sn"]
elif "station" in data:
sn_dict = data["station"]
sn_lst = list(sn_dict.keys())
for i in sn_lst:
idx = int(i) - 1
gv_lst[idx] = sn_dict[i]
return "gv." + attr + " has ben updated"
elif "item" in data:
try:
item_dict = data["item"]
item_lst = list(item_dict.keys())
for i in item_lst:
idx = int(i) - 1
gv_lst[idx] = item_dict[i]
return "gv." + attr + " has ben updated"
except Exception as e:
return e
elif "index" in data:
try:
index_dict = data["item"]
index_lst = list(index_dict.keys())
for i in index_lst:
idx = int(i) - 1
gv_lst[idx] = index_dict[i]
return "gv." + attr + " has ben updated"
except Exception as e:
return e
elif hasattr(gv, data["gv"]):
setattr(gv, data["gv"], data["val"])
return "gv." + data["gv"] + " updated to " + str(data["val"])
else:
return "Unknown request"
except Exception as e:
return e
else:
msg = "Global variable (gv) changes are disabled"
to_node_red(msg)
#######################
#### set sd values ####
elif "sd" in data and "val" in data:
if "chng-sd" in nr_settings:
# Change values
try:
set_sd[data["sd"]](int(data["val"]))
except KeyError:
msg = "invalid request"
to_node_red(msg)
return
if "bit" in data:
bit_write(data["sd"], data["bit"])
else: # handle all other vars
if not data["sd"] in danger_list or (
"force" in data and data["force"] == 1
):
gv.sd[data["sd"]] = val
else:
return "Not recommended"
except Exception as e:
return e
else:
msg = "Settinges (sd) changes are disabled"
to_node_red(msg)
# Station on off
elif ("sn" in data or "station" in data):
print("station on off: ", data) # - test
if "chng-stn" in nr_settings:
station_on_off(data)
else:
msg = "Station control is disabled"
to_node_red(msg)
# run once
elif ("ro" in data or "run once" in data):
if "chng-ro" in nr_settings:
pre = 1
if "preempt" in data and data["preempt"] == 0:
pre = 0
if "ro" in data:
list = data["ro"]
elif "run once" in data:
list = data["run_once"]
# gv.rovals = [0] * gv.sd["nst"] # clear gv.rovals
for s in list:
ident = s[0]
try:
if isinstance(ident, int):
sid = ident - 1
elif ident.isnumeric(): # quoted number
sid = int(ident) - 1
else:
sid = gv.snames.index(ident)
except Exception as e:
print("Error: ", e) # name not found
return e
gv.rovals[sid] = s[1]
run_once(pnum = 100, bump = pre)
else:
msg = "Run Once is disabled"
to_node_red(msg)
# run now
elif "runProg" in data:
if "chng-rn" in nr_settings:
run_now(data["runProg"])
else:
msg = "Run now is disabled"
to_node_red(msg)
# program on/off
elif ("prog" in data or "program" in data):
if "chng-prog" in nr_settings:
program_on_off(data)
else:
msg = "Program control is disabled"
to_node_red(msg)
# stop all stations
elif "stopAll" in data:
if "stop-stn" in nr_settings:
stop_stations()
else:
msg = "Stop stations is disabled"
to_node_red(msg)
else:
msg = "Unknown request"
to_node_red(msg)
return