// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using RGB.NET.Core;
using RGB.NET.Devices.SoIP.Client;
using RGB.NET.Devices.SoIP.Generic;
using RGB.NET.Devices.SoIP.Server;
namespace RGB.NET.Devices.SoIP
{
///
///
/// Represents a device provider responsible for debug devices.
///
public class SoIPDeviceProvider : IRGBDeviceProvider
{
#region Properties & Fields
private static SoIPDeviceProvider _instance;
///
/// Gets the singleton instance.
///
public static SoIPDeviceProvider Instance => _instance ?? new SoIPDeviceProvider();
///
public bool IsInitialized { get; private set; }
///
public IEnumerable Devices { get; private set; }
///
public bool HasExclusiveAccess => false;
///
/// Gets a list of all defined device-definitions.
///
public List DeviceDefinitions { get; } = new List();
///
/// The used to trigger the updates for dmx devices.
///
public DeviceUpdateTrigger UpdateTrigger { get; private set; }
#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 SoIPDeviceProvider()
{
if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(SoIPDeviceProvider)}");
_instance = this;
UpdateTrigger = new DeviceUpdateTrigger();
}
#endregion
#region Methods
///
/// Adds the given to this device-provider.
///
/// The to add.
public void AddDeviceDefinition(ISoIPDeviceDefinition deviceDefinition) => DeviceDefinitions.Add(deviceDefinition);
///
public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.Unknown, bool exclusiveAccessIfPossible = false, bool throwExceptions = false)
{
IsInitialized = false;
try
{
UpdateTrigger.Stop();
IList devices = new List();
foreach (ISoIPDeviceDefinition deviceDefinition in DeviceDefinitions)
{
try
{
ISoIPRGBDevice device = null;
switch (deviceDefinition)
{
case SoIPServerDeviceDefinition serverDeviceDefinition:
if (serverDeviceDefinition.Leds.Count > 0)
device = new SoIPServerRGBDevice(new SoIPServerRGBDeviceInfo(serverDeviceDefinition), serverDeviceDefinition.Leds);
break;
case SoIPClientDeviceDefinition clientDeviceDefinition:
device = new SoIPClientRGBDevice(new SoIPClientRGBDeviceInfo(clientDeviceDefinition));
break;
}
if (device != null)
{
device.Initialize(UpdateTrigger);
devices.Add(device);
}
}
catch { if (throwExceptions) throw; }
}
UpdateTrigger.Start();
Devices = new ReadOnlyCollection(devices);
IsInitialized = true;
}
catch
{
if (throwExceptions) throw;
return false;
}
return true;
}
///
public void ResetDevices()
{ }
///
public void Dispose()
{
foreach (IRGBDevice device in Devices)
device.Dispose();
}
#endregion
}
}