diff --git a/apps/iot/src/iot_device_sup.erl b/apps/iot/src/iot_device_sup.erl index f6ecbd1..7f41d66 100644 --- a/apps/iot/src/iot_device_sup.erl +++ b/apps/iot/src/iot_device_sup.erl @@ -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,15 +17,20 @@ 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) -> - case supervisor:start_child(?MODULE, child_spec(DeviceUUID)) of - {ok, Pid} when is_pid(Pid) -> - {ok, Pid}; - {error, {'already_started', Pid}} when is_pid(Pid) -> - {ok, Pid}; - {error, Error} -> - {error, Error} +-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}; + {error, {'already_started', Pid}} when is_pid(Pid) -> + {ok, Pid}; + {error, Error} -> + {error, Error} + end; + Pid when is_pid(Pid) -> + {ok, Pid} end. delete_device(UUID) when is_binary(UUID) -> diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index b1e4782..fac5f36 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -154,7 +154,7 @@ init([UUID]) -> %% 启动主机相关的device,此时device的状态为离线状态 {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,24 +272,19 @@ 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 - {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) -> + case iot_device_sup:ensured_device_started(DeviceUUID) of + {ok, 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; 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}]}; handle_event({call, From}, {activate_device, DeviceUUID, Auth}, 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 - {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) -> + case iot_device_sup:ensured_device_started(DeviceUUID) of + {ok, DevicePid} -> iot_device:auth(DevicePid, Auth), - iot_device:change_status(DevicePid, DeviceStatus), - {keep_state, State, [{reply, From, ok}]} + 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; %% 需要将消息转换成json格式然后再处理, 需要在host进程里面处理, 数据带有props,服务端暂时未用到