// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System;
using System.Collections.Generic;
using System.Threading;
using RGB.NET.Core;
using RGB.NET.Layout;
namespace RGB.NET.Devices.Debug;
///
///
/// Represents a device provider responsible for debug devices.
///
public sealed class DebugDeviceProvider : AbstractRGBDeviceProvider
{
#region Properties & Fields
// ReSharper disable once InconsistentNaming
private static readonly Lock _lock = new();
private static DebugDeviceProvider? _instance;
///
/// Gets the singleton instance.
///
public static DebugDeviceProvider Instance
{
get
{
lock (_lock)
return _instance ?? new DebugDeviceProvider();
}
}
private readonly List<(IDeviceLayout layout, Action>? updateLedsAction)> _fakeDeviceDefinitions = [];
#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 DebugDeviceProvider()
{
lock (_lock)
{
if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(DebugDeviceProvider)}");
_instance = this;
}
}
#endregion
#region Methods
///
/// Adds a new fake device definition.
///
/// The path of the layout file to be used.
/// A action emulating led-updates.
public void AddFakeDeviceDefinition(IDeviceLayout layout, Action>? updateLedsAction = null)
=> _fakeDeviceDefinitions.Add((layout, updateLedsAction));
///
/// Removes all previously added fake device definitions.
///
public void ClearFakeDeviceDefinitions() => _fakeDeviceDefinitions.Clear();
///
protected override void InitializeSDK() { }
///
protected override IEnumerable LoadDevices()
{
foreach ((IDeviceLayout layout, Action>? updateLedsAction) in _fakeDeviceDefinitions)
yield return new DebugRGBDevice(layout, updateLedsAction);
}
///
protected override void Dispose(bool disposing)
{
lock (_lock)
{
base.Dispose(disposing);
_fakeDeviceDefinitions.Clear();
_instance = null;
}
}
#endregion
}