-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.py
72 lines (55 loc) · 1.95 KB
/
server.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
import time
import json
import zmq
from patienttubingdescriptorcalculator \
import PatientTubingDescriptorCalculator
from sensors import Sensors
from tetra_constants import NUMBER_OF_PATIENTS
class Calculator():
def __init__(self):
self._calculators = (
tuple(PatientTubingDescriptorCalculator(time.time())
for _ in range(NUMBER_OF_PATIENTS)))
def add_datum(self, datum):
for i in range(len(self._calculators)):
self._calculators[i].add_pressure_datum(datum[i][0])
if len(datum[i]) > 1:
self._calculators[i].add_flow_rate_datum(datum[i][1],
time.time())
def get_datum(self):
return {f"patient-{i}": self._calculators[i].descriptors
for i in range(len(self._calculators))}
class Communicator():
def __init__(self, port=5000):
self._context = zmq.Context()
self._socket = self._context.socket(zmq.PUB)
self._socket.bind(f"tcp://*:{port}")
self._closed = False
def close(self):
if not self._closed:
self._socket.close()
self._context.term()
self._closed = True
def publish_message(self, message):
self._socket.send_multipart([b"",
json.dumps(message).encode("ascii")])
def main():
sensors = Sensors()
calculator = Calculator()
communicator = Communicator()
running = True
while running:
try:
start_time = time.time()
data = sensors.poll()
calculator.add_datum(data)
datum = calculator.get_datum()
communicator.publish_message(datum)
while (time.time() - start_time < 1.0):
time.sleep(0.1)
except KeyboardInterrupt:
running = False
sensors.close()
communicator.close()
if "__main__" == __name__:
main()