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