-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.py
executable file
·89 lines (78 loc) · 3.5 KB
/
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
#!/usr/bin/env python3
import logging, threading
logging.basicConfig(level=logging.INFO, format="[%(module)s] %(message)s")
def call_repeatedly(interval, func, *args, **kwargs):
stopped = threading.Event()
def loop():
while not stopped.wait(interval): # the first call is in `interval` secs
func(*args, **kwargs)
threading.Thread(target=loop, daemon=True).start()
return stopped.set
class FakeGatoTimer():
def __init__(self, minutes, accessoryName, *args, **kwargs):
self.minutes = minutes
self.subscribedServices = []
self.running = False
self.accessoryName = accessoryName
def subscribe(self, service, callback):
#logging.info("** Fakegato-timer Subscription : {0}".format(service.accessoryName))
newService = {
'service': service,
'callback': callback, # -> calculateAverage/select_types
'backLog': [],
'previousBackLog': [],
'previousAvrg': {}
}
self.subscribedServices.append(newService)
def getSubscriber(self, service):
for i in self.subscribedServices:
if i['service'] == service:
break
return i
def stop(self):
logging.info("**Stop Global Fakegato-Timer****")
self.cancel_future_calls()
self.running = False
def executeCallbacks(self):
logging.info("**Fakegato-timer: executeCallbacks** {0} ** interval {1} minutes**".format(self.accessoryName, self.minutes))
if len(self.subscribedServices) != 0:
for service in self.subscribedServices:
if callable(service["callback"]): # --> calculateAverage
service["previousAvrg"] = service["callback"](
{
'backLog': service['backLog'],
'previousAvrg': service['previousAvrg'],
'timer': self,
'immediate': False
}
)
def executeImmediateCallback(self, service):
logging.info("**Fakegato-timer: executeImmediateCallback**")
if callable(service['callback']) and len(service['backLog']) != 0:
service['callback']({
'backLog': service['backLog'],
'timer': self,
'immediate': True
})
def addData(self, params):
data = params['entry']
service = params['service']
immediateCallback = params['immediateCallback'] if 'immediateCallback' in params else False
if immediateCallback == True: # door or motion -> replace
if len(self.getSubscriber(service)['backLog']) == 0:
self.getSubscriber(service)['backLog'].append(data)
else:
self.getSubscriber(service)['backLog'][0] = data
self.executeImmediateCallback(self.getSubscriber(service))
else:
self.getSubscriber(service)['backLog'].append(data)
if self.running == False:
logging.info("**Start Fakegato-Timer {0} with {1} minutes inverval**".format(self.accessoryName, self.minutes))
self.running = True
self.cancel_future_calls = call_repeatedly(self.minutes*60, self.executeCallbacks)
def emptyData(self, service):
#logging.info("**Fakegato-timer: emptyData ** {0} ".format(service.accessoryName))
source = self.getSubscriber(service)
if len(source['backLog']) != 0:
source['previousBackLog'] = source['backLog']
source['backLog'] = []