Compare commits

...

4 Commits

Author SHA1 Message Date
Neil Dorin
c284c4275f feat: Enhance UDPServer initialization in Connect method
Updated the `Connect` method in `GenericUdpServer.cs` to include error handling for hostname parsing. The method now attempts to create a `UDPServer` instance with specified parameters and falls back to default initialization if an error occurs. This improves flexibility and robustness in server setup.
2025-10-09 11:31:38 -06:00
Neil Dorin
0418f8a7cc fix: Fix typos and enhance item selection handling
Corrected parameter name in GenericUdpServer constructor.
Added new action for item selection in ISelectableItemsMessenger
with error handling for missing or invalid keys.
Updated SetItems method to improve clarity and ensure proper
clearing and re-adding of item actions.
2025-10-09 09:40:46 -06:00
Neil Dorin
419177ccd5 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.
2025-10-08 12:24:17 -06:00
Neil Dorin
bd01e2bacc fix: Add KeyName class and update camera messaging
This commit introduces a new `KeyName` class implementing the `IKeyName` interface, enhancing the representation of camera data. The `CameraController_CameraSelected` and `SendFullStatus` methods are updated to utilize `KeyName` instances for selected and listed cameras, improving data encapsulation and consistency in the `IHasCamerasWithControlsStateMessage`. Additionally, new using directives for logging and core functionalities are added.
2025-10-07 17:10:11 -06:00
3 changed files with 119 additions and 16 deletions

View File

@@ -131,14 +131,14 @@ namespace PepperDash.Core
/// <param name="key"></param>
/// <param name="address"></param>
/// <param name="port"></param>
/// <param name="buffefSize"></param>
public GenericUdpServer(string key, string address, int port, int buffefSize)
/// <param name="bufferSize"></param>
public GenericUdpServer(string key, string address, int port, int bufferSize)
: base(key)
{
StreamDebugging = new CommunicationStreamDebugging(key);
Hostname = address;
Port = port;
BufferSize = buffefSize;
BufferSize = bufferSize;
CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler);
CrestronEnvironment.EthernetEventHandler += new EthernetEventHandler(CrestronEnvironment_EthernetEventHandler);
@@ -194,7 +194,21 @@ namespace PepperDash.Core
{
if (Server == null)
{
Server = new UDPServer();
try
{
var address = IPAddress.Parse(Hostname);
Server = new UDPServer(address, Port, BufferSize);
}
catch (Exception ex)
{
this.LogError("Error parsing IP Address '{ipAddress}': message: {message}", Hostname, ex.Message);
this.LogInformation("Creating UDPServer with default buffersize");
Server = new UDPServer();
}
}
if (string.IsNullOrEmpty(Hostname))

View File

@@ -1,5 +1,7 @@
using Newtonsoft.Json;
using PepperDash.Core;
using PepperDash.Core.Logging;
using PepperDash.Essentials.Core;
using PepperDash.Essentials.Devices.Common.Cameras;
using System;
using System.Collections.Generic;
@@ -33,9 +35,15 @@ namespace PepperDash.Essentials.AppServer.Messengers
private void CameraController_CameraSelected(object sender, CameraSelectedEventArgs<IHasCameraControls> e)
{
var selectedCamera = new KeyName
{
Key = e.SelectedCamera.Key,
Name = e.SelectedCamera.Name
};
PostStatusMessage(new IHasCamerasWithControlsStateMessage
{
SelectedCamera = e.SelectedCamera
SelectedCamera = selectedCamera
});
}
@@ -68,10 +76,30 @@ namespace PepperDash.Essentials.AppServer.Messengers
private void SendFullStatus(string clientId)
{
var cameraList = new List<IKeyName>();
KeyName selectedCamera = null;
foreach (var cam in CameraController.Cameras)
{
cameraList.Add(new KeyName{
Key = cam.Key,
Name = cam.Name
});
}
if (CameraController.SelectedCamera != null)
{
selectedCamera = new KeyName
{
Key = CameraController.SelectedCamera.Key,
Name = CameraController.SelectedCamera.Name
};
}
var state = new IHasCamerasWithControlsStateMessage
{
CameraList = CameraController.Cameras.Cast<IKeyName>().ToList(),
SelectedCamera = CameraController.SelectedCamera as IKeyName
CameraList = cameraList,
SelectedCamera = selectedCamera
};
PostStatusMessage(state, clientId);
@@ -96,5 +124,14 @@ namespace PepperDash.Essentials.AppServer.Messengers
public IKeyName SelectedCamera { get; set; }
}
class KeyName : IKeyName
{
public string Key { get; set; }
public string Name { get; set; }
public KeyName()
{
Key = "";
Name = "";
}
}
}

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>
@@ -39,9 +41,35 @@ namespace PepperDash.Essentials.AppServer.Messengers
AddAction("/itemsStatus", (id, content) => SendFullStatus(id));
AddAction("/selectItem", (id, content) =>
{
try
{
var key = content.ToObject<TKey>();
if (key == null)
{
this.LogError("No key specified to select");
return;
}
if (itemDevice.Items.ContainsKey((TKey)Convert.ChangeType(key, typeof(TKey))))
{
itemDevice.Items[(TKey)Convert.ChangeType(key, typeof(TKey))].Select();
}
else
{
this.LogError("Key {0} not found in items", key);
}
}
catch (Exception e)
{
this.LogError("Error selecting item: {0}", e.Message);
}
});
itemDevice.ItemsUpdated += (sender, args) =>
{
SendFullStatus();
SetItems();
};
itemDevice.CurrentItemChanged += (sender, args) =>
@@ -49,23 +77,47 @@ namespace PepperDash.Essentials.AppServer.Messengers
SendFullStatus();
};
foreach (var input in itemDevice.Items)
SetItems();
}
/// <summary>
/// Sets the items and registers their update events
/// </summary>
private void SetItems()
{
if (_itemKeys != null && _itemKeys.Count > 0)
{
var key = input.Key;
var localItem = input.Value;
/// Clear out any existing item actions
foreach (var item in _itemKeys)
{
RemoveAction($"/{item}");
}
_itemKeys.Clear();
}
foreach (var item in itemDevice.Items)
{
var key = item.Key;
var localItem = item.Value;
AddAction($"/{key}", (id, content) =>
{
localItem.Select();
});
localItem.ItemUpdated += (sender, args) =>
{
SendFullStatus();
};
_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)
{
try