From 2b6381fa46bb7fb5d43bc6fddf7801b8b89ffc81 Mon Sep 17 00:00:00 2001 From: peterlu14 Date: Tue, 18 Apr 2023 16:12:34 +0800 Subject: [PATCH] - devices table add new column calibration version - save calibration info into db compare with cali version --- python/biopro/db/device.py | 51 +++++++++++++++++++++++++++ python/biopro/device/device_cc2650.py | 19 ++++++++++ python/biopro/server/main.py | 41 +++++++++++---------- update_db_package.sh | 3 ++ 4 files changed, 96 insertions(+), 18 deletions(-) create mode 100644 python/biopro/db/device.py diff --git a/python/biopro/db/device.py b/python/biopro/db/device.py new file mode 100644 index 0000000..7049673 --- /dev/null +++ b/python/biopro/db/device.py @@ -0,0 +1,51 @@ +from sqlalchemy import Table, Column, String, MetaData, ForeignKey, JSON +from sqlalchemy.sql import select, func +from sqlalchemy.types import Integer, BigInteger, String, Boolean, TIMESTAMP, Numeric, Float, BINARY, LargeBinary +from sqlalchemy.dialects.postgresql import JSONB + +from .base import Base, Session + +class Device(Base): + __tablename__ = "devices" + + id = Column(Integer, primary_key=True) + name = Column(String(255)) + mac_address = Column(String(255)) + serial_number = Column(String(255)) + configuration = Column(JSONB) + library = Column(String(255)) + library_version = Column(String(255)) + device_version = Column(String(255)) + type = Column(String(255)) + battery = Column(Integer) + temperature = Column(Float) + auto_connect = Column(Boolean) + connect_priority = Column(Integer) + connect_time = Column(BigInteger) + parameter_set = Column(JSONB) + running = Column(Boolean) + calibration = Column(LargeBinary) + calibration_version = Column(Integer, default=-1) + user_auth = Column(JSONB) + deleted = Column(Boolean) + created_at = Column(TIMESTAMP(timezone=True), server_default=func.now()) + updated_at = Column(TIMESTAMP(timezone=True), onupdate=func.now()) + + @classmethod + def update_device(cls, device, options): + with Session() as session: + result = session.query(Device).filter(Device.mac_address == device['mac_address']).first() + for key, value in options.items(): + setattr(result, key, value) + session.commit() + + @classmethod + def get_device(cls, device): + with Session() as session: + result = session.query(Device).filter(Device.mac_address == device['mac_address']).first() + return result + + + + # def __repr__(self): + # return f"User(id={self.id!r}, name={self.name!r}, fullname={self.task!r})" \ No newline at end of file diff --git a/python/biopro/device/device_cc2650.py b/python/biopro/device/device_cc2650.py index e4be8c7..4f86244 100644 --- a/python/biopro/device/device_cc2650.py +++ b/python/biopro/device/device_cc2650.py @@ -350,6 +350,7 @@ class CC2650Device(Device): self._recording_file_name: str = 'recording_data' self._coeff: bytes = b'' self._device_version = "" + self._cali_version = -1 @property def device_id(self) -> int: @@ -549,6 +550,24 @@ class CC2650Device(Device): self.update_calibration_info(device_type) return self._coeff + + def update_cali_version(self) -> bool: + try: + code = self._encode_instruction(DeviceInstruction.TYP_CIS, DeviceInstruction.CIS_CALI, 0) + self._master.write_characteristic(self.device_id, CC2650MasterDevice.COMMAND_HANDLE, code) + except SendInstructionTimeoutError: + self._master.log_warn('device', self.device_id, 'update cali version error') + return False + except RuntimeError: + self._master.log_warn('device', self.device_id, 'update cali version error') + return False + else: + sleep(0.1) + # receive + version = self._master.read_characteristic(self.device_id,CC2650MasterDevice.RETURN_HANDLE) + self._cali_version = struct.unpack(' Union[bool, str]: connect_device = self.device_manager.get_device(device) - mac_address = connect_device.mac_address - + if connect_device == None: + return + mac_address = address_str(connect_device.mac_address) + device = Device.get_device({"mac_address": mac_address}) try: - if connect_device.library.name.startswith('Neulive3.'): - connect_device.calibration_info('NeuliveThreeOne') - elif connect_device.library.name.startswith('Neulive'): - connect_device.calibration_info('TDC4VC') - elif connect_device.library.name.startswith('Elite_EIS_1.1'): - connect_device.calibration_info('EISZeroOne') - + if connect_device.library.name.startswith('Elite_EIS_1.1'): + # update calibration version + connect_device._device.update_cali_version() + # check if is first time or calibration version is different + if connect_device._device._cali_version == -1 or (device.calibration_version != connect_device._device._cali_version): + # get calibration info from device + connect_device.calibration_info('EISZeroOne') + # update_calibration_info + Device.update_device( + { "mac_address": mac_address }, + { + 'calibration': connect_device.calibration , + 'calibration_version': connect_device._device._cali_version + } + ) + else: + connect_device._device._coeff = device.calibration except BaseException as e: # reset device info in db DeviceAPI.updateByMac( - address_str(mac_address), + mac_address, { 'connect_id': -1, 'connect_status': 'idle', @@ -887,14 +900,6 @@ class ControlServer(SocketServer, ControlServerAPI): print(e) return False else: - # update_calibration_info - DeviceAPI.updateByMac( - address_str(mac_address), - { - 'calibration': str(connect_device.calibration) - } - ) - # DeviceAPI.updateByAttrs('connect_status', 'update', {"connect_status": 'connect'}) return True @logging_info diff --git a/update_db_package.sh b/update_db_package.sh index eba7040..6858364 100755 --- a/update_db_package.sh +++ b/update_db_package.sh @@ -27,3 +27,6 @@ sudo su -c "psql -d postgres -c \"ALTER TABLE IF EXISTS project_report RENAME TO # change table project_meta column cycle to type jsonb sudo su -c "psql -d postgres -c \"ALTER TABLE project_metas ALTER COLUMN cycle type jsonb USING (cycle::jsonb);\"" postgres + +# add column project in recording_data_metas +sudo su -c "psql -d postgres -c \"ALTER TABLE devices ADD COLUMN IF NOT EXISTS calibration_version Int4 DEFAULT -1;\"" postgres \ No newline at end of file