using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crestron.SimplSharp;
using Crestron.SimplSharp.CrestronSockets;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
namespace PepperDash.Core
{
///
/// 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
///
[JsonProperty("isConnected")]
bool IsConnected { get; }
///
/// Connect to the device
///
void Connect();
///
/// Disconnect from the device
///
void Disconnect();
}
///
/// Defines the contract for IBasicCommunication
///
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
///
[JsonProperty("streamDebugging")]
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
///
[JsonProperty("clientStatus")]
[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
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
///
[JsonProperty("autoReconnect")]
bool AutoReconnect { get; set; }
///
/// Interval in ms to attempt automatic recconnections
///
[JsonProperty("autoReconnectIntervalMs")]
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
{
///
/// Gets or sets the Bytes
///
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
///
///
///
///
/// GetEscapedText method
///
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
///
///
///
///
/// GetDebugText method
///
public static string GetDebugText(string text)
{
return Regex.Replace(text, @"[^\u0020-\u007E]", a => GetEscapedText(a.Value));
}
}
}