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 { /// /// Implements IBasicCommunication and sends all communication through an EISC /// [Description("Generic communication wrapper class for any IBasicCommunication type")] public class CommBridge : EssentialsBridgeableDevice, IBasicCommunication { private EiscApiAdvanced eisc; private IBasicCommunicationJoinMap joinMap; /// /// Event triggered when text is received through the communication bridge. /// public event EventHandler TextReceived; /// /// Event triggered when bytes are received through the communication bridge. /// public event EventHandler BytesReceived; /// /// Indicates whether the communication bridge is currently connected. /// public bool IsConnected { get; private set; } /// /// Initializes a new instance of the class. /// /// The unique key for the communication bridge. /// The display name for the communication bridge. public CommBridge(string key, string name) : base(key, name) { } /// /// Sends a byte array through the communication bridge. /// /// The byte array to send. 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)); } /// /// Sends a text string through the communication bridge. /// /// The text string to send. public void SendText(string text) { if (eisc == null) { this.LogWarning("EISC is null, cannot send text."); return; } eisc.Eisc.SetString(joinMap.SendText.JoinNumber, text); } /// /// Initiates a connection through the communication bridge. /// public void Connect() { if (eisc == null) { this.LogWarning("EISC is null, cannot connect."); return; } eisc.Eisc.SetBool(joinMap.Connect.JoinNumber, true); } /// /// Terminates the connection through the communication bridge. /// public void Disconnect() { if (eisc == null) { this.LogWarning("EISC is null, cannot disconnect."); return; } eisc.Eisc.SetBool(joinMap.Connect.JoinNumber, false); } /// 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(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))); }); } } }