This commit is contained in:
anlicheng 2025-06-03 19:37:31 +08:00
parent 2be6f8040e
commit 4ea8882585

View File

@ -93,6 +93,19 @@ handle_request("POST", "/host/activate", _, #{<<"uuid">> := UUID, <<"auth">> :=
{ok, 200, iot_util:json_data(<<"success">>)}
end;
%%
handle_request("POST", "/host/pub", _, #{<<"uuid">> := UUID, <<"topic">> := Topic, <<"content">> := Content})
when is_binary(UUID), is_binary(Topic), is_binary(Content) ->
case iot_host_sup:ensured_host_started(UUID) of
{error, Reason} ->
lager:debug("[host_handler] pub host_id: ~p, topic: ~p, failed with reason: ~p", [UUID, Topic, Reason]),
{ok, 200, iot_util:json_error(400, <<"host not found">>)};
{ok, Pid} when is_pid(Pid) ->
ok = iot_host:pub(Pid, Topic, Content),
{ok, 200, iot_util:json_data(<<"success">>)}
end;
handle_request(_, Path, _, _) ->
Path1 = list_to_binary(Path),
{ok, 200, iot_util:json_error(-1, <<"url: ", Path1/binary, " not found">>)}.