fix host_handler

This commit is contained in:
anlicheng 2023-06-15 15:36:09 +08:00
parent 42ee5ccaa3
commit 23d78471ab

View File

@ -105,37 +105,63 @@ handle_request("POST", "/host/publish_command", _,
end end
end; end;
%% "激活/取消激活" %%
handle_request("POST", "/host/activate", _, #{<<"uuid">> := UUID, <<"auth">> := Auth, <<"timeout">> := Timeout}) when is_binary(UUID), is_boolean(Auth) -> handle_request("POST", "/host/activate", _, #{<<"uuid">> := UUID, <<"auth">> := true, <<"timeout">> := Timeout}) when is_binary(UUID) ->
case iot_host_sup:ensured_host_started(UUID) of
{error, 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, Pid} when is_pid(Pid) ->
lager:debug("[host_handler] activate host_id: ~p, start", [UUID]),
{ok, Assoc} = iot_host:make_assoc(Pid),
ReplyTopic = iot_host:upstream_topic(UUID),
BinReply = jiffy:encode(#{<<"auth">> => true, <<"reply">> => #{<<"topic">> => ReplyTopic, <<"assoc">> => Assoc}}, [force_utf8]),
case iot_mqtt_publisher:publish(iot_host:downstream_topic(UUID), <<8:8, BinReply/binary>>, 2) of
{ok, Ref} ->
receive
{ok, Ref, _PacketId} ->
receive
{host_reply, Assoc, #{<<"code">> := 1}} ->
ok = iot_host:activate(Pid, true),
{ok, 200, iot_util:json_data(<<"success">>)};
{host_reply, Assoc, #{<<"code">> := 0, <<"message">> := Message}} when is_binary(Message) ->
{ok, 200, iot_util:json_error(401, <<"操作失败: "/utf8, Message/binary>>)}
after Timeout * 1000 ->
{ok, 200, iot_util:json_error(401, <<"操作超时,请重试: "/utf8>>)}
end
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>>)}
end;
{error, Reason} ->
lager:debug("[iot_host] host_id uuid: ~p, publish topic get error: ~p", [UUID, Reason]),
{ok, 200, iot_util:json_error(402, <<"发送命令到mqtt服务失败"/utf8>>)}
end
end;
%%
handle_request("POST", "/host/activate", _, #{<<"uuid">> := UUID, <<"auth">> := false, <<"timeout">> := Timeout}) when is_binary(UUID) ->
case iot_host_sup:ensured_host_started(UUID) of case iot_host_sup:ensured_host_started(UUID) of
{error, Reason} -> {error, Reason} ->
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) ->
%% %%
case iot_host:has_session(Pid) or Auth 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]),
{ok, Assoc} = iot_host:make_assoc(Pid), {ok, Assoc} = iot_host:make_assoc(Pid),
ReplyTopic = iot_host:upstream_topic(UUID), ReplyTopic = iot_host:upstream_topic(UUID),
Reply = case Auth of BinReply = jiffy:encode(#{<<"auth">> => false, <<"reply">> => #{<<"topic">> => ReplyTopic, <<"assoc">> => Assoc}}, [force_utf8]),
true ->
#{<<"auth">> => true, <<"reply">> => #{<<"topic">> => ReplyTopic, <<"assoc">> => Assoc}};
false ->
#{<<"auth">> => false, <<"reply">> => #{ <<"topic">> => ReplyTopic, <<"assoc">> => Assoc}}
end,
BinReply = jiffy:encode(Reply, [force_utf8]),
CommandType = 8, case iot_mqtt_publisher:publish(iot_host:downstream_topic(UUID), <<8:8, BinReply/binary>>, 2) of
Topic = iot_host:downstream_topic(UUID),
case iot_mqtt_publisher:publish(Topic, <<CommandType:8, BinReply/binary>>, 2) of
{ok, Ref} -> {ok, Ref} ->
receive receive
{ok, Ref, _PacketId} -> {ok, Ref, _PacketId} ->
receive receive
{host_reply, Assoc, #{<<"code">> := 1}} -> {host_reply, Assoc, #{<<"code">> := 1}} ->
ok = iot_host:activate(Pid, Auth), ok = iot_host:activate(Pid, false),
{ok, 200, iot_util:json_data(<<"success">>)}; {ok, 200, iot_util:json_data(<<"success">>)};
{host_reply, Assoc, #{<<"code">> := 0, <<"message">> := Message}} when is_binary(Message) -> {host_reply, Assoc, #{<<"code">> := 0, <<"message">> := Message}} when is_binary(Message) ->
{ok, 200, iot_util:json_error(401, <<"操作失败: "/utf8, Message/binary>>)} {ok, 200, iot_util:json_error(401, <<"操作失败: "/utf8, Message/binary>>)}
@ -151,7 +177,7 @@ handle_request("POST", "/host/activate", _, #{<<"uuid">> := UUID, <<"auth">> :=
{ok, 200, iot_util:json_error(402, <<"发送命令到mqtt服务失败"/utf8>>)} {ok, 200, iot_util:json_error(402, <<"发送命令到mqtt服务失败"/utf8>>)}
end; end;
false -> false ->
ok = iot_host:activate(Pid, Auth), ok = iot_host:activate(Pid, false),
{ok, 200, iot_util:json_data(<<"success">>)} {ok, 200, iot_util:json_data(<<"success">>)}
end end
end; end;