This commit is contained in:
anlicheng 2025-04-28 22:49:50 +08:00
parent 2802d9ac32
commit 7e3d522ecd
3 changed files with 7 additions and 8 deletions

View File

@ -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, _} ->

View File

@ -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

View File

@ -108,7 +108,6 @@ handle_info({tcp, Socket, <<?PACKET_REQUEST, PacketId:32, ?METHOD_AUTH:8, AuthRe
Transport:send(Socket, <<?PACKET_RESPONSE, PacketId:32, 0:8, AuthReplyBin/binary>>),
{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{