mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-01-11 19:44:52 +00:00
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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user