fix
This commit is contained in:
parent
66d3854993
commit
dc7aa27e4e
@ -119,26 +119,21 @@ handle_request("POST", "/host/activate", _, #{<<"uuid">> := UUID, <<"auth">> :=
|
|||||||
lager:debug("[host_handler] activate host_id: ~p, start", [UUID]),
|
lager:debug("[host_handler] activate host_id: ~p, start", [UUID]),
|
||||||
{ok, Aes} = iot_host:get_aes(Pid),
|
{ok, Aes} = iot_host:get_aes(Pid),
|
||||||
BinReply = jiffy:encode(#{<<"auth">> => true, <<"aes">> => Aes}, [force_utf8]),
|
BinReply = jiffy:encode(#{<<"auth">> => true, <<"aes">> => Aes}, [force_utf8]),
|
||||||
|
ok = iot_host:activate(Pid, true),
|
||||||
|
|
||||||
case iot_host:publish_message(Pid, 8, BinReply) of
|
case iot_host:publish_message(Pid, 8, BinReply) of
|
||||||
{ok, Ref} ->
|
{ok, Ref} ->
|
||||||
receive
|
receive
|
||||||
{response, Ref, Response} ->
|
{response, Ref, Response} ->
|
||||||
case jiffy:decode(Response, [return_maps]) of
|
lager:debug("[iot_host_handler] host_id uuid: ~p, publish topic success, get response: ~p", [Response])
|
||||||
#{<<"code">> := 1} ->
|
|
||||||
ok = iot_host:activate(Pid, true),
|
|
||||||
{ok, 200, iot_util:json_data(<<"success">>)};
|
|
||||||
#{<<"code">> := 0, <<"message">> := Message} when is_binary(Message) ->
|
|
||||||
{ok, 200, iot_util:json_error(401, <<"操作失败: "/utf8, Message/binary>>)}
|
|
||||||
end
|
|
||||||
after Timeout * 1000 ->
|
after Timeout * 1000 ->
|
||||||
lager:debug("[iot_host_handler] host_id uuid: ~p, publish topic success, but get ack timeout", [UUID]),
|
lager:debug("[iot_host_handler] host_id uuid: ~p, publish topic success, but get ack timeout", [UUID])
|
||||||
{ok, 200, iot_util:json_error(401, <<"命令执行超时, 请重试"/utf8>>)}
|
|
||||||
end;
|
end;
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
lager:debug("[iot_host] host_id uuid: ~p, publish command get error: ~p", [UUID, Reason]),
|
lager:debug("[iot_host] host_id uuid: ~p, publish command get error: ~p", [UUID, Reason])
|
||||||
{ok, 200, iot_util:json_error(402, Reason)}
|
end,
|
||||||
end
|
|
||||||
|
{ok, 200, iot_util:json_data(<<"success">>)}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
%% 处理主机的授权的 取消激活
|
%% 处理主机的授权的 取消激活
|
||||||
@ -148,35 +143,27 @@ handle_request("POST", "/host/activate", _, #{<<"uuid">> := UUID, <<"auth">> :=
|
|||||||
lager:debug("[host_handler] activate host_id: ~p, failed with reason: ~p", [UUID, Reason]),
|
lager:debug("[host_handler] activate host_id: ~p, failed with reason: ~p", [UUID, Reason]),
|
||||||
{ok, 200, iot_util:json_error(400, <<"host not found">>)};
|
{ok, 200, iot_util:json_error(400, <<"host not found">>)};
|
||||||
{ok, Pid} when is_pid(Pid) ->
|
{ok, Pid} when is_pid(Pid) ->
|
||||||
|
ok = iot_host:activate(Pid, false),
|
||||||
%% 激活的话不管是否存在会话都需要通知
|
%% 激活的话不管是否存在会话都需要通知
|
||||||
case iot_host:has_session(Pid) of
|
case iot_host:has_session(Pid) of
|
||||||
true ->
|
true ->
|
||||||
lager:debug("[host_handler] activate host_id: ~p, start", [UUID]),
|
lager:debug("[host_handler] activate host_id: ~p, start", [UUID]),
|
||||||
BinReply = jiffy:encode(#{<<"auth">> => false}, [force_utf8]),
|
BinReply = jiffy:encode(#{<<"auth">> => false}, [force_utf8]),
|
||||||
|
|
||||||
case iot_host:publish_message(Pid, 8, BinReply) of
|
case iot_host:publish_message(Pid, 8, BinReply) of
|
||||||
{ok, Ref} ->
|
{ok, Ref} ->
|
||||||
receive
|
receive
|
||||||
{response, Ref, Response} ->
|
{response, Ref, Response} ->
|
||||||
case jiffy:decode(Response, [return_maps]) of
|
lager:debug("[iot_host_handler] host_id uuid: ~p, publish topic success, get response: ~p", [Response])
|
||||||
#{<<"code">> := 1} ->
|
|
||||||
ok = iot_host:activate(Pid, false),
|
|
||||||
{ok, 200, iot_util:json_data(<<"success">>)};
|
|
||||||
#{<<"code">> := 0, <<"message">> := Message} when is_binary(Message) ->
|
|
||||||
{ok, 200, iot_util:json_error(401, <<"操作失败: "/utf8, Message/binary>>)}
|
|
||||||
end
|
|
||||||
after Timeout * 1000 ->
|
after Timeout * 1000 ->
|
||||||
lager:debug("[iot_host_handler] host_id uuid: ~p, publish topic success, but get ack timeout", [UUID]),
|
lager:debug("[iot_host_handler] host_id uuid: ~p, publish topic success, but get ack timeout", [UUID])
|
||||||
{ok, 200, iot_util:json_error(401, <<"命令执行超时, 请重试"/utf8>>)}
|
|
||||||
end;
|
end;
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
lager:debug("[iot_host] host_id uuid: ~p, publish command get error: ~p", [UUID, Reason]),
|
lager:debug("[iot_host] host_id uuid: ~p, publish command get error: ~p", [UUID, Reason])
|
||||||
{ok, 200, iot_util:json_error(402, Reason)}
|
|
||||||
end;
|
end;
|
||||||
false ->
|
false ->
|
||||||
ok = iot_host:activate(Pid, false),
|
ok
|
||||||
{ok, 200, iot_util:json_data(<<"success">>)}
|
end,
|
||||||
end
|
{ok, 200, iot_util:json_data(<<"success">>)}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
handle_request(_, Path, _, _) ->
|
handle_request(_, Path, _, _) ->
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user