fix endpoint
This commit is contained in:
parent
d25cb95a61
commit
6acedc44cb
200
apps/endpoint/src/endpoint_mysql.erl
Normal file
200
apps/endpoint/src/endpoint_mysql.erl
Normal file
@ -0,0 +1,200 @@
|
|||||||
|
|
||||||
|
%%%-------------------------------------------------------------------
|
||||||
|
%%% @author aresei
|
||||||
|
%%% @copyright (C) 2023, <COMPANY>
|
||||||
|
%%% @doc
|
||||||
|
%%%
|
||||||
|
%%% @end
|
||||||
|
%%% Created : 06. 7月 2023 12:02
|
||||||
|
%%%-------------------------------------------------------------------
|
||||||
|
-module(endpoint_mysql).
|
||||||
|
|
||||||
|
-include("endpoint.hrl").
|
||||||
|
-behaviour(gen_statem).
|
||||||
|
|
||||||
|
%% API
|
||||||
|
-export([start_link/2]).
|
||||||
|
-export([get_name/1, get_pid/1, forward/4, get_stat/1, reload/2, cleanup/1]).
|
||||||
|
|
||||||
|
%% gen_statem callbacks
|
||||||
|
-export([init/1, handle_event/4, terminate/3, code_change/4, callback_mode/0]).
|
||||||
|
|
||||||
|
%% 消息重发间隔
|
||||||
|
-define(RETRY_INTERVAL, 5000).
|
||||||
|
|
||||||
|
-record(state, {
|
||||||
|
endpoint :: #endpoint{},
|
||||||
|
buffer :: endpoint_buffer:buffer(),
|
||||||
|
pool_pid :: undefined | pid()
|
||||||
|
}).
|
||||||
|
|
||||||
|
%%%===================================================================
|
||||||
|
%%% API
|
||||||
|
%%%===================================================================
|
||||||
|
|
||||||
|
-spec get_name(Id :: integer()) -> atom().
|
||||||
|
get_name(Id) when is_integer(Id) ->
|
||||||
|
list_to_atom("endpoint:" ++ integer_to_list(Id)).
|
||||||
|
|
||||||
|
-spec get_pid(Name :: binary()) -> undefined | pid().
|
||||||
|
get_pid(Name) when is_binary(Name) ->
|
||||||
|
whereis(get_name(Name)).
|
||||||
|
|
||||||
|
-spec forward(Pid :: undefined | pid(), LocationCode :: binary(), Fields :: list(), Timestamp :: integer()) -> no_return().
|
||||||
|
forward(undefined, _, _, _) ->
|
||||||
|
ok;
|
||||||
|
forward(Pid, LocationCode, Fields, Timestamp) when is_pid(Pid), is_binary(LocationCode), is_list(Fields); is_binary(Fields), is_integer(Timestamp) ->
|
||||||
|
gen_statem:cast(Pid, {forward, LocationCode, Fields, Timestamp}).
|
||||||
|
|
||||||
|
reload(Pid, NEndpoint = #endpoint{}) when is_pid(Pid) ->
|
||||||
|
gen_statem:cast(Pid, {reload, NEndpoint}).
|
||||||
|
|
||||||
|
-spec get_stat(Pid :: pid()) -> {ok, Stat :: #{}}.
|
||||||
|
get_stat(Pid) when is_pid(Pid) ->
|
||||||
|
gen_statem:call(Pid, get_stat, 5000).
|
||||||
|
|
||||||
|
-spec cleanup(Pid :: pid()) -> ok.
|
||||||
|
cleanup(Pid) when is_pid(Pid) ->
|
||||||
|
gen_statem:cast(Pid, cleanup).
|
||||||
|
|
||||||
|
%% @doc Creates a gen_statem process which calls Module:init/1 to
|
||||||
|
%% initialize. To ensure a synchronized start-up procedure, this
|
||||||
|
%% function does not return until Module:init/1 has returned.
|
||||||
|
start_link(Name, Endpoint = #endpoint{}) when is_atom(Name) ->
|
||||||
|
gen_statem:start_link({local, Name}, ?MODULE, [Endpoint], []).
|
||||||
|
|
||||||
|
%%%===================================================================
|
||||||
|
%%% gen_statem callbacks
|
||||||
|
%%%===================================================================
|
||||||
|
|
||||||
|
%% @private
|
||||||
|
%% @doc Whenever a gen_statem is started using gen_statem:start/[3,4] or
|
||||||
|
%% gen_statem:start_link/[3,4], this function is called by the new
|
||||||
|
%% process to initialize.
|
||||||
|
init([Endpoint]) ->
|
||||||
|
erlang:process_flag(trap_exit, true),
|
||||||
|
%% 创建转发器, 避免阻塞当前进程的创建,因此采用了延时初始化的机制
|
||||||
|
erlang:start_timer(0, self(), create_postman),
|
||||||
|
%% 初始化存储
|
||||||
|
Buffer = endpoint_buffer:new(Endpoint, 10),
|
||||||
|
|
||||||
|
{ok, disconnected, #state{endpoint = Endpoint, buffer = Buffer}}.
|
||||||
|
|
||||||
|
%% @private
|
||||||
|
%% @doc This function is called by a gen_statem when it needs to find out
|
||||||
|
%% the callback mode of the callback module.
|
||||||
|
callback_mode() ->
|
||||||
|
handle_event_function.
|
||||||
|
|
||||||
|
%% @private
|
||||||
|
%% @doc There should be one instance of this function for each possible
|
||||||
|
%% state name. If callback_mode is state_functions, one of these
|
||||||
|
%% functions is called when gen_statem receives and event from
|
||||||
|
%% call/2, cast/2, or as a normal process message.
|
||||||
|
|
||||||
|
handle_event(cast, {forward, LocationCode, Fields, Timestamp}, _StateName, State = #state{buffer = Buffer, endpoint = #endpoint{name = Name}}) ->
|
||||||
|
lager:debug("[iot_endpoint] name: ~p, match location_code: ~p", [Name, LocationCode]),
|
||||||
|
NBuffer = endpoint_buffer:append({LocationCode, Fields, Timestamp}, Buffer),
|
||||||
|
{keep_state, State#state{buffer = NBuffer}};
|
||||||
|
|
||||||
|
handle_event(cast, cleanup, _StateName, State = #state{buffer = Buffer}) ->
|
||||||
|
endpoint_buffer:cleanup(Buffer),
|
||||||
|
{keep_state, State};
|
||||||
|
|
||||||
|
handle_event(info, {timeout, _, create_postman}, disconnected,
|
||||||
|
State = #state{buffer = Buffer, endpoint = #endpoint{name = Name, config = #mysql_endpoint{host = Host, port = Port, username = Username, password = Password, database = Database, table_name = TableName, pool_size = PoolSize}}}) ->
|
||||||
|
lager:debug("[iot_endpoint] endpoint: ~p, create postman", [Name]),
|
||||||
|
WorkerArgs = [
|
||||||
|
{host, binary_to_list(Host)},
|
||||||
|
{port, Port},
|
||||||
|
{user, binary_to_list(Username)},
|
||||||
|
{password, binary_to_list(Password)},
|
||||||
|
{keep_alive, true},
|
||||||
|
{database, binary_to_list(Database)},
|
||||||
|
{queries, [<<"set names utf8">>]}
|
||||||
|
],
|
||||||
|
|
||||||
|
%% 启动工作的线程池
|
||||||
|
case poolboy:start_link([{size, PoolSize}, {max_overflow, PoolSize}, {worker_module, mysql}], WorkerArgs) of
|
||||||
|
{ok, PoolPid} ->
|
||||||
|
NBuffer = endpoint_buffer:trigger_n(Buffer),
|
||||||
|
{next_state, connected, State#state{pool_pid = PoolPid, buffer = NBuffer}};
|
||||||
|
ignore ->
|
||||||
|
erlang:start_timer(?RETRY_INTERVAL, self(), create_postman),
|
||||||
|
{keep_state, State};
|
||||||
|
{error, Reason} ->
|
||||||
|
lager:warning("[mqtt_postman] start connect pool, get error: ~p", [Reason]),
|
||||||
|
erlang:start_timer(?RETRY_INTERVAL, self(), create_postman),
|
||||||
|
{keep_state, State}
|
||||||
|
end;
|
||||||
|
|
||||||
|
%% 获取当前统计信息
|
||||||
|
handle_event({call, From}, get_stat, _StateName, State = #state{buffer = Buffer}) ->
|
||||||
|
Stat = endpoint_buffer:stat(Buffer),
|
||||||
|
|
||||||
|
{keep_state, State, [{reply, From, Stat}]};
|
||||||
|
|
||||||
|
%% 离线时,忽略数据发送逻辑
|
||||||
|
handle_event(info, {next_data, _Id, _LocationCode, _Message}, disconnected, State) ->
|
||||||
|
{keep_state, State};
|
||||||
|
%% 发送数据到mqtt服务器
|
||||||
|
handle_event(info, {next_data, Id, _LocationCode, Fields}, connected,
|
||||||
|
State = #state{pool_pid = PoolPid, buffer = Buffer, endpoint = #endpoint{name = Name, config = #mysql_endpoint{table_name = Table}}}) ->
|
||||||
|
|
||||||
|
{ok, InsertSql, Values} = insert_sql(Table, Fields),
|
||||||
|
case poolboy:transaction(PoolPid, fun(ConnPid) -> mysql:query(ConnPid, InsertSql, Values) end) of
|
||||||
|
ok ->
|
||||||
|
NBuffer = endpoint_buffer:ack(Id, Buffer),
|
||||||
|
{keep_state, State#state{buffer = NBuffer}};
|
||||||
|
Error ->
|
||||||
|
lager:warning("[endpoint_mysql] endpoint: ~p, insert mysql get error: ~p", [Name, Error]),
|
||||||
|
{keep_state, State}
|
||||||
|
end;
|
||||||
|
|
||||||
|
%% postman进程挂掉时,重新建立新的
|
||||||
|
handle_event(info, {'EXIT', PoolPid, Reason}, connected, State = #state{endpoint = #endpoint{name = Name}, pool_pid = PoolPid}) ->
|
||||||
|
lager:warning("[enpoint_mqtt] endpoint: ~p, conn pid exit with reason: ~p", [Name, Reason]),
|
||||||
|
erlang:start_timer(?RETRY_INTERVAL, self(), create_postman),
|
||||||
|
{next_state, disconnected, State#state{pool_pid = undefined}};
|
||||||
|
|
||||||
|
%% @private
|
||||||
|
%% @doc If callback_mode is handle_event_function, then whenever a
|
||||||
|
%% gen_statem receives an event from call/2, cast/2, or as a normal
|
||||||
|
%% process message, this function is called.
|
||||||
|
handle_event(EventType, Event, StateName, State) ->
|
||||||
|
lager:warning("[iot_endpoint] unknown message, event_type: ~p, event: ~p, state_name: ~p, state: ~p", [EventType, Event, StateName, State]),
|
||||||
|
{keep_state, State}.
|
||||||
|
|
||||||
|
%% @private
|
||||||
|
%% @doc This function is called by a gen_statem when it is about to
|
||||||
|
%% terminate. It should be the opposite of Module:init/1 and do any
|
||||||
|
%% necessary cleaning up. When it returns, the gen_statem terminates with
|
||||||
|
%% Reason. The return value is ignored.
|
||||||
|
terminate(Reason, _StateName, #state{endpoint = #endpoint{name = Name}, buffer = Buffer}) ->
|
||||||
|
lager:debug("[iot_endpoint] endpoint: ~p, terminate with reason: ~p", [Name, Reason]),
|
||||||
|
endpoint_buffer:cleanup(Buffer),
|
||||||
|
ok.
|
||||||
|
|
||||||
|
%% @private
|
||||||
|
%% @doc Convert process state when code is changed
|
||||||
|
code_change(_OldVsn, StateName, State = #state{}, _Extra) ->
|
||||||
|
{ok, StateName, State}.
|
||||||
|
|
||||||
|
%%%===================================================================
|
||||||
|
%%% Internal functions
|
||||||
|
%%%===================================================================
|
||||||
|
|
||||||
|
-spec insert_sql(Table :: binary(), Fields :: list()) -> {ok, Sql :: binary(), Values :: list()}.
|
||||||
|
insert_sql(Table, Fields) when is_binary(Table), is_list(Fields) ->
|
||||||
|
{Keys, Values} = kvs(Fields),
|
||||||
|
|
||||||
|
FieldSql = iolist_to_binary(lists:join(<<", ">>, Keys)),
|
||||||
|
Placeholders = lists:duplicate(length(Keys), <<"?">>),
|
||||||
|
ValuesPlaceholder = iolist_to_binary(lists:join(<<", ">>, Placeholders)),
|
||||||
|
|
||||||
|
{ok, <<"INSERT INTO ", Table/binary, "(", FieldSql/binary, ") VALUES(", ValuesPlaceholder/binary, ")">>, Values}.
|
||||||
|
|
||||||
|
-spec kvs(Fields :: list()) -> {Keys :: list(), Values :: list()}.
|
||||||
|
kvs(Fields) when is_list(Fields) ->
|
||||||
|
{Keys0, Values0} = lists:foldl(fun({K, V}, {Acc0, Acc1}) -> {[K|Acc0], [V|Acc1]} end, {[], []}, Fields),
|
||||||
|
{lists:reverse(Keys0), lists:reverse(Values0)}.
|
||||||
@ -1,374 +0,0 @@
|
|||||||
|
|
||||||
%%%-------------------------------------------------------------------
|
|
||||||
%%% @author aresei
|
|
||||||
%%% @copyright (C) 2023, <COMPANY>
|
|
||||||
%%% @doc
|
|
||||||
%%%
|
|
||||||
%%% @end
|
|
||||||
%%% Created : 06. 7月 2023 12:02
|
|
||||||
%%%-------------------------------------------------------------------
|
|
||||||
-module(gen_endpoint).
|
|
||||||
|
|
||||||
-include("endpoint.hrl").
|
|
||||||
-behaviour(gen_statem).
|
|
||||||
|
|
||||||
%% API
|
|
||||||
-export([start_link/3]).
|
|
||||||
-export([get_name/1, get_pid/1, forward/4, get_stat/1, reload/2, clean_up/1, get_mapper_fun/1]).
|
|
||||||
|
|
||||||
%% gen_statem callbacks
|
|
||||||
-export([init/1, handle_event/4, terminate/3, code_change/4, callback_mode/0]).
|
|
||||||
|
|
||||||
%% 消息重发间隔
|
|
||||||
-define(RETRY_INTERVAL, 5000).
|
|
||||||
|
|
||||||
-record(state, {
|
|
||||||
endpoint,
|
|
||||||
mod :: atom(),
|
|
||||||
mod_state :: any(),
|
|
||||||
%% ets存储
|
|
||||||
last_id = 1 :: integer(),
|
|
||||||
cursor :: integer(),
|
|
||||||
tid :: reference(),
|
|
||||||
%% 定时器
|
|
||||||
timer_map = #{},
|
|
||||||
%% 窗口大小,允许最大的未确认消息数
|
|
||||||
window_size = 10,
|
|
||||||
%% 未确认的消息数
|
|
||||||
flight_num = 0,
|
|
||||||
%% 记录成功处理的消息数
|
|
||||||
acc_num = 0
|
|
||||||
}).
|
|
||||||
|
|
||||||
-callback init(Args :: term()) ->
|
|
||||||
{ok, State :: term()} | {ok, State :: term(), timeout() | hibernate | {continue, term()}} |
|
|
||||||
{stop, Reason :: term()} | ignore.
|
|
||||||
|
|
||||||
-callback handle_data(Request :: term(), From :: from(),
|
|
||||||
State :: term()) ->
|
|
||||||
{reply, Reply :: term(), NewState :: term()} |
|
|
||||||
{reply, Reply :: term(), NewState :: term(), timeout() | hibernate | {continue, term()}} |
|
|
||||||
{noreply, NewState :: term()} |
|
|
||||||
{noreply, NewState :: term(), timeout() | hibernate | {continue, term()}} |
|
|
||||||
{stop, Reason :: term(), Reply :: term(), NewState :: term()} |
|
|
||||||
{stop, Reason :: term(), NewState :: term()}.
|
|
||||||
|
|
||||||
-callback handle_cast(Request :: term(), State :: term()) ->
|
|
||||||
{noreply, NewState :: term()} |
|
|
||||||
{noreply, NewState :: term(), timeout() | hibernate | {continue, term()}} |
|
|
||||||
{stop, Reason :: term(), NewState :: term()}.
|
|
||||||
-callback handle_info(Info :: timeout | term(), State :: term()) ->
|
|
||||||
{noreply, NewState :: term()} |
|
|
||||||
{noreply, NewState :: term(), timeout() | hibernate | {continue, term()}} |
|
|
||||||
{stop, Reason :: term(), NewState :: term()}.
|
|
||||||
-callback handle_continue(Info :: term(), State :: term()) ->
|
|
||||||
{noreply, NewState :: term()} |
|
|
||||||
{noreply, NewState :: term(), timeout() | hibernate | {continue, term()}} |
|
|
||||||
{stop, Reason :: term(), NewState :: term()}.
|
|
||||||
-callback terminate(Reason :: (normal | shutdown | {shutdown, term()} |
|
|
||||||
term()),
|
|
||||||
State :: term()) ->
|
|
||||||
term().
|
|
||||||
-callback code_change(OldVsn :: (term() | {down, term()}), State :: term(),
|
|
||||||
Extra :: term()) ->
|
|
||||||
{ok, NewState :: term()} | {error, Reason :: term()}.
|
|
||||||
|
|
||||||
%%%===================================================================
|
|
||||||
%%% API
|
|
||||||
%%%===================================================================
|
|
||||||
|
|
||||||
-spec get_name(Name :: binary() | #endpoint{}) -> atom().
|
|
||||||
get_name(#endpoint{name = Name}) when is_binary(Name) ->
|
|
||||||
get_name(Name);
|
|
||||||
get_name(EndpointName) when is_binary(EndpointName) ->
|
|
||||||
binary_to_atom(<<"endpoint:", EndpointName/binary>>).
|
|
||||||
|
|
||||||
-spec get_pid(Name :: binary()) -> undefined | pid().
|
|
||||||
get_pid(Name) when is_binary(Name) ->
|
|
||||||
whereis(get_name(Name)).
|
|
||||||
|
|
||||||
-spec forward(Pid :: undefined | pid(), LocationCode :: binary(), Fields :: list(), Timestamp :: integer()) -> no_return().
|
|
||||||
forward(undefined, _, _, _) ->
|
|
||||||
ok;
|
|
||||||
forward(Pid, LocationCode, Fields, Timestamp) when is_pid(Pid), is_binary(LocationCode), is_list(Fields); is_binary(Fields), is_integer(Timestamp) ->
|
|
||||||
gen_statem:cast(Pid, {forward, LocationCode, Fields, Timestamp}).
|
|
||||||
|
|
||||||
reload(Pid, NEndpoint = #endpoint{}) when is_pid(Pid) ->
|
|
||||||
gen_statem:cast(Pid, {reload, NEndpoint}).
|
|
||||||
|
|
||||||
-spec get_stat(Pid :: pid()) -> {ok, Stat :: #{}}.
|
|
||||||
get_stat(Pid) when is_pid(Pid) ->
|
|
||||||
gen_statem:call(Pid, get_stat, 5000).
|
|
||||||
|
|
||||||
-spec clean_up(Pid :: pid()) -> ok.
|
|
||||||
clean_up(Pid) when is_pid(Pid) ->
|
|
||||||
gen_statem:call(Pid, clean_up, 5000).
|
|
||||||
|
|
||||||
-spec get_mapper_fun(Pid :: pid()) -> fun().
|
|
||||||
get_mapper_fun(Pid) when is_pid(Pid) ->
|
|
||||||
gen_statem:call(Pid, get_mapper_fun).
|
|
||||||
|
|
||||||
%% @doc Creates a gen_statem process which calls Module:init/1 to
|
|
||||||
%% initialize. To ensure a synchronized start-up procedure, this
|
|
||||||
%% function does not return until Module:init/1 has returned.
|
|
||||||
start_link(Name, Mod, Endpoint = #endpoint{}) ->
|
|
||||||
gen_statem:start_link({local, Name}, ?MODULE, [Mod, Endpoint], []).
|
|
||||||
|
|
||||||
%%%===================================================================
|
|
||||||
%%% gen_statem callbacks
|
|
||||||
%%%===================================================================
|
|
||||||
|
|
||||||
%% @private
|
|
||||||
%% @doc Whenever a gen_statem is started using gen_statem:start/[3,4] or
|
|
||||||
%% gen_statem:start_link/[3,4], this function is called by the new
|
|
||||||
%% process to initialize.
|
|
||||||
init([Mod, Endpoint = #endpoint{id = Id}]) ->
|
|
||||||
erlang:process_flag(trap_exit, true),
|
|
||||||
|
|
||||||
%% 创建转发器, 避免阻塞当前进程的创建,因此采用了延时初始化的机制
|
|
||||||
erlang:start_timer(0, self(), create_postman),
|
|
||||||
|
|
||||||
%% 初始化存储
|
|
||||||
EtsName = list_to_atom("endpoint_ets:" ++ integer_to_list(Id)),
|
|
||||||
Tid = ets:new(EtsName, [ordered_set, private]),
|
|
||||||
|
|
||||||
case Mod:init(Endpoint) of
|
|
||||||
{ok, StateName, MState} ->
|
|
||||||
{ok, StateName, #state{endpoint = Endpoint, mod = Mod, tid = Tid, mod_state = MState}};
|
|
||||||
{stop, Reason} ->
|
|
||||||
{stop, Reason}
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% @private
|
|
||||||
%% @doc This function is called by a gen_statem when it needs to find out
|
|
||||||
%% the callback mode of the callback module.
|
|
||||||
callback_mode() ->
|
|
||||||
handle_event_function.
|
|
||||||
|
|
||||||
%% @private
|
|
||||||
%% @doc There should be one instance of this function for each possible
|
|
||||||
%% state name. If callback_mode is state_functions, one of these
|
|
||||||
%% functions is called when gen_statem receives and event from
|
|
||||||
%% call/2, cast/2, or as a normal process message.
|
|
||||||
|
|
||||||
%% TODO 不支持, 重新加载新的终端配置, 对应的办法是关闭后重启
|
|
||||||
|
|
||||||
handle_event(cast, {forward, LocationCode, Fields, Timestamp}, StateName, State = #state{tid = Tid, last_id = LastId, window_size = WindowSize, flight_num = FlightNum, endpoint = #endpoint{name = Name}}) ->
|
|
||||||
lager:debug("[iot_endpoint] name: ~p, match location_code: ~p", [Name, LocationCode]),
|
|
||||||
NorthData = #north_data{id = LastId, location_code = LocationCode, fields = Fields, timestamp = Timestamp},
|
|
||||||
true = ets:insert(Tid, NorthData),
|
|
||||||
%% 避免不必要的内部消息
|
|
||||||
Actions = case StateName =:= connected andalso FlightNum < WindowSize of
|
|
||||||
true -> [{next_event, info, fetch_next}];
|
|
||||||
false -> []
|
|
||||||
end,
|
|
||||||
{keep_state, State#state{last_id = LastId + 1}, Actions};
|
|
||||||
|
|
||||||
%% 触发读取下一条数据
|
|
||||||
handle_event(info, fetch_next, connected, State = #state{flight_num = FlightNum, window_size = WindowSize}) when FlightNum >= WindowSize ->
|
|
||||||
{keep_state, State};
|
|
||||||
handle_event(info, fetch_next, connected, State = #state{tid = Tid, cursor = Cursor, endpoint = #endpoint{name = Name}, timer_map = TimerMap, flight_num = FlightNum}) ->
|
|
||||||
case ets:next(Tid, Cursor) of
|
|
||||||
'$end_of_table' ->
|
|
||||||
{keep_state, State};
|
|
||||||
NKey ->
|
|
||||||
[NorthData = #north_data{id = Id}] = ets:lookup(Tid, NKey),
|
|
||||||
lager:debug("[iot_endpoint] endpoint: ~p, fetch_next success, north data is: ~p", [Name, NorthData]),
|
|
||||||
case do_post(NorthData, State) of
|
|
||||||
error ->
|
|
||||||
{keep_state, State};
|
|
||||||
{ok, TimerRef} ->
|
|
||||||
{keep_state, State#state{timer_map = maps:put(Id, TimerRef, TimerMap), flight_num = FlightNum + 1}}
|
|
||||||
end
|
|
||||||
end;
|
|
||||||
|
|
||||||
%% 收到确认消息
|
|
||||||
handle_event(info, {ack, Id}, StateName, State = #state{tid = Tid, endpoint = #endpoint{name = Name}, timer_map = TimerMap, acc_num = AccNum, flight_num = FlightNum}) ->
|
|
||||||
true = ets:delete(Tid, Id),
|
|
||||||
|
|
||||||
lager:debug("[iot_endpoint] endpoint: ~p, get ack: ~p, delete from mnesia", [Name, Id]),
|
|
||||||
Actions = case StateName =:= connected of
|
|
||||||
true -> [{next_event, info, fetch_next}];
|
|
||||||
false -> []
|
|
||||||
end,
|
|
||||||
{keep_state, State#state{timer_map = remove_timer(Id, TimerMap), acc_num = AccNum + 1, flight_num = FlightNum - 1}, Actions};
|
|
||||||
|
|
||||||
%% 收到重发过期请求
|
|
||||||
handle_event(info, {timeout, _, {repost_ticker, NorthData = #north_data{id = Id}}}, connected,
|
|
||||||
State = #state{endpoint = #endpoint{name = Name}, timer_map = TimerMap}) ->
|
|
||||||
|
|
||||||
lager:debug("[iot_endpoint] endpoint: ~p, repost data: ~p", [Name, Id]),
|
|
||||||
case do_post(NorthData, State) of
|
|
||||||
error ->
|
|
||||||
{keep_state, State};
|
|
||||||
{ok, TimerRef} ->
|
|
||||||
{keep_state, State#state{timer_map = maps:put(Id, TimerRef, TimerMap)}}
|
|
||||||
end;
|
|
||||||
|
|
||||||
%% todo 离线时,忽略超时逻辑
|
|
||||||
handle_event(info, {timeout, _, {repost_ticker, _}}, disconnected, State) ->
|
|
||||||
{keep_state, State};
|
|
||||||
|
|
||||||
handle_event(info, {timeout, _, create_postman}, disconnected,
|
|
||||||
State = #state{endpoint = Endpoint = #endpoint{name = Name}, window_size = WindowSize, mod = Mod, mod_state = ModState}) ->
|
|
||||||
lager:debug("[iot_endpoint] endpoint: ~p, create postman", [Name]),
|
|
||||||
case Mod:create_postman(Endpoint, ModState) of
|
|
||||||
{ok, NModState} ->
|
|
||||||
%% 最多允许window_size
|
|
||||||
Actions = lists:map(fun(_) -> {next_event, info, fetch_next} end, lists:seq(1, WindowSize)),
|
|
||||||
{next_state, connected, State#state{endpoint = Endpoint, timer_map = maps:new(), flight_num = 0, mod_state = NModState}, Actions};
|
|
||||||
error ->
|
|
||||||
{keep_state, State}
|
|
||||||
end;
|
|
||||||
|
|
||||||
handle_event({call, From}, get_mapper_fun, _, State = #state{endpoint = #endpoint{mapper_fun = F}}) ->
|
|
||||||
{keep_state, State, [{reply, From, F}]};
|
|
||||||
|
|
||||||
%% 获取当前统计信息
|
|
||||||
handle_event({call, From}, get_stat, StateName, State = #state{acc_num = AccNum, tid = Tid}) ->
|
|
||||||
Stat = #{
|
|
||||||
<<"acc_num">> => AccNum,
|
|
||||||
<<"queue_num">> => ets:info(Tid, size),
|
|
||||||
<<"state_name">> => atom_to_binary(StateName)
|
|
||||||
},
|
|
||||||
{keep_state, State, [{reply, From, Stat}]};
|
|
||||||
|
|
||||||
%% postman进程挂掉时,重新建立新的
|
|
||||||
handle_event(info, Info, connected, State = #state{endpoint = #endpoint{name = Name}, timer_map = TimerMap, mod = Mod, mod_state = ModState}) ->
|
|
||||||
case Mod:handle_info(Info, ModState) of
|
|
||||||
{} ->
|
|
||||||
ok
|
|
||||||
end,
|
|
||||||
|
|
||||||
lists:foreach(fun({_, TimerRef}) -> catch erlang:cancel_timer(TimerRef) end, maps:to_list(TimerMap)),
|
|
||||||
erlang:start_timer(?RETRY_INTERVAL, self(), create_postman),
|
|
||||||
|
|
||||||
{next_state, disconnected, State#state{timer_map = maps:new()}};
|
|
||||||
|
|
||||||
%% @private
|
|
||||||
%% @doc If callback_mode is handle_event_function, then whenever a
|
|
||||||
%% gen_statem receives an event from call/2, cast/2, or as a normal
|
|
||||||
%% process message, this function is called.
|
|
||||||
handle_event(EventType, Event, StateName, State) ->
|
|
||||||
lager:warning("[iot_endpoint] unknown message, event_type: ~p, event: ~p, state_name: ~p, state: ~p", [EventType, Event, StateName, State]),
|
|
||||||
{keep_state, State}.
|
|
||||||
|
|
||||||
%% @private
|
|
||||||
%% @doc This function is called by a gen_statem when it is about to
|
|
||||||
%% terminate. It should be the opposite of Module:init/1 and do any
|
|
||||||
%% necessary cleaning up. When it returns, the gen_statem terminates with
|
|
||||||
%% Reason. The return value is ignored.
|
|
||||||
terminate(Reason, _StateName, #state{endpoint = #endpoint{name = Name}, timer_map = TimerMap}) ->
|
|
||||||
lager:debug("[iot_endpoint] endpoint: ~p, terminate with reason: ~p", [Name, Reason]),
|
|
||||||
lists:foreach(fun({_, TimerRef}) -> catch erlang:cancel_timer(TimerRef) end, maps:to_list(TimerMap)),
|
|
||||||
ok.
|
|
||||||
|
|
||||||
%% @private
|
|
||||||
%% @doc Convert process state when code is changed
|
|
||||||
code_change(_OldVsn, StateName, State = #state{}, _Extra) ->
|
|
||||||
{ok, StateName, State}.
|
|
||||||
|
|
||||||
%%%===================================================================
|
|
||||||
%%% Internal functions
|
|
||||||
%%%===================================================================
|
|
||||||
|
|
||||||
-spec remove_timer(Id :: integer(), TimerMap :: #{}) -> NTimerMap :: #{}.
|
|
||||||
remove_timer(Id, TimerMap) when is_integer(Id), is_map(TimerMap) ->
|
|
||||||
case maps:take(Id, TimerMap) of
|
|
||||||
error ->
|
|
||||||
TimerMap;
|
|
||||||
{TimerRef, NTimerMap} ->
|
|
||||||
is_reference(TimerRef) andalso erlang:cancel_timer(TimerRef),
|
|
||||||
NTimerMap
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% 对http和https协议的支持
|
|
||||||
create_postman(#endpoint{config = #http_endpoint{url = Url, pool_size = PoolSize}}) ->
|
|
||||||
WorkerArgs = [{url, Url}],
|
|
||||||
broker_postman:start_link(http_postman, WorkerArgs, PoolSize);
|
|
||||||
|
|
||||||
%% 对mqtt协议的支持, 只需要建立单个链接
|
|
||||||
create_postman(#endpoint{name = Name, config = #mqtt_endpoint{host = Host, port = Port, username = Username, password = Password, topic = Topic, qos = Qos}}) ->
|
|
||||||
Node = atom_to_binary(node()),
|
|
||||||
ClientId = <<"mqtt-client-", Node/binary, "-", Name/binary>>,
|
|
||||||
Opts = [
|
|
||||||
{clientid, ClientId},
|
|
||||||
{host, binary_to_list(Host)},
|
|
||||||
{port, Port},
|
|
||||||
{tcp_opts, []},
|
|
||||||
{username, binary_to_list(Username)},
|
|
||||||
{password, binary_to_list(Password)},
|
|
||||||
{keepalive, 86400},
|
|
||||||
{auto_ack, true},
|
|
||||||
{connect_timeout, 5000},
|
|
||||||
{proto_ver, v5},
|
|
||||||
{retry_interval, 5000}
|
|
||||||
],
|
|
||||||
|
|
||||||
mqtt_postman:start_link(Opts, Topic, Qos);
|
|
||||||
|
|
||||||
%% 对mysql协议的支持
|
|
||||||
create_postman(#endpoint{config = #mysql_endpoint{host = Host, port = Port, username = Username, password = Password, database = Database, table_name = TableName, pool_size = PoolSize}}) ->
|
|
||||||
WorkerArgs = [
|
|
||||||
{mysql_opts, [
|
|
||||||
{host, binary_to_list(Host)},
|
|
||||||
{port, Port},
|
|
||||||
{user, binary_to_list(Username)},
|
|
||||||
{password, binary_to_list(Password)},
|
|
||||||
{keep_alive, true},
|
|
||||||
{database, binary_to_list(Database)},
|
|
||||||
{queries, [<<"set names utf8">>]}
|
|
||||||
]},
|
|
||||||
{table, TableName}
|
|
||||||
],
|
|
||||||
broker_postman:start_link(mysql_postman, WorkerArgs, PoolSize);
|
|
||||||
create_postman(#endpoint{}) ->
|
|
||||||
throw(<<"not supported">>).
|
|
||||||
|
|
||||||
-spec do_post(NorthData :: #north_data{}, State :: #state{}) -> error | {ok, TimerRef :: reference()}.
|
|
||||||
do_post(NorthData = #north_data{id = Id, location_code = LocationCode}, #state{tid = Tid, mod = Mod, mod_state = ModState, endpoint = #endpoint{name = Name, mapper_fun = MapperFun}}) ->
|
|
||||||
lager:debug("[iot_endpoint] endpoint: ~p, fetch_next success, north data is: ~p", [Name, NorthData]),
|
|
||||||
case safe_invoke_mapper(MapperFun, NorthData) of
|
|
||||||
{ok, Body} ->
|
|
||||||
Mod:handle_data({Id, LocationCode, Body}, ModState),
|
|
||||||
|
|
||||||
%% 重发机制, 在发送的过程中mapper可能会改变
|
|
||||||
TimerRef = erlang:start_timer(?RETRY_INTERVAL, self(), {repost_ticker, NorthData}),
|
|
||||||
{ok, TimerRef};
|
|
||||||
{error, Error} ->
|
|
||||||
lager:debug("[iot_endpoint] forward endpoint: ~p, mapper get error: ~p, message discard", [Name, Error]),
|
|
||||||
ets:delete(Tid, Id),
|
|
||||||
error;
|
|
||||||
ignore ->
|
|
||||||
lager:debug("[iot_endpoint] forward endpoint: ~p, mapper ignore, messge discard", [Name]),
|
|
||||||
ets:delete(Tid, Id),
|
|
||||||
error
|
|
||||||
end.
|
|
||||||
|
|
||||||
-spec safe_invoke_mapper(MapperFun :: fun(), NorthData :: #north_data{}) ->
|
|
||||||
{ok, Body :: any()} | ignore | {error, Reason :: any()}.
|
|
||||||
safe_invoke_mapper(MapperFun, #north_data{location_code = LocationCode, fields = Fields, timestamp = Timestamp}) ->
|
|
||||||
try
|
|
||||||
if
|
|
||||||
is_function(MapperFun, 2) ->
|
|
||||||
MapperFun(LocationCode, Fields);
|
|
||||||
is_function(MapperFun, 3) ->
|
|
||||||
MapperFun(LocationCode, Fields, Timestamp)
|
|
||||||
end
|
|
||||||
catch _:Error ->
|
|
||||||
{error, Error}
|
|
||||||
end.
|
|
||||||
|
|
||||||
-spec config_equals(any(), any()) -> boolean().
|
|
||||||
config_equals(#http_endpoint{url = Url}, #http_endpoint{url = Url}) ->
|
|
||||||
true;
|
|
||||||
config_equals(#ws_endpoint{url = Url}, #ws_endpoint{url = Url}) ->
|
|
||||||
true;
|
|
||||||
config_equals(#kafka_endpoint{username = Username, password = Password, bootstrap_servers = BootstrapServers, topic = Topic},
|
|
||||||
#kafka_endpoint{username = Username, password = Password, bootstrap_servers = BootstrapServers, topic = Topic}) ->
|
|
||||||
true;
|
|
||||||
config_equals(#mqtt_endpoint{host = Host, port = Port, username = Username, password = Password, topic = Topic, qos = Qos},
|
|
||||||
#mqtt_endpoint{host = Host, port = Port, username = Username, password = Password, topic = Topic, qos = Qos}) ->
|
|
||||||
true;
|
|
||||||
config_equals(_, _) ->
|
|
||||||
false.
|
|
||||||
Loading…
x
Reference in New Issue
Block a user