Skip to content
Snippets Groups Projects
Commit 55e8ec8c authored by Andreas Gattringer's avatar Andreas Gattringer
Browse files

switched communication to use device types and messages

parent d7902dc7
No related branches found
No related tags found
No related merge requests found
import sys
from umnp.devices import DEVICE_TYPE_RHTP
from umnp.microcontroller.devices.network.ethernet_w5500 import EthernetW5500
from umnp.microcontroller.measurementdevice import MeasurementDevice
from umnp.microcontroller.sensors.lps28dfw import LPS28DFW
from umnp.microcontroller.sensors.sht45 import SHT45
from umnp.microcontroller.tasks.periodictask import PeriodicTask
from umnp.protocol.data_message import DataMessage
if sys.implementation.name == "micropython":
# noinspection PyUnresolvedReferences
......@@ -20,15 +22,17 @@ async def aggregate_and_send(
device: MeasurementDevice, sht45: SHT45, p_sensor: LPS28DFW
):
comm = device.communicator
t, rh = await sht45.measure()
p, p_t = await p_sensor.measure()
data = f"{t},{rh},{p},{p_t}"
await comm.send_data_message(data)
msg = DataMessage(data, device.identifier_raw, device.device_type)
await comm.send_message(msg)
async def main():
# configure network
device = MeasurementDevice()
device = MeasurementDevice(device_type=DEVICE_TYPE_RHTP)
spi = machine.SPI(
0, 2_000_000, mosi=machine.Pin(19), miso=machine.Pin(16), sck=machine.Pin(18)
)
......
DEVICE_TYPE_UNKNOWN = 0
DEVICE_TYPE_RHTP = 1
......@@ -3,6 +3,7 @@ import time
from umnp.microcontroller.devices.network.udp import UDPSender, UDPReceiver
from umnp.microcontroller.tasks.periodictask import PeriodicTask
from umnp.protocol.message import Message
if sys.implementation.name == "micropython":
# noinspection PyUnresolvedReferences
......@@ -12,12 +13,19 @@ if sys.implementation.name == "micropython":
import machine
else:
import asyncio
from umnp.microcontroller.umock import machine
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from umnp.microcontroller.measurementdevice import MeasurementDevice
class UDPCommunicator:
def __init__(
self, sender: UDPSender, receiver: UDPReceiver, device_id, max_msgs: int = 10
self,
sender: UDPSender,
receiver: UDPReceiver,
device: "MeasurementDevice",
max_msgs: int = 10,
):
self._receive_lock = asyncio.Lock()
self._send_lock = asyncio.Lock()
......@@ -28,7 +36,7 @@ class UDPCommunicator:
self._sender = sender
self._receiver = receiver
self._tasks = {}
self._device_id = device_id
self._device = device
async def queue_incoming_message(self, msg, source):
async with self._receive_lock:
......@@ -49,43 +57,30 @@ class UDPCommunicator:
await asyncio.sleep(0.500)
async def send_task(self):
device_id = self._device_id
rtc = machine.RTC()
while True:
msg = None
async with self._send_lock:
if len(self._messages_send_queue) > 0:
msg = self._messages_send_queue.pop()
if msg is not None:
msg = msg.replace(",", ";")
now = rtc.datetime()
now = "%04d-%02d-%02dT%02d:%02d:%02d" % (
now[0],
now[1],
now[2],
now[4],
now[5],
now[6],
)
await self._sender.broadcast("%s,%s,%s" % (device_id, now, msg))
if msg is None:
await asyncio.sleep(0.5)
continue
if isinstance(msg, Message):
await self._sender.broadcast(msg.encode())
await asyncio.sleep(0.5)
async def send_data_message(self, data: str):
async with self._send_lock:
self._messages_send_queue.append(data)
async def send_message(self, msg: Message):
self._messages_send_queue.append(msg)
async def control_task(self):
while True:
async with self._receive_lock:
# print("Control: %d" % len(self.msgs))
msg = await self.get_newest_message()
if msg is not None:
pass
# print("Controll::msg::", msg)
await asyncio.sleep(0.5)
async def start(self):
......
......@@ -2,6 +2,7 @@ import binascii
import sys
import time
from umnp.devices import DEVICE_TYPE_UNKNOWN
from umnp.microcontroller.communication.udp_communicator import UDPCommunicator
from umnp.microcontroller.devices.network.udp import (
UDPSender,
......@@ -19,7 +20,7 @@ else:
class MeasurementDevice:
def __init__(self):
def __init__(self, device_type: int = DEVICE_TYPE_UNKNOWN):
self._boot_time = time.time()
self._identifier_raw = machine.unique_id()
self._identifier = binascii.hexlify(self.identifier_raw).decode(
......@@ -29,6 +30,11 @@ class MeasurementDevice:
self._sender = None
self._receiver = None
self._communicator = None
self._type = device_type
@property
def device_type(self):
return self._type
@property
def boot_time(self):
......
from umnp.protocol.data_message import DataMessage
from umnp.protocol.messagetype import MessageType
from umnp.protocol.register_messages import register_messages
register_messages(MessageType.MSG_DEVICE_DATA, DataMessage)
......@@ -49,8 +49,8 @@ class Message:
@classmethod
def add_message_type(cls, msg_type: int, msg: typing.Type["Message"]):
if msg_type in cls._registered_types:
logging.info(f"Already registered {msg_type}")
return
print(f"Registering message type {msg_type}")
cls._registered_types[msg_type] = msg
@classmethod
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment