fix mqtt
This commit is contained in:
parent
569e25b23f
commit
1f4c17f118
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
%%%-------------------------------------------------------------------
|
%%%-------------------------------------------------------------------
|
||||||
%%% @author aresei
|
%%% @author aresei
|
||||||
%%% @copyright (C) 2023, <COMPANY>
|
%%% @copyright (C) 2023, <COMPANY>
|
||||||
@ -10,30 +9,27 @@
|
|||||||
-module(endpoint_mqtt).
|
-module(endpoint_mqtt).
|
||||||
|
|
||||||
-include("endpoint.hrl").
|
-include("endpoint.hrl").
|
||||||
-behaviour(gen_server).
|
-behaviour(gen_statem).
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([start_link/2]).
|
-export([start_link/2]).
|
||||||
|
|
||||||
%% gen_server callbacks
|
%% gen_statem callbacks
|
||||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
|
-export([callback_mode/0, init/1, handle_event/4, terminate/3, code_change/4]).
|
||||||
|
|
||||||
%% 消息重发间隔
|
%% 消息重发间隔
|
||||||
-define(RETRY_INTERVAL, 15000).
|
-define(RETRY_INTERVAL, 15000).
|
||||||
|
|
||||||
-define(DISCONNECTED, disconnected).
|
|
||||||
-define(CONNECTED, connected).
|
|
||||||
|
|
||||||
-record(state, {
|
-record(state, {
|
||||||
endpoint :: #endpoint{},
|
endpoint :: #endpoint{},
|
||||||
buffer :: endpoint_buffer:buffer(),
|
buffer :: endpoint_buffer:buffer(),
|
||||||
conn_pid :: undefined | pid(),
|
conn_pid :: undefined | pid(),
|
||||||
%% 待确认的数据, #{PacketId :: integer() => Id :: integer()}
|
%% 待确认的数据, #{PacketId :: integer() => Id :: integer()}
|
||||||
inflight = #{},
|
inflight = #{}
|
||||||
|
|
||||||
status = disconnected
|
|
||||||
}).
|
}).
|
||||||
|
|
||||||
|
-type mqtt_state() :: disconnected | connecting | connected.
|
||||||
|
|
||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
%%% API
|
%%% API
|
||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
@ -41,69 +37,67 @@
|
|||||||
%% @doc Creates a gen_statem process which calls Module:init/1 to
|
%% @doc Creates a gen_statem process which calls Module:init/1 to
|
||||||
%% initialize. To ensure a synchronized start-up procedure, this
|
%% initialize. To ensure a synchronized start-up procedure, this
|
||||||
%% function does not return until Module:init/1 has returned.
|
%% function does not return until Module:init/1 has returned.
|
||||||
|
-spec start_link(LocalName :: atom(), Endpoint :: #endpoint{}) ->
|
||||||
|
{ok, pid()} | ignore | {error, term()}.
|
||||||
start_link(LocalName, Endpoint = #endpoint{}) when is_atom(LocalName) ->
|
start_link(LocalName, Endpoint = #endpoint{}) when is_atom(LocalName) ->
|
||||||
gen_server:start_link({local, LocalName}, ?MODULE, [Endpoint], []).
|
gen_statem:start_link({local, LocalName}, ?MODULE, [Endpoint], []).
|
||||||
|
|
||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
%%% gen_statem callbacks
|
%%% gen_statem callbacks
|
||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
|
|
||||||
|
-spec callback_mode() -> handle_event_function.
|
||||||
|
callback_mode() ->
|
||||||
|
handle_event_function.
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
%% @doc Whenever a gen_statem is started using gen_statem:start/[3,4] or
|
%% @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
|
%% gen_statem:start_link/[3,4], this function is called by the new
|
||||||
%% process to initialize.
|
%% process to initialize.
|
||||||
|
-spec init(term()) -> gen_statem:init_result(mqtt_state(), #state{}).
|
||||||
init([Endpoint = #endpoint{matcher = Matcher}]) ->
|
init([Endpoint = #endpoint{matcher = Matcher}]) ->
|
||||||
ok = iot_log:set_metadata(),
|
ok = iot_log:set_metadata(),
|
||||||
erlang:process_flag(trap_exit, true),
|
erlang:process_flag(trap_exit, true),
|
||||||
endpoint_subscription:subscribe(Matcher, self()),
|
ok = endpoint_subscription:subscribe(Matcher, self()),
|
||||||
|
|
||||||
%% 创建转发器, 避免阻塞当前进程的创建,因此采用了延时初始化的机制
|
|
||||||
erlang:start_timer(0, self(), create_postman),
|
|
||||||
%% 初始化存储
|
|
||||||
Buffer = endpoint_buffer:new(Endpoint, 10),
|
Buffer = endpoint_buffer:new(Endpoint, 10),
|
||||||
|
{ok, disconnected, #state{endpoint = Endpoint, buffer = Buffer}, [{next_event, internal, connect}]}.
|
||||||
|
|
||||||
{ok, #state{endpoint = Endpoint, buffer = Buffer, status = ?DISCONNECTED}}.
|
-spec handle_event(gen_statem:event_type(), term(), mqtt_state(), #state{}) ->
|
||||||
|
gen_statem:event_handler_result(mqtt_state(), #state{}).
|
||||||
%% @private
|
handle_event({call, From}, get_stat, _StateName, State = #state{buffer = Buffer}) ->
|
||||||
%% @doc Handling call messages
|
|
||||||
-spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()},
|
|
||||||
State :: #state{}) ->
|
|
||||||
{reply, Reply :: term(), NewState :: #state{}} |
|
|
||||||
{reply, Reply :: term(), NewState :: #state{}, timeout() | hibernate} |
|
|
||||||
{noreply, NewState :: #state{}} |
|
|
||||||
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
|
||||||
{stop, Reason :: term(), Reply :: term(), NewState :: #state{}} |
|
|
||||||
{stop, Reason :: term(), NewState :: #state{}}).
|
|
||||||
handle_call(get_stat, _From, State = #state{buffer = Buffer}) ->
|
|
||||||
Stat = endpoint_buffer:stat(Buffer),
|
Stat = endpoint_buffer:stat(Buffer),
|
||||||
{reply, {ok, Stat}, State}.
|
{keep_state, State, [{reply, From, {ok, Stat}}]};
|
||||||
|
handle_event(cast, {forward, Metric}, _StateName, State = #state{buffer = Buffer}) ->
|
||||||
%% @private
|
|
||||||
%% @doc Handling cast messages
|
|
||||||
-spec(handle_cast(Request :: term(), State :: #state{}) ->
|
|
||||||
{noreply, NewState :: #state{}} |
|
|
||||||
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
|
||||||
{stop, Reason :: term(), NewState :: #state{}}).
|
|
||||||
handle_cast({forward, Metric}, State = #state{buffer = Buffer}) ->
|
|
||||||
NBuffer = endpoint_buffer:append(Metric, Buffer),
|
NBuffer = endpoint_buffer:append(Metric, Buffer),
|
||||||
{noreply, State#state{buffer = NBuffer}};
|
{keep_state, State#state{buffer = NBuffer}};
|
||||||
handle_cast({reload, NEndpoint = #endpoint{matcher = NMatcher}}, State = #state{endpoint = #endpoint{matcher = Matcher}, conn_pid = ConnPid}) ->
|
handle_event(cast, cleanup, _StateName, #state{buffer = Buffer}) ->
|
||||||
|
endpoint_buffer:cleanup(Buffer),
|
||||||
|
keep_state_and_data;
|
||||||
|
handle_event(cast, {reload, NEndpoint = #endpoint{matcher = NMatcher}}, StateName,
|
||||||
|
State = #state{endpoint = #endpoint{matcher = Matcher}, conn_pid = ConnPid}) ->
|
||||||
ensure_subscription(Matcher, NMatcher),
|
ensure_subscription(Matcher, NMatcher),
|
||||||
stop_mqtt_conn(ConnPid),
|
stop_mqtt_conn(ConnPid),
|
||||||
schedule_reconnect(),
|
NState = State#state{endpoint = NEndpoint, conn_pid = undefined, inflight = #{}},
|
||||||
{noreply, State#state{endpoint = NEndpoint, conn_pid = undefined, inflight = #{}, status = ?DISCONNECTED}};
|
{next_state, connecting, NState, maybe_connect_actions(StateName)};
|
||||||
handle_cast(cleanup, State = #state{buffer = Buffer}) ->
|
|
||||||
endpoint_buffer:cleanup(Buffer),
|
|
||||||
{noreply, State}.
|
|
||||||
|
|
||||||
%% @private
|
handle_event(internal, connect, disconnected, State) ->
|
||||||
%% @doc Handling all non call/cast messages
|
{next_state, connecting, State, [{next_event, internal, do_connect}]};
|
||||||
-spec(handle_info(Info :: timeout() | term(), State :: #state{}) ->
|
handle_event(internal, connect, connecting, State) ->
|
||||||
{noreply, NewState :: #state{}} |
|
{keep_state, State, [{next_event, internal, do_connect}]};
|
||||||
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
handle_event(internal, connect, connected, State) ->
|
||||||
{stop, Reason :: term(), NewState :: #state{}}).
|
{keep_state, State};
|
||||||
handle_info({timeout, _, create_postman}, State = #state{buffer = Buffer, status = ?DISCONNECTED,
|
handle_event(internal, do_connect, connecting,
|
||||||
endpoint = #endpoint{title = Title, config = #mqtt_endpoint{host = Host, port = Port, username = Username, password = Password, client_id = ClientId}}}) ->
|
State = #state{buffer = Buffer,
|
||||||
|
endpoint = #endpoint{
|
||||||
|
title = Title,
|
||||||
|
config = #mqtt_endpoint{
|
||||||
|
host = Host,
|
||||||
|
port = Port,
|
||||||
|
username = Username,
|
||||||
|
password = Password,
|
||||||
|
client_id = ClientId
|
||||||
|
}
|
||||||
|
}}) ->
|
||||||
logger:debug("[endpoint_mqtt] endpoint: ~ts, create postman", [Title]),
|
logger:debug("[endpoint_mqtt] endpoint: ~ts, create postman", [Title]),
|
||||||
Opts = [
|
Opts = [
|
||||||
{owner, self()},
|
{owner, self()},
|
||||||
@ -119,84 +113,88 @@ handle_info({timeout, _, create_postman}, State = #state{buffer = Buffer, status
|
|||||||
{proto_ver, v5},
|
{proto_ver, v5},
|
||||||
{retry_interval, 5000}
|
{retry_interval, 5000}
|
||||||
],
|
],
|
||||||
|
case connect_mqtt(Opts) of
|
||||||
try
|
{ok, ConnPid} ->
|
||||||
{ok, ConnPid} = emqtt:start_link(Opts),
|
logger:debug("[endpoint_mqtt] connect success, pid: ~p", [ConnPid]),
|
||||||
logger:debug("[endpoint_mqtt] start connect, options: ~p", [Opts]),
|
NBuffer = endpoint_buffer:trigger_n(Buffer),
|
||||||
case emqtt:connect(ConnPid, 5000) of
|
{next_state, connected, State#state{conn_pid = ConnPid, buffer = NBuffer}};
|
||||||
{ok, _} ->
|
{error, Reason} ->
|
||||||
logger:debug("[endpoint_mqtt] connect success, pid: ~p", [ConnPid]),
|
logger:warning("[endpoint_mqtt] connect get error: ~p", [Reason]),
|
||||||
NBuffer = endpoint_buffer:trigger_n(Buffer),
|
{next_state, disconnected, State#state{conn_pid = undefined, inflight = #{}},
|
||||||
{noreply, State#state{conn_pid = ConnPid, buffer = NBuffer, status = ?CONNECTED}};
|
[{state_timeout, ?RETRY_INTERVAL, connect}]}
|
||||||
{error, Reason} ->
|
|
||||||
logger:warning("[endpoint_mqtt] connect get error: ~p", [Reason]),
|
|
||||||
erlang:start_timer(?RETRY_INTERVAL, self(), create_postman),
|
|
||||||
{noreply, State}
|
|
||||||
end
|
|
||||||
catch _:Error->
|
|
||||||
logger:warning("[endpoint_mqtt] connect get error: ~p", [Error]),
|
|
||||||
erlang:start_timer(?RETRY_INTERVAL, self(), create_postman),
|
|
||||||
{noreply, State}
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
%% 离线时,忽略数据发送逻辑
|
handle_event(state_timeout, connect, disconnected, State) ->
|
||||||
handle_info({next_data, _Id, _Tuple}, State = #state{status = ?DISCONNECTED}) ->
|
{next_state, connecting, State, [{next_event, internal, do_connect}]};
|
||||||
{noreply, State};
|
handle_event(state_timeout, connect, connecting, State) ->
|
||||||
%% 发送数据到mqtt服务器
|
{keep_state, State, [{next_event, internal, do_connect}]};
|
||||||
handle_info({next_data, Id, Metric}, State = #state{status = ?CONNECTED, conn_pid = ConnPid, buffer = Buffer, inflight = InFlight,
|
|
||||||
endpoint = #endpoint{config = #mqtt_endpoint{topic = Topic, qos = Qos}}}) ->
|
|
||||||
|
|
||||||
|
%% 离线或连接中时,忽略数据发送逻辑
|
||||||
|
handle_event(info, {next_data, _Id, _Tuple}, disconnected, State) ->
|
||||||
|
{keep_state, State};
|
||||||
|
handle_event(info, {next_data, _Id, _Tuple}, connecting, State) ->
|
||||||
|
{keep_state, State};
|
||||||
|
%% 发送数据到mqtt服务器
|
||||||
|
handle_event(info, {next_data, Id, Metric}, connected,
|
||||||
|
State = #state{
|
||||||
|
conn_pid = ConnPid,
|
||||||
|
buffer = Buffer,
|
||||||
|
inflight = InFlight,
|
||||||
|
endpoint = #endpoint{config = #mqtt_endpoint{topic = Topic, qos = Qos}}
|
||||||
|
}) ->
|
||||||
logger:debug("[endpoint_mqtt] will publish topic: ~p, metric: ~p, qos: ~p", [Topic, Metric, Qos]),
|
logger:debug("[endpoint_mqtt] will publish topic: ~p, metric: ~p, qos: ~p", [Topic, Metric, Qos]),
|
||||||
case emqtt:publish(ConnPid, Topic, #{}, Metric, [{qos, Qos}, {retain, true}]) of
|
case emqtt:publish(ConnPid, Topic, #{}, Metric, [{qos, Qos}, {retain, true}]) of
|
||||||
ok ->
|
ok ->
|
||||||
NBuffer = endpoint_buffer:ack(Id, Buffer),
|
NBuffer = endpoint_buffer:ack(Id, Buffer),
|
||||||
{noreply, State#state{buffer = NBuffer}};
|
{keep_state, State#state{buffer = NBuffer}};
|
||||||
{ok, PacketId} ->
|
{ok, PacketId} ->
|
||||||
{noreply, State#state{inflight = maps:put(PacketId, Id, InFlight)}};
|
{keep_state, State#state{inflight = maps:put(PacketId, Id, InFlight)}};
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
logger:warning("[endpoint_mqtt] send message to topic: ~p, get error: ~p", [Topic, Reason]),
|
logger:warning("[endpoint_mqtt] send message to topic: ~p, get error: ~p", [Topic, Reason]),
|
||||||
stop_mqtt_conn(ConnPid),
|
stop_mqtt_conn(ConnPid),
|
||||||
schedule_reconnect(),
|
{next_state, disconnected, State#state{conn_pid = undefined, inflight = #{}},
|
||||||
{noreply, State#state{conn_pid = undefined, inflight = #{}, status = ?DISCONNECTED}}
|
[{state_timeout, ?RETRY_INTERVAL, connect}]}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
handle_info({disconnected, ReasonCode, Properties}, State = #state{status = ?CONNECTED}) ->
|
handle_event(info, {disconnected, ReasonCode, Properties}, connected, State = #state{conn_pid = ConnPid}) ->
|
||||||
logger:debug("[endpoint_mqtt] Recv a DISONNECT packet - ReasonCode: ~p, Properties: ~p", [ReasonCode, Properties]),
|
logger:debug("[endpoint_mqtt] Recv a DISONNECT packet - ReasonCode: ~p, Properties: ~p", [ReasonCode, Properties]),
|
||||||
schedule_reconnect(),
|
stop_mqtt_conn(ConnPid),
|
||||||
{noreply, State#state{conn_pid = undefined, inflight = #{}, status = ?DISCONNECTED}};
|
{next_state, disconnected, State#state{conn_pid = undefined, inflight = #{}},
|
||||||
|
[{state_timeout, ?RETRY_INTERVAL, connect}]};
|
||||||
handle_info({publish, Message = #{packet_id := _PacketId, payload := Payload}}, State = #state{status = ?CONNECTED}) ->
|
handle_event(info, {publish, Message = #{packet_id := _PacketId, payload := Payload}}, connected, State) ->
|
||||||
logger:debug("[endpoint_mqtt] Recv a publish packet: ~p, payload: ~p", [Message, Payload]),
|
logger:debug("[endpoint_mqtt] Recv a publish packet: ~p, payload: ~p", [Message, Payload]),
|
||||||
{noreply, State};
|
{keep_state, State};
|
||||||
|
handle_event(info, {puback, #{packet_id := PacketId}}, connected,
|
||||||
%% 收到确认的消息
|
State = #state{inflight = Inflight, buffer = Buffer}) ->
|
||||||
handle_info({puback, #{packet_id := PacketId}}, State = #state{status = ?CONNECTED, inflight = Inflight, buffer = Buffer}) ->
|
|
||||||
case maps:take(PacketId, Inflight) of
|
case maps:take(PacketId, Inflight) of
|
||||||
{Id, RestInflight} ->
|
{Id, RestInflight} ->
|
||||||
NBuffer = endpoint_buffer:ack(Id, Buffer),
|
NBuffer = endpoint_buffer:ack(Id, Buffer),
|
||||||
{noreply, State#state{buffer = NBuffer, inflight = RestInflight}};
|
{keep_state, State#state{buffer = NBuffer, inflight = RestInflight}};
|
||||||
error ->
|
error ->
|
||||||
{noreply, State}
|
{keep_state, State}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
%% postman进程挂掉时,重新建立新的
|
%% postman进程挂掉时,重新建立新的
|
||||||
handle_info({'EXIT', ConnPid, Reason}, State = #state{endpoint = #endpoint{title = Title}, conn_pid = ConnPid}) ->
|
handle_event(info, {'EXIT', ConnPid, Reason}, _StateName,
|
||||||
|
State = #state{endpoint = #endpoint{title = Title}, conn_pid = ConnPid}) ->
|
||||||
logger:warning("[endpoint_mqtt] endpoint: ~p, conn pid exit with reason: ~p", [Title, Reason]),
|
logger:warning("[endpoint_mqtt] endpoint: ~p, conn pid exit with reason: ~p", [Title, Reason]),
|
||||||
schedule_reconnect(),
|
{next_state, disconnected, State#state{conn_pid = undefined, inflight = #{}},
|
||||||
{noreply, State#state{conn_pid = undefined, inflight = #{}, status = ?DISCONNECTED}};
|
[{state_timeout, ?RETRY_INTERVAL, connect}]};
|
||||||
|
|
||||||
handle_info(Info, State = #state{status = Status}) ->
|
handle_event(info, Info, StateName, State) ->
|
||||||
logger:warning("[endpoint_mqtt] unknown message: ~p, status: ~p", [Info, Status]),
|
logger:warning("[endpoint_mqtt] unknown message: ~p, status: ~p", [Info, StateName]),
|
||||||
{noreply, State}.
|
{keep_state, State};
|
||||||
|
handle_event(EventType, EventContent, StateName, State) ->
|
||||||
|
logger:warning("[endpoint_mqtt] unknown event: ~p, content: ~p, status: ~p", [EventType, EventContent, StateName]),
|
||||||
|
{keep_state, State}.
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
%% @doc This function is called by a gen_server when it is about to
|
%% @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
|
%% terminate. It should be the opposite of Module:init/1 and do any
|
||||||
%% necessary cleaning up. When it returns, the gen_server terminates
|
%% necessary cleaning up. When it returns, the gen_statem terminates
|
||||||
%% with Reason. The return value is ignored.
|
%% with Reason. The return value is ignored.
|
||||||
-spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()),
|
-spec terminate(Reason :: term(), StateName :: mqtt_state(), State :: #state{}) -> term().
|
||||||
State :: #state{}) -> term()).
|
terminate(Reason, _StateName, #state{endpoint = #endpoint{title = Title}, buffer = Buffer, conn_pid = ConnPid}) ->
|
||||||
terminate(Reason, #state{endpoint = #endpoint{title = Title}, buffer = Buffer, conn_pid = ConnPid}) ->
|
|
||||||
logger:debug("[endpoint_mqtt] endpoint: ~p, terminate with reason: ~p", [Title, Reason]),
|
logger:debug("[endpoint_mqtt] endpoint: ~p, terminate with reason: ~p", [Title, Reason]),
|
||||||
stop_mqtt_conn(ConnPid),
|
stop_mqtt_conn(ConnPid),
|
||||||
endpoint_buffer:cleanup(Buffer),
|
endpoint_buffer:cleanup(Buffer),
|
||||||
@ -204,11 +202,13 @@ terminate(Reason, #state{endpoint = #endpoint{title = Title}, buffer = Buffer, c
|
|||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
%% @doc Convert process state when code is changed
|
%% @doc Convert process state when code is changed
|
||||||
-spec(code_change(OldVsn :: term() | {down, term()}, State :: #state{},
|
-spec code_change(OldVsn :: term() | {down, term()},
|
||||||
Extra :: term()) ->
|
StateName :: mqtt_state(),
|
||||||
{ok, NewState :: #state{}} | {error, Reason :: term()}).
|
State :: #state{},
|
||||||
code_change(_OldVsn, State = #state{}, _Extra) ->
|
Extra :: term()) ->
|
||||||
{ok, State}.
|
{ok, StateName :: mqtt_state(), NewState :: #state{}} | {error, term()}.
|
||||||
|
code_change(_OldVsn, StateName, State = #state{}, _Extra) ->
|
||||||
|
{ok, StateName, State}.
|
||||||
|
|
||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
%%% Internal functions
|
%%% Internal functions
|
||||||
@ -221,9 +221,30 @@ ensure_subscription(Matcher, NMatcher) ->
|
|||||||
ok = endpoint_subscription:unsubscribe(Matcher, self()),
|
ok = endpoint_subscription:unsubscribe(Matcher, self()),
|
||||||
endpoint_subscription:subscribe(NMatcher, self()).
|
endpoint_subscription:subscribe(NMatcher, self()).
|
||||||
|
|
||||||
-spec schedule_reconnect() -> reference().
|
-spec maybe_connect_actions(mqtt_state()) -> [gen_statem:action()].
|
||||||
schedule_reconnect() ->
|
maybe_connect_actions(connected) ->
|
||||||
erlang:start_timer(?RETRY_INTERVAL, self(), create_postman).
|
[{next_event, internal, do_connect}];
|
||||||
|
maybe_connect_actions(connecting) ->
|
||||||
|
[{next_event, internal, do_connect}];
|
||||||
|
maybe_connect_actions(disconnected) ->
|
||||||
|
[{next_event, internal, do_connect}].
|
||||||
|
|
||||||
|
-spec connect_mqtt(list()) -> {ok, pid()} | {error, term()}.
|
||||||
|
connect_mqtt(Opts) ->
|
||||||
|
try
|
||||||
|
{ok, ConnPid} = emqtt:start_link(Opts),
|
||||||
|
logger:debug("[endpoint_mqtt] start connect, options: ~p", [Opts]),
|
||||||
|
case emqtt:connect(ConnPid, 5000) of
|
||||||
|
{ok, _} ->
|
||||||
|
{ok, ConnPid};
|
||||||
|
{error, Reason} ->
|
||||||
|
stop_mqtt_conn(ConnPid),
|
||||||
|
{error, Reason}
|
||||||
|
end
|
||||||
|
catch
|
||||||
|
_:Error ->
|
||||||
|
{error, Error}
|
||||||
|
end.
|
||||||
|
|
||||||
-spec stop_mqtt_conn(undefined | pid()) -> ok.
|
-spec stop_mqtt_conn(undefined | pid()) -> ok.
|
||||||
stop_mqtt_conn(undefined) ->
|
stop_mqtt_conn(undefined) ->
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user