-
Notifications
You must be signed in to change notification settings - Fork 1
/
yolink_delay_timer.py
140 lines (117 loc) · 5.04 KB
/
yolink_delay_timer.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
#!/usr/bin/env python3
from threading import Timer, Lock
try:
import udi_interface
logging = udi_interface.LOGGER
Custom = udi_interface.Custom
except ImportError:
import logging
logging.basicConfig(level=logging.DEBUG)
"""
Object for handling Delay - background timer
"""
class RepeatTimer(Timer):
def run(self):
while not self.finished.wait(self.interval):
self.function(*self.args, **self.kwargs)
"""
Object for handling Delay - background countdown timer
Requires call back to handle updates
"""
class CountdownTimer(object):
def __init__ (self):
self.delayTimes = []
self.updateInterval = 5
self.timeRemain = []
self.timerRunning = False
self.timer_cleared = True
self.lock = Lock()
self.callback = None
#self.timer = RepeatTimer(self.updateInterval, self.timeUpdate )
def timerCallback (self, callback, updateInterval = 5):
self.callback = callback
self.updateInterval = updateInterval
#self.timer = RepeatTimer(self.updateInterval, self.timeUpdate )
# list of delays with format [{'ch':channel, 'on':on in min, 'off':off in min}]
def addDelays(self, delayTimes):
self.lock.acquire()
if not self.timerRunning:
self.timer = RepeatTimer(self.updateInterval, self.timeUpdate )
self.timer.start()
self.timerRunning = True
for indx in range(0,len(delayTimes)):
alreadyExists = False
if 'ch' in delayTimes[indx]:
ch = delayTimes[indx]['ch']
for delayIndx in range(0,len(self.timeRemain)):
if ch == self.timeRemain[delayIndx]['ch']:
alreadyExists = True
if 'on' in delayTimes[indx]:
self.timeRemain[delayIndx]['on'] = int(delayTimes[indx]['on']*60)
if 'off' in delayTimes[indx]:
self.timeRemain[delayIndx]['off'] = int(delayTimes[indx]['off']*60)
if 'delayOn' in delayTimes[indx]:
self.timeRemain[delayIndx]['on'] = int(delayTimes[indx]['delayOn']*60)
if 'delayOff' in delayTimes[indx]:
self.timeRemain[delayIndx]['off'] = int(delayTimes[indx]['delayOff']*60)
if not alreadyExists:
self.timeRemain.append(delayTimes[indx])
else:
logging.debug('not supported yet - needs to redo code')
self.lock.release()
# updated the reporting interval in sec
def timerReportInterval (self, reportInterval):
self.lock.acquire()
self.updateInterval = reportInterval
if self.timerRunning:
self.timer.cancel()
self.timerRunning = False
self.timer = RepeatTimer(reportInterval, self.timeUpdate )
self.timer.start()
self.timerRunning = True
self.lock.release()
# updated the remainig time in the running count down timer
def timeUpdate(self):
activeDelays = False
self.lock.acquire()
for delay in range(0,len(self.timeRemain)):
if 'delayOn' in self.timeRemain[delay]:
self.timeRemain[delay]['delayOn'] -= self.updateInterval
if self.timeRemain[delay]['delayOn'] > 0:
activeDelays = True
else:
self.timeRemain[delay]['delayOn'] = 0
if 'on' in self.timeRemain[delay]:
self.timeRemain[delay]['on'] -= self.updateInterval
if self.timeRemain[delay]['on'] > 0:
activeDelays = True
else:
self.timeRemain[delay]['on'] = 0
if 'delayOff' in self.timeRemain[delay]:
self.timeRemain[delay]['delayOff'] -= self.updateInterval
if self.timeRemain[delay]['delayOff'] > 0:
activeDelays = True
else:
self.timeRemain[delay]['delayOff'] = 0
if 'off' in self.timeRemain[delay]:
self.timeRemain[delay]['off'] -= self.updateInterval
if self.timeRemain[delay]['off'] > 0:
activeDelays = True
else:
self.timeRemain[delay]['off'] = 0
if self.callback and activeDelays:
self.callback(self.timeRemain)
#logging.debug('timeUpdate: {} {} {}'.format(activeDelays, self.timer_cleared, self.timeRemain))
#if self.callback:
# if activeDelays:
# self.callback(self.timeRemain)
# self.timer_cleared = False
# elif not self.timer_cleared: # makes sure 0 is sent to display
# self.callback(self.timeRemain)
# self.timer_cleared = True
self.lock.release()
def stop(self):
self.timerRunning = False
self.timer.cancel()
def timeRemaining(self):
return(self.timeRemain)