From 4ea8882585ac85495a2cc696241409fb0aac29ca Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Tue, 3 Jun 2025 19:37:31 +0800 Subject: [PATCH] pub/sub --- apps/iot/src/http/host_handler.erl | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/apps/iot/src/http/host_handler.erl b/apps/iot/src/http/host_handler.erl index 9b0358b..2701736 100644 --- a/apps/iot/src/http/host_handler.erl +++ b/apps/iot/src/http/host_handler.erl @@ -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">>)}.