-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathDMXDeviceProvider.cs
115 lines (95 loc) · 3.38 KB
/
DMXDeviceProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System;
using System.Collections.Generic;
using System.Threading;
using RGB.NET.Core;
using RGB.NET.Devices.DMX.E131;
namespace RGB.NET.Devices.DMX;
/// <inheritdoc />
/// <summary>
/// Represents a device provider responsible for DMX devices.
/// </summary>
public sealed class DMXDeviceProvider : AbstractRGBDeviceProvider
{
#region Properties & Fields
// ReSharper disable once InconsistentNaming
private static readonly Lock _lock = new();
private static DMXDeviceProvider? _instance;
/// <summary>
/// Gets the singleton <see cref="DMXDeviceProvider"/> instance.
/// </summary>
public static DMXDeviceProvider Instance
{
get
{
lock (_lock)
return _instance ?? new DMXDeviceProvider();
}
}
/// <summary>
/// Gets a list of all defined device-definitions.
/// </summary>
public List<IDMXDeviceDefinition> DeviceDefinitions { get; } = [];
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="DMXDeviceProvider"/> class.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown if this constructor is called even if there is already an instance of this class.</exception>
public DMXDeviceProvider()
{
lock (_lock)
{
if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(DMXDeviceProvider)}");
_instance = this;
}
}
#endregion
#region Methods
/// <summary>
/// Adds the specified <see cref="IDMXDeviceDefinition" /> to this device-provider.
/// </summary>
/// <param name="deviceDefinition">The <see cref="IDMXDeviceDefinition"/> to add.</param>
public void AddDeviceDefinition(IDMXDeviceDefinition deviceDefinition) => DeviceDefinitions.Add(deviceDefinition);
/// <inheritdoc />
protected override void InitializeSDK() { }
/// <inheritdoc />
protected override IEnumerable<IRGBDevice> LoadDevices()
{
for (int i = 0; i < DeviceDefinitions.Count; i++)
{
IDMXDeviceDefinition dmxDeviceDefinition = DeviceDefinitions[i];
IRGBDevice? device = null;
try
{
if (dmxDeviceDefinition is E131DMXDeviceDefinition e131DMXDeviceDefinition)
if (e131DMXDeviceDefinition.Leds.Count > 0)
device = new E131Device(new E131DeviceInfo(e131DMXDeviceDefinition), e131DMXDeviceDefinition.Leds, GetUpdateTrigger(i));
}
catch (Exception ex)
{
Throw(ex);
}
if (device != null)
yield return device;
}
}
protected override IDeviceUpdateTrigger CreateUpdateTrigger(int id, double updateRateHardLimit)
{
DeviceUpdateTrigger updateTrigger = new(updateRateHardLimit);
if ((DeviceDefinitions[id] is E131DMXDeviceDefinition e131DMXDeviceDefinition))
updateTrigger.HeartbeatTimer = e131DMXDeviceDefinition.HeartbeatTimer;
return updateTrigger;
}
/// <inheritdoc />
protected override void Dispose(bool disposing)
{
lock (_lock)
{
base.Dispose(disposing);
_instance = null;
}
}
#endregion
}