1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-12 17:48:31 +00:00

Merge pull request #275 from DarthAffe/DeviceHeartbeat

Device heartbeats
This commit is contained in:
DarthAffe 2022-11-06 02:12:00 +01:00 committed by GitHub
commit 8555d6b961
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 73 additions and 87 deletions

View File

@ -42,7 +42,7 @@ public abstract class AbstractRGBDeviceProvider : IRGBDeviceProvider
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="AbstractRGBDeviceProvider" /> class.
/// </summary>
@ -157,7 +157,6 @@ public abstract class AbstractRGBDeviceProvider : IRGBDeviceProvider
/// <returns>The newly created update trigger.</returns>
protected virtual IDeviceUpdateTrigger CreateUpdateTrigger(int id, double updateRateHardLimit) => new DeviceUpdateTrigger(updateRateHardLimit);
/// <summary>
/// Resets the device provider and disposes all devices and update triggers.
/// </summary>

View File

@ -40,4 +40,16 @@ public static class CustomUpdateDataExtension
customUpdateData[CustomUpdateDataIndex.UPDATE_DEVICES] = value;
return customUpdateData;
}
/// <summary>
/// Sets the <see cref="CustomUpdateDataIndex.HEARTBEAT"/>-Parameter to the given value.
/// </summary>
/// <param name="customUpdateData">The update-data to modify.</param>
/// <param name="value">The value to set.</param>
/// <returns>The modified update-data.</returns>
public static CustomUpdateData Heartbeat(this CustomUpdateData customUpdateData, bool value = true)
{
customUpdateData[CustomUpdateDataIndex.HEARTBEAT] = value;
return customUpdateData;
}
}

View File

@ -24,6 +24,11 @@ public static class CustomUpdateDataIndex
/// default: true
/// </summary>
public const string UPDATE_DEVICES = "updateDevices";
/// <summary>
/// Used by <see cref="DeviceUpdateTrigger"/> to indicate heatbeat updates.
/// </summary>
public const string HEARTBEAT = "heartbeat";
}
/// <summary>

View File

@ -1,5 +1,6 @@
// ReSharper disable MemberCanBePrivate.Global
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
@ -52,9 +53,20 @@ public class DeviceUpdateTrigger : AbstractUpdateTrigger, IDeviceUpdateTrigger
}
}
/// <summary>
/// Gets or sets the time in ms after which a refresh-request is sent even if no changes are made in the meantime to prevent the target from timing out or similar problems.
/// To disable heartbeats leave it at 0.
/// </summary>
public int HeartbeatTimer { get; set; }
/// <inheritdoc />
public override double LastUpdateTime { get; protected set; }
/// <summary>
/// Gets or sets the timestamp of the last update.
/// </summary>
protected long LastUpdateTimestamp { get; set; }
/// <summary>
/// Gets or sets the event to trigger when new data is available (<see cref="TriggerHasData"/>).
/// </summary>
@ -145,6 +157,14 @@ public class DeviceUpdateTrigger : AbstractUpdateTrigger, IDeviceUpdateTrigger
while (!UpdateToken.IsCancellationRequested)
if (HasDataEvent.WaitOne(Timeout))
LastUpdateTime = TimerHelper.Execute(() => OnUpdate(), UpdateFrequency * 1000);
else if ((HeartbeatTimer > 0) && (LastUpdateTimestamp > 0) && (TimerHelper.GetElapsedTime(LastUpdateTimestamp) > HeartbeatTimer))
OnUpdate(new CustomUpdateData().Heartbeat());
}
protected override void OnUpdate(CustomUpdateData? updateData = null)
{
base.OnUpdate(updateData);
LastUpdateTimestamp = Stopwatch.GetTimestamp();
}
/// <inheritdoc />

View File

@ -57,14 +57,15 @@ public class DMXDeviceProvider : AbstractRGBDeviceProvider
/// <inheritdoc />
protected override IEnumerable<IRGBDevice> LoadDevices()
{
foreach (IDMXDeviceDefinition dmxDeviceDefinition in DeviceDefinitions)
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(0));
device = new E131Device(new E131DeviceInfo(e131DMXDeviceDefinition), e131DMXDeviceDefinition.Leds, GetUpdateTrigger(i));
}
catch (Exception ex)
{
@ -76,5 +77,14 @@ public class DMXDeviceProvider : AbstractRGBDeviceProvider
}
}
protected override IDeviceUpdateTrigger CreateUpdateTrigger(int id, double updateRateHardLimit)
{
DeviceUpdateTrigger updateTrigger = new(updateRateHardLimit);
if ((DeviceDefinitions[id] is E131DMXDeviceDefinition e131DMXDeviceDefinition))
updateTrigger.HeartbeatTimer = e131DMXDeviceDefinition.HeartbeatTimer;
return updateTrigger;
}
#endregion
}

View File

@ -57,6 +57,12 @@ public class E131DMXDeviceDefinition : IDMXDeviceDefinition
/// </summary>
public Dictionary<LedId, List<(int channel, Func<Color, byte> getValueFunc)>> Leds { get; } = new();
/// <summary>
/// The time in ms after which the device is updated even if no changes are made in the meantime to prevent the target from timing out or similar problems.
/// To disable heartbeats leave it at 0.
/// </summary>
public int HeartbeatTimer { get; set; } = 0;
#endregion
#region Constructors

View File

@ -50,6 +50,14 @@ public class E131UpdateQueue : UpdateQueue
#region Methods
protected override void OnUpdate(object? sender, CustomUpdateData customData)
{
if (customData[CustomUpdateDataIndex.HEARTBEAT] as bool? ?? false)
Update(Array.Empty<(object key, Color color)>());
else
base.OnUpdate(sender, customData);
}
/// <inheritdoc />
protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet)
{

View File

@ -14,7 +14,7 @@ internal class SteelSeriesDeviceUpdateQueue : UpdateQueue
{
#region Properties & Fields
private string _deviceType;
private readonly string _deviceType;
#endregion
@ -37,7 +37,7 @@ internal class SteelSeriesDeviceUpdateQueue : UpdateQueue
protected override void OnUpdate(object? sender, CustomUpdateData customData)
{
if (customData["refresh"] as bool? ?? false)
if (customData[CustomUpdateDataIndex.HEARTBEAT] as bool? ?? false)
SteelSeriesSDK.SendHeartbeat();
else
base.OnUpdate(sender, customData);

View File

@ -1,80 +0,0 @@
// ReSharper disable MemberCanBePrivate.Global
using System.Diagnostics;
using System.Threading;
using RGB.NET.Core;
namespace RGB.NET.Devices.SteelSeries;
/// <summary>
/// Represents an update-trigger used to update SteelSeries devices
/// </summary>
public class SteelSeriesDeviceUpdateTrigger : DeviceUpdateTrigger
{
#region Constants
private static readonly long FLUSH_TIMER = 5 * 1000 * (long)(Stopwatch.Frequency / 1000.0); // flush the device every 5 seconds to prevent timeouts
#endregion
#region Properties & Fields
private long _lastUpdateTimestamp;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="SteelSeriesDeviceUpdateTrigger"/> class.
/// </summary>
public SteelSeriesDeviceUpdateTrigger()
{ }
/// <summary>
/// Initializes a new instance of the <see cref="SteelSeriesDeviceUpdateTrigger"/> class.
/// </summary>
/// <param name="updateRateHardLimit">The hard limit of the update rate of this trigger.</param>
public SteelSeriesDeviceUpdateTrigger(double updateRateHardLimit)
: base(updateRateHardLimit)
{ }
#endregion
#region Methods
/// <inheritdoc />
protected override void UpdateLoop()
{
OnStartup();
while (!UpdateToken.IsCancellationRequested)
{
if (HasDataEvent.WaitOne(Timeout))
{
long preUpdateTicks = Stopwatch.GetTimestamp();
OnUpdate();
if (UpdateFrequency > 0)
{
double lastUpdateTime = ((_lastUpdateTimestamp - preUpdateTicks) / (Stopwatch.Frequency / 1000.0));
int sleep = (int)((UpdateFrequency * 1000.0) - lastUpdateTime);
if (sleep > 0)
Thread.Sleep(sleep);
}
}
else if ((_lastUpdateTimestamp > 0) && ((Stopwatch.GetTimestamp() - _lastUpdateTimestamp) > FLUSH_TIMER))
OnUpdate(new CustomUpdateData(("refresh", true)));
}
}
/// <inheritdoc />
protected override void OnUpdate(CustomUpdateData? updateData = null)
{
base.OnUpdate(updateData);
_lastUpdateTimestamp = Stopwatch.GetTimestamp();
}
#endregion
}

View File

@ -12,6 +12,12 @@ namespace RGB.NET.Devices.SteelSeries;
/// </summary>
public class SteelSeriesDeviceProvider : AbstractRGBDeviceProvider
{
#region Constants
private static readonly int HEARTBEAT_TIMER = 5000; // flush the device every 5 seconds to prevent timeouts
#endregion
#region Properties & Fields
private static SteelSeriesDeviceProvider? _instance;
@ -121,7 +127,7 @@ public class SteelSeriesDeviceProvider : AbstractRGBDeviceProvider
}
/// <inheritdoc />
protected override IDeviceUpdateTrigger CreateUpdateTrigger(int id, double updateRateHardLimit) => new SteelSeriesDeviceUpdateTrigger(updateRateHardLimit);
protected override IDeviceUpdateTrigger CreateUpdateTrigger(int id, double updateRateHardLimit) => new DeviceUpdateTrigger(updateRateHardLimit) { HeartbeatTimer = HEARTBEAT_TIMER };
/// <inheritdoc />
public override void Dispose()