From 0425030730d7b39cb81b6060ba2deb3f50aebecb Mon Sep 17 00:00:00 2001 From: anlicheng Date: Thu, 15 Jun 2023 21:36:14 +0800 Subject: [PATCH] fix --- apps/iot/src/iot_host.erl | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index ad58207..68cba50 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -141,6 +141,8 @@ init([UUID]) -> %% 启动心跳定时器 erlang:start_timer(?TICKER_INTERVAL, self(), ping_ticker), Aes = list_to_binary(iot_util:rand_bytes(32)), + %% 告知主机端需要重新授权 + gen_server:cast(self(), need_auth), {ok, #state{host_id = HostId, uuid = UUID, aes = Aes, is_activated = (Status /= ?HOST_STATUS_INACTIVE), status = Status, has_session = false}}; undefined -> @@ -227,6 +229,18 @@ handle_call(Info, _From, State) -> {noreply, NewState :: #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} -> + lager:debug("[iot_host] host_id uuid: ~p, packet_id: ~p, publish need_auth reply success", [UUID, PacketId]) + after 5000 -> + lager:debug("[iot_host] host_id uuid: ~p, publish need_auth reply get error is: timeout", [UUID]) + end, + {noreply, State}; handle_cast({handle, Payload}, State) -> %% 处理消息 NState = handle_message(Payload, State), @@ -396,6 +410,7 @@ handle_message(Msg = #{<<"code">> := _Code, <<"assoc">> := Assoc}, State = #stat end; handle_message(Message, State = #state{uuid = UUID, has_session = HasSession}) -> + not HasSession andalso gen_server:cast(self(), need_auth), lager:warning("[iot_host] host_id uuid: ~p, get a unknown message: ~p, session: ~p", [UUID, Message, HasSession]), State.