fix endpoint

This commit is contained in:
anlicheng 2024-05-07 16:54:10 +08:00
parent 6acedc44cb
commit f31ad3fb05
6 changed files with 49 additions and 366 deletions

View File

@ -10,16 +10,18 @@
-record(http_endpoint, { -record(http_endpoint, {
url = <<>> :: binary(), url = <<>> :: binary(),
pool_size = 10 :: integer() %% post | put
}). method = post :: atom(),
%% json | web_form,
data_format = json :: atom(),
-record(ws_endpoint, { pool_size = 10 :: integer()
url = <<>> :: binary()
}). }).
-record(mqtt_endpoint, { -record(mqtt_endpoint, {
host = <<>> :: binary(), host = <<>> :: binary(),
port = 0 :: integer(), port = 0 :: integer(),
client_id = <<>> :: binary(),
username = <<>> :: binary(), username = <<>> :: binary(),
password = <<>> :: binary(), password = <<>> :: binary(),
topic = <<>> :: binary(), topic = <<>> :: binary(),
@ -45,8 +47,6 @@
-record(endpoint, { -record(endpoint, {
id :: integer(), id :: integer(),
%%
name = <<>> :: binary(),
%% %%
title = <<>> :: binary(), title = <<>> :: binary(),
mapper = <<>> :: binary(), mapper = <<>> :: binary(),
@ -55,17 +55,9 @@
%% fun(LocationCode :: binary(), Fields :: [{<<"key">> => <<>>, <<"value">> => <<>>, <<"unit">> => <<>>}], Timestamp :: integer()) %% fun(LocationCode :: binary(), Fields :: [{<<"key">> => <<>>, <<"value">> => <<>>, <<"unit">> => <<>>}], Timestamp :: integer())
mapper_fun = fun(_, Fields) -> Fields end :: fun(), mapper_fun = fun(_, Fields) -> Fields end :: fun(),
%% , : #{<<"protocol">> => <<"http|https|ws|kafka|mqtt">>, <<"args">> => #{}} %% , : #{<<"protocol">> => <<"http|https|ws|kafka|mqtt">>, <<"args">> => #{}}
config = #http_endpoint{} :: #http_endpoint{} | #ws_endpoint{} | #mqtt_endpoint{} | #kafka_endpoint{} | #mysql_endpoint{}, config = #http_endpoint{} :: #http_endpoint{} | #mqtt_endpoint{} | #kafka_endpoint{} | #mysql_endpoint{},
%% %%
updated_at = 0 :: integer(), updated_at = 0 :: integer(),
%% %%
created_at = 0 :: integer() created_at = 0 :: integer()
}). }).
-record(post_data, {
id,
location_code,
body
}).

View File

@ -10,349 +10,49 @@
-module(endpoint). -module(endpoint).
-include("endpoint.hrl"). -include("endpoint.hrl").
-behaviour(gen_statem).
%% API %% API
-export([start_link/2]). -export([start_link/1]).
-export([get_name/1, get_pid/1, forward/4, get_stat/1, reload/2, clean_up/1, get_mapper_fun/1]). -export([get_name/1, get_pid/1, forward/4, reload/2, clean_up/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,
mp,
postman_pid :: undefined | pid(),
%%
queue,
%%
timer_map = #{},
%%
window_size = 10,
%%
flight_num = 0,
%%
acc_num = 0
}).
%%%=================================================================== %%%===================================================================
%%% API %%% API
%%%=================================================================== %%%===================================================================
-spec get_name(Name :: binary() | #endpoint{}) -> atom(). -spec start_link(Endpoint :: #endpoint{}) -> {'ok', pid()} | 'ignore' | {'error', term()}.
get_name(#endpoint{name = Name}) when is_binary(Name) -> start_link(Endpoint = #endpoint{id = Id, config = #http_endpoint{}}) ->
get_name(Name); Name = get_name(Id),
get_name(EndpointName) when is_binary(EndpointName) -> endpoint_http:start_link(Name, Endpoint);
binary_to_atom(<<"iot_endpoint:", EndpointName/binary>>). start_link(Endpoint = #endpoint{id = Id, config = #mqtt_endpoint{}}) ->
Name = get_name(Id),
endpoint_mqtt:start_link(Name, Endpoint);
start_link(Endpoint = #endpoint{id = Id, config = #mysql_endpoint{}}) ->
Name = get_name(Id),
endpoint_mysql:start_link(Name, Endpoint).
-spec get_pid(Name :: binary()) -> undefined | pid(). -spec get_name(Name :: binary() | #endpoint{}) -> atom().
get_pid(Name) when is_binary(Name) -> get_name(Id) when is_integer(Id) ->
whereis(get_name(Name)). list_to_atom("endpoint:" ++ integer_to_list(Id)).
-spec get_pid(Id :: integer()) -> undefined | pid().
get_pid(Id) when is_integer(Id) ->
whereis(get_name(Id)).
-spec forward(Pid :: undefined | pid(), LocationCode :: binary(), Fields :: list(), Timestamp :: integer()) -> no_return(). -spec forward(Pid :: undefined | pid(), LocationCode :: binary(), Fields :: list(), Timestamp :: integer()) -> no_return().
forward(undefined, _, _, _) -> forward(undefined, _, _, _) ->
ok; ok;
forward(Pid, LocationCode, Fields, Timestamp) when is_pid(Pid), is_binary(LocationCode), is_list(Fields); is_binary(Fields), is_integer(Timestamp) -> 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}). Pid ! {forward, LocationCode, Fields, Timestamp}.
reload(Pid, NEndpoint = #endpoint{}) when is_pid(Pid) -> reload(Pid, NEndpoint = #endpoint{}) when is_pid(Pid) ->
gen_statem:cast(Pid, {reload, NEndpoint}). 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. -spec clean_up(Pid :: pid()) -> ok.
clean_up(Pid) when is_pid(Pid) -> clean_up(Pid) when is_pid(Pid) ->
gen_statem:call(Pid, clean_up, 5000). 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, Endpoint = #endpoint{}) ->
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 = #endpoint{matcher = Regexp}]) ->
erlang:process_flag(trap_exit, true),
%%
{ok, MP} = re:compile(Regexp),
%% ,
erlang:start_timer(0, self(), create_postman),
{ok, disconnected, #state{endpoint = Endpoint, mp = MP, queue = queue:new(), postman_pid = undefined}}.
%% @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, {reload, NEndpoint}, disconnected, State = #state{endpoint = Endpoint}) ->
lager:warning("[iot_endpoint] state_name: disconnected, reload endpoint, old: ~p, new: ~p", [Endpoint, NEndpoint]),
{keep_state, State#state{endpoint = NEndpoint}};
handle_event(cast, {reload, NEndpoint = #endpoint{name = Name}}, connected, State = #state{endpoint = Endpoint, timer_map = TimerMap, postman_pid = PostmanPid}) ->
lager:debug("[iot_endpoint] state_name: connected, reload endpoint, old: ~p, new: ~p", [Endpoint, NEndpoint]),
case config_equals(NEndpoint#endpoint.config, Endpoint#endpoint.config) of
true ->
lager:debug("[iot_endpoint] reload endpoint: ~p, config equals", [Name]),
{keep_state, State#state{endpoint = NEndpoint}};
false ->
%% postman的link关系
unlink(PostmanPid),
%% postman进程
catch PostmanPid ! stop,
%%
lists:foreach(fun({_, TimerRef}) -> catch erlang:cancel_timer(TimerRef) end, maps:to_list(TimerMap)),
%% postman
erlang:start_timer(0, self(), create_postman),
{next_state, disconnected, State#state{endpoint = NEndpoint, timer_map = maps:new(), postman_pid = undefined}}
end;
handle_event(cast, {forward, LocationCode, Fields, Timestamp}, StateName, State = #state{mp = MP, queue = Q, window_size = WindowSize, flight_num = FlightNum, endpoint = #endpoint{name = Name}}) ->
case re:run(LocationCode, MP, [{capture, all, list}]) of
nomatch ->
{keep_state, State};
{match, _} ->
lager:debug("[iot_endpoint] name: ~p, match location_code: ~p", [Name, LocationCode]),
Q1 = queue:in(#north_data{location_code = LocationCode, fields = Fields, timestamp = Timestamp}, Q),
%%
Actions = case StateName =:= connected andalso FlightNum < WindowSize of
true -> [{next_event, info, fetch_next}];
false -> []
end,
{keep_state, State#state{queue = Q1}, Actions}
end;
%%
handle_event(info, fetch_next, disconnected, State = #state{endpoint = #endpoint{name = Name}}) ->
lager:debug("[iot_endpoint] fetch_next endpoint: ~p, postman offline, data in queue", [Name]),
{keep_state, State};
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{queue = Q, endpoint = #endpoint{name = Name}, timer_map = TimerMap, flight_num = FlightNum}) ->
case queue:out(Q) of
{{value, NorthData = #north_data{id = Id}}, Q1} ->
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;
{empty, Q1} ->
{keep_state, State#state{queue = Q1}}
end;
%%
handle_event(info, {ack, Id}, StateName, State = #state{tab_name = TabName, endpoint = #endpoint{name = Name}, timer_map = TimerMap, acc_num = AccNum, flight_num = FlightNum}) ->
ok = mnesia_queue:delete(TabName, 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;
%% 线
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, config = Config}, window_size = WindowSize}) ->
lager:debug("[iot_endpoint] endpoint: ~p, create postman", [Name]),
try
{ok, PostmanPid} = create_postman(Endpoint),
%% window_size
Actions = lists:map(fun(_) -> {next_event, info, fetch_next} end, lists:seq(1, WindowSize)),
{next_state, connected, State#state{endpoint = Endpoint, postman_pid = PostmanPid, timer_map = maps:new(), flight_num = 0}, Actions}
catch _:Error ->
lager:warning("[iot_endpoint] endpoint: ~p, config: ~p, create postman get error: ~p", [Name, Config, Error]),
erlang:start_timer(?RETRY_INTERVAL, self(), create_postman),
{keep_state, State#state{endpoint = Endpoint, postman_pid = undefined}}
end;
%%
handle_event({call, From}, clean_up, _, State = #state{tab_name = TabName}) ->
mnesia:delete_table(TabName),
{keep_state, State, [{reply, From, ok}]};
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, tab_name = TabName}) ->
Stat = #{
<<"acc_num">> => AccNum,
<<"queue_num">> => mnesia_queue:table_size(TabName),
<<"state_name">> => atom_to_binary(StateName)
},
{keep_state, State, [{reply, From, Stat}]};
%% postman进程挂掉时
handle_event(info, {'EXIT', PostmanPid, Reason}, connected, State = #state{endpoint = #endpoint{name = Name}, timer_map = TimerMap, postman_pid = PostmanPid}) ->
lager:warning("[iot_endpoint] endpoint: ~p, postman exited with reason: ~p", [Name, Reason]),
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(), postman_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}}) ->
lager:debug("[iot_endpoint] endpoint: ~p, terminate with reason: ~p", [Name, Reason]),
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}, #state{postman_pid = PostmanPid, tab_name = TabName, 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} ->
PostmanPid ! {post, self(), make_post_data(NorthData, Body)},
%% , 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]),
mnesia_queue:delete(TabName, Id),
error;
ignore ->
lager:debug("[iot_endpoint] forward endpoint: ~p, mapper ignore, messge discard", [Name]),
mnesia_queue:delete(TabName, 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 make_post_data(NorthData :: #north_data{}, Body :: any()) -> PostData :: #post_data{}.
make_post_data(#north_data{id = Id, location_code = LocationCode}, Body) ->
#post_data{id = Id, location_code = LocationCode, body = Body}.
-spec config_equals(any(), any()) -> boolean(). -spec config_equals(any(), any()) -> boolean().
config_equals(#http_endpoint{url = Url}, #http_endpoint{url = Url}) -> config_equals(#http_endpoint{url = Url}, #http_endpoint{url = Url}) ->
true; 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}, config_equals(#kafka_endpoint{username = Username, password = Password, bootstrap_servers = BootstrapServers, topic = Topic},
#kafka_endpoint{username = Username, password = Password, bootstrap_servers = BootstrapServers, topic = Topic}) -> #kafka_endpoint{username = Username, password = Password, bootstrap_servers = BootstrapServers, topic = Topic}) ->
true; true;

View File

@ -72,7 +72,7 @@ trigger_n(Buffer = #buffer{window_size = WindowSize}) ->
%% %%
-spec trigger_next(Buffer :: #buffer{}) -> NBuffer :: #buffer{}. -spec trigger_next(Buffer :: #buffer{}) -> NBuffer :: #buffer{}.
trigger_next(Buffer = #buffer{tid = Tid, cursor = Cursor, timer_pid = TimerPid, flight_num = FlightNum, endpoint = #endpoint{name = Name, mapper_fun = MapperFun}}) -> trigger_next(Buffer = #buffer{tid = Tid, cursor = Cursor, timer_pid = TimerPid, flight_num = FlightNum, endpoint = #endpoint{title = Title, mapper_fun = MapperFun}}) ->
case ets:next(Tid, Cursor) of case ets:next(Tid, Cursor) of
'$end_of_table' -> '$end_of_table' ->
Buffer; Buffer;
@ -88,11 +88,11 @@ trigger_next(Buffer = #buffer{tid = Tid, cursor = Cursor, timer_pid = TimerPid,
Buffer#buffer{flight_num = FlightNum + 1}; Buffer#buffer{flight_num = FlightNum + 1};
{error, Error} -> {error, Error} ->
lager:debug("[iot_endpoint] forward endpoint: ~p, mapper get error: ~p, message discard", [Name, Error]), lager:debug("[iot_endpoint] forward endpoint: ~p, mapper get error: ~p, message discard", [Title, Error]),
ets:delete(Tid, Id), ets:delete(Tid, Id),
Buffer; Buffer;
ignore -> ignore ->
lager:debug("[iot_endpoint] forward endpoint: ~p, mapper ignore, messge discard", [Name]), lager:debug("[iot_endpoint] forward endpoint: ~p, mapper ignore, messge discard", [Title]),
ets:delete(Tid, Id), ets:delete(Tid, Id),
Buffer Buffer
end end

View File

@ -53,7 +53,7 @@ cleanup(Pid) when is_pid(Pid) ->
gen_server:cast(Pid, cleanup). gen_server:cast(Pid, cleanup).
%% @doc Spawns the server and registers the local name (unique) %% @doc Spawns the server and registers the local name (unique)
-spec(start_link(Name, Endpoint :: #http_endpoint{}) -> -spec(start_link(Name :: atom(), Endpoint :: #http_endpoint{}) ->
{ok, Pid :: pid()} | ignore | {error, Reason :: term()}). {ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
start_link(Name, Endpoint = #endpoint{config = #http_endpoint{}}) when is_atom(Name) -> start_link(Name, Endpoint = #endpoint{config = #http_endpoint{}}) when is_atom(Name) ->
gen_server:start_link({local, Name}, ?MODULE, [Endpoint], []). gen_server:start_link({local, Name}, ?MODULE, [Endpoint], []).
@ -105,7 +105,7 @@ handle_cast(cleanup, State = #state{buffer = Buffer}) ->
{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({next_data, Id, _LocationCode, Body}, State = #state{buffer = Buffer, http_endpoint = #http_endpoint{url = Url}}) -> handle_info({next_data, Id, _LocationCode, Body}, State = #state{buffer = Buffer, endpoint = #http_endpoint{url = Url}}) ->
Headers = [ Headers = [
{<<"content-type">>, <<"application/json">>} {<<"content-type">>, <<"application/json">>}
], ],

View File

@ -98,21 +98,13 @@ callback_mode() ->
%% functions is called when gen_statem receives and event from %% functions is called when gen_statem receives and event from
%% call/2, cast/2, or as a normal process message. %% 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}}) -> handle_event(cast, {forward, LocationCode, Fields, Timestamp}, _StateName, State = #state{buffer = Buffer}) ->
lager:debug("[iot_endpoint] name: ~p, match location_code: ~p", [Name, LocationCode]),
NBuffer = endpoint_buffer:append({LocationCode, Fields, Timestamp}, Buffer), NBuffer = endpoint_buffer:append({LocationCode, Fields, Timestamp}, Buffer),
{keep_state, State#state{buffer = NBuffer}}; {keep_state, State#state{buffer = NBuffer}};
handle_event(info, {timeout, _, create_postman}, disconnected, State = #state{buffer = Buffer,
endpoint = #endpoint{title = Title, config = #mqtt_endpoint{host = Host, port = Port, username = Username, password = Password, client_id = ClientId}}}) ->
handle_event(info, {timeout, _, create_postman}, disconnected, lager:debug("[iot_endpoint] endpoint: ~p, create postman", [Title]),
State = #state{buffer = Buffer, endpoint = #endpoint{name = Name, config = #mqtt_endpoint{host = Host, port = Port, username = Username, password = Password, topic = Topic, qos = Qos}}}) ->
lager:debug("[iot_endpoint] endpoint: ~p, create postman", [Name]),
Node = atom_to_binary(node()),
ClientId = <<"mqtt-client-", Node/binary, "-", Name/binary>>,
Opts = [ Opts = [
{owner, self()}, {owner, self()},
{clientid, ClientId}, {clientid, ClientId},
@ -190,8 +182,8 @@ handle_event(info, {puback, #{packet_id := PacketId}}, connected, State = #state
end; end;
%% postman进程挂掉时 %% postman进程挂掉时
handle_event(info, {'EXIT', ConnPid, Reason}, connected, State = #state{endpoint = #endpoint{name = Name}, conn_pid = ConnPid}) -> handle_event(info, {'EXIT', ConnPid, Reason}, connected, State = #state{endpoint = #endpoint{title = Title}, conn_pid = ConnPid}) ->
lager:warning("[enpoint_mqtt] endpoint: ~p, conn pid exit with reason: ~p", [Name, Reason]), lager:warning("[enpoint_mqtt] endpoint: ~p, conn pid exit with reason: ~p", [Title, Reason]),
erlang:start_timer(?RETRY_INTERVAL, self(), create_postman), erlang:start_timer(?RETRY_INTERVAL, self(), create_postman),
{next_state, disconnected, State#state{conn_pid = undefined}}; {next_state, disconnected, State#state{conn_pid = undefined}};
@ -208,8 +200,8 @@ handle_event(EventType, Event, StateName, State) ->
%% terminate. It should be the opposite of Module:init/1 and do any %% terminate. It should be the opposite of Module:init/1 and do any
%% necessary cleaning up. When it returns, the gen_statem terminates with %% necessary cleaning up. When it returns, the gen_statem terminates with
%% Reason. The return value is ignored. %% Reason. The return value is ignored.
terminate(Reason, _StateName, #state{endpoint = #endpoint{name = Name}, buffer = Buffer}) -> terminate(Reason, _StateName, #state{endpoint = #endpoint{title = Title}, buffer = Buffer}) ->
lager:debug("[iot_endpoint] endpoint: ~p, terminate with reason: ~p", [Name, Reason]), lager:debug("[iot_endpoint] endpoint: ~p, terminate with reason: ~p", [Title, Reason]),
endpoint_buffer:cleanup(Buffer), endpoint_buffer:cleanup(Buffer),
ok. ok.

View File

@ -92,8 +92,7 @@ callback_mode() ->
%% functions is called when gen_statem receives and event from %% functions is called when gen_statem receives and event from
%% call/2, cast/2, or as a normal process message. %% 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}}) -> handle_event(cast, {forward, LocationCode, Fields, Timestamp}, _StateName, State = #state{buffer = Buffer}) ->
lager:debug("[iot_endpoint] name: ~p, match location_code: ~p", [Name, LocationCode]),
NBuffer = endpoint_buffer:append({LocationCode, Fields, Timestamp}, Buffer), NBuffer = endpoint_buffer:append({LocationCode, Fields, Timestamp}, Buffer),
{keep_state, State#state{buffer = NBuffer}}; {keep_state, State#state{buffer = NBuffer}};
@ -102,8 +101,8 @@ handle_event(cast, cleanup, _StateName, State = #state{buffer = Buffer}) ->
{keep_state, State}; {keep_state, State};
handle_event(info, {timeout, _, create_postman}, disconnected, 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}}}) -> State = #state{buffer = Buffer, endpoint = #endpoint{title = Title, 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]), lager:debug("[iot_endpoint] endpoint: ~p, create postman", [Title]),
WorkerArgs = [ WorkerArgs = [
{host, binary_to_list(Host)}, {host, binary_to_list(Host)},
{port, Port}, {port, Port},
@ -139,7 +138,7 @@ handle_event(info, {next_data, _Id, _LocationCode, _Message}, disconnected, Stat
{keep_state, State}; {keep_state, State};
%% mqtt服务器 %% mqtt服务器
handle_event(info, {next_data, Id, _LocationCode, Fields}, connected, 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}}}) -> State = #state{pool_pid = PoolPid, buffer = Buffer, endpoint = #endpoint{title = Title, config = #mysql_endpoint{table_name = Table}}}) ->
{ok, InsertSql, Values} = insert_sql(Table, Fields), {ok, InsertSql, Values} = insert_sql(Table, Fields),
case poolboy:transaction(PoolPid, fun(ConnPid) -> mysql:query(ConnPid, InsertSql, Values) end) of case poolboy:transaction(PoolPid, fun(ConnPid) -> mysql:query(ConnPid, InsertSql, Values) end) of
@ -147,13 +146,13 @@ handle_event(info, {next_data, Id, _LocationCode, Fields}, connected,
NBuffer = endpoint_buffer:ack(Id, Buffer), NBuffer = endpoint_buffer:ack(Id, Buffer),
{keep_state, State#state{buffer = NBuffer}}; {keep_state, State#state{buffer = NBuffer}};
Error -> Error ->
lager:warning("[endpoint_mysql] endpoint: ~p, insert mysql get error: ~p", [Name, Error]), lager:warning("[endpoint_mysql] endpoint: ~p, insert mysql get error: ~p", [Title, Error]),
{keep_state, State} {keep_state, State}
end; end;
%% postman进程挂掉时 %% postman进程挂掉时
handle_event(info, {'EXIT', PoolPid, Reason}, connected, State = #state{endpoint = #endpoint{name = Name}, pool_pid = PoolPid}) -> handle_event(info, {'EXIT', PoolPid, Reason}, connected, State = #state{endpoint = #endpoint{title = Title}, pool_pid = PoolPid}) ->
lager:warning("[enpoint_mqtt] endpoint: ~p, conn pid exit with reason: ~p", [Name, Reason]), lager:warning("[enpoint_mqtt] endpoint: ~p, conn pid exit with reason: ~p", [Title, Reason]),
erlang:start_timer(?RETRY_INTERVAL, self(), create_postman), erlang:start_timer(?RETRY_INTERVAL, self(), create_postman),
{next_state, disconnected, State#state{pool_pid = undefined}}; {next_state, disconnected, State#state{pool_pid = undefined}};
@ -170,8 +169,8 @@ handle_event(EventType, Event, StateName, State) ->
%% terminate. It should be the opposite of Module:init/1 and do any %% terminate. It should be the opposite of Module:init/1 and do any
%% necessary cleaning up. When it returns, the gen_statem terminates with %% necessary cleaning up. When it returns, the gen_statem terminates with
%% Reason. The return value is ignored. %% Reason. The return value is ignored.
terminate(Reason, _StateName, #state{endpoint = #endpoint{name = Name}, buffer = Buffer}) -> terminate(Reason, _StateName, #state{endpoint = #endpoint{title = Title}, buffer = Buffer}) ->
lager:debug("[iot_endpoint] endpoint: ~p, terminate with reason: ~p", [Name, Reason]), lager:debug("[iot_endpoint] endpoint: ~p, terminate with reason: ~p", [Title, Reason]),
endpoint_buffer:cleanup(Buffer), endpoint_buffer:cleanup(Buffer),
ok. ok.