fix device

This commit is contained in:
anlicheng 2023-08-17 12:44:51 +08:00
parent 314b089024
commit 97571c702d
2 changed files with 39 additions and 45 deletions

View File

@ -9,7 +9,7 @@
-behaviour(supervisor).
-export([start_link/0, init/1, delete_device/1, start_device/1]).
-export([start_link/0, init/1, delete_device/1, ensured_device_started/1]).
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
@ -17,8 +17,10 @@ start_link() ->
init([]) ->
{ok, {#{strategy => one_for_one, intensity => 1000, period => 3600}, []}}.
-spec start_device(UUID :: binary()) -> {ok, Pid :: pid()} | {error, Reason :: any()}.
start_device(DeviceUUID) when is_binary(DeviceUUID) ->
-spec ensured_device_started(UUID :: binary()) -> {ok, Pid :: pid()} | {error, Reason :: any()}.
ensured_device_started(DeviceUUID) when is_binary(DeviceUUID) ->
case iot_device:get_pid(DeviceUUID) of
undefined ->
case supervisor:start_child(?MODULE, child_spec(DeviceUUID)) of
{ok, Pid} when is_pid(Pid) ->
{ok, Pid};
@ -26,6 +28,9 @@ start_device(DeviceUUID) when is_binary(DeviceUUID) ->
{ok, Pid};
{error, Error} ->
{error, Error}
end;
Pid when is_pid(Pid) ->
{ok, Pid}
end.
delete_device(UUID) when is_binary(UUID) ->

View File

@ -154,7 +154,7 @@ init([UUID]) ->
%% devicedevice的状态为离线状态
{ok, Devices} = device_bo:get_host_devices(HostId),
lists:foreach(fun(DeviceUUID) ->
case iot_device_sup:start_device(DeviceUUID) of
case iot_device_sup:ensured_device_started(DeviceUUID) of
{ok, DevicePid} ->
iot_device:change_status(DevicePid, ?DEVICE_OFFLINE);
{error, Reason} ->
@ -272,25 +272,20 @@ handle_event({call, From}, {create_session, PubKey}, StateName, State = #state{h
%%
handle_event({call, From}, {reload_device, DeviceUUID}, StateName, State) ->
DeviceStatus = case StateName =:= ?STATE_SESSION of
true -> ?DEVICE_ONLINE;
false -> ?DEVICE_OFFLINE
end,
case iot_device:get_pid(DeviceUUID) of
undefined ->
case iot_device_sup:start_device(DeviceUUID) of
case iot_device_sup:ensured_device_started(DeviceUUID) of
{ok, DevicePid} ->
iot_device:change_status(DevicePid, DeviceStatus),
iot_device:reload(DevicePid),
case StateName =:= ?STATE_SESSION of
true ->
iot_device:change_status(DevicePid, ?DEVICE_ONLINE);
false ->
iot_device:change_status(DevicePid, ?DEVICE_OFFLINE)
end,
{keep_state, State, [{reply, From, ok}]};
{error, Reason} ->
{keep_state, State, [{reply, From, {error, Reason}}]}
end;
DevicePid when is_pid(DevicePid) ->
iot_device:reload(DevicePid),
iot_device:change_status(DevicePid, DeviceStatus),
{keep_state, State, [{reply, From, ok}]}
end;
handle_event({call, From}, {delete_device, DeviceUUID}, _, State) ->
case iot_device:get_pid(DeviceUUID) of
@ -302,26 +297,20 @@ handle_event({call, From}, {delete_device, DeviceUUID}, _, State) ->
{keep_state, State, [{reply, From, ok}]};
handle_event({call, From}, {activate_device, DeviceUUID, Auth}, StateName, State) ->
DeviceStatus = case StateName =:= ?STATE_SESSION of
true -> ?DEVICE_ONLINE;
false -> ?DEVICE_OFFLINE
case iot_device_sup:ensured_device_started(DeviceUUID) of
{ok, DevicePid} ->
iot_device:auth(DevicePid, Auth),
case StateName =:= ?STATE_SESSION of
true ->
iot_device:change_status(DevicePid, ?DEVICE_ONLINE);
false ->
iot_device:change_status(DevicePid, ?DEVICE_OFFLINE)
end,
case iot_device:get_pid(DeviceUUID) of
undefined ->
case iot_device_sup:start_device(DeviceUUID) of
{ok, Pid} ->
iot_device:auth(Pid, Auth),
iot_device:change_status(Pid, DeviceStatus),
{keep_state, State, [{reply, From, ok}]};
{error, Reason} ->
{keep_state, State, [{reply, From, {error, Reason}}]}
end;
DevicePid when is_pid(DevicePid) ->
iot_device:auth(DevicePid, Auth),
iot_device:change_status(DevicePid, DeviceStatus),
{keep_state, State, [{reply, From, ok}]}
end;
%% json格式然后再处理, host进程里面处理, props
handle_event(cast, {handle, {data, Data}}, ?STATE_SESSION, State = #state{aes = AES}) ->