-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:Aiky30/shed-pi into feature/paginat…
…ed-endpoints
- Loading branch information
Showing
43 changed files
with
842 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
shedpi_hub_dashboard/migrations/0002_alter_devicemodulereading_created_at.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 5.0.1 on 2024-04-19 18:32 | ||
|
||
import django.utils.timezone | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("shedpi_hub_dashboard", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="devicemodulereading", | ||
name="created_at", | ||
field=models.DateTimeField(default=django.utils.timezone.now), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
54 changes: 54 additions & 0 deletions
54
shedpi_hub_dashboard/tests/integration/test_module_reading_submission.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import json | ||
from unittest.mock import Mock, patch | ||
|
||
import pytest | ||
from rest_framework import status | ||
|
||
from shedpi_hub_dashboard.models import DeviceModuleReading | ||
from shedpi_hub_dashboard.tests.utils.factories import ( | ||
DeviceModuleFactory, | ||
) | ||
from standalone_modules.shed_pi_module_utils.data_submission import ( | ||
ReadingSubmissionService, | ||
) | ||
from standalone_modules.temperature_module.temperature_probe import ( | ||
TempProbe, | ||
) | ||
|
||
|
||
@patch("standalone_modules.temperature_module.temperature_probe.Path") | ||
@pytest.mark.django_db | ||
def test_temperature_module_reading_submission(mocked_path, live_server): | ||
schema = { | ||
"$id": "https://example.com/person.schema.json", | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"title": "Reading", | ||
"type": "object", | ||
"properties": { | ||
"temperature": {"type": "string", "description": "The Temperature"}, | ||
}, | ||
} | ||
device_module = DeviceModuleFactory(schema=schema) | ||
submission_service = ReadingSubmissionService() | ||
# Override the serviuce url | ||
submission_service.base_url = live_server.url | ||
|
||
probe = TempProbe(submission_service=submission_service) | ||
# Override the module id | ||
probe.device_id = device_module.id | ||
|
||
probe.read_temp_raw = Mock( | ||
return_value=[ | ||
"YES", | ||
"t=12345", | ||
] | ||
) | ||
|
||
response = probe.submit_reading() | ||
|
||
assert response.status_code == status.HTTP_201_CREATED | ||
|
||
response_data = json.loads(response.text) | ||
|
||
assert "created_at" in response_data | ||
assert DeviceModuleReading.objects.filter(device_module=device_module).count() == 1 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import logging | ||
import os | ||
|
||
import requests | ||
|
||
from standalone_modules.shed_pi_module_utils.data_submission import ( | ||
ReadingSubmissionService, | ||
) | ||
from standalone_modules.shed_pi_module_utils.utils import get_time | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
MODULE_VERSION = "0.0.1" | ||
|
||
|
||
class RPIDevice: | ||
def __init__( | ||
self, | ||
submission_service: ReadingSubmissionService, | ||
device_module_id: int, | ||
cpu_module_id: int, | ||
) -> None: | ||
self.device_module_id = device_module_id | ||
self.cpu_module_id = cpu_module_id | ||
self.submission_service = submission_service | ||
|
||
def get_cpu_temp(self): | ||
cpu_temp = os.popen("vcgencmd measure_temp").readline() | ||
|
||
# Convert the temp read from the OS to a clean float | ||
return float(cpu_temp.replace("temp=", "").replace("'C\n", "")) | ||
|
||
def submit_reading(self) -> requests.Response: | ||
""" | ||
Submits a reading to an external endpoint | ||
:return: | ||
""" | ||
cpu_temp = self.get_cpu_temp() | ||
|
||
# FIXME: Should this be a float or a string? Broke the test | ||
data = {"temperature": str(cpu_temp)} | ||
|
||
response = self.submission_service.submit( | ||
device_module_id=self.device_module_id, data=data | ||
) | ||
|
||
return response | ||
|
||
def submit_device_startup(self): | ||
logger.info(f"Shed pi started: {get_time()}, using version: {MODULE_VERSION}") | ||
|
||
data = {"power": True} | ||
response = self.submission_service.submit( | ||
device_module_id=self.device_module_id, data=data | ||
) | ||
return response | ||
|
||
def submit_device_shutdown(self): | ||
data = {"power": False} | ||
response = self.submission_service.submit( | ||
device_module_id=self.device_module_id, data=data | ||
) | ||
return response |
Oops, something went wrong.