fix: add IHasInputsMessenger

In order to match up with how existing front-end apps are expecting to
recieve data for devices that implement the `IHasInputs<T>` interface,
there is now an IHasInputsMessengers that is implemented for devices
that implement `IHasInputs<string>`, `IHasInputs<int>` or
`IHasInputs<byte>` interfaces.
This commit is contained in:
Andrew Welker
2025-04-09 11:48:05 -05:00
parent 8d3fd343f1
commit 0b59990532
2 changed files with 107 additions and 9 deletions

View File

@@ -0,0 +1,101 @@
using Newtonsoft.Json;
using PepperDash.Core;
using PepperDash.Core.Logging;
using PepperDash.Essentials.Core.DeviceTypeInterfaces;
using System;
using System.Collections.Generic;
namespace PepperDash.Essentials.AppServer.Messengers
{
public class IHasInputsMessenger<TKey> : MessengerBase
{
private readonly IHasInputs<TKey> itemDevice;
/// <summary>
/// Constructs a messenger for a device that implements IHasInputs<typeparamref name="TKey"/>
/// </summary>
/// <param name="key"></param>
/// <param name="messagePath"></param>
/// <param name="device"></param>
public IHasInputsMessenger(string key, string messagePath, IHasInputs<TKey> device) : base(key, messagePath, device)
{
itemDevice = device;
}
protected override void RegisterActions()
{
base.RegisterActions();
AddAction("/fullStatus", (id, context) =>
{
SendFullStatus();
});
itemDevice.Inputs.ItemsUpdated += (sender, args) =>
{
SendFullStatus();
};
itemDevice.Inputs.CurrentItemChanged += (sender, args) =>
{
SendFullStatus();
};
foreach (var input in itemDevice.Inputs.Items)
{
var key = input.Key;
var localItem = input.Value;
AddAction($"/{key}", (id, content) =>
{
localItem.Select();
});
localItem.ItemUpdated += (sender, args) =>
{
SendFullStatus();
};
}
}
private void SendFullStatus()
{
try
{
this.LogInformation("Sending full status");
var stateObject = new IHasInputsStateMessage<TKey>
{
Inputs = new Inputs<TKey>
{
Items = itemDevice.Inputs.Items,
CurrentItem = itemDevice.Inputs.CurrentItem
}
};
PostStatusMessage(stateObject);
}
catch (Exception e)
{
this.LogError("Error sending full status: {0}", e.Message);
}
}
}
public class IHasInputsStateMessage<TKey> : DeviceStateMessageBase
{
[JsonProperty("inputs")]
public Inputs<TKey> Inputs { get; set; }
}
public class Inputs<TKey>
{
[JsonProperty("items")]
public Dictionary<TKey, ISelectableItem> Items { get; set; }
[JsonProperty("currentItem")]
public TKey CurrentItem { get; set; }
}
}

View File

@@ -763,11 +763,10 @@ namespace PepperDash.Essentials
{
this.LogVerbose("Adding InputsMessenger<string> for {deviceKey}", device.Key);
var messenger = new ISelectableItemsMessenger<string>(
var messenger = new IHasInputsMessenger<string>(
$"{device.Key}-inputs-{Key}",
$"/device/{device.Key}",
stringInputs,
"inputs"
stringInputs
);
AddDefaultDeviceMessenger(messenger);
@@ -779,11 +778,10 @@ namespace PepperDash.Essentials
{
this.LogVerbose("Adding InputsMessenger for {deviceKey}", device.Key);
var messenger = new ISelectableItemsMessenger<byte>(
var messenger = new IHasInputsMessenger<byte>(
$"{device.Key}-inputs-{Key}",
$"/device/{device.Key}",
byteInputs,
"inputs"
byteInputs
);
AddDefaultDeviceMessenger(messenger);
@@ -795,11 +793,10 @@ namespace PepperDash.Essentials
{
this.LogVerbose("Adding InputsMessenger for {deviceKey}", device.Key);
var messenger = new ISelectableItemsMessenger<int>(
var messenger = new IHasInputsMessenger<int>(
$"{device.Key}-inputs-{Key}",
$"/device/{device.Key}",
intInputs,
"inputs"
intInputs
);
AddDefaultDeviceMessenger(messenger);