diff --git a/src/host/iot_host.erl b/src/host/iot_host.erl index e7a5d14..8f5f025 100644 --- a/src/host/iot_host.erl +++ b/src/host/iot_host.erl @@ -266,24 +266,26 @@ 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}, StateName, State = #state{uuid = UUID, channel_pid = undefined}) -> - case StateName of - ?STATE_ACTIVATED -> - erlang:monitor(process, ChannelPid), - %% 更新主机为在线状态 - ChangeResult = iot_api_client:change_host_status(UUID, ?HOST_ONLINE), - logger:debug("[iot_host] host_id(attach_channel) uuid: ~p, will change status, result: ~p", [UUID, ChangeResult]), - {keep_state, State#state{channel_pid = ChannelPid, has_session = true}, [{reply, From, ok}]}; - %% 主机未激活 - ?STATE_DENIED -> - logger: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">>}}]} +handle_event({call, From}, {attach_channel, ChannelPid}, StateName, State = #state{uuid = UUID, channel_pid = OldChannelPid}) -> + case OldChannelPid == undefined orelse OldChannelPid =:= ChannelPid of + true -> + case StateName of + ?STATE_ACTIVATED -> + erlang:monitor(process, ChannelPid), + %% 更新主机为在线状态 + ChangeResult = iot_api_client:change_host_status(UUID, ?HOST_ONLINE), + logger:debug("[iot_host] host_id(attach_channel) uuid: ~p, will change status, result: ~p", [UUID, ChangeResult]), + {keep_state, State#state{channel_pid = ChannelPid, has_session = true}, [{reply, From, ok}]}; + %% 主机未激活 + ?STATE_DENIED -> + logger: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; + false -> + logger:notice("[iot_host] attach_channel host_id uuid: ~p, old channel exists: ~p", [UUID, OldChannelPid]), + {keep_state, State, [{reply, From, {error, <<"channel existed">>}}]} end; -%% 已经绑定了channel -handle_event({call, From}, {attach_channel, _}, _, State = #state{uuid = UUID, channel_pid = OldChannelPid}) -> - logger:notice("[iot_host] attach_channel host_id uuid: ~p, old channel exists: ~p", [UUID, OldChannelPid]), - {keep_state, State, [{reply, From, {error, <<"channel existed">>}}]}; %% 数据分发 handle_event(cast, {handle, {data, RouteKey, MetricBin}}, ?STATE_ACTIVATED, diff --git a/src/transport/tcp/ssl_channel.erl b/src/transport/tcp/ssl_channel.erl index a0f975d..6c7a731 100644 --- a/src/transport/tcp/ssl_channel.erl +++ b/src/transport/tcp/ssl_channel.erl @@ -55,10 +55,10 @@ command(Pid, Command) when is_pid(Pid) -> -spec activate(Pid :: pid(), Auth :: boolean()) -> no_return(). activate(Pid, Auth) when is_pid(Pid), is_boolean(Auth) -> Cmd = case Auth of true -> 'ACTIVATE'; false -> 'DEACTIVATE' end, - Command = #'CastFrame.Command'{ + Command = #'CastFrame.Command'{ command = {auth, #'CastFrame.Command.Authorization'{cmd = Cmd}} }, - command(Pid, Command). + gen_server:cast(Pid, {command, Command}). -spec container_call(Pid :: pid(), ReceiverPid :: pid(), Request :: message_pb:'ContainerRequest'()) -> Ref :: reference(). container_call(Pid, ReceiverPid, Request) when is_pid(Pid), is_pid(ReceiverPid), is_record(Request, 'ContainerRequest') -> @@ -114,7 +114,7 @@ handle_cast({pub, Topic, Qos, Content}, State = #state{transport = Transport, so %% 发送Command消息 handle_cast({command, Command}, State = #state{transport = Transport, socket = Socket}) -> Encoded = message_pb:encode_msg(#'CastFrame'{ - body = Command + body = {command, Command} }), Transport:send(Socket, <>), {noreply, State};