mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-02-16 05:05:00 +00:00
feat: add CommBridge class and enhance EssentialsBridgeableDevice with new constructors
This commit is contained in:
@@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
138
src/PepperDash.Essentials.Core/Comm and IR/CommBridge.cs
Normal file
138
src/PepperDash.Essentials.Core/Comm and IR/CommBridge.cs
Normal 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)));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ using Crestron.SimplSharpPro;
|
|||||||
using Crestron.SimplSharpPro.DM;
|
using Crestron.SimplSharpPro.DM;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
using Org.BouncyCastle.Tls;
|
||||||
using PepperDash.Core;
|
using PepperDash.Core;
|
||||||
using PepperDash.Essentials.Core.Config;
|
using PepperDash.Essentials.Core.Config;
|
||||||
using Serilog.Events;
|
using Serilog.Events;
|
||||||
@@ -56,6 +57,9 @@ 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.ComBridge:
|
||||||
|
comm = new CommBridge(deviceConfig.Key + "-simpl", deviceConfig.Name + " Simpl");
|
||||||
|
break;
|
||||||
case eControlMethod.Cec:
|
case eControlMethod.Cec:
|
||||||
comm = new CecPortController(deviceConfig.Key + "-cec", GetCecPort, controlConfig);
|
comm = new CecPortController(deviceConfig.Key + "-cec", GetCecPort, controlConfig);
|
||||||
break;
|
break;
|
||||||
@@ -65,7 +69,7 @@ namespace PepperDash.Essentials.Core
|
|||||||
{
|
{
|
||||||
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;
|
||||||
@@ -107,8 +111,7 @@ 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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user