This commit is contained in:
安礼成 2023-04-17 15:53:34 +08:00
parent a4c8a0554a
commit 4956e46035
4 changed files with 14 additions and 9 deletions

View File

@ -77,6 +77,8 @@ init([Host = #host{host_id = HostId}]) ->
{username, Username}, {username, Username},
{password, Password}, {password, Password},
{keepalive, Keepalive}, {keepalive, Keepalive},
{auto_ack, false},
{proto_ver, v5},
{retry_interval, RetryInterval} {retry_interval, RetryInterval}
], ],
@ -107,7 +109,7 @@ init([Host = #host{host_id = HostId}]) ->
{stop, Reason :: 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, Message}, _From, State = #state{emqx_pid = ConnPid, host = #host{host_id = HostId}}) ->
Topic = <<"/host/", HostId/binary, "/downstream">>, Topic = <<"/host/", HostId/binary, "/downstream">>,
Result = emqtt:publish(ConnPid, Topic, #{}, Message, [{qos, 2}]), Result = emqtt:publish(ConnPid, Topic, #{}, Message, [{qos, 2}, {retain, true}]),
{reply, Result, State}. {reply, Result, State}.
%% @private %% @private
@ -161,10 +163,11 @@ handle_info(Info, State = #state{host = #host{host_id = HostId}}) ->
%% 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 :: (normal | shutdown | {shutdown, term()} | term()),
State :: #state{}) -> term()). State :: #state{}) -> term()).
terminate(_Reason, _State = #state{emqx_pid = ConnPid}) when is_pid(ConnPid) -> terminate(Reason, _State = #state{emqx_pid = ConnPid}) when is_pid(ConnPid) ->
{ok, _Props, _ReasonCode} = emqtt:unsubscribe(ConnPid, #{}, <<"hello">>), {ok, _Props, _ReasonCode} = emqtt:unsubscribe(ConnPid, #{}, <<"hello">>),
ok = emqtt:disconnect(ConnPid), ok = emqtt:disconnect(ConnPid),
ok = emqtt:stop(ConnPid), ok = emqtt:stop(ConnPid),
lager:debug("[iot_host] terminate with reason: ~p", [Reason]),
ok; ok;
terminate(Reason, _State) -> terminate(Reason, _State) ->
lager:debug("[iot_host] terminate with reason: ~p", [Reason]), lager:debug("[iot_host] terminate with reason: ~p", [Reason]),

View File

@ -37,7 +37,7 @@ publish(Pid, Message) when is_pid(Pid), is_binary(Message) ->
-spec(start_link(HostId :: binary()) -> -spec(start_link(HostId :: binary()) ->
{ok, Pid :: pid()} | ignore | {error, Reason :: term()}). {ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
start_link(HostId) when is_binary(HostId) -> start_link(HostId) when is_binary(HostId) ->
gen_server:start_link(?MODULE, [HostId], []). gen_server:start_link({local, ?MODULE}, ?MODULE, [HostId], []).
%%%=================================================================== %%%===================================================================
%%% gen_server callbacks %%% gen_server callbacks
@ -65,6 +65,8 @@ init([HostId]) ->
{username, Username}, {username, Username},
{password, Password}, {password, Password},
{keepalive, Keepalive}, {keepalive, Keepalive},
{proto_ver, v5},
{auto_ack, true},
{retry_interval, RetryInterval} {retry_interval, RetryInterval}
], ],
@ -130,17 +132,17 @@ handle_info({disconnect, ReasonCode, Properties}, State = #state{host_id = HostI
lager:debug("[iot_host_mocker] host: ~p, Recv a DISONNECT packet - ReasonCode: ~p, Properties: ~p", [HostId, ReasonCode, Properties]), lager:debug("[iot_host_mocker] host: ~p, Recv a DISONNECT packet - ReasonCode: ~p, Properties: ~p", [HostId, ReasonCode, Properties]),
{stop, disconnected, State}; {stop, disconnected, State};
%% qos为2的包 %% qos为2的包
handle_info({publish, Message = #{packet_id := PacketId, payload := Payload, qos := 2}}, State = #state{emqx_pid = ConnPid, host_id = HostId}) -> handle_info({publish, Message = #{packet_id := PacketId, payload := Payload, reason_code := ReasonCode, qos := 2}}, State = #state{emqx_pid = ConnPid, host_id = HostId}) ->
lager:debug("[iot_host_mocker] host: ~p, qos: 2, Recv a publish packet: ~p, payload: ~p", [HostId, Message, Payload]), lager:debug("[iot_host_mocker] host: ~p, qos: 2, Recv a publish packet: ~p, payload: ~p", [HostId, Message, Payload]),
%% %%
emqtt:pubrec(ConnPid, PacketId), % emqtt:pubrec(ConnPid, PacketId, ReasonCode),
{noreply, State}; {noreply, State};
handle_info({publish, Message = #{packet_id := PacketId, payload := Payload}}, State = #state{emqx_pid = _ConnPid, host_id = HostId}) -> handle_info({publish, Message = #{packet_id := PacketId, payload := Payload}}, State = #state{emqx_pid = ConnPid, host_id = HostId}) ->
lager:debug("[iot_host_mocker] host: ~p, Recv a publish packet: ~p, payload: ~p, packet_id: ~p", [HostId, Message, Payload, PacketId]), lager:debug("[iot_host_mocker] host: ~p, Recv a publish packet: ~p, payload: ~p, packet_id: ~p", [HostId, Message, Payload, PacketId]),
%% %%
% emqtt:puback(ConnPid, PacketId), % emqtt:pubrec(ConnPid, PacketId),
{noreply, State}; {noreply, State};
handle_info({puback, #{packet_id := PacketId, reason_code := ReasonCode}}, State = #state{host_id = HostId}) -> handle_info({puback, #{packet_id := PacketId, reason_code := ReasonCode}}, State = #state{host_id = HostId}) ->
lager:debug("[iot_host_mocker] host: ~p, receive puback packet_id: ~p, reason_code: ~p", [HostId, PacketId, ReasonCode]), lager:debug("[iot_host_mocker] host: ~p, receive puback packet_id: ~p, reason_code: ~p", [HostId, PacketId, ReasonCode]),

View File

@ -41,7 +41,7 @@ insert_hosts() ->
}, },
host_model:add_host(Host) host_model:add_host(Host)
end, lists:seq(1, 100)). end, lists:seq(1, 1)).
insert_services(HostId) -> insert_services(HostId) ->
lists:foreach(fun(Id0) -> lists:foreach(fun(Id0) ->

View File

@ -5,7 +5,7 @@
+K true +K true
+A30 +A30
-mnesia dir '"//usr/local/code/data/iot"' -mnesia dir '"/usr/local/code/data/iot"'
-mnesia dump_log_write_threshold 50000 -mnesia dump_log_write_threshold 50000
-mnesia dc_dump_limit 40 -mnesia dc_dump_limit 40