diff --git a/src/Pepperdash Core/Comm/CommunicationExtras.cs b/src/Pepperdash Core/Comm/CommunicationExtras.cs new file mode 100644 index 0000000..a5c048d --- /dev/null +++ b/src/Pepperdash Core/Comm/CommunicationExtras.cs @@ -0,0 +1,240 @@ + +using Crestron.SimplSharp.CrestronSockets; +using PepperDash.Core.Interfaces; +using System; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; + +namespace PepperDash.Core.Comm +{ + /// + /// An incoming communication stream + /// + public interface ICommunicationReceiver : IKeyed + { + /// + /// Notifies of bytes received + /// + event EventHandler BytesReceived; + /// + /// Notifies of text received + /// + event EventHandler TextReceived; + + /// + /// Indicates connection status + /// + bool IsConnected { get; } + /// + /// Connect to the device + /// + void Connect(); + /// + /// Disconnect from the device + /// + void Disconnect(); + } + + /// + /// Represents a device that uses basic connection + /// + public interface IBasicCommunication : ICommunicationReceiver + { + /// + /// Send text to the device + /// + /// + void SendText(string text); + + /// + /// Send bytes to the device + /// + /// + void SendBytes(byte[] bytes); + } + + /// + /// Represents a device that implements IBasicCommunication and IStreamDebugging + /// + public interface IBasicCommunicationWithStreamDebugging : IBasicCommunication, IStreamDebugging + { + + } + + /// + /// Represents a device with stream debugging capablities + /// + public interface IStreamDebugging + { + /// + /// Object to enable stream debugging + /// + CommunicationStreamDebugging StreamDebugging { get; } + } + + /// + /// For IBasicCommunication classes that have SocketStatus. GenericSshClient, + /// GenericTcpIpClient + /// + public interface ISocketStatus : IBasicCommunication + { + /// + /// Notifies of socket status changes + /// + event EventHandler ConnectionChange; + + /// + /// The current socket status of the client + /// + SocketStatus ClientStatus { get; } + } + + /// + /// Describes a device that implements ISocketStatus and IStreamDebugging + /// + public interface ISocketStatusWithStreamDebugging : ISocketStatus, IStreamDebugging + { + + } + + /// + /// Describes a device that can automatically attempt to reconnect + /// + public interface IAutoReconnect + { + /// + /// Enable automatic recconnect + /// + bool AutoReconnect { get; set; } + /// + /// Interval in ms to attempt automatic recconnections + /// + int AutoReconnectIntervalMs { get; set; } + } + + /// + /// + /// + public enum eGenericCommMethodStatusChangeType + { + /// + /// Connected + /// + Connected, + /// + /// Disconnected + /// + Disconnected + } + + /// + /// This delegate defines handler for IBasicCommunication status changes + /// + /// Device firing the status change + /// + public delegate void GenericCommMethodStatusHandler(IBasicCommunication comm, eGenericCommMethodStatusChangeType status); + + /// + /// + /// + public class GenericCommMethodReceiveBytesArgs : EventArgs + { + /// + /// + /// + public byte[] Bytes { get; private set; } + + /// + /// + /// + /// + public GenericCommMethodReceiveBytesArgs(byte[] bytes) + { + Bytes = bytes; + } + + /// + /// S+ Constructor + /// + public GenericCommMethodReceiveBytesArgs() { } + } + + /// + /// + /// + public class GenericCommMethodReceiveTextArgs : EventArgs + { + /// + /// + /// + public string Text { get; private set; } + /// + /// + /// + public string Delimiter { get; private set; } + /// + /// + /// + /// + public GenericCommMethodReceiveTextArgs(string text) + { + Text = text; + } + + /// + /// + /// + /// + /// + public GenericCommMethodReceiveTextArgs(string text, string delimiter) + : this(text) + { + Delimiter = delimiter; + } + + /// + /// S+ Constructor + /// + public GenericCommMethodReceiveTextArgs() { } + } + + + + /// + /// + /// + public class ComTextHelper + { + /// + /// Gets escaped text for a byte array + /// + /// + /// + public static string GetEscapedText(byte[] bytes) + { + return string.Concat(bytes.Select(b => string.Format(@"[{0:X2}]", (int)b)).ToArray()); + } + + /// + /// Gets escaped text for a string + /// + /// + /// + public static string GetEscapedText(string text) + { + var bytes = Encoding.GetEncoding(28591).GetBytes(text); + return string.Concat(bytes.Select(b => string.Format(@"[{0:X2}]", (int)b)).ToArray()); + } + + /// + /// Gets debug text for a string + /// + /// + /// + public static string GetDebugText(string text) + { + return Regex.Replace(text, @"[^\u0020-\u007E]", a => GetEscapedText(a.Value)); + } + } +} \ No newline at end of file diff --git a/src/Pepperdash Core/Interfaces/CoreInterfaces.cs b/src/Pepperdash Core/Interfaces/CoreInterfaces.cs new file mode 100644 index 0000000..dc3bcd5 --- /dev/null +++ b/src/Pepperdash Core/Interfaces/CoreInterfaces.cs @@ -0,0 +1,24 @@ +namespace PepperDash.Core.Interfaces +{ + /// + /// Unique key interface to require a unique key for the class + /// + public interface IKeyed + { + /// + /// Unique Key + /// + string Key { get; } + } + + /// + /// Named Keyed device interface. Forces the devie to have a Unique Key and a name. + /// + public interface IKeyName : IKeyed + { + /// + /// Isn't it obvious :) + /// + string Name { get; } + } +} \ No newline at end of file