完善中间件自动更新逻辑
This commit is contained in:
+30
-11
@@ -1,3 +1,5 @@
|
||||
import asyncio
|
||||
import pathlib
|
||||
import uuid
|
||||
from uuid import UUID
|
||||
|
||||
@@ -26,21 +28,34 @@ async def emqx_webhook(data: EMQXWebhook):
|
||||
|
||||
if event == "client.connected":
|
||||
redis_client.set_online(device_id)
|
||||
# 这里刻意等1s 是因为设备连接后这里首先接到通知,但是状态信息设备来没来得及通过mqtt发送来,所以在此等待
|
||||
# 没有直接在mqtt发送来的消息中获取在线状态是因为 这里是通过emqx的webhooks通知的,两种通知方式不同,一方面防止其中一种逻辑失效,另一方面在mqtt消息接收中设置在线状态会存在滞后性,同时也需要设置遗嘱消息,较为
|
||||
await asyncio.sleep(1)
|
||||
await ws_manager.noticeOnlineStatus(
|
||||
{
|
||||
"deviceId": device_id,
|
||||
"online": True,
|
||||
"type": "status",
|
||||
}
|
||||
)
|
||||
|
||||
await ws_manager.noticeOnlineStatus({"deviceId": device_id, "online": True})
|
||||
|
||||
print(f"[ONLINE] {device_id}")
|
||||
print(f"[新设备在线] {device_id}")
|
||||
|
||||
elif event == "client.disconnected":
|
||||
redis_client.set_offline(device_id)
|
||||
await ws_manager.noticeOnlineStatus(
|
||||
{
|
||||
"deviceId": device_id,
|
||||
"online": False,
|
||||
"type": "status",
|
||||
}
|
||||
)
|
||||
|
||||
await ws_manager.noticeOnlineStatus({"deviceId": device_id, "online": False})
|
||||
|
||||
print(f"[OFFLINE] {device_id}")
|
||||
print(f"[设备离线] {device_id}")
|
||||
|
||||
else:
|
||||
# 其他事件直接忽略
|
||||
print(f"[IGNORE] {event}")
|
||||
print(f"[其他事件] {event}")
|
||||
|
||||
return {"ok": True}
|
||||
|
||||
@@ -80,8 +95,6 @@ async def get_device_list(
|
||||
d["memory_total"] = info_json.get("memory_total", "")
|
||||
d["disk_total"] = info_json.get("disk_total", "")
|
||||
d["last_seen"] = info_json.get("last_seen", "")
|
||||
d["project"] = info_json.get("project", "")
|
||||
d["device_type"] = info_json.get("deviceType", "")
|
||||
|
||||
return BaseResponse(data={"list": devices, "total": total})
|
||||
|
||||
@@ -214,10 +227,14 @@ async def delete_update(
|
||||
|
||||
@iot_router.get("/common/update/getUploadUrl")
|
||||
def getUploadUrl(
|
||||
filename: str | None = None,
|
||||
user_id: UUID = Depends(get_user_id_from_token),
|
||||
):
|
||||
if not user_id:
|
||||
return {"error": "userId is required"}
|
||||
# 生成唯一文件名,避免覆盖
|
||||
object_name = f"{uuid.uuid4()}"
|
||||
ext = pathlib.Path(filename).suffix if filename else "" # 获取文件后缀
|
||||
object_name = f"{uuid.uuid4()}{ext}" # 拼接到 UUID 后面
|
||||
return BaseResponse(
|
||||
data={
|
||||
"uploadUrl": get_upload_token("iot-update", object_name),
|
||||
@@ -231,6 +248,8 @@ def updateGetMaxCodeByDeptId(
|
||||
user_id: UUID = Depends(get_user_id_from_token),
|
||||
dept_id: str | None = None,
|
||||
):
|
||||
if not user_id:
|
||||
return {"error": "userId is required"}
|
||||
# 生成唯一文件名,避免覆盖
|
||||
return BaseResponse(data=getMaxCodeByDeptId(dept_id))
|
||||
|
||||
@@ -251,6 +270,6 @@ async def command(
|
||||
return {"error": "userId is required"}
|
||||
|
||||
await mqtt_publish(
|
||||
data.project, "cmd", data.device_type, data.id, data.command, "{}"
|
||||
data.dept_id, "cmd", data.device_type, data.id, data.command, "{}"
|
||||
)
|
||||
return BaseResponse(data=None)
|
||||
|
||||
Reference in New Issue
Block a user