From b5717b1f782b6ce1ce39af6e122358d2fc06ac54 Mon Sep 17 00:00:00 2001 From: anlicheng Date: Thu, 15 Jun 2023 13:02:59 +0800 Subject: [PATCH] fix activate --- .../src/http_handler/http_host_handler.erl | 2 +- apps/iot/src/iot_host.erl | 24 ++++++++++++------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/apps/iot/src/http_handler/http_host_handler.erl b/apps/iot/src/http_handler/http_host_handler.erl index 395e32f..3e9ea47 100644 --- a/apps/iot/src/http_handler/http_host_handler.erl +++ b/apps/iot/src/http_handler/http_host_handler.erl @@ -137,7 +137,7 @@ handle_request("POST", "/host/activate", _, #{<<"uuid">> := UUID, <<"auth">> := %% 等待主机返回确认消息 receive {host_reply, Assoc, #{<<"code">> := 1}} -> - ok = iot_host:activate(Pid), + ok = iot_host:activate(Pid, Auth), {ok, 200, iot_util:json_data(<<"success">>)}; {host_reply, Assoc, #{<<"code">> := 0, <<"message">> := Message}} when is_binary(Message) -> {ok, 200, iot_util:json_error(401, <<"操作失败: "/utf8, Message/binary>>)} diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index 894a1a7..fb5422d 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -17,7 +17,7 @@ -define(TICKER_INTERVAL, 5000 * 2). %% API --export([start_link/2, get_name/1, get_pid/1, handle/2, reload/1, activate/1]). +-export([start_link/2, get_name/1, get_pid/1, handle/2, reload/1, activate/2]). -export([get_metric/1, aes_encode/3, downstream_topic/1, upstream_topic/1, get_aes/1, make_assoc/1, rsa_encode/3]). %% gen_server callbacks @@ -100,10 +100,10 @@ get_aes(Pid) when is_pid(Pid) -> make_assoc(Pid) when is_pid(Pid) -> gen_server:call(Pid, {make_assoc, self()}). -%% 激活主机 --spec activate(Pid :: pid()) -> ok. -activate(Pid) when is_pid(Pid) -> - gen_server:call(Pid, activate). +%% 激活主机, true 表示激活; false表示关闭激活 +-spec activate(Pid :: pid(), Auth :: boolean()) -> ok. +activate(Pid, Auth) when is_pid(Pid), is_binary(Auth) -> + gen_server:call(Pid, {activate, Auth}). -spec get_metric(Pid :: pid()) -> {ok, MetricInfo :: map()}. get_metric(Pid) when is_pid(Pid) -> @@ -185,9 +185,15 @@ handle_call(reload, _From, State = #state{uuid = UUID}) -> end; % 处理主机的激活 -handle_call(activate, _From, State = #state{uuid = UUID, is_activated = IsActivated}) -> - lager:debug("[iot_host] host uuid: ~p, activated, before status is: ~p", [UUID, IsActivated]), - {reply, ok, State#state{is_activated = true}}; +handle_call({activate, Auth}, _From, State = #state{uuid = UUID, is_activated = IsActivated}) -> + lager:debug("[iot_host] host uuid: ~p, new activate status: ~p, before status is: ~p", [UUID, Auth, IsActivated]), + %% 关闭授权时,认为会话时关闭状态 + case Auth of + true -> + {reply, ok, State#state{is_activated = true}}; + false -> + {reply, ok, State#state{is_activated = false, has_session = false, pub_key = <<>>}} + end; %% 创建命令 handle_call({aes_encode, _, _}, _From, State = #state{has_session = false}) -> @@ -297,7 +303,7 @@ handle_message(#{<<"method">> := <<"create_session">>, <<"params">> := #{<<"pub_ receive {ok, Ref, PacketId} -> lager:debug("[iot_host] host_id uuid: ~p, packet_id: ~p, publish register reply success", [UUID, PacketId]), - State#state{pub_key = PubKey, aes = Aes, has_session = true} + State#state{pub_key = PubKey, has_session = true} after 10000 -> lager:debug("[iot_host] host_id uuid: ~p, publish register reply get error is: timeout", [UUID]), State