-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
92 lines (69 loc) · 2.18 KB
/
model.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
import threading
import time
import Queue
from collections import namedtuple
from threading import _Event
class ComponentModel(object):
def __init__(self):
# self._tick = 1.0 #seconds
self._running = False
self._queue = Queue.Queue()
self._thread = threading.Thread(target=self._loop)
def get_current(self):
raise NotImplementedError()
def _handle_event(self, event):
raise NotImplementedError()
def _update(self):
raise NotImplementedError()
def _loop(self):
while self._running:
try:
event = self._queue.get_nowait()
self._handle_event(event)
except Queue.Empty:
pass
self._update()
# time.sleep(self._tick)
def on_event(self, event):
self._queue.put(event)
def start(self):
if self._running:
return
self._running = True
self._thread.start()
print '{s.__class__} started'.format(s=self)
def stop(self):
if not self._running:
return
self._running = False
self._thread.join()
print '{s.__class__} halt'.format(s=self)
class Model(ComponentModel):
def __init__(self, device):
super(self.__class__, self).__init__()
self._components = {}
self.device = device
def add_component(self, event, component):
self._components[event] = component
def get_current(self):
return sum([c.get_current() for c in self._components.values()])
def _handle_event(self, event):
# TODO: accept regex
# print '_handle_event {}'.format(event)
for k, v in self._components.items():
if k in event:
v.on_event(k)
def _update(self):
pass
def start(self):
for component in self._components.values():
component.start()
super(self.__class__, self).start()
def run(self):
self.start()
self.device.run(model=self)
def stop(self):
for component in self._components.values():
component.stop()
super(self.__class__, self).stop()
#self.device.stop()