-
Notifications
You must be signed in to change notification settings - Fork 30
/
test_app.py
94 lines (65 loc) · 2.2 KB
/
test_app.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
from PySide6.QtCore import QObject, Signal, QTimer
from random import randrange, randint
import uuid
from openhrv.utils import get_sensor_address
class MockBluetoothMac:
def __init__(self, mac):
self._mac = mac
def toString(self):
return self._mac
class MockBluetoothUuid:
def __init__(self, uuid):
self._uuid = uuid
def toString(self):
return f"{self._uuid}"
class MockSensor:
def __init__(self):
self._mac = MockBluetoothMac(
":".join([f"{randint(0, 255):02x}" for _ in range(6)])
)
self._uuid = MockBluetoothUuid(uuid.uuid4())
self._name = "MockSensor"
def name(self):
return self._name
def address(self):
return self._mac
def deviceUuid(self):
return self._uuid
class MockSensorScanner(QObject):
sensor_update = Signal(object)
status_update = Signal(str)
def scan(self):
polar_sensors = [MockSensor() for _ in range(3)]
self.sensor_update.emit(polar_sensors)
self.status_update.emit(f"Found {len(polar_sensors)} sensor(s).")
class MockSensorClient(QObject):
ibi_update = Signal(object)
status_update = Signal(object)
def __init__(self):
super().__init__()
self.timer = QTimer()
self.timer.setInterval(1000)
self.timer.timeout.connect(self.simulate_ibi)
def connect_client(self, sensor):
self.status_update.emit(
f"Connecting to sensor at {get_sensor_address(sensor)}."
)
self.timer.start()
def disconnect_client(self):
self.status_update.emit("Disconnecting from sensor.")
self.timer.stop()
def simulate_ibi(self):
self.ibi_update.emit(randrange(700, 1400))
def main():
"""Mock sensor classes.
Mock classes need to replace their mocked counterparts in namespace before
the latter are imported elsewhere:
https://stackoverflow.com/questions/3765222/monkey-patch-python-class
"""
from openhrv import sensor # noqa
sensor.SensorClient = MockSensorClient
sensor.SensorScanner = MockSensorScanner
from openhrv.app import main as mock_main # noqa
mock_main()
if __name__ == "__main__":
main()