From 748a4c2d9a6faf7a6d1554766e88af2bee94b8c5 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Mon, 11 May 2026 10:26:24 +0800 Subject: [PATCH] fix --- src/host/iot_host.erl | 25 +++++++++++++++---------- src/http/host_handler.erl | 8 ++++++-- src/transport/udp_server.erl | 1 - 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/host/iot_host.erl b/src/host/iot_host.erl index 14e8d06..93fc72e 100644 --- a/src/host/iot_host.erl +++ b/src/host/iot_host.erl @@ -247,17 +247,22 @@ handle_event({call, From}, {cancel_command_call, Ref}, _, State = #state{channel end; %% 发送指令时, pub/sub -handle_event({call, From}, {pub, Topic, Qos, Content}, ?STATE_ACTIVATED, State = #state{uuid = UUID, channel_pid = ChannelPid, has_session = HasSession}) -> - case HasSession andalso is_pid(ChannelPid) of - true -> - logger:debug("[iot_host] host: ~p, publish to topic: ~p, content: ~p", [UUID, Topic, Content]), - %% 通过websocket发送消息 - ssl_channel:pub(ChannelPid, Topic, Qos, Content), +handle_event({call, From}, {pub, Topic, Qos, Content}, StateName, State = #state{uuid = UUID, channel_pid = ChannelPid, has_session = HasSession}) -> + case StateName of + ?STATE_ACTIVATED -> + case HasSession andalso is_pid(ChannelPid) of + true -> + logger:debug("[iot_host] host: ~p, publish to topic: ~p, content: ~p", [UUID, Topic, Content]), + %% 通过websocket发送消息 + ssl_channel:pub(ChannelPid, Topic, Qos, Content), - {keep_state, State, [{reply, From, ok}]}; - false -> - logger:debug("[iot_host] uuid: ~p, publish to topic: ~p, content: ~p, invalid state: ~p", [UUID, Topic, Content, state_map(State)]), - {keep_state, State, [{reply, From, {error, <<"主机离线,发送失败"/utf8>>}}]} + {keep_state, State, [{reply, From, ok}]}; + false -> + logger:debug("[iot_host] uuid: ~p, publish to topic: ~p, content: ~p, invalid state: ~p", [UUID, Topic, Content, state_map(State)]), + {keep_state, State, [{reply, From, {error, <<"主机离线,发送失败"/utf8>>}}]} + end; + ?STATE_DENIED -> + {keep_state, State, [{reply, From, {error, <<"主机未激活,发送失败"/utf8>>}}]} end; %% 激活/关闭授权只修改 iot 本地授权状态;efka 连接保持在线,数据是否处理由 host 状态决定。 diff --git a/src/http/host_handler.erl b/src/http/host_handler.erl index 4329004..42a6a6c 100644 --- a/src/http/host_handler.erl +++ b/src/http/host_handler.erl @@ -84,8 +84,12 @@ handle_request("POST", "/host/pub", _, #{<<"uuid">> := UUID, <<"topic">> := Topi logger:debug("[host_handler] pub host_id: ~p, topic: ~p, failed with reason: ~p", [UUID, Topic, Reason]), {ok, 200, iot_util:json_error(400, <<"host not found">>)}; {ok, Pid} when is_pid(Pid) -> - ok = iot_host:pub(Pid, Topic, Qos, Content), - {ok, 200, iot_util:json_data(<<"success">>)} + case iot_host:pub(Pid, Topic, Qos, Content) of + ok -> + {ok, 200, iot_util:json_data(<<"success">>)}; + {error, Reason} -> + {ok, 200, iot_util:json_error(400, Reason)} + end end; handle_request(_, Path, _, _) -> diff --git a/src/transport/udp_server.erl b/src/transport/udp_server.erl index 23f8432..db0657d 100644 --- a/src/transport/udp_server.erl +++ b/src/transport/udp_server.erl @@ -118,7 +118,6 @@ handle_heartbeat_packet(Packet, Peer) when is_binary(Packet) -> {ok, UUID, Timestamp, Payload, Mac} -> case efka_client_store:verify_heartbeat(UUID, Timestamp, Payload, Mac) of true -> - logger:warning("[udp_server] heartbeat auth from peer: ~p, uuid: ~p", [Peer, UUID]), Pid = iot_host:get_pid(UUID), iot_host:heartbeat(Pid); false ->