Streamlined device loading/handling in device providers

This commit is contained in:
DarthAffe committed 2021-03-04 23:33:00 +01:00
1 parent f6f3e9185c
commit 2a9a43683c
81 files changed
+867 -1864

No files matched your search

@@ -1,5 +1,4 @@
using System.Collections.Generic;
using RGB.NET.Core;
using RGB.NET.Core;
namespace RGB.NET.Devices.SteelSeries
{
@@ -7,7 +6,5 @@ namespace RGB.NET.Devices.SteelSeries
/// Represents a steelseries RGB-device.
/// </summary>
internal interface ISteelSeriesRGBDevice : IRGBDevice
{
void Initialize(UpdateQueue updateQueue, Dictionary<LedId, SteelSeriesLedId> ledMapping);
}
{ }
}
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using RGB.NET.Core;
namespace RGB.NET.Devices.SteelSeries
@@ -13,19 +12,7 @@ namespace RGB.NET.Devices.SteelSeries
{
#region Properties & Fields
private Dictionary<LedId, SteelSeriesLedId> _ledMapping = new();
/// <inheritdoc />
/// <summary>
/// Gets information about the <see cref="SteelSeriesRGBDevice" />.
/// </summary>
public override SteelSeriesRGBDeviceInfo DeviceInfo { get; }
/// <summary>
/// Gets or sets the update queue performing updates for this device.
/// </summary>
// ReSharper disable once MemberCanBePrivate.Global
protected UpdateQueue? UpdateQueue { get; set; }
private readonly Dictionary<LedId, SteelSeriesLedId> _ledMapping;
#endregion
@@ -35,34 +22,30 @@ namespace RGB.NET.Devices.SteelSeries
/// Initializes a new instance of the <see cref="SteelSeriesRGBDevice"/> class.
/// </summary>
/// <param name="info">The generic information provided by SteelSeries for the device.</param>
internal SteelSeriesRGBDevice(SteelSeriesRGBDeviceInfo info)
internal SteelSeriesRGBDevice(SteelSeriesRGBDeviceInfo info, string apiName, Dictionary<LedId, SteelSeriesLedId> ledMapping, IDeviceUpdateTrigger updateTrigger)
: base(info, new SteelSeriesDeviceUpdateQueue(updateTrigger, apiName))
{
this.DeviceInfo = info;
this._ledMapping = ledMapping;
InitializeLayout();
}
#endregion
#region Methods
/// <summary>
/// Initializes the device.
/// </summary>
void ISteelSeriesRGBDevice.Initialize(UpdateQueue updateQueue, Dictionary<LedId, SteelSeriesLedId> ledMapping)
private void InitializeLayout()
{
_ledMapping = ledMapping;
int counter = 0;
foreach (KeyValuePair<LedId, SteelSeriesLedId> mapping in ledMapping)
foreach (KeyValuePair<LedId, SteelSeriesLedId> mapping in _ledMapping)
AddLed(mapping.Key, new Point((counter++) * 10, 0), new Size(10, 10));
UpdateQueue = updateQueue;
}
/// <inheritdoc />
protected override object GetLedCustomData(LedId ledId) => _ledMapping[ledId];
/// <inheritdoc />
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue?.SetData(GetUpdateData(ledsToUpdate));
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate));
/// <inheritdoc />
public override void Dispose()
@@ -1,7 +1,5 @@
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;
@@ -12,7 +10,7 @@ namespace RGB.NET.Devices.SteelSeries
/// <summary>
/// Represents a device provider responsible for SteelSeries- devices.
/// </summary>
public class SteelSeriesDeviceProvider : IRGBDeviceProvider
public class SteelSeriesDeviceProvider : AbstractRGBDeviceProvider
{
#region Properties & Fields
@@ -22,20 +20,6 @@ namespace RGB.NET.Devices.SteelSeries
/// </summary>
public static SteelSeriesDeviceProvider Instance => _instance ?? new SteelSeriesDeviceProvider();
/// <inheritdoc />
/// <summary>
/// Indicates if the SDK is initialized and ready to use.
/// </summary>
public bool IsInitialized { get; private set; }
/// <inheritdoc />
public IEnumerable<IRGBDevice> Devices { get; private set; } = Enumerable.Empty<IRGBDevice>();
/// <summary>
/// The <see cref="SteelSeriesDeviceUpdateTrigger"/> used to trigger the updates for SteelSeries devices.
/// </summary>
public SteelSeriesDeviceUpdateTrigger UpdateTrigger { get; }
#endregion
#region Constructors
@@ -48,67 +32,41 @@ namespace RGB.NET.Devices.SteelSeries
{
if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(SteelSeriesDeviceProvider)}");
_instance = this;
UpdateTrigger = new SteelSeriesDeviceUpdateTrigger();
}
#endregion
#region Methods
/// <inheritdoc />
public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool throwExceptions = false)
protected override void InitializeSDK()
{
try
{
IsInitialized = false;
UpdateTrigger.Stop();
if (!SteelSeriesSDK.IsInitialized)
SteelSeriesSDK.Initialize();
IList<IRGBDevice> devices = new List<IRGBDevice>();
DeviceChecker.LoadDeviceList(loadFilter);
try
{
foreach ((string model, RGBDeviceType deviceType, int _, SteelSeriesDeviceType steelSeriesDeviceType, Dictionary<LedId, SteelSeriesLedId> 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<IRGBDevice>(devices);
IsInitialized = true;
}
catch
{
IsInitialized = false;
if (throwExceptions) throw;
return false;
}
return true;
if (!SteelSeriesSDK.IsInitialized)
SteelSeriesSDK.Initialize();
}
/// <inheritdoc />
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<IRGBDevice>();
protected override IEnumerable<IRGBDevice> GetLoadedDevices(RGBDeviceType loadFilter)
{
DeviceChecker.LoadDeviceList(loadFilter);
return base.GetLoadedDevices(loadFilter);
}
protected override IEnumerable<IRGBDevice> LoadDevices()
{
foreach ((string model, RGBDeviceType deviceType, int _, SteelSeriesDeviceType steelSeriesDeviceType, Dictionary<LedId, SteelSeriesLedId> ledMapping) in DeviceChecker.ConnectedDevices)
{
string? apiName = steelSeriesDeviceType.GetAPIName();
if (apiName == null)
Throw(new RGBDeviceException($"Missing API-name for device {model}"));
else
yield return new SteelSeriesRGBDevice(new SteelSeriesRGBDeviceInfo(deviceType, model, steelSeriesDeviceType), apiName, ledMapping, GetUpdateTrigger());
}
}
/// <inheritdoc />
public override void Dispose()
{
base.Dispose();
try { SteelSeriesSDK.Dispose(); }
catch { /* shit happens */ }