using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crestron.SimplSharp;
using Crestron.SimplSharp.CrestronSockets;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace PepperDash.Core
{
///
/// An incoming communication stream
///
public interface ICommunicationReceiver : IKeyed
{
event EventHandler BytesReceived;
event EventHandler TextReceived;
bool IsConnected { get; }
void Connect();
void Disconnect();
}
///
/// Represents a device that uses basic connection
///
public interface IBasicCommunication : ICommunicationReceiver
{
void SendText(string text);
void SendBytes(byte[] bytes);
}
///
/// For IBasicCommunication classes that have SocketStatus. GenericSshClient,
/// GenericTcpIpClient
///
public interface ISocketStatus : IBasicCommunication
{
event EventHandler ConnectionChange;
SocketStatus ClientStatus { get; }
}
public interface IAutoReconnect
{
bool AutoReconnect { get; set; }
int AutoReconnectIntervalMs { get; set; }
}
///
///
///
public enum eGenericCommMethodStatusChangeType
{
Connected, 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;
}
///
/// Stupid S+ Constructor
///
public GenericCommMethodReceiveBytesArgs() { }
}
///
///
///
public class GenericCommMethodReceiveTextArgs : EventArgs
{
public string Text { get; private set; }
public GenericCommMethodReceiveTextArgs(string text)
{
Text = text;
}
///
/// Stupid S+ Constructor
///
public GenericCommMethodReceiveTextArgs() { }
}
///
///
///
public class ComTextHelper
{
public static string GetEscapedText(byte[] bytes)
{
return String.Concat(bytes.Select(b => string.Format(@"[{0:X2}]", (int)b)).ToArray());
}
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());
}
}
}