forked from hwinther/homeautomation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
habase.py
71 lines (58 loc) · 2.77 KB
/
habase.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
#!/usr/bin/python
import logging, threading, time, json
from hacommon import QueueList, ThreadList
class HomeAutomationThread(threading.Thread):
webservice_definitions = None
def __init__(self, name, callback_function):
threading.Thread.__init__(self)
self.stop_event = threading.Event()
self.setName(name)
self.callback_function = callback_function
def finalize(self):
if self.callback_function != None:
self.callback_function()
def get_json_status(self):
return json.dumps({self.get_class_name(): []})
def get_class_name(self):
return self.__class__.__name__
class HomeAutomationQueueThread(HomeAutomationThread):
def __init__(self, name, callback_function, queue, threadlist):
HomeAutomationThread.__init__(self, name, callback_function)
if queue == None: queue = QueueList()
self.queue = queue
if threadlist == None: threadlist = ThreadList()
self.threadlist = threadlist
def pre_processqueue(self): #Have the subclasses override processqueue and call super afterwards to get the same effect
pass #bad naming, before processing queue items at all
def processqueue(self):
self.pre_processqueue()
clsname = self.get_class_name()
while not self.stop_event.is_set():
for item in [i for i in self.queue if i.cls == clsname]:
#TODO: should really be True.. but this will be used until the modules return the right default value via decorator
#logging.debug('Exec queue item: ' + `item`)
item_return_value = self.exec_item(item)
#logging.debug('Exec queue item ret: ' + `item_return_value`)
if item_return_value == None or item_return_value == True:
self.queue.remove(item)
elif item_return_value == False:
pass #handle this again later
else:
#try to translate the str in item into a function call within this context
logging.debug('attempting to translate function call in deserialized queue item: ' + item.func)
res = self
for x in item.func.split('.'):
res = getattr(res, x)
logging.debug('res = ' + str(res))
item.func = res #let it be handled on the next iteration..
self.post_processqueue()
time.sleep(0.1)
self.finalize()
def run(self):
self.processqueue()
def post_processqueue(self):
pass
def get_class_name(self):
return self.__class__.__name__
def exec_item(self, item):
return item()