This commit is contained in:
anlicheng 2025-11-07 16:10:31 +08:00
parent 00eb9dd585
commit 36a64939da
3 changed files with 23 additions and 26 deletions

View File

@ -42,20 +42,6 @@ handle_request("GET", "/host/status", #{<<"uuid">> := UUID}, _) when is_binary(U
{ok, 200, iot_util:json_data(StatusInfo)} {ok, 200, iot_util:json_data(StatusInfo)}
end; 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) -> handle_request("POST", "/host/delete", _, #{<<"uuid">> := UUID}) when is_binary(UUID) ->
case iot_host_sup:delete_host(UUID) of case iot_host_sup:delete_host(UUID) of

View File

@ -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()}. -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) -> 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()}. -spec get_host_devices(HostId :: integer()) -> {ok, Devices :: [map()]} | {error, Reason::any()}.
get_host_devices(HostId) when is_integer(HostId) -> 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()}. -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) -> 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 %% endpoint相关的api
@ -88,8 +88,8 @@ get_all_endpoints() ->
-spec get_endpoint(Id :: integer()) -> undefined | {ok, EndpointInfo :: map()}. -spec get_endpoint(Id :: integer()) -> undefined | {ok, EndpointInfo :: map()}.
get_endpoint(Id) when is_integer(Id) -> get_endpoint(Id) when is_integer(Id) ->
case do_get(<<"/get_endpoint">>, [{<<"id">>, integer_to_binary(Id)}]) of case do_get("/get_endpoint", [{<<"id">>, integer_to_binary(Id)}]) of
{ok, EndpointInfo} -> {ok, EndpointInfo} when is_map(EndpointInfo) ->
{ok, EndpointInfo}; {ok, EndpointInfo};
_ -> _ ->
undefined undefined
@ -124,7 +124,7 @@ ai_event(Id) when is_integer(Id) ->
%% helper methods %% 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) -> do_post(Path, Params) when is_list(Path), is_map(Params) ->
{ok, BaseUrl} = application:get_env(iot, api_url), {ok, BaseUrl} = application:get_env(iot, api_url),
Headers = [ Headers = [
@ -158,14 +158,21 @@ do_post(Path, Params) when is_list(Path), is_map(Params) ->
{error, Reason} {error, Reason}
end. 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) -> do_get(Path, Params) when is_list(Path), is_list(Params) ->
{ok, BaseUrl} = application:get_env(iot, api_url), {ok, BaseUrl} = application:get_env(iot, api_url),
Headers = [ Headers = [
{<<"Accept">>, <<"application/json">>} {<<"Accept">>, <<"application/json">>}
], ],
Url = case length(Params) > 0 of
true ->
QS = binary_to_list(uri_string:compose_query(Params)), QS = binary_to_list(uri_string:compose_query(Params)),
Url = BaseUrl ++ Path ++ "?" ++ QS, BaseUrl ++ Path ++ "?" ++ QS;
false ->
BaseUrl ++ Path
end,
case hackney:request(get, Url, Headers, <<>>, [{pool, false}]) of case hackney:request(get, Url, Headers, <<>>, [{pool, false}]) of
{ok, 200, _, ClientRef} -> {ok, 200, _, ClientRef} ->
{ok, RespBody} = hackney:body(ClientRef), {ok, RespBody} = hackney:body(ClientRef),

View File

@ -70,7 +70,11 @@ init([]) ->
%% internal functions %% internal functions
pools() -> pools() ->
{ok, Pools} = application:get_env(iot, pools), case application:get_env(iot, pools) of
undefined ->
[];
{ok, Pools} ->
lists:map(fun({Name, PoolArgs, WorkerArgs}) -> lists:map(fun({Name, PoolArgs, WorkerArgs}) ->
poolboy:child_spec(Name, [{name, {local, Name}}|PoolArgs], WorkerArgs) poolboy:child_spec(Name, [{name, {local, Name}}|PoolArgs], WorkerArgs)
end, Pools). end, Pools)
end.