using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using RGB.NET.Core; using RGB.NET.Devices.SteelSeries.API; using RGB.NET.Devices.SteelSeries.HID; namespace RGB.NET.Devices.SteelSeries { /// /// /// Represents a device provider responsible for SteelSeries- devices. /// public class SteelSeriesDeviceProvider : IRGBDeviceProvider { #region Properties & Fields private static SteelSeriesDeviceProvider? _instance; /// /// Gets the singleton instance. /// public static SteelSeriesDeviceProvider Instance => _instance ?? new SteelSeriesDeviceProvider(); /// /// /// Indicates if the SDK is initialized and ready to use. /// public bool IsInitialized { get; private set; } /// public IEnumerable Devices { get; private set; } = Enumerable.Empty(); /// /// The used to trigger the updates for SteelSeries devices. /// public SteelSeriesDeviceUpdateTrigger UpdateTrigger { get; } #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 SteelSeriesDeviceProvider() { if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(SteelSeriesDeviceProvider)}"); _instance = this; UpdateTrigger = new SteelSeriesDeviceUpdateTrigger(); } #endregion #region Methods /// public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool throwExceptions = false) { try { IsInitialized = false; UpdateTrigger.Stop(); if (!SteelSeriesSDK.IsInitialized) SteelSeriesSDK.Initialize(); IList devices = new List(); DeviceChecker.LoadDeviceList(loadFilter); try { foreach ((string model, RGBDeviceType deviceType, int _, SteelSeriesDeviceType steelSeriesDeviceType, Dictionary ledMapping) in DeviceChecker.ConnectedDevices) { ISteelSeriesRGBDevice device = new SteelSeriesRGBDevice(new SteelSeriesRGBDeviceInfo(deviceType, model, steelSeriesDeviceType)); string apiName = steelSeriesDeviceType.GetAPIName() ?? throw new RGBDeviceException($"Missing API-name for device {model}"); SteelSeriesDeviceUpdateQueue updateQueue = new(UpdateTrigger, apiName); device.Initialize(updateQueue, ledMapping); devices.Add(device); } } catch { if (throwExceptions) throw; } UpdateTrigger.Start(); Devices = new ReadOnlyCollection(devices); IsInitialized = true; } catch { IsInitialized = false; if (throwExceptions) throw; return false; } return true; } /// public void Dispose() { try { UpdateTrigger.Dispose(); } catch { /* at least we tried */ } foreach (IRGBDevice device in Devices) try { device.Dispose(); } catch { /* at least we tried */ } Devices = Enumerable.Empty(); try { SteelSeriesSDK.Dispose(); } catch { /* shit happens */ } } #endregion } }