diff --git a/docs/iot_api.md b/docs/iot_api.md index a1f14bb..e48caf4 100644 --- a/docs/iot_api.md +++ b/docs/iot_api.md @@ -3,7 +3,7 @@ ```markdown # 📘 IoT API 接口文档 -> 模块:`iot_api` +> 模块:`iot_api_client` > 作者:**anlicheng** > 创建时间:2023-12-24 > 数据格式:`application/json` @@ -406,4 +406,4 @@ GET /get_endpoint?id= "message": "Invalid parameter" } } -``` \ No newline at end of file +``` diff --git a/src/iot_auth.erl b/src/adapters/auth/iot_auth.erl similarity index 100% rename from src/iot_auth.erl rename to src/adapters/auth/iot_auth.erl diff --git a/src/iot_api.erl b/src/adapters/control_api/iot_api_client.erl similarity index 87% rename from src/iot_api.erl rename to src/adapters/control_api/iot_api_client.erl index a986f7a..5b68cbf 100644 --- a/src/iot_api.erl +++ b/src/adapters/control_api/iot_api_client.erl @@ -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. \ No newline at end of file + end. diff --git a/src/util/http_client.erl b/src/adapters/http/http_client.erl similarity index 77% rename from src/util/http_client.erl rename to src/adapters/http/http_client.erl index fb064ab..fcc47df 100644 --- a/src/util/http_client.erl +++ b/src/adapters/http/http_client.erl @@ -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. \ No newline at end of file + end. diff --git a/src/iot_http_client.erl b/src/adapters/http/iot_http_client.erl similarity index 100% rename from src/iot_http_client.erl rename to src/adapters/http/iot_http_client.erl diff --git a/src/influxdb/influx_client.erl b/src/adapters/influxdb/influx_client.erl similarity index 100% rename from src/influxdb/influx_client.erl rename to src/adapters/influxdb/influx_client.erl diff --git a/src/influxdb/influx_point.erl b/src/adapters/influxdb/influx_point.erl similarity index 100% rename from src/influxdb/influx_point.erl rename to src/adapters/influxdb/influx_point.erl diff --git a/src/mysql/mysql_pool.erl b/src/adapters/mysql/mysql_pool.erl similarity index 100% rename from src/mysql/mysql_pool.erl rename to src/adapters/mysql/mysql_pool.erl diff --git a/src/mysql/mysql_provider.erl b/src/adapters/mysql/mysql_provider.erl similarity index 100% rename from src/mysql/mysql_provider.erl rename to src/adapters/mysql/mysql_provider.erl diff --git a/src/redis/redis_client.erl b/src/adapters/redis/redis_client.erl similarity index 100% rename from src/redis/redis_client.erl rename to src/adapters/redis/redis_client.erl diff --git a/src/data_format/line_format.erl b/src/codec/line_format.erl similarity index 100% rename from src/data_format/line_format.erl rename to src/codec/line_format.erl diff --git a/src/message/message_codec.erl b/src/codec/message_codec.erl similarity index 100% rename from src/message/message_codec.erl rename to src/codec/message_codec.erl diff --git a/src/mocker/endpoint_kafka_test.erl b/src/devtools/endpoint_kafka_test.erl similarity index 100% rename from src/mocker/endpoint_kafka_test.erl rename to src/devtools/endpoint_kafka_test.erl diff --git a/src/mocker/endpoint_mqtt_subscriber.erl b/src/devtools/endpoint_mqtt_subscriber.erl similarity index 100% rename from src/mocker/endpoint_mqtt_subscriber.erl rename to src/devtools/endpoint_mqtt_subscriber.erl diff --git a/src/mocker/eval_test.erl b/src/devtools/eval_test.erl similarity index 100% rename from src/mocker/eval_test.erl rename to src/devtools/eval_test.erl diff --git a/src/iot_logger.erl b/src/devtools/iot_logger.erl similarity index 100% rename from src/iot_logger.erl rename to src/devtools/iot_logger.erl diff --git a/src/mocker/iot_mock.erl b/src/devtools/iot_mock.erl similarity index 100% rename from src/mocker/iot_mock.erl rename to src/devtools/iot_mock.erl diff --git a/src/iot_observer.erl b/src/devtools/iot_observer.erl similarity index 100% rename from src/iot_observer.erl rename to src/devtools/iot_observer.erl diff --git a/src/endpoint/endpoint_sup.erl b/src/endpoint/endpoint_sup.erl index ad59018..d998103 100644 --- a/src/endpoint/endpoint_sup.erl +++ b/src/endpoint/endpoint_sup.erl @@ -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 -> @@ -69,4 +69,4 @@ child_spec(Endpoint = #endpoint{id = Id}) -> restart => permanent, shutdown => 2000, type => worker, - modules => ['endpoint']}. \ No newline at end of file + modules => ['endpoint']}. diff --git a/src/iot_event_stream_observer.erl b/src/host/iot_event_stream_observer.erl similarity index 100% rename from src/iot_event_stream_observer.erl rename to src/host/iot_event_stream_observer.erl diff --git a/src/iot_host.erl b/src/host/iot_host.erl similarity index 98% rename from src/iot_host.erl rename to src/host/iot_host.erl index ebd6f14..8235430 100644 --- a/src/iot_host.erl +++ b/src/host/iot_host.erl @@ -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,主机需要重新连接,才能保存状态的一致 @@ -387,4 +387,4 @@ state_map(#state{host_id = HostId, uuid = UUID, has_session = HasSession, heartb heartbeat_counter => HeartbeatCounter, channel_pid => ChannelPid, metrics => Metrics - }. \ No newline at end of file + }. diff --git a/src/iot_host_sup.erl b/src/host/iot_host_sup.erl similarity index 94% rename from src/iot_host_sup.erl rename to src/host/iot_host_sup.erl index 4762613..cfc6d51 100644 --- a/src/iot_host_sup.erl +++ b/src/host/iot_host_sup.erl @@ -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}}. @@ -56,4 +56,4 @@ child_spec(UUID) -> restart => permanent, shutdown => 2000, type => worker, - modules => ['iot_host']}. \ No newline at end of file + modules => ['iot_host']}. diff --git a/src/http_handlers/container_handler.erl b/src/transport/http/container_handler.erl similarity index 100% rename from src/http_handlers/container_handler.erl rename to src/transport/http/container_handler.erl diff --git a/src/http_handlers/endpoint_handler.erl b/src/transport/http/endpoint_handler.erl similarity index 98% rename from src/http_handlers/endpoint_handler.erl rename to src/transport/http/endpoint_handler.erl index b390b62..0928816 100644 --- a/src/http_handlers/endpoint_handler.erl +++ b/src/transport/http/endpoint_handler.erl @@ -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} -> @@ -207,4 +207,4 @@ handle_request("POST", "/endpoint/publish_metric", _, #{<<"route_key">> := Route handle_request(_, Path, _, _) -> Path1 = list_to_binary(Path), - {ok, 200, iot_util:json_error(-1, <<"url: ", Path1/binary, " not found">>)}. \ No newline at end of file + {ok, 200, iot_util:json_error(-1, <<"url: ", Path1/binary, " not found">>)}. diff --git a/src/http_handlers/event_stream_handler.erl b/src/transport/http/event_stream_handler.erl similarity index 100% rename from src/http_handlers/event_stream_handler.erl rename to src/transport/http/event_stream_handler.erl diff --git a/src/http_handlers/host_handler.erl b/src/transport/http/host_handler.erl similarity index 100% rename from src/http_handlers/host_handler.erl rename to src/transport/http/host_handler.erl diff --git a/src/http_handlers/http_protocol.erl b/src/transport/http/http_protocol.erl similarity index 100% rename from src/http_handlers/http_protocol.erl rename to src/transport/http/http_protocol.erl diff --git a/src/tcp/tcp_channel.erl b/src/transport/tcp/tcp_channel.erl similarity index 99% rename from src/tcp/tcp_channel.erl rename to src/transport/tcp/tcp_channel.erl index cf344a6..1c76dc1 100644 --- a/src/tcp/tcp_channel.erl +++ b/src/transport/tcp/tcp_channel.erl @@ -96,7 +96,7 @@ handle_info({tcp, Socket, <>}, 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}; @@ -192,4 +192,4 @@ terminate(Reason, #state{}) -> ok. code_change(_OldVsn, State, _Extra) -> - {ok, State}. \ No newline at end of file + {ok, State}. diff --git a/src/iot_udp_handler.erl b/src/transport/udp/iot_udp_handler.erl similarity index 100% rename from src/iot_udp_handler.erl rename to src/transport/udp/iot_udp_handler.erl