Merge pull request #1312 from PepperDash/comm-bridge-add

This commit is contained in:
Neil Dorin
2025-08-15 09:02:23 -06:00
committed by GitHub
5 changed files with 318 additions and 156 deletions

View File

@@ -74,6 +74,10 @@ namespace PepperDash.Core
/// <summary> /// <summary>
/// Secure TCP/IP /// Secure TCP/IP
/// </summary> /// </summary>
SecureTcpIp SecureTcpIp,
/// <summary>
/// Used when comms needs to be handled in SIMPL and bridged opposite the normal direction
/// </summary>
ComBridge
} }
} }

View File

@@ -7,6 +7,13 @@ namespace PepperDash.Essentials.Core.Bridges
/// </summary> /// </summary>
public interface IBridgeAdvanced public interface IBridgeAdvanced
{ {
/// <summary>
/// Links the bridge to the API using the provided trilist, join start, join map key, and bridge.
/// </summary>
/// <param name="trilist">The trilist to link to.</param>
/// <param name="joinStart">The starting join number.</param>
/// <param name="joinMapKey">The key for the join map.</param>
/// <param name="bridge">The EISC API bridge.</param>
void LinkToApi(BasicTriList trilist, uint joinStart, string joinMapKey, EiscApiAdvanced bridge); void LinkToApi(BasicTriList trilist, uint joinStart, string joinMapKey, EiscApiAdvanced bridge);
} }
} }

View File

