Skip to content

Commit 0aa4fd1

Browse files
committed
fix: Fixed the test for the example ready to test on hardware
1 parent da14ab8 commit 0aa4fd1

4 files changed

Lines changed: 60 additions & 36 deletions

File tree

standalone_modules/rpi/device.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ class RPIDevice:
1616
def __init__(
1717
self,
1818
submission_service: ReadingSubmissionService,
19-
device_module_id: int,
20-
cpu_module_id: int,
19+
device_module_id: str,
20+
cpu_module_id: str,
2121
) -> None:
2222
self.device_module_id = device_module_id
2323
self.cpu_module_id = cpu_module_id
2424
self.submission_service = submission_service
2525

26-
def get_cpu_temp(self):
26+
def get_cpu_temp(self) -> float:
2727
cpu_temp = os.popen("vcgencmd measure_temp").readline()
2828

2929
# Convert the temp read from the OS to a clean float
@@ -36,12 +36,10 @@ def submit_reading(self) -> requests.Response:
3636
:return:
3737
"""
3838
cpu_temp = self.get_cpu_temp()
39-
40-
# FIXME: Should this be a float or a string? Broke the test
4139
data = {"temperature": str(cpu_temp)}
4240

4341
response = self.submission_service.submit(
44-
device_module_id=self.device_module_id, data=data
42+
device_module_id=self.cpu_module_id, data=data
4543
)
4644

4745
return response
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[server]
2+
url = http://localhost:8000
3+
4+
[device]
5+
module_id = 0
6+
7+
[external_temp]
8+
module_id = 0
9+
10+
[cpu_temp]
11+
module_id = 0

standalone_modules/shed_pi_example_device_installation/device_protocol.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import configparser
12
import time
2-
from typing import Optional
33

44
import requests
55
from shed_pi_module_utils.base_protocol import BaseProtocol
@@ -18,23 +18,32 @@ class DeviceProtocol(BaseProtocol):
1818
def __init__(
1919
self,
2020
submission_service: ReadingSubmissionService,
21-
temp_probe_device_id: Optional[int] = None,
2221
):
22+
config = self.get_config()
23+
device_module_id: str = config["device"]["module_id"]
24+
ext_temp_module_id: str = config["external_temp"]["module_id"]
25+
cpu_temp_module_id: str = config["cpu_temp"]["module_id"]
26+
2327
# Installed modules
2428
self.temp_probe = TempProbe()
2529
self.rpi_device = RPIDevice(
2630
submission_service=submission_service,
27-
device_module_id=None,
28-
cpu_module_id=None,
31+
device_module_id=device_module_id,
32+
cpu_module_id=cpu_temp_module_id,
2933
)
3034
self.submission_delay = TIME_TO_SLEEP
3135

32-
# FIXME: Part of the migration of submission service out of the probe driver
3336
self.submission_service = submission_service
34-
self.temp_probe_device_id = temp_probe_device_id
37+
self.temp_probe_device_id = ext_temp_module_id
38+
39+
def get_config(self) -> dict:
40+
config = configparser.ConfigParser()
41+
config.read("config.ini")
42+
return config
3543

3644
def stop(self):
37-
# FIXKE: This should be a threading event, to break when the execution is terminated, prevents leaving threads behind
45+
# FIXME: This should be a threading event, to break when the execution is terminated,
46+
# prevents leaving threads behind
3847
return False
3948

4049
def startup(self):
@@ -51,7 +60,7 @@ def run(self):
5160
def shutdown(self):
5261
self.rpi_device.submit_device_shutdown()
5362

54-
def get_reading(self) -> bytes:
63+
def get_reading(self) -> float:
5564
"""
5665
Useful for collecting many readings from different modules, rather than submitting all
5766
at once
@@ -65,8 +74,6 @@ def submit_reading(self) -> requests.Response:
6574
:return:
6675
"""
6776
probe_1_temp = self.get_reading()
68-
69-
# FIXME: Should this be a float or a string? Broke the test
7077
data = {"temperature": str(probe_1_temp)}
7178

7279
response = self.submission_service.submit(

standalone_modules/shed_pi_example_device_installation/tests/unit/test_device_protocol.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from unittest.mock import Mock
1+
from unittest.mock import Mock, patch
22

33
import pytest
44
from shed_pi_module_utils.data_submission import (
@@ -27,23 +27,6 @@ def test_device_protocol(temp_probe_path, live_server):
2727
},
2828
}
2929
temp_probe = DeviceModuleFactory(schema=schema)
30-
# Submission service
31-
submission_service = ReadingSubmissionService()
32-
submission_service.base_url = live_server.url
33-
# Device Protocol
34-
device_protocol = DeviceProtocol(
35-
submission_service=submission_service, temp_probe_device_id=temp_probe.id
36-
)
37-
# Override the loop timer for the test to end instantly
38-
device_protocol.submission_delay = 0
39-
device_protocol.stop = Mock(side_effect=[False, True])
40-
41-
device_protocol.temp_probe.read_temp_raw = Mock(
42-
return_value=[
43-
"YES",
44-
"t=12345",
45-
]
46-
)
4730
# RPI CPU temp probe
4831
rpi_schema = {
4932
"$id": "https://example.com/person.schema.json",
@@ -55,10 +38,35 @@ def test_device_protocol(temp_probe_path, live_server):
5538
},
5639
}
5740
rpi_cpu_temp = DeviceModuleFactory(schema=rpi_schema)
58-
device_protocol.rpi_device.device_module_id = rpi_cpu_temp.id
41+
# Submission service
42+
submission_service = ReadingSubmissionService()
43+
submission_service.base_url = live_server.url
44+
config = {
45+
"device": {
46+
"module_id": "",
47+
},
48+
"external_temp": {
49+
"module_id": temp_probe.id,
50+
},
51+
"cpu_temp": {"module_id": rpi_cpu_temp.id},
52+
}
53+
with patch.object(DeviceProtocol, "get_config", Mock(return_value=config)):
54+
# Device Protocol
55+
device_protocol = DeviceProtocol(submission_service=submission_service)
56+
57+
# Override the loop timer for the test to end instantly
58+
# FIXME: This is just a patched sleep!
59+
device_protocol.submission_delay = 0
60+
device_protocol.stop = Mock(side_effect=[False, True])
61+
device_protocol.temp_probe.read_temp = Mock(
62+
return_value=30.00,
63+
)
5964
device_protocol.rpi_device.get_cpu_temp = Mock(return_value=10.0)
6065

61-
device_protocol.run()
66+
# FIXME: Would be better if we could run for a cycle and exit to prove out the run
67+
# method
68+
device_protocol.submit_reading()
69+
device_protocol.rpi_device.submit_reading()
6270

6371
# Check that the data was submitted
6472
assert DeviceModuleReading.objects.filter(device_module=rpi_cpu_temp).count() == 1

0 commit comments

Comments
 (0)