| @@ -3,7 +3,7 @@ import json | |||||
| import logging | import logging | ||||
| import os | import os | ||||
| import time | import time | ||||
| from typing import Dict, Optional, Tuple | |||||
| from typing import Dict, Optional | |||||
| import config_env | import config_env | ||||
| from logica_reslevis.gateway import GatewayJsonRepository | from logica_reslevis.gateway import GatewayJsonRepository | ||||
| @@ -43,7 +43,7 @@ def _norm_mac(value: str) -> str: | |||||
| return "".join(ch for ch in str(value).strip().lower() if ch.isalnum()) | return "".join(ch for ch in str(value).strip().lower() if ch.isalnum()) | ||||
| def _parse_line(line: str) -> Optional[Tuple[str, bool]]: | |||||
| def _parse_line(line: str) -> Optional[str]: | |||||
| line = line.strip() | line = line.strip() | ||||
| if not line or " " not in line: | if not line or " " not in line: | ||||
| return None | return None | ||||
| @@ -75,14 +75,7 @@ def _parse_line(line: str) -> Optional[Tuple[str, bool]]: | |||||
| return None | return None | ||||
| nums = gateway_entry.get("nums") if gateway_entry else None | nums = gateway_entry.get("nums") if gateway_entry else None | ||||
| has_data = len(data) > 1 | |||||
| if nums is not None: | |||||
| try: | |||||
| has_data = has_data and int(nums) > 0 | |||||
| except (TypeError, ValueError): | |||||
| pass | |||||
| return mac, has_data | |||||
| return mac | |||||
| class MqttGatewayMonitor: | class MqttGatewayMonitor: | ||||
| @@ -107,7 +100,6 @@ class MqttGatewayMonitor: | |||||
| self._gateway_repo = gateway_repo or GatewayJsonRepository() | self._gateway_repo = gateway_repo or GatewayJsonRepository() | ||||
| self._last_seen: Dict[str, float] = {} | self._last_seen: Dict[str, float] = {} | ||||
| self._last_has_data: Dict[str, bool] = {} | |||||
| self._lock = asyncio.Lock() | self._lock = asyncio.Lock() | ||||
| self._stop_event = asyncio.Event() | self._stop_event = asyncio.Event() | ||||
| self._reader_task: Optional[asyncio.Task] = None | self._reader_task: Optional[asyncio.Task] = None | ||||
| @@ -165,16 +157,14 @@ class MqttGatewayMonitor: | |||||
| line = await self._proc.stdout.readline() | line = await self._proc.stdout.readline() | ||||
| if not line: | if not line: | ||||
| break | break | ||||
| parsed = _parse_line(line.decode("utf-8", errors="ignore")) | |||||
| if not parsed: | |||||
| mac = _parse_line(line.decode("utf-8", errors="ignore")) | |||||
| if not mac: | |||||
| continue | continue | ||||
| mac, has_data = parsed | |||||
| mac_norm = _norm_mac(mac) | mac_norm = _norm_mac(mac) | ||||
| if not mac_norm: | if not mac_norm: | ||||
| continue | continue | ||||
| async with self._lock: | async with self._lock: | ||||
| self._last_seen[mac_norm] = time.monotonic() | self._last_seen[mac_norm] = time.monotonic() | ||||
| self._last_has_data[mac_norm] = bool(has_data) | |||||
| finally: | finally: | ||||
| if self._proc and self._proc.returncode is None: | if self._proc and self._proc.returncode is None: | ||||
| self._proc.terminate() | self._proc.terminate() | ||||
| @@ -194,7 +184,6 @@ class MqttGatewayMonitor: | |||||
| async with self._lock: | async with self._lock: | ||||
| last_seen = dict(self._last_seen) | last_seen = dict(self._last_seen) | ||||
| last_has_data = dict(self._last_has_data) | |||||
| status_by_mac: Dict[str, str] = {} | status_by_mac: Dict[str, str] = {} | ||||
| for gw in gateways: | for gw in gateways: | ||||
| @@ -203,8 +192,7 @@ class MqttGatewayMonitor: | |||||
| continue | continue | ||||
| seen_at = last_seen.get(mac_norm) | seen_at = last_seen.get(mac_norm) | ||||
| has_data = last_has_data.get(mac_norm, False) | |||||
| if seen_at is None or (now - seen_at) > self._stale_after or not has_data: | |||||
| if seen_at is None or (now - seen_at) > self._stale_after: | |||||
| status = "disabled" | status = "disabled" | ||||
| else: | else: | ||||
| status = "active" | status = "active" | ||||