From d4d1c060867d79b60a4bb8fd12d85e3abf43002a Mon Sep 17 00:00:00 2001 From: anlicheng Date: Wed, 16 Aug 2023 16:48:59 +0800 Subject: [PATCH] fix device --- apps/iot/src/http_handler/device_handler.erl | 2 +- apps/iot/src/iot_host.erl | 3 ++ apps/iot/src/iot_host_monitor.erl | 39 ++++++++++++++------ apps/iot/src/iot_sup.erl | 9 +++++ 4 files changed, 40 insertions(+), 13 deletions(-) diff --git a/apps/iot/src/http_handler/device_handler.erl b/apps/iot/src/http_handler/device_handler.erl index 4d395b3..8e19100 100644 --- a/apps/iot/src/http_handler/device_handler.erl +++ b/apps/iot/src/http_handler/device_handler.erl @@ -56,7 +56,7 @@ handle_request("POST", "/device/activate", _, #{<<"device_uuid">> := DeviceUUID, lager:debug("[device_handler] reload device: ~p, get error: ~p", [DeviceUUID, Reason]), {ok, 200, iot_util:json_error(404, <<"activate device failed">>)} end; - {ok, DevicePid} when is_pid(DevicePid) -> + DevicePid when is_pid(DevicePid) -> iot_device:auth(DevicePid, Auth), {ok, 200, iot_util:json_data(<<"success">>)} end; diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index 3ffc6b6..1a24843 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -124,6 +124,9 @@ init([UUID]) -> case host_bo:get_host_by_uuid(UUID) of {ok, #{<<"authorize_status">> := AuthorizeStatus, <<"id">> := HostId}} -> + %% 注册监听事件 + iot_host_monitor:register(self(), HostId), + Aes = list_to_binary(iot_util:rand_bytes(32)), StateName = case AuthorizeStatus =:= ?HOST_AUTHED of false -> denied; diff --git a/apps/iot/src/iot_host_monitor.erl b/apps/iot/src/iot_host_monitor.erl index e20432f..858c5e7 100644 --- a/apps/iot/src/iot_host_monitor.erl +++ b/apps/iot/src/iot_host_monitor.erl @@ -13,7 +13,7 @@ %% API -export([start_link/0]). --export([register/2, unregister/1]). +-export([register/2]). %% gen_server callbacks -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). @@ -21,18 +21,15 @@ -define(SERVER, ?MODULE). -record(state, { - + register_map = #{} }). %%%=================================================================== %%% API %%%=================================================================== -register(HostPid, HostUUID) when is_pid(HostPid), is_binary(HostUUID) -> - gen_server:call(?MODULE, {register, HostPid, HostUUID}). - -unregister(HostPid) when is_pid(HostPid) -> - gen_server:call(?MODULE, {unregister, HostPid}). +register(HostPid, HostId) when is_pid(HostPid), is_integer(HostId) -> + gen_server:call(?MODULE, {register, HostPid, HostId}). %% @doc Spawns the server and registers the local name (unique) -spec(start_link() -> @@ -62,8 +59,9 @@ init([]) -> {noreply, NewState :: #state{}, timeout() | hibernate} | {stop, Reason :: term(), Reply :: term(), NewState :: #state{}} | {stop, Reason :: term(), NewState :: #state{}}). -handle_call({register, HostPid, HostUUID}, _From, State = #state{}) -> - {reply, ok, State}. +handle_call({register, HostPid, HostId}, _From, State = #state{register_map = RegisterMap}) -> + erlang:monitor(process, HostPid), + {reply, ok, State#state{register_map = maps:put(HostPid, HostId, RegisterMap)}}. %% @private %% @doc Handling cast messages @@ -80,8 +78,25 @@ handle_cast(_Request, State = #state{}) -> {noreply, NewState :: #state{}} | {noreply, NewState :: #state{}, timeout() | hibernate} | {stop, Reason :: term(), NewState :: #state{}}). -handle_info(_Info, State = #state{}) -> - {noreply, State}. +%% 当websocket断开的时候,则设置主机状态为下线状态; 主机的状态需要转换 +handle_info({'DOWN', _Ref, process, HostPid, Reason}, State = #state{register_map = RegisterMap}) -> + lager:warning("[iot_host_monitor] host: ~p, down with reason: ~p", [HostPid, Reason]), + case maps:take(HostPid, RegisterMap) of + error -> + {noreply, State}; + {HostId, NRegisterMap} -> + case device_bo:get_host_devices(HostId) of + {ok, Devices} -> + DevicesUUIDs = lists:map(fun(#{<<"device_uuid">> := DeviceUUID}) -> DeviceUUID end, Devices), + lists:foreach(fun(DeviceUUID) -> + DevicePid = iot_device:get_pid(DeviceUUID), + iot_device:change_status(DevicePid, 0) + end, DevicesUUIDs); + {error, _} -> + ok + end, + {noreply, State#state{register_map = NRegisterMap}} + end. %% @private %% @doc This function is called by a gen_server when it is about to @@ -103,4 +118,4 @@ code_change(_OldVsn, State = #state{}, _Extra) -> %%%=================================================================== %%% Internal functions -%%%=================================================================== +%%%=================================================================== \ No newline at end of file diff --git a/apps/iot/src/iot_sup.erl b/apps/iot/src/iot_sup.erl index 96707ee..e11c993 100644 --- a/apps/iot/src/iot_sup.erl +++ b/apps/iot/src/iot_sup.erl @@ -37,6 +37,15 @@ init([]) -> modules => ['iot_endpoint_monitor'] }, + #{ + id => iot_host_monitor, + start => {'iot_host_monitor', start_link, []}, + restart => permanent, + shutdown => 2000, + type => supervisor, + modules => ['iot_host_monitor'] + }, + #{ id => 'iot_endpoint_sup', start => {'iot_endpoint_sup', start_link, []},