This commit is contained in:
anlicheng 2023-06-15 21:36:14 +08:00
parent 98501ad6c5
commit 0425030730

View File

@ -141,6 +141,8 @@ init([UUID]) ->
%% %%
erlang:start_timer(?TICKER_INTERVAL, self(), ping_ticker), erlang:start_timer(?TICKER_INTERVAL, self(), ping_ticker),
Aes = list_to_binary(iot_util:rand_bytes(32)), 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}}; {ok, #state{host_id = HostId, uuid = UUID, aes = Aes, is_activated = (Status /= ?HOST_STATUS_INACTIVE), status = Status, has_session = false}};
undefined -> undefined ->
@ -227,6 +229,18 @@ handle_call(Info, _From, State) ->
{noreply, NewState :: #state{}} | {noreply, NewState :: #state{}} |
{noreply, NewState :: #state{}, timeout() | hibernate} | {noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #state{}}). {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) -> handle_cast({handle, Payload}, State) ->
%% %%
NState = handle_message(Payload, State), NState = handle_message(Payload, State),
@ -396,6 +410,7 @@ handle_message(Msg = #{<<"code">> := _Code, <<"assoc">> := Assoc}, State = #stat
end; end;
handle_message(Message, State = #state{uuid = UUID, has_session = HasSession}) -> 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]), lager:warning("[iot_host] host_id uuid: ~p, get a unknown message: ~p, session: ~p", [UUID, Message, HasSession]),
State. State.