From 43864341e666b8e72445fa27b04170f3e80ef943 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Wed, 7 May 2025 10:38:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AF=B9=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E4=B8=BB=E5=8A=A8=E4=B8=8A=E6=8A=A5=E7=9A=84?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/iot/include/iot.hrl | 4 +++- apps/iot/src/iot_host.erl | 14 ++++++++++++++ apps/iot/src/websocket/ws_channel.erl | 5 +++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/apps/iot/include/iot.hrl b/apps/iot/include/iot.hrl index fd99058..33bb99e 100644 --- a/apps/iot/include/iot.hrl +++ b/apps/iot/include/iot.hrl @@ -35,7 +35,9 @@ -define(METHOD_FEEDBACK_RESULT, 16#06). -define(METHOD_EVENT, 16#07). %% ai识别的事件上报 --define(METHOD_AI_EVENT, 17#08). +-define(METHOD_AI_EVENT, 16#08). +%% 设备状态上报 +-define(METHOD_DEVICE_REPORT, 16#09). %% 消息体类型 -define(PACKET_REQUEST, 16#01). diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index 6541eaf..d4a8519 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -403,6 +403,20 @@ handle_event(cast, {handle, {data, Data}}, ?STATE_ACTIVATED, State = #state{aes end, {keep_state, State}; +handle_event(cast, {handle, {device_report, Report}}, ?STATE_ACTIVATED, State = #state{uuid = UUID, aes = AES, has_session = true}) -> + PlainReport = iot_cipher_aes:decrypt(AES, Report), + case catch jiffy:decode(PlainReport, [return_maps]) of + DeviceEdgeStatusList when is_list(DeviceEdgeStatusList) -> + lager:debug("[iot_host] host: ~p, update device edge_status num: ~p", [UUID, length(DeviceEdgeStatusList)]), + %% 更新设备的状态 + lists:foreach(fun(#{<<"device_uuid">> := DeviceUUID, <<"edge_status">> := EdgeStatus}) -> + device_bo:change_edge_status(DeviceUUID, EdgeStatus) + end, DeviceEdgeStatusList); + Other -> + lager:notice("[iot_host] the device_report is invalid json: ~p", [Other]) + end, + {keep_state, State}; + %% ping的数据是通过aes加密后的,因此需要在有会话的情况下才行 handle_event(cast, {handle, {ping, CipherMetric}}, ?STATE_ACTIVATED, State = #state{uuid = UUID, name = Name, aes = AES, heartbeat_counter = HeartbeatCounter, has_session = true}) -> diff --git a/apps/iot/src/websocket/ws_channel.erl b/apps/iot/src/websocket/ws_channel.erl index c2f4fd8..5782488 100644 --- a/apps/iot/src/websocket/ws_channel.erl +++ b/apps/iot/src/websocket/ws_channel.erl @@ -126,6 +126,11 @@ websocket_handle({binary, <>}, State = #state{host_pid = HostPid}) when is_pid(HostPid) -> + iot_host:handle(HostPid, {device_report, CipherReport}), + {ok, State}; + %% 主机端的消息响应 websocket_handle({binary, <>}, State = #state{uuid = UUID}) -> lager:debug("[ws_channel] uuid: ~p, get send response message: ~p", [UUID, Body]),