Phase 5 Complete by Claude Code

This commit is contained in:
2026-02-17 23:45:20 +02:00
parent fc2d04b8bb
commit c0605c77db
17 changed files with 1663 additions and 12 deletions

View File

@@ -1 +1,70 @@
# TODO: Log storage and retrieval
import logging
from mqtt import database as db
logger = logging.getLogger("mqtt.logger")
LEVEL_MAP = {
"🟢 INFO": "INFO",
"🟡 WARN": "WARN",
"🔴 EROR": "ERROR",
"INFO": "INFO",
"WARN": "WARN",
"ERROR": "ERROR",
"EROR": "ERROR",
}
async def handle_message(serial: str, topic_type: str, payload: dict):
try:
if topic_type == "status/heartbeat":
await _handle_heartbeat(serial, payload)
elif topic_type == "logs":
await _handle_log(serial, payload)
elif topic_type == "data":
await _handle_data_response(serial, payload)
else:
logger.debug(f"Unhandled topic type: {topic_type} for {serial}")
except Exception as e:
logger.error(f"Error handling {topic_type} for {serial}: {e}")
async def _handle_heartbeat(serial: str, payload: dict):
inner = payload.get("payload", {})
await db.insert_heartbeat(
device_serial=serial,
device_id=inner.get("device_id", ""),
firmware_version=inner.get("firmware_version", ""),
ip_address=inner.get("ip_address", ""),
gateway=inner.get("gateway", ""),
uptime_ms=inner.get("uptime_ms", 0),
uptime_display=inner.get("timestamp", ""),
)
async def _handle_log(serial: str, payload: dict):
raw_level = payload.get("level", "INFO")
level = LEVEL_MAP.get(raw_level, "INFO")
message = payload.get("message", "")
device_timestamp = payload.get("timestamp")
await db.insert_log(
device_serial=serial,
level=level,
message=message,
device_timestamp=device_timestamp,
)
async def _handle_data_response(serial: str, payload: dict):
status = payload.get("status", "")
pending = await db.get_pending_command(serial)
if pending:
cmd_status = "success" if status == "SUCCESS" else "error"
await db.update_command_response(
command_id=pending["id"],
status=cmd_status,
response_payload=payload,
)
else:
logger.debug(f"Received data response for {serial} with no pending command")