From 3fcba98d43573e4f1253eedbdfb7a227584ef29f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=89=E7=A4=BC=E6=88=90?= Date: Mon, 17 Apr 2023 16:45:07 +0800 Subject: [PATCH] fix --- apps/iot/src/iot_config.erl | 35 +++++ apps/iot/src/iot_emqtt_client.erl | 119 -------------- apps/iot/src/iot_host.erl | 69 +++++---- apps/iot/src/iot_host_mocker.erl | 21 +-- apps/iot/src/iot_mqtt_share_subscriber.erl | 172 +++++++++++++++++++++ apps/iot/src/iot_sup.erl | 6 +- 6 files changed, 248 insertions(+), 174 deletions(-) create mode 100644 apps/iot/src/iot_config.erl delete mode 100644 apps/iot/src/iot_emqtt_client.erl create mode 100644 apps/iot/src/iot_mqtt_share_subscriber.erl diff --git a/apps/iot/src/iot_config.erl b/apps/iot/src/iot_config.erl new file mode 100644 index 0000000..660c5bb --- /dev/null +++ b/apps/iot/src/iot_config.erl @@ -0,0 +1,35 @@ +%%%------------------------------------------------------------------- +%%% @author licheng5 +%%% @copyright (C) 2023, +%%% @doc +%%% +%%% @end +%%% Created : 17. 4月 2023 16:41 +%%%------------------------------------------------------------------- +-module(iot_config). +-author("licheng5"). + +%% API +-export([emqt_opts/0]). + +emqt_opts() -> + %% 建立到emqx服务器的连接 + {ok, Props} = application:get_env(iot, emqx_server), + EMQXHost = proplists:get_value(host, Props), + EMQXPort = proplists:get_value(port, Props, 18080), + Username = proplists:get_value(username, Props), + Password = proplists:get_value(password, Props), + RetryInterval = proplists:get_value(retry_interval, Props, 5), + Keepalive = proplists:get_value(keepalive, Props, 86400), + [ + {host, EMQXHost}, + {port, EMQXPort}, + {owner, self()}, + {tcp_opts, []}, + {username, Username}, + {password, Password}, + {keepalive, Keepalive}, + {auto_ack, false}, + {proto_ver, v5}, + {retry_interval, RetryInterval} + ]. \ No newline at end of file diff --git a/apps/iot/src/iot_emqtt_client.erl b/apps/iot/src/iot_emqtt_client.erl deleted file mode 100644 index 95e8329..0000000 --- a/apps/iot/src/iot_emqtt_client.erl +++ /dev/null @@ -1,119 +0,0 @@ -%%%------------------------------------------------------------------- -%%% @author licheng5 -%%% @copyright (C) 2023, -%%% @doc -%%% -%%% @end -%%% Created : 06. 3月 2023 15:42 -%%%------------------------------------------------------------------- --module(iot_emqtt_client). --author("licheng5"). - --behaviour(gen_server). - -%% API --export([start_link/0]). - -%% gen_server callbacks --export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). - --export([publish/3]). - --define(SERVER, ?MODULE). - --record(state, { - conn_pid :: pid() -}). - -%%%=================================================================== -%%% API -%%%=================================================================== - -publish(Topic, Message, Qos) when is_binary(Topic), is_binary(Message), is_integer(Qos), Qos == 0; Qos == 1 -> - gen_server:call(?SERVER, {publish, Topic, Message, Qos}). - -%% @doc Spawns the server and registers the local name (unique) --spec(start_link() -> - {ok, Pid :: pid()} | ignore | {error, Reason :: term()}). -start_link() -> - gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). - -%%%=================================================================== -%%% gen_server callbacks -%%%=================================================================== - -%% @private -%% @doc Initializes the server --spec(init(Args :: term()) -> - {ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} | - {stop, Reason :: term()} | ignore). -init([]) -> - %{ok, ServerOpts} = application:get_env(iot, emqx_server), - % {ok, ConnPid} = emqtt:start_link([{clientid, iot_emqtt_client}, {owner, self()}|ServerOpts]), - %{ok, _Props} = emqtt:connect(ConnPid), - - %SubOpts = [{qos, 1}], - %{ok, _Props, _ReasonCodes} = emqtt:subscribe(ConnPid, #{}, [{<<"hello">>, SubOpts}]), - - {ok, #state{}}. - -%% @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({publish, Topic, Message, Qos}, _From, State = #state{conn_pid = ConnPid}) -> - if - Qos == 0 -> - ok = emqtt:publish(ConnPid, Topic, #{}, Message, [{qos, 0}]); - Qos == 1 -> - {ok, _PktId} = emqtt:publish(ConnPid, Topic, #{}, Message, [{qos, 1}]); - true -> - ok - end, - {reply, ok, 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(_Request, State = #state{}) -> - {noreply, State}. - -%% @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(_Info, State = #state{}) -> - {noreply, State}. - -%% @private -%% @doc This function is called by a gen_server 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 -%% with Reason. The return value is ignored. --spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()), - State :: #state{}) -> term()). -terminate(_Reason, _State = #state{}) -> - ok. - -%% @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}. - -%%%=================================================================== -%%% Internal functions -%%%=================================================================== diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index 6780066..3d8a17c 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -14,7 +14,7 @@ %% API -export([test/1]). --export([start_link/2, get_name/1, get_pid/1, publish/2]). +-export([start_link/2, get_name/1, get_pid/1, publish/2, publish_result/2]). %% gen_server callbacks -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). @@ -23,7 +23,9 @@ -record(state, { host :: #host{}, - emqx_pid :: pid() + emqx_pid :: pid(), + %% 发送的未确认的包 + inflight = #{} }). test(Msg) when is_binary(Msg) -> @@ -40,9 +42,21 @@ get_pid(HostId) when is_binary(HostId) -> get_name(HostId) when is_binary(HostId) -> binary_to_atom(<<"iot_host:", HostId/binary>>). --spec publish(pid(), binary()) -> {ok, PacketId :: integer()} | {error, term()}. +-spec publish(pid(), binary()) -> {ok, Ref :: reference()} | {error, term()}. publish(Pid, Message) when is_pid(Pid), is_binary(Message) -> - gen_server:call(Pid, {publish, Message}). + gen_server:call(Pid, {publish, self(), Message}). + +%% 获取publish的结果 +-spec publish_result(tuple(), Timeout :: integer()) -> {ok, PacketId :: integer()} | {error, Reason :: any()}. +publish_result({error, Reason}, _) -> + {error, Reason}; +publish_result({ok, Ref}, Timeout) when is_reference(Ref), is_integer(Timeout) -> + receive + {ok, Ref, PacketId} -> + {ok, PacketId} + after Timeout -> + {error, timeout} + end. %% @doc Spawns the server and registers the local name (unique) -spec(start_link(Name :: atom(), Host :: #host{}) -> @@ -61,27 +75,8 @@ start_link(Name, Host = #host{}) -> {stop, Reason :: term()} | ignore). init([Host = #host{host_id = HostId}]) -> lager:debug("[iot_host] host_id: ~p, host is: ~p", [HostId, Host]), - %% 建立到emqx服务器的连接 - {ok, Props} = application:get_env(iot, emqx_server), - EMQXHost = proplists:get_value(host, Props), - EMQXPort = proplists:get_value(port, Props, 18080), - Username = proplists:get_value(username, Props), - Password = proplists:get_value(password, Props), - RetryInterval = proplists:get_value(retry_interval, Props, 5), - Keepalive = proplists:get_value(keepalive, Props, 86400), - Opts = [ - {host, EMQXHost}, - {port, EMQXPort}, - {owner, self()}, - {tcp_opts, []}, - {username, Username}, - {password, Password}, - {keepalive, Keepalive}, - {auto_ack, false}, - {proto_ver, v5}, - {retry_interval, RetryInterval} - ], + Opts = iot_config:emqt_opts(), case emqtt:start_link(Opts) of {ok, ConnPid} -> lager:debug("[iot_host] connect success, pid: ~p", [ConnPid]), @@ -107,10 +102,15 @@ init([Host = #host{host_id = HostId}]) -> {noreply, NewState :: #state{}, timeout() | hibernate} | {stop, Reason :: term(), Reply :: term(), NewState :: #state{}} | {stop, Reason :: term(), NewState :: #state{}}). -handle_call({publish, Message}, _From, State = #state{emqx_pid = ConnPid, host = #host{host_id = HostId}}) -> +handle_call({publish, ReceiverPid, Message}, _From, State = #state{emqx_pid = ConnPid, inflight = InFlight, host = #host{host_id = HostId}}) -> Topic = <<"/host/", HostId/binary, "/downstream">>, - Result = emqtt:publish(ConnPid, Topic, #{}, Message, [{qos, 2}, {retain, true}]), - {reply, Result, State}. + case emqtt:publish(ConnPid, Topic, #{}, Message, [{qos, 2}, {retain, true}]) of + {ok, PacketId} -> + Ref = make_ref(), + {reply, {ok, Ref}, State#state{inflight = maps:put(PacketId, {ReceiverPid, Ref, Message}, InFlight)}}; + {error, Reason} -> + {reply, {error, Reason}, State} + end. %% @private %% @doc Handling cast messages @@ -145,12 +145,17 @@ handle_info({disconnect, ReasonCode, Properties}, State = #state{host = #host{ho {stop, disconnected, State}; handle_info({publish, Message = #{packet_id := _PacketId, payload := Payload}}, State = #state{emqx_pid = _ConnPid, host = #host{host_id = HostId}}) -> lager:debug("[iot_host] host: ~p, Recv a publish packet: ~p, payload: ~p", [HostId, Message, Payload]), - % emqtt:pubrec(ConnPid, PacketId), - {noreply, State}; -handle_info({puback, Msg = #{packet_id := PacketId, reason_code := ReasonCode}}, State = #state{emqx_pid = ConnPid, host = #host{host_id = HostId}}) -> - lager:debug("[iot_host] host: ~p, receive puback packet_id: ~p, reason_code: ~p, msg: ~p", [HostId, PacketId, ReasonCode, Msg]), - emqtt:pubrel(ConnPid, PacketId), {noreply, State}; +handle_info({puback, Packet = #{packet_id := PacketId}}, State = #state{inflight = Inflight, host = #host{host_id = HostId}}) -> + case maps:take(PacketId, Inflight) of + {{ReceiverPid, Ref, Message}, Inflight1} -> + lager:debug("[iot_host] host: ~p, receive puback packet: ~p, assoc message: ~p", [HostId, Packet, Message]), + ReceiverPid ! {ok, Ref, PacketId}, + {noreply, State#state{inflight = Inflight1}}; + error -> + lager:warning("[iot_host] host: ~p, receive unknown puback packet: ~p", [HostId, Packet]), + {noreply, State} + end; handle_info(Info, State = #state{host = #host{host_id = HostId}}) -> lager:debug("host_id: ~p, get info: ~p", [HostId, Info]), diff --git a/apps/iot/src/iot_host_mocker.erl b/apps/iot/src/iot_host_mocker.erl index 9ec9099..1ef3042 100644 --- a/apps/iot/src/iot_host_mocker.erl +++ b/apps/iot/src/iot_host_mocker.erl @@ -50,26 +50,7 @@ start_link(HostId) when is_binary(HostId) -> {stop, Reason :: term()} | ignore). init([HostId]) -> %% 建立到emqx服务器的连接 - {ok, Props} = application:get_env(iot, emqx_server), - EMQXHost = proplists:get_value(host, Props), - EMQXPort = proplists:get_value(port, Props, 18080), - Username = proplists:get_value(username, Props), - Password = proplists:get_value(password, Props), - RetryInterval = proplists:get_value(retry_interval, Props, 5), - Keepalive = proplists:get_value(keepalive, Props, 86400), - Opts = [ - {host, EMQXHost}, - {port, EMQXPort}, - {owner, self()}, - {tcp_opts, []}, - {username, Username}, - {password, Password}, - {keepalive, Keepalive}, - {proto_ver, v5}, - {auto_ack, true}, - {retry_interval, RetryInterval} - ], - + Opts = iot_config:emqt_opts(), case emqtt:start_link(Opts) of {ok, ConnPid} -> lager:debug("[iot_host_mocker] connect success, pid: ~p", [ConnPid]), diff --git a/apps/iot/src/iot_mqtt_share_subscriber.erl b/apps/iot/src/iot_mqtt_share_subscriber.erl new file mode 100644 index 0000000..58067f4 --- /dev/null +++ b/apps/iot/src/iot_mqtt_share_subscriber.erl @@ -0,0 +1,172 @@ +%%%------------------------------------------------------------------- +%%% @author aresei +%%% @copyright (C) 2023, +%%% @doc +%%% +%%% @end +%%% Created : 12. 3月 2023 21:27 +%%%------------------------------------------------------------------- +-module(iot_mqtt_share_subscriber). +-author("aresei"). +-include("iot.hrl"). + +-behaviour(gen_server). + +%% API +-export([test/1]). +-export([start_link/0, get_name/1, get_pid/1, publish/2, publish_result/2]). + +%% gen_server callbacks +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). + +-define(SERVER, ?MODULE). + +-record(state, { + conn_pid :: pid() +}). + +test(Msg) when is_binary(Msg) -> + Pid = get_pid(<<"1">>), + publish(Pid, Msg). + +%%%=================================================================== +%%% API +%%%=================================================================== +get_pid(HostId) when is_binary(HostId) -> + Name = get_name(HostId), + global:whereis_name(Name). + +get_name(HostId) when is_binary(HostId) -> + binary_to_atom(<<"iot_host:", HostId/binary>>). + +-spec publish(pid(), binary()) -> {ok, Ref :: reference()} | {error, term()}. +publish(Pid, Message) when is_pid(Pid), is_binary(Message) -> + gen_server:call(Pid, {publish, self(), Message}). + +%% 获取publish的结果 +-spec publish_result(tuple(), Timeout :: integer()) -> {ok, PacketId :: integer()} | {error, Reason :: any()}. +publish_result({error, Reason}, _) -> + {error, Reason}; +publish_result({ok, Ref}, Timeout) when is_reference(Ref), is_integer(Timeout) -> + receive + {ok, Ref, PacketId} -> + {ok, PacketId} + after Timeout -> + {error, timeout} + end. + +%% @doc Spawns the server and registers the local name (unique) +-spec(start_link() -> + {ok, Pid :: pid()} | ignore | {error, Reason :: term()}). +start_link() -> + gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). + +%%%=================================================================== +%%% gen_server callbacks +%%%=================================================================== + +%% @private +%% @doc Initializes the server +-spec(init(Args :: term()) -> + {ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} | + {stop, Reason :: term()} | ignore). +init([]) -> + %% 建立到emqx服务器的连接 + Opts = iot_config:emqt_opts(), + case emqtt:start_link(Opts) of + {ok, ConnPid} -> + lager:debug("[iot_host] connect success, pid: ~p", [ConnPid]), + %% 快速启动避免阻塞iot_host_sup的启动 + erlang:start_timer(0, self(), subscribe_ticker), + + {ok, #state{conn_pid = ConnPid}}; + ignore -> + lager:debug("[iot_host] connect emqx get ignore"), + {stop, ignore}; + {error, Reason} -> + lager:debug("[iot_host] connect emqx get error: ~p", [Reason]), + {stop, Reason} + end. + +%% @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(_Info, _From, State = #state{conn_pid = _ConnPid}) -> + {reply, ok, 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(_Request, State = #state{}) -> + {noreply, State}. + +%% @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{}}). +%% 处理topic的订阅事件 +handle_info({timeout, _, subscribe_ticker}, State = #state{conn_pid = ConnPid}) -> + %% 监听和host相关的全部事件 + {ok, _} = emqtt:connect(ConnPid), + Topics = [ + + ], + SubscribeResult = emqtt:subscribe(ConnPid, Topics), + lager:debug("[iot_mqtt_share_subscriber] subscribe result is: ~p", [SubscribeResult]), + + {noreply, State#state{conn_pid = ConnPid}}; + +handle_info({disconnect, ReasonCode, Properties}, State = #state{}) -> + lager:debug("[iot_mqtt_share_subscriber] Recv a DISONNECT packet - ReasonCode: ~p, Properties: ~p", [ReasonCode, Properties]), + {stop, disconnected, State}; +handle_info({publish, Message = #{packet_id := _PacketId, payload := Payload}}, State = #state{conn_pid = _ConnPid}) -> + lager:debug("[iot_mqtt_share_subscriber] Recv a publish packet: ~p, payload: ~p", [Message, Payload]), + {noreply, State}; +handle_info({puback, Packet = #{packet_id := _PacketId}}, State = #state{}) -> + lager:debug("[iot_mqtt_share_subscriber] receive puback packet: ~p", [Packet]), + {noreply, State}; + +handle_info(Info, State = #state{}) -> + lager:debug("[iot_mqtt_share_subscriber] get info: ~p", [Info]), + {noreply, State}. + +%% @private +%% @doc This function is called by a gen_server 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 +%% with Reason. The return value is ignored. +-spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()), + State :: #state{}) -> term()). +terminate(Reason, _State = #state{conn_pid = ConnPid}) when is_pid(ConnPid) -> + {ok, _Props, _ReasonCode} = emqtt:unsubscribe(ConnPid, #{}, <<"hello">>), + ok = emqtt:disconnect(ConnPid), + ok = emqtt:stop(ConnPid), + lager:debug("[iot_host] terminate with reason: ~p", [Reason]), + ok; +terminate(Reason, _State) -> + lager:debug("[iot_host] terminate with reason: ~p", [Reason]), + ok. + +%% @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}. + +%%%=================================================================== +%%% Internal functions +%%%=================================================================== \ No newline at end of file diff --git a/apps/iot/src/iot_sup.erl b/apps/iot/src/iot_sup.erl index 87d955e..352a011 100644 --- a/apps/iot/src/iot_sup.erl +++ b/apps/iot/src/iot_sup.erl @@ -56,12 +56,12 @@ init([]) -> }, #{ - id => 'iot_emqtt_client', - start => {'iot_emqtt_client', start_link, []}, + id => 'iot_mqtt_share_subscriber', + start => {'iot_mqtt_share_subscriber', start_link, []}, restart => permanent, shutdown => 2000, type => worker, - modules => ['iot_emqtt_client'] + modules => ['iot_mqtt_share_subscriber'] } ], {ok, {SupFlags, ChildSpecs}}.