|
@@ -1,7 +1,5 @@
|
|
|
import asyncio
|
|
import asyncio
|
|
|
import logging
|
|
import logging
|
|
|
-from typing import Dict
|
|
|
|
|
-import json
|
|
|
|
|
from models.cell import Cell, CellLimits
|
|
from models.cell import Cell, CellLimits
|
|
|
from models.device import Device, DeviceStatus, Slot
|
|
from models.device import Device, DeviceStatus, Slot
|
|
|
from services.i2c_service import I2CService
|
|
from services.i2c_service import I2CService
|
|
@@ -23,18 +21,18 @@ class MeasurementController:
|
|
|
self.measurement_data_task = None
|
|
self.measurement_data_task = None
|
|
|
|
|
|
|
|
devices_config = config['devices']
|
|
devices_config = config['devices']
|
|
|
- self.devices = [Device(conf['id'], conf) for conf in devices_config]
|
|
|
|
|
|
|
+ self.devices = {conf['id']: Device(conf['id'], conf) for conf in devices_config}
|
|
|
|
|
|
|
|
self.subscribe_prefix = self.config['mqtt']['subscribe_prefix']
|
|
self.subscribe_prefix = self.config['mqtt']['subscribe_prefix']
|
|
|
- for device in self.devices:
|
|
|
|
|
|
|
+ for _, device in self.devices.items():
|
|
|
self.mqtt_service.register_device(device.id, len(device.slots), self._update_inserted_cell)
|
|
self.mqtt_service.register_device(device.id, len(device.slots), self._update_inserted_cell)
|
|
|
|
|
|
|
|
async def start_polling(self):
|
|
async def start_polling(self):
|
|
|
- """Start the polling tasks."""
|
|
|
|
|
|
|
+ """Start polling task."""
|
|
|
self.polling_task = asyncio.create_task(self._poll_devices())
|
|
self.polling_task = asyncio.create_task(self._poll_devices())
|
|
|
|
|
|
|
|
async def stop_polling(self):
|
|
async def stop_polling(self):
|
|
|
- """Stop the polling tasks."""
|
|
|
|
|
|
|
+ """Stop polling task."""
|
|
|
if self.polling_task:
|
|
if self.polling_task:
|
|
|
self.polling_task.cancel()
|
|
self.polling_task.cancel()
|
|
|
|
|
|
|
@@ -44,11 +42,11 @@ class MeasurementController:
|
|
|
|
|
|
|
|
while True:
|
|
while True:
|
|
|
await asyncio.sleep(polling_interval)
|
|
await asyncio.sleep(polling_interval)
|
|
|
- for device in self.devices:
|
|
|
|
|
|
|
+ for device_id, device in self.devices.items():
|
|
|
try:
|
|
try:
|
|
|
# Read slot status via I2C
|
|
# Read slot status via I2C
|
|
|
- new_status_list = self.i2c_service.request_status_list(device.i2c_address)
|
|
|
|
|
- if len(device.status_list) != len(device.slots):
|
|
|
|
|
|
|
+ new_status_list = self.i2c_service.request_status_list(device.i2c_address, len(device.slots))
|
|
|
|
|
+ if len(new_status_list) != len(device.slots):
|
|
|
raise IndexError(f"Invalid status list length: {len(device.status_list)} != {len(device.slots)}")
|
|
raise IndexError(f"Invalid status list length: {len(device.status_list)} != {len(device.slots)}")
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
logger.error(f"Error during polling device: {device.id}:\n{str(e)}")
|
|
logger.error(f"Error during polling device: {device.id}:\n{str(e)}")
|
|
@@ -60,9 +58,13 @@ class MeasurementController:
|
|
|
slot = device.slots[idx]
|
|
slot = device.slots[idx]
|
|
|
prev_state = device.status_list[idx]
|
|
prev_state = device.status_list[idx]
|
|
|
device.status_list[idx] = status
|
|
device.status_list[idx] = status
|
|
|
|
|
+
|
|
|
|
|
+ if slot.is_empty() and status is not DeviceStatus.EMPTY:
|
|
|
|
|
+ logging.warning(f"Device {device.id}, Slot {slot.id} is empty, but status is {status}")
|
|
|
|
|
+ continue
|
|
|
|
|
|
|
|
# Check for unconfigured cell
|
|
# Check for unconfigured cell
|
|
|
- if status is DeviceStatus.INSERTED and slot.get_cell() and not slot.get_cell().limits_transmitted:
|
|
|
|
|
|
|
+ if status is DeviceStatus.INSERTED and not slot.get_cell().limits_transmitted:
|
|
|
self._update_cell_limits(device, slot)
|
|
self._update_cell_limits(device, slot)
|
|
|
continue
|
|
continue
|
|
|
# Check for state transitions to "DONE"
|
|
# Check for state transitions to "DONE"
|
|
@@ -78,7 +80,7 @@ class MeasurementController:
|
|
|
continue
|
|
continue
|
|
|
|
|
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
- logger.error(f"Error during processing device: {device.id}), slot: {slot.id}:\n{str(e)}")
|
|
|
|
|
|
|
+ logger.error(f"Error during processing status {status} for device {device.id}, slot {slot.id}: {str(e)}")
|
|
|
continue
|
|
continue
|
|
|
|
|
|
|
|
def _update_cell_limits(self, device: Device, slot: Slot):
|
|
def _update_cell_limits(self, device: Device, slot: Slot):
|