diff --git a/apps/iot/src/endpoint/endpoint_subscription.erl b/apps/iot/src/endpoint/endpoint_subscription.erl index 48ceffc..4bcd702 100644 --- a/apps/iot/src/endpoint/endpoint_subscription.erl +++ b/apps/iot/src/endpoint/endpoint_subscription.erl @@ -13,7 +13,7 @@ %% API -export([start_link/0]). --export([subscribe/2, publish/2]). +-export([subscribe/2, publish/2, get_subscribers/0]). -export([match_components/2, is_valid_components/1, of_components/1]). %% gen_server callbacks @@ -45,6 +45,10 @@ subscribe(Topic, SubscriberPid) when is_binary(Topic), is_pid(SubscriberPid) -> gen_server:call(?SERVER, {subscribe, Topic, SubscriberPid}). +-spec get_subscribers() -> {ok, Subscribers :: map()}. +get_subscribers() -> + gen_server:call(?SERVER, get_subscribers). + -spec publish(RouteKey :: binary(), Content :: binary()) -> no_return(). publish(RouteKey, Content) when is_binary(RouteKey), is_binary(Content) -> gen_server:cast(?SERVER, {publish, RouteKey, Content}). @@ -78,6 +82,8 @@ init([]) -> {stop, Reason :: term(), Reply :: term(), NewState :: #state{}} | {stop, Reason :: term(), NewState :: #state{}}). %% 同一个SubscriberPid只能订阅同一个topic一次 +handle_call(get_subscribers, _From, State = #state{subscribers = Subscribers}) -> + {reply, {ok, Subscribers}, State}; handle_call({subscribe, Topic, SubscriberPid}, _From, State = #state{subscribers = Subscribers}) -> Components = of_components(Topic), case is_valid_components(Components) of