forked from gruijter/Plugwise-2-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugwise-2.py
executable file
·1475 lines (1352 loc) · 63.7 KB
/
Plugwise-2.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
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2012,2013,2014,2015,2016,2017,2018,2019,2020 Seven Watt <info@sevenwatt.com>
# <http://www.sevenwatt.com>
#
# This file is part of Plugwise-2-py.
#
# Plugwise-2-py is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Plugwise-2-py is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Plugwise-2-py. If not, see <http://www.gnu.org/licenses/>.
#
# The program is a major modification and extension to:
# python-plugwise - written in 2011 by Sven Petai <hadara@bsd.ee>
# which itself is inspired by Plugwise-on-Linux (POL):
# POL v0.2 - written in 2009 by Maarten Damen <http://www.maartendamen.com>
from serial.serialutil import SerialException
from plugwise import *
from swutil.util import *
from swutil.pwmqtt import *
from plugwise.api import *
from datetime import datetime, timedelta, timezone
#import datetime
import time
import calendar
import subprocess
import glob
import os
import logging
import queue
import threading
import itertools
mqtt = True
try:
import paho.mqtt.client as mosquitto
except:
mqtt = False
print(mqtt)
import pprint as pp
import json
#from json import encoder
#encoder.FLOAT_REPR = lambda o: format(o, '.2f')
json.encoder.FLOAT_REPR = lambda f: ("%.2f" % f)
def jsondefault(object):
return object.decode('utf-8')
#DEBUG_PROTOCOL = False
log_comm(True)
#LOG_LEVEL = 2
schedules_path = "config/schedules"
cfg = json.load(open("config/pw-hostconfig.json"))
tmppath = cfg['tmp_path']+'/'
perpath = cfg['permanent_path']+'/'
logpath = cfg['log_path']+'/'
#make sure log directory exists
if not os.path.exists(logpath):
os.makedirs(logpath)
port = cfg['serial']
epochf = False
if 'log_format' in cfg and cfg['log_format'] == 'epoch':
epochf = True
actdir = 'pwact/'
actpre = 'pwact-'
actpost = '.log'
curpre = 'pwpower'
curpost = '.log'
logdir = 'pwlog/'
logpre = 'pw-'
logpost = '.log'
open_logcomm(logpath+"pw-communication.log")
#prepare for cleanup of /tmp after n days.
cleanage = 604800; # seven days in seconds
# datetime.datetime.now(datetime.UTC). DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC)
locnow = datetime.now(tz=timezone.utc)-timedelta(seconds=time.timezone)
now = locnow
yrfolder = str(now.year)+'/'
if not os.path.exists(perpath+yrfolder+actdir):
os.makedirs(perpath+yrfolder+actdir)
if not os.path.exists(perpath+yrfolder+logdir):
os.makedirs(perpath+yrfolder+logdir)
if not os.path.exists(tmppath+yrfolder+actdir):
os.makedirs(tmppath+yrfolder+actdir)
rsyncing = True
if tmppath == None or tmppath == "/":
tmppath = perpath
rsyncing = False
if rsyncing:
# Could be a recovery after a power failure
# /tmp/pwact-* may have disappeared, while the persitent version exists
perfile = perpath + yrfolder + actdir + actpre + now.date().isoformat() + '*' + actpost
cmd = "rsync -aXuq " + perfile + " " + tmppath + yrfolder + actdir
print(cmd)
subprocess.call(cmd, shell=True)
class PWControl(object):
"""Main program class
"""
def __init__(self):
"""
...
"""
global port
global tmppath
global curpre
global curpost
self.device = Stick(port, timeout=1)
while not self.device.connected:
time.sleep(5)
self.device.reconnect()
self.staticconfig_fn = 'config/pw-conf.json'
self.control_fn = 'config/pw-control.json'
#self.schedule_fn = 'config/pw-schedules.json'
self.last_schedule_ts = None
self.last_control_ts = None
self.circles = []
self.schedules = []
self.controls = []
self.controlsjson = dict()
self.save_controls = False
self.bymac = dict()
self.byname = dict()
self.schedulebyname = dict()
self.curfname = tmppath + curpre + curpost
self.curfile = open(self.curfname, 'w')
self.statuslogfname = tmppath+'pw-status.json'
self.statusfile = open(self.statuslogfname, 'w')
self.statusdumpfname = perpath+'pw-statusdump.json'
self.actfiles = dict()
self.logfnames = dict()
self.daylogfnames = dict()
self.lastlogfname = perpath+'pwlastlog.log'
#read the static configuration
sconf = json.load(open(self.staticconfig_fn))
i=0
for item in sconf['static']:
#remove tabs which survive dialect='trimmed'
for key in item:
if isinstance(item[key],str): item[key] = item[key].strip()
item['mac'] = item['mac'].upper()
if item['production'].strip().lower() in ['true', '1', 't', 'y', 'yes', 'on']:
item['production'] = True
if 'reverse_pol' not in item:
item['reverse_pol'] = False
self.bymac[item.get('mac')]=i
self.byname[item.get('name')]=i
#exception handling timeouts done by circle object for init
self.circles.append(Circle(item['mac'], self.device, item))
self.set_interval_production(self.circles[-1])
i += 1
info("adding circle: %s" % (self.circles[-1].name,))
#retrieve last log addresses from persistent storage
with open(self.lastlogfname, 'a+') as f:
f.seek(0)
for line in f:
parts = line.split(',')
mac, logaddr = parts[0:2]
idx = 0
ts = 0
cum_energy = 0
if len(parts) == 5:
cum_energy = float(parts[4])
if len(parts) >= 4:
idx = int(parts[2])
ts = int(parts[3])
logaddr = int(logaddr)
debug("mac -%s- logaddr -%s- logaddr_idx -%s- logaddr_ts -%s- cum_energy -%s-" % (mac, logaddr, idx, ts, cum_energy))
try:
self.circles[self.bymac[mac]].last_log = logaddr
self.circles[self.bymac[mac]].last_log_idx = idx
self.circles[self.bymac[mac]].last_log_ts = ts
self.circles[self.bymac[mac]].cum_energy = cum_energy
except:
error("PWControl.__init__(): lastlog mac not found in circles")
self.schedulesstat = dict ((f, os.path.getmtime(f)) for f in glob.glob(schedules_path+'/*.json'))
self.schedules = self.read_schedules()
self.poll_configuration()
def get_relays(self):
"""
Update the relay state for circles with schedules enabled.
"""
for c in self.circles:
if c.online and c.schedule_state == 'on':
try:
c.get_info()
except (TimeoutException, SerialException, ValueError) as reason:
debug("Error in get_relays(): %s" % (reason,))
continue
#publish relay_state for schedule-operated circles.
#could also be done unconditionally every 15 minutes in main loop.
self.publish_circle_state(c.mac)
def get_status_json(self, mac):
try:
c = self.circles[self.bymac[mac]]
control = self.controls[self.controlsbymac[mac]]
except:
info("get_status_json: mac not found in circles or controls")
return ""
try:
status = c.get_status()
status["monitor"] = (control['monitor'].lower() == 'yes')
status["savelog"] = (control['savelog'].lower() == 'yes')
#json.encoder.FLOAT_REPR = lambda f: ("%.2f" % f)
#msg = json.dumps(status, default = jsondefault)
msg = json.dumps(status)
except (ValueError, TimeoutException, SerialException) as reason:
error("Error in get_status_json: %s" % (reason,))
msg = ""
return str(msg)
def log_status(self):
self.statusfile.seek(0)
self.statusfile.truncate(0)
self.statusfile.write('{"circles": [\n')
comma = False
for c in self.circles:
if comma:
self.statusfile.write(",\n")
else:
comma = True
#json.dump(c.get_status(), self.statusfile, default = jsondefault)
self.statusfile.write(self.get_status_json(c.mac))
#str('{"typ":"circle","ts":%d,"mac":"%s","online":"%s","switch":"%s","schedule":"%s","power":%.2f,
#"avgpower1h":%.2f,"powts":%d,"seents":%d,"interval":%d,"production":%s,"monitor":%s,"savelog":%s}'
self.statusfile.write('\n] }\n')
self.statusfile.flush()
def dump_status(self):
self.statusdumpfile = open(self.statusdumpfname, 'w+')
self.statusdumpfile.write('{"circles": [\n')
comma = False
for c in self.circles:
if comma:
self.statusdumpfile.write(",\n")
else:
comma = True
json.dump(c.dump_status(), self.statusdumpfile, default = jsondefault)
self.statusdumpfile.write('\n] }\n')
self.statusdumpfile.close()
def sync_time(self):
for c in self.circles:
if not c.online:
continue
try:
info("sync_time: circle %s time is %s" % (c.name, c.get_clock().isoformat()))
if c.type()=='circle+':
#now = datetime.now()
#local time not following DST (always non-DST)
locnow = datetime.utcnow()-timedelta(seconds=time.timezone)
now = locnow
c.set_circleplus_datetime(now)
#now = datetime.now()
#local time not following DST (always non-DST)
locnow = datetime.utcnow()-timedelta(seconds=time.timezone)
now = locnow
c.set_clock(now)
except (ValueError, TimeoutException, SerialException) as reason:
error("Error in sync_time: %s" % (reason,))
def set_interval_production(self, c):
if not c.online:
return
try:
#TODO: Check this. Previously log_interval was only set when difference between config file and circle state
c.set_log_interval(c.loginterval, c.production)
except (ValueError, TimeoutException, SerialException) as reason:
error("Error in set_interval_production: %s" % (reason,))
def generate_test_schedule(self, val):
#generate test schedules
if val == -2:
testschedule = []
for i in range (0, 336):
testschedule.append(-1)
testschedule.append(0)
else:
testschedule = []
for i in range (0, 672):
testschedule.append(val)
return testschedule
def read_schedules(self):
#read schedules
debug("read_schedules")
newschedules = []
self.schedulebyname = dict()
newschedules.append(self.generate_test_schedule(-2))
self.schedulebyname['__PW2PY__test-alternate']=0
info("generate schedule: __PW2PY__test-alternate")
newschedules.append(self.generate_test_schedule(10))
self.schedulebyname['__PW2PY__test-10']=1
info("generate schedule: __PW2PY__test-10")
i=len(newschedules)
schedule_names = [os.path.splitext(os.path.basename(x))[0] for x in glob.glob(schedules_path+'/*.json')]
for sched_fn in schedule_names:
schedfpath = schedules_path+'/'+sched_fn+'.json'
try:
rawsched = json.load(open(schedfpath))
self.schedulebyname[sched_fn]=i
newschedules.append(list(itertools.chain.from_iterable(rawsched['schedule'])))
info("import schedule: %s.json" % (sched_fn,))
#print("import schedule: %s.json" % (sched_fn,))
i += 1
except:
error("Unable to read or parse schedule file %s" % (schedfpath,))
return newschedules
def apply_schedule_changes(self):
""" in case off a failure to upload schedule,
c.online is set to False by api, so reload handled through
self.test_offline() and self.apply_<func>_to_circle
"""
debug("apply_schedule_changes()")
for c in self.circles:
if not c.online:
continue
if c.schedule != None:
if c.schedule.name in self.schedulebyname:
sched = self.schedules[self.schedulebyname[c.schedule.name]]
if sched != c.schedule._watt:
info("apply_schedule_changes: schedule changed. Update in circle %s - %s" % (c.name, c.schedule.name))
#schedule changed so upload to this circle
c.define_schedule(c.schedule.name, sched, time.localtime().tm_isdst)
try:
sched_state = c.schedule_state
c.schedule_off()
c.load_schedule(time.localtime().tm_isdst)
#update scheduleCRC
c.get_clock()
if sched_state == 'on':
c.schedule_on()
except (ValueError, TimeoutException, SerialException) as reason:
#failure to upload schedule.
c.undefine_schedule() #clear schedule forces a retry at next call
error("Error during uploading schedule: %s" % (reason,))
self.publish_circle_state(c.mac)
else:
error("Error during uploading schedule. Schedule %s not found." % (c.schedule.name,))
def read_apply_controls(self):
debug("read_apply_controls")
#read the user control settings
controls = json.load(open(self.control_fn))
self.controlsjson = controls
self.controlsbymac = dict()
newcontrols = []
i=0
for item in controls['dynamic']:
#remove tabs which survive dialect='trimmed'
for key in item:
if isinstance(item[key],str): item[key] = item[key].strip()
item['mac'] = item['mac'].upper()
newcontrols.append(item)
self.controlsbymac[item['mac']]=i
i += 1
#set log settings
if 'log_comm' in controls:
log_comm(controls['log_comm'].strip().lower() == 'yes')
if 'log_level' in controls:
if controls['log_level'].strip().lower() == 'debug':
log_level(logging.DEBUG)
elif controls['log_level'].strip().lower() == 'info':
log_level(logging.INFO)
elif controls['log_level'].strip().lower() == 'error':
log_level(logging.ERROR)
else:
log_level(logging.INFO)
self.controls = newcontrols
for mac, idx in self.controlsbymac.items():
self.apply_control_to_circle(self.controls[idx], mac, force=False)
return
def apply_control_to_circle(self, control, mac, force=False):
"""apply control settings to circle
in case of a communication problem, c.online is set to False by api
self.test_offline() will apply the control settings again by calling this function
"""
updated = self.apply_schedule_to_circle(control, mac, force)
c = self.circles[self.bymac[mac]]
#no longer support setting the switch and schedule state on/off from the control json file.
#debug('circle mac: %s before1 - state [r,sw,sc] %s %s %s - scname %s' % (mac, c.relay_state, control['switch_state'], control['schedule_state'], control['schedule']))
#debug('circle mac: %s before2 - state [r,sw,sc] %s %s %s' % (c.mac, c.relay_state, c.switch_state, c.schedule_state))
#source = "internal"
#updated = updated | self.apply_schedstate_to_circle(control, mac, source, force)
#if control['schedule_state'] != 'on':
# updated = updated | self.apply_switch_to_circle(control, mac, source, force)
#very old approach to schedules
#comment out code
# else:
# #prime the switch state for consistency between circle and control
# try:
# c = self.circles[self.bymac[mac]]
# c.get_info()
# updated = updated | (c.switch_state != control['switch_state'])
# c.switch_state = control['switch_state']
# except:
# info("mac from controls not found in circles while prime switch state")
if updated:
self.publish_circle_state(mac)
#debug('circle mac: %s after1 - state [r,sw,sc] %s %s %s - scname %s' % (mac, c.relay_state, control['switch_state'], control['schedule_state'], control['schedule']))
#debug('circle mac: %s after2 - state [r,sw,sc] %s %s %s' % (c.mac, c.relay_state, c.switch_state, c.schedule_state))
def apply_schedule_to_circle(self, control, mac, force=False):
"""apply control settings to circle
in case of a communication problem, c.online is set to False by api
self.test_offline() will apply the control settings again by calling this function
"""
try:
c = self.circles[self.bymac[mac]]
except:
info("mac from controls not found in circles")
return False
if not c.online:
return False
#load new schedule if required
schedname = str(control['schedule'])
#make sure the scheduleCRC read from circle is set
try:
c.get_clock()
except (ValueError, TimeoutException, SerialException) as reason:
error("Error in apply_schedule_to_circle get_clock: %s" % (reason,))
return False
circle_changed = False
if schedname == '':
#no schedule specified.
try:
#only change schedules when schedule_state = off
c.schedule_off()
except (ValueError, TimeoutException, SerialException) as reason:
error("Error in apply_schedule_to_circle schedule_off: %s" % (reason,))
c.undefine_schedule()
if c.scheduleCRC != 17786:
#set always-on schedule in circle
info('circle mac: %s needs schedule to be undefined' % (mac,))
#print('circle mac: %s needs schedule to be undefined' % (mac,))
try:
c.set_schedule_value(-1)
except (ValueError, TimeoutException, SerialException) as reason:
error("Error in apply_schedule_to_circle set always on schedule: %s" % (reason,))
return False
circle_changed = True
else:
try:
sched = self.schedules[self.schedulebyname[schedname]]
if c.schedule is None or schedname != c.schedule.name or sched != c.schedule._watt:
info('circle mac: %s needs schedule to be defined' % (mac,))
#print('circle mac: %s needs schedule to be defined' % (mac,))
#define schedule object for circle
c.define_schedule(schedname, sched, time.localtime().tm_isdst)
#Only upload when mismatch in CRC
debug("apply_control_to_circle: compare CRC's: %d %d" %(c.schedule.CRC, c.scheduleCRC))
if c.schedule.CRC != c.scheduleCRC or c.schedule.dst != time.localtime().tm_isdst:
info('circle mac: %s needs schedule to be uploaded' % (mac,))
try:
#only change schedules when schedule_state = off
#save current state
act_state = c.schedule_state
c.schedule_off()
c.load_schedule(time.localtime().tm_isdst)
#update scheduleCRC
c.get_clock()
#restore previous state
if act_state == 'on':
c.schedule_on()
except (ValueError, TimeoutException, SerialException) as reason:
error("Error in apply_control_to_circle load_schedule: %s" % (reason,))
return False
circle_changed = True
except:
error("schedule name from controls '%s' not found in table of schedules" % (schedname,))
return circle_changed
def apply_switch_to_circle(self, control, mac, source, force=False):
"""apply control settings to circle
in case of a communication problem, c.online is set to False by api
self.test_offline() will apply the control settings again by calling this function
"""
try:
c = self.circles[self.bymac[mac]]
except:
info("mac from controls not found in circles")
return False
if not c.online:
return False
switched = False
c.requid = source
#switch on/off if required
sw_state = control['switch_state'].lower()
if sw_state == 'on' or sw_state == 'off':
sw = True if sw_state == 'on' else False
if force or sw_state != c.relay_state or sw_state != c.switch_state:
info('circle mac: %s needs to be switched %s' % (mac, sw_state))
try:
c.switch(sw)
except (ValueError, TimeoutException, SerialException) as reason:
error("Error in apply_switch_to_circle failed to switch: %s" % (reason,))
return False
switched = True
else:
error('invalid switch_state value in controls file')
return switched
def apply_schedstate_to_circle(self, control, mac, source, force=False):
"""apply control settings to circle
in case of a communication problem, c.online is set to False by api
self.test_offline() will apply the control settings again by calling this function
"""
try:
c = self.circles[self.bymac[mac]]
except:
info("mac from controls not found in circles")
return False
if not c.online:
print("offline")
return False
switched = False
c.requid = source
#force schedule_state to off when no schedule is defined
#keep writing state to control file, but no longer support applying switch/schedule state on/off
if ((not control['schedule']) or control['schedule'] == "") and control['schedule_state'].lower() == 'on':
control['schedule_state'] = 'off'
info('circle mac: %s schedule forced to off because no schedule defined' % (mac,))
self.write_control_file()
self.last_control_ts = os.stat(self.control_fn).st_mtime
#switch schedule on/off if required
#New approach to schedules. No need to operate switch
#comment out code
#sw_state = control['switch_state'].lower()
#sw = True if sw_state == 'on' else False
sc_state = control['schedule_state'].lower()
if sc_state == 'on' or sc_state == 'off':
sc = True if sc_state == 'on' else False
if force or sc_state != c.schedule_state:
info('circle mac: %s needs schedule to be switched %s' % (mac, sc_state))
try:
c.schedule_onoff(sc)
#New approach to schedules. No need to operate switch
if sc:
#update switch in circles and controls to relay state
#temporary logging to monitor changed schedule policy
if (c.switch_state != c.relay_state):
info("apply_schedstate_to_circle: set switch_state to relay_state when schedule is used")
c.switch_state = c.relay_state
control['switch_state'] = c.switch_state
#comment out code
#if not sc:
# #make sure to put switch in proper position when switching off schedule
# c.switch(sw)
except (ValueError, TimeoutException, SerialException) as reason:
error("Error in apply_schedstate_to_circle failed to switch schedule: %s" % (reason,))
return False
switched = True
#update the switch_state
else:
error('invalid schedule_state value in controls file')
return switched
def setup_actfiles(self):
global tmppath
global perpath
global actpre
global actpost
#close all open act files
for m, f in self.actfiles.items():
f.close()
#open actfiles according to (new) config
self.actfiles = dict()
#now = datetime.now()
#local time not following DST (always non-DST)
locnow = datetime.utcnow()-timedelta(seconds=time.timezone)
now = locnow
today = now.date().isoformat()
yrfold = str(now.year)+'/'
if not os.path.exists(tmppath+yrfold+actdir):
os.makedirs(tmppath+yrfold+actdir)
for mac, idx in self.controlsbymac.items():
if self.controls[idx]['monitor'].lower() == 'yes':
fname = tmppath + yrfold + actdir + actpre + today + '-' + mac + actpost
f = open(fname, 'a')
self.actfiles[mac]=f
# def setup_logfiles(self):
# global tmppath
# global perpath
# global logpre
# global logpost
# #name logfiles according to (new) config
# self.logfnames = dict()
# self.daylogfnames = dict()
# #TODO: use locnow
# now = datetime.now()
# today = now.date().isoformat()
# for mac, idx in self.controlsbymac.iteritems():
# if self.controls[idx]['savelog'].lower() == 'yes':
# try:
# if int(self.circles[self.bymac[self.controls[idx]['mac']]].loginterval) <60:
# #daily logfiles - persistent iso tmp
# #fname = tmppath + logdir + logpre + today + '-' + mac + logpost
# fname = perpath + yrfolder + logdir + logpre + today + '-' + mac + logpost
# self.daylogfnames[mac]=fname
# except:
# #assume contineous logging only
# pass
# #contineous log files
# fname = perpath + yrfolder + logdir + logpre + mac + logpost
# self.logfnames[mac]=fname
# #f = open(fname, 'a')
def rsync_to_persistent(self):
global tmppath
global perpath
global actpre
global actpost
global logpre
global logpost
locnow = datetime.utcnow()-timedelta(seconds=time.timezone)
year = locnow.year
if rsyncing:
# /tmp/<year>/pwact-*
tmpfile = tmppath + str(year) + '/' + actdir + actpre + '*' + actpost
cmd = "rsync -aXq " + tmpfile + " " + perpath + str(year) + '/' + actdir
subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
# /tmp/<prev_year>/pwact-*
tmpfile = tmppath + str(year-1) + '/' + actdir + actpre + '*' + actpost
cmd = "rsync -aXq " + tmpfile + " " + perpath + str(year-1) + '/' + actdir
subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
def cleanup_tmp(self):
# tmpfiles = tmppath + actpre + '*' + actpost
# for fn in glob.iglob(tmpfiles):
# if time.time()-os.path.getmtime(fn) > cleanage:
# os.unlink(fn)
tmpfiles = tmppath + '*/' + actdir + actpre + '*' + actpost
for fn in glob.iglob(tmpfiles):
if time.time()-os.path.getmtime(fn) > cleanage:
os.unlink(fn)
def test_mtime(self, before, after):
modified = []
if after:
for (bf,bmod) in list(before.items()):
if (bf in after and after[bf] > bmod):
modified.append(bf)
return modified
def poll_configuration(self):
debug("poll_configuration()")
before = self.schedulesstat
try:
after = dict ((f, os.path.getmtime(f)) for f in glob.glob(schedules_path+'/*.json'))
added = [f for f in list(after.keys()) if not f in list(before.keys())]
removed = [f for f in list(before.keys()) if not f in list(after.keys())]
modified = self.test_mtime(before,after)
if (added or removed or modified):
self.schedules = self.read_schedules()
self.schedulesstat = after
self.apply_schedule_changes()
#TODO: Remove. The schedule is changed, but not the schedule_state is switched on or off!
#for mac, idx in self.controlsbymac.iteritems():
# self.apply_control_to_circle(self.controls[idx], mac, force=True)
except OSError as reason:
error("Error in poll_configuration(): %s" % (reason,))
# if self.last_schedule_ts != os.stat(self.schedule_fn).st_mtime:
# self.last_schedule_ts = os.stat(self.schedule_fn).st_mtime
# self.schedules = self.read_schedules()
# self.apply_schedule_changes()
if self.last_control_ts != os.stat(self.control_fn).st_mtime:
self.last_control_ts = os.stat(self.control_fn).st_mtime
self.read_apply_controls()
self.setup_actfiles()
#self.setup_logfiles()
#failure to apply control settings to a certain circle results
#in offline state for that circle, so it get repaired when the
#self.test_offline() method detects it is back online
#a failure to load a schedule data also results in online = False,
#and recovery is done by the same functions.
def process_mqtt_commands(self):
updated = False
while not qsub.empty():
rcv = qsub.get()
topic = rcv[0]
payl = rcv[1]
info("process_mqtt_commands: %s %s" % (topic, payl))
#topic format: plugwise2py/cmd/<cmdname>/<mac>
st = topic.split('/')
try:
mac = st[-1]
cmd = st[-2]
#msg format: json: {"mac":"...", "cmd":"", "val":""}
msg = json.loads(payl)
control = self.controls[self.controlsbymac[mac]]
val = msg['val']
try:
source = msg['uid']
except: #KeyError:
source = "anonymous_mqtt"
except:
error("MQTT: Invalid message format in topic or JSON payload")
continue
if cmd == "switch":
val = val.lower()
if val == "on" or val == "off":
control['switch_state'] = val
updated = self.apply_switch_to_circle(control, mac, source)
##switch command overrides schedule_state setting
#control['schedule_state'] = "off"
else:
error("MQTT command has invalid value %s" % (val,))
elif cmd == "schedule":
val = val.lower()
if val == "on" or val == "off":
control['schedule_state'] = val
updated = self.apply_schedstate_to_circle(control, mac, source)
else:
error("MQTT command has invalid value %s" % (val,))
elif cmd == "setsched":
error("MQTT command not implemented")
elif cmd == "reqstate":
#refresh power readings for circle
try:
c = self.circles[self.bymac[mac]]
c.get_power_usage()
info("Just read power for status update")
except:
info("Error in reading power for status update")
#return message is generic state message below
self.publish_circle_state(mac)
if updated:
self.write_control_file()
self.last_control_ts = os.stat(self.control_fn).st_mtime
def ftopic(self, keyword, mac):
return ("plugwise2py/state/" + keyword + "/" + mac)
def publish_circle_state(self, mac):
qpub.put((self.ftopic("circle", mac), str(self.get_status_json(mac)), True))
def write_control_file(self):
#write control file for testing purposes
fjson = open("config/pw-control.json", 'w')
self.controlsjson['dynamic'] = self.controls
json.dump(self.controlsjson, fjson, indent=4)
fjson.close()
def ten_seconds(self):
"""
Failure to read an actual usage is not treated as a severe error.
The missed values are just not logged. The circle ends up in
online = False, and the self.test_offline() tries to recover
"""
self.curfile.seek(0)
self.curfile.truncate(0)
for mac, f in self.actfiles.items():
try:
c = self.circles[self.bymac[mac]]
except:
error("Error in ten_seconds(): mac from controls not found in circles")
continue
if not c.online:
continue
#prepare for logging values
if epochf:
ts = calendar.timegm(datetime.utcnow().utctimetuple())
else:
t = datetime.time(datetime.utcnow()-timedelta(seconds=time.timezone))
ts = 3600*t.hour+60*t.minute+t.second
try:
_, usage, _, _ = c.get_power_usage()
#print("%10d, %8.2f" % (ts, usage,))
f.write("%5d, %8.2f\n" % (ts, usage,))
self.curfile.write("%s, %.2f\n" % (mac, usage))
#debug("MQTT put value in qpub")
msg = str('{"typ":"pwpower","ts":%d,"mac":"%s","power":%.2f}' % (ts, mac, usage))
qpub.put((self.ftopic("power", mac), msg, True))
except ValueError:
#print("%5d, " % (ts,))
f.write("%5d, \n" % (ts,))
self.curfile.write("%s, \n" % (mac,))
except (TimeoutException, SerialException) as reason:
#for continuous monitoring just retry
error("Error in ten_seconds(): %s" % (reason,))
f.flush()
#prevent backlog in command queue
if mqtt: self.process_mqtt_commands()
self.curfile.flush()
return
# def hourly(self):
# return
def log_recording(self, control, mac):
"""
Failure to read recordings for a circle will prevent writing any new
history data to the log files. Also the counter in the counter file is not
updated. Consequently, at the next call (one hour later) reading the
history is retried.
"""
fileopen = False
if control['savelog'].lower() == 'yes':
info("%s: save log " % (mac,))
try:
c = self.circles[self.bymac[mac]]
except:
error("mac from controls not found in circles")
return
if not c.online:
return
#figure out what already has been logged.
try:
c_info = c.get_info()
#update c.power fields for administrative purposes
c.get_power_usage()
except ValueError:
return
except (TimeoutException, SerialException) as reason:
error("Error in log_recording() get_info: %s" % (reason,))
return
last = c_info['last_logaddr']
first = c.last_log
idx = c.last_log_idx
if idx == 4:
idx = 0
first = first + 1
if c.last_log_ts != 0:
last_dt = datetime.utcfromtimestamp(c.last_log_ts)-timedelta(seconds=time.timezone)
else:
last_dt = None
if last_dt ==None:
debug("start with first %d, last %d, idx %d, last_dt None" % (first, last, idx))
else:
debug("start with first %d, last %d, idx %d, last_dt %s" % (first, last, idx, last_dt.strftime("%Y-%m-%d %H:%M")))
#check for buffer wrap around
#The last log_idx is 6015. 6016 is for the range function
if last < first:
if (first == 6015 and idx == 4) or first >= 6016:
first = 0
else:
#last = 6016
#TODO: correct if needed
last = 6015
#read maximum 100 positions at a time for responsiveness and robustness for communication errors
if last > first + 99:
last = first + 99
log = []
try:
#read one more than request to determine interval of first measurement
#TODO: fix after reading debug log
if last_dt == None:
if first>0:
#last_dt == None and first != 0 can occur under exceptional conditions:
#Possibly in case of corruption of pwlastlog.log, or deliberately after
#manual edits of this file
powlist = c.get_power_usage_history(first-1)
if len(powlist) < 4:
#this may occur when history is not written before: error in first-address
#or when the currently to be written address is catching up with the first-address
#to be read here. Soon to be written history addresses are initialized to FF.
#to solve this, just walk to next address and try in next iteration again.
error("log_recording: first time history reading: history entry not complete at first %d, cur %d" % (first, c_info['last_logaddr']))
if first != c_info['last_logaddr']:
first = first + 1
if first >= 6016:
first = 0
c.last_log = first
c.last_log_idx = 0
c.last_log_ts = 0
return
last_dt = powlist[3][0]
#The unexpected case where both consumption and production are logged
#Probably this case does not work at all
if powlist[1][0]==powlist[2][0]:
#not correct for out of sync usage and production buffer
#the returned value will be production only
last_dt=powlist[2][0]
debug("determine last_dt - buffer dts: %s %s %s %s" %
(powlist[0][0].strftime("%Y-%m-%d %H:%M"),
powlist[1][0].strftime("%Y-%m-%d %H:%M"),
powlist[2][0].strftime("%Y-%m-%d %H:%M"),
powlist[3][0].strftime("%Y-%m-%d %H:%M")))
elif first == 0:
#this is the "first run" use case where there was no prior data extracted from history buffers.
powlist = c.get_power_usage_history(0)
if len(powlist) > 2 and powlist[0][0] is not None and powlist[1][0] is not None:
last_dt = powlist[0][0]
#subtract the interval between index 0 and 1
last_dt -= powlist[1][0] - powlist[0][0]
else:
#last_dt cannot be determined yet. wait for 2 hours of recordings. return.
info("log_recording: last_dt cannot be determined. circles did not record data yet.")
return
#loop over log addresses and write to file
for log_idx in range(first, last+1):
buffer = c.get_power_usage_history(log_idx, last_dt)
idx = idx % 4
debug("len buffer: %d, production: %s" % (len(buffer), c.production))
for i, (dt, watt, watt_hour) in enumerate(buffer):
if i >= idx and not dt is None and dt >= last_dt:
#if the timestamp is identical to the previous, add production to usage
#in case of hourly production logging, and end of daylightsaving, duplicate
#timestamps can be present for two subsequent hours. Test the index
#to be odd handles this.
idx = i + 1
if dt == last_dt and c.production == True and i & 1:
tdt, twatt, twatt_hour = log[-1]
twatt+=watt
twatt_hour+=watt_hour
log[-1]=[tdt, twatt, twatt_hour]
else:
log.append([dt, watt, watt_hour])
info("circle buffers: %s %d %s %d %d" % (mac, log_idx, dt.strftime("%Y-%m-%d %H:%M"), watt, watt_hour))
debug("proce with first %d, last %d, idx %d, last_dt %s" % (first, last, idx, last_dt.strftime("%Y-%m-%d %H:%M")))
last_dt = dt
# if idx < 4:
# #not completely read yet.
# last -= 1
# if idx >= 4:
# #not completely read yet.
# last += 1
#idx = idx % 4
# #TODO: buffer is also len=4 for production?
# if len(buffer) == 4 or (len(buffer) == 2 and c.production == True):
# for i, (dt, watt, watt_hour) in enumerate(buffer):
# if not dt is None:
# #if the timestamp is identical to the previous, add production to usage
# #in case of hourly production logging, and end of daylightsaving, duplicate
# #timestamps can be present for two subsequent hours. Test the index
# #to be odd handles this.
# if dt == last_dt and c.production == True and i & 1:
# tdt, twatt, twatt_hour = log[-1]
# twatt+=watt
# twatt_hour+=watt_hour
# log[-1]=[tdt, twatt, twatt_hour]
# else:
# log.append([dt, watt, watt_hour])
# debug("circle buffers: %s %d %s %d %d" % (mac, log_idx, dt.strftime("%Y-%m-%d %H:%M"), watt, watt_hour))
# last_dt = dt
# else: