diff --git a/apps/iot/src/endpoint/endpoint.erl b/apps/iot/src/endpoint/endpoint.erl index 0026b97..da0c9a3 100644 --- a/apps/iot/src/endpoint/endpoint.erl +++ b/apps/iot/src/endpoint/endpoint.erl @@ -39,14 +39,16 @@ get_name(Id) when is_integer(Id) -> list_to_atom("endpoint:" ++ integer_to_list(Id)). -spec get_pid(Id :: integer()) -> undefined | pid(). -get_pid(Id) when is_binary(Id) -> +get_pid(Id) when is_integer(Id) -> whereis(get_name(Id)). +-spec get_alias_name(Name :: binary()) -> atom(). get_alias_name(Name) when is_binary(Name) -> list_to_atom("endpoint:" ++ binary_to_list(Name)). +-spec get_alias_pid(Name :: binary()) -> undefined | pid(). get_alias_pid(Name) when is_binary(Name) -> - global:whereis_name(get_alias_name(Name)). + iot_name_server:whereis_alias(get_alias_name(Name)). -spec forward(Pid :: pid(), ServiceId :: binary(), Format :: binary(), Metric :: binary()) -> no_return(). forward(Pid, ServiceId, Format, Metric) when is_pid(Pid), is_binary(ServiceId), is_binary(Format), is_binary(Metric) -> diff --git a/apps/iot/src/endpoint/endpoint_sup.erl b/apps/iot/src/endpoint/endpoint_sup.erl index 101f1ae..a2beb50 100644 --- a/apps/iot/src/endpoint/endpoint_sup.erl +++ b/apps/iot/src/endpoint/endpoint_sup.erl @@ -29,7 +29,7 @@ start_link() -> %% modules => modules()} % optional init([]) -> SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600}, - {ok, Endpoints} = endpoint_bo:get_all_endpoints(), + Endpoints = endpoint_bo:get_all_endpoints(), ChildSpecs = lists:flatmap(fun(EndpointInfo) -> case endpoint_bo:endpoint_record(EndpointInfo) of error -> diff --git a/apps/iot/src/http_handlers/endpoint_handler.erl b/apps/iot/src/http_handlers/endpoint_handler.erl index 6d34389..0e14e16 100644 --- a/apps/iot/src/http_handlers/endpoint_handler.erl +++ b/apps/iot/src/http_handlers/endpoint_handler.erl @@ -54,7 +54,7 @@ handle_request("POST", "/endpoint/stop", _, #{<<"id">> := Id}) when is_integer(I {ok, 200, iot_util:json_error(404, <<"endpoint not found">>)}; {ok, _} -> case endpoint_sup:delete_endpoint(Id) of - {ok, Pid} when is_pid(Pid) -> + ok -> {ok, 200, iot_util:json_data(<<"success">>)}; {error, Reason} -> lager:warning("[endpoint_handler] stop endpoint id: ~p, get error: ~p", [Id, Reason]), diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index 45052d4..eedef03 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -350,7 +350,7 @@ handle_event(cast, {handle, {data, #data{service_id = ServiceId, device_uuid = D case iot_device:is_activated(Device) of true -> RouteKey = get_route_key(RouteKey0), - case endpoint:get_pid(RouteKey) of + case endpoint:get_alias_pid(RouteKey) of undefined -> ok; EndpointPid -> diff --git a/apps/iot/src/iot_name_server.erl b/apps/iot/src/iot_name_server.erl index 8bbc0c7..c98fc41 100644 --- a/apps/iot/src/iot_name_server.erl +++ b/apps/iot/src/iot_name_server.erl @@ -13,7 +13,7 @@ %% API -export([start_link/0]). --export([whereis/1, register/2]). +-export([whereis_alias/1, register/2]). %% gen_server callbacks -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). @@ -35,8 +35,8 @@ register(Name, Pid) when is_atom(Name), is_pid(Pid) -> gen_server:call(?SERVER, {register, Name, Pid}). --spec whereis(Name :: atom()) -> undefined | pid(). -whereis(Name) when is_atom(Name) -> +-spec whereis_alias(Name :: atom()) -> undefined | pid(). +whereis_alias(Name) when is_atom(Name) -> case ets:lookup(?TAB, Name) of [] -> undefined;