fix: Add item management and update handling in messenger

Introduces a new private field `_itemKeys` to store item keys. Adds the `SetItems` method to manage item actions and update events, ensuring proper registration and cleanup. The `SendFullStatus` method is now invoked from a dedicated event handler `LocalItem_ItemUpdated`, improving the handling of item updates.
This commit is contained in:
Neil Dorin
2025-10-08 12:24:17 -06:00
parent bd01e2bacc
commit 419177ccd5

View File

@@ -16,6 +16,8 @@ namespace PepperDash.Essentials.AppServer.Messengers
private readonly string _propName;
private List<string> _itemKeys = new List<string>();
/// <summary>
/// Constructs a messenger for a device that implements ISelectableItems<typeparamref name="TKey"/>
/// </summary>
@@ -41,7 +43,7 @@ namespace PepperDash.Essentials.AppServer.Messengers
itemDevice.ItemsUpdated += (sender, args) =>
{
SendFullStatus();
SetItems();
};
itemDevice.CurrentItemChanged += (sender, args) =>
@@ -49,6 +51,22 @@ namespace PepperDash.Essentials.AppServer.Messengers
SendFullStatus();
};
SetItems();
}
/// <summary>
/// Sets the items and registers their update events
/// </summary>
private void SetItems()
{
/// Clear out any existing item actions
foreach (var item in _itemKeys)
{
RemoveAction($"/{item}");
}
_itemKeys.Clear();
foreach (var input in itemDevice.Items)
{
var key = input.Key;
@@ -59,11 +77,16 @@ namespace PepperDash.Essentials.AppServer.Messengers
localItem.Select();
});
localItem.ItemUpdated += (sender, args) =>
_itemKeys.Add(key.ToString());
localItem.ItemUpdated -= LocalItem_ItemUpdated;
localItem.ItemUpdated += LocalItem_ItemUpdated;
}
}
private void LocalItem_ItemUpdated(object sender, EventArgs e)
{
SendFullStatus();
};
}
}
private void SendFullStatus(string id = null)