From 2802d9ac325cb3398630f4587f922a77da243555 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Tue, 22 Apr 2025 14:50:12 +0800 Subject: [PATCH] fix --- apps/iot/src/iot_host.erl | 28 ++++++++++++++------------ apps/iot/src/websocket/tcp_channel.erl | 16 ++++++++++++++- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index ba1c9ab..9e4b74c 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -304,19 +304,21 @@ handle_event({call, From}, {activate, false}, _, State = #state{uuid = UUID, cha {next_state, ?STATE_DENIED, State#state{channel_pid = undefined, has_session = false}, [{reply, From, ok}]}; %% 绑定channel -handle_event({call, From}, {attach_channel, ChannelPid}, ?STATE_ACTIVATED, State = #state{uuid = UUID, channel_pid = undefined}) -> - erlang:monitor(process, ChannelPid), - %% 更新主机为在线状态 - {ok, AffectedRow} = host_bo:change_status(UUID, ?HOST_ONLINE), - report_event(UUID, ?HOST_ONLINE), - lager:debug("[iot_host] host_id(attach_channel) uuid: ~p, will change status, affected_row: ~p", [UUID, AffectedRow]), - - {keep_state, State#state{channel_pid = ChannelPid, has_session = true}, [{reply, From, ok}]}; -%% 主机没有激活 -handle_event({call, From}, {attach_channel, ChannelPid}, ?STATE_DENIED, State = #state{uuid = UUID, channel_pid = undefined}) -> - lager:notice("[iot_host] attach_channel host_id uuid: ~p, channel: ~p, host inactivated", [UUID, ChannelPid]), - erlang:monitor(process, ChannelPid), - {keep_state, State, [{reply, From, {error, <<"host inactivated">>}}]}; +handle_event({call, From}, {attach_channel, ChannelPid}, StateName, State = #state{uuid = UUID, channel_pid = undefined}) -> + case StateName of + ?STATE_ACTIVATED -> + erlang:monitor(process, ChannelPid), + %% 更新主机为在线状态 + {ok, AffectedRow} = host_bo:change_status(UUID, ?HOST_ONLINE), + report_event(UUID, ?HOST_ONLINE), + lager:debug("[iot_host] host_id(attach_channel) uuid: ~p, will change status, affected_row: ~p", [UUID, AffectedRow]), + {keep_state, State#state{channel_pid = ChannelPid, has_session = true}, [{reply, From, ok}]}; + %% 主机未激活 + ?STATE_DENIED -> + lager:notice("[iot_host] attach_channel host_id uuid: ~p, channel: ~p, host inactivated", [UUID, ChannelPid]), + erlang:monitor(process, ChannelPid), + {keep_state, State#state{channel_pid = ChannelPid}, [{reply, From, {denied, <<"host inactivated">>}}]} + end; %% 已经绑定了channel handle_event({call, From}, {attach_channel, _}, _, State = #state{uuid = UUID, channel_pid = OldChannelPid}) -> lager:notice("[iot_host] attach_channel host_id uuid: ~p, old channel exists: ~p", [UUID, OldChannelPid]), diff --git a/apps/iot/src/websocket/tcp_channel.erl b/apps/iot/src/websocket/tcp_channel.erl index 03e9577..ca940f4 100644 --- a/apps/iot/src/websocket/tcp_channel.erl +++ b/apps/iot/src/websocket/tcp_channel.erl @@ -108,7 +108,9 @@ handle_info({tcp, Socket, <>), {noreply, State#state{uuid = UUID, host_pid = HostPid}}; - {error, Reason} when is_binary(Reason) -> + + {denied, Reason} when is_binary(Reason) -> + erlang:monitor(process, HostPid), AuthReplyBin = message_pb:encode_msg(#auth_reply{ code = -1, message = Reason, @@ -118,6 +120,18 @@ handle_info({tcp, Socket, < + AuthReplyBin = message_pb:encode_msg(#auth_reply{ + code = -2, + message = Reason, + repository_url = <<"">> + }), + Transport:send(Socket, <>), + + lager:debug("[ws_channel] uuid: ~p, attach channel get error: ~p, stop channel", [UUID, Reason]), + {stop, State} end end;