修改整体的架构

This commit is contained in:
anlicheng 2026-04-18 15:34:36 +08:00
parent 4ab2ea59bc
commit 92f0b2b100
29 changed files with 32 additions and 32 deletions

View File

@ -3,7 +3,7 @@
```markdown
# 📘 IoT API 接口文档
> 模块:`iot_api`
> 模块:`iot_api_client`
> 作者:**anlicheng**
> 创建时间2023-12-24
> 数据格式:`application/json`

View File

@ -6,7 +6,7 @@
%%% @end
%%% Created : 24. 12 2023 15:42
%%%-------------------------------------------------------------------
-module(iot_api).
-module(iot_api_client).
-author("anlicheng").
%% API
@ -110,14 +110,14 @@ ai_event(Id) when is_integer(Id) ->
case hackney:request(post, Url, Headers, Body, [{pool, false}]) of
{ok, 200, _, ClientRef} ->
{ok, RespBody} = hackney:body(ClientRef),
lager:debug("[iot_api] send body: ~p, get error is: ~p", [Body, RespBody]),
lager:debug("[iot_api_client] send body: ~p, get error is: ~p", [Body, RespBody]),
hackney:close(ClientRef);
{ok, HttpCode, _, ClientRef} ->
{ok, RespBody} = hackney:body(ClientRef),
hackney:close(ClientRef),
lager:warning("[iot_api] send body: ~p, get error is: ~p", [Body, {HttpCode, RespBody}]);
lager:warning("[iot_api_client] send body: ~p, get error is: ~p", [Body, {HttpCode, RespBody}]);
{error, Reason} ->
lager:warning("[iot_api] send body: ~p, get error is: ~p", [Body, Reason])
lager:warning("[iot_api_client] send body: ~p, get error is: ~p", [Body, Reason])
end.
%%%-------------------------------------------------------------------
@ -136,7 +136,7 @@ do_post(Path, Params) when is_list(Path), is_map(Params) ->
case hackney:request(post, Url, Headers, Body, [{pool, false}]) of
{ok, 200, _, ClientRef} ->
{ok, RespBody} = hackney:body(ClientRef),
lager:debug("[iot_api] request url: ~p, send body: ~p, get response is: ~p", [Url, Body, RespBody]),
lager:debug("[iot_api_client] request url: ~p, send body: ~p, get response is: ~p", [Url, Body, RespBody]),
hackney:close(ClientRef),
case catch jiffy:decode(RespBody, [return_maps]) of
#{<<"result">> := Result} ->
@ -151,10 +151,10 @@ do_post(Path, Params) when is_list(Path), is_map(Params) ->
{ok, HttpCode, _, ClientRef} ->
{ok, RespBody} = hackney:body(ClientRef),
hackney:close(ClientRef),
lager:warning("[iot_api] request url: ~p, send body: ~p, get error is: ~p", [Url, Body, {HttpCode, RespBody}]),
lager:warning("[iot_api_client] request url: ~p, send body: ~p, get error is: ~p", [Url, Body, {HttpCode, RespBody}]),
{error, {HttpCode, RespBody}};
{error, Reason} ->
lager:warning("[iot_api] request url: ~p, send body: ~p, get error is: ~p", [Url, Body, Reason]),
lager:warning("[iot_api_client] request url: ~p, send body: ~p, get error is: ~p", [Url, Body, Reason]),
{error, Reason}
end.
@ -177,7 +177,7 @@ do_get(Path, Params) when is_list(Path), is_list(Params) ->
{ok, 200, _, ClientRef} ->
{ok, RespBody} = hackney:body(ClientRef),
hackney:close(ClientRef),
lager:debug("[iot_api] url: ~p, get response is: ~p", [Url, RespBody]),
lager:debug("[iot_api_client] url: ~p, get response is: ~p", [Url, RespBody]),
case catch jiffy:decode(RespBody, [return_maps]) of
#{<<"result">> := Result} ->
{ok, Result};
@ -191,9 +191,9 @@ do_get(Path, Params) when is_list(Path), is_list(Params) ->
{ok, HttpCode, _, ClientRef} ->
{ok, RespBody} = hackney:body(ClientRef),
hackney:close(ClientRef),
lager:warning("[iot_api] request url: ~p, get error is: ~p", [Url, {HttpCode, RespBody}]),
lager:warning("[iot_api_client] request url: ~p, get error is: ~p", [Url, {HttpCode, RespBody}]),
{error, {HttpCode, RespBody}};
{error, Reason} ->
lager:warning("[iot_api] request url: ~p, get error is: ~p", [Url, Reason]),
lager:warning("[iot_api_client] request url: ~p, get error is: ~p", [Url, Reason]),
{error, Reason}
end.

View File

@ -20,15 +20,15 @@ post(Url, Headers, Body) when is_list(Url), is_list(Headers), is_binary(Body) ->
case hackney:request(post, Url, Headers, Body, [{pool, false}]) of
{ok, 200, _, ClientRef} ->
{ok, RespBody} = hackney:body(ClientRef),
lager:debug("[iot_api] send body: ~p, get error is: ~p", [Body, RespBody]),
lager:debug("[http_client] send body: ~p, get error is: ~p", [Body, RespBody]),
hackney:close(ClientRef),
{ok, RespBody};
{ok, HttpCode, _, ClientRef} ->
{ok, RespBody} = hackney:body(ClientRef),
hackney:close(ClientRef),
lager:warning("[iot_api] send body: ~p, get error is: ~p", [Body, {HttpCode, RespBody}]),
lager:warning("[http_client] send body: ~p, get error is: ~p", [Body, {HttpCode, RespBody}]),
{error, {HttpCode, RespBody}};
{error, Reason} ->
lager:warning("[iot_api] send body: ~p, get error is: ~p", [Body, Reason]),
lager:warning("[http_client] send body: ~p, get error is: ~p", [Body, Reason]),
{error, Reason}
end.

View File

@ -29,7 +29,7 @@ start_link() ->
%% modules => modules()} % optional
init([]) ->
SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600},
Endpoints = iot_api:get_all_endpoints(),
Endpoints = iot_api_client:get_all_endpoints(),
ChildSpecs = lists:filtermap(fun(EndpointInfo) ->
case endpoint:endpoint_record(EndpointInfo) of
error ->

View File

@ -174,7 +174,7 @@ start_link(Name, UUID) when is_atom(Name), is_binary(UUID) ->
%% gen_statem:start_link/[3,4], this function is called by the new
%% process to initialize.
init([UUID]) ->
case iot_api:get_host_by_uuid(UUID) of
case iot_api_client:get_host_by_uuid(UUID) of
{ok, #{<<"id">> := HostId, <<"authorize_status">> := AuthorizeStatus}} ->
%% host_id注册别名, HostPid
AliasName = get_alias_name(HostId),
@ -286,7 +286,7 @@ handle_event({call, From}, {attach_channel, ChannelPid}, StateName, State = #sta
?STATE_ACTIVATED ->
erlang:monitor(process, ChannelPid),
%% 线
ChangeResult = iot_api:change_host_status(UUID, ?HOST_ONLINE),
ChangeResult = iot_api_client:change_host_status(UUID, ?HOST_ONLINE),
lager:debug("[iot_host] host_id(attach_channel) uuid: ~p, will change status, result: ~p", [UUID, ChangeResult]),
{keep_state, State#state{channel_pid = ChannelPid, has_session = true}, [{reply, From, ok}]};
%%
@ -320,14 +320,14 @@ handle_event(cast, heartbeat, _, State = #state{heartbeat_counter = HeartbeatCou
%% 线,
handle_event(info, {timeout, _, heartbeat_ticker}, _, State = #state{uuid = UUID, heartbeat_counter = 0, channel_pid = ChannelPid}) ->
lager:warning("[iot_host] uuid: ~p, heartbeat lost, devices will unknown", [UUID]),
{ok, #{<<"status">> := Status}} = iot_api:get_host_by_uuid(UUID),
{ok, #{<<"status">> := Status}} = iot_api_client:get_host_by_uuid(UUID),
case Status of
?HOST_NOT_JOINED ->
lager:debug("[iot_host] host: ~p, host_maybe_offline, host not joined, can not change to offline", [UUID]);
?HOST_OFFLINE ->
lager:debug("[iot_host] host: ~p, host_maybe_offline, host now is offline, do nothing", [UUID]);
?HOST_ONLINE ->
iot_api:change_host_status(UUID, ?HOST_OFFLINE)
iot_api_client:change_host_status(UUID, ?HOST_OFFLINE)
end,
%% channel

View File

@ -15,7 +15,7 @@ start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
init([]) ->
Specs = lists:map(fun child_spec/1, iot_api:get_all_hosts()),
Specs = lists:map(fun child_spec/1, iot_api_client:get_all_hosts()),
{ok, {#{strategy => one_for_one, intensity => 1000, period => 3600}, Specs}}.

View File

@ -30,7 +30,7 @@ handle_request("POST", "/endpoint/run_statuses", _, Ids) when is_list(Ids) ->
{ok, 200, iot_util:json_data(Statuses)};
handle_request("POST", "/endpoint/start", _, #{<<"id">> := Id}) when is_integer(Id) ->
case iot_api:get_endpoint(Id) of
case iot_api_client:get_endpoint(Id) of
undefined ->
{ok, 200, iot_util:json_error(404, <<"endpoint not found">>)};
{ok, EndpointInfo} ->
@ -49,7 +49,7 @@ handle_request("POST", "/endpoint/start", _, #{<<"id">> := Id}) when is_integer(
end;
handle_request("POST", "/endpoint/stop", _, #{<<"id">> := Id}) when is_integer(Id) ->
case iot_api:get_endpoint(Id) of
case iot_api_client:get_endpoint(Id) of
undefined ->
{ok, 200, iot_util:json_error(404, <<"endpoint not found">>)};
{ok, _} ->
@ -63,7 +63,7 @@ handle_request("POST", "/endpoint/stop", _, #{<<"id">> := Id}) when is_integer(I
end;
handle_request("POST", "/endpoint/restart", _, #{<<"id">> := Id}) when is_integer(Id) ->
case iot_api:get_endpoint(Id) of
case iot_api_client:get_endpoint(Id) of
undefined ->
{ok, 200, iot_util:json_error(404, <<"endpoint not found">>)};
{ok, EndpointInfo} ->

View File

@ -96,7 +96,7 @@ handle_info({tcp, Socket, <<?PACKET_REQUEST, PacketId:32, RequestBin/binary>>},
lager:debug("[ws_channel] auth uuid: ~p", [UUID]),
case iot_auth:check(Username, Token, UUID, Salt, Timestamp) of
true ->
case iot_api:get_host_by_uuid(UUID) of
case iot_api_client:get_host_by_uuid(UUID) of
undefined ->
lager:warning("[ws_channel] uuid: ~p, user: ~p, host not found", [UUID, Username]),
{stop, State};