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). -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() -> start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []). supervisor:start_link({local, ?MODULE}, ?MODULE, []).
@ -17,15 +17,20 @@ start_link() ->
init([]) -> init([]) ->
{ok, {#{strategy => one_for_one, intensity => 1000, period => 3600}, []}}. {ok, {#{strategy => one_for_one, intensity => 1000, period => 3600}, []}}.
-spec start_device(UUID :: binary()) -> {ok, Pid :: pid()} | {error, Reason :: any()}. -spec ensured_device_started(UUID :: binary()) -> {ok, Pid :: pid()} | {error, Reason :: any()}.
start_device(DeviceUUID) when is_binary(DeviceUUID) -> ensured_device_started(DeviceUUID) when is_binary(DeviceUUID) ->
case supervisor:start_child(?MODULE, child_spec(DeviceUUID)) of case iot_device:get_pid(DeviceUUID) of
{ok, Pid} when is_pid(Pid) -> undefined ->
{ok, Pid}; case supervisor:start_child(?MODULE, child_spec(DeviceUUID)) of
{error, {'already_started', Pid}} when is_pid(Pid) -> {ok, Pid} when is_pid(Pid) ->
{ok, Pid}; {ok, Pid};
{error, Error} -> {error, {'already_started', Pid}} when is_pid(Pid) ->
{error, Error} {ok, Pid};
{error, Error} ->
{error, Error}
end;
Pid when is_pid(Pid) ->
{ok, Pid}
end. end.
delete_device(UUID) when is_binary(UUID) -> delete_device(UUID) when is_binary(UUID) ->

View File

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