fix activate

This commit is contained in:
anlicheng 2023-06-15 13:02:59 +08:00
parent bd418e8e6a
commit b5717b1f78
2 changed files with 16 additions and 10 deletions

View File

@ -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>>)}

View File

@ -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