forked from Dan-in-CA/SIP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpages.py
909 lines (769 loc) · 29.8 KB
/
webpages.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
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
# -*- coding: utf-8 -*-
# standard library imports
import json
import ast
import datetime
import io
import threading
import time
# local module imports
from blinker import signal
from gpio_pins import set_output
import gv
from helpers import *
from sip import template_render
import web
loggedin = signal("loggedin")
def report_login():
loggedin.send()
value_change = signal("value_change")
def report_value_change():
value_change.send()
option_change = signal("option_change")
def report_option_change():
option_change.send()
rebooted = signal("rebooted")
def report_rebooted():
rebooted.send()
station_names = signal("station_names")
def report_station_names():
station_names.send()
program_change = signal("program_change")
def report_program_change():
program_change.send()
program_added = signal("program_added")
def report_program_added():
program_added.send()
program_deleted = signal("program_deleted")
def report_program_deleted():
program_deleted.send()
program_toggled = signal("program_toggled")
def report_program_toggle(index, state):
program_toggled.send("SIP", index = index, state = state)
### Web pages ######################
class WebPage(object):
def __init__(self):
gv.cputemp = get_cpu_temp()
class ProtectedPage(WebPage):
def __init__(self):
check_login(True)
WebPage.__init__(self)
class login(WebPage):
"""Login page"""
def GET(self):
return template_render.login(signin_form())
def POST(self):
my_signin = signin_form()
if not my_signin.validates():
return template_render.login(my_signin)
else:
web.config._session.user = "admin"
report_login()
raise web.seeother("/")
class logout(WebPage):
def GET(self):
web.config._session.user = "anonymous"
web.session.Session.kill(web.config._session)
raise web.seeother("/")
class sw_restart(ProtectedPage):
"""Restart system."""
def GET(self):
restart(1)
return template_render.restarting()
###########################
#### Class Definitions ####
class home(ProtectedPage):
"""Open Home page."""
def GET(self):
return template_render.home()
class change_values(ProtectedPage):
"""Save controller values, return browser to home page."""
def GET(self):
self.change_values()
def change_values(self):
qdict = web.input()
if "rsn" in qdict and qdict["rsn"] == "1":
stop_stations()
raise web.seeother("/")
elif "en" in qdict and qdict["en"] == "0":
gv.srvals = [0] * (gv.sd["nst"]) # turn off all stations
set_output()
if "mm" in qdict and qdict["mm"] == "0":
clear_mm()
if "rd" in qdict:
if qdict["rd"]:
gv.sd["rd"] = int(float(qdict["rd"]))
gv.sd["rdst"] = round(gv.now + gv.sd["rd"] * 3600)
stop_onrain()
report_rain_delay_change()
else:
gv.sd["rd"] = 0
gv.sd["rdst"] = 0
report_rain_delay_change()
for key in list(qdict.keys()):
try:
gv.sd[key] = int(qdict[key])
except Exception:
pass
jsave(gv.sd, "sd")
report_value_change()
raise web.seeother("/") # Send browser back to home page
class view_options(ProtectedPage):
"""Open the options page for viewing and editing."""
def GET(self):
qdict = web.input()
errorCode = "none"
if "errorCode" in qdict:
errorCode = qdict["errorCode"]
return template_render.options(errorCode)
class change_options(ProtectedPage):
"""Save changes to options made on the options page."""
def GET(self):
self.change_options()
def POST(self):
self.change_options()
def change_options(self):
qdict = web.input()
print(qdict)
for i in range(gv.sd["nbrd"]): # capture master associations
if "m" + str(i) in qdict:
try:
gv.sd["mo"][i] = int(qdict["m" + str(i)])
except ValueError:
gv.sd["mo"][i] = 0
if "i" + str(i) in qdict:
try:
gv.sd["ir"][i] = int(qdict["i" + str(i)])
except ValueError:
gv.sd["ir"][i] = 0
if "w" + str(i) in qdict:
try:
gv.sd["iw"][i] = int(qdict["w" + str(i)])
except ValueError:
gv.sd["iw"][i] = 0
if "sh" + str(i) in qdict:
try:
gv.sd["show"][i] = int(qdict["sh" + str(i)])
except ValueError:
gv.sd["show"][i] = 255
if "d" + str(i) in qdict:
try:
gv.sd["show"][i] = ~int(qdict["d" + str(i)]) & 255
except ValueError:
gv.sd["show"][i] = 255
names = []
for i in range(gv.sd["nst"]):
if "s" + str(i) in qdict:
names.append(qdict["s" + str(i)])
else:
names.append("S" + "{:0>2d}".format(i + 1))
gv.snames = names
jsave(names, "snames")
report_station_names()
if "opw" in qdict and qdict["opw"] != "":
try:
if password_hash(qdict["opw"]) == gv.sd["passphrase"]:
if qdict["npw"] == "":
raise web.seeother("/vo?errorCode=pw_blank")
elif qdict["cpw"] != "" and qdict["cpw"] == qdict["npw"]:
gv.sd["passphrase"] = password_hash( # Set new passphrase.
qdict["npw"]
)
else:
raise web.seeother("/vo?errorCode=pw_mismatch")
else:
raise web.seeother("/vo?errorCode=pw_wrong")
except KeyError:
pass
for f in ["name"]:
if "o" + f in qdict:
gv.sd[f] = qdict["o" + f]
for f in ["loc", "lang"]:
if "o" + f in qdict:
if f not in gv.sd or gv.sd[f] != qdict["o" + f]:
qdict["rstrt"] = "1" # force restart with change
gv.sd[f] = qdict["o" + f]
if "onbrd" in qdict:
if qdict["onbrd"] == "": # - test
qdict["onbrd"] = 0
brd_count = int(qdict["onbrd"]) + 1
if brd_count != gv.sd["nbrd"]: # number of boards has changed
brd_chng = brd_count - gv.sd["nbrd"]
self.update_scount(brd_chng)
gv.sd["nbrd"] = brd_count
gv.sd["nst"] = gv.sd["nbrd"] * 8
self.update_prog_lists("nbrd")
if "ohtp" in qdict:
if qdict["ohtp"] == "":
qdict["ohtp"] = 80
if "htp" not in gv.sd or gv.sd["htp"] != int(qdict["ohtp"]):
qdict["rstrt"] = "1" # force restart with change in htp
gv.sd["htp"] = int(qdict["ohtp"])
if "oidd" in qdict:
idd_int = 1
else:
idd_int = 0
if idd_int != gv.sd["idd"]:
gv.sd["idd"] = idd_int
self.update_prog_lists("idd")
if "ohtip" in qdict:
if "htip" not in gv.sd or gv.sd["htip"] != qdict["ohtip"]:
qdict["rstrt"] = "1" # force restart with change in htip
gv.sd["htip"] = qdict["ohtip"]
for f in ["sdt", "mas", "mton", "mtoff", "wl", "lr", "tz"]:
if (f == "sdt"
or f == "mton"
or f == "mtoff"
and qdict["o" + f] == ""
):
qdict["o" + f] = 0
elif (f == "wl"
or f == "lr"
and qdict["o" + f] == ""
):
qdict["o" + f] = 100
if "o" + f in qdict:
if (f == "mton"
and (int(qdict["o" + f]) < -60
or int(qdict["o" + f]) > 60)
): # handle values less than -60 or greater than 60 (temp fix)
raise web.seeother("/vo?errorCode=mton_mismatch")
elif (
f == "mtoff"
and (int(qdict["o" + f]) < -60
or int(qdict["o" + f]) > 60)
): # handle values less than -60 or greater than 60
raise web.seeother("/vo?errorCode=mtoff_mismatch")
gv.sd[f] = int(qdict["o" + f])
if "opigpio" in qdict and (
qdict["opigpio"] == "on" or qdict["opigpio"] == "1"
):
gv.sd["pigpio"] = 1
qdict["rstrt"] = "1" # force restart with change in htip
elif not "opigpio" in qdict and (gv.sd["pigpio"] == 1):
gv.sd["pigpio"] = 0
qdict["rstrt"] = "1" # force restart with change in htip
for f in [
"upas",
"tf",
"urs",
"seq",
"rst",
"lg",
"alr",
]:
if "o" + f in qdict and (
qdict["o" + f] == "on" or qdict["o" + f] == "1"
):
gv.sd[f] = 1
else:
gv.sd[f] = 0
jsave(gv.sd, "sd")
report_option_change()
if "rbt" in qdict and qdict["rbt"] == "1":
gv.srvals = [0] * (gv.sd["nst"])
set_output()
report_rebooted()
reboot()
if "rstrt" in qdict and qdict["rstrt"] == "1":
restart(2)
raise web.seeother("/restart")
raise web.seeother("/")
@staticmethod
def update_scount(brd_chng):
"""
Increase or decrease the number of stations displayed when
number of expansion boards is changed in options.
"""
# print("changing scount", brd_chng) # - test
if brd_chng > 0: # Lengthen lists
incr = brd_chng - (gv.sd["nbrd"] - 1)
sn_incr = incr * 8
gv.sd["mo"].extend([0] * incr)
gv.sd["ir"].extend([0] * incr)
gv.sd["iw"].extend([0] * incr)
gv.sd["show"].extend([255] * incr)
gv.sbits.extend([0] * incr)
gv.srvals.extend([0] * sn_incr)
gv.ps.extend([[0, 0]] * sn_incr)
gv.rs.extend([[0, 0, 0, 0]] * sn_incr)
ln = len(gv.snames)
for i in range(sn_incr):
gv.snames.append(("S" + f"{i + 1 + ln}".zfill(2)))
elif brd_chng < 0: # Shorten lists
new_count = gv.sd["nbrd"] + brd_chng
gv.sd["mo"] = gv.sd["mo"][: new_count]
gv.sd["ir"] = gv.sd["ir"][: new_count]
gv.sd["iw"] = gv.sd["iw"][: new_count]
gv.sd["show"] = gv.sd["show"][: new_count]
newlen = gv.sd["nst"] + (brd_chng *8)
gv.srvals = gv.srvals[:newlen]
gv.ps = gv.ps[:newlen]
gv.rs = gv.rs[:newlen]
gv.snames = gv.snames[:newlen]
gv.sbits = gv.sbits[: new_count]
jsave(gv.snames, "snames")
# change_values.update_prog_lists("nbrd")
@staticmethod
def update_prog_lists(change):
"""
Increase or decrase the lengths of program "duration_sec" and "station_mask"
when number of expansion boards is changed
"""
for p in gv.pd:
if (
change == "idd"
or change == "nbrd"
): # change length of p["duration_sec"]
if not gv.sd["idd"]:
p["duration_sec"] = p["duration_sec"][:1]
if p["duration_sec"][0] == 0:
p["enabled"] = 0
else:
old_dur = None
if (
change == "idd"
and gv.sd["idd"]
and len(p["duration_sec"]) == 1 # changed from !idd -> idd
):
old_dur = p["duration_sec"][0]
p["duration_sec"][0] = 0
if gv.sd["nst"] > len(p["duration_sec"]):
p["duration_sec"].extend(
[0] * (gv.sd["nst"] - len(p["duration_sec"]))
)
if old_dur:
for b in range(
len(p["station_mask"])
): # set duration to old_dur for each active station.
for s in range(8):
if p["station_mask"][b] & 1 << s:
p["duration_sec"][b * 8 + s] = old_dur
elif gv.sd["nst"] < len(p["duration_sec"]):
p["duration_sec"] = p["duration_sec"][: gv.sd["nst"]]
if change == "nbrd": # change length of p["station_mask"]
if gv.sd["nbrd"] > len(p["station_mask"]):
p["station_mask"].extend(
[0] * (gv.sd["nbrd"] - len(p["station_mask"]))
)
elif gv.sd["nbrd"] < len(p["station_mask"]):
p["station_mask"] = p["station_mask"][: gv.sd["nbrd"]]
jsave(gv.pd, "programData")
class get_set_station(ProtectedPage):
"""Return a page containing a number representing the state of a station or all stations if 0 is entered as station number."""
def GET(self):
qdict = web.input()
sid = get_input(qdict, "sid", 0, int) - 1
bid = sid // 8
snmo = (gv.sd["mo"][bid] >> (sid % 8)) & 1 # station enables master operation
set_to = get_input(qdict, "set_to", None, int)
set_time = get_input(qdict, "set_time", 0, int)
if set_to is None:
if sid < 0:
status = "<!DOCTYPE html>\n"
status += "".join(str(x) for x in gv.srvals)
return status
elif sid < gv.sd["nbrd"] * 8:
status = "<!DOCTYPE html>\n"
status += str(gv.srvals[sid])
return status
else:
return _("Station ") + str(sid + 1) + _(" not found.")
elif gv.sd["mm"]:
if set_to: # if station is turning on
if gv.sd["seq"]:
if gv.sd["mas"] and snmo: # if a master is set
for i in range(gv.sd["nst"]): # clear running stations
if i != gv.sd["mas"] - 1: # if not mster
gv.srvals[i] = 0
gv.rs[i] = [0, 0, 0, 0]
gv.ps[i] = [0, 0]
set_output()
sb_byte = (gv.sd["mas"] - 1) // 8
gv.sbits[sb_byte] = 1 << (gv.sd["mas"] - 1) % 8
for b in range(len(gv.sbits)):
if b != sb_byte:
gv.sbits[b] = 0
else:
stop_stations()
gv.rs[sid][0] = gv.now # set start time to current time
if set_time > 0: # if an optional duration time is given
gv.rs[sid][2] = set_time
gv.rs[sid][1] = (
gv.rs[sid][0] + set_time
) # stop time = start time + duration
else:
gv.rs[sid][1] = float("inf") # stop time = infinity
gv.rs[sid][3] = 99 # set program index
gv.ps[sid][1] = set_time
gv.sd["bsy"] = 1
time.sleep(1)
else: # If station is turning off
gv.rs[sid][1] = gv.now + 1
if gv.sd["mas"]:
gv.rs[gv.sd["mas"] - 1][1] = gv.now + 1
time.sleep(1)
raise web.seeother("/")
else:
return _("Manual mode not active.")
class view_runonce(ProtectedPage):
"""Open a page to view and edit a run once program."""
def GET(self):
return template_render.runonce()
class change_runonce(ProtectedPage):
"""Start a Run Once program.
This will override any running program.
"""
def GET(self):
qdict = web.input()
if not gv.sd["en"]: # check operation status
return
gv.rovals = json.loads(qdict["t"])
run_once()
raise web.seeother("/")
class view_programs(ProtectedPage):
"""Open programs page."""
def GET(self):
return template_render.programs()
class modify_program(ProtectedPage):
"""Open page to allow program modification."""
def GET(self):
p_name = ""
qdict = web.input()
pid = int(qdict["pid"])
prog = []
if pid != -1:
mp = gv.pd[pid] # Modified program
if mp["type"] == "interval":
# Convert absolute to relative days remaining for display
rel_rem = (
((mp["day_mask"]) + mp["interval_base_day"])
- (gv.dse % mp["interval_base_day"])
) % mp["interval_base_day"]
p_name = mp["name"]
prog = str(mp).replace(" ", "") # strip out spaces
return template_render.modify(pid, prog, p_name)
class change_program(ProtectedPage):
"""Add a program or modify an existing one."""
def GET(self):
qdict = web.input()
pnum = int(qdict["pid"]) + 1 # program number
cp = json.loads(qdict["v"])
if cp["enabled"] == 0 and pnum == gv.pon: # if disabled and program is running
for i in range(len(gv.ps)):
if gv.ps[i][0] == pnum:
gv.ps[i] = [0, 0]
if gv.srvals[i]:
gv.srvals[i] = 0
for i in range(len(gv.rs)):
if gv.rs[i][3] == pnum:
gv.rs[i] = [0, 0, 0, 0]
if cp["type"] == "interval":
ref = gv.dse + cp["day_mask"] # - 128
cp["day_mask"] = ref % cp["interval_base_day"] # + 128
if qdict["pid"] == "-1": # add new program
gv.pd.append(cp)
gv.pnames.append(cp["name"])
report_program_added()
# print("program added") # - test
else:
gv.pd[int(qdict["pid"])] = cp # replace program
try:
gv.pnames[int(qdict["pid"])] = cp["name"]
except IndexError:
if len(gv.pnames) < len(gv.pd):
diff = len(gv.pd) - len(gv.pnames)
gv.pnames.extend([""] * diff)
gv.pnames[int(qdict["pid"])] = cp["name"]
report_program_change() ### add program index ###
# print("program modified") # - test
jsave(gv.pd, "programData")
raise web.seeother("/vp")
class delete_program(ProtectedPage):
"""Delete one or all existing program(s)."""
def GET(self):
qdict = web.input()
if qdict["pid"] == "-1":
del gv.pd[:]
del gv.pnames[:]
jsave(gv.pd, "programData")
else:
del gv.pd[int(qdict["pid"])]
del gv.pnames[int(qdict["pid"])]
jsave(gv.pd, "programData")
report_program_deleted() ### add program index ###
raise web.seeother("/vp")
class enable_program(ProtectedPage):
"""Activate or deactivate an existing program(s)."""
def GET(self):
qdict = web.input()
index = int(qdict["pid"])
state = int(qdict["enable"])
gv.pd[index]["enabled"] = state
jsave(gv.pd, "programData")
report_program_toggle(index, state) # send program index and state
raise web.seeother("/vp")
class view_log(ProtectedPage):
"""View Log"""
def GET(self):
records = read_log()
return template_render.log(records)
class clear_log(ProtectedPage):
"""Delete all log records"""
def GET(self):
with io.open("./data/log.json", "w") as f:
f.write("")
raise web.seeother("/vl")
class run_now(ProtectedPage):
"""Run a scheduled program now. This will override any running programs."""
def GET(self):
qdict = web.input()
run_program(int(qdict["pid"]))
report_running_program_change()
raise web.seeother("/")
class toggle_temp(ProtectedPage):
"""Change units of Raspi"s CPU temperature display on home page."""
def GET(self):
qdict = web.input()
if qdict["tunit"] == "C":
gv.sd["tu"] = "F"
else:
gv.sd["tu"] = "C"
jsave(gv.sd, "sd")
raise web.seeother("/")
class api_status(ProtectedPage):
"""Simple Status API"""
def GET(self):
statuslist = []
status = {
"systemName": gv.sd["name"],
"systemStatus": gv.sd["en"],
"waterLevel": gv.sd["wl"],
"rainDelay": gv.sd["rd"],
"mode": gv.sd["mm"]
}
statuslist.append(status)
for bid in range(0, gv.sd["nbrd"]):
for s in range(0, 8):
if (gv.sd["show"][bid] >> s) & 1 == 1:
sid = bid * 8 + s
sn = sid + 1
sname = gv.snames[sid]
sbit = (gv.sbits[bid] >> s) & 1
irbit = (gv.sd["ir"][bid] >> s) & 1
status = {
"station": sid,
"status": "disabled",
"reason": "",
"master": 0,
"programName": "",
"remaining": 0,
"name": sname,
}
if gv.sd["en"] == 1:
if sbit:
status["status"] = "on"
if not irbit:
if gv.sd["rd"] != 0:
status["reason"] = "rain_delay"
if gv.sd["urs"] != 0 and gv.sd["rs"] != 0:
status["reason"] = "rain_sensed"
if sn == gv.sd["mas"]:
status["master"] = 1
status["reason"] = "master"
else:
rem = gv.ps[sid][1]
if rem > 65536:
rem = 0
id_nr = gv.ps[sid][0]
if (gv.pon
and not gv.pon > len(gv.pnames)
):
pname = gv.pnames[gv.pon - 1]
else:
pname = "P" + str(id_nr)
if id_nr == 255 or id_nr == 99:
pname = "Manual Mode"
if id_nr == 254 or id_nr == 98:
pname = "Run-once Program"
if id_nr == 254 or id_nr == 100:
pname = "Node-red Program"
if sbit:
status["status"] = "on"
status["reason"] = "program"
status["programName"] = pname
status["remaining"] = rem
else:
if gv.ps[sid][0] == 0:
status["status"] = "off"
else:
status["status"] = "waiting"
status["reason"] = "program"
status["programName"] = pname
status["remaining"] = rem
else:
status["reason"] = "system_off"
statuslist.append(status)
web.header("Content-Type", "application/json")
return json.dumps(statuslist)
class api_log(ProtectedPage):
"""Simple Log API"""
def GET(self):
qdict = web.input()
thedate = qdict["date"]
# date parameter filters the log values returned; "yyyy-mm-dd" format
theday = datetime.date(*map(int, thedate.split("-")))
prevday = theday - datetime.timedelta(days=1)
prevdate = prevday.strftime("%Y-%m-%d")
records = read_log()
data = []
for event in records:
# return any records starting on this date
if "date" not in qdict or event["date"] == thedate:
data.append(event)
# also return any records starting the day before and completing after midnight
if event["date"] == prevdate:
duration_components = event["duration"].split(":")
duration = int(duration_components[0]);
if len(duration_components) > 2:
duration = duration*60 + int(duration_components[1])
if (
int(event["start"].split(":")[0]) * 60
+ int(event["start"].split(":")[1])
+ duration
> 24 * 60
):
data.append(event)
web.header("Content-Type", "application/json")
return json.dumps(data)
class water_log(ProtectedPage):
"""Simple Log API"""
def GET(self):
records = read_log()
data = _("Date, Start Time, Zone, Duration, Program Name, Program Index, Adjustment") + "\n"
for r in records:
event = ast.literal_eval(json.dumps(r))
if not("program_index" in event):
event["program_index"] = "n/a"
if not("adjustment" in event):
event["adjustment"] = "n/a"
data += (
event["date"]
+ ", "
+ event["start"]
+ ", "
+ str(event["station"] + 1)
+ ", "
+ event["duration"]
+ ", "
+ event["program"]
+ ", "
+ event["program_index"]
+ ", "
+ event["adjustment"]
+ "\n"
)
web.header("Content-Type", "text/csv")
return data
class showInFooter(object):
"""
Enables plugins to display e.g. sensor readings in the footer of SIP's UI
"""
def __init__(self, label = "", val = "", unit = ""):
self._label = label
self._val = val
self._unit = unit
self._idx = len(gv.pluginFtr)
gv.pluginFtr.append({"label": self._label, "val": self._val, "unit": self._unit})
@property
def clear(self):
del gv.pluginFtr[self._idx][:] # Remove elements of list but keep empty list
@property
def label(self):
if not self._label:
return "label not set"
else:
return self._label
@label.setter
def label(self, text):
self._label = text
if self._label:
gv.pluginFtr[self._idx]["label"] = self._label + ": "
@property
def val(self):
if self._val == "":
return "val not set"
else:
return self._val
@val.setter
def val(self, num):
self._val = num
if self._val:
gv.pluginFtr[self._idx]["val"] = self._val
@property
def unit(self):
if not self.unit:
return "unit not set"
else:
return self._unit
@unit.setter
def unit(self, text):
self._unit = text
gv.pluginFtr[self._idx]["unit"] = self._unit
class showOnTimeline(object):
"""
Used to display plugin data next to station time countdown on home page timeline.
use [instance name].unit = [unit name] to set unit for data e.g. "lph".
use [instance name].val = [plugin data] to display plugin data
use [instance name].clear to remove from display e.g. if station not included in plugin.
"""
def __init__(self, val = "", unit = ""):
self._val = val
self._unit = unit
self._idx = None
self._idx = len(gv.pluginStn)
gv.pluginStn.append([self._unit, self._val])
@property
def clear(self):
del gv.pluginStn[self._idx][:] # Remove elements of list but keep empty list
@property
def unit(self):
if not self.unit:
return "unit not set"
else:
return self._unit
@unit.setter
def unit(self, text):
self._unit = text
gv.pluginStn[self._idx][0] = self._unit
@property
def val(self):
if not self._val:
return "val not set"
else:
return self._val
@val.setter
def val(self, num):
self._val = num
gv.pluginStn[self._idx][1] = self._val
class plugin_data(ProtectedPage):
"""Simple plugin data api
Called through URLs as /api/plugins.
"""
def GET(self):
footer_data = []
station_data = []
data = {}
for i, v in enumerate(gv.pluginFtr):
footer_data.append((i, v["val"]))
for v in gv.pluginStn:
station_data.append(v[1])
data["fdata"] = footer_data
data["sdata"] = station_data
web.header('Content-Type', 'application/json')
return json.dumps(data, ensure_ascii=False)
class rain_sensor_state(ProtectedPage):
"""Return rain sensor state."""
def GET(self):
web.header("Content-Type", "application/json")
return gv.sd["rs"]