From 7e3d522ecd4aaad5a58d1052f92b0925ad4e9a65 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Mon, 28 Apr 2025 22:49:50 +0800 Subject: [PATCH] fix host --- apps/iot/src/database/host_bo.erl | 2 +- apps/iot/src/iot_host.erl | 12 ++++++------ apps/iot/src/websocket/tcp_channel.erl | 1 - 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/apps/iot/src/database/host_bo.erl b/apps/iot/src/database/host_bo.erl index bb4b2b0..f10d127 100644 --- a/apps/iot/src/database/host_bo.erl +++ b/apps/iot/src/database/host_bo.erl @@ -15,7 +15,7 @@ -spec get_all_hosts() -> UUIDList :: [binary()]. get_all_hosts() -> - case mysql_pool:get_all(mysql_iot, <<"SELECT uuid FROM host where uuid != ''">>) of + case mysql_pool:get_all(mysql_iot, <<"SELECT uuid FROM host where uuid != '' limit 10">>) of {ok, Hosts} -> lists:map(fun(#{<<"uuid">> := UUID}) -> UUID end, Hosts); {error, _} -> diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index 9e4b74c..28d1c11 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -141,7 +141,7 @@ send_directive(Pid, DeviceUUID, DirectiveType, Version, DirectiveParams) }, JsonDirective = iolist_to_binary(jiffy:encode(Directive, [force_utf8])), - gen_statem:call(Pid, {send_directive, JsonDirective}). + gen_statem:call(Pid, {send_directive, DeviceUUID, JsonDirective}). %% 设备管理相关 @@ -263,13 +263,13 @@ handle_event({call, From}, {publish_directive, _, Directive}, _, State = #state{ lager:debug("[iot_host] uuid: ~p, publish_directive: ~p, invalid state: ~p", [UUID, Directive, state_map(State)]), {keep_state, State, [{reply, From, {error, <<"主机离线,发送指令失败"/utf8>>}}]}; -%% 发送指令时, 指令要通过aes加密,必须要求session是存在的 -handle_event({call, From}, {send_directive, Directive}, ?STATE_ACTIVATED, +%% 发送指令时!! 指令要明确发送到的目标device_uuid, 因为指令内容采用了json的格式 +handle_event({call, From}, {send_directive, DeviceUUID, Directive}, ?STATE_ACTIVATED, State = #state{uuid = UUID, channel_pid = ChannelPid, has_session = true}) -> lager:debug("[iot_host] host: ~p, will publish_directive: ~p", [UUID, Directive]), %% 通过websocket发送请求 - tcp_channel:send(ChannelPid, <<16:8, Directive/binary>>), + tcp_channel:send(ChannelPid, <<16:8, DeviceUUID/binary, Directive/binary>>), {keep_state, State, [{reply, From, ok}]}; @@ -283,11 +283,11 @@ handle_event({call, From}, {activate, true}, _, State = #state{uuid = UUID, chan case is_pid(ChannelPid) of true -> BinReply = message_pb:encode_msg(#activate_push{auth = true}), + lager:debug("[iot_host] uuid: ~p, activate: true, will send message: ~p", [UUID, BinReply]), tcp_channel:send(ChannelPid, <<8:8, BinReply/binary>>); false -> lager:debug("[iot_host] uuid: ~p, activate: true, no channel", [UUID]) end, - lager:debug("[iot_host] uuid: ~p, activate: true, will send message: ~p", [UUID, BinReply]), {next_state, ?STATE_ACTIVATED, State, [{reply, From, ok}]}; %% 关闭授权 @@ -296,11 +296,11 @@ handle_event({call, From}, {activate, false}, _, State = #state{uuid = UUID, cha true -> BinReply = message_pb:encode_msg(#activate_push{auth = false}), tcp_channel:send(ChannelPid, <<8:8, BinReply/binary>>), + lager:debug("[iot_host] uuid: ~p, activate: false, will send message: ~p", [UUID, BinReply]), tcp_channel:stop(ChannelPid, closed); false -> lager:debug("[iot_host] uuid: ~p, activate: false, no channel", [UUID]) end, - lager:debug("[iot_host] uuid: ~p, activate: false, will send message: ~p", [UUID, BinReply]), {next_state, ?STATE_DENIED, State#state{channel_pid = undefined, has_session = false}, [{reply, From, ok}]}; %% 绑定channel diff --git a/apps/iot/src/websocket/tcp_channel.erl b/apps/iot/src/websocket/tcp_channel.erl index ca940f4..d4f15eb 100644 --- a/apps/iot/src/websocket/tcp_channel.erl +++ b/apps/iot/src/websocket/tcp_channel.erl @@ -108,7 +108,6 @@ handle_info({tcp, Socket, <>), {noreply, State#state{uuid = UUID, host_pid = HostPid}}; - {denied, Reason} when is_binary(Reason) -> erlang:monitor(process, HostPid), AuthReplyBin = message_pb:encode_msg(#auth_reply{