修改整体的架构

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 ```markdown
# 📘 IoT API 接口文档 # 📘 IoT API 接口文档
> 模块:`iot_api` > 模块:`iot_api_client`
> 作者:**anlicheng** > 作者:**anlicheng**
> 创建时间2023-12-24 > 创建时间2023-12-24
> 数据格式:`application/json` > 数据格式:`application/json`
@ -406,4 +406,4 @@ GET /get_endpoint?id=<id>
"message": "Invalid parameter" "message": "Invalid parameter"
} }
} }
``` ```

View File

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

View File

@ -29,7 +29,7 @@ start_link() ->
%% modules => modules()} % optional %% modules => modules()} % optional
init([]) -> init([]) ->
SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600}, 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) -> ChildSpecs = lists:filtermap(fun(EndpointInfo) ->
case endpoint:endpoint_record(EndpointInfo) of case endpoint:endpoint_record(EndpointInfo) of
error -> error ->
@ -69,4 +69,4 @@ child_spec(Endpoint = #endpoint{id = Id}) ->
restart => permanent, restart => permanent,
shutdown => 2000, shutdown => 2000,
type => worker, type => worker,
modules => ['endpoint']}. modules => ['endpoint']}.

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 %% gen_statem:start_link/[3,4], this function is called by the new
%% process to initialize. %% process to initialize.
init([UUID]) -> 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}} -> {ok, #{<<"id">> := HostId, <<"authorize_status">> := AuthorizeStatus}} ->
%% host_id注册别名, HostPid %% host_id注册别名, HostPid
AliasName = get_alias_name(HostId), AliasName = get_alias_name(HostId),
@ -286,7 +286,7 @@ handle_event({call, From}, {attach_channel, ChannelPid}, StateName, State = #sta
?STATE_ACTIVATED -> ?STATE_ACTIVATED ->
erlang:monitor(process, ChannelPid), 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]), 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}]}; {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}) -> 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]), 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 case Status of
?HOST_NOT_JOINED -> ?HOST_NOT_JOINED ->
lager:debug("[iot_host] host: ~p, host_maybe_offline, host not joined, can not change to offline", [UUID]); lager:debug("[iot_host] host: ~p, host_maybe_offline, host not joined, can not change to offline", [UUID]);
?HOST_OFFLINE -> ?HOST_OFFLINE ->
lager:debug("[iot_host] host: ~p, host_maybe_offline, host now is offline, do nothing", [UUID]); lager:debug("[iot_host] host: ~p, host_maybe_offline, host now is offline, do nothing", [UUID]);
?HOST_ONLINE -> ?HOST_ONLINE ->
iot_api:change_host_status(UUID, ?HOST_OFFLINE) iot_api_client:change_host_status(UUID, ?HOST_OFFLINE)
end, end,
%% channel %% channel
@ -387,4 +387,4 @@ state_map(#state{host_id = HostId, uuid = UUID, has_session = HasSession, heartb
heartbeat_counter => HeartbeatCounter, heartbeat_counter => HeartbeatCounter,
channel_pid => ChannelPid, channel_pid => ChannelPid,
metrics => Metrics metrics => Metrics
}. }.

View File

@ -15,7 +15,7 @@ start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []). supervisor:start_link({local, ?MODULE}, ?MODULE, []).
init([]) -> 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}}. {ok, {#{strategy => one_for_one, intensity => 1000, period => 3600}, Specs}}.
@ -56,4 +56,4 @@ child_spec(UUID) ->
restart => permanent, restart => permanent,
shutdown => 2000, shutdown => 2000,
type => worker, type => worker,
modules => ['iot_host']}. modules => ['iot_host']}.

View File

@ -30,7 +30,7 @@ handle_request("POST", "/endpoint/run_statuses", _, Ids) when is_list(Ids) ->
{ok, 200, iot_util:json_data(Statuses)}; {ok, 200, iot_util:json_data(Statuses)};
handle_request("POST", "/endpoint/start", _, #{<<"id">> := Id}) when is_integer(Id) -> 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 -> undefined ->
{ok, 200, iot_util:json_error(404, <<"endpoint not found">>)}; {ok, 200, iot_util:json_error(404, <<"endpoint not found">>)};
{ok, EndpointInfo} -> {ok, EndpointInfo} ->
@ -49,7 +49,7 @@ handle_request("POST", "/endpoint/start", _, #{<<"id">> := Id}) when is_integer(
end; end;
handle_request("POST", "/endpoint/stop", _, #{<<"id">> := Id}) when is_integer(Id) -> 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 -> undefined ->
{ok, 200, iot_util:json_error(404, <<"endpoint not found">>)}; {ok, 200, iot_util:json_error(404, <<"endpoint not found">>)};
{ok, _} -> {ok, _} ->
@ -63,7 +63,7 @@ handle_request("POST", "/endpoint/stop", _, #{<<"id">> := Id}) when is_integer(I
end; end;
handle_request("POST", "/endpoint/restart", _, #{<<"id">> := Id}) when is_integer(Id) -> 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 -> undefined ->
{ok, 200, iot_util:json_error(404, <<"endpoint not found">>)}; {ok, 200, iot_util:json_error(404, <<"endpoint not found">>)};
{ok, EndpointInfo} -> {ok, EndpointInfo} ->
@ -207,4 +207,4 @@ handle_request("POST", "/endpoint/publish_metric", _, #{<<"route_key">> := Route
handle_request(_, Path, _, _) -> handle_request(_, Path, _, _) ->
Path1 = list_to_binary(Path), Path1 = list_to_binary(Path),
{ok, 200, iot_util:json_error(-1, <<"url: ", Path1/binary, " not found">>)}. {ok, 200, iot_util:json_error(-1, <<"url: ", Path1/binary, " not found">>)}.

View File

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