using System; using System.Collections.Generic; using System.Linq; using System.Text; using Crestron.SimplSharp; using Crestron.SimplSharpPro; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Linq; using PepperDash.Essentials.Core; using PepperDash.Essentials.Devices; using PepperDash.Core; namespace PepperDash.Essentials { public static class FactoryHelper { public static string IrDriverPathPrefix = Global.FilePathPrefix + "IR" + Global.DirectorySeparator; public static void HandleUnknownType(JToken devToken, string type) { Debug.Console(0, "[Config] ERROR: Type '{0}' not found in group '{1}'", type, devToken.Value("group")); } public static void HandleDeviceCreationError(JToken devToken, Exception e) { Debug.Console(0, "[Config] ERROR creating device [{0}]: \r{1}", devToken["key"].Value(), e); Debug.Console(0, "Relevant config:\r{0}", devToken.ToString(Newtonsoft.Json.Formatting.Indented)); } /// /// Finds either the ControlSystem or a device controller that contains IR ports and /// returns a port from the hardware device /// /// /// Crestron IrPort or null if device doesn't have IR or is not found public static IrOutPortConfig GetIrPort(JToken propsToken) { var irSpec = propsToken["control"]["irSpec"]; var portDevKey = irSpec.Value("portDeviceKey"); var portNum = irSpec.Value("portNumber"); IIROutputPorts irDev = null; if (portDevKey.Equals("controlSystem", StringComparison.OrdinalIgnoreCase) || portDevKey.Equals("processor", StringComparison.OrdinalIgnoreCase)) irDev = Global.ControlSystem; else irDev = DeviceManager.GetDeviceForKey(portDevKey) as IIROutputPorts; if (irDev == null) { Debug.Console(0, "[Config] Error, device with IR ports '{0}' not found", portDevKey); return null; } if (portNum <= irDev.NumberOfIROutputPorts) // success! { var file = IrDriverPathPrefix + irSpec["file"].Value(); return new IrOutPortConfig { Port = irDev.IROutputPorts[portNum], FileName = file }; } else { Debug.Console(0, "[Config] Error, device '{0}' IR port {1} out of range", portDevKey, portNum); return null; } } /// /// Finds either the ControlSystem or a device controller that contains com ports and /// returns a port from the hardware device /// /// The Properties token from the device's config /// Crestron ComPort or null if device doesn't have IR or is not found public static ComPort GetComPort(JToken propsToken) { var portDevKey = propsToken.Value("comPortDevice"); var portNum = propsToken.Value("comPortNumber"); IComPorts comDev = null; if (portDevKey.Equals("controlSystem", StringComparison.OrdinalIgnoreCase)) comDev = Global.ControlSystem; else comDev = DeviceManager.GetDeviceForKey(portDevKey) as IComPorts; if (comDev == null) { Debug.Console(0, "[Config] Error, device with com ports '{0}' not found", portDevKey); return null; } if (portNum <= comDev.NumberOfComPorts) // success! return comDev.ComPorts[portNum]; else { Debug.Console(0, "[Config] Error, device '{0}' com port {1} out of range", portDevKey, portNum); return null; } } /// /// Returns the key if it exists or converts the name into a key /// public static string KeyOrConvertName(string key, string name) { if (string.IsNullOrEmpty(key)) return name.Replace(' ', '-'); return key; } } /// /// Wrapper to help in IR port creation /// public class IrOutPortConfig { public IROutputPort Port { get; set; } public string FileName { get; set; } } }