From c473cc32cebf3a14a912dbb9832576ea54a4ed7b Mon Sep 17 00:00:00 2001 From: Maxim Andreev Date: Mon, 7 Jun 2021 21:20:18 +0300 Subject: [PATCH] use absolute datetime --- .flake8 | 1 + example.py | 8 ++++++-- radiacode/decoders/databuf.py | 15 ++++++++------- radiacode/decoders/spectrum.py | 4 +++- radiacode/radiacode.py | 15 +++++++++------ radiacode/transports/bluetooth.py | 12 ++++++++++-- radiacode/types.py | 13 +++++++------ 7 files changed, 44 insertions(+), 24 deletions(-) diff --git a/.flake8 b/.flake8 index 8394637..15e5415 100644 --- a/.flake8 +++ b/.flake8 @@ -63,3 +63,4 @@ ignore = WPS114, # underscore in name WPS336, # explicit string concat WPS432, # magic numbers + WPS237, # too complex `f` string diff --git a/example.py b/example.py index a29eca7..ed41240 100644 --- a/example.py +++ b/example.py @@ -1,4 +1,6 @@ import argparse +import sys +import time from radiacode import RadiaCode @@ -24,8 +26,10 @@ def main(): print('--------') print('### DataBuf:') - for v in rc.data_buf(): - print(v) + while True: + for v in rc.data_buf(): + print(v.dt.isoformat(), v) + time.sleep(2) if __name__ == '__main__': diff --git a/radiacode/decoders/databuf.py b/radiacode/decoders/databuf.py index 66c639d..227eeca 100644 --- a/radiacode/decoders/databuf.py +++ b/radiacode/decoders/databuf.py @@ -1,15 +1,16 @@ +import datetime from typing import List, Union from radiacode.bytes_buffer import BytesBuffer from radiacode.types import CountRate, DoseRate, DoseRateDB, Event, RareData -def decode_VS_DATA_BUF(br: BytesBuffer) -> List[Union[CountRate, DoseRate, DoseRateDB, RareData, Event]]: +def decode_VS_DATA_BUF(br: BytesBuffer, base_time: datetime.datetime) -> List[Union[CountRate, DoseRate, DoseRateDB, RareData, Event]]: ret: List[Union[CountRate, DoseRate, DoseRateDB, RareData, Event]] = [] next_seq = None while br.size() > 0: seq, eid, gid, ts_offset = br.unpack(' List[Union[CountRate, DoseRate, DoseR if eid == 0 and gid == 0: # GRP_CountRate count, count_rate, flags = br.unpack(' List[Union[CountRate, DoseRate, DoseR elif eid == 0 and gid == 1: # GRP_DoseRate dose_rate, dose_rate_err, flags = br.unpack(' List[Union[CountRate, DoseRate, DoseR elif eid == 0 and gid == 2: # GRP_DoseRateDB count, count_rate, dose_rate, dose_rate_err, flags = br.unpack(' List[Union[CountRate, DoseRate, DoseR elif eid == 0 and gid == 3: # GRP_RareData duration, dose, temperature, charge_level, flags = br.unpack(' List[Union[CountRate, DoseRate, DoseR elif eid == 0 and gid == 7: # GRP_Event event, event_param1, flags = br.unpack(' Spectrum: while br.size() > 0: counts.append(br.unpack(' datetime.datetime: + return self._base_time def execute(self, reqtype: bytes, args: bytes = None) -> BytesBuffer: assert len(reqtype) == 2 @@ -43,7 +46,7 @@ def execute(self, reqtype: bytes, args: bytes = None) -> BytesBuffer: response = self._connection.execute(full_request) resp_header = response.unpack('<4s')[0] - assert req_header == resp_header, f'req={str(req_header)} resp={str(resp_header)}' + assert req_header == resp_header, f'req={req_header.hex()} resp={resp_header.hex()}' return response def read_request(self, command_id: Union[int, VS, VSFR]) -> BytesBuffer: @@ -114,11 +117,11 @@ def commands(self) -> str: # called with 0 after init! def device_time(self, v: int) -> None: - self.write_request(VSFR.DEVICE_TIME, struct.pack(' List[Union[CountRate, DoseRate, DoseRateDB, RareData, Event]]: r = self.read_request(VS.DATA_BUF) - return decode_VS_DATA_BUF(r) + return decode_VS_DATA_BUF(r, self._base_time) def spectrum(self) -> Spectrum: r = self.read_request(VS.SPECTRUM) diff --git a/radiacode/transports/bluetooth.py b/radiacode/transports/bluetooth.py index ce9b990..a8a22b2 100644 --- a/radiacode/transports/bluetooth.py +++ b/radiacode/transports/bluetooth.py @@ -1,17 +1,25 @@ import struct -from bluepy.btle import DefaultDelegate, Peripheral +from bluepy.btle import BTLEDisconnectError, DefaultDelegate, Peripheral from radiacode.bytes_buffer import BytesBuffer +class DeviceNotFound(Exception): + pass + + class Bluetooth(DefaultDelegate): def __init__(self, mac): self._resp_buffer = b'' self._resp_size = 0 self._response = None - self.p = Peripheral(mac) + try: + self.p = Peripheral(mac) + except BTLEDisconnectError: + raise DeviceNotFound('Device not found or bluetooth adapter is not powered on') + self.p.withDelegate(self) service = self.p.getServiceByUUID('e63215e5-7003-49d8-96b0-b024798fb901') diff --git a/radiacode/types.py b/radiacode/types.py index c078fa1..5265652 100644 --- a/radiacode/types.py +++ b/radiacode/types.py @@ -1,3 +1,4 @@ +import datetime from dataclasses import dataclass from enum import Enum from typing import List @@ -5,7 +6,7 @@ @dataclass class CountRate: - timestamp: float + dt: datetime.datetime count: int count_rate: int flags: int @@ -13,7 +14,7 @@ class CountRate: @dataclass class DoseRate: - timestamp: float + dt: datetime.datetime dose_rate: int dose_rate_err: int flags: int @@ -21,7 +22,7 @@ class DoseRate: @dataclass class DoseRateDB: - timestamp: float + dt: datetime.datetime count: int count_rate: int dose_rate: int @@ -31,7 +32,7 @@ class DoseRateDB: @dataclass class RareData: - timestamp: float + dt: datetime.datetime duration: int # for dose, in seconds dose: int temperature: int @@ -41,7 +42,7 @@ class RareData: @dataclass class Event: - timestamp: float + dt: datetime.datetime event: int event_param1: int flags: int @@ -49,7 +50,7 @@ class Event: @dataclass class Spectrum: - timestamp: float + duration: datetime.timedelta a0: float a1: float a2: float