fix device

This commit is contained in:
anlicheng 2023-08-16 16:48:59 +08:00
parent 0bb6883ac6
commit d4d1c06086
4 changed files with 40 additions and 13 deletions

View File

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

View File

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

View File

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

View File

@ -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, []},