From 0d5d5aa8529c9b60ee079cd51d656d26dd111940 Mon Sep 17 00:00:00 2001 From: anlicheng Date: Thu, 15 Jun 2023 21:44:28 +0800 Subject: [PATCH] fix host mocker --- apps/iot/src/iot_host.erl | 2 -- apps/iot/src/iot_sup.erl | 18 ++++++++-------- apps/iot/src/mocker/host_mocker.erl | 32 ++++++++++++++++------------- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index 68cba50..bc5079c 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -230,9 +230,7 @@ handle_call(Info, _From, State) -> {noreply, NewState :: #state{}, timeout() | hibernate} | {stop, Reason :: term(), NewState :: #state{}}). handle_cast(need_auth, State = #state{uuid = UUID}) -> - lager:debug("[iot_host] host_id uuid: ~p, create_session", [UUID]), Reply = jiffy:encode(#{<<"a">> => false, <<"aes">> => <<"">>}, [force_utf8]), - {ok, Ref} = iot_mqtt_publisher:publish(downstream_topic(UUID), <<8:8, Reply/binary>>, 1), receive {ok, Ref, PacketId} -> diff --git a/apps/iot/src/iot_sup.erl b/apps/iot/src/iot_sup.erl index f048218..4d261ee 100644 --- a/apps/iot/src/iot_sup.erl +++ b/apps/iot/src/iot_sup.erl @@ -28,14 +28,7 @@ start_link() -> init([]) -> SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600}, ChildSpecs = [ - #{ - id => 'iot_host_sup', - start => {'iot_host_sup', start_link, []}, - restart => permanent, - shutdown => 2000, - type => supervisor, - modules => ['iot_host_sup'] - }, + #{ id => 'iot_mqtt_subscriber', @@ -62,6 +55,15 @@ init([]) -> shutdown => 2000, type => worker, modules => ['iot_mqtt_publisher'] + }, + + #{ + id => 'iot_host_sup', + start => {'iot_host_sup', start_link, []}, + restart => permanent, + shutdown => 2000, + type => supervisor, + modules => ['iot_host_sup'] } ], {ok, {SupFlags, pools() ++ ChildSpecs}}. diff --git a/apps/iot/src/mocker/host_mocker.erl b/apps/iot/src/mocker/host_mocker.erl index 2e6ab90..ab4e6bb 100644 --- a/apps/iot/src/mocker/host_mocker.erl +++ b/apps/iot/src/mocker/host_mocker.erl @@ -212,22 +212,26 @@ handle_info({publish, #{payload := Payload, qos := Qos, topic := FromTopic}}, {noreply, State}; - #{<<"auth">> := false, <<"reply">> := #{<<"topic">> := Topic, <<"assoc">> := Assoc}} -> - Msg = jiffy:encode(#{ - <<"code">> => 1, - <<"message">> => "", - <<"assoc">> => Assoc - }, [force_utf8]), + #{<<"auth">> := false, <<"reply">> := #{<<"topic">> := Topic, <<"assoc">> := Assoc}} -> + Msg = jiffy:encode(#{ + <<"code">> => 1, + <<"message">> => "", + <<"assoc">> => Assoc + }, [force_utf8]), - {ok, Ref} = iot_mqtt_publisher:publish(Topic, Msg, 1), - receive - {ok, Ref, PacketId} -> - lager:debug("[host_mocker] send reply success, packet_id: ~p", [PacketId]); - {error, Reason} -> - lager:debug("[host_mocker] send reply failed, reason: ~p", [Reason]) - end, + {ok, Ref} = iot_mqtt_publisher:publish(Topic, Msg, 1), + receive + {ok, Ref, PacketId} -> + lager:debug("[host_mocker] send reply success, packet_id: ~p", [PacketId]); + {error, Reason} -> + lager:debug("[host_mocker] send reply failed, reason: ~p", [Reason]) + end, - {noreply, State#state{aes = <<>>}} + {noreply, State#state{aes = <<>>}}; + #{<<"auth">> := false} -> + self() ! create_session, + + {noreply, State#state{aes = <<>>}} end; %% 处理其他指令