diff --git a/apps/iot/src/iot_api.erl b/apps/iot/src/iot_api.erl index abf5f34..4985683 100644 --- a/apps/iot/src/iot_api.erl +++ b/apps/iot/src/iot_api.erl @@ -12,13 +12,14 @@ -define(API_TOKEN, <<"wv6fGyBhl*7@AsD9">>). %% API --export([ai_event/1]). +-export([ai_event/1, get_event_period_settings/0]). -spec ai_event(Id :: integer()) -> no_return(). ai_event(Id) when is_integer(Id) -> Task = fun() -> Token = iot_util:md5(<>), - {ok, Url} = application:get_env(iot, api_url), + {ok, Url0} = application:get_env(iot, api_url), + Url = Url0 ++ "/api/v1/taskLog", Headers = [ {<<"content-type">>, <<"application/json">>} @@ -41,4 +42,30 @@ ai_event(Id) when is_integer(Id) -> lager:warning("[iot_api] send body: ~p, get error is: ~p", [Body, Reason]) end end, - iot_task:submit(Task). \ No newline at end of file + iot_task:submit(Task). + +-spec get_event_period_settings() -> {ok, Resp :: binary()} | {error, Reason :: any()}. +get_event_period_settings() -> + Token = iot_util:md5(<>), + {ok, Url0} = application:get_env(iot, api_url), + Url = Url0 ++ "/api/v1/eventPeriodSettings", + + Headers = [ + {<<"content-type">>, <<"application/json">>} + ], + ReqData = #{ + <<"token">> => Token + }, + Body = iolist_to_binary(jiffy:encode(ReqData, [force_utf8])), + case hackney:request(post, Url, Headers, Body, [{pool, false}]) of + {ok, 200, _, ClientRef} -> + {ok, RespBody} = hackney:body(ClientRef), + hackney:close(ClientRef), + {ok, RespBody}; + {ok, HttpCode, _, ClientRef} -> + {ok, RespBody} = hackney:body(ClientRef), + hackney:close(ClientRef), + {error, {http_error, HttpCode}}; + {error, Reason} -> + {error, Reason} + end. \ No newline at end of file diff --git a/apps/iot/src/iot_event_period_settings.erl b/apps/iot/src/iot_event_period_settings.erl index f0a9275..47cfff3 100644 --- a/apps/iot/src/iot_event_period_settings.erl +++ b/apps/iot/src/iot_event_period_settings.erl @@ -22,6 +22,12 @@ -define(TAB_NAME, iot_ets_event_period). +%% 更新周期, 单位:秒 +-define(UPDATE_TICKER, 300). + +%% 默认周期, 单位:秒 +-define(DEFAULT_THROTTLE, 300). + -record(state, { }). @@ -40,7 +46,7 @@ get_throttle(GroupKey) -> case ets:lookup(?TAB_NAME, GroupKey) of [] -> - 300; + ?DEFAULT_THROTTLE; [#period{throttle = Throttle}|_] -> Throttle end. @@ -62,6 +68,8 @@ start_link() -> {stop, Reason :: term()} | ignore). init([]) -> ets:new(?TAB_NAME, [public, set, named_table, {keypos, 2}]), + settings(iot_api:get_event_period_settings()), + erlang:start_timer(?UPDATE_TICKER * 1000, self(), update_ticker), {ok, #state{}}. %% @private @@ -74,13 +82,8 @@ init([]) -> {noreply, NewState :: #state{}, timeout() | hibernate} | {stop, Reason :: term(), Reply :: term(), NewState :: #state{}} | {stop, Reason :: term(), NewState :: #state{}}). -handle_call({get_throttle, GroupKey}, _From, State = #state{settings = Settings}) -> - case maps:find(GroupKey, Settings) of - {ok, Throttle} -> - {reply, {ok, Throttle}, State}; - error -> - {reply, {ok, 300}, State} - end. +handle_call(_Request, _From, State = #state{}) -> + {reply, ok, State}. %% @private %% @doc Handling cast messages @@ -97,7 +100,9 @@ handle_cast(_Request, State = #state{}) -> {noreply, NewState :: #state{}} | {noreply, NewState :: #state{}, timeout() | hibernate} | {stop, Reason :: term(), NewState :: #state{}}). -handle_info(_Info, State = #state{}) -> +handle_info({timeout, _, update_ticker}, State = #state{}) -> + settings(iot_api:get_event_period_settings()), + erlang:start_timer(?UPDATE_TICKER * 1000, self(), update_ticker), {noreply, State}. %% @private @@ -121,3 +126,21 @@ code_change(_OldVsn, State = #state{}, _Extra) -> %%%=================================================================== %%% Internal functions %%%=================================================================== + +-spec settings(tuple()) -> no_return(). +settings({ok, Resp}) when is_binary(Resp) -> + case catch jiffy:decode(Resp, [return_maps]) of + Settings when is_map(Settings) -> + lists:foreach(fun({GroupKey, Throttle}) -> + case is_integer(Throttle) andalso Throttle > 0 of + true -> + ets:insert(?TAB_NAME, #period{group_key = GroupKey, throttle = Throttle}); + false -> + ok + end + end, maps:to_list(Settings)); + Error -> + lager:debug("[iot_event_period_settings] get event_period_settings from api get error: ~p", [Error]) + end; +settings({error, Reason}) -> + lager:debug("[iot_event_period_settings] get event_period_settings from api get error: ~p", [Reason]). \ No newline at end of file diff --git a/config/sys-dev.config b/config/sys-dev.config index 2fb8c42..0919faf 100644 --- a/config/sys-dev.config +++ b/config/sys-dev.config @@ -18,7 +18,7 @@ {port, 18080} ]}, - {api_url, "http://39.98.184.67:8800/api/v1/taskLog"}, + {api_url, "http://39.98.184.67:8800"}, %% 目标服务器地址 {emqx_server, [ diff --git a/config/sys-prod.config b/config/sys-prod.config index e7f331d..ecbad58 100644 --- a/config/sys-prod.config +++ b/config/sys-prod.config @@ -23,7 +23,7 @@ {<<"test">>, <<"iot2023">>} ]}, - {api_url, "https://lgsiot.njau.edu.cn/api/v1/taskLog"}, + {api_url, "https://lgsiot.njau.edu.cn"}, %% 配置中电的数据转发, mqtt协议 {zhongdian, [