mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-02-15 04:34:56 +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 readonly string _propName;
|
||||||
|
|
||||||
|
private List<string> _itemKeys = new List<string>();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Constructs a messenger for a device that implements ISelectableItems<typeparamref name="TKey"/>
|
/// Constructs a messenger for a device that implements ISelectableItems<typeparamref name="TKey"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -41,7 +43,7 @@ namespace PepperDash.Essentials.AppServer.Messengers
|
|||||||
|
|
||||||
itemDevice.ItemsUpdated += (sender, args) =>
|
itemDevice.ItemsUpdated += (sender, args) =>
|
||||||
{
|
{
|
||||||
SendFullStatus();
|
SetItems();
|
||||||
};
|
};
|
||||||
|
|
||||||
itemDevice.CurrentItemChanged += (sender, args) =>
|
itemDevice.CurrentItemChanged += (sender, args) =>
|
||||||
@@ -49,6 +51,22 @@ namespace PepperDash.Essentials.AppServer.Messengers
|
|||||||
SendFullStatus();
|
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)
|
foreach (var input in itemDevice.Items)
|
||||||
{
|
{
|
||||||
var key = input.Key;
|
var key = input.Key;
|
||||||
@@ -59,13 +77,18 @@ namespace PepperDash.Essentials.AppServer.Messengers
|
|||||||
localItem.Select();
|
localItem.Select();
|
||||||
});
|
});
|
||||||
|
|
||||||
localItem.ItemUpdated += (sender, args) =>
|
_itemKeys.Add(key.ToString());
|
||||||
{
|
|
||||||
SendFullStatus();
|
localItem.ItemUpdated -= LocalItem_ItemUpdated;
|
||||||
};
|
localItem.ItemUpdated += LocalItem_ItemUpdated;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void LocalItem_ItemUpdated(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
SendFullStatus();
|
||||||
|
}
|
||||||
|
|
||||||
private void SendFullStatus(string id = null)
|
private void SendFullStatus(string id = null)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|||||||
Reference in New Issue
Block a user