@@ -0,0 +1,138 @@
using System;
using System.Collections.Generic;
using System.Text;
using Crestron.SimplSharp.CrestronSockets;
using Crestron.SimplSharpPro.DeviceSupport;
using Newtonsoft.Json;
using PepperDash.Core;
using PepperDash.Core.Logging;
using PepperDash.Essentials.Core.Bridges;
using PepperDash.Essentials.Core.Config;
using PepperDash.Essentials.Core.Devices;
namespace PepperDash.Essentials.Core
{
/// <summary>
/// Implements IBasicCommunication and sends all communication through an EISC
/// </summary>
[Description("Generic communication wrapper class for any IBasicCommunication type")]
public class CommBridge : EssentialsBridgeableDevice, IBasicCommunication
{
private EiscApiAdvanced eisc;
private IBasicCommunicationJoinMap joinMap;
/// <summary>
/// Event triggered when text is received through the communication bridge.
/// </summary>
public event EventHandler<GenericCommMethodReceiveTextArgs> TextReceived;
/// <summary>
/// Event triggered when bytes are received through the communication bridge.
/// </summary>
public event EventHandler<GenericCommMethodReceiveBytesArgs> BytesReceived;
/// <summary>
/// Indicates whether the communication bridge is currently connected.
/// </summary>
public bool IsConnected { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="CommBridge"/> class.
/// </summary>
/// <param name="key">The unique key for the communication bridge.</param>
/// <param name="name">The display name for the communication bridge.</param>
public CommBridge(string key, string name)
: base(key, name)
{
}
/// <summary>
/// Sends a byte array through the communication bridge.
/// </summary>
/// <param name="bytes">The byte array to send.</param>
public void SendBytes(byte[] bytes)
{
if (eisc == null)
{
this.LogWarning("EISC is null, cannot send bytes.");
return;
}
eisc.Eisc.SetString(joinMap.SendText.JoinNumber, Encoding.ASCII.GetString(bytes, 0, bytes.Length));
}
/// <summary>
/// Sends a text string through the communication bridge.
/// </summary>
/// <param name="text">The text string to send.</param>
public void SendText(string text)
{
if (eisc == null)
{
this.LogWarning("EISC is null, cannot send text.");
return;
}
eisc.Eisc.SetString(joinMap.SendText.JoinNumber, text);
}
/// <summary>
/// Initiates a connection through the communication bridge.
/// </summary>
public void Connect()
{
if (eisc == null)
{
this.LogWarning("EISC is null, cannot connect.");
return;
}
eisc.Eisc.SetBool(joinMap.Connect.JoinNumber, true);
}
/// <summary>
/// Terminates the connection through the communication bridge.
/// </summary>
public void Disconnect()
{
if (eisc == null)
{
this.LogWarning("EISC is null, cannot disconnect.");
return;
}
eisc.Eisc.SetBool(joinMap.Connect.JoinNumber, false);
}
/// <inheritdoc />
public override void LinkToApi(BasicTriList trilist, uint joinStart, string joinMapKey, EiscApiAdvanced bridge)
{
joinMap = new IBasicCommunicationJoinMap(joinStart);
var joinMapSerialized = JoinMapHelper.GetSerializedJoinMapForDevice(joinMapKey);
if (!string.IsNullOrEmpty(joinMapSerialized))
joinMap = JsonConvert.DeserializeObject<IBasicCommunicationJoinMap>(joinMapSerialized);
if (bridge != null)
{
bridge.AddJoinMap(Key, joinMap);
}
else
{
this.LogWarning("Please update config to use 'eiscapiadvanced' to get all join map features for this device.");
}
this.LogDebug("Linking to Trilist '{0}'", trilist.ID.ToString("X"));
eisc = bridge;
trilist.SetBoolSigAction(joinMap.Connected.JoinNumber, (b) => IsConnected = b);
trilist.SetStringSigAction(joinMap.TextReceived.JoinNumber, (s) =>
{
TextReceived?.Invoke(this, new GenericCommMethodReceiveTextArgs(s));
BytesReceived?.Invoke(this, new GenericCommMethodReceiveBytesArgs(Encoding.ASCII.GetBytes(s)));
});
}
}
}

View File

@@ -1,11 +1,9 @@
using System; using System;
using Crestron.SimplSharp;
using Crestron.SimplSharpPro; using Crestron.SimplSharpPro;
using Crestron.SimplSharpPro.DM; using Crestron.SimplSharpPro.DM;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PepperDash.Core; using PepperDash.Core;
using PepperDash.Essentials.Core.Config; using PepperDash.Essentials.Core.Config;
using Serilog.Events; using Serilog.Events;
@@ -38,9 +36,9 @@ namespace PepperDash.Essentials.Core
/// Returns a comm method of either com port, TCP, SSH, and puts this into the DeviceManager /// Returns a comm method of either com port, TCP, SSH, and puts this into the DeviceManager
/// </summary> /// </summary>
/// <param name="deviceConfig">The Device config object</param> /// <param name="deviceConfig">The Device config object</param>
/// <summary> /// <summary>
/// CreateCommForDevice method /// CreateCommForDevice method
/// </summary> /// </summary>
public static IBasicCommunication CreateCommForDevice(DeviceConfig deviceConfig) public static IBasicCommunication CreateCommForDevice(DeviceConfig deviceConfig)
{ {
EssentialsControlPropertiesConfig controlConfig = GetControlPropertiesConfig(deviceConfig); EssentialsControlPropertiesConfig controlConfig = GetControlPropertiesConfig(deviceConfig);
@@ -56,35 +54,38 @@ namespace PepperDash.Essentials.Core
case eControlMethod.Com: case eControlMethod.Com:
comm = new ComPortController(deviceConfig.Key + "-com", GetComPort, controlConfig.ComParams.Value, controlConfig); comm = new ComPortController(deviceConfig.Key + "-com", GetComPort, controlConfig.ComParams.Value, controlConfig);
break; break;
case eControlMethod.Cec: case eControlMethod.ComBridge:
comm = new CecPortController(deviceConfig.Key + "-cec", GetCecPort, controlConfig); comm = new CommBridge(deviceConfig.Key + "-simpl", deviceConfig.Name + " Simpl");
break; break;
case eControlMethod.Cec:
comm = new CecPortController(deviceConfig.Key + "-cec", GetCecPort, controlConfig);
break;
case eControlMethod.IR: case eControlMethod.IR:
break; break;
case eControlMethod.Ssh: case eControlMethod.Ssh:
{ {
var ssh = new GenericSshClient(deviceConfig.Key + "-ssh", c.Address, c.Port, c.Username, c.Password); var ssh = new GenericSshClient(deviceConfig.Key + "-ssh", c.Address, c.Port, c.Username, c.Password);
ssh.AutoReconnect = c.AutoReconnect; ssh.AutoReconnect = c.AutoReconnect;
if(ssh.AutoReconnect) if (ssh.AutoReconnect)
ssh.AutoReconnectIntervalMs = c.AutoReconnectIntervalMs; ssh.AutoReconnectIntervalMs = c.AutoReconnectIntervalMs;
comm = ssh; comm = ssh;
break; break;
} }
case eControlMethod.Tcpip: case eControlMethod.Tcpip:
{ {
var tcp = new GenericTcpIpClient(deviceConfig.Key + "-tcp", c.Address, c.Port, c.BufferSize); var tcp = new GenericTcpIpClient(deviceConfig.Key + "-tcp", c.Address, c.Port, c.BufferSize);
tcp.AutoReconnect = c.AutoReconnect; tcp.AutoReconnect = c.AutoReconnect;
if (tcp.AutoReconnect) if (tcp.AutoReconnect)
tcp.AutoReconnectIntervalMs = c.AutoReconnectIntervalMs; tcp.AutoReconnectIntervalMs = c.AutoReconnectIntervalMs;
comm = tcp; comm = tcp;
break; break;
} }
case eControlMethod.Udp: case eControlMethod.Udp:
{ {
var udp = new GenericUdpServer(deviceConfig.Key + "-udp", c.Address, c.Port, c.BufferSize); var udp = new GenericUdpServer(deviceConfig.Key + "-udp", c.Address, c.Port, c.BufferSize);
comm = udp; comm = udp;
break; break;
} }
case eControlMethod.Telnet: case eControlMethod.Telnet:
break; break;
case eControlMethod.SecureTcpIp: case eControlMethod.SecureTcpIp:
@@ -98,7 +99,7 @@ namespace PepperDash.Essentials.Core
} }
default: default:
break; break;
} }
} }
catch (Exception e) catch (Exception e)
{ {
@@ -107,15 +108,14 @@ namespace PepperDash.Essentials.Core
} }
// put it in the device manager if it's the right flavor // put it in the device manager if it's the right flavor
var comDev = comm as Device; if (comm is Device comDev)
if (comDev != null)
DeviceManager.AddDevice(comDev); DeviceManager.AddDevice(comDev);
return comm; return comm;
} }
/// <summary> /// <summary>
/// GetComPort method /// GetComPort method
/// </summary> /// </summary>
public static ComPort GetComPort(EssentialsControlPropertiesConfig config) public static ComPort GetComPort(EssentialsControlPropertiesConfig config)
{ {
var comPar = config.ComParams; var comPar = config.ComParams;
@@ -126,74 +126,74 @@ namespace PepperDash.Essentials.Core
return null; return null;
} }
/// <summary> /// <summary>
/// Gets an ICec port from a RoutingInput or RoutingOutput on a device /// Gets an ICec port from a RoutingInput or RoutingOutput on a device
/// </summary> /// </summary>
/// <param name="config"></param> /// <param name="config"></param>
/// <returns></returns> /// <returns></returns>
/// <summary> /// <summary>
/// GetCecPort method /// GetCecPort method
/// </summary> /// </summary>
public static ICec GetCecPort(ControlPropertiesConfig config) public static ICec GetCecPort(ControlPropertiesConfig config)
{ {
try try
{ {
var dev = DeviceManager.GetDeviceForKey(config.ControlPortDevKey); var dev = DeviceManager.GetDeviceForKey(config.ControlPortDevKey);
Debug.LogMessage(LogEventLevel.Information, "GetCecPort: device '{0}' {1}", config.ControlPortDevKey, dev == null Debug.LogMessage(LogEventLevel.Information, "GetCecPort: device '{0}' {1}", config.ControlPortDevKey, dev == null
? "is not valid, failed to get cec port" ? "is not valid, failed to get cec port"
: "found in device manager, attempting to get cec port"); : "found in device manager, attempting to get cec port");
if (dev == null) if (dev == null)
return null; return null;
if (String.IsNullOrEmpty(config.ControlPortName)) if (String.IsNullOrEmpty(config.ControlPortName))
{ {
Debug.LogMessage(LogEventLevel.Information, "GetCecPort: '{0}' - Configuration missing 'ControlPortName'", config.ControlPortDevKey); Debug.LogMessage(LogEventLevel.Information, "GetCecPort: '{0}' - Configuration missing 'ControlPortName'", config.ControlPortDevKey);
return null; return null;
} }
var inputsOutputs = dev as IRoutingInputsOutputs; var inputsOutputs = dev as IRoutingInputsOutputs;
if (inputsOutputs == null) if (inputsOutputs == null)
{ {
Debug.LogMessage(LogEventLevel.Information, "GetCecPort: Device '{0}' does not support IRoutingInputsOutputs, failed to get CEC port called '{1}'", Debug.LogMessage(LogEventLevel.Information, "GetCecPort: Device '{0}' does not support IRoutingInputsOutputs, failed to get CEC port called '{1}'",
config.ControlPortDevKey, config.ControlPortName); config.ControlPortDevKey, config.ControlPortName);
return null; return null;
} }
var inputPort = inputsOutputs.InputPorts[config.ControlPortName]; var inputPort = inputsOutputs.InputPorts[config.ControlPortName];
if (inputPort != null && inputPort.Port is ICec) if (inputPort != null && inputPort.Port is ICec)
return inputPort.Port as ICec; return inputPort.Port as ICec;
var outputPort = inputsOutputs.OutputPorts[config.ControlPortName];
if (outputPort != null && outputPort.Port is ICec) var outputPort = inputsOutputs.OutputPorts[config.ControlPortName];
return outputPort.Port as ICec; if (outputPort != null && outputPort.Port is ICec)
} return outputPort.Port as ICec;
catch (Exception ex) }
{ catch (Exception ex)
Debug.LogMessage(LogEventLevel.Debug, "GetCecPort Exception Message: {0}", ex.Message); {
Debug.LogMessage(LogEventLevel.Verbose, "GetCecPort Exception StackTrace: {0}", ex.StackTrace); Debug.LogMessage(LogEventLevel.Debug, "GetCecPort Exception Message: {0}", ex.Message);
if (ex.InnerException != null) Debug.LogMessage(LogEventLevel.Verbose, "GetCecPort Exception StackTrace: {0}", ex.StackTrace);
Debug.LogMessage(LogEventLevel.Information, "GetCecPort Exception InnerException: {0}", ex.InnerException); if (ex.InnerException != null)
} Debug.LogMessage(LogEventLevel.Information, "GetCecPort Exception InnerException: {0}", ex.InnerException);
}
Debug.LogMessage(LogEventLevel.Information, "GetCecPort: Device '{0}' does not have a CEC port called '{1}'", Debug.LogMessage(LogEventLevel.Information, "GetCecPort: Device '{0}' does not have a CEC port called '{1}'",
config.ControlPortDevKey, config.ControlPortName); config.ControlPortDevKey, config.ControlPortName);
return null; return null;
} }
/// <summary> /// <summary>
/// Helper to grab the IComPorts device for this PortDeviceKey. Key "controlSystem" will /// Helper to grab the IComPorts device for this PortDeviceKey. Key "controlSystem" will
/// return the ControlSystem object from the Global class. /// return the ControlSystem object from the Global class.
/// </summary> /// </summary>
/// <returns>IComPorts device or null if the device is not found or does not implement IComPorts</returns> /// <returns>IComPorts device or null if the device is not found or does not implement IComPorts</returns>
/// <summary> /// <summary>
/// GetIComPortsDeviceFromManagedDevice method /// GetIComPortsDeviceFromManagedDevice method
/// </summary> /// </summary>
public static IComPorts GetIComPortsDeviceFromManagedDevice(string ComPortDevKey) public static IComPorts GetIComPortsDeviceFromManagedDevice(string ComPortDevKey)
{ {
if ((ComPortDevKey.Equals("controlSystem", System.StringComparison.OrdinalIgnoreCase) if ((ComPortDevKey.Equals("controlSystem", System.StringComparison.OrdinalIgnoreCase)
@@ -210,81 +210,81 @@ namespace PepperDash.Essentials.Core
} }
} }
/// <summary> /// <summary>
/// Represents a EssentialsControlPropertiesConfig /// Represents a EssentialsControlPropertiesConfig
/// </summary> /// </summary>
public class EssentialsControlPropertiesConfig : public class EssentialsControlPropertiesConfig :
ControlPropertiesConfig ControlPropertiesConfig
{ {
[JsonProperty("comParams", NullValueHandling = NullValueHandling.Ignore)] [JsonProperty("comParams", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(ComSpecJsonConverter))] [JsonConverter(typeof(ComSpecJsonConverter))]
public ComPort.ComPortSpec? ComParams { get; set; } public ComPort.ComPortSpec? ComParams { get; set; }
[JsonProperty("cresnetId", NullValueHandling = NullValueHandling.Ignore)] [JsonProperty("cresnetId", NullValueHandling = NullValueHandling.Ignore)]
public string CresnetId { get; set; } public string CresnetId { get; set; }
/// <summary> /// <summary>
/// Attempts to provide uint conversion of string CresnetId /// Attempts to provide uint conversion of string CresnetId
/// </summary> /// </summary>
[JsonIgnore] [JsonIgnore]
public uint CresnetIdInt public uint CresnetIdInt
{ {
get get
{ {
try try
{ {
return Convert.ToUInt32(CresnetId, 16); return Convert.ToUInt32(CresnetId, 16);
} }
catch (Exception) catch (Exception)
{ {
throw new FormatException(string.Format("ERROR:Unable to convert Cresnet ID: {0} to hex. Error:\n{1}", CresnetId)); throw new FormatException(string.Format("ERROR:Unable to convert Cresnet ID: {0} to hex. Error:\n{1}", CresnetId));
} }
} }
} }
[JsonProperty("infinetId", NullValueHandling = NullValueHandling.Ignore)] [JsonProperty("infinetId", NullValueHandling = NullValueHandling.Ignore)]
/// <summary> /// <summary>
/// Gets or sets the InfinetId /// Gets or sets the InfinetId
/// </summary> /// </summary>
public string InfinetId { get; set; } public string InfinetId { get; set; }
/// <summary> /// <summary>
/// Attepmts to provide uiont conversion of string InifinetId /// Attepmts to provide uiont conversion of string InifinetId
/// </summary> /// </summary>
[JsonIgnore] [JsonIgnore]
public uint InfinetIdInt public uint InfinetIdInt
{ {
get get
{ {
try try
{ {
return Convert.ToUInt32(InfinetId, 16); return Convert.ToUInt32(InfinetId, 16);
} }
catch (Exception) catch (Exception)
{ {
throw new FormatException(string.Format("ERROR:Unable to conver Infinet ID: {0} to hex. Error:\n{1}", InfinetId)); throw new FormatException(string.Format("ERROR:Unable to conver Infinet ID: {0} to hex. Error:\n{1}", InfinetId));
} }
} }
} }
} }
/// <summary> /// <summary>
/// Represents a IrControlSpec /// Represents a IrControlSpec
/// </summary> /// </summary>
public class IrControlSpec public class IrControlSpec
{ {
/// <summary> /// <summary>
/// Gets or sets the PortDeviceKey /// Gets or sets the PortDeviceKey
/// </summary> /// </summary>
public string PortDeviceKey { get; set; } public string PortDeviceKey { get; set; }
/// <summary> /// <summary>
/// Gets or sets the PortNumber /// Gets or sets the PortNumber
/// </summary> /// </summary>
public uint PortNumber { get; set; } public uint PortNumber { get; set; }
/// <summary> /// <summary>
/// Gets or sets the File /// Gets or sets the File
/// </summary> /// </summary>
public string File { get; set; } public string File { get; set; }
} }
} }

View File

@@ -3,16 +3,29 @@ using PepperDash.Essentials.Core.Bridges;
namespace PepperDash.Essentials.Core namespace PepperDash.Essentials.Core
{ {
public abstract class EssentialsBridgeableDevice:EssentialsDevice, IBridgeAdvanced /// <summary>
/// Base class for devices that can be bridged to an EISC API.
/// </summary>
public abstract class EssentialsBridgeableDevice : EssentialsDevice, IBridgeAdvanced
{ {
/// <summary>
/// Initializes a new instance of the <see cref="EssentialsBridgeableDevice"/> class with the specified key.
/// </summary>
/// <param name="key">The unique key for the device.</param>
protected EssentialsBridgeableDevice(string key) : base(key) protected EssentialsBridgeableDevice(string key) : base(key)
{ {
} }
/// <summary>
/// Initializes a new instance of the <see cref="EssentialsBridgeableDevice"/> class with the specified key and name.
/// </summary>
/// <param name="key">The unique key for the device.</param>
/// <param name="name">The display name for the device.</param>
protected EssentialsBridgeableDevice(string key, string name) : base(key, name) protected EssentialsBridgeableDevice(string key, string name) : base(key, name)
{ {
} }
/// <inheritdoc />
public abstract void LinkToApi(BasicTriList trilist, uint joinStart, string joinMapKey, EiscApiAdvanced bridge); public abstract void LinkToApi(BasicTriList trilist, uint joinStart, string joinMapKey, EiscApiAdvanced bridge);
} }
} }