From 1f4c17f118e68018dacc383e572e1d5c2083c659 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Sun, 19 Apr 2026 16:30:57 +0800 Subject: [PATCH] fix mqtt --- src/endpoint/endpoint_mqtt.erl | 243 ++++++++++++++++++--------------- 1 file changed, 132 insertions(+), 111 deletions(-) diff --git a/src/endpoint/endpoint_mqtt.erl b/src/endpoint/endpoint_mqtt.erl index 8f5acf4..05fa168 100644 --- a/src/endpoint/endpoint_mqtt.erl +++ b/src/endpoint/endpoint_mqtt.erl @@ -1,4 +1,3 @@ - %%%------------------------------------------------------------------- %%% @author aresei %%% @copyright (C) 2023, @@ -10,30 +9,27 @@ -module(endpoint_mqtt). -include("endpoint.hrl"). --behaviour(gen_server). +-behaviour(gen_statem). %% API -export([start_link/2]). -%% gen_server callbacks --export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). +%% gen_statem callbacks +-export([callback_mode/0, init/1, handle_event/4, terminate/3, code_change/4]). %% 消息重发间隔 -define(RETRY_INTERVAL, 15000). --define(DISCONNECTED, disconnected). --define(CONNECTED, connected). - -record(state, { endpoint :: #endpoint{}, buffer :: endpoint_buffer:buffer(), conn_pid :: undefined | pid(), %% 待确认的数据, #{PacketId :: integer() => Id :: integer()} - inflight = #{}, - - status = disconnected + inflight = #{} }). +-type mqtt_state() :: disconnected | connecting | connected. + %%%=================================================================== %%% API %%%=================================================================== @@ -41,69 +37,67 @@ %% @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. +-spec start_link(LocalName :: atom(), Endpoint :: #endpoint{}) -> + {ok, pid()} | ignore | {error, term()}. 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 %%%=================================================================== +-spec callback_mode() -> handle_event_function. +callback_mode() -> + handle_event_function. + %% @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. +-spec init(term()) -> gen_statem:init_result(mqtt_state(), #state{}). init([Endpoint = #endpoint{matcher = Matcher}]) -> ok = iot_log:set_metadata(), erlang:process_flag(trap_exit, true), - endpoint_subscription:subscribe(Matcher, self()), - - %% 创建转发器, 避免阻塞当前进程的创建,因此采用了延时初始化的机制 - erlang:start_timer(0, self(), create_postman), - %% 初始化存储 + ok = endpoint_subscription:subscribe(Matcher, self()), 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}}. - -%% @private -%% @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}) -> +-spec handle_event(gen_statem:event_type(), term(), mqtt_state(), #state{}) -> + gen_statem:event_handler_result(mqtt_state(), #state{}). +handle_event({call, From}, get_stat, _StateName, State = #state{buffer = Buffer}) -> Stat = endpoint_buffer:stat(Buffer), - {reply, {ok, Stat}, State}. - -%% @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}) -> + {keep_state, State, [{reply, From, {ok, Stat}}]}; +handle_event(cast, {forward, Metric}, _StateName, State = #state{buffer = Buffer}) -> NBuffer = endpoint_buffer:append(Metric, Buffer), - {noreply, State#state{buffer = NBuffer}}; -handle_cast({reload, NEndpoint = #endpoint{matcher = NMatcher}}, State = #state{endpoint = #endpoint{matcher = Matcher}, conn_pid = ConnPid}) -> + {keep_state, State#state{buffer = NBuffer}}; +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), stop_mqtt_conn(ConnPid), - schedule_reconnect(), - {noreply, State#state{endpoint = NEndpoint, conn_pid = undefined, inflight = #{}, status = ?DISCONNECTED}}; -handle_cast(cleanup, State = #state{buffer = Buffer}) -> - endpoint_buffer:cleanup(Buffer), - {noreply, State}. + NState = State#state{endpoint = NEndpoint, conn_pid = undefined, inflight = #{}}, + {next_state, connecting, NState, maybe_connect_actions(StateName)}; -%% @private -%% @doc Handling all non call/cast messages --spec(handle_info(Info :: timeout() | term(), State :: #state{}) -> - {noreply, NewState :: #state{}} | - {noreply, NewState :: #state{}, timeout() | hibernate} | - {stop, Reason :: term(), NewState :: #state{}}). -handle_info({timeout, _, create_postman}, State = #state{buffer = Buffer, status = ?DISCONNECTED, - endpoint = #endpoint{title = Title, config = #mqtt_endpoint{host = Host, port = Port, username = Username, password = Password, client_id = ClientId}}}) -> +handle_event(internal, connect, disconnected, State) -> + {next_state, connecting, State, [{next_event, internal, do_connect}]}; +handle_event(internal, connect, connecting, State) -> + {keep_state, State, [{next_event, internal, do_connect}]}; +handle_event(internal, connect, connected, State) -> + {keep_state, State}; +handle_event(internal, do_connect, connecting, + 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]), Opts = [ {owner, self()}, @@ -119,84 +113,88 @@ handle_info({timeout, _, create_postman}, State = #state{buffer = Buffer, status {proto_ver, v5}, {retry_interval, 5000} ], - - try - {ok, ConnPid} = emqtt:start_link(Opts), - logger:debug("[endpoint_mqtt] start connect, options: ~p", [Opts]), - case emqtt:connect(ConnPid, 5000) of - {ok, _} -> - logger:debug("[endpoint_mqtt] connect success, pid: ~p", [ConnPid]), - NBuffer = endpoint_buffer:trigger_n(Buffer), - {noreply, State#state{conn_pid = ConnPid, buffer = NBuffer, status = ?CONNECTED}}; - {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} + case connect_mqtt(Opts) of + {ok, ConnPid} -> + logger:debug("[endpoint_mqtt] connect success, pid: ~p", [ConnPid]), + NBuffer = endpoint_buffer:trigger_n(Buffer), + {next_state, connected, State#state{conn_pid = ConnPid, buffer = NBuffer}}; + {error, Reason} -> + logger:warning("[endpoint_mqtt] connect get error: ~p", [Reason]), + {next_state, disconnected, State#state{conn_pid = undefined, inflight = #{}}, + [{state_timeout, ?RETRY_INTERVAL, connect}]} end; -%% 离线时,忽略数据发送逻辑 -handle_info({next_data, _Id, _Tuple}, State = #state{status = ?DISCONNECTED}) -> - {noreply, State}; -%% 发送数据到mqtt服务器 -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(state_timeout, connect, disconnected, State) -> + {next_state, connecting, State, [{next_event, internal, do_connect}]}; +handle_event(state_timeout, connect, connecting, State) -> + {keep_state, State, [{next_event, internal, do_connect}]}; +%% 离线或连接中时,忽略数据发送逻辑 +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]), case emqtt:publish(ConnPid, Topic, #{}, Metric, [{qos, Qos}, {retain, true}]) of ok -> NBuffer = endpoint_buffer:ack(Id, Buffer), - {noreply, State#state{buffer = NBuffer}}; + {keep_state, State#state{buffer = NBuffer}}; {ok, PacketId} -> - {noreply, State#state{inflight = maps:put(PacketId, Id, InFlight)}}; + {keep_state, State#state{inflight = maps:put(PacketId, Id, InFlight)}}; {error, Reason} -> logger:warning("[endpoint_mqtt] send message to topic: ~p, get error: ~p", [Topic, Reason]), stop_mqtt_conn(ConnPid), - schedule_reconnect(), - {noreply, State#state{conn_pid = undefined, inflight = #{}, status = ?DISCONNECTED}} + {next_state, disconnected, State#state{conn_pid = undefined, inflight = #{}}, + [{state_timeout, ?RETRY_INTERVAL, connect}]} 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]), - schedule_reconnect(), - {noreply, State#state{conn_pid = undefined, inflight = #{}, status = ?DISCONNECTED}}; - -handle_info({publish, Message = #{packet_id := _PacketId, payload := Payload}}, State = #state{status = ?CONNECTED}) -> + stop_mqtt_conn(ConnPid), + {next_state, disconnected, State#state{conn_pid = undefined, inflight = #{}}, + [{state_timeout, ?RETRY_INTERVAL, connect}]}; +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]), - {noreply, State}; - -%% 收到确认的消息 -handle_info({puback, #{packet_id := PacketId}}, State = #state{status = ?CONNECTED, inflight = Inflight, buffer = Buffer}) -> + {keep_state, State}; +handle_event(info, {puback, #{packet_id := PacketId}}, connected, + State = #state{inflight = Inflight, buffer = Buffer}) -> case maps:take(PacketId, Inflight) of {Id, RestInflight} -> NBuffer = endpoint_buffer:ack(Id, Buffer), - {noreply, State#state{buffer = NBuffer, inflight = RestInflight}}; + {keep_state, State#state{buffer = NBuffer, inflight = RestInflight}}; error -> - {noreply, State} + {keep_state, State} end; %% 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]), - schedule_reconnect(), - {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(Info, State = #state{status = Status}) -> - logger:warning("[endpoint_mqtt] unknown message: ~p, status: ~p", [Info, Status]), - {noreply, State}. +handle_event(info, Info, StateName, State) -> + logger:warning("[endpoint_mqtt] unknown message: ~p, status: ~p", [Info, StateName]), + {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 -%% @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 -%% 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. --spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()), - State :: #state{}) -> term()). -terminate(Reason, #state{endpoint = #endpoint{title = Title}, buffer = Buffer, conn_pid = ConnPid}) -> +-spec terminate(Reason :: term(), StateName :: mqtt_state(), State :: #state{}) -> term(). +terminate(Reason, _StateName, #state{endpoint = #endpoint{title = Title}, buffer = Buffer, conn_pid = ConnPid}) -> logger:debug("[endpoint_mqtt] endpoint: ~p, terminate with reason: ~p", [Title, Reason]), stop_mqtt_conn(ConnPid), endpoint_buffer:cleanup(Buffer), @@ -204,11 +202,13 @@ terminate(Reason, #state{endpoint = #endpoint{title = Title}, buffer = Buffer, c %% @private %% @doc Convert process state when code is changed --spec(code_change(OldVsn :: term() | {down, term()}, State :: #state{}, - Extra :: term()) -> - {ok, NewState :: #state{}} | {error, Reason :: term()}). -code_change(_OldVsn, State = #state{}, _Extra) -> - {ok, State}. +-spec code_change(OldVsn :: term() | {down, term()}, + StateName :: mqtt_state(), + State :: #state{}, + Extra :: term()) -> + {ok, StateName :: mqtt_state(), NewState :: #state{}} | {error, term()}. +code_change(_OldVsn, StateName, State = #state{}, _Extra) -> + {ok, StateName, State}. %%%=================================================================== %%% Internal functions @@ -221,9 +221,30 @@ ensure_subscription(Matcher, NMatcher) -> ok = endpoint_subscription:unsubscribe(Matcher, self()), endpoint_subscription:subscribe(NMatcher, self()). --spec schedule_reconnect() -> reference(). -schedule_reconnect() -> - erlang:start_timer(?RETRY_INTERVAL, self(), create_postman). +-spec maybe_connect_actions(mqtt_state()) -> [gen_statem:action()]. +maybe_connect_actions(connected) -> + [{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. stop_mqtt_conn(undefined) ->