diff --git a/apps/iot/src/database/device_bo.erl b/apps/iot/src/database/device_bo.erl index c41e705..eab0e5f 100644 --- a/apps/iot/src/database/device_bo.erl +++ b/apps/iot/src/database/device_bo.erl @@ -17,9 +17,14 @@ get_all_devices() -> mysql_pool:get_all(mysql_iot, <<"SELECT * FROM device WHERE device_uuid != ''">>). --spec get_host_devices(HostId :: integer()) -> {ok, Devices::list()} | {error, Reason::any()}. +-spec get_host_devices(HostId :: integer()) -> {ok, Devices :: [binary()]} | {error, Reason::any()}. get_host_devices(HostId) when is_integer(HostId) -> - mysql_pool:get_all(mysql_iot, <<"SELECT * FROM device WHERE host_id = ? AND device_uuid != ''">>, [HostId]). + case mysql_pool:get_all(mysql_iot, <<"SELECT device_uuid FROM device WHERE host_id = ? AND device_uuid != ''">>, [HostId]) of + {ok, Devices} -> + {ok, lists:map(fun(#{<<"device_uuid">> := DeviceUUID}) -> DeviceUUID end, Devices)}; + {error, Reason} -> + {error, Reason} + end. -spec get_device_by_uuid(DeviceUUID :: binary()) -> {ok, DeviceInfo :: map()} | undefined. get_device_by_uuid(DeviceUUID) when is_binary(DeviceUUID) -> diff --git a/apps/iot/src/iot_device_sup.erl b/apps/iot/src/iot_device_sup.erl index b863da3..f6ecbd1 100644 --- a/apps/iot/src/iot_device_sup.erl +++ b/apps/iot/src/iot_device_sup.erl @@ -32,14 +32,6 @@ delete_device(UUID) when is_binary(UUID) -> Id = iot_device:get_name(UUID), supervisor:terminate_child(?MODULE, Id). -child_spec(Device = #{<<"device_uuid">> := DeviceUUID}) when is_binary(DeviceUUID) -> - Name = iot_device:get_name(DeviceUUID), - #{id => Name, - start => {iot_device, start_link, [Name, Device]}, - restart => permanent, - shutdown => 2000, - type => worker, - modules => ['iot_device']}; child_spec(DeviceUUID) when is_binary(DeviceUUID) -> Name = iot_device:get_name(DeviceUUID), #{id => Name, diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index 4d55892..b1e4782 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -58,7 +58,7 @@ get_pid(UUID) when is_binary(UUID) -> get_name(UUID) when is_binary(UUID) -> binary_to_atom(<<"iot_host:", UUID/binary>>). --spec get_name(HostId :: integer()) -> atom(). +-spec get_alias_name(HostId :: integer()) -> atom(). get_alias_name(HostId0) when is_integer(HostId0) -> HostId = integer_to_binary(HostId0), binary_to_atom(<<"iot_host_id:", HostId/binary>>). @@ -153,8 +153,8 @@ init([UUID]) -> end, %% 启动主机相关的device,此时device的状态为离线状态 {ok, Devices} = device_bo:get_host_devices(HostId), - lists:foreach(fun(Device = #{<<"device_uuid">> := DeviceUUID}) -> - case iot_device_sup:start_device(Device) of + lists:foreach(fun(DeviceUUID) -> + case iot_device_sup:start_device(DeviceUUID) of {ok, DevicePid} -> iot_device:change_status(DevicePid, ?DEVICE_OFFLINE); {error, Reason} -> @@ -463,7 +463,7 @@ code_change(_OldVsn, StateName, State = #state{}, _Extra) -> change_devices_status(HostId, NewStatus) when is_integer(HostId), is_integer(NewStatus) -> {ok, Devices} = device_bo:get_host_devices(HostId), - lists:foreach(fun(#{<<"device_uuid">> := DeviceUUID}) -> + lists:foreach(fun(DeviceUUID) -> Pid = iot_device:get_pid(DeviceUUID), iot_device:change_status(Pid, NewStatus) end, Devices).