// ReSharper disable MemberCanBePrivate.Global using System; using System.Collections.Generic; using RGB.NET.Core; namespace RGB.NET.Devices.WS281X { /// /// /// Represents a device provider responsible for WS2812B- and WS2811-Led-devices. /// // ReSharper disable once InconsistentNaming public class WS281XDeviceProvider : IRGBDeviceProvider { #region Properties & Fields private static WS281XDeviceProvider _instance; /// /// Gets the singleton instance. /// public static WS281XDeviceProvider Instance => _instance ?? new WS281XDeviceProvider(); /// public bool IsInitialized { get; private set; } /// public IEnumerable Devices { get; private set; } /// public bool HasExclusiveAccess => false; /// /// Gets a list of all defined device-definitions. /// // ReSharper disable once CollectionNeverUpdated.Global // ReSharper disable once ReturnTypeCanBeEnumerable.Global public List DeviceDefinitions { get; } = new List(); #endregion #region Constructors /// /// Initializes a new instance of the class. /// /// Thrown if this constructor is called even if there is already an instance of this class. public WS281XDeviceProvider() { if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(WS281XDeviceProvider)}"); _instance = this; } #endregion #region Methods /// /// Adds the given to this device-provider. /// /// The to add. // ReSharper disable once UnusedMember.Global public void AddDeviceDefinition(IWS281XDeviceDefinition deviceDefinition) => DeviceDefinitions.Add(deviceDefinition); /// public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.Unknown, bool exclusiveAccessIfPossible = false, bool throwExceptions = false) { IsInitialized = false; try { List devices = new List(); foreach (IWS281XDeviceDefinition deviceDefinition in DeviceDefinitions) { try { devices.AddRange(deviceDefinition.CreateDevices()); } catch { if (throwExceptions) throw; } } Devices = devices; IsInitialized = true; } catch { if (throwExceptions) throw; return false; } return true; } /// public void ResetDevices() { } /// public void Dispose() { if (IsInitialized) foreach (IRGBDevice device in Devices) if (device is IDisposable disposable) disposable.Dispose(); } #endregion } }