From c9b06ec3caccedc2d2af5eae8b820a5fa75ed58f Mon Sep 17 00:00:00 2001 From: Jason DeVito Date: Tue, 1 Oct 2019 13:54:26 -0500 Subject: [PATCH] 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 @@ + +