From 5ddcc66f0d2b54b41304a26be4f4fac766a9e914 Mon Sep 17 00:00:00 2001 From: anlicheng Date: Wed, 16 Aug 2023 15:58:35 +0800 Subject: [PATCH] fix device --- apps/iot/src/database/device_bo.erl | 13 +-- apps/iot/src/http_handler/device_handler.erl | 49 +++++---- apps/iot/src/iot_host_monitor.erl | 106 +++++++++++++++++++ 3 files changed, 137 insertions(+), 31 deletions(-) create mode 100644 apps/iot/src/iot_host_monitor.erl diff --git a/apps/iot/src/database/device_bo.erl b/apps/iot/src/database/device_bo.erl index b7266b5..c41e705 100644 --- a/apps/iot/src/database/device_bo.erl +++ b/apps/iot/src/database/device_bo.erl @@ -11,7 +11,7 @@ -include("iot.hrl"). %% API --export([get_all_devices/0, get_host_devices/1, get_device_by_uuid/1, change_status/2, get_host_by_uuid/1]). +-export([get_all_devices/0, get_host_devices/1, get_device_by_uuid/1, change_status/2]). -spec get_all_devices() -> {ok, Devices :: [map()]} | {error, Reason :: any()}. get_all_devices() -> @@ -28,13 +28,4 @@ get_device_by_uuid(DeviceUUID) when is_binary(DeviceUUID) -> %% 修改主机的状态 -spec change_status(DeviceId :: integer(), Status :: integer()) -> {ok, AffectedRows :: integer()} | {error, Reason :: any()}. change_status(DeviceId, Status) when is_integer(DeviceId), is_integer(Status) -> - mysql_pool:update_by(mysql_iot, <<"UPDATE device SET status = ? WHERE id = ? LIMIT 1">>, [Status, DeviceId]). - --spec get_host_by_uuid(DeviceUUID :: binary()) -> undefined | {ok, HostInfo :: map()}. -get_host_by_uuid(DeviceUUID) when is_binary(DeviceUUID) -> - case get_device_by_uuid(DeviceUUID) of - undefined -> - undefined; - {ok, #{<<"host_id">> := HostId}} -> - host_bo:get_host_by_id(HostId) - end. \ No newline at end of file + mysql_pool:update_by(mysql_iot, <<"UPDATE device SET status = ? WHERE id = ? LIMIT 1">>, [Status, DeviceId]). \ No newline at end of file diff --git a/apps/iot/src/http_handler/device_handler.erl b/apps/iot/src/http_handler/device_handler.erl index 7861f80..5b3d99d 100644 --- a/apps/iot/src/http_handler/device_handler.erl +++ b/apps/iot/src/http_handler/device_handler.erl @@ -20,37 +20,46 @@ %% 重新加载对应的主机信息 handle_request("POST", "/device/reload", _, #{<<"device_uuid">> := DeviceUUID}) when is_binary(DeviceUUID) -> lager:debug("[device_handler] will reload device uuid: ~p", [DeviceUUID]), - case device_bo:get_host_by_uuid(DeviceUUID) of + case iot_device:get_pid(DeviceUUID) of undefined -> - {ok, 200, iot_util:json_error(404, <<"device not found, reload error">>)}; - {ok, #{<<"uuid">> := UUID}} -> - Pid = iot_host:get_pid(UUID), - ok = iot_host:reload_device(Pid, DeviceUUID), + case iot_device_sup:start_device(DeviceUUID) of + {ok, _} -> + {ok, 200, iot_util:json_data(<<"success">>)}; + {error, Reason} -> + lager:debug("[device_handler] reload device: ~p, get error: ~p", [DeviceUUID, Reason]), + {ok, 200, iot_util:json_error(404, <<"reload device failed">>)} + end; + {ok, DevicePid} when is_pid(DevicePid) -> + iot_device:reload(DevicePid), {ok, 200, iot_util:json_data(<<"success">>)} end; %% 删除对应的主机信息 handle_request("POST", "/device/delete", _, #{<<"device_uuid">> := DeviceUUID}) when is_binary(DeviceUUID) -> - case device_bo:get_host_by_uuid(DeviceUUID) of + case iot_device:get_pid(DeviceUUID) of undefined -> - ok; - {ok, #{<<"uuid">> := UUID}} -> - Pid = iot_host:get_pid(UUID), - ok = iot_host:delete_device(Pid, DeviceUUID) - end, - {ok, 200, iot_util:json_data(<<"success">>)}; + {ok, 200, iot_util:json_data(<<"success">>)}; + {ok, DevicePid} when is_pid(DevicePid) -> + iot_device_sup:delete_device(DeviceUUID), + {ok, 200, iot_util:json_data(<<"success">>)} + end; %% 处理主机的授权的激活 handle_request("POST", "/device/activate", _, #{<<"device_uuid">> := DeviceUUID, <<"auth">> := Auth}) when is_binary(DeviceUUID) -> - case device_bo:get_host_by_uuid(DeviceUUID) of + case iot_device:get_pid(DeviceUUID) of undefined -> - ok; - {ok, #{<<"uuid">> := UUID}} -> - Pid = iot_host:get_pid(UUID), - ok = iot_host:auth_device(Pid, DeviceUUID, Auth) - end, - {ok, 200, iot_util:json_data(<<"success">>)}; - + case iot_device_sup:start_device(DeviceUUID) of + {ok, Pid} -> + iot_device:auth(Pid, Auth), + {ok, 200, iot_util:json_data(<<"success">>)}; + {error, Reason} -> + 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) -> + iot_device:auth(DevicePid, Auth), + {ok, 200, iot_util:json_data(<<"success">>)} + end; handle_request(_, Path, _, _) -> Path1 = list_to_binary(Path), diff --git a/apps/iot/src/iot_host_monitor.erl b/apps/iot/src/iot_host_monitor.erl new file mode 100644 index 0000000..e20432f --- /dev/null +++ b/apps/iot/src/iot_host_monitor.erl @@ -0,0 +1,106 @@ +%%%------------------------------------------------------------------- +%%% @author aresei +%%% @copyright (C) 2023, +%%% @doc +%%% 观察主机,当主机下线时;主机下面的所有设备均需要处于离线状态 +%%% @end +%%% Created : 16. 8月 2023 14:47 +%%%------------------------------------------------------------------- +-module(iot_host_monitor). +-author("aresei"). + +-behaviour(gen_server). + +%% API +-export([start_link/0]). +-export([register/2, unregister/1]). + +%% gen_server callbacks +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). + +-define(SERVER, ?MODULE). + +-record(state, { + +}). + +%%%=================================================================== +%%% 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}). + +%% @doc Spawns the server and registers the local name (unique) +-spec(start_link() -> + {ok, Pid :: pid()} | ignore | {error, Reason :: term()}). +start_link() -> + gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). + +%%%=================================================================== +%%% gen_server callbacks +%%%=================================================================== + +%% @private +%% @doc Initializes the server +-spec(init(Args :: term()) -> + {ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} | + {stop, Reason :: term()} | ignore). +init([]) -> + {ok, #state{}}. + +%% @private +%% @doc Handling call messages +-spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()}, + State :: #state{}) -> + {reply, Reply :: term(), NewState :: #state{}} | + {reply, Reply :: term(), NewState :: #state{}, timeout() | hibernate} | + {noreply, NewState :: #state{}} | + {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}. + +%% @private +%% @doc Handling cast messages +-spec(handle_cast(Request :: term(), State :: #state{}) -> + {noreply, NewState :: #state{}} | + {noreply, NewState :: #state{}, timeout() | hibernate} | + {stop, Reason :: term(), NewState :: #state{}}). +handle_cast(_Request, State = #state{}) -> + {noreply, State}. + +%% @private +%% @doc Handling all non call/cast messages +-spec(handle_info(Info :: timeout() | term(), State :: #state{}) -> + {noreply, NewState :: #state{}} | + {noreply, NewState :: #state{}, timeout() | hibernate} | + {stop, Reason :: term(), NewState :: #state{}}). +handle_info(_Info, State = #state{}) -> + {noreply, State}. + +%% @private +%% @doc This function is called by a gen_server when it is about to +%% terminate. It should be the opposite of Module:init/1 and do any +%% necessary cleaning up. When it returns, the gen_server terminates +%% with Reason. The return value is ignored. +-spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()), + State :: #state{}) -> term()). +terminate(_Reason, _State = #state{}) -> + ok. + +%% @private +%% @doc Convert process state when code is changed +-spec(code_change(OldVsn :: term() | {down, term()}, State :: #state{}, + Extra :: term()) -> + {ok, NewState :: #state{}} | {error, Reason :: term()}). +code_change(_OldVsn, State = #state{}, _Extra) -> + {ok, State}. + +%%%=================================================================== +%%% Internal functions +%%%===================================================================