using System; using System.Collections.Generic; using System.Linq; using System.Text; using Crestron.SimplSharp; using PepperDash.Core; using PepperDash.Essentials.Core.Config; using Serilog.Events; namespace PepperDash.Essentials.Core { /// /// Represents a ConsoleCommMockDevice /// public class ConsoleCommMockDevice : EssentialsDevice, ICommunicationMonitor { /// /// Gets or sets the Communication /// public IBasicCommunication Communication { get; private set; } /// /// Gets or sets the PortGather /// public CommunicationGather PortGather { get; private set; } /// /// Gets or sets the CommunicationMonitor /// public StatusMonitorBase CommunicationMonitor { get; private set; } /// /// Gets or sets the LineEnding /// public string LineEnding { get; set; } /// /// Gets or sets the ShowHexResponse /// public bool ShowHexResponse { get; set; } public ConsoleCommMockDevice(string key, string name, ConsoleCommMockDevicePropertiesConfig props, IBasicCommunication comm) :base(key, name) { Communication = comm; PortGather = new CommunicationGather(Communication, '\x0d'); //PortGather.LineReceived += this.Port_LineReceived; CommunicationMonitor = new GenericCommunicationMonitor(this, Communication, props.CommunicationMonitorProperties); LineEnding = props.LineEnding; } /// /// CustomActivate method /// /// public override bool CustomActivate() { Communication.Connect(); CommunicationMonitor.StatusChange += (o, a) => { Debug.LogMessage(LogEventLevel.Verbose, this, "Communication monitor state: {0}", CommunicationMonitor.Status); }; CommunicationMonitor.Start(); CrestronConsole.AddNewConsoleCommand(SendLine, "send" + Key, "", ConsoleAccessLevelEnum.AccessOperator); CrestronConsole.AddNewConsoleCommand(s => Communication.Connect(), "con" + Key, "", ConsoleAccessLevelEnum.AccessOperator); return true; } void SendLine(string s) { //if (Debug.Level == 2) // Debug.LogMessage(LogEventLevel.Verbose, this, " Send '{0}'", ComTextHelper.GetEscapedText(s)); Communication.SendText(s + LineEnding); } } /// /// Represents a ConsoleCommMockDevicePropertiesConfig /// public class ConsoleCommMockDevicePropertiesConfig { /// /// Gets or sets the LineEnding /// public string LineEnding { get; set; } /// /// Gets or sets the CommunicationMonitorProperties /// public CommunicationMonitorConfig CommunicationMonitorProperties { get; set; } public ConsoleCommMockDevicePropertiesConfig() { LineEnding = "\x0a"; } } /// /// Represents a ConsoleCommMockDeviceFactory /// public class ConsoleCommMockDeviceFactory : EssentialsDeviceFactory { public ConsoleCommMockDeviceFactory() { TypeNames = new List() { "commmock" }; } /// /// BuildDevice method /// /// public override EssentialsDevice BuildDevice(DeviceConfig dc) { Debug.LogMessage(LogEventLevel.Debug, "Factory Attempting to create new Comm Mock Device"); var comm = CommFactory.CreateCommForDevice(dc); var props = Newtonsoft.Json.JsonConvert.DeserializeObject( dc.Properties.ToString()); return new ConsoleCommMockDevice(dc.Key, dc.Name, props, comm); } } }