// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using RGB.NET.Core;
namespace RGB.NET.Devices.Debug
{
///
///
/// Represents a device provider responsible for debug devices.
///
public class DebugDeviceProvider : IRGBDeviceProvider
{
#region Properties & Fields
private static DebugDeviceProvider _instance;
///
/// Gets the singleton instance.
///
public static DebugDeviceProvider Instance => _instance ?? new DebugDeviceProvider();
///
public bool IsInitialized { get; private set; }
///
public IEnumerable Devices { get; private set; }
///
public bool HasExclusiveAccess { get; private set; }
private List<(string layout, string imageLayout, Func> syncBackFunc, Action> updateLedsAction)> _fakeDeviceDefinitions
= new List<(string layout, string imageLayout, Func> syncBackFunc, Action> updateLedsAction)>();
#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()
{
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.
/// The image-layout to load.
/// A function emulating device syncback.
/// A action emulating led-updates.
public void AddFakeDeviceDefinition(string layout, string imageLayout, Func> syncBackFunc = null, Action> updateLedsAction = null)
=> _fakeDeviceDefinitions.Add((layout, imageLayout, syncBackFunc, updateLedsAction));
///
/// Removes all previously added fake device definitions.
///
public void ClearFakeDeviceDefinitions() => _fakeDeviceDefinitions.Clear();
///
public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.Unknown, bool exclusiveAccessIfPossible = false, bool throwExceptions = false)
{
IsInitialized = false;
try
{
HasExclusiveAccess = exclusiveAccessIfPossible;
List devices = new List();
foreach ((string layout, string imageLayout, Func> syncBackFunc, Action> updateLedsAction) in _fakeDeviceDefinitions)
{
DebugRGBDevice device = new DebugRGBDevice(layout, syncBackFunc, updateLedsAction);
device.Initialize(layout, imageLayout);
devices.Add(device);
}
Devices = new ReadOnlyCollection(devices);
IsInitialized = true;
return true;
}
catch
{
if (throwExceptions) throw;
return false;
}
}
///
public void ResetDevices()
{ }
///
public void Dispose()
{
_fakeDeviceDefinitions.Clear();
}
#endregion
}
}