using System.Collections.Generic;
namespace OpenMacroBoard.SDK
{
///
/// A adapter that replays brightness and key bitmaps if a device is disconnected.
///
public class DisconnectReplayAdapter : MacroBoardAdapter
{
private readonly Dictionary mostRecentKeyBitmaps = new();
private byte? mostRecentBrightness = null;
///
/// Initializes a new instance of the class.
///
public DisconnectReplayAdapter(IMacroBoard macroBoard)
: base(macroBoard)
{
ConnectionStateChanged += ReplayEventsForConnectionStateChange;
}
///
public override void SetBrightness(byte percent)
{
mostRecentBrightness = percent;
base.SetBrightness(percent);
}
///
public override void SetButtonImage(int keyId, KeyBitmap bitmapData)
{
mostRecentKeyBitmaps[keyId] = bitmapData;
base.SetButtonImage(keyId, bitmapData);
}
private void ReplayEventsForConnectionStateChange(object sender, ConnectionEventArgs e)
{
if (e.NewConnectionState)
{
// devices connected again: replay last known values
if (mostRecentBrightness is not null)
{
base.SetBrightness(mostRecentBrightness.Value);
}
foreach (var bmp in mostRecentKeyBitmaps)
{
base.SetButtonImage(bmp.Key, bmp.Value);
}
}
}
}
}