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]),
|
||||
{ok, Aes} = iot_host:get_aes(Pid),
|
||||
BinReply = jiffy:encode(#{<<"auth">> => true, <<"aes">> => Aes}, [force_utf8]),
|
||||
ok = iot_host:activate(Pid, true),
|
||||
|
||||
case iot_host:publish_message(Pid, 8, BinReply) of
|
||||
{ok, Ref} ->
|
||||
receive
|
||||
{response, Ref, Response} ->
|
||||
case jiffy:decode(Response, [return_maps]) of
|
||||
#{<<"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
|
||||
lager:debug("[iot_host_handler] host_id uuid: ~p, publish topic success, get response: ~p", [Response])
|
||||
after Timeout * 1000 ->
|
||||
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>>)}
|
||||
lager:debug("[iot_host_handler] host_id uuid: ~p, publish topic success, but get ack timeout", [UUID])
|
||||
end;
|
||||
{error, 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
|
||||
lager:debug("[iot_host] host_id uuid: ~p, publish command get error: ~p", [UUID, Reason])
|
||||
end,
|
||||
|
||||
{ok, 200, iot_util:json_data(<<"success">>)}
|
||||
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]),
|
||||
{ok, 200, iot_util:json_error(400, <<"host not found">>)};
|
||||
{ok, Pid} when is_pid(Pid) ->
|
||||
ok = iot_host:activate(Pid, false),
|
||||
%% 激活的话不管是否存在会话都需要通知
|
||||
case iot_host:has_session(Pid) of
|
||||
true ->
|
||||
lager:debug("[host_handler] activate host_id: ~p, start", [UUID]),
|
||||
BinReply = jiffy:encode(#{<<"auth">> => false}, [force_utf8]),
|
||||
|
||||
case iot_host:publish_message(Pid, 8, BinReply) of
|
||||
{ok, Ref} ->
|
||||
receive
|
||||
{response, Ref, Response} ->
|
||||
case jiffy:decode(Response, [return_maps]) of
|
||||
#{<<"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
|
||||
lager:debug("[iot_host_handler] host_id uuid: ~p, publish topic success, get response: ~p", [Response])
|
||||
after Timeout * 1000 ->
|
||||
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>>)}
|
||||
lager:debug("[iot_host_handler] host_id uuid: ~p, publish topic success, but get ack timeout", [UUID])
|
||||
end;
|
||||
{error, Reason} ->
|
||||
lager:debug("[iot_host] host_id uuid: ~p, publish command get error: ~p", [UUID, Reason]),
|
||||
{ok, 200, iot_util:json_error(402, Reason)}
|
||||
lager:debug("[iot_host] host_id uuid: ~p, publish command get error: ~p", [UUID, Reason])
|
||||
end;
|
||||
false ->
|
||||
ok = iot_host:activate(Pid, false),
|
||||
{ok, 200, iot_util:json_data(<<"success">>)}
|
||||
end
|
||||
ok
|
||||
end,
|
||||
{ok, 200, iot_util:json_data(<<"success">>)}
|
||||
end;
|
||||
|
||||
handle_request(_, Path, _, _) ->
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user