-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathWLedDeviceProvider.cs
114 lines (90 loc) · 3.42 KB
/
WLedDeviceProvider.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
// ReSharper disable MemberCanBePrivate.Global
using System;
using System.Collections.Generic;
using System.Threading;
using RGB.NET.Core;
namespace RGB.NET.Devices.WLED;
/// <inheritdoc />
/// <summary>
/// Represents a device provider responsible for WS2812B- and WS2811-Led-devices.
/// </summary>
// ReSharper disable once InconsistentNaming
// ReSharper disable once UnusedType.Global
public sealed class WledDeviceProvider : AbstractRGBDeviceProvider
{
#region Constants
private const int HEARTBEAT_TIMER = 250;
#endregion
#region Properties & Fields
// ReSharper disable once InconsistentNaming
private static readonly Lock _lock = new();
private static WledDeviceProvider? _instance;
/// <summary>
/// Gets the singleton <see cref="WledDeviceProvider"/> instance.
/// </summary>
public static WledDeviceProvider Instance
{
get
{
lock (_lock)
return _instance ?? new WledDeviceProvider();
}
}
/// <summary>
/// Gets a list of all defined device-definitions.
/// </summary>
public List<IWledDeviceDefinition> DeviceDefinitions { get; } = [];
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="WledDeviceProvider"/> class.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown if this constructor is called even if there is already an instance of this class.</exception>
public WledDeviceProvider()
{
lock (_lock)
{
if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(WledDeviceProvider)}");
_instance = this;
}
}
#endregion
#region Methods
/// <summary>
/// Adds the specified <see cref="IWledDeviceDefinition" /> to this device-provider.
/// </summary>
/// <param name="deviceDefinition">The <see cref="IWledDeviceDefinition"/> to add.</param>
public void AddDeviceDefinition(IWledDeviceDefinition deviceDefinition) => DeviceDefinitions.Add(deviceDefinition);
/// <inheritdoc />
protected override void InitializeSDK() { }
/// <inheritdoc />
protected override IEnumerable<IRGBDevice> LoadDevices()
{
int i = 0;
foreach (IWledDeviceDefinition deviceDefinition in DeviceDefinitions)
{
IDeviceUpdateTrigger updateTrigger = GetUpdateTrigger(i++);
WledRGBDevice? device = CreateWledDevice(deviceDefinition, updateTrigger);
if (device != null)
yield return device;
}
}
private static WledRGBDevice? CreateWledDevice(IWledDeviceDefinition deviceDefinition, IDeviceUpdateTrigger updateTrigger)
{
WledInfo? wledInfo = WledAPI.Info(deviceDefinition.Address);
if (wledInfo == null) return null;
return new WledRGBDevice(new WledRGBDeviceInfo(wledInfo, deviceDefinition.Manufacturer, deviceDefinition.Model), deviceDefinition.Address, updateTrigger);
}
protected override IDeviceUpdateTrigger CreateUpdateTrigger(int id, double updateRateHardLimit) => new DeviceUpdateTrigger(updateRateHardLimit) { HeartbeatTimer = HEARTBEAT_TIMER };
/// <inheritdoc />
protected override void Dispose(bool disposing)
{
lock (_lock)
{
base.Dispose(disposing);
DeviceDefinitions.Clear();
_instance = null;
}
}
#endregion
}