Skip to content

Commit

Permalink
Fixed a failing test on Protocol taking an id
Browse files Browse the repository at this point in the history
  • Loading branch information
Aiky30 committed Dec 8, 2024
1 parent 0916f50 commit cf8283f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import time
from typing import Optional

import requests
from shed_pi_module_utils.base_protocol import BaseProtocol
from shed_pi_module_utils.data_submission import (
ReadingSubmissionService,
Expand All @@ -13,16 +15,24 @@


class DeviceProtocol(BaseProtocol):
def __init__(self, submission_service: ReadingSubmissionService):
def __init__(
self,
submission_service: ReadingSubmissionService,
temp_probe_device_id: Optional[int] = None,
):
# Installed modules
self.temp_probe = TempProbe(submission_service=submission_service)
self.temp_probe = TempProbe()
self.rpi_device = RPIDevice(
submission_service=submission_service,
device_module_id=None,
cpu_module_id=None,
)
self.submission_delay = TIME_TO_SLEEP

# FIXME: Part of the migration of submission service out of the probe driver
self.submission_service = submission_service
self.temp_probe_device_id = temp_probe_device_id

def stop(self):
return False

Expand All @@ -32,14 +42,31 @@ def startup(self):
def run(self):
while not self.stop():
# TODO: Would be nice to be able to bundle multiple calls into 1, less of an issue initially
self.temp_probe.submit_reading()
self.submit_reading()
self.rpi_device.submit_reading()

time.sleep(self.submission_delay)

def shutdown(self):
self.rpi_device.submit_device_shutdown()

def submit_reading(self) -> requests.Response:
"""
Submits a reading to an external endpoint
:return:
"""
probe_1_temp = self.temp_probe.read_temp()

# FIXME: Should this be a float or a string? Broke the test
data = {"temperature": str(probe_1_temp)}

response = self.submission_service.submit(
device_module_id=self.temp_probe_device_id, data=data
)

return response


def main():
if not check_arch_is_arm():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from unittest.mock import Mock, patch
from unittest.mock import Mock

import pytest
from shed_pi_module_utils.data_submission import (
Expand All @@ -14,17 +14,8 @@
)


@patch("standalone_modules.temperature_module.temperature_probe.Path")
@pytest.mark.django_db
def test_device_protocol(mocked_path, live_server):
# Submission service
submission_service = ReadingSubmissionService()
submission_service.base_url = live_server.url
# Device Protocol
device_protocol = DeviceProtocol(submission_service=submission_service)
# Override the loop timer for the test to end instantly
device_protocol.submission_delay = 0
device_protocol.stop = Mock(side_effect=[False, True])
def test_device_protocol(temp_probe_path, live_server):
# Temp probe
schema = {
"$id": "https://example.com/person.schema.json",
Expand All @@ -36,7 +27,17 @@ def test_device_protocol(mocked_path, live_server):
},
}
temp_probe = DeviceModuleFactory(schema=schema)
device_protocol.temp_probe.device_id = temp_probe.id
# Submission service
submission_service = ReadingSubmissionService()
submission_service.base_url = live_server.url
# Device Protocol
device_protocol = DeviceProtocol(
submission_service=submission_service, temp_probe_device_id=temp_probe.id
)
# Override the loop timer for the test to end instantly
device_protocol.submission_delay = 0
device_protocol.stop = Mock(side_effect=[False, True])

device_protocol.temp_probe.read_temp_raw = Mock(
return_value=[
"YES",
Expand Down
2 changes: 2 additions & 0 deletions standalone_modules/temperature_module/device_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def __init__(self, submission_service: ReadingSubmissionService):
self.temp_probe = TempProbe(submission_service=submission_service)
self.submission_delay = TIME_TO_SLEEP
self.submission_service = submission_service

# FIXME:
self.device_id: int = None

def stop(self):
Expand Down

0 comments on commit cf8283f

Please sign in to comment.