fix host
This commit is contained in:
parent
2802d9ac32
commit
7e3d522ecd
@ -15,7 +15,7 @@
|
|||||||
|
|
||||||
-spec get_all_hosts() -> UUIDList :: [binary()].
|
-spec get_all_hosts() -> UUIDList :: [binary()].
|
||||||
get_all_hosts() ->
|
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} ->
|
{ok, Hosts} ->
|
||||||
lists:map(fun(#{<<"uuid">> := UUID}) -> UUID end, Hosts);
|
lists:map(fun(#{<<"uuid">> := UUID}) -> UUID end, Hosts);
|
||||||
{error, _} ->
|
{error, _} ->
|
||||||
|
|||||||
@ -141,7 +141,7 @@ send_directive(Pid, DeviceUUID, DirectiveType, Version, DirectiveParams)
|
|||||||
},
|
},
|
||||||
JsonDirective = iolist_to_binary(jiffy:encode(Directive, [force_utf8])),
|
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)]),
|
lager:debug("[iot_host] uuid: ~p, publish_directive: ~p, invalid state: ~p", [UUID, Directive, state_map(State)]),
|
||||||
{keep_state, State, [{reply, From, {error, <<"主机离线,发送指令失败"/utf8>>}}]};
|
{keep_state, State, [{reply, From, {error, <<"主机离线,发送指令失败"/utf8>>}}]};
|
||||||
|
|
||||||
%% 发送指令时, 指令要通过aes加密,必须要求session是存在的
|
%% 发送指令时!! 指令要明确发送到的目标device_uuid, 因为指令内容采用了json的格式
|
||||||
handle_event({call, From}, {send_directive, Directive}, ?STATE_ACTIVATED,
|
handle_event({call, From}, {send_directive, DeviceUUID, Directive}, ?STATE_ACTIVATED,
|
||||||
State = #state{uuid = UUID, channel_pid = ChannelPid, has_session = true}) ->
|
State = #state{uuid = UUID, channel_pid = ChannelPid, has_session = true}) ->
|
||||||
|
|
||||||
lager:debug("[iot_host] host: ~p, will publish_directive: ~p", [UUID, Directive]),
|
lager:debug("[iot_host] host: ~p, will publish_directive: ~p", [UUID, Directive]),
|
||||||
%% 通过websocket发送请求
|
%% 通过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}]};
|
{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
|
case is_pid(ChannelPid) of
|
||||||
true ->
|
true ->
|
||||||
BinReply = message_pb:encode_msg(#activate_push{auth = 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>>);
|
tcp_channel:send(ChannelPid, <<8:8, BinReply/binary>>);
|
||||||
false ->
|
false ->
|
||||||
lager:debug("[iot_host] uuid: ~p, activate: true, no channel", [UUID])
|
lager:debug("[iot_host] uuid: ~p, activate: true, no channel", [UUID])
|
||||||
end,
|
end,
|
||||||
lager:debug("[iot_host] uuid: ~p, activate: true, will send message: ~p", [UUID, BinReply]),
|
|
||||||
{next_state, ?STATE_ACTIVATED, State, [{reply, From, ok}]};
|
{next_state, ?STATE_ACTIVATED, State, [{reply, From, ok}]};
|
||||||
|
|
||||||
%% 关闭授权
|
%% 关闭授权
|
||||||
@ -296,11 +296,11 @@ handle_event({call, From}, {activate, false}, _, State = #state{uuid = UUID, cha
|
|||||||
true ->
|
true ->
|
||||||
BinReply = message_pb:encode_msg(#activate_push{auth = false}),
|
BinReply = message_pb:encode_msg(#activate_push{auth = false}),
|
||||||
tcp_channel:send(ChannelPid, <<8:8, BinReply/binary>>),
|
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);
|
tcp_channel:stop(ChannelPid, closed);
|
||||||
false ->
|
false ->
|
||||||
lager:debug("[iot_host] uuid: ~p, activate: false, no channel", [UUID])
|
lager:debug("[iot_host] uuid: ~p, activate: false, no channel", [UUID])
|
||||||
end,
|
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}]};
|
{next_state, ?STATE_DENIED, State#state{channel_pid = undefined, has_session = false}, [{reply, From, ok}]};
|
||||||
|
|
||||||
%% 绑定channel
|
%% 绑定channel
|
||||||
|
|||||||
@ -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>>),
|
Transport:send(Socket, <<?PACKET_RESPONSE, PacketId:32, 0:8, AuthReplyBin/binary>>),
|
||||||
|
|
||||||
{noreply, State#state{uuid = UUID, host_pid = HostPid}};
|
{noreply, State#state{uuid = UUID, host_pid = HostPid}};
|
||||||
|
|
||||||
{denied, Reason} when is_binary(Reason) ->
|
{denied, Reason} when is_binary(Reason) ->
|
||||||
erlang:monitor(process, HostPid),
|
erlang:monitor(process, HostPid),
|
||||||
AuthReplyBin = message_pb:encode_msg(#auth_reply{
|
AuthReplyBin = message_pb:encode_msg(#auth_reply{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user