using System;
using AuraServiceLib;
using RGB.NET.Core;
namespace RGB.NET.Devices.Asus
{
///
///
/// Represents the update-queue performing updates for asus devices.
///
public class AsusUpdateQueue : UpdateQueue
{
#region Properties & Fields
private readonly IAuraRgbLight[] _lights;
private readonly IAuraRgbKey[] _keys;
///
/// The device to be updated.
///
protected IAuraSyncDevice Device { get; }
#endregion
#region Constructors
///
/// Initializes a new instance of the class.
///
/// The update trigger used by this queue.
/// The SDK-aura-device this device represents.
public AsusUpdateQueue(IDeviceUpdateTrigger updateTrigger, IAuraSyncDevice device)
: base(updateTrigger)
{
this.Device = device;
this._lights = new IAuraRgbLight[device.Lights.Count];
for (int i = 0; i < device.Lights.Count; i++)
_lights[i] = device.Lights[i];
if (Device is IAuraSyncKeyboard keyboard)
{
this._keys = new IAuraRgbKey[keyboard.Keys.Count];
for (int i = 0; i < keyboard.Keys.Count; i++)
_keys[i] = keyboard.Keys[i];
}
else
this._keys = new IAuraRgbKey[0];
}
#endregion
#region Methods
///
protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet)
{
try
{
if ((Device.Type == (uint)AsusDeviceType.KEYBOARD_RGB) || (Device.Type == (uint)AsusDeviceType.NB_KB_RGB))
{
if (Device is not IAuraSyncKeyboard)
return;
foreach ((object customData, Color value) in dataSet)
{
(AsusLedType ledType, int id) = (AsusKeyboardLedCustomData)customData;
if (ledType == AsusLedType.Key)
{
IAuraRgbLight light = _keys[(ushort)id];
(_, byte r, byte g, byte b) = value.GetRGBBytes();
light.Red = r;
light.Green = g;
light.Blue = b;
}
else
{
IAuraRgbLight light = _lights[id];
(_, byte r, byte g, byte b) = value.GetRGBBytes();
light.Red = r;
light.Green = g;
light.Blue = b;
}
}
}
else
{
foreach ((object key, Color value) in dataSet)
{
int index = (int)key;
IAuraRgbLight light = _lights[index];
(_, byte r, byte g, byte b) = value.GetRGBBytes();
light.Red = r;
light.Green = g;
light.Blue = b;
}
}
Device.Apply();
}
catch
{ /* "The server threw an exception." seems to be a thing here ... */
}
}
#endregion
}
}