完善牧安云哨-后端

This commit is contained in:
BBIT-Kai
2025-12-29 16:30:36 +08:00
parent cd7aa35960
commit b9b8d30ebf
23 changed files with 1074 additions and 41 deletions
+24 -1
View File
@@ -1,8 +1,10 @@
import redis
class RedisClient:
# ---------------- Redis Client ----------------
class RedisClient:
def __init__(self, config_path="config.yaml"):
self.redis = redis.Redis(
"10.10.12.101",
@@ -22,3 +24,24 @@ class RedisClient:
def is_device_online(self, device_id: str) -> bool:
key = f"device:online:{device_id}"
return self.redis.exists(key) == 1
def set_device_info(self, device_id: str, info: dict):
"""
存储完整设备信息到 redis hash
将 bool 转为 int
"""
key = f"device:info:{device_id}"
# 转换 bool 为 int
sanitized_info = {
k: (int(v) if isinstance(v, bool) else v) for k, v in info.items()
}
self.redis.hmset(key, sanitized_info)
def get_device_info(self, device_id: str) -> dict:
key = f"device:info:{device_id}"
return self.redis.hgetall(key)
redis_client = RedisClient()