From 0ade04d0c7e19a191862920ffc6218c0e2310261 Mon Sep 17 00:00:00 2001 From: Jason DeVito Date: Sun, 29 Sep 2019 08:28:18 -0500 Subject: [PATCH 1/9] Updated JsonToSimpMaster.cs and EventArgs and Constants.cs to implement passing resolved file path and filename to SIMPL. All edits are flagged with TODO: pdc-20 for easier review. --- .../JsonToSimpl/EventArgs and Constants.cs | 248 ++++++------ .../JsonToSimpl/JsonToSimplFileMaster.cs | 371 +++++++++--------- 2 files changed, 320 insertions(+), 299 deletions(-) diff --git a/Pepperdash Core/Pepperdash Core/JsonToSimpl/EventArgs and Constants.cs b/Pepperdash Core/Pepperdash Core/JsonToSimpl/EventArgs and Constants.cs index bbbf7e2..4fa4dee 100644 --- a/Pepperdash Core/Pepperdash Core/JsonToSimpl/EventArgs and Constants.cs +++ b/Pepperdash Core/Pepperdash Core/JsonToSimpl/EventArgs and Constants.cs @@ -1,125 +1,129 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Crestron.SimplSharp; - -namespace PepperDash.Core.JsonToSimpl -{ - /// - /// Constants for Simpl modules - /// - public class JsonToSimplConstants - { +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; + +namespace PepperDash.Core.JsonToSimpl +{ + /// + /// Constants for Simpl modules + /// + public class JsonToSimplConstants + { public const ushort JsonIsValidBoolChange = 2; - - - public const ushort BoolValueChange = 1; - public const ushort UshortValueChange = 101; - public const ushort StringValueChange = 201; + + + public const ushort BoolValueChange = 1; + public const ushort UshortValueChange = 101; + public const ushort StringValueChange = 201; public const ushort FullPathToArrayChange = 202; - public const ushort ActualFilePathChange = 203; - } - - //**************************************************************************************************// - public delegate void SPlusValuesDelegate(); - - public class SPlusValueWrapper - { - public SPlusType ValueType { get; private set; } - public ushort Index { get; private set; } - public ushort BoolUShortValue { get; set; } - public string StringValue { get; set; } - - public SPlusValueWrapper() { } - - public SPlusValueWrapper(SPlusType type, ushort index) - { - ValueType = type; - Index = index; - } - } - - public enum SPlusType - { - Digital, Analog, String - } - - - //**************************************************************************************************// - public class BoolChangeEventArgs : EventArgs - { - public bool State { get; set; } - public ushort IntValue { get { return (ushort)(State ? 1 : 0); } } - public ushort Type { get; set; } - public ushort Index { get; set; } - - public BoolChangeEventArgs() - { - } - - public BoolChangeEventArgs(bool state, ushort type) - { - State = state; - Type = type; - } - - public BoolChangeEventArgs(bool state, ushort type, ushort index) - { - State = state; - Type = type; - Index = index; - } - } - - //**************************************************************************************************// - public class UshrtChangeEventArgs : EventArgs - { - public ushort IntValue { get; set; } - public ushort Type { get; set; } - public ushort Index { get; set; } - - public UshrtChangeEventArgs() - { - } - - public UshrtChangeEventArgs(ushort intValue, ushort type) - { - IntValue = intValue; - Type = type; - } - - public UshrtChangeEventArgs(ushort intValue, ushort type, ushort index) - { - IntValue = intValue; - Type = type; - Index = index; - } - } - - //**************************************************************************************************// - public class StringChangeEventArgs : EventArgs - { - public string StringValue { get; set; } - public ushort Type { get; set; } - public ushort Index { get; set; } - - public StringChangeEventArgs() - { - } - - public StringChangeEventArgs(string stringValue, ushort type) - { - StringValue = stringValue; - Type = type; - } - - public StringChangeEventArgs(string stringValue, ushort type, ushort index) - { - StringValue = stringValue; - Type = type; - Index = index; - } - - } + public const ushort ActualFilePathChange = 203; + + // TODO: pdc-20: Added below constants for passing file path and filename back to S+ + public const ushort FilenameResolvedChange = 204; + public const ushort FilePathResolvedChange = 205; + } + + //**************************************************************************************************// + public delegate void SPlusValuesDelegate(); + + public class SPlusValueWrapper + { + public SPlusType ValueType { get; private set; } + public ushort Index { get; private set; } + public ushort BoolUShortValue { get; set; } + public string StringValue { get; set; } + + public SPlusValueWrapper() { } + + public SPlusValueWrapper(SPlusType type, ushort index) + { + ValueType = type; + Index = index; + } + } + + public enum SPlusType + { + Digital, Analog, String + } + + + //**************************************************************************************************// + public class BoolChangeEventArgs : EventArgs + { + public bool State { get; set; } + public ushort IntValue { get { return (ushort)(State ? 1 : 0); } } + public ushort Type { get; set; } + public ushort Index { get; set; } + + public BoolChangeEventArgs() + { + } + + public BoolChangeEventArgs(bool state, ushort type) + { + State = state; + Type = type; + } + + public BoolChangeEventArgs(bool state, ushort type, ushort index) + { + State = state; + Type = type; + Index = index; + } + } + + //**************************************************************************************************// + public class UshrtChangeEventArgs : EventArgs + { + public ushort IntValue { get; set; } + public ushort Type { get; set; } + public ushort Index { get; set; } + + public UshrtChangeEventArgs() + { + } + + public UshrtChangeEventArgs(ushort intValue, ushort type) + { + IntValue = intValue; + Type = type; + } + + public UshrtChangeEventArgs(ushort intValue, ushort type, ushort index) + { + IntValue = intValue; + Type = type; + Index = index; + } + } + + //**************************************************************************************************// + public class StringChangeEventArgs : EventArgs + { + public string StringValue { get; set; } + public ushort Type { get; set; } + public ushort Index { get; set; } + + public StringChangeEventArgs() + { + } + + public StringChangeEventArgs(string stringValue, ushort type) + { + StringValue = stringValue; + Type = type; + } + + public StringChangeEventArgs(string stringValue, ushort type, ushort index) + { + StringValue = stringValue; + Type = type; + Index = index; + } + + } } \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/JsonToSimpl/JsonToSimplFileMaster.cs b/Pepperdash Core/Pepperdash Core/JsonToSimpl/JsonToSimplFileMaster.cs index bd97267..0047c6c 100644 --- a/Pepperdash Core/Pepperdash Core/JsonToSimpl/JsonToSimplFileMaster.cs +++ b/Pepperdash Core/Pepperdash Core/JsonToSimpl/JsonToSimplFileMaster.cs @@ -1,178 +1,195 @@ -using System; -//using System.IO; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Crestron.SimplSharp; -using Crestron.SimplSharp.CrestronIO; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; - -namespace PepperDash.Core.JsonToSimpl -{ - public class JsonToSimplFileMaster : JsonToSimplMaster - { - /// - /// Sets the filepath as well as registers this with the Global.Masters list - /// - public string Filepath { get; private set; } - - public string ActualFilePath { get; private set; } - - /*****************************************************************************************/ - /** Privates **/ - - - // The JSON file in JObject form - // For gathering the incoming data - object StringBuilderLock = new object(); - // To prevent multiple same-file access - static object FileLock = new object(); - - /*****************************************************************************************/ - - /// - /// SIMPL+ default constructor. - /// - public JsonToSimplFileMaster() - { - } - - /// - /// Read, evaluate and udpate status - /// - public void EvaluateFile(string filepath) - { - Filepath = filepath; - - OnBoolChange(false, 0, JsonToSimplConstants.JsonIsValidBoolChange); - if (string.IsNullOrEmpty(Filepath)) - { - CrestronConsole.PrintLine("Cannot evaluate file. JSON file path not set"); - return; - } - - // Resolve wildcard - var dir = Path.GetDirectoryName(Filepath); - Debug.Console(1, "Checking directory {0}", dir); - var fileName = Path.GetFileName(Filepath); - var directory = new DirectoryInfo(dir); - - var actualFile = directory.GetFiles(fileName).FirstOrDefault(); - if(actualFile == null) - { - var msg = string.Format("JSON file not found: {0}", Filepath); - CrestronConsole.PrintLine(msg); - ErrorLog.Error(msg); - return; - } - //var actualFileName = actualFile.FullName; +using System; +//using System.IO; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; +using Crestron.SimplSharp.CrestronIO; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace PepperDash.Core.JsonToSimpl +{ + public class JsonToSimplFileMaster : JsonToSimplMaster + { + /// + /// Sets the filepath as well as registers this with the Global.Masters list + /// + public string Filepath { get; private set; } + + public string ActualFilePath { get; private set; } + + // TODO: pdc-20: added to return filename back to SIMPL + public string Filename { get; private set; } + public string FilePathName { get; private set; } + + /*****************************************************************************************/ + /** Privates **/ + + + // The JSON file in JObject form + // For gathering the incoming data + object StringBuilderLock = new object(); + // To prevent multiple same-file access + static object FileLock = new object(); + + /*****************************************************************************************/ + + /// + /// SIMPL+ default constructor. + /// + public JsonToSimplFileMaster() + { + } + + /// + /// Read, evaluate and udpate status + /// + public void EvaluateFile(string filepath) + { + Filepath = filepath; + + OnBoolChange(false, 0, JsonToSimplConstants.JsonIsValidBoolChange); + if (string.IsNullOrEmpty(Filepath)) + { + CrestronConsole.PrintLine("Cannot evaluate file. JSON file path not set"); + return; + } + + // Resolve wildcard + var dir = Path.GetDirectoryName(Filepath); + Debug.Console(1, "Checking directory {0}", dir); + var fileName = Path.GetFileName(Filepath); + var directory = new DirectoryInfo(dir); + + var actualFile = directory.GetFiles(fileName).FirstOrDefault(); + if (actualFile == null) + { + var msg = string.Format("JSON file not found: {0}", Filepath); + CrestronConsole.PrintLine(msg); + ErrorLog.Error(msg); + return; + } + + // \xSE\xR\PDT000-Template_Main_Config-Combined_DSP_v00.02.json + // \USER\PDT000-Template_Main_Config-Combined_DSP_v00.02.json ActualFilePath = actualFile.FullName; - OnStringChange(ActualFilePath, 0, JsonToSimplConstants.ActualFilePathChange); - Debug.Console(1, "Actual JSON file is {0}", ActualFilePath); - - string json = File.ReadToEnd(ActualFilePath, System.Text.Encoding.ASCII); - - try - { - JsonObject = JObject.Parse(json); - foreach (var child in Children) - child.ProcessAll(); - OnBoolChange(true, 0, JsonToSimplConstants.JsonIsValidBoolChange); - } - catch (Exception e) - { - var msg = string.Format("JSON parsing failed:\r{0}", e); - CrestronConsole.PrintLine(msg); - ErrorLog.Error(msg); - return; - } - } - public void setDebugLevel(int level) { - Debug.SetDebugLevel(level); - } - public override void Save() - { - // this code is duplicated in the other masters!!!!!!!!!!!!! - UnsavedValues = new Dictionary(); - // Make each child update their values into master object - foreach (var child in Children) - { - Debug.Console(1, "Master [{0}] checking child [{1}] for updates to save", UniqueID, child.Key); - child.UpdateInputsForMaster(); - } - - if (UnsavedValues == null || UnsavedValues.Count == 0) - { - Debug.Console(1, "Master [{0}] No updated values to save. Skipping", UniqueID); - return; - } - lock (FileLock) - { - Debug.Console(1, "Saving"); - foreach (var path in UnsavedValues.Keys) - { - var tokenToReplace = JsonObject.SelectToken(path); - if (tokenToReplace != null) - {// It's found - tokenToReplace.Replace(UnsavedValues[path]); - Debug.Console(1, "JSON Master[{0}] Updating '{1}'", UniqueID, path); - } - else // No token. Let's make one - { - //http://stackoverflow.com/questions/17455052/how-to-set-the-value-of-a-json-path-using-json-net - Debug.Console(1, "JSON Master[{0}] Cannot write value onto missing property: '{1}'", UniqueID, path); - -// JContainer jpart = JsonObject; -// // walk down the path and find where it goes -//#warning Does not handle arrays. -// foreach (var part in path.Split('.')) -// { - -// var openPos = part.IndexOf('['); -// if (openPos > -1) -// { -// openPos++; // move to number -// var closePos = part.IndexOf(']'); -// var arrayName = part.Substring(0, openPos - 1); // get the name -// var index = Convert.ToInt32(part.Substring(openPos, closePos - openPos)); - -// // Check if the array itself exists and add the item if so -// if (jpart[arrayName] != null) -// { -// var arrayObj = jpart[arrayName] as JArray; -// var item = arrayObj[index]; -// if (item == null) -// arrayObj.Add(new JObject()); -// } - -// Debug.Console(0, "IGNORING MISSING ARRAY VALUE FOR NOW"); -// continue; -// } -// // Build the -// if (jpart[part] == null) -// jpart.Add(new JProperty(part, new JObject())); -// jpart = jpart[part] as JContainer; -// } -// jpart.Replace(UnsavedValues[path]); - } - } - using (StreamWriter sw = new StreamWriter(ActualFilePath)) - { - try - { - sw.Write(JsonObject.ToString()); - sw.Flush(); - } - catch (Exception e) - { - string err = string.Format("Error writing JSON file:\r{0}", e); - Debug.Console(0, err); - ErrorLog.Warn(err); - return; - } - } - } - } - } -} + OnStringChange(ActualFilePath, 0, JsonToSimplConstants.ActualFilePathChange); + Debug.Console(1, "Actual JSON file is {0}", ActualFilePath); + + // TODO: pdc-20: added to retrun filename to SIMPL + Filename = actualFile.Name; + OnStringChange(Filename, 0, JsonToSimplConstants.FilenameResolvedChange); + Debug.Console(1, "JSON Filename is {0}", Filename); + + // TODO: pdc-20: added to return the file path to SIMPL + FilePathName = string.Format(@"{0}\", actualFile.DirectoryName); + OnStringChange(FilePathName, 0, JsonToSimplConstants.FilePathResolvedChange); + Debug.Console(1, "JSON File Path is {0}", FilePathName); + + string json = File.ReadToEnd(ActualFilePath, System.Text.Encoding.ASCII); + + try + { + JsonObject = JObject.Parse(json); + foreach (var child in Children) + child.ProcessAll(); + OnBoolChange(true, 0, JsonToSimplConstants.JsonIsValidBoolChange); + } + catch (Exception e) + { + var msg = string.Format("JSON parsing failed:\r{0}", e); + CrestronConsole.PrintLine(msg); + ErrorLog.Error(msg); + return; + } + } + public void setDebugLevel(int level) + { + Debug.SetDebugLevel(level); + } + public override void Save() + { + // this code is duplicated in the other masters!!!!!!!!!!!!! + UnsavedValues = new Dictionary(); + // Make each child update their values into master object + foreach (var child in Children) + { + Debug.Console(1, "Master [{0}] checking child [{1}] for updates to save", UniqueID, child.Key); + child.UpdateInputsForMaster(); + } + + if (UnsavedValues == null || UnsavedValues.Count == 0) + { + Debug.Console(1, "Master [{0}] No updated values to save. Skipping", UniqueID); + return; + } + lock (FileLock) + { + Debug.Console(1, "Saving"); + foreach (var path in UnsavedValues.Keys) + { + var tokenToReplace = JsonObject.SelectToken(path); + if (tokenToReplace != null) + {// It's found + tokenToReplace.Replace(UnsavedValues[path]); + Debug.Console(1, "JSON Master[{0}] Updating '{1}'", UniqueID, path); + } + else // No token. Let's make one + { + //http://stackoverflow.com/questions/17455052/how-to-set-the-value-of-a-json-path-using-json-net + Debug.Console(1, "JSON Master[{0}] Cannot write value onto missing property: '{1}'", UniqueID, path); + + // JContainer jpart = JsonObject; + // // walk down the path and find where it goes + //#warning Does not handle arrays. + // foreach (var part in path.Split('.')) + // { + + // var openPos = part.IndexOf('['); + // if (openPos > -1) + // { + // openPos++; // move to number + // var closePos = part.IndexOf(']'); + // var arrayName = part.Substring(0, openPos - 1); // get the name + // var index = Convert.ToInt32(part.Substring(openPos, closePos - openPos)); + + // // Check if the array itself exists and add the item if so + // if (jpart[arrayName] != null) + // { + // var arrayObj = jpart[arrayName] as JArray; + // var item = arrayObj[index]; + // if (item == null) + // arrayObj.Add(new JObject()); + // } + + // Debug.Console(0, "IGNORING MISSING ARRAY VALUE FOR NOW"); + // continue; + // } + // // Build the + // if (jpart[part] == null) + // jpart.Add(new JProperty(part, new JObject())); + // jpart = jpart[part] as JContainer; + // } + // jpart.Replace(UnsavedValues[path]); + } + } + using (StreamWriter sw = new StreamWriter(ActualFilePath)) + { + try + { + sw.Write(JsonObject.ToString()); + sw.Flush(); + } + catch (Exception e) + { + string err = string.Format("Error writing JSON file:\r{0}", e); + Debug.Console(0, err); + ErrorLog.Warn(err); + return; + } + } + } + } + } +} From c9b06ec3caccedc2d2af5eae8b820a5fa75ed58f Mon Sep 17 00:00:00 2001 From: Jason DeVito Date: Tue, 1 Oct 2019 13:54:26 -0500 Subject: [PATCH 2/9] Checked out PDC-20, implemented updates in PDC-25, committing back to PDC-25 --- .../EventArgs and Constants.cs | 69 +++++ .../JsonStandardObjects/JsonToSimplDevice.cs | 293 ++++++++++++++++++ .../Pepperdash Core/PepperDash_Core.csproj | 2 + 3 files changed, 364 insertions(+) create mode 100644 Pepperdash Core/Pepperdash Core/JsonStandardObjects/EventArgs and Constants.cs create mode 100644 Pepperdash Core/Pepperdash Core/JsonStandardObjects/JsonToSimplDevice.cs diff --git a/Pepperdash Core/Pepperdash Core/JsonStandardObjects/EventArgs and Constants.cs b/Pepperdash Core/Pepperdash Core/JsonStandardObjects/EventArgs and Constants.cs new file mode 100644 index 0000000..f4a3118 --- /dev/null +++ b/Pepperdash Core/Pepperdash Core/JsonStandardObjects/EventArgs and Constants.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; + +namespace PepperDash.Core.JsonStandardObjects +{ + #region Constants + + /// + /// Constants for simpl modules + /// + public class JsonStandardDeviceConstants + { + public const ushort JsonObjectEvaluated = 2; + + public const ushort JsonObjectChanged = 104; + } + + #endregion Constants + + + #region ObjectChangeEventArgs + + /// + /// + /// + public class ObjectChangeEventArgs : EventArgs + { + public DeviceConfig Device { get; set; } + public ushort Type { get; set; } + public ushort Index { get; set; } + + /// + /// Default constructor + /// + public ObjectChangeEventArgs() + { + + } + + /// + /// Constructor overload + /// + /// + /// + public ObjectChangeEventArgs(DeviceConfig device, ushort type) + { + Device = device; + Type = type; + } + + /// + /// Constructor overload + /// + /// + /// + /// + public ObjectChangeEventArgs(DeviceConfig device, ushort type, ushort index) + { + Device = device; + Type = type; + Index = index; + } + } + + #endregion ObjectChangeEventArgs +} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/JsonStandardObjects/JsonToSimplDevice.cs b/Pepperdash Core/Pepperdash Core/JsonStandardObjects/JsonToSimplDevice.cs new file mode 100644 index 0000000..139f215 --- /dev/null +++ b/Pepperdash Core/Pepperdash Core/JsonStandardObjects/JsonToSimplDevice.cs @@ -0,0 +1,293 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; +using PepperDash.Core.JsonToSimpl; + +namespace PepperDash.Core.JsonStandardObjects +{ + /* + Convert JSON snippt to C#: http://json2csharp.com/# + + JSON Snippet: + { + "devices": [ + { + "key": "deviceKey", + "name": "deviceName", + "type": "deviceType", + "properties": { + "deviceId": 1, + "enabled": true, + "control": { + "method": "methodName", + "controlPortDevKey": "deviceControlPortDevKey", + "controlPortNumber": 1, + "comParams": { + "baudRate": 9600, + "dataBits": 8, + "stopBits": 1, + "parity": "None", + "protocol": "RS232", + "hardwareHandshake": "None", + "softwareHandshake": "None", + "pacing": 0 + }, + "tcpSshProperties": { + "address": "172.22.1.101", + "port": 23, + "username": "user01", + "password": "password01", + "autoReconnect": false, + "autoReconnectIntervalMs": 10000 + } + } + } + } + ] + } + */ + + /// + /// Device class + /// + public class DeviceConfig + { + /// + /// JSON config key property + /// + public string key { get; set; } + /// + /// JSON config name property + /// + public string name { get; set; } + /// + /// JSON config type property + /// + public string type { get; set; } + /// + /// JSON config properties + /// + public PropertiesConfig properties { get; set; } + + /// + /// Bool change event handler + /// + public event EventHandler BoolChange; + /// + /// Ushort change event handler + /// + public event EventHandler UshrtChange; + /// + /// String change event handler + /// + public event EventHandler StringChange; + /// + /// Object change event handler + /// + public event EventHandler ObjectChange; + + /// + /// Constructor + /// + public DeviceConfig() + { + // add logic here if necessary + } + + /// + /// Initialize Device Module + /// + /// JSON master unique ID + /// Device key to search for + public void Initialize(string uniqueID, string key) + { + // S+ set EvaluateFb low + OnBoolChange(false, 0, JsonStandardDeviceConstants.JsonObjectEvaluated); + // validate parameters + if (string.IsNullOrEmpty(uniqueID)) + { + Debug.Console(1, "UniqueID is null or empty"); + return; + } + if (string.IsNullOrEmpty(key)) + { + Debug.Console(1, "Device key is null or empty"); + return; + } + + try + { + // get the file using the unique ID + JsonToSimplMaster jsonMaster = J2SGlobal.GetMasterByFile(uniqueID); + var device = jsonMaster.JsonObject.ToObject().devices.FirstOrDefault(d => d.key.Equals(key)); + + name = device.name; + type = device.type; + properties = device.properties; + // Pass object to S+ + OnObjectChange(this, 0, JsonStandardDeviceConstants.JsonObjectChanged); + + } + catch (Exception e) + { + var msg = string.Format("Device lookup failed:\r{0}", e); + CrestronConsole.PrintLine(msg); + ErrorLog.Error(msg); + return; + } + + // S+ set EvaluteFb high + OnBoolChange(true, 0, JsonStandardDeviceConstants.JsonObjectEvaluated); + } + + #region EventHandler Helpers + + /// + /// BoolChange event handler helper + /// + /// + /// + /// + protected void OnBoolChange(bool state, ushort index, ushort type) + { + if (BoolChange != null) + { + var args = new BoolChangeEventArgs(state, type); + args.Index = index; + BoolChange(this, args); + } + } + + /// + /// UshrtChange event handler helper + /// + /// + /// + /// + protected void OnUshrtChange(ushort state, ushort index, ushort type) + { + if (UshrtChange != null) + { + var args = new UshrtChangeEventArgs(state, type); + args.Index = index; + UshrtChange(this, args); + } + } + + /// + /// StringChange event handler helper + /// + /// + /// + /// + protected void OnStringChange(string value, ushort index, ushort type) + { + if (StringChange != null) + { + var args = new StringChangeEventArgs(value, type); + args.Index = index; + StringChange(this, args); + } + } + + /// + /// ObjectChange event handler helper + /// + /// + /// + /// + protected void OnObjectChange(DeviceConfig device, ushort index, ushort type) + { + if (ObjectChange != null) + { + var args = new ObjectChangeEventArgs(device, type); + args.Index = index; + ObjectChange(this, args); + } + } + + #endregion EventHandler Helpers + } + + #region JSON Configuration Classes + + /// + /// Device communication parameter class + /// + public class ComParamsConfig + { + public int baudRate { get; set; } + public int dataBits { get; set; } + public int stopBits { get; set; } + public string parity { get; set; } + public string protocol { get; set; } + public string hardwareHandshake { get; set; } + public string softwareHandshake { get; set; } + public int pacing { get; set; } + + // convert properties for simpl + public ushort simplBaudRate { get { return (ushort)Convert.ToUInt16(baudRate); } } + public ushort simplDataBits { get { return Convert.ToUInt16(dataBits); } } + public ushort simplStopBits { get { return Convert.ToUInt16(stopBits); } } + public ushort simplPacing { get { return Convert.ToUInt16(pacing); } } + } + + /// + /// Device TCP/SSH properties class + /// + public class TcpSshPropertiesConfig + { + public string address { get; set; } + public int port { get; set; } + public string username { get; set; } + public string password { get; set; } + public bool autoReconnect { get; set; } + public int autoReconnectIntervalMs { get; set; } + + // convert properties for simpl + public ushort simplPort { get { return (ushort)Convert.ToUInt16(port); } } + public ushort simplAutoReconnect { get { return (ushort)(autoReconnect ? 1 : 0); } } + public ushort simplAutoReconnectIntervalMs { get { return Convert.ToUInt16(autoReconnectIntervalMs); } } + } + + /// + /// Device control class + /// + public class ControlConfig + { + public string method { get; set; } + public string controlPortDevKey { get; set; } + public int controlPortNumber { get; set; } + public ComParamsConfig comParams { get; set; } + public TcpSshPropertiesConfig tcpSshProperties { get; set; } + + // convert properties for simpl + public ushort simplControlPortNumber { get { return Convert.ToUInt16(controlPortNumber); } } + } + + /// + /// Device properties class + /// + public class PropertiesConfig + { + public int deviceId { get; set; } + public bool enabled { get; set; } + public ControlConfig control { get; set; } + + // convert properties for simpl + public ushort simplDeviceId { get { return Convert.ToUInt16(deviceId); } } + public ushort simplEnabled { get { return (ushort)(enabled ? 1 : 0); } } + } + + /// + /// Root device class + /// + public class RootObject + { + public List devices { get; set; } + } + + #endregion JSON Configuration Classes +} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj b/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj index 6c7960d..04bce68 100644 --- a/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj +++ b/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj @@ -86,6 +86,8 @@ + + From efe60b3ae0838baa78add5563fd331e3decff09c Mon Sep 17 00:00:00 2001 From: Jason T Alborough Date: Wed, 2 Oct 2019 15:55:17 -0400 Subject: [PATCH 3/9] Adds RecivedFromCLientIndexUshort to GenericTcpServerCommMethodRecieveTextArgs --- Pepperdash Core/Pepperdash Core/Comm/EventArgs.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Pepperdash Core/Pepperdash Core/Comm/EventArgs.cs b/Pepperdash Core/Pepperdash Core/Comm/EventArgs.cs index e9e9f1f..ecf6db8 100644 --- a/Pepperdash Core/Pepperdash Core/Comm/EventArgs.cs +++ b/Pepperdash Core/Pepperdash Core/Comm/EventArgs.cs @@ -76,6 +76,14 @@ namespace PepperDash.Core public class GenericTcpServerCommMethodReceiveTextArgs : EventArgs { public uint ReceivedFromClientIndex { get; private set; } + public ushort ReceivedFromClientIndexShort + { + get + { + return (ushort)ReceivedFromClientIndex; + } + } + public string Text { get; private set; } public GenericTcpServerCommMethodReceiveTextArgs(string text) From 9ba46eb3d5df6e35eca3775a1eb48fde34330ba6 Mon Sep 17 00:00:00 2001 From: Jason DeVito Date: Wed, 2 Oct 2019 18:38:35 -0500 Subject: [PATCH 4/9] Completed development and testing of PDC-25. Reviewed with Heath, tweaked a few things for better efficency, I have a sample program available for review --- .../JsonStandardObjects/JsonToSimplDevice.cs | 101 ++- .../JsonToSimplArrayLookupChild.cs | 226 +++--- .../JsonToSimpl/JsonToSimplChildObjectBase.cs | 650 +++++++++--------- .../Pepperdash Core/PepperDash_Core.csproj | 2 + 4 files changed, 513 insertions(+), 466 deletions(-) diff --git a/Pepperdash Core/Pepperdash Core/JsonStandardObjects/JsonToSimplDevice.cs b/Pepperdash Core/Pepperdash Core/JsonStandardObjects/JsonToSimplDevice.cs index 139f215..7ad1a89 100644 --- a/Pepperdash Core/Pepperdash Core/JsonStandardObjects/JsonToSimplDevice.cs +++ b/Pepperdash Core/Pepperdash Core/JsonStandardObjects/JsonToSimplDevice.cs @@ -3,10 +3,12 @@ using System.Collections.Generic; using System.Linq; using System.Text; using Crestron.SimplSharp; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using PepperDash.Core.JsonToSimpl; namespace PepperDash.Core.JsonStandardObjects -{ +{ /* Convert JSON snippt to C#: http://json2csharp.com/# @@ -52,8 +54,8 @@ namespace PepperDash.Core.JsonStandardObjects /// /// Device class /// - public class DeviceConfig - { + public class DeviceConfig : JsonToSimplChildObjectBase + { /// /// JSON config key property /// @@ -93,53 +95,63 @@ namespace PepperDash.Core.JsonStandardObjects /// public DeviceConfig() { - // add logic here if necessary + properties = new PropertiesConfig(); } /// - /// Initialize Device Module + /// Initialize method /// - /// JSON master unique ID - /// Device key to search for - public void Initialize(string uniqueID, string key) + /// + /// + public void Initialize(string uniqueID, string deviceKey) { // S+ set EvaluateFb low OnBoolChange(false, 0, JsonStandardDeviceConstants.JsonObjectEvaluated); // validate parameters - if (string.IsNullOrEmpty(uniqueID)) + if (string.IsNullOrEmpty(uniqueID) || string.IsNullOrEmpty(deviceKey)) { - Debug.Console(1, "UniqueID is null or empty"); - return; - } - if (string.IsNullOrEmpty(key)) - { - Debug.Console(1, "Device key is null or empty"); + Debug.Console(1, "UniqueID ({0} or key ({1} is null or empty", uniqueID, deviceKey); + // S+ set EvaluteFb high + OnBoolChange(true, 0, JsonStandardDeviceConstants.JsonObjectEvaluated); return; } + key = deviceKey; + try { // get the file using the unique ID JsonToSimplMaster jsonMaster = J2SGlobal.GetMasterByFile(uniqueID); - var device = jsonMaster.JsonObject.ToObject().devices.FirstOrDefault(d => d.key.Equals(key)); + if (jsonMaster == null) + { + Debug.Console(1, "Could not find JSON file with uniqueID {0}", uniqueID); + return; + } - name = device.name; - type = device.type; - properties = device.properties; - // Pass object to S+ - OnObjectChange(this, 0, JsonStandardDeviceConstants.JsonObjectChanged); + // get the device configuration using the key + var devices = jsonMaster.JsonObject.ToObject().devices; + var device = devices.FirstOrDefault(d => d.key.Equals(key)); + if (device == null) + { + Debug.Console(1, "Could not find device with key {0}", key); + return; + } + OnObjectChange(device, 0, JsonStandardDeviceConstants.JsonObjectChanged); + var index = devices.IndexOf(device); + OnStringChange(string.Format("devices[{0}]", index), 0, JsonToSimplConstants.FullPathToArrayChange); } catch (Exception e) { - var msg = string.Format("Device lookup failed:\r{0}", e); + var msg = string.Format("Device {0} lookup failed:\r{1}", key, e); CrestronConsole.PrintLine(msg); ErrorLog.Error(msg); - return; } - - // S+ set EvaluteFb high - OnBoolChange(true, 0, JsonStandardDeviceConstants.JsonObjectEvaluated); + finally + { + // S+ set EvaluteFb high + OnBoolChange(true, 0, JsonStandardDeviceConstants.JsonObjectEvaluated); + } } #region EventHandler Helpers @@ -228,10 +240,18 @@ namespace PepperDash.Core.JsonStandardObjects public int pacing { get; set; } // convert properties for simpl - public ushort simplBaudRate { get { return (ushort)Convert.ToUInt16(baudRate); } } + public ushort simplBaudRate { get { return Convert.ToUInt16(baudRate); } } public ushort simplDataBits { get { return Convert.ToUInt16(dataBits); } } public ushort simplStopBits { get { return Convert.ToUInt16(stopBits); } } public ushort simplPacing { get { return Convert.ToUInt16(pacing); } } + + /// + /// Constructor + /// + public ComParamsConfig() + { + + } } /// @@ -247,9 +267,17 @@ namespace PepperDash.Core.JsonStandardObjects public int autoReconnectIntervalMs { get; set; } // convert properties for simpl - public ushort simplPort { get { return (ushort)Convert.ToUInt16(port); } } + public ushort simplPort { get { return Convert.ToUInt16(port); } } public ushort simplAutoReconnect { get { return (ushort)(autoReconnect ? 1 : 0); } } public ushort simplAutoReconnectIntervalMs { get { return Convert.ToUInt16(autoReconnectIntervalMs); } } + + /// + /// Constructor + /// + public TcpSshPropertiesConfig() + { + + } } /// @@ -265,6 +293,15 @@ namespace PepperDash.Core.JsonStandardObjects // convert properties for simpl public ushort simplControlPortNumber { get { return Convert.ToUInt16(controlPortNumber); } } + + /// + /// Constructor + /// + public ControlConfig() + { + comParams = new ComParamsConfig(); + tcpSshProperties = new TcpSshPropertiesConfig(); + } } /// @@ -279,6 +316,14 @@ namespace PepperDash.Core.JsonStandardObjects // convert properties for simpl public ushort simplDeviceId { get { return Convert.ToUInt16(deviceId); } } public ushort simplEnabled { get { return (ushort)(enabled ? 1 : 0); } } + + /// + /// Constructor + /// + public PropertiesConfig() + { + control = new ControlConfig(); + } } /// diff --git a/Pepperdash Core/Pepperdash Core/JsonToSimpl/JsonToSimplArrayLookupChild.cs b/Pepperdash Core/Pepperdash Core/JsonToSimpl/JsonToSimplArrayLookupChild.cs index 56ef04a..0ac1bbf 100644 --- a/Pepperdash Core/Pepperdash Core/JsonToSimpl/JsonToSimplArrayLookupChild.cs +++ b/Pepperdash Core/Pepperdash Core/JsonToSimpl/JsonToSimplArrayLookupChild.cs @@ -1,116 +1,116 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Crestron.SimplSharp; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; - -namespace PepperDash.Core.JsonToSimpl -{ - public class JsonToSimplArrayLookupChild : JsonToSimplChildObjectBase - { - public string SearchPropertyName { get; set; } - public string SearchPropertyValue { get; set; } - - int ArrayIndex; - - /// - /// For <2.4.1 array lookups - /// - /// - /// - /// - /// - /// - /// - public void Initialize(string file, string key, string pathPrefix, string pathSuffix, - string searchPropertyName, string searchPropertyValue) - { - base.Initialize(file, key, pathPrefix, pathSuffix); - SearchPropertyName = searchPropertyName; - SearchPropertyValue = searchPropertyValue; - } - - - /// - /// For newer >=2.4.1 array lookups. - /// - /// - /// - /// - /// - /// - /// - /// - public void InitializeWithAppend(string file, string key, string pathPrefix, string pathAppend, - string pathSuffix, string searchPropertyName, string searchPropertyValue) +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace PepperDash.Core.JsonToSimpl +{ + public class JsonToSimplArrayLookupChild : JsonToSimplChildObjectBase + { + public string SearchPropertyName { get; set; } + public string SearchPropertyValue { get; set; } + + int ArrayIndex; + + /// + /// For <2.4.1 array lookups + /// + /// + /// + /// + /// + /// + /// + public void Initialize(string file, string key, string pathPrefix, string pathSuffix, + string searchPropertyName, string searchPropertyValue) + { + base.Initialize(file, key, pathPrefix, pathSuffix); + SearchPropertyName = searchPropertyName; + SearchPropertyValue = searchPropertyValue; + } + + + /// + /// For newer >=2.4.1 array lookups. + /// + /// + /// + /// + /// + /// + /// + /// + public void InitializeWithAppend(string file, string key, string pathPrefix, string pathAppend, + string pathSuffix, string searchPropertyName, string searchPropertyValue) + { + string pathPrefixWithAppend = (pathPrefix != null ? pathPrefix : "") + GetPathAppend(pathAppend); + base.Initialize(file, key, pathPrefixWithAppend, pathSuffix); + + SearchPropertyName = searchPropertyName; + SearchPropertyValue = searchPropertyValue; + } + + + + //PathPrefix+ArrayName+[x]+path+PathSuffix + /// + /// + /// + /// + /// + protected override string GetFullPath(string path) { - string pathPrefixWithAppend = (pathPrefix != null ? pathPrefix : "") + GetPathAppend(pathAppend); - base.Initialize(file, key, pathPrefixWithAppend, pathSuffix); - - SearchPropertyName = searchPropertyName; - SearchPropertyValue = searchPropertyValue; - } - - - - //PathPrefix+ArrayName+[x]+path+PathSuffix - /// - /// - /// - /// - /// - protected override string GetFullPath(string path) - { return string.Format("{0}[{1}].{2}{3}", PathPrefix == null ? "" : PathPrefix, - ArrayIndex, + ArrayIndex, path, - PathSuffix == null ? "" : PathSuffix); - } - - public override void ProcessAll() - { - if(FindInArray()) - base.ProcessAll(); - } - - /// - /// Provides the path append for GetFullPath - /// - /// - string GetPathAppend(string a) - { - if (string.IsNullOrEmpty(a)) - { - return ""; - } - if (a.StartsWith(".")) - { - return a; - } - else - { - return "." + a; - } - } - - /// - /// - /// - /// - bool FindInArray() - { - if (Master == null) - throw new InvalidOperationException("Cannot do operations before master is linked"); - if (Master.JsonObject == null) - throw new InvalidOperationException("Cannot do operations before master JSON has read"); - if (PathPrefix == null) + PathSuffix == null ? "" : PathSuffix); + } + + public override void ProcessAll() + { + if (FindInArray()) + base.ProcessAll(); + } + + /// + /// Provides the path append for GetFullPath + /// + /// + string GetPathAppend(string a) + { + if (string.IsNullOrEmpty(a)) + { + return ""; + } + if (a.StartsWith(".")) + { + return a; + } + else + { + return "." + a; + } + } + + /// + /// + /// + /// + bool FindInArray() + { + if (Master == null) + throw new InvalidOperationException("Cannot do operations before master is linked"); + if (Master.JsonObject == null) + throw new InvalidOperationException("Cannot do operations before master JSON has read"); + if (PathPrefix == null) throw new InvalidOperationException("Cannot do operations before PathPrefix is set"); - var token = Master.JsonObject.SelectToken(PathPrefix); + var token = Master.JsonObject.SelectToken(PathPrefix); if (token is JArray) { var array = token as JArray; @@ -139,15 +139,15 @@ namespace PepperDash.Core.JsonToSimpl catch (Exception e) { Debug.Console(1, "JSON Child[{0}] Array '{1}' lookup error: '{2}={3}'\r{4}", Key, - PathPrefix, SearchPropertyName, SearchPropertyValue, e); + PathPrefix, SearchPropertyName, SearchPropertyValue, e); } } else { - Debug.Console(1, "JSON Child[{0}] Path '{1}' is not an array", Key, PathPrefix); - } - - return false; - } - } + Debug.Console(1, "JSON Child[{0}] Path '{1}' is not an array", Key, PathPrefix); + } + + return false; + } + } } \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/JsonToSimpl/JsonToSimplChildObjectBase.cs b/Pepperdash Core/Pepperdash Core/JsonToSimpl/JsonToSimplChildObjectBase.cs index ec855f9..93c5ee5 100644 --- a/Pepperdash Core/Pepperdash Core/JsonToSimpl/JsonToSimplChildObjectBase.cs +++ b/Pepperdash Core/Pepperdash Core/JsonToSimpl/JsonToSimplChildObjectBase.cs @@ -1,327 +1,327 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Crestron.SimplSharp; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; - -namespace PepperDash.Core.JsonToSimpl -{ - public abstract class JsonToSimplChildObjectBase: IKeyed - { - - public event EventHandler BoolChange; - public event EventHandler UShortChange; - public event EventHandler StringChange; - - public SPlusValuesDelegate GetAllValuesDelegate { get; set; } - - /// - /// Use a callback to reduce task switch/threading - /// - public SPlusValuesDelegate SetAllPathsDelegate { get; set; } - - public string Key { get; protected set; } - - /// - /// This will be prepended to all paths to allow path swapping or for more organized - /// sub-paths - /// +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace PepperDash.Core.JsonToSimpl +{ + public abstract class JsonToSimplChildObjectBase: IKeyed + { + + public event EventHandler BoolChange; + public event EventHandler UShortChange; + public event EventHandler StringChange; + + public SPlusValuesDelegate GetAllValuesDelegate { get; set; } + + /// + /// Use a callback to reduce task switch/threading + /// + public SPlusValuesDelegate SetAllPathsDelegate { get; set; } + + public string Key { get; protected set; } + + /// + /// This will be prepended to all paths to allow path swapping or for more organized + /// sub-paths + /// public string PathPrefix { get; protected set; } - - /// - /// This is added to the end of all paths - /// - public string PathSuffix { get; protected set; } - - public bool LinkedToObject { get; protected set; } - - protected JsonToSimplMaster Master; - - // The sent-in JPaths for the various types - protected Dictionary BoolPaths = new Dictionary(); - protected Dictionary UshortPaths = new Dictionary(); - protected Dictionary StringPaths = new Dictionary(); - - /// - /// Call this before doing anything else - /// - /// - /// - /// - /// - public void Initialize(string masterUniqueId, string key, string pathPrefix, string pathSuffix) - { - Key = key; - PathPrefix = pathPrefix; - PathSuffix = pathSuffix; - - Master = J2SGlobal.GetMasterByFile(masterUniqueId); - if (Master != null) - Master.AddChild(this); - else - Debug.Console(1, "JSON Child [{0}] cannot link to master {1}", key, masterUniqueId); - } - - public void SetPathPrefix(string pathPrefix) { - PathPrefix = pathPrefix; - } - /// - /// Set the JPath to evaluate for a given bool out index. - /// - public void SetBoolPath(ushort index, string path) - { - Debug.Console(1, "JSON Child[{0}] SetBoolPath {1}={2}", Key, index, path); - if (path == null || path.Trim() == string.Empty) return; - BoolPaths[index] = path; - } - - /// - /// Set the JPath for a ushort out index. - /// - public void SetUshortPath(ushort index, string path) - { - Debug.Console(1, "JSON Child[{0}] SetUshortPath {1}={2}", Key, index, path); - if (path == null || path.Trim() == string.Empty) return; - UshortPaths[index] = path; - } - - /// - /// Set the JPath for a string output index. - /// - public void SetStringPath(ushort index, string path) - { - Debug.Console(1, "JSON Child[{0}] SetStringPath {1}={2}", Key, index, path); - if (path == null || path.Trim() == string.Empty) return; - StringPaths[index] = path; - } - - /// - /// Evalutates all outputs with defined paths. called by S+ when paths are ready to process - /// and by Master when file is read. - /// - public virtual void ProcessAll() - { - if (!LinkedToObject) - { - Debug.Console(1, this, "Not linked to object in file. Skipping"); - return; - } - - if (SetAllPathsDelegate == null) - { - Debug.Console(1, this, "No SetAllPathsDelegate set. Ignoring ProcessAll"); - return; - } - SetAllPathsDelegate(); - foreach (var kvp in BoolPaths) - ProcessBoolPath(kvp.Key); - foreach (var kvp in UshortPaths) - ProcessUshortPath(kvp.Key); - foreach (var kvp in StringPaths) - ProcessStringPath(kvp.Key); - } - - /// - /// Processes a bool property, converting to bool, firing off a BoolChange event - /// - void ProcessBoolPath(ushort index) - { - string response; - if (Process(BoolPaths[index], out response)) - OnBoolChange(response.Equals("true", StringComparison.OrdinalIgnoreCase), - index, JsonToSimplConstants.BoolValueChange); - else { } - // OnBoolChange(false, index, JsonToSimplConstants.BoolValueChange); - } - - // Processes the path to a ushort, converting to ushort if able, firing off UshrtChange event - void ProcessUshortPath(ushort index) - { - string response; - if (Process(UshortPaths[index], out response)) { - ushort val; - try { val = Convert.ToUInt16(response); } catch { val = 0; } - OnUShortChange(val, index, JsonToSimplConstants.UshortValueChange); - } - else { } - // OnUShortChange(0, index, JsonToSimplConstants.UshortValueChange); - } - - // Processes the path to a string property and fires of a StringChange event. - void ProcessStringPath(ushort index) - { - string response; - if (Process(StringPaths[index], out response)) - OnStringChange(response, index, JsonToSimplConstants.StringValueChange); - else { } - // OnStringChange("", index, JsonToSimplConstants.StringValueChange); - } - - /// - /// Processes the given path. - /// - /// JPath formatted path to the desired property - /// The string value of the property, or a default value if it - /// doesn't exist - /// This will return false in the case that EvaulateAllOnJsonChange - /// is false and the path does not evaluate to a property in the incoming JSON. - bool Process(string path, out string response) - { - path = GetFullPath(path); - Debug.Console(1, "JSON Child[{0}] Processing {1}", Key, path); - response = ""; - if (Master == null) - { - Debug.Console(1, "JSONChild[{0}] cannot process without Master attached", Key); - return false; - } - - if (Master.JsonObject != null && path != string.Empty) - { - bool isCount = false; - path = path.Trim(); - if (path.EndsWith(".Count")) - { - path = path.Remove(path.Length - 6, 6); - isCount = true; - } - try // Catch a strange cast error on a bad path - { - var t = Master.JsonObject.SelectToken(path); - if (t != null) - { - // return the count of children objects - if any - if (isCount) - response = (t.HasValues ? t.Children().Count() : 0).ToString(); - else - response = t.Value(); - Debug.Console(1, " ='{0}'", response); - return true; - } - } - catch - { - response = ""; - } - } - // If the path isn't found, return this to determine whether to pass out the non-value or not. - return false; - } - - - //************************************************************************************************ - // Save-related functions - - - /// - /// Called from Master to read inputs and update their values in master JObject - /// Callback should hit one of the following four methods - /// - public void UpdateInputsForMaster() - { - if (!LinkedToObject) - { - Debug.Console(1, this, "Not linked to object in file. Skipping"); - return; - } - - if (SetAllPathsDelegate == null) - { - Debug.Console(1, this, "No SetAllPathsDelegate set. Ignoring UpdateInputsForMaster"); - return; - } - SetAllPathsDelegate(); - var del = GetAllValuesDelegate; - if (del != null) - GetAllValuesDelegate(); - } - - public void USetBoolValue(ushort key, ushort theValue) - { - SetBoolValue(key, theValue == 1); - } - - public void SetBoolValue(ushort key, bool theValue) - { - if (BoolPaths.ContainsKey(key)) - SetValueOnMaster(BoolPaths[key], new JValue(theValue)); - } - - public void SetUShortValue(ushort key, ushort theValue) - { - if (UshortPaths.ContainsKey(key)) - SetValueOnMaster(UshortPaths[key], new JValue(theValue)); - } - - public void SetStringValue(ushort key, string theValue) - { - if (StringPaths.ContainsKey(key)) - SetValueOnMaster(StringPaths[key], new JValue(theValue)); - } - - public void SetValueOnMaster(string keyPath, JValue valueToSave) - { - var path = GetFullPath(keyPath); - try - { - Debug.Console(1, "JSON Child[{0}] Queueing value on master {1}='{2}'", Key, path, valueToSave); - - //var token = Master.JsonObject.SelectToken(path); - //if (token != null) // The path exists in the file - Master.AddUnsavedValue(path, valueToSave); - } - catch (Exception e) - { - Debug.Console(1, "JSON Child[{0}] Failed setting value for path '{1}'\r{2}", Key, path, e); - } - } - - /// - /// Called during Process(...) to get the path to a given property. By default, - /// returns PathPrefix+path+PathSuffix. Override to change the way path is built. - /// - protected virtual string GetFullPath(string path) - { - return (PathPrefix != null ? PathPrefix : "") + - path + (PathSuffix != null ? PathSuffix : ""); - } - - // Helpers for events - //****************************************************************************************** - protected void OnBoolChange(bool state, ushort index, ushort type) - { - var handler = BoolChange; - if (handler != null) - { - var args = new BoolChangeEventArgs(state, type); - args.Index = index; - BoolChange(this, args); - } - } - - //****************************************************************************************** - protected void OnUShortChange(ushort state, ushort index, ushort type) - { - var handler = UShortChange; - if (handler != null) - { - var args = new UshrtChangeEventArgs(state, type); - args.Index = index; - UShortChange(this, args); - } - } - - protected void OnStringChange(string value, ushort index, ushort type) - { - var handler = StringChange; - if (handler != null) - { - var args = new StringChangeEventArgs(value, type); - args.Index = index; - StringChange(this, args); - } - } - } + + /// + /// This is added to the end of all paths + /// + public string PathSuffix { get; protected set; } + + public bool LinkedToObject { get; protected set; } + + protected JsonToSimplMaster Master; + + // The sent-in JPaths for the various types + protected Dictionary BoolPaths = new Dictionary(); + protected Dictionary UshortPaths = new Dictionary(); + protected Dictionary StringPaths = new Dictionary(); + + /// + /// Call this before doing anything else + /// + /// + /// + /// + /// + public void Initialize(string masterUniqueId, string key, string pathPrefix, string pathSuffix) + { + Key = key; + PathPrefix = pathPrefix; + PathSuffix = pathSuffix; + + Master = J2SGlobal.GetMasterByFile(masterUniqueId); + if (Master != null) + Master.AddChild(this); + else + Debug.Console(1, "JSON Child [{0}] cannot link to master {1}", key, masterUniqueId); + } + + public void SetPathPrefix(string pathPrefix) { + PathPrefix = pathPrefix; + } + /// + /// Set the JPath to evaluate for a given bool out index. + /// + public void SetBoolPath(ushort index, string path) + { + Debug.Console(1, "JSON Child[{0}] SetBoolPath {1}={2}", Key, index, path); + if (path == null || path.Trim() == string.Empty) return; + BoolPaths[index] = path; + } + + /// + /// Set the JPath for a ushort out index. + /// + public void SetUshortPath(ushort index, string path) + { + Debug.Console(1, "JSON Child[{0}] SetUshortPath {1}={2}", Key, index, path); + if (path == null || path.Trim() == string.Empty) return; + UshortPaths[index] = path; + } + + /// + /// Set the JPath for a string output index. + /// + public void SetStringPath(ushort index, string path) + { + Debug.Console(1, "JSON Child[{0}] SetStringPath {1}={2}", Key, index, path); + if (path == null || path.Trim() == string.Empty) return; + StringPaths[index] = path; + } + + /// + /// Evalutates all outputs with defined paths. called by S+ when paths are ready to process + /// and by Master when file is read. + /// + public virtual void ProcessAll() + { + if (!LinkedToObject) + { + Debug.Console(1, this, "Not linked to object in file. Skipping"); + return; + } + + if (SetAllPathsDelegate == null) + { + Debug.Console(1, this, "No SetAllPathsDelegate set. Ignoring ProcessAll"); + return; + } + SetAllPathsDelegate(); + foreach (var kvp in BoolPaths) + ProcessBoolPath(kvp.Key); + foreach (var kvp in UshortPaths) + ProcessUshortPath(kvp.Key); + foreach (var kvp in StringPaths) + ProcessStringPath(kvp.Key); + } + + /// + /// Processes a bool property, converting to bool, firing off a BoolChange event + /// + void ProcessBoolPath(ushort index) + { + string response; + if (Process(BoolPaths[index], out response)) + OnBoolChange(response.Equals("true", StringComparison.OrdinalIgnoreCase), + index, JsonToSimplConstants.BoolValueChange); + else { } + // OnBoolChange(false, index, JsonToSimplConstants.BoolValueChange); + } + + // Processes the path to a ushort, converting to ushort if able, firing off UshrtChange event + void ProcessUshortPath(ushort index) + { + string response; + if (Process(UshortPaths[index], out response)) { + ushort val; + try { val = Convert.ToUInt16(response); } catch { val = 0; } + OnUShortChange(val, index, JsonToSimplConstants.UshortValueChange); + } + else { } + // OnUShortChange(0, index, JsonToSimplConstants.UshortValueChange); + } + + // Processes the path to a string property and fires of a StringChange event. + void ProcessStringPath(ushort index) + { + string response; + if (Process(StringPaths[index], out response)) + OnStringChange(response, index, JsonToSimplConstants.StringValueChange); + else { } + // OnStringChange("", index, JsonToSimplConstants.StringValueChange); + } + + /// + /// Processes the given path. + /// + /// JPath formatted path to the desired property + /// The string value of the property, or a default value if it + /// doesn't exist + /// This will return false in the case that EvaulateAllOnJsonChange + /// is false and the path does not evaluate to a property in the incoming JSON. + bool Process(string path, out string response) + { + path = GetFullPath(path); + Debug.Console(1, "JSON Child[{0}] Processing {1}", Key, path); + response = ""; + if (Master == null) + { + Debug.Console(1, "JSONChild[{0}] cannot process without Master attached", Key); + return false; + } + + if (Master.JsonObject != null && path != string.Empty) + { + bool isCount = false; + path = path.Trim(); + if (path.EndsWith(".Count")) + { + path = path.Remove(path.Length - 6, 6); + isCount = true; + } + try // Catch a strange cast error on a bad path + { + var t = Master.JsonObject.SelectToken(path); + if (t != null) + { + // return the count of children objects - if any + if (isCount) + response = (t.HasValues ? t.Children().Count() : 0).ToString(); + else + response = t.Value(); + Debug.Console(1, " ='{0}'", response); + return true; + } + } + catch + { + response = ""; + } + } + // If the path isn't found, return this to determine whether to pass out the non-value or not. + return false; + } + + + //************************************************************************************************ + // Save-related functions + + + /// + /// Called from Master to read inputs and update their values in master JObject + /// Callback should hit one of the following four methods + /// + public void UpdateInputsForMaster() + { + if (!LinkedToObject) + { + Debug.Console(1, this, "Not linked to object in file. Skipping"); + return; + } + + if (SetAllPathsDelegate == null) + { + Debug.Console(1, this, "No SetAllPathsDelegate set. Ignoring UpdateInputsForMaster"); + return; + } + SetAllPathsDelegate(); + var del = GetAllValuesDelegate; + if (del != null) + GetAllValuesDelegate(); + } + + public void USetBoolValue(ushort key, ushort theValue) + { + SetBoolValue(key, theValue == 1); + } + + public void SetBoolValue(ushort key, bool theValue) + { + if (BoolPaths.ContainsKey(key)) + SetValueOnMaster(BoolPaths[key], new JValue(theValue)); + } + + public void SetUShortValue(ushort key, ushort theValue) + { + if (UshortPaths.ContainsKey(key)) + SetValueOnMaster(UshortPaths[key], new JValue(theValue)); + } + + public void SetStringValue(ushort key, string theValue) + { + if (StringPaths.ContainsKey(key)) + SetValueOnMaster(StringPaths[key], new JValue(theValue)); + } + + public void SetValueOnMaster(string keyPath, JValue valueToSave) + { + var path = GetFullPath(keyPath); + try + { + Debug.Console(1, "JSON Child[{0}] Queueing value on master {1}='{2}'", Key, path, valueToSave); + + //var token = Master.JsonObject.SelectToken(path); + //if (token != null) // The path exists in the file + Master.AddUnsavedValue(path, valueToSave); + } + catch (Exception e) + { + Debug.Console(1, "JSON Child[{0}] Failed setting value for path '{1}'\r{2}", Key, path, e); + } + } + + /// + /// Called during Process(...) to get the path to a given property. By default, + /// returns PathPrefix+path+PathSuffix. Override to change the way path is built. + /// + protected virtual string GetFullPath(string path) + { + return (PathPrefix != null ? PathPrefix : "") + + path + (PathSuffix != null ? PathSuffix : ""); + } + + // Helpers for events + //****************************************************************************************** + protected void OnBoolChange(bool state, ushort index, ushort type) + { + var handler = BoolChange; + if (handler != null) + { + var args = new BoolChangeEventArgs(state, type); + args.Index = index; + BoolChange(this, args); + } + } + + //****************************************************************************************** + protected void OnUShortChange(ushort state, ushort index, ushort type) + { + var handler = UShortChange; + if (handler != null) + { + var args = new UshrtChangeEventArgs(state, type); + args.Index = index; + UShortChange(this, args); + } + } + + protected void OnStringChange(string value, ushort index, ushort type) + { + var handler = StringChange; + if (handler != null) + { + var args = new StringChangeEventArgs(value, type); + args.Index = index; + StringChange(this, args); + } + } + } } \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj b/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj index 04bce68..8869ca9 100644 --- a/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj +++ b/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj @@ -119,5 +119,7 @@ rem S# preparation will execute after these operations + del "$(TargetDir)PepperDash_Core.*" /q + \ No newline at end of file From 139777b501044030d9132ce659d50115685eed03 Mon Sep 17 00:00:00 2001 From: Jason T Alborough Date: Fri, 11 Oct 2019 05:59:10 -0400 Subject: [PATCH 5/9] Adds conditions for when creating a new Ssh client. --- Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs | 7 ++++++- Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs | 2 -- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs b/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs index 6af5d7b..8a6e9fd 100644 --- a/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs +++ b/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs @@ -221,7 +221,12 @@ namespace PepperDash.Core Debug.Console(1, this, "Creating new SshClient"); ConnectionInfo connectionInfo = new ConnectionInfo(Hostname, Port, Username, pauth, kauth); - Client = new SshClient(connectionInfo); + + if (Client == null) + { + Client = new SshClient(connectionInfo); + } + Client.ErrorOccurred -= Client_ErrorOccurred; Client.ErrorOccurred += Client_ErrorOccurred; //You can do it! diff --git a/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs b/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs index 525f32b..e9a7ccf 100644 --- a/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs +++ b/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs @@ -250,8 +250,6 @@ namespace PepperDash.Core if (Client == null) { - - Client = new TCPClient(Hostname, Port, BufferSize); Client.SocketStatusChange += Client_SocketStatusChange; } From 5808e5361839e01e97f1a0b3b7c00ae352762ba4 Mon Sep 17 00:00:00 2001 From: Jason DeVito Date: Fri, 11 Oct 2019 17:48:56 -0500 Subject: [PATCH 6/9] Work in progress. Add EventArgs.cs to root of PepperDash core, exposing the event arg classes to everything in PDC --- Pepperdash Core/Pepperdash Core/EventArgs.cs | 172 ++++++++++++ .../GenericRESTfulCommunications/Constants.cs | 39 +++ .../GenericRESTful.cs | 253 +++++++++++++++++ .../EventArgs and Constants.cs | 34 ++- .../JsonStandardObjects/JsonToSimplDevice.cs | 174 +----------- .../JsonToSimplDeviceConfig.cs | 158 +++++++++++ .../Pepperdash Core/JsonToSimpl/Constants.cs | 59 ++++ .../JsonToSimpl/EventArgs and Constants.cs | 129 --------- .../Pepperdash Core/PepperDash_Core.csproj | 13 +- .../SystemInfo/ConsoleHelper.cs | 52 ++++ .../SystemInfo/ControlSubnetInfo.cs | 81 ++++++ .../SystemInfo/EthernetInfo.cs | 108 ++++++++ .../SystemInfo/EventArgs and Constants.cs | 187 +++++++++++++ .../SystemInfo/ProcessorInfo.cs | 121 +++++++++ .../Pepperdash Core/SystemInfo/ProgramInfo.cs | 88 ++++++ .../SystemInfo/SystemInfoToSimpl.cs | 254 ++++++++++++++++++ 16 files changed, 1616 insertions(+), 306 deletions(-) create mode 100644 Pepperdash Core/Pepperdash Core/EventArgs.cs create mode 100644 Pepperdash Core/Pepperdash Core/GenericRESTfulCommunications/Constants.cs create mode 100644 Pepperdash Core/Pepperdash Core/GenericRESTfulCommunications/GenericRESTful.cs create mode 100644 Pepperdash Core/Pepperdash Core/JsonStandardObjects/JsonToSimplDeviceConfig.cs create mode 100644 Pepperdash Core/Pepperdash Core/JsonToSimpl/Constants.cs delete mode 100644 Pepperdash Core/Pepperdash Core/JsonToSimpl/EventArgs and Constants.cs create mode 100644 Pepperdash Core/Pepperdash Core/SystemInfo/ConsoleHelper.cs create mode 100644 Pepperdash Core/Pepperdash Core/SystemInfo/ControlSubnetInfo.cs create mode 100644 Pepperdash Core/Pepperdash Core/SystemInfo/EthernetInfo.cs create mode 100644 Pepperdash Core/Pepperdash Core/SystemInfo/EventArgs and Constants.cs create mode 100644 Pepperdash Core/Pepperdash Core/SystemInfo/ProcessorInfo.cs create mode 100644 Pepperdash Core/Pepperdash Core/SystemInfo/ProgramInfo.cs create mode 100644 Pepperdash Core/Pepperdash Core/SystemInfo/SystemInfoToSimpl.cs diff --git a/Pepperdash Core/Pepperdash Core/EventArgs.cs b/Pepperdash Core/Pepperdash Core/EventArgs.cs new file mode 100644 index 0000000..6def673 --- /dev/null +++ b/Pepperdash Core/Pepperdash Core/EventArgs.cs @@ -0,0 +1,172 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; + +namespace PepperDash.Core +{ + /// + /// Bool change event args + /// + public class BoolChangeEventArgs : EventArgs + { + /// + /// Boolean state property + /// + public bool State { get; set; } + + /// + /// Boolean ushort value property + /// + public ushort IntValue { get { return (ushort)(State ? 1 : 0); } } + + /// + /// Boolean change event args type + /// + public ushort Type { get; set; } + + /// + /// Boolean change event args index + /// + public ushort Index { get; set; } + + /// + /// Constructor + /// + public BoolChangeEventArgs() + { + + } + + /// + /// Constructor overload + /// + /// + /// + public BoolChangeEventArgs(bool state, ushort type) + { + State = state; + Type = type; + } + + /// + /// Constructor overload + /// + /// + /// + /// + public BoolChangeEventArgs(bool state, ushort type, ushort index) + { + State = state; + Type = type; + Index = index; + } + } + + /// + /// Ushort change event args + /// + public class UshrtChangeEventArgs : EventArgs + { + /// + /// Ushort change event args integer value + /// + public ushort IntValue { get; set; } + + /// + /// Ushort change event args type + /// + public ushort Type { get; set; } + + /// + /// Ushort change event args index + /// + public ushort Index { get; set; } + + /// + /// Constructor + /// + public UshrtChangeEventArgs() + { + + } + + /// + /// Constructor overload + /// + /// + /// + public UshrtChangeEventArgs(ushort intValue, ushort type) + { + IntValue = intValue; + Type = type; + } + + /// + /// Constructor overload + /// + /// + /// + /// + public UshrtChangeEventArgs(ushort intValue, ushort type, ushort index) + { + IntValue = intValue; + Type = type; + Index = index; + } + } + + /// + /// String change event args + /// + public class StringChangeEventArgs : EventArgs + { + /// + /// String change event args value + /// + public string StringValue { get; set; } + + /// + /// String change event args type + /// + public ushort Type { get; set; } + + /// + /// string change event args index + /// + public ushort Index { get; set; } + + /// + /// Constructor + /// + public StringChangeEventArgs() + { + + } + + /// + /// Constructor overload + /// + /// + /// + public StringChangeEventArgs(string stringValue, ushort type) + { + StringValue = stringValue; + Type = type; + } + + /// + /// Constructor overload + /// + /// + /// + /// + public StringChangeEventArgs(string stringValue, ushort type, ushort index) + { + StringValue = stringValue; + Type = type; + Index = index; + } + } +} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/GenericRESTfulCommunications/Constants.cs b/Pepperdash Core/Pepperdash Core/GenericRESTfulCommunications/Constants.cs new file mode 100644 index 0000000..9df43f0 --- /dev/null +++ b/Pepperdash Core/Pepperdash Core/GenericRESTfulCommunications/Constants.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; + +namespace PepperDash.Core.GenericRESTfulCommunications +{ + /// + /// Constants + /// + public class GenericRESTfulConstants + { + /// + /// Generic boolean change + /// + public const ushort BoolValueChange = 1; + /// + /// Generic Ushort change + /// + public const ushort UshrtValueChange = 101; + /// + /// Response Code Ushort change + /// + public const ushort ResponseCodeChange = 102; + /// + /// Generic String chagne + /// + public const ushort StringValueChange = 201; + /// + /// Response string change + /// + public const ushort ResponseStringChange = 202; + /// + /// Error string change + /// + public const ushort ErrorStringChange = 203; + } +} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/GenericRESTfulCommunications/GenericRESTful.cs b/Pepperdash Core/Pepperdash Core/GenericRESTfulCommunications/GenericRESTful.cs new file mode 100644 index 0000000..7d8df61 --- /dev/null +++ b/Pepperdash Core/Pepperdash Core/GenericRESTfulCommunications/GenericRESTful.cs @@ -0,0 +1,253 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; +using Crestron.SimplSharp.Net.Http; +using Crestron.SimplSharp.Net.Https; + +namespace PepperDash.Core.GenericRESTfulCommunications +{ + /// + /// Generic RESTful communication class + /// + public class GenericRESTfulClient + { + /// + /// Boolean event handler + /// + public event EventHandler BoolChange; + /// + /// Ushort event handler + /// + public event EventHandler UshrtChange; + /// + /// String event handler + /// + public event EventHandler StringChange; + + /// + /// Constructor + /// + public GenericRESTfulClient() + { + + } + + /// + /// Generic RESTful submit request + /// + /// + /// + /// + /// + /// + public void SubmitRequest(string url, ushort port, ushort requestType, string contentType, string username, string password) + { + if (url.StartsWith("https:", StringComparison.OrdinalIgnoreCase)) + { + SubmitRequestHttps(url, port, requestType, contentType, username, password); + } + else if (url.StartsWith("http:", StringComparison.OrdinalIgnoreCase)) + { + SubmitRequestHttp(url, port, requestType, contentType, username, password); + } + else + { + OnStringChange(string.Format("Invalid URL {0}", url), 0, GenericRESTfulConstants.ErrorStringChange); + } + } + + /// + /// Private HTTP submit request + /// + /// + /// + /// + /// + /// + private void SubmitRequestHttp(string url, ushort port, ushort requestType, string contentType, string username, string password) + { + try + { + HttpClient client = new HttpClient(); + HttpClientRequest request = new HttpClientRequest(); + HttpClientResponse response; + + client.KeepAlive = false; + + if(port >= 1 || port <= 65535) + client.Port = port; + else + client.Port = 80; + + var authorization = ""; + if (!string.IsNullOrEmpty(username)) + authorization = EncodeBase64(username, password); + + if (!string.IsNullOrEmpty(authorization)) + request.Header.SetHeaderValue("Authorization", authorization); + + if (!string.IsNullOrEmpty(contentType)) + request.Header.ContentType = contentType; + + request.Url.Parse(url); + request.RequestType = (Crestron.SimplSharp.Net.Http.RequestType)requestType; + + response = client.Dispatch(request); + + CrestronConsole.PrintLine(string.Format("SubmitRequestHttp Response[{0}]: {1}", response.Code, response.ContentString.ToString())); + + if (!string.IsNullOrEmpty(response.ContentString.ToString())) + OnStringChange(response.ContentString.ToString(), 0, GenericRESTfulConstants.ResponseStringChange); + + if (response.Code > 0) + OnUshrtChange((ushort)response.Code, 0, GenericRESTfulConstants.ResponseCodeChange); + } + catch (Exception e) + { + //var msg = string.Format("SubmitRequestHttp({0}, {1}, {2}) failed:{3}", url, port, requestType, e.Message); + //CrestronConsole.PrintLine(msg); + //ErrorLog.Error(msg); + + CrestronConsole.PrintLine(e.Message); + OnStringChange(e.Message, 0, GenericRESTfulConstants.ErrorStringChange); + } + } + + /// + /// Private HTTPS submit request + /// + /// + /// + /// + /// + /// + private void SubmitRequestHttps(string url, ushort port, ushort requestType, string contentType, string username, string password) + { + try + { + HttpsClient client = new HttpsClient(); + HttpsClientRequest request = new HttpsClientRequest(); + HttpsClientResponse response; + + client.KeepAlive = false; + client.HostVerification = false; + client.PeerVerification = false; + + var authorization = ""; + if (!string.IsNullOrEmpty(username)) + authorization = EncodeBase64(username, password); + + if (!string.IsNullOrEmpty(authorization)) + request.Header.SetHeaderValue("Authorization", authorization); + + if (!string.IsNullOrEmpty(contentType)) + request.Header.ContentType = contentType; + + request.Url.Parse(url); + request.RequestType = (Crestron.SimplSharp.Net.Https.RequestType)requestType; + + response = client.Dispatch(request); + + CrestronConsole.PrintLine(string.Format("SubmitRequestHttp Response[{0}]: {1}", response.Code, response.ContentString.ToString())); + + if(!string.IsNullOrEmpty(response.ContentString.ToString())) + OnStringChange(response.ContentString.ToString(), 0, GenericRESTfulConstants.ResponseStringChange); + + if(response.Code > 0) + OnUshrtChange((ushort)response.Code, 0, GenericRESTfulConstants.ResponseCodeChange); + + } + catch (Exception e) + { + //var msg = string.Format("SubmitRequestHttps({0}, {1}, {2}, {3}, {4}) failed:{5}", url, port, requestType, username, password, e.Message); + //CrestronConsole.PrintLine(msg); + //ErrorLog.Error(msg); + + CrestronConsole.PrintLine(e.Message); + OnStringChange(e.Message, 0, GenericRESTfulConstants.ErrorStringChange); + } + } + + /// + /// Private method to encode username and password to Base64 string + /// + /// + /// + /// authorization + private string EncodeBase64(string username, string password) + { + var authorization = ""; + + try + { + if (!string.IsNullOrEmpty(username)) + { + string base64String = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(string.Format("{0}:{1}", username, password))); + authorization = string.Format("Basic {0}", base64String); + } + } + catch (Exception e) + { + var msg = string.Format("EncodeBase64({0}, {1}) failed:\r{2}", username, password, e); + CrestronConsole.PrintLine(msg); + ErrorLog.Error(msg); + return "" ; + } + + return authorization; + } + + /// + /// Protected method to handle boolean change events + /// + /// + /// + /// + protected void OnBoolChange(bool state, ushort index, ushort type) + { + var handler = BoolChange; + if (handler != null) + { + var args = new BoolChangeEventArgs(state, type); + args.Index = index; + BoolChange(this, args); + } + } + + /// + /// Protected mehtod to handle ushort change events + /// + /// + /// + /// + protected void OnUshrtChange(ushort value, ushort index, ushort type) + { + var handler = UshrtChange; + if (handler != null) + { + var args = new UshrtChangeEventArgs(value, type); + args.Index = index; + UshrtChange(this, args); + } + } + + /// + /// Protected method to handle string change events + /// + /// + /// + /// + protected void OnStringChange(string value, ushort index, ushort type) + { + var handler = StringChange; + if (handler != null) + { + var args = new StringChangeEventArgs(value, type); + args.Index = index; + StringChange(this, args); + } + } + } +} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/JsonStandardObjects/EventArgs and Constants.cs b/Pepperdash Core/Pepperdash Core/JsonStandardObjects/EventArgs and Constants.cs index f4a3118..2bac226 100644 --- a/Pepperdash Core/Pepperdash Core/JsonStandardObjects/EventArgs and Constants.cs +++ b/Pepperdash Core/Pepperdash Core/JsonStandardObjects/EventArgs and Constants.cs @@ -6,36 +6,46 @@ using Crestron.SimplSharp; namespace PepperDash.Core.JsonStandardObjects { - #region Constants - /// /// Constants for simpl modules /// public class JsonStandardDeviceConstants { + /// + /// Json object evaluated constant + /// public const ushort JsonObjectEvaluated = 2; + /// + /// Json object changed constant + /// public const ushort JsonObjectChanged = 104; } - #endregion Constants - - - #region ObjectChangeEventArgs - /// /// /// - public class ObjectChangeEventArgs : EventArgs + public class DeviceChangeEventArgs : EventArgs { + /// + /// Device change event args object + /// public DeviceConfig Device { get; set; } + + /// + /// Device change event args type + /// public ushort Type { get; set; } + + /// + /// Device change event args index + /// public ushort Index { get; set; } /// /// Default constructor /// - public ObjectChangeEventArgs() + public DeviceChangeEventArgs() { } @@ -45,7 +55,7 @@ namespace PepperDash.Core.JsonStandardObjects /// /// /// - public ObjectChangeEventArgs(DeviceConfig device, ushort type) + public DeviceChangeEventArgs(DeviceConfig device, ushort type) { Device = device; Type = type; @@ -57,13 +67,11 @@ namespace PepperDash.Core.JsonStandardObjects /// /// /// - public ObjectChangeEventArgs(DeviceConfig device, ushort type, ushort index) + public DeviceChangeEventArgs(DeviceConfig device, ushort type, ushort index) { Device = device; Type = type; Index = index; } } - - #endregion ObjectChangeEventArgs } \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/JsonStandardObjects/JsonToSimplDevice.cs b/Pepperdash Core/Pepperdash Core/JsonStandardObjects/JsonToSimplDevice.cs index 7ad1a89..eef68f2 100644 --- a/Pepperdash Core/Pepperdash Core/JsonStandardObjects/JsonToSimplDevice.cs +++ b/Pepperdash Core/Pepperdash Core/JsonStandardObjects/JsonToSimplDevice.cs @@ -9,52 +9,10 @@ using PepperDash.Core.JsonToSimpl; namespace PepperDash.Core.JsonStandardObjects { - /* - Convert JSON snippt to C#: http://json2csharp.com/# - - JSON Snippet: - { - "devices": [ - { - "key": "deviceKey", - "name": "deviceName", - "type": "deviceType", - "properties": { - "deviceId": 1, - "enabled": true, - "control": { - "method": "methodName", - "controlPortDevKey": "deviceControlPortDevKey", - "controlPortNumber": 1, - "comParams": { - "baudRate": 9600, - "dataBits": 8, - "stopBits": 1, - "parity": "None", - "protocol": "RS232", - "hardwareHandshake": "None", - "softwareHandshake": "None", - "pacing": 0 - }, - "tcpSshProperties": { - "address": "172.22.1.101", - "port": 23, - "username": "user01", - "password": "password01", - "autoReconnect": false, - "autoReconnectIntervalMs": 10000 - } - } - } - } - ] - } - */ - /// /// Device class /// - public class DeviceConfig : JsonToSimplChildObjectBase + public class DeviceConfig { /// /// JSON config key property @@ -88,7 +46,7 @@ namespace PepperDash.Core.JsonStandardObjects /// /// Object change event handler /// - public event EventHandler ObjectChange; + public event EventHandler DeviceChange; /// /// Constructor @@ -164,7 +122,8 @@ namespace PepperDash.Core.JsonStandardObjects /// protected void OnBoolChange(bool state, ushort index, ushort type) { - if (BoolChange != null) + var handler = BoolChange; + if (handler != null) { var args = new BoolChangeEventArgs(state, type); args.Index = index; @@ -180,7 +139,8 @@ namespace PepperDash.Core.JsonStandardObjects /// protected void OnUshrtChange(ushort state, ushort index, ushort type) { - if (UshrtChange != null) + var handler = UshrtChange; + if (handler != null) { var args = new UshrtChangeEventArgs(state, type); args.Index = index; @@ -196,7 +156,8 @@ namespace PepperDash.Core.JsonStandardObjects /// protected void OnStringChange(string value, ushort index, ushort type) { - if (StringChange != null) + var handler = StringChange; + if (handler != null) { var args = new StringChangeEventArgs(value, type); args.Index = index; @@ -212,127 +173,14 @@ namespace PepperDash.Core.JsonStandardObjects /// protected void OnObjectChange(DeviceConfig device, ushort index, ushort type) { - if (ObjectChange != null) + if (DeviceChange != null) { - var args = new ObjectChangeEventArgs(device, type); + var args = new DeviceChangeEventArgs(device, type); args.Index = index; - ObjectChange(this, args); + DeviceChange(this, args); } } #endregion EventHandler Helpers } - - #region JSON Configuration Classes - - /// - /// Device communication parameter class - /// - public class ComParamsConfig - { - public int baudRate { get; set; } - public int dataBits { get; set; } - public int stopBits { get; set; } - public string parity { get; set; } - public string protocol { get; set; } - public string hardwareHandshake { get; set; } - public string softwareHandshake { get; set; } - public int pacing { get; set; } - - // convert properties for simpl - public ushort simplBaudRate { get { return Convert.ToUInt16(baudRate); } } - public ushort simplDataBits { get { return Convert.ToUInt16(dataBits); } } - public ushort simplStopBits { get { return Convert.ToUInt16(stopBits); } } - public ushort simplPacing { get { return Convert.ToUInt16(pacing); } } - - /// - /// Constructor - /// - public ComParamsConfig() - { - - } - } - - /// - /// Device TCP/SSH properties class - /// - public class TcpSshPropertiesConfig - { - public string address { get; set; } - public int port { get; set; } - public string username { get; set; } - public string password { get; set; } - public bool autoReconnect { get; set; } - public int autoReconnectIntervalMs { get; set; } - - // convert properties for simpl - public ushort simplPort { get { return Convert.ToUInt16(port); } } - public ushort simplAutoReconnect { get { return (ushort)(autoReconnect ? 1 : 0); } } - public ushort simplAutoReconnectIntervalMs { get { return Convert.ToUInt16(autoReconnectIntervalMs); } } - - /// - /// Constructor - /// - public TcpSshPropertiesConfig() - { - - } - } - - /// - /// Device control class - /// - public class ControlConfig - { - public string method { get; set; } - public string controlPortDevKey { get; set; } - public int controlPortNumber { get; set; } - public ComParamsConfig comParams { get; set; } - public TcpSshPropertiesConfig tcpSshProperties { get; set; } - - // convert properties for simpl - public ushort simplControlPortNumber { get { return Convert.ToUInt16(controlPortNumber); } } - - /// - /// Constructor - /// - public ControlConfig() - { - comParams = new ComParamsConfig(); - tcpSshProperties = new TcpSshPropertiesConfig(); - } - } - - /// - /// Device properties class - /// - public class PropertiesConfig - { - public int deviceId { get; set; } - public bool enabled { get; set; } - public ControlConfig control { get; set; } - - // convert properties for simpl - public ushort simplDeviceId { get { return Convert.ToUInt16(deviceId); } } - public ushort simplEnabled { get { return (ushort)(enabled ? 1 : 0); } } - - /// - /// Constructor - /// - public PropertiesConfig() - { - control = new ControlConfig(); - } - } - - /// - /// Root device class - /// - public class RootObject - { - public List devices { get; set; } - } - - #endregion JSON Configuration Classes } \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/JsonStandardObjects/JsonToSimplDeviceConfig.cs b/Pepperdash Core/Pepperdash Core/JsonStandardObjects/JsonToSimplDeviceConfig.cs new file mode 100644 index 0000000..92e9f03 --- /dev/null +++ b/Pepperdash Core/Pepperdash Core/JsonStandardObjects/JsonToSimplDeviceConfig.cs @@ -0,0 +1,158 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; + +namespace PepperDash.Core.JsonStandardObjects +{ + /* + Convert JSON snippt to C#: http://json2csharp.com/# + + JSON Snippet: + { + "devices": [ + { + "key": "deviceKey", + "name": "deviceName", + "type": "deviceType", + "properties": { + "deviceId": 1, + "enabled": true, + "control": { + "method": "methodName", + "controlPortDevKey": "deviceControlPortDevKey", + "controlPortNumber": 1, + "comParams": { + "baudRate": 9600, + "dataBits": 8, + "stopBits": 1, + "parity": "None", + "protocol": "RS232", + "hardwareHandshake": "None", + "softwareHandshake": "None", + "pacing": 0 + }, + "tcpSshProperties": { + "address": "172.22.1.101", + "port": 23, + "username": "user01", + "password": "password01", + "autoReconnect": false, + "autoReconnectIntervalMs": 10000 + } + } + } + } + ] + } + */ + /// + /// Device communication parameter class + /// + public class ComParamsConfig + { + public int baudRate { get; set; } + public int dataBits { get; set; } + public int stopBits { get; set; } + public string parity { get; set; } + public string protocol { get; set; } + public string hardwareHandshake { get; set; } + public string softwareHandshake { get; set; } + public int pacing { get; set; } + + // convert properties for simpl + public ushort simplBaudRate { get { return Convert.ToUInt16(baudRate); } } + public ushort simplDataBits { get { return Convert.ToUInt16(dataBits); } } + public ushort simplStopBits { get { return Convert.ToUInt16(stopBits); } } + public ushort simplPacing { get { return Convert.ToUInt16(pacing); } } + + /// + /// Constructor + /// + public ComParamsConfig() + { + + } + } + + /// + /// Device TCP/SSH properties class + /// + public class TcpSshPropertiesConfig + { + public string address { get; set; } + public int port { get; set; } + public string username { get; set; } + public string password { get; set; } + public bool autoReconnect { get; set; } + public int autoReconnectIntervalMs { get; set; } + + // convert properties for simpl + public ushort simplPort { get { return Convert.ToUInt16(port); } } + public ushort simplAutoReconnect { get { return (ushort)(autoReconnect ? 1 : 0); } } + public ushort simplAutoReconnectIntervalMs { get { return Convert.ToUInt16(autoReconnectIntervalMs); } } + + /// + /// Constructor + /// + public TcpSshPropertiesConfig() + { + + } + } + + /// + /// Device control class + /// + public class ControlConfig + { + public string method { get; set; } + public string controlPortDevKey { get; set; } + public int controlPortNumber { get; set; } + public ComParamsConfig comParams { get; set; } + public TcpSshPropertiesConfig tcpSshProperties { get; set; } + + // convert properties for simpl + public ushort simplControlPortNumber { get { return Convert.ToUInt16(controlPortNumber); } } + + /// + /// Constructor + /// + public ControlConfig() + { + comParams = new ComParamsConfig(); + tcpSshProperties = new TcpSshPropertiesConfig(); + } + } + + /// + /// Device properties class + /// + public class PropertiesConfig + { + public int deviceId { get; set; } + public bool enabled { get; set; } + public ControlConfig control { get; set; } + + // convert properties for simpl + public ushort simplDeviceId { get { return Convert.ToUInt16(deviceId); } } + public ushort simplEnabled { get { return (ushort)(enabled ? 1 : 0); } } + + /// + /// Constructor + /// + public PropertiesConfig() + { + control = new ControlConfig(); + } + } + + /// + /// Root device class + /// + public class RootObject + { + public List devices { get; set; } + } +} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/JsonToSimpl/Constants.cs b/Pepperdash Core/Pepperdash Core/JsonToSimpl/Constants.cs new file mode 100644 index 0000000..0838d56 --- /dev/null +++ b/Pepperdash Core/Pepperdash Core/JsonToSimpl/Constants.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; + +namespace PepperDash.Core.JsonToSimpl +{ + /// + /// Constants for Simpl modules + /// + public class JsonToSimplConstants + { + public const ushort BoolValueChange = 1; + public const ushort JsonIsValidBoolChange = 2; + + public const ushort UshortValueChange = 101; + + public const ushort StringValueChange = 201; + public const ushort FullPathToArrayChange = 202; + public const ushort ActualFilePathChange = 203; + + // TODO: pdc-20: Added below constants for passing file path and filename back to S+ + public const ushort FilenameResolvedChange = 204; + public const ushort FilePathResolvedChange = 205; + } + + /// + /// S+ values delegate + /// + public delegate void SPlusValuesDelegate(); + + /// + /// S+ values wrapper + /// + public class SPlusValueWrapper + { + public SPlusType ValueType { get; private set; } + public ushort Index { get; private set; } + public ushort BoolUShortValue { get; set; } + public string StringValue { get; set; } + + public SPlusValueWrapper() { } + + public SPlusValueWrapper(SPlusType type, ushort index) + { + ValueType = type; + Index = index; + } + } + + /// + /// S+ types enum + /// + public enum SPlusType + { + Digital, Analog, String + } +} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/JsonToSimpl/EventArgs and Constants.cs b/Pepperdash Core/Pepperdash Core/JsonToSimpl/EventArgs and Constants.cs deleted file mode 100644 index 4fa4dee..0000000 --- a/Pepperdash Core/Pepperdash Core/JsonToSimpl/EventArgs and Constants.cs +++ /dev/null @@ -1,129 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Crestron.SimplSharp; - -namespace PepperDash.Core.JsonToSimpl -{ - /// - /// Constants for Simpl modules - /// - public class JsonToSimplConstants - { - public const ushort JsonIsValidBoolChange = 2; - - - public const ushort BoolValueChange = 1; - public const ushort UshortValueChange = 101; - public const ushort StringValueChange = 201; - public const ushort FullPathToArrayChange = 202; - public const ushort ActualFilePathChange = 203; - - // TODO: pdc-20: Added below constants for passing file path and filename back to S+ - public const ushort FilenameResolvedChange = 204; - public const ushort FilePathResolvedChange = 205; - } - - //**************************************************************************************************// - public delegate void SPlusValuesDelegate(); - - public class SPlusValueWrapper - { - public SPlusType ValueType { get; private set; } - public ushort Index { get; private set; } - public ushort BoolUShortValue { get; set; } - public string StringValue { get; set; } - - public SPlusValueWrapper() { } - - public SPlusValueWrapper(SPlusType type, ushort index) - { - ValueType = type; - Index = index; - } - } - - public enum SPlusType - { - Digital, Analog, String - } - - - //**************************************************************************************************// - public class BoolChangeEventArgs : EventArgs - { - public bool State { get; set; } - public ushort IntValue { get { return (ushort)(State ? 1 : 0); } } - public ushort Type { get; set; } - public ushort Index { get; set; } - - public BoolChangeEventArgs() - { - } - - public BoolChangeEventArgs(bool state, ushort type) - { - State = state; - Type = type; - } - - public BoolChangeEventArgs(bool state, ushort type, ushort index) - { - State = state; - Type = type; - Index = index; - } - } - - //**************************************************************************************************// - public class UshrtChangeEventArgs : EventArgs - { - public ushort IntValue { get; set; } - public ushort Type { get; set; } - public ushort Index { get; set; } - - public UshrtChangeEventArgs() - { - } - - public UshrtChangeEventArgs(ushort intValue, ushort type) - { - IntValue = intValue; - Type = type; - } - - public UshrtChangeEventArgs(ushort intValue, ushort type, ushort index) - { - IntValue = intValue; - Type = type; - Index = index; - } - } - - //**************************************************************************************************// - public class StringChangeEventArgs : EventArgs - { - public string StringValue { get; set; } - public ushort Type { get; set; } - public ushort Index { get; set; } - - public StringChangeEventArgs() - { - } - - public StringChangeEventArgs(string stringValue, ushort type) - { - StringValue = stringValue; - Type = type; - } - - public StringChangeEventArgs(string stringValue, ushort type, ushort index) - { - StringValue = stringValue; - Type = type; - Index = index; - } - - } -} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj b/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj index 8869ca9..ec3427c 100644 --- a/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj +++ b/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj @@ -86,7 +86,11 @@ + + + + @@ -96,7 +100,7 @@ - + @@ -106,6 +110,13 @@ + + + + + + + diff --git a/Pepperdash Core/Pepperdash Core/SystemInfo/ConsoleHelper.cs b/Pepperdash Core/Pepperdash Core/SystemInfo/ConsoleHelper.cs new file mode 100644 index 0000000..c38279d --- /dev/null +++ b/Pepperdash Core/Pepperdash Core/SystemInfo/ConsoleHelper.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; + +namespace PepperDash.Core.SystemInfo +{ + /// + /// Console helper class + /// + public class ConsoleHelper + { + /// + /// Cosntructor + /// + public ConsoleHelper() + { + + } + + /// + /// Parse console respopnse method + /// + /// + /// + /// + /// + /// console response + public string ParseConsoleResponse(string response, string line, string startString, string endString) + { + if (string.IsNullOrEmpty(response) || string.IsNullOrEmpty(line) || string.IsNullOrEmpty(startString) || string.IsNullOrEmpty(endString)) + return ""; + + try + { + var linePos = response.IndexOf(line); + var startPos = response.IndexOf(startString, linePos) + startString.Length; + var endPos = response.IndexOf(endString, startPos); + response = response.Substring(startPos, endPos - startPos).Trim(); + } + catch (Exception e) + { + var msg = string.Format("ConsoleHelper.ParseConsoleResponse failed:\r{0}", e); + CrestronConsole.PrintLine(msg); + ErrorLog.Error(msg); + } + + return response; + } + } +} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/SystemInfo/ControlSubnetInfo.cs b/Pepperdash Core/Pepperdash Core/SystemInfo/ControlSubnetInfo.cs new file mode 100644 index 0000000..6353339 --- /dev/null +++ b/Pepperdash Core/Pepperdash Core/SystemInfo/ControlSubnetInfo.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; + +namespace PepperDash.Core.SystemInfo +{ + /// + /// Control subnet class + /// + public class ControlSubnetConfig + { + public ushort Enabled { get; set; } + public ushort IsInAutomaticMode { get; set; } + public string MacAddress { get; set; } + public string IpAddress { get; set; } + public string Subnet { get; set; } + public string RouterPrefix { get; set; } + + /// + /// Constructor + /// + public ControlSubnetConfig() + { + // add logic here if necessary + } + } + + /// + /// Control subnet info class + /// + public class ControlSubnetInfo + { + public ControlSubnetConfig properties { get; set; } + + /// + /// Constructor + /// + public ControlSubnetInfo() + { + properties.Enabled = (ushort)0; + properties.IsInAutomaticMode = (ushort)0; + properties.MacAddress = "NA"; + properties.IpAddress = "NA"; + properties.Subnet = "NA"; + properties.RouterPrefix = "NA"; + } + + /// + /// Get control subnet info + /// + /// + public bool GetInfo() + { + try + { + // get cs adapter id + var adapterId = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(EthernetAdapterType.EthernetCSAdapter); + if (!adapterId.Equals(EthernetAdapterType.EthernetUnknownAdapter)) + { + properties.Enabled = (ushort)EthernetAdapterType.EthernetCSAdapter; + properties.IsInAutomaticMode = (ushort)(CrestronEthernetHelper.IsControlSubnetInAutomaticMode ? 1 : 0); + properties.MacAddress = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_MAC_ADDRESS, adapterId); + properties.IpAddress = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_IP_ADDRESS, adapterId); + properties.Subnet = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_IP_MASK, adapterId); + properties.RouterPrefix = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CONTROL_SUBNET_ROUTER_PREFIX, adapterId); + } + } + catch (Exception e) + { + var msg = string.Format("ControlSubnetInfo.GetInfo() failed:\r{0}", e); + CrestronConsole.PrintLine(msg); + ErrorLog.Error(msg); + return false; + } + + return true; + } + } +} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/SystemInfo/EthernetInfo.cs b/Pepperdash Core/Pepperdash Core/SystemInfo/EthernetInfo.cs new file mode 100644 index 0000000..d98d186 --- /dev/null +++ b/Pepperdash Core/Pepperdash Core/SystemInfo/EthernetInfo.cs @@ -0,0 +1,108 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; + +namespace PepperDash.Core.SystemInfo +{ + /// + /// Ethernet class + /// + public class EthernetConfig + { + public ushort DhcpIsOn { get; set; } + public string Hostname { get; set; } + public string MacAddress { get; set; } + public string IpAddress { get; set; } + public string Subnet { get; set; } + public string Gateway { get; set; } + public string Dns1 { get; set; } + public string Dns2 { get; set; } + public string Dns3 { get; set; } + public string Domain { get; set; } + + /// + /// Constructor + /// + public EthernetConfig() + { + // add logic here if necessary + } + } + + /// + /// + /// + public class EthernetInfo + { + public EthernetConfig properties { get; set; } + + /// + /// Constructor + /// + public EthernetInfo() + { + + } + + /// + /// + /// + /// + public bool GetInfo() + { + try + { + // get lan adapter id + var adapterId = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(EthernetAdapterType.EthernetLANAdapter); + + // get lan adapter info + var dhcpState = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_DHCP_STATE, adapterId); + if (!string.IsNullOrEmpty(dhcpState)) + properties.DhcpIsOn = (ushort)(dhcpState.ToLower().Contains("on") ? 1 : 0); + + properties.Hostname = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_HOSTNAME, adapterId); + properties.MacAddress = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_MAC_ADDRESS, adapterId); + properties.IpAddress = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_IP_ADDRESS, adapterId); + properties.Subnet = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_IP_MASK, adapterId); + properties.Gateway = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_ROUTER, adapterId); + properties.Domain = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_DOMAIN_NAME, adapterId); + + // returns comma seperate list of dns servers with trailing comma + // example return: "8.8.8.8 (DHCP),8.8.4.4 (DHCP)," + string dns = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_DNS_SERVER, adapterId); + if (string.IsNullOrEmpty(dns)) + { + properties.Dns1 = "0.0.0.0"; + properties.Dns2 = "0.0.0.0"; + properties.Dns3 = "0.0.0.0"; + } + + if (dns.Contains(",")) + { + string[] dnsList = dns.Split(','); + properties.Dns1 = !string.IsNullOrEmpty(dnsList[0]) ? dnsList[0] : "0.0.0.0"; + properties.Dns2 = !string.IsNullOrEmpty(dnsList[1]) ? dnsList[1] : "0.0.0.0"; + properties.Dns3 = !string.IsNullOrEmpty(dnsList[2]) ? dnsList[2] : "0.0.0.0"; + } + else + { + + properties.Dns1 = !string.IsNullOrEmpty(dns) ? dns : "0.0.0.0"; + properties.Dns2 = "0.0.0.0"; + properties.Dns3 = "0.0.0.0"; + } + } + catch(Exception e) + { + var msg = string.Format("EthernetInfo.GetInfo() failed:\r{0}", e); + CrestronConsole.PrintLine(msg); + ErrorLog.Error(msg); + return false; + } + + return true; + } + } +} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/SystemInfo/EventArgs and Constants.cs b/Pepperdash Core/Pepperdash Core/SystemInfo/EventArgs and Constants.cs new file mode 100644 index 0000000..169bf4b --- /dev/null +++ b/Pepperdash Core/Pepperdash Core/SystemInfo/EventArgs and Constants.cs @@ -0,0 +1,187 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; + +namespace PepperDash.Core.SystemInfo +{ + /// + /// Constants + /// + public class SystemInfoConstants + { + public const ushort BoolValueChange = 1; + public const ushort CompleteBoolChange = 2; + public const ushort BusyBoolChange = 3; + + public const ushort UshortValueChange = 101; + + public const ushort StringValueChange = 201; + public const ushort ConsoleResponseChange = 202; + public const ushort ProcessorUptimeChange = 203; + public const ushort ProgramUptimeChange = 204; + + public const ushort ObjectChange = 301; + public const ushort ProcessorObjectChange = 302; + public const ushort EthernetObjectChange = 303; + public const ushort ControlSubnetObjectChange = 304; + public const ushort ProgramObjectChange = 305; + } + + /// + /// Processor Change Event Args Class + /// + public class ProcessorChangeEventArgs : EventArgs + { + public ProcessorConfig Processor { get; set; } + public ushort Type { get; set; } + public ushort Index { get; set; } + + /// + /// Constructor + /// + public ProcessorChangeEventArgs() + { + + } + + /// + /// Constructor overload + /// + public ProcessorChangeEventArgs(ProcessorConfig processor, ushort type) + { + Processor = processor; + Type = type; + } + + /// + /// Constructor + /// + public ProcessorChangeEventArgs(ProcessorConfig processor, ushort type, ushort index) + { + Processor = processor; + Type = type; + Index = index; + } + } + + /// + /// Ethernet Change Event Args Class + /// + public class EthernetChangeEventArgs : EventArgs + { + public EthernetConfig Adapter { get; set; } + public ushort Type { get; set; } + public ushort Index { get; set; } + + /// + /// Constructor + /// + public EthernetChangeEventArgs() + { + + } + + /// + /// Constructor overload + /// + /// + /// + public EthernetChangeEventArgs(EthernetConfig ethernet, ushort type) + { + Adapter = ethernet; + Type = type; + } + + /// + /// Constructor overload + /// + /// + /// + public EthernetChangeEventArgs(EthernetConfig ethernet, ushort type, ushort index) + { + Adapter = ethernet; + Type = type; + Index = index; + } + } + + /// + /// Control Subnet Chage Event Args Class + /// + public class ControlSubnetChangeEventArgs : EventArgs + { + public ControlSubnetConfig Adapter { get; set; } + public ushort Type { get; set; } + public ushort Index { get; set; } + + /// + /// Constructor + /// + public ControlSubnetChangeEventArgs() + { + + } + + /// + /// Constructor overload + /// + public ControlSubnetChangeEventArgs(ControlSubnetConfig controlSubnet, ushort type) + { + Adapter = controlSubnet; + Type = type; + } + + /// + /// Constructor overload + /// + public ControlSubnetChangeEventArgs(ControlSubnetConfig controlSubnet, ushort type, ushort index) + { + Adapter = controlSubnet; + Type = type; + Index = index; + } + } + + /// + /// Program Change Event Args Class + /// + public class ProgramChangeEventArgs : EventArgs + { + public ProgramConfig Program { get; set; } + public ushort Type { get; set; } + public ushort Index { get; set; } + + /// + /// Constructor + /// + public ProgramChangeEventArgs() + { + + } + + /// + /// Constructor overload + /// + /// + /// + public ProgramChangeEventArgs(ProgramConfig program, ushort type) + { + Program = program; + Type = type; + } + + /// + /// Constructor overload + /// + /// + /// + public ProgramChangeEventArgs(ProgramConfig program, ushort type, ushort index) + { + Program = program; + Type = type; + Index = index; + } + } +} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/SystemInfo/ProcessorInfo.cs b/Pepperdash Core/Pepperdash Core/SystemInfo/ProcessorInfo.cs new file mode 100644 index 0000000..701956e --- /dev/null +++ b/Pepperdash Core/Pepperdash Core/SystemInfo/ProcessorInfo.cs @@ -0,0 +1,121 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; + +namespace PepperDash.Core.SystemInfo +{ + /// + /// Processor properties class + /// + public class ProcessorConfig + { + public string Model { get; set; } + public string SerialNumber { get; set; } + public string Firmware { get; set; } + public string FirmwareDate { get; set; } + public string OsVersion { get; set; } + public string RuntimeEnvironment { get; set; } + public string DevicePlatform { get; set; } + public string ModuleDirectory { get; set; } + public string LocalTimeZone { get; set; } + public string ProgramIdTag { get; set; } + + /// + /// Constructor + /// + public ProcessorConfig() + { + Model = ""; + SerialNumber = ""; + Firmware = ""; + FirmwareDate = ""; + OsVersion = ""; + RuntimeEnvironment = ""; + DevicePlatform = ""; + ModuleDirectory = ""; + LocalTimeZone = ""; + ProgramIdTag = ""; + } + } + + /// + /// + /// + public class ProcessorInfo + { + public ProcessorConfig properties { get; set; } + + /// + /// Constructor + /// + public ProcessorInfo() + { + properties.Model = ""; + properties.SerialNumber = ""; + properties.Firmware = ""; + properties.FirmwareDate = ""; + properties.OsVersion = ""; + properties.RuntimeEnvironment = ""; + properties.DevicePlatform = ""; + properties.ModuleDirectory = ""; + properties.LocalTimeZone = ""; + properties.ProgramIdTag = ""; + } + + /// + /// Get processor info method + /// + /// bool + public bool GetInfo() + { + var console = new ConsoleHelper(); + + try + { + properties.Model = InitialParametersClass.ControllerPromptName; + properties.SerialNumber = CrestronEnvironment.SystemInfo.SerialNumber; + properties.ModuleDirectory = InitialParametersClass.ProgramDirectory.ToString(); + properties.ProgramIdTag = InitialParametersClass.ProgramIDTag; + properties.DevicePlatform = CrestronEnvironment.DevicePlatform.ToString(); + properties.OsVersion = CrestronEnvironment.OSVersion.Version.ToString(); + properties.RuntimeEnvironment = CrestronEnvironment.RuntimeEnvironment.ToString(); + properties.LocalTimeZone = CrestronEnvironment.GetTimeZone().Offset; + + // Does not return firmware version matching a "ver" command + // returns the "ver -v" 'CAB' version + // example return ver -v: + // RMC3 Cntrl Eng [v1.503.3568.25373 (Oct 09 2018), #4001E302] @E-00107f4420f0 + // Build: 14:05:46 Oct 09 2018 (3568.25373) + // Cab: 1.503.0070 + // Applications: 1.0.6855.21351 + // Updater: 1.4.24 + // Bootloader: 1.22.00 + // RMC3-SetupProgram: 1.003.0011 + // IOPVersion: FPGA [v09] slot:7 + // PUF: Unknown + //Firmware = CrestronEnvironment.OSVersion.Firmware; + //Firmware = InitialParametersClass.FirmwareVersion; + + // Use below logic to get actual firmware ver, not the 'CAB' returned by the above + // matches console return of a "ver" and on SystemInfo page + // example return ver: + // RMC3 Cntrl Eng [v1.503.3568.25373 (Oct 09 2018), #4001E302] @E-00107f4420f0 + var response = ""; + CrestronConsole.SendControlSystemCommand("ver", ref response); + properties.Firmware = console.ParseConsoleResponse(response, "Cntrl Eng", "[", "("); + properties.FirmwareDate = console.ParseConsoleResponse(response, "Cntrl Eng", "(", ")"); + } + catch (Exception e) + { + var msg = string.Format("ProcessorInfo.GetInfo() failed:\r{0}", e); + CrestronConsole.PrintLine(msg); + ErrorLog.Error(msg); + return false; + } + + return true; + } + } +} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/SystemInfo/ProgramInfo.cs b/Pepperdash Core/Pepperdash Core/SystemInfo/ProgramInfo.cs new file mode 100644 index 0000000..46cebd1 --- /dev/null +++ b/Pepperdash Core/Pepperdash Core/SystemInfo/ProgramInfo.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; + +namespace PepperDash.Core.SystemInfo +{ + /// + /// Program class + /// + public class ProgramConfig + { + public string Key { get; set; } + public string Name { get; set; } + public string Header { get; set; } + public string System { get; set; } + public string CompileTime { get; set; } + public string Database { get; set; } + public string Environment { get; set; } + public string Programmer { get; set; } + + /// + /// Constructor + /// + public ProgramConfig() + { + // add logic here if necessary + } + } + + /// + /// Program info class + /// + public class ProgramInfo + { + public ProgramConfig properties { get; set; } + + /// + /// Constructor + /// + public ProgramInfo() + { + + } + + /// + /// Get program info by index + /// + /// + /// + public bool GetInfoByIndex(int index) + { + var console = new ConsoleHelper(); + + try + { + string response = ""; + CrestronConsole.SendControlSystemCommand(string.Format("progcomments:{0}", index), ref response); + + // SIMPL returns + properties.Name = console.ParseConsoleResponse(response, "Program File", ":", "\x0D"); + properties.System = console.ParseConsoleResponse(response, "System Name", ":", "\x0D"); + properties.Programmer = console.ParseConsoleResponse(response, "Programmer", ":", "\x0D"); + properties.CompileTime = console.ParseConsoleResponse(response, "Compiled On", ":", "\x0D"); + properties.Database = console.ParseConsoleResponse(response, "CrestronDB", ":", "\x0D"); + properties.Environment = console.ParseConsoleResponse(response, "Source Env", ":", "\x0D"); + + // S# returns + if (properties.System.Length == 0) + properties.System = console.ParseConsoleResponse(response, "Application Name", ":", "\x0D"); + if (properties.Database.Length == 0) + properties.Database = console.ParseConsoleResponse(response, "PlugInVersion", ":", "\x0D"); + if (properties.Environment.Length == 0) + properties.Environment = console.ParseConsoleResponse(response, "Program Tool", ":", "\x0D"); + } + catch (Exception e) + { + var msg = string.Format("ProgramInfo.GetInfoByIndex({0}) failed:\r{1}", index, e); + CrestronConsole.PrintLine(msg); + ErrorLog.Error(msg); + return false; + } + + return true; + } + } +} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/SystemInfo/SystemInfoToSimpl.cs b/Pepperdash Core/Pepperdash Core/SystemInfo/SystemInfoToSimpl.cs new file mode 100644 index 0000000..c86d469 --- /dev/null +++ b/Pepperdash Core/Pepperdash Core/SystemInfo/SystemInfoToSimpl.cs @@ -0,0 +1,254 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; +using Crestron.SimplSharp.CrestronIO; + +namespace PepperDash.Core.SystemInfo +{ + /// + /// Processor Info to Simpl Class + /// Ported from Technics_Core + /// + public class SystemInfoToSimpl + { + public event EventHandler BoolChange; + public event EventHandler StringChange; + public event EventHandler ProcessorChange; + public event EventHandler EthernetChange; + public event EventHandler ControlSubnetChange; + public event EventHandler ProgramChange; + + /// + /// Constructor + /// + public SystemInfoToSimpl() + { + + } + + /// + /// Get processor info method + /// + public void GetProcessorInfo() + { + OnBoolChange(true, 0, SystemInfoConstants.BusyBoolChange); + + var processor = new ProcessorInfo(); + if (processor.GetInfo() == true) + OnProcessorChange(processor.properties, 0, SystemInfoConstants.ProcessorObjectChange); + + OnBoolChange(false, 0, SystemInfoConstants.BusyBoolChange); + } + + /// + /// Get processor uptime method + /// + public void RefreshProcessorUptime() + { + var console = new ConsoleHelper(); + + try + { + string response = ""; + CrestronConsole.SendControlSystemCommand("uptime", ref response); + var uptime = console.ParseConsoleResponse(response, "running for", "running for", "\x0D"); + OnStringChange(uptime, 0, SystemInfoConstants.ProcessorUptimeChange); + } + catch (Exception e) + { + var msg = string.Format("RefreshProcessorUptime failed:\r{0}", e); + CrestronConsole.PrintLine(msg); + ErrorLog.Error(msg); + } + } + + /// + /// get ethernet info method + /// + public void GetEthernetInfo() + { + OnBoolChange(true, 0, SystemInfoConstants.BusyBoolChange); + + var ethernet = new EthernetInfo(); + if(ethernet.GetInfo() == true) + OnEthernetInfoChange(ethernet.properties, 0, SystemInfoConstants.ObjectChange); + + OnBoolChange(false, 0, SystemInfoConstants.BusyBoolChange); + } + + /// + /// Get control subnet ethernet info method + /// + public void GetControlSubnetInfo() + { + OnBoolChange(true, 0, SystemInfoConstants.BusyBoolChange); + + var ethernet = new ControlSubnetInfo(); + if(ethernet.GetInfo() == true) + OnControlSubnetInfoChange(ethernet.properties, 0, SystemInfoConstants.ObjectChange); + + OnBoolChange(false, 0, SystemInfoConstants.BusyBoolChange); + } + + /// + /// Get program info by index method + /// + /// + public void GetProgramInfoByIndex(ushort index) + { + OnBoolChange(true, 0, SystemInfoConstants.BusyBoolChange); + + var program = new ProgramInfo(); + if (program.GetInfoByIndex(index) == true) + OnProgramChange(program.properties, index, SystemInfoConstants.ObjectChange); + + OnBoolChange(false, 0, SystemInfoConstants.BusyBoolChange); + } + + /// + /// Refresh program uptime by index + /// + /// + public void RefreshProgramUptimeByIndex(int index) + { + var console = new ConsoleHelper(); + + try + { + string response = ""; + CrestronConsole.SendControlSystemCommand(string.Format("proguptime:{0}", index), ref response); + string uptime = console.ParseConsoleResponse(response, "running for", "running for", "\x0D"); + OnStringChange(uptime, (ushort)index, SystemInfoConstants.ProgramUptimeChange); + } + catch (Exception e) + { + var msg = string.Format("RefreshProgramUptimebyIndex({0}) failed:\r{1}", index, e); + CrestronConsole.PrintLine(msg); + ErrorLog.Error(msg); + } + } + + /// + /// Send console command + /// + /// + public void SendConsoleCommand(string cmd) + { + if (string.IsNullOrEmpty(cmd)) + return; + + string consoleResponse = ""; + CrestronConsole.SendControlSystemCommand(cmd, ref consoleResponse); + if (!string.IsNullOrEmpty(consoleResponse)) + { + if (consoleResponse.Contains("\x0D\x0A")) + consoleResponse.Trim('\n'); + OnStringChange(consoleResponse, 0, SystemInfoConstants.ConsoleResponseChange); + } + } + + /// + /// Boolean change event handler + /// + /// + /// + /// + protected void OnBoolChange(bool state, ushort index, ushort type) + { + var handler = BoolChange; + if (handler != null) + { + var args = new BoolChangeEventArgs(state, type); + args.Index = index; + BoolChange(this, args); + } + } + + /// + /// String change event handler + /// + /// + /// + /// + protected void OnStringChange(string value, ushort index, ushort type) + { + var handler = StringChange; + if (handler != null) + { + var args = new StringChangeEventArgs(value, type); + args.Index = index; + StringChange(this, args); + } + } + + /// + /// Processor change event handler + /// + /// + /// + /// + protected void OnProcessorChange(ProcessorConfig processor, ushort index, ushort type) + { + var handler = ProcessorChange; + if (handler != null) + { + var args = new ProcessorChangeEventArgs(processor, type); + args.Index = index; + ProcessorChange(this, args); + } + } + + /// + /// Ethernet change event handler + /// + /// + /// + /// + protected void OnEthernetInfoChange(EthernetConfig ethernet, ushort index, ushort type) + { + var handler = EthernetChange; + if (handler != null) + { + var args = new EthernetChangeEventArgs(ethernet, type); + args.Index = index; + EthernetChange(this, args); + } + } + + /// + /// Control Subnet change event handler + /// + /// + /// + /// + protected void OnControlSubnetInfoChange(ControlSubnetConfig ethernet, ushort index, ushort type) + { + var handler = ControlSubnetChange; + if (handler != null) + { + var args = new ControlSubnetChangeEventArgs(ethernet, type); + args.Index = index; + ControlSubnetChange(this, args); + } + } + + /// + /// Program change event handler + /// + /// + /// + /// + protected void OnProgramChange(ProgramConfig program, ushort index, ushort type) + { + var handler = ProgramChange; + if (handler != null) + { + var args = new ProgramChangeEventArgs(program, type); + args.Index = index; + ProgramChange(this, args); + } + } + } +} \ No newline at end of file From 7a636b78e2b8fe481e8c51f317124b2108070414 Mon Sep 17 00:00:00 2001 From: Jason DeVito Date: Sat, 12 Oct 2019 00:25:47 -0500 Subject: [PATCH 7/9] Completed SystemInfo class, created example program for testing. PepperDash Core updates ready for review. Additional changes made to core: Moved EventArgs from JsonToSimpl to root of core for access to the Bool, Ushrt, String change event args classes --- .../Pepperdash Core/PepperDash_Core.csproj | 8 +- .../SystemInfo/ConsoleHelper.cs | 52 --- .../SystemInfo/ControlSubnetInfo.cs | 81 ----- .../SystemInfo/EthernetInfo.cs | 108 ------ .../SystemInfo/EventArgs and Constants.cs | 32 +- .../SystemInfo/ProcessorInfo.cs | 121 ------- .../Pepperdash Core/SystemInfo/ProgramInfo.cs | 88 ----- .../SystemInfo/SystemInfoConfig.cs | 102 ++++++ .../SystemInfo/SystemInfoToSimpl.cs | 325 ++++++++++++++---- 9 files changed, 377 insertions(+), 540 deletions(-) delete mode 100644 Pepperdash Core/Pepperdash Core/SystemInfo/ConsoleHelper.cs delete mode 100644 Pepperdash Core/Pepperdash Core/SystemInfo/ControlSubnetInfo.cs delete mode 100644 Pepperdash Core/Pepperdash Core/SystemInfo/EthernetInfo.cs delete mode 100644 Pepperdash Core/Pepperdash Core/SystemInfo/ProcessorInfo.cs delete mode 100644 Pepperdash Core/Pepperdash Core/SystemInfo/ProgramInfo.cs create mode 100644 Pepperdash Core/Pepperdash Core/SystemInfo/SystemInfoConfig.cs diff --git a/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj b/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj index ec3427c..5cfaffc 100644 --- a/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj +++ b/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj @@ -110,14 +110,10 @@ - - - - - - + + diff --git a/Pepperdash Core/Pepperdash Core/SystemInfo/ConsoleHelper.cs b/Pepperdash Core/Pepperdash Core/SystemInfo/ConsoleHelper.cs deleted file mode 100644 index c38279d..0000000 --- a/Pepperdash Core/Pepperdash Core/SystemInfo/ConsoleHelper.cs +++ /dev/null @@ -1,52 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Crestron.SimplSharp; - -namespace PepperDash.Core.SystemInfo -{ - /// - /// Console helper class - /// - public class ConsoleHelper - { - /// - /// Cosntructor - /// - public ConsoleHelper() - { - - } - - /// - /// Parse console respopnse method - /// - /// - /// - /// - /// - /// console response - public string ParseConsoleResponse(string response, string line, string startString, string endString) - { - if (string.IsNullOrEmpty(response) || string.IsNullOrEmpty(line) || string.IsNullOrEmpty(startString) || string.IsNullOrEmpty(endString)) - return ""; - - try - { - var linePos = response.IndexOf(line); - var startPos = response.IndexOf(startString, linePos) + startString.Length; - var endPos = response.IndexOf(endString, startPos); - response = response.Substring(startPos, endPos - startPos).Trim(); - } - catch (Exception e) - { - var msg = string.Format("ConsoleHelper.ParseConsoleResponse failed:\r{0}", e); - CrestronConsole.PrintLine(msg); - ErrorLog.Error(msg); - } - - return response; - } - } -} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/SystemInfo/ControlSubnetInfo.cs b/Pepperdash Core/Pepperdash Core/SystemInfo/ControlSubnetInfo.cs deleted file mode 100644 index 6353339..0000000 --- a/Pepperdash Core/Pepperdash Core/SystemInfo/ControlSubnetInfo.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Crestron.SimplSharp; - -namespace PepperDash.Core.SystemInfo -{ - /// - /// Control subnet class - /// - public class ControlSubnetConfig - { - public ushort Enabled { get; set; } - public ushort IsInAutomaticMode { get; set; } - public string MacAddress { get; set; } - public string IpAddress { get; set; } - public string Subnet { get; set; } - public string RouterPrefix { get; set; } - - /// - /// Constructor - /// - public ControlSubnetConfig() - { - // add logic here if necessary - } - } - - /// - /// Control subnet info class - /// - public class ControlSubnetInfo - { - public ControlSubnetConfig properties { get; set; } - - /// - /// Constructor - /// - public ControlSubnetInfo() - { - properties.Enabled = (ushort)0; - properties.IsInAutomaticMode = (ushort)0; - properties.MacAddress = "NA"; - properties.IpAddress = "NA"; - properties.Subnet = "NA"; - properties.RouterPrefix = "NA"; - } - - /// - /// Get control subnet info - /// - /// - public bool GetInfo() - { - try - { - // get cs adapter id - var adapterId = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(EthernetAdapterType.EthernetCSAdapter); - if (!adapterId.Equals(EthernetAdapterType.EthernetUnknownAdapter)) - { - properties.Enabled = (ushort)EthernetAdapterType.EthernetCSAdapter; - properties.IsInAutomaticMode = (ushort)(CrestronEthernetHelper.IsControlSubnetInAutomaticMode ? 1 : 0); - properties.MacAddress = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_MAC_ADDRESS, adapterId); - properties.IpAddress = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_IP_ADDRESS, adapterId); - properties.Subnet = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_IP_MASK, adapterId); - properties.RouterPrefix = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CONTROL_SUBNET_ROUTER_PREFIX, adapterId); - } - } - catch (Exception e) - { - var msg = string.Format("ControlSubnetInfo.GetInfo() failed:\r{0}", e); - CrestronConsole.PrintLine(msg); - ErrorLog.Error(msg); - return false; - } - - return true; - } - } -} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/SystemInfo/EthernetInfo.cs b/Pepperdash Core/Pepperdash Core/SystemInfo/EthernetInfo.cs deleted file mode 100644 index d98d186..0000000 --- a/Pepperdash Core/Pepperdash Core/SystemInfo/EthernetInfo.cs +++ /dev/null @@ -1,108 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Crestron.SimplSharp; - -namespace PepperDash.Core.SystemInfo -{ - /// - /// Ethernet class - /// - public class EthernetConfig - { - public ushort DhcpIsOn { get; set; } - public string Hostname { get; set; } - public string MacAddress { get; set; } - public string IpAddress { get; set; } - public string Subnet { get; set; } - public string Gateway { get; set; } - public string Dns1 { get; set; } - public string Dns2 { get; set; } - public string Dns3 { get; set; } - public string Domain { get; set; } - - /// - /// Constructor - /// - public EthernetConfig() - { - // add logic here if necessary - } - } - - /// - /// - /// - public class EthernetInfo - { - public EthernetConfig properties { get; set; } - - /// - /// Constructor - /// - public EthernetInfo() - { - - } - - /// - /// - /// - /// - public bool GetInfo() - { - try - { - // get lan adapter id - var adapterId = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(EthernetAdapterType.EthernetLANAdapter); - - // get lan adapter info - var dhcpState = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_DHCP_STATE, adapterId); - if (!string.IsNullOrEmpty(dhcpState)) - properties.DhcpIsOn = (ushort)(dhcpState.ToLower().Contains("on") ? 1 : 0); - - properties.Hostname = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_HOSTNAME, adapterId); - properties.MacAddress = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_MAC_ADDRESS, adapterId); - properties.IpAddress = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_IP_ADDRESS, adapterId); - properties.Subnet = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_IP_MASK, adapterId); - properties.Gateway = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_ROUTER, adapterId); - properties.Domain = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_DOMAIN_NAME, adapterId); - - // returns comma seperate list of dns servers with trailing comma - // example return: "8.8.8.8 (DHCP),8.8.4.4 (DHCP)," - string dns = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_DNS_SERVER, adapterId); - if (string.IsNullOrEmpty(dns)) - { - properties.Dns1 = "0.0.0.0"; - properties.Dns2 = "0.0.0.0"; - properties.Dns3 = "0.0.0.0"; - } - - if (dns.Contains(",")) - { - string[] dnsList = dns.Split(','); - properties.Dns1 = !string.IsNullOrEmpty(dnsList[0]) ? dnsList[0] : "0.0.0.0"; - properties.Dns2 = !string.IsNullOrEmpty(dnsList[1]) ? dnsList[1] : "0.0.0.0"; - properties.Dns3 = !string.IsNullOrEmpty(dnsList[2]) ? dnsList[2] : "0.0.0.0"; - } - else - { - - properties.Dns1 = !string.IsNullOrEmpty(dns) ? dns : "0.0.0.0"; - properties.Dns2 = "0.0.0.0"; - properties.Dns3 = "0.0.0.0"; - } - } - catch(Exception e) - { - var msg = string.Format("EthernetInfo.GetInfo() failed:\r{0}", e); - CrestronConsole.PrintLine(msg); - ErrorLog.Error(msg); - return false; - } - - return true; - } - } -} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/SystemInfo/EventArgs and Constants.cs b/Pepperdash Core/Pepperdash Core/SystemInfo/EventArgs and Constants.cs index 169bf4b..4e6b500 100644 --- a/Pepperdash Core/Pepperdash Core/SystemInfo/EventArgs and Constants.cs +++ b/Pepperdash Core/Pepperdash Core/SystemInfo/EventArgs and Constants.cs @@ -23,10 +23,10 @@ namespace PepperDash.Core.SystemInfo public const ushort ProgramUptimeChange = 204; public const ushort ObjectChange = 301; - public const ushort ProcessorObjectChange = 302; - public const ushort EthernetObjectChange = 303; - public const ushort ControlSubnetObjectChange = 304; - public const ushort ProgramObjectChange = 305; + public const ushort ProcessorConfigChange = 302; + public const ushort EthernetConfigChange = 303; + public const ushort ControlSubnetConfigChange = 304; + public const ushort ProgramConfigChange = 305; } /// @@ -34,7 +34,7 @@ namespace PepperDash.Core.SystemInfo /// public class ProcessorChangeEventArgs : EventArgs { - public ProcessorConfig Processor { get; set; } + public ProcessorInfo Processor { get; set; } public ushort Type { get; set; } public ushort Index { get; set; } @@ -49,7 +49,7 @@ namespace PepperDash.Core.SystemInfo /// /// Constructor overload /// - public ProcessorChangeEventArgs(ProcessorConfig processor, ushort type) + public ProcessorChangeEventArgs(ProcessorInfo processor, ushort type) { Processor = processor; Type = type; @@ -58,7 +58,7 @@ namespace PepperDash.Core.SystemInfo /// /// Constructor /// - public ProcessorChangeEventArgs(ProcessorConfig processor, ushort type, ushort index) + public ProcessorChangeEventArgs(ProcessorInfo processor, ushort type, ushort index) { Processor = processor; Type = type; @@ -71,7 +71,7 @@ namespace PepperDash.Core.SystemInfo /// public class EthernetChangeEventArgs : EventArgs { - public EthernetConfig Adapter { get; set; } + public EthernetInfo Adapter { get; set; } public ushort Type { get; set; } public ushort Index { get; set; } @@ -88,7 +88,7 @@ namespace PepperDash.Core.SystemInfo /// /// /// - public EthernetChangeEventArgs(EthernetConfig ethernet, ushort type) + public EthernetChangeEventArgs(EthernetInfo ethernet, ushort type) { Adapter = ethernet; Type = type; @@ -99,7 +99,7 @@ namespace PepperDash.Core.SystemInfo /// /// /// - public EthernetChangeEventArgs(EthernetConfig ethernet, ushort type, ushort index) + public EthernetChangeEventArgs(EthernetInfo ethernet, ushort type, ushort index) { Adapter = ethernet; Type = type; @@ -112,7 +112,7 @@ namespace PepperDash.Core.SystemInfo /// public class ControlSubnetChangeEventArgs : EventArgs { - public ControlSubnetConfig Adapter { get; set; } + public ControlSubnetInfo Adapter { get; set; } public ushort Type { get; set; } public ushort Index { get; set; } @@ -127,7 +127,7 @@ namespace PepperDash.Core.SystemInfo /// /// Constructor overload /// - public ControlSubnetChangeEventArgs(ControlSubnetConfig controlSubnet, ushort type) + public ControlSubnetChangeEventArgs(ControlSubnetInfo controlSubnet, ushort type) { Adapter = controlSubnet; Type = type; @@ -136,7 +136,7 @@ namespace PepperDash.Core.SystemInfo /// /// Constructor overload /// - public ControlSubnetChangeEventArgs(ControlSubnetConfig controlSubnet, ushort type, ushort index) + public ControlSubnetChangeEventArgs(ControlSubnetInfo controlSubnet, ushort type, ushort index) { Adapter = controlSubnet; Type = type; @@ -149,7 +149,7 @@ namespace PepperDash.Core.SystemInfo /// public class ProgramChangeEventArgs : EventArgs { - public ProgramConfig Program { get; set; } + public ProgramInfo Program { get; set; } public ushort Type { get; set; } public ushort Index { get; set; } @@ -166,7 +166,7 @@ namespace PepperDash.Core.SystemInfo /// /// /// - public ProgramChangeEventArgs(ProgramConfig program, ushort type) + public ProgramChangeEventArgs(ProgramInfo program, ushort type) { Program = program; Type = type; @@ -177,7 +177,7 @@ namespace PepperDash.Core.SystemInfo /// /// /// - public ProgramChangeEventArgs(ProgramConfig program, ushort type, ushort index) + public ProgramChangeEventArgs(ProgramInfo program, ushort type, ushort index) { Program = program; Type = type; diff --git a/Pepperdash Core/Pepperdash Core/SystemInfo/ProcessorInfo.cs b/Pepperdash Core/Pepperdash Core/SystemInfo/ProcessorInfo.cs deleted file mode 100644 index 701956e..0000000 --- a/Pepperdash Core/Pepperdash Core/SystemInfo/ProcessorInfo.cs +++ /dev/null @@ -1,121 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Crestron.SimplSharp; - -namespace PepperDash.Core.SystemInfo -{ - /// - /// Processor properties class - /// - public class ProcessorConfig - { - public string Model { get; set; } - public string SerialNumber { get; set; } - public string Firmware { get; set; } - public string FirmwareDate { get; set; } - public string OsVersion { get; set; } - public string RuntimeEnvironment { get; set; } - public string DevicePlatform { get; set; } - public string ModuleDirectory { get; set; } - public string LocalTimeZone { get; set; } - public string ProgramIdTag { get; set; } - - /// - /// Constructor - /// - public ProcessorConfig() - { - Model = ""; - SerialNumber = ""; - Firmware = ""; - FirmwareDate = ""; - OsVersion = ""; - RuntimeEnvironment = ""; - DevicePlatform = ""; - ModuleDirectory = ""; - LocalTimeZone = ""; - ProgramIdTag = ""; - } - } - - /// - /// - /// - public class ProcessorInfo - { - public ProcessorConfig properties { get; set; } - - /// - /// Constructor - /// - public ProcessorInfo() - { - properties.Model = ""; - properties.SerialNumber = ""; - properties.Firmware = ""; - properties.FirmwareDate = ""; - properties.OsVersion = ""; - properties.RuntimeEnvironment = ""; - properties.DevicePlatform = ""; - properties.ModuleDirectory = ""; - properties.LocalTimeZone = ""; - properties.ProgramIdTag = ""; - } - - /// - /// Get processor info method - /// - /// bool - public bool GetInfo() - { - var console = new ConsoleHelper(); - - try - { - properties.Model = InitialParametersClass.ControllerPromptName; - properties.SerialNumber = CrestronEnvironment.SystemInfo.SerialNumber; - properties.ModuleDirectory = InitialParametersClass.ProgramDirectory.ToString(); - properties.ProgramIdTag = InitialParametersClass.ProgramIDTag; - properties.DevicePlatform = CrestronEnvironment.DevicePlatform.ToString(); - properties.OsVersion = CrestronEnvironment.OSVersion.Version.ToString(); - properties.RuntimeEnvironment = CrestronEnvironment.RuntimeEnvironment.ToString(); - properties.LocalTimeZone = CrestronEnvironment.GetTimeZone().Offset; - - // Does not return firmware version matching a "ver" command - // returns the "ver -v" 'CAB' version - // example return ver -v: - // RMC3 Cntrl Eng [v1.503.3568.25373 (Oct 09 2018), #4001E302] @E-00107f4420f0 - // Build: 14:05:46 Oct 09 2018 (3568.25373) - // Cab: 1.503.0070 - // Applications: 1.0.6855.21351 - // Updater: 1.4.24 - // Bootloader: 1.22.00 - // RMC3-SetupProgram: 1.003.0011 - // IOPVersion: FPGA [v09] slot:7 - // PUF: Unknown - //Firmware = CrestronEnvironment.OSVersion.Firmware; - //Firmware = InitialParametersClass.FirmwareVersion; - - // Use below logic to get actual firmware ver, not the 'CAB' returned by the above - // matches console return of a "ver" and on SystemInfo page - // example return ver: - // RMC3 Cntrl Eng [v1.503.3568.25373 (Oct 09 2018), #4001E302] @E-00107f4420f0 - var response = ""; - CrestronConsole.SendControlSystemCommand("ver", ref response); - properties.Firmware = console.ParseConsoleResponse(response, "Cntrl Eng", "[", "("); - properties.FirmwareDate = console.ParseConsoleResponse(response, "Cntrl Eng", "(", ")"); - } - catch (Exception e) - { - var msg = string.Format("ProcessorInfo.GetInfo() failed:\r{0}", e); - CrestronConsole.PrintLine(msg); - ErrorLog.Error(msg); - return false; - } - - return true; - } - } -} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/SystemInfo/ProgramInfo.cs b/Pepperdash Core/Pepperdash Core/SystemInfo/ProgramInfo.cs deleted file mode 100644 index 46cebd1..0000000 --- a/Pepperdash Core/Pepperdash Core/SystemInfo/ProgramInfo.cs +++ /dev/null @@ -1,88 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Crestron.SimplSharp; - -namespace PepperDash.Core.SystemInfo -{ - /// - /// Program class - /// - public class ProgramConfig - { - public string Key { get; set; } - public string Name { get; set; } - public string Header { get; set; } - public string System { get; set; } - public string CompileTime { get; set; } - public string Database { get; set; } - public string Environment { get; set; } - public string Programmer { get; set; } - - /// - /// Constructor - /// - public ProgramConfig() - { - // add logic here if necessary - } - } - - /// - /// Program info class - /// - public class ProgramInfo - { - public ProgramConfig properties { get; set; } - - /// - /// Constructor - /// - public ProgramInfo() - { - - } - - /// - /// Get program info by index - /// - /// - /// - public bool GetInfoByIndex(int index) - { - var console = new ConsoleHelper(); - - try - { - string response = ""; - CrestronConsole.SendControlSystemCommand(string.Format("progcomments:{0}", index), ref response); - - // SIMPL returns - properties.Name = console.ParseConsoleResponse(response, "Program File", ":", "\x0D"); - properties.System = console.ParseConsoleResponse(response, "System Name", ":", "\x0D"); - properties.Programmer = console.ParseConsoleResponse(response, "Programmer", ":", "\x0D"); - properties.CompileTime = console.ParseConsoleResponse(response, "Compiled On", ":", "\x0D"); - properties.Database = console.ParseConsoleResponse(response, "CrestronDB", ":", "\x0D"); - properties.Environment = console.ParseConsoleResponse(response, "Source Env", ":", "\x0D"); - - // S# returns - if (properties.System.Length == 0) - properties.System = console.ParseConsoleResponse(response, "Application Name", ":", "\x0D"); - if (properties.Database.Length == 0) - properties.Database = console.ParseConsoleResponse(response, "PlugInVersion", ":", "\x0D"); - if (properties.Environment.Length == 0) - properties.Environment = console.ParseConsoleResponse(response, "Program Tool", ":", "\x0D"); - } - catch (Exception e) - { - var msg = string.Format("ProgramInfo.GetInfoByIndex({0}) failed:\r{1}", index, e); - CrestronConsole.PrintLine(msg); - ErrorLog.Error(msg); - return false; - } - - return true; - } - } -} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/SystemInfo/SystemInfoConfig.cs b/Pepperdash Core/Pepperdash Core/SystemInfo/SystemInfoConfig.cs new file mode 100644 index 0000000..cbf5f4b --- /dev/null +++ b/Pepperdash Core/Pepperdash Core/SystemInfo/SystemInfoConfig.cs @@ -0,0 +1,102 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; + +namespace PepperDash.Core.SystemInfo +{ + /// + /// Processor info class + /// + public class ProcessorInfo + { + public string Model { get; set; } + public string SerialNumber { get; set; } + public string Firmware { get; set; } + public string FirmwareDate { get; set; } + public string OsVersion { get; set; } + public string RuntimeEnvironment { get; set; } + public string DevicePlatform { get; set; } + public string ModuleDirectory { get; set; } + public string LocalTimeZone { get; set; } + public string ProgramIdTag { get; set; } + + /// + /// Constructor + /// + public ProcessorInfo() + { + + } + } + + /// + /// Ethernet info class + /// + public class EthernetInfo + { + public ushort DhcpIsOn { get; set; } + public string Hostname { get; set; } + public string MacAddress { get; set; } + public string IpAddress { get; set; } + public string Subnet { get; set; } + public string Gateway { get; set; } + public string Dns1 { get; set; } + public string Dns2 { get; set; } + public string Dns3 { get; set; } + public string Domain { get; set; } + + /// + /// Constructor + /// + public EthernetInfo() + { + + } + } + + /// + /// Control subnet info class + /// + public class ControlSubnetInfo + { + public ushort Enabled { get; set; } + public ushort IsInAutomaticMode { get; set; } + public string MacAddress { get; set; } + public string IpAddress { get; set; } + public string Subnet { get; set; } + public string RouterPrefix { get; set; } + + /// + /// Constructor + /// + public ControlSubnetInfo() + { + + } + } + + /// + /// Program info class + /// + public class ProgramInfo + { + public string Name { get; set; } + public string Header { get; set; } + public string System { get; set; } + public string ProgramIdTag { get; set; } + public string CompileTime { get; set; } + public string Database { get; set; } + public string Environment { get; set; } + public string Programmer { get; set; } + + /// + /// Constructor + /// + public ProgramInfo() + { + + } + } +} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/SystemInfo/SystemInfoToSimpl.cs b/Pepperdash Core/Pepperdash Core/SystemInfo/SystemInfoToSimpl.cs index c86d469..80c8246 100644 --- a/Pepperdash Core/Pepperdash Core/SystemInfo/SystemInfoToSimpl.cs +++ b/Pepperdash Core/Pepperdash Core/SystemInfo/SystemInfoToSimpl.cs @@ -3,18 +3,17 @@ using System.Collections.Generic; using System.Linq; using System.Text; using Crestron.SimplSharp; -using Crestron.SimplSharp.CrestronIO; namespace PepperDash.Core.SystemInfo { /// - /// Processor Info to Simpl Class - /// Ported from Technics_Core + /// System Info class /// public class SystemInfoToSimpl { public event EventHandler BoolChange; - public event EventHandler StringChange; + public event EventHandler StringChange; + public event EventHandler ProcessorChange; public event EventHandler EthernetChange; public event EventHandler ControlSubnetChange; @@ -29,109 +28,266 @@ namespace PepperDash.Core.SystemInfo } /// - /// Get processor info method + /// Gets the current processor info /// public void GetProcessorInfo() { OnBoolChange(true, 0, SystemInfoConstants.BusyBoolChange); - var processor = new ProcessorInfo(); - if (processor.GetInfo() == true) - OnProcessorChange(processor.properties, 0, SystemInfoConstants.ProcessorObjectChange); + try + { + var processor = new ProcessorInfo(); + processor.Model = InitialParametersClass.ControllerPromptName; + processor.SerialNumber = CrestronEnvironment.SystemInfo.SerialNumber; + processor.ModuleDirectory = InitialParametersClass.ProgramDirectory.ToString(); + processor.ProgramIdTag = InitialParametersClass.ProgramIDTag; + processor.DevicePlatform = CrestronEnvironment.DevicePlatform.ToString(); + processor.OsVersion = CrestronEnvironment.OSVersion.Version.ToString(); + processor.RuntimeEnvironment = CrestronEnvironment.RuntimeEnvironment.ToString(); + processor.LocalTimeZone = CrestronEnvironment.GetTimeZone().Offset; + + // Does not return firmware version matching a "ver" command + // returns the "ver -v" 'CAB' version + // example return ver -v: + // RMC3 Cntrl Eng [v1.503.3568.25373 (Oct 09 2018), #4001E302] @E-00107f4420f0 + // Build: 14:05:46 Oct 09 2018 (3568.25373) + // Cab: 1.503.0070 + // Applications: 1.0.6855.21351 + // Updater: 1.4.24 + // Bootloader: 1.22.00 + // RMC3-SetupProgram: 1.003.0011 + // IOPVersion: FPGA [v09] slot:7 + // PUF: Unknown + //Firmware = CrestronEnvironment.OSVersion.Firmware; + //Firmware = InitialParametersClass.FirmwareVersion; + + // Use below logic to get actual firmware ver, not the 'CAB' returned by the above + // matches console return of a "ver" and on SystemInfo page + // example return ver: + // RMC3 Cntrl Eng [v1.503.3568.25373 (Oct 09 2018), #4001E302] @E-00107f4420f0 + var response = ""; + CrestronConsole.SendControlSystemCommand("ver", ref response); + processor.Firmware = ParseConsoleResponse(response, "Cntrl Eng", "[", "("); + processor.FirmwareDate = ParseConsoleResponse(response, "Cntrl Eng", "(", ")"); + + OnProcessorChange(processor, 0, SystemInfoConstants.ProcessorConfigChange); + } + catch (Exception e) + { + var msg = string.Format("GetProcessorInfo failed: {0}", e.Message); + CrestronConsole.PrintLine(msg); + //ErrorLog.Error(msg); + } OnBoolChange(false, 0, SystemInfoConstants.BusyBoolChange); } /// - /// Get processor uptime method - /// - public void RefreshProcessorUptime() - { - var console = new ConsoleHelper(); - - try - { - string response = ""; - CrestronConsole.SendControlSystemCommand("uptime", ref response); - var uptime = console.ParseConsoleResponse(response, "running for", "running for", "\x0D"); - OnStringChange(uptime, 0, SystemInfoConstants.ProcessorUptimeChange); - } - catch (Exception e) - { - var msg = string.Format("RefreshProcessorUptime failed:\r{0}", e); - CrestronConsole.PrintLine(msg); - ErrorLog.Error(msg); - } - } - - /// - /// get ethernet info method + /// Gets the current ethernet info /// public void GetEthernetInfo() { OnBoolChange(true, 0, SystemInfoConstants.BusyBoolChange); - var ethernet = new EthernetInfo(); - if(ethernet.GetInfo() == true) - OnEthernetInfoChange(ethernet.properties, 0, SystemInfoConstants.ObjectChange); + var adapter = new EthernetInfo(); + + try + { + // get lan adapter id + var adapterId = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(EthernetAdapterType.EthernetLANAdapter); + + // get lan adapter info + var dhcpState = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_DHCP_STATE, adapterId); + if (!string.IsNullOrEmpty(dhcpState)) + adapter.DhcpIsOn = (ushort)(dhcpState.ToLower().Contains("on") ? 1 : 0); + + adapter.Hostname = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_HOSTNAME, adapterId); + adapter.MacAddress = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_MAC_ADDRESS, adapterId); + adapter.IpAddress = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_IP_ADDRESS, adapterId); + adapter.Subnet = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_IP_MASK, adapterId); + adapter.Gateway = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_ROUTER, adapterId); + adapter.Domain = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_DOMAIN_NAME, adapterId); + + // returns comma seperate list of dns servers with trailing comma + // example return: "8.8.8.8 (DHCP),8.8.4.4 (DHCP)," + string dns = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_DNS_SERVER, adapterId); + if (dns.Contains(",")) + { + string[] dnsList = dns.Split(','); + for (var i = 0; i < dnsList.Length; i++) + { + if(i == 0) + adapter.Dns1 = !string.IsNullOrEmpty(dnsList[0]) ? dnsList[0] : "0.0.0.0"; + if(i == 1) + adapter.Dns2 = !string.IsNullOrEmpty(dnsList[1]) ? dnsList[1] : "0.0.0.0"; + if(i == 2) + adapter.Dns3 = !string.IsNullOrEmpty(dnsList[2]) ? dnsList[2] : "0.0.0.0"; + } + } + else + { + adapter.Dns1 = !string.IsNullOrEmpty(dns) ? dns : "0.0.0.0"; + adapter.Dns2 = "0.0.0.0"; + adapter.Dns3 = "0.0.0.0"; + } + + OnEthernetInfoChange(adapter, 0, SystemInfoConstants.EthernetConfigChange); + } + catch (Exception e) + { + var msg = string.Format("GetEthernetInfo failed: {0}", e.Message); + CrestronConsole.PrintLine(msg); + //ErrorLog.Error(msg); + } OnBoolChange(false, 0, SystemInfoConstants.BusyBoolChange); } /// - /// Get control subnet ethernet info method + /// Gets the current control subnet info /// public void GetControlSubnetInfo() { OnBoolChange(true, 0, SystemInfoConstants.BusyBoolChange); - var ethernet = new ControlSubnetInfo(); - if(ethernet.GetInfo() == true) - OnControlSubnetInfoChange(ethernet.properties, 0, SystemInfoConstants.ObjectChange); + var adapter = new ControlSubnetInfo(); + try + { + // get cs adapter id + var adapterId = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(EthernetAdapterType.EthernetCSAdapter); + if (!adapterId.Equals(EthernetAdapterType.EthernetUnknownAdapter)) + { + adapter.Enabled = 1; + adapter.IsInAutomaticMode = (ushort)(CrestronEthernetHelper.IsControlSubnetInAutomaticMode ? 1 : 0); + adapter.MacAddress = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_MAC_ADDRESS, adapterId); + adapter.IpAddress = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_IP_ADDRESS, adapterId); + adapter.Subnet = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_IP_MASK, adapterId); + adapter.RouterPrefix = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CONTROL_SUBNET_ROUTER_PREFIX, adapterId); + } + } + catch (Exception e) + { + adapter.Enabled = 0; + adapter.IsInAutomaticMode = 0; + adapter.MacAddress = "NA"; + adapter.IpAddress = "NA"; + adapter.Subnet = "NA"; + adapter.RouterPrefix = "NA"; + + var msg = string.Format("GetControlSubnetInfo failed: {0}", e.Message); + CrestronConsole.PrintLine(msg); + //ErrorLog.Error(msg); + } + + OnControlSubnetInfoChange(adapter, 0, SystemInfoConstants.ControlSubnetConfigChange); OnBoolChange(false, 0, SystemInfoConstants.BusyBoolChange); } /// - /// Get program info by index method + /// Gets the program info by index /// /// public void GetProgramInfoByIndex(ushort index) { + if (index < 1 || index > 10) + return; + OnBoolChange(true, 0, SystemInfoConstants.BusyBoolChange); - + var program = new ProgramInfo(); - if (program.GetInfoByIndex(index) == true) - OnProgramChange(program.properties, index, SystemInfoConstants.ObjectChange); - + + try + { + var response = ""; + CrestronConsole.SendControlSystemCommand(string.Format("progcomments:{0}", index), ref response); + + // no program loaded or running + if (response.Contains("Bad or Incomplete Command")) + { + program.Name = ""; + program.System = ""; + program.Programmer = ""; + program.CompileTime = ""; + program.Database = ""; + program.Environment = ""; + } + else + { + // SIMPL returns + program.Name = ParseConsoleResponse(response, "Program File", ":", "\x0D"); + program.System = ParseConsoleResponse(response, "System Name", ":", "\x0D"); + program.ProgramIdTag = ParseConsoleResponse(response, "Friendly Name", ":", "\x0D"); + program.Programmer = ParseConsoleResponse(response, "Programmer", ":", "\x0D"); + program.CompileTime = ParseConsoleResponse(response, "Compiled On", ":", "\x0D"); + program.Database = ParseConsoleResponse(response, "CrestronDB", ":", "\x0D"); + program.Environment = ParseConsoleResponse(response, "Source Env", ":", "\x0D"); + + // S# returns + if (program.System.Length == 0) + program.System = ParseConsoleResponse(response, "Application Name", ":", "\x0D"); + if (program.Database.Length == 0) + program.Database = ParseConsoleResponse(response, "PlugInVersion", ":", "\x0D"); + if (program.Environment.Length == 0) + program.Environment = ParseConsoleResponse(response, "Program Tool", ":", "\x0D"); + + } + + OnProgramChange(program, index, SystemInfoConstants.ProgramConfigChange); + } + catch (Exception e) + { + var msg = string.Format("GetProgramInfoByIndex failed: {0}", e.Message); + CrestronConsole.PrintLine(msg); + //ErrorLog.Error(msg); + } + OnBoolChange(false, 0, SystemInfoConstants.BusyBoolChange); } /// - /// Refresh program uptime by index + /// Gets the processor uptime and passes it to S+ /// - /// - public void RefreshProgramUptimeByIndex(int index) + public void RefreshProcessorUptime() { - var console = new ConsoleHelper(); - try { string response = ""; - CrestronConsole.SendControlSystemCommand(string.Format("proguptime:{0}", index), ref response); - string uptime = console.ParseConsoleResponse(response, "running for", "running for", "\x0D"); - OnStringChange(uptime, (ushort)index, SystemInfoConstants.ProgramUptimeChange); + CrestronConsole.SendControlSystemCommand("uptime", ref response); + var uptime = ParseConsoleResponse(response, "running for", "running for", "\x0D"); + OnStringChange(uptime, 0, SystemInfoConstants.ProcessorUptimeChange); } catch (Exception e) { - var msg = string.Format("RefreshProgramUptimebyIndex({0}) failed:\r{1}", index, e); + var msg = string.Format("RefreshProcessorUptime failed:\r{0}", e.Message); CrestronConsole.PrintLine(msg); - ErrorLog.Error(msg); + //ErrorLog.Error(msg); } } /// - /// Send console command + /// Gets the program uptime, by index, and passes it to S+ + /// + /// + public void RefreshProgramUptimeByIndex(int index) + { + try + { + string response = ""; + CrestronConsole.SendControlSystemCommand(string.Format("proguptime:{0}", index), ref response); + string uptime = ParseConsoleResponse(response, "running for", "running for", "\x0D"); + OnStringChange(uptime, (ushort)index, SystemInfoConstants.ProgramUptimeChange); + } + catch (Exception e) + { + var msg = string.Format("RefreshProgramUptimebyIndex({0}) failed:\r{1}", index, e.Message); + CrestronConsole.PrintLine(msg); + //ErrorLog.Error(msg); + } + } + + /// + /// Sends command to console, passes response back using string change event /// /// public void SendConsoleCommand(string cmd) @@ -139,18 +295,51 @@ namespace PepperDash.Core.SystemInfo if (string.IsNullOrEmpty(cmd)) return; - string consoleResponse = ""; - CrestronConsole.SendControlSystemCommand(cmd, ref consoleResponse); - if (!string.IsNullOrEmpty(consoleResponse)) + string response = ""; + CrestronConsole.SendControlSystemCommand(cmd, ref response); + if (!string.IsNullOrEmpty(response)) { - if (consoleResponse.Contains("\x0D\x0A")) - consoleResponse.Trim('\n'); - OnStringChange(consoleResponse, 0, SystemInfoConstants.ConsoleResponseChange); + if (response.EndsWith("\x0D\\x0A")) + response.Trim('\n'); + + OnStringChange(response, 0, SystemInfoConstants.ConsoleResponseChange); } - } + } /// - /// Boolean change event handler + /// private method to parse console messages + /// + /// + /// + /// + /// + /// + private string ParseConsoleResponse(string data, string line, string dataStart, string dataEnd) + { + var response = ""; + + if (string.IsNullOrEmpty(data) || string.IsNullOrEmpty(line) || string.IsNullOrEmpty(dataStart) || string.IsNullOrEmpty(dataEnd)) + return response; + + try + { + var linePos = data.IndexOf(line); + var startPos = data.IndexOf(dataStart, linePos) + dataStart.Length; + var endPos = data.IndexOf(dataEnd, startPos); + response = data.Substring(startPos, endPos - startPos).Trim(); + } + catch (Exception e) + { + var msg = string.Format("ParseConsoleResponse failed: {0}", e.Message); + CrestronConsole.PrintLine(msg); + //ErrorLog.Error(msg); + } + + return response; + } + + /// + /// Protected boolean change event handler /// /// /// @@ -167,7 +356,7 @@ namespace PepperDash.Core.SystemInfo } /// - /// String change event handler + /// Protected string change event handler /// /// /// @@ -184,12 +373,12 @@ namespace PepperDash.Core.SystemInfo } /// - /// Processor change event handler + /// Protected processor config change event handler /// /// /// /// - protected void OnProcessorChange(ProcessorConfig processor, ushort index, ushort type) + protected void OnProcessorChange(ProcessorInfo processor, ushort index, ushort type) { var handler = ProcessorChange; if (handler != null) @@ -206,7 +395,7 @@ namespace PepperDash.Core.SystemInfo /// /// /// - protected void OnEthernetInfoChange(EthernetConfig ethernet, ushort index, ushort type) + protected void OnEthernetInfoChange(EthernetInfo ethernet, ushort index, ushort type) { var handler = EthernetChange; if (handler != null) @@ -223,7 +412,7 @@ namespace PepperDash.Core.SystemInfo /// /// /// - protected void OnControlSubnetInfoChange(ControlSubnetConfig ethernet, ushort index, ushort type) + protected void OnControlSubnetInfoChange(ControlSubnetInfo ethernet, ushort index, ushort type) { var handler = ControlSubnetChange; if (handler != null) @@ -240,7 +429,7 @@ namespace PepperDash.Core.SystemInfo /// /// /// - protected void OnProgramChange(ProgramConfig program, ushort index, ushort type) + protected void OnProgramChange(ProgramInfo program, ushort index, ushort type) { var handler = ProgramChange; if (handler != null) From 8174ece6509a5afc3eb1eb47e1f12f14bd2c27f5 Mon Sep 17 00:00:00 2001 From: Jason DeVito Date: Sat, 12 Oct 2019 00:32:36 -0500 Subject: [PATCH 8/9] Completed adding GenericRESTful client to PepperDash Core. Built sample program to test updates made. --- .../{GenericRESTful.cs => GenericRESTfulClient.cs} | 0 Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename Pepperdash Core/Pepperdash Core/GenericRESTfulCommunications/{GenericRESTful.cs => GenericRESTfulClient.cs} (100%) diff --git a/Pepperdash Core/Pepperdash Core/GenericRESTfulCommunications/GenericRESTful.cs b/Pepperdash Core/Pepperdash Core/GenericRESTfulCommunications/GenericRESTfulClient.cs similarity index 100% rename from Pepperdash Core/Pepperdash Core/GenericRESTfulCommunications/GenericRESTful.cs rename to Pepperdash Core/Pepperdash Core/GenericRESTfulCommunications/GenericRESTfulClient.cs diff --git a/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj b/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj index 5cfaffc..e0fef86 100644 --- a/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj +++ b/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj @@ -88,7 +88,7 @@ - + From c205f2b671ece17a65fbdbd345e0c445277a486a Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Tue, 15 Oct 2019 15:48:43 -0600 Subject: [PATCH 9/9] Added echo statement to PS Script --- .../Pepperdash Core/Properties/UpdateAssemblyVersion.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/Pepperdash Core/Pepperdash Core/Properties/UpdateAssemblyVersion.ps1 b/Pepperdash Core/Pepperdash Core/Properties/UpdateAssemblyVersion.ps1 index 46392b3..f1c6c38 100644 --- a/Pepperdash Core/Pepperdash Core/Properties/UpdateAssemblyVersion.ps1 +++ b/Pepperdash Core/Pepperdash Core/Properties/UpdateAssemblyVersion.ps1 @@ -24,6 +24,7 @@ function Update-AllAssemblyInfoFiles ( $version ) $r= [System.Text.RegularExpressions.Regex]::Match($args[0], "^\d+\.\d+\.\d+$"); if ($r.Success) { + echo "Updating Assembly Version..."; Update-AllAssemblyInfoFiles $args[0]; } else