fix device

This commit is contained in:
anlicheng 2023-08-16 15:58:35 +08:00
parent d9b94af0de
commit 5ddcc66f0d
3 changed files with 137 additions and 31 deletions

View File

@ -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.
mysql_pool:update_by(mysql_iot, <<"UPDATE device SET status = ? WHERE id = ? LIMIT 1">>, [Status, DeviceId]).

View File

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

View File

@ -0,0 +1,106 @@
%%%-------------------------------------------------------------------
%%% @author aresei
%%% @copyright (C) 2023, <COMPANY>
%%% @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
%%%===================================================================