fix event_period_settings
This commit is contained in:
parent
9119aaff69
commit
2e1ea10a49
@ -12,13 +12,14 @@
|
|||||||
-define(API_TOKEN, <<"wv6fGyBhl*7@AsD9">>).
|
-define(API_TOKEN, <<"wv6fGyBhl*7@AsD9">>).
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([ai_event/1]).
|
-export([ai_event/1, get_event_period_settings/0]).
|
||||||
|
|
||||||
-spec ai_event(Id :: integer()) -> no_return().
|
-spec ai_event(Id :: integer()) -> no_return().
|
||||||
ai_event(Id) when is_integer(Id) ->
|
ai_event(Id) when is_integer(Id) ->
|
||||||
Task = fun() ->
|
Task = fun() ->
|
||||||
Token = iot_util:md5(<<?API_TOKEN/binary, (integer_to_binary(Id))/binary, ?API_TOKEN/binary>>),
|
Token = iot_util:md5(<<?API_TOKEN/binary, (integer_to_binary(Id))/binary, ?API_TOKEN/binary>>),
|
||||||
{ok, Url} = application:get_env(iot, api_url),
|
{ok, Url0} = application:get_env(iot, api_url),
|
||||||
|
Url = Url0 ++ "/api/v1/taskLog",
|
||||||
|
|
||||||
Headers = [
|
Headers = [
|
||||||
{<<"content-type">>, <<"application/json">>}
|
{<<"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])
|
lager:warning("[iot_api] send body: ~p, get error is: ~p", [Body, Reason])
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
iot_task:submit(Task).
|
iot_task:submit(Task).
|
||||||
|
|
||||||
|
-spec get_event_period_settings() -> {ok, Resp :: binary()} | {error, Reason :: any()}.
|
||||||
|
get_event_period_settings() ->
|
||||||
|
Token = iot_util:md5(<<?API_TOKEN/binary, ?API_TOKEN/binary>>),
|
||||||
|
{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.
|
||||||
@ -22,6 +22,12 @@
|
|||||||
|
|
||||||
-define(TAB_NAME, iot_ets_event_period).
|
-define(TAB_NAME, iot_ets_event_period).
|
||||||
|
|
||||||
|
%% 更新周期, 单位:秒
|
||||||
|
-define(UPDATE_TICKER, 300).
|
||||||
|
|
||||||
|
%% 默认周期, 单位:秒
|
||||||
|
-define(DEFAULT_THROTTLE, 300).
|
||||||
|
|
||||||
-record(state, {
|
-record(state, {
|
||||||
|
|
||||||
}).
|
}).
|
||||||
@ -40,7 +46,7 @@
|
|||||||
get_throttle(GroupKey) ->
|
get_throttle(GroupKey) ->
|
||||||
case ets:lookup(?TAB_NAME, GroupKey) of
|
case ets:lookup(?TAB_NAME, GroupKey) of
|
||||||
[] ->
|
[] ->
|
||||||
300;
|
?DEFAULT_THROTTLE;
|
||||||
[#period{throttle = Throttle}|_] ->
|
[#period{throttle = Throttle}|_] ->
|
||||||
Throttle
|
Throttle
|
||||||
end.
|
end.
|
||||||
@ -62,6 +68,8 @@ start_link() ->
|
|||||||
{stop, Reason :: term()} | ignore).
|
{stop, Reason :: term()} | ignore).
|
||||||
init([]) ->
|
init([]) ->
|
||||||
ets:new(?TAB_NAME, [public, set, named_table, {keypos, 2}]),
|
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{}}.
|
{ok, #state{}}.
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
@ -74,13 +82,8 @@ init([]) ->
|
|||||||
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
||||||
{stop, Reason :: term(), Reply :: term(), NewState :: #state{}} |
|
{stop, Reason :: term(), Reply :: term(), NewState :: #state{}} |
|
||||||
{stop, Reason :: term(), NewState :: #state{}}).
|
{stop, Reason :: term(), NewState :: #state{}}).
|
||||||
handle_call({get_throttle, GroupKey}, _From, State = #state{settings = Settings}) ->
|
handle_call(_Request, _From, State = #state{}) ->
|
||||||
case maps:find(GroupKey, Settings) of
|
{reply, ok, State}.
|
||||||
{ok, Throttle} ->
|
|
||||||
{reply, {ok, Throttle}, State};
|
|
||||||
error ->
|
|
||||||
{reply, {ok, 300}, State}
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
%% @doc Handling cast messages
|
%% @doc Handling cast messages
|
||||||
@ -97,7 +100,9 @@ handle_cast(_Request, State = #state{}) ->
|
|||||||
{noreply, NewState :: #state{}} |
|
{noreply, NewState :: #state{}} |
|
||||||
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
||||||
{stop, Reason :: term(), NewState :: #state{}}).
|
{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}.
|
{noreply, State}.
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
@ -121,3 +126,21 @@ code_change(_OldVsn, State = #state{}, _Extra) ->
|
|||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
%%% Internal functions
|
%%% 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]).
|
||||||
@ -18,7 +18,7 @@
|
|||||||
{port, 18080}
|
{port, 18080}
|
||||||
]},
|
]},
|
||||||
|
|
||||||
{api_url, "http://39.98.184.67:8800/api/v1/taskLog"},
|
{api_url, "http://39.98.184.67:8800"},
|
||||||
|
|
||||||
%% 目标服务器地址
|
%% 目标服务器地址
|
||||||
{emqx_server, [
|
{emqx_server, [
|
||||||
|
|||||||
@ -23,7 +23,7 @@
|
|||||||
{<<"test">>, <<"iot2023">>}
|
{<<"test">>, <<"iot2023">>}
|
||||||
]},
|
]},
|
||||||
|
|
||||||
{api_url, "https://lgsiot.njau.edu.cn/api/v1/taskLog"},
|
{api_url, "https://lgsiot.njau.edu.cn"},
|
||||||
|
|
||||||
%% 配置中电的数据转发, mqtt协议
|
%% 配置中电的数据转发, mqtt协议
|
||||||
{zhongdian, [
|
{zhongdian, [
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user