From 36a64939da613f1a30c10498312368b099d466b9 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Fri, 7 Nov 2025 16:10:31 +0800 Subject: [PATCH] fix bug --- apps/iot/src/http_handlers/host_handler.erl | 14 ------------- apps/iot/src/iot_api.erl | 23 ++++++++++++++------- apps/iot/src/iot_sup.erl | 12 +++++++---- 3 files changed, 23 insertions(+), 26 deletions(-) diff --git a/apps/iot/src/http_handlers/host_handler.erl b/apps/iot/src/http_handlers/host_handler.erl index 23930ba..3dde9c6 100644 --- a/apps/iot/src/http_handlers/host_handler.erl +++ b/apps/iot/src/http_handlers/host_handler.erl @@ -42,20 +42,6 @@ handle_request("GET", "/host/status", #{<<"uuid">> := UUID}, _) when is_binary(U {ok, 200, iot_util:json_data(StatusInfo)} end; -%% 重新加载对应的主机信息 -handle_request("POST", "/host/reload", _, #{<<"uuid">> := UUID}) when is_binary(UUID) -> - lager:debug("[host_handler] will reload host uuid: ~p", [UUID]), - case iot_host_sup:ensured_host_started(UUID) of - {ok, Pid} when is_pid(Pid) -> - {ok, #{<<"authorize_status">> := AuthorizeStatus}} = iot_api:get_host_by_uuid(UUID), - ok = iot_host:activate(Pid, AuthorizeStatus =:= 1), - lager:debug("[host_handler] already_started reload host uuid: ~p, success", [UUID]), - {ok, 200, iot_util:json_data(<<"success">>)}; - Error -> - lager:debug("[host_handler] reload host uuid: ~p, error: ~p", [UUID, Error]), - {ok, 200, iot_util:json_error(404, <<"reload error">>)} - end; - %% 删除对应的主机信息 handle_request("POST", "/host/delete", _, #{<<"uuid">> := UUID}) when is_binary(UUID) -> case iot_host_sup:delete_host(UUID) of diff --git a/apps/iot/src/iot_api.erl b/apps/iot/src/iot_api.erl index 9dc495d..b8fcebd 100644 --- a/apps/iot/src/iot_api.erl +++ b/apps/iot/src/iot_api.erl @@ -52,7 +52,7 @@ get_host_by_id(HostId) when is_integer(HostId) -> %% 修改主机的状态 -spec change_host_status(UUID :: binary(), Status :: integer()) -> {ok, Result :: any()} | {error, Reason :: any()}. change_host_status(UUID, NStatus) when is_binary(UUID), is_integer(NStatus) -> - do_post("/change_host_status", [{<<"uuid">>, UUID}, {<<"new_status">>, integer_to_binary(NStatus)}]). + do_post("/change_host_status", #{<<"uuid">> => UUID, <<"new_status">> => NStatus}). -spec get_host_devices(HostId :: integer()) -> {ok, Devices :: [map()]} | {error, Reason::any()}. get_host_devices(HostId) when is_integer(HostId) -> @@ -70,7 +70,7 @@ get_device_by_uuid(DeviceUUID) when is_binary(DeviceUUID) -> %% 修改主机的状态 -spec change_device_status(DeviceUUID :: binary(), Status :: integer()) -> {ok, AffectedRows :: integer()} | {error, Reason :: any()}. change_device_status(DeviceUUID, NStatus) when is_binary(DeviceUUID), is_integer(NStatus) -> - do_post("/change_device_status", [{<<"device_uuid">>, DeviceUUID}, {<<"new_status">>, integer_to_binary(NStatus)}]). + do_post("/change_device_status", #{<<"device_uuid">> => DeviceUUID, <<"new_status">> => NStatus}). %%%------------------------------------------------------------------- %% endpoint相关的api @@ -88,8 +88,8 @@ get_all_endpoints() -> -spec get_endpoint(Id :: integer()) -> undefined | {ok, EndpointInfo :: map()}. get_endpoint(Id) when is_integer(Id) -> - case do_get(<<"/get_endpoint">>, [{<<"id">>, integer_to_binary(Id)}]) of - {ok, EndpointInfo} -> + case do_get("/get_endpoint", [{<<"id">>, integer_to_binary(Id)}]) of + {ok, EndpointInfo} when is_map(EndpointInfo) -> {ok, EndpointInfo}; _ -> undefined @@ -124,7 +124,7 @@ ai_event(Id) when is_integer(Id) -> %% helper methods %%%------------------------------------------------------------------- --spec do_post(Path :: string(), Params :: map()) -> {ok, Resp :: binary()} | {error, Reason :: any()}. +-spec do_post(Path :: string(), Params :: map()) -> {ok, Resp :: any()} | {error, Reason :: any()}. do_post(Path, Params) when is_list(Path), is_map(Params) -> {ok, BaseUrl} = application:get_env(iot, api_url), Headers = [ @@ -158,14 +158,21 @@ do_post(Path, Params) when is_list(Path), is_map(Params) -> {error, Reason} end. --spec do_get(Path :: string(), Params :: [{Key :: binary(), Val :: binary()}]) -> {ok, Resp :: binary()} | {error, Reason :: any()}. +-spec do_get(Path :: string(), Params :: [{Key :: binary(), Val :: binary()}]) -> {ok, Resp :: any()} | {error, Reason :: any()}. do_get(Path, Params) when is_list(Path), is_list(Params) -> {ok, BaseUrl} = application:get_env(iot, api_url), Headers = [ {<<"Accept">>, <<"application/json">>} ], - QS = binary_to_list(uri_string:compose_query(Params)), - Url = BaseUrl ++ Path ++ "?" ++ QS, + + Url = case length(Params) > 0 of + true -> + QS = binary_to_list(uri_string:compose_query(Params)), + BaseUrl ++ Path ++ "?" ++ QS; + false -> + BaseUrl ++ Path + end, + case hackney:request(get, Url, Headers, <<>>, [{pool, false}]) of {ok, 200, _, ClientRef} -> {ok, RespBody} = hackney:body(ClientRef), diff --git a/apps/iot/src/iot_sup.erl b/apps/iot/src/iot_sup.erl index 56c6e86..7d4c4b7 100644 --- a/apps/iot/src/iot_sup.erl +++ b/apps/iot/src/iot_sup.erl @@ -70,7 +70,11 @@ init([]) -> %% internal functions pools() -> - {ok, Pools} = application:get_env(iot, pools), - lists:map(fun({Name, PoolArgs, WorkerArgs}) -> - poolboy:child_spec(Name, [{name, {local, Name}}|PoolArgs], WorkerArgs) - end, Pools). \ No newline at end of file + case application:get_env(iot, pools) of + undefined -> + []; + {ok, Pools} -> + lists:map(fun({Name, PoolArgs, WorkerArgs}) -> + poolboy:child_spec(Name, [{name, {local, Name}}|PoolArgs], WorkerArgs) + end, Pools) + end.