2024-05-31 14:58:46 +03:00
|
|
|
from dataclasses import dataclass
|
2024-05-31 15:09:48 +03:00
|
|
|
from typing import Any, Iterable
|
2024-05-31 14:58:46 +03:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class SensorData:
|
|
|
|
mac: str
|
|
|
|
value: Any
|
|
|
|
time: int | None = None
|
|
|
|
name: str | None = None
|
2024-05-31 15:09:48 +03:00
|
|
|
|
|
|
|
|
|
|
|
def create_nm_req(
|
|
|
|
dev_mac: str,
|
|
|
|
dev_name: str | None = None,
|
2024-05-31 15:20:25 +03:00
|
|
|
sensors: Iterable[SensorData] = ()) -> bytes:
|
2024-05-31 15:09:48 +03:00
|
|
|
'''Generate a NarodMon format (#mac#value) request'''
|
|
|
|
|
|
|
|
res = '#' + dev_mac
|
|
|
|
if dev_name is not None:
|
|
|
|
res += '#' + dev_name
|
|
|
|
res += '\n'
|
|
|
|
|
|
|
|
for s in sensors:
|
|
|
|
res += f'#{s.mac}#{s.value}'
|
|
|
|
if s.time is not None:
|
|
|
|
res += f'#{s.time}'
|
|
|
|
if s.name is not None:
|
|
|
|
res += '#' + s.name
|
|
|
|
res += '\n'
|
|
|
|
|
2024-05-31 15:28:21 +03:00
|
|
|
res += '##'
|
|
|
|
|
2024-05-31 15:20:25 +03:00
|
|
|
return res.encode('utf-8')
|