This commit is contained in:
anlicheng 2025-05-07 22:14:29 +08:00
parent 04254def4b
commit 37cb8d89af
3 changed files with 1 additions and 70 deletions

View File

@ -1,60 +0,0 @@
%%%-------------------------------------------------------------------
%%% @author aresei
%%% @copyright (C) 2023, <COMPANY>
%%% @doc
%%% @end
%%%-------------------------------------------------------------------
-module(iot_device_sup).
-include("iot.hrl").
-behaviour(supervisor).
-export([start_link/0, init/1, delete_device/1, ensured_device_started/1]).
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
init([]) ->
%% devices
{ok, DeviceInfos} = device_bo:get_all_devices(),
Specs = lists:map(fun child_spec/1, DeviceInfos),
{ok, {#{strategy => one_for_one, intensity => 1000, period => 3600}, Specs}}.
-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) ->
Id = iot_device:get_name(UUID),
ok = supervisor:terminate_child(?MODULE, Id),
supervisor:delete_child(?MODULE, Id).
child_spec(DeviceUUID) when is_binary(DeviceUUID) ->
Name = iot_device:get_name(DeviceUUID),
#{id => Name,
start => {iot_device, start_link, [Name, DeviceUUID]},
restart => permanent,
shutdown => 2000,
type => worker,
modules => ['iot_device']};
child_spec(DeviceInfo = #{<<"device_uuid">> := DeviceUUID}) when is_binary(DeviceUUID) ->
Name = iot_device:get_name(DeviceUUID),
#{id => Name,
start => {iot_device, start_link, [Name, DeviceInfo]},
restart => permanent,
shutdown => 2000,
type => worker,
modules => ['iot_device']}.

View File

@ -325,7 +325,7 @@ handle_event({call, From}, {activate_device, DeviceUUID, Auth}, _, State = #stat
%% json格式然后再处理, host进程里面处理, props
handle_event(cast, {handle, {data, Data}}, ?STATE_ACTIVATED, State = #state{has_session = true}) ->
handle_data(Data, State),
%handle_data(Data, State),
{keep_state, State};
%% ping的数据是通过aes加密后的

View File

@ -28,15 +28,6 @@ start_link() ->
init([]) ->
SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600},
Specs = [
#{
id => 'iot_device_sup',
start => {'iot_device_sup', start_link, []},
restart => permanent,
shutdown => 2000,
type => supervisor,
modules => ['iot_device_sup']
},
#{
id => 'iot_host_sup',
start => {'iot_host_sup', start_link, []},