修改整体的架构
This commit is contained in:
parent
4ab2ea59bc
commit
92f0b2b100
@ -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`
|
||||||
|
|||||||
@ -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.
|
||||||
@ -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.
|
||||||
@ -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 ->
|
||||||
|
|||||||
@ -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,主机需要重新连接,才能保存状态的一致
|
||||||
@ -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}}.
|
||||||
|
|
||||||
@ -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} ->
|
||||||
@ -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};
|
||||||
Loading…
x
Reference in New Issue
Block a user