diff --git a/apps/endpoint/src/endpoint_adapter_sup.erl b/apps/endpoint/src/endpoint_adapter_sup.erl index a64f319..09b63f1 100644 --- a/apps/endpoint/src/endpoint_adapter_sup.erl +++ b/apps/endpoint/src/endpoint_adapter_sup.erl @@ -10,6 +10,7 @@ -export([start_link/0]). -export([ensured_endpoint_started/1, delete_endpoint/1]). +-export([start_endpoints/1]). -export([init/1]). @@ -29,21 +30,15 @@ start_link() -> %% modules => modules()} % optional init([]) -> SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600}, - Endpoints = iot_api_client:get_all_endpoints(), - ChildSpecs = lists:filtermap(fun(EndpointInfo) -> - case endpoint:endpoint_record(EndpointInfo) of - error -> - false; - {ok, Endpoint} -> - case endpoint:is_support(endpoint:get_protocol(Endpoint)) of - true -> - {true, child_spec(Endpoint)}; - false -> - false - end - end - end, Endpoints), - {ok, {SupFlags, ChildSpecs}}. + {ok, {SupFlags, []}}. + +-spec start_endpoints(Endpoints :: list()) -> ok. +start_endpoints(Endpoints) when is_list(Endpoints) -> + lists:foreach(fun(Endpoint) -> + Spec = child_spec(Endpoint), + {ok, _} = supervisor:start_child(?MODULE, Spec) + end, Endpoints), + ok. -spec ensured_endpoint_started(Endpoint :: #endpoint{}) -> {ok, Pid :: pid()} | {error, Reason :: any()}. ensured_endpoint_started(Endpoint = #endpoint{}) -> diff --git a/apps/iot/src/iot_app.erl b/apps/iot/src/iot_app.erl index 08c37bc..fc8b932 100644 --- a/apps/iot/src/iot_app.erl +++ b/apps/iot/src/iot_app.erl @@ -22,6 +22,8 @@ start(_StartType, _StartArgs) -> iot_mnesia:init() end, + start_endpoints(), + %% 启动http服务。当前 supervisor 初始化依赖本机 simulator API,因此保留此顺序。 start_http_server(), @@ -36,6 +38,18 @@ stop(_State) -> %% internal functions +start_endpoints() -> + EndpointInfos = iot_api_client:get_all_endpoints(), + Endpoints = lists:filtermap(fun(EndpointInfo) -> + case endpoint:endpoint_record(EndpointInfo) of + error -> + false; + {ok, Endpoint} -> + {true, Endpoint} + end + end, EndpointInfos), + ok = endpoint_adapter_sup:start_endpoints(Endpoints). + start_http_server() -> {ok, Props} = application:get_env(iot, http_server), Acceptors = proplists:get_value(acceptors, Props, 50),