This repository has been archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor_readout.py
158 lines (116 loc) · 3.77 KB
/
sensor_readout.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
from random import randint
from threading import Lock
from _thread import start_new_thread
from os.path import exists
from asyncio import sleep
from ctypes import cdll
from utils import is_wallaby
from highway import log as logging
ANALOG = 1
DIGITAL = 2
NAMED_MODES = {ANALOG : "analog", DIGITAL : "digital"}
REVERSE_NAMED_MODES = {v : k for k, v in NAMED_MODES.items()}
MODES = tuple(NAMED_MODES.keys())
CONFIG_PATH = "fl0w.cfg"
IS_WALLABY = is_wallaby()
LIB_WALLABY = "/usr/lib/libwallaby.so"
READOUT_TEMPLATE = {
ANALOG : [],
DIGITAL : []
}
def valid_port(port, mode):
if mode == ANALOG:
return port >= 0 and port <= 5
elif mode == DIGITAL:
return port >= 0 and port <= 9
return False
class SensorReadout:
def __init__(self, poll_rate=0.2):
self.poll_rate = poll_rate
self.handler_lock = Lock()
self.handlers = {}
self.running = True
self.readout_required = READOUT_TEMPLATE.copy()
# Wallaby library avaliable?
if not exists(LIB_WALLABY):
logging.warning("Wallaby library unavaliable.")
self.get_sensor_value = self.__get_random_value
else:
self.wallaby_library = cdll.LoadLibrary(LIB_WALLABY)
self.get_sensor_value = self.__get_sensor_value
async def run(self):
while self.running:
current_values = {
ANALOG : {},
DIGITAL : {}
}
for mode in MODES:
for port in self.readout_required[mode]:
current_values[mode][port] = self.get_sensor_value(port, mode)
self.handler_lock.acquire()
for handler in self.handlers:
readouts = 0
response = {ANALOG : {}, DIGITAL : {}}
for mode in MODES:
for port in self.handlers[handler][mode]:
response[mode][port] = current_values[mode][port]
readouts += 1
if readouts != 0:
await handler.send(response, "sensor_stream")
self.handler_lock.release()
await sleep(self.poll_rate)
def stop(self):
self.running = False
def subscribe(self, port, mode, handler):
if port not in self.readout_required[mode]:
self.readout_required[mode].append(port)
if not handler in self.handlers:
self.handler_lock.acquire()
self.handlers[handler] = READOUT_TEMPLATE.copy()
self.handler_lock.release()
self.handler_lock.acquire()
self.handlers[handler][mode].append(port)
self.handler_lock.release()
def unsubscribe(self, port, mode, handler):
if handler in self.handlers:
if port in self.handlers[handler][mode]:
self.handler_lock.acquire()
del self.handlers[handler][mode][ \
self.handlers[handler][mode].index(port)]
self.handler_lock.release()
self.determine_required_readouts()
def determine_required_readouts(self):
readout_required = READOUT_TEMPLATE.copy()
self.handler_lock.acquire()
for handler in self.handlers:
for mode in MODES:
for port in self.handlers[handler][mode]:
if not port in readout_required[mode]:
readout_required[mode].append(port)
self.handler_lock.release()
self.readout_required = readout_required
async def unsubscribe_all(self, handler):
successful = False
if handler in self.handlers:
self.handler_lock.acquire()
del self.handlers[handler]
self.handler_lock.release()
successful = True
self.determine_required_readouts()
await handler.send(successful, "sensor_unsubscribe")
def handler_disconnected(self, handler):
if handler in self.handlers:
self.handler_lock.acquire()
del self.handlers[handler]
self.handler_lock.release()
self.determine_required_readouts()
def __get_sensor_value(self, port, mode):
if mode == SensorReadout.ANALOG:
return self.wallaby_library.analog(port)
elif mode == SensorReadout.DIGITAL:
return self.wallaby_library.digital(port)
def __get_random_value(self, port, mode):
if mode == ANALOG:
return randint(0, 4095)
elif mode == DIGITAL:
return randint(0, 1)