/// <summary> /// Converts a value to be sent to the provider. /// </summary> /// <param name="value">The value to convert</param> /// <param name="formula">The formula string as returned by the ConsumerToProvider /// property of class EmberLib.Glow.GlowFormulaSource. Pass null if not present.</param> /// <param name="factor">A factor used to convert fixed-point real values to integer /// values as defined by the "factor" field of EmberPlus-Glow.ParameterContents.</param> /// <param name="log">A callback function invoked to log formula compilation errors.</param> /// <returns>The converted value.</returns> /// <remarks>This method caches formula compilation results in a global cache. This method is thread-safe.</remarks> public GlowValue Convert(GlowValue value, string formula, int?factor, Action <string> log = null) { if (String.IsNullOrEmpty(formula) == false) { if (value.Type == GlowParameterType.Integer || value.Type == GlowParameterType.Real) { var result = ValueConversion.CompileFormula(formula, log); if (result.Success) { if (value.Type == GlowParameterType.Integer) { value = result.Formula.Eval(value.Integer); } else { value = result.Formula.Eval(value.Real); } } } } else { if (factor != null && value.Type == GlowParameterType.Real) { value = new GlowValue((long)(value.Real * (double)factor.Value)); } } return(value); }
/// <summary> /// Converts a value received from the provider. /// </summary> /// <param name="value">The value to convert</param> /// <param name="format">The stream descriptor format as defined by the "format" field of /// EmberPlus-Glow.StreamDescription. Only applies if value was received through octet /// string streaming. Pass null if not present.</param> /// <param name="offset">The stream descriptor offset as defined by the "offset" field of /// EmberPlus-Glow.StreamDescription. Only applies if value was received through octet /// string streaming. Pass null if not present.</param> /// <param name="formula">The formula string as returned by the ProviderToConsumer /// property of class EmberLib.Glow.GlowFormulaSource. Pass null if not present.</param> /// <param name="factor">A factor used to convert integer value into fixed-point /// real values as defined by the "factor" field of EmberPlus-Glow.ParameterContents.</param> /// <param name="log">A callback function invoked to log formula compilation errors.</param> /// <returns>The converted value.</returns> /// <remarks>This method caches formula compilation results in a global cache. This method is thread-safe.</remarks> public GlowValue Convert(GlowValue value, int? format, int? offset, string formula, int? factor, Action<string> log = null) { if(format != null && offset != null && value.Type == GlowParameterType.Octets) value = ValueConversion.ValueFromOctets(value.Octets, format.Value, offset.Value); if(String.IsNullOrEmpty(formula) == false) { if(value.Type == GlowParameterType.Integer || value.Type == GlowParameterType.Real) { var result = ValueConversion.CompileFormula(formula, log); if(result.Success) { if(value.Type == GlowParameterType.Integer) value = result.Formula.Eval(value.Integer); else value = result.Formula.Eval(value.Real); } } } else { if(factor != null && value.Type == GlowParameterType.Integer) value = new GlowValue(value.Integer / (double)factor.Value); } return value; }
/// <summary> /// Converts a value to be sent to the provider. /// </summary> /// <param name="value">The value to convert</param> /// <param name="formula">The formula string as returned by the ConsumerToProvider /// property of class EmberLib.Glow.GlowFormulaSource. Pass null if not present.</param> /// <param name="factor">A factor used to convert fixed-point real values to integer /// values as defined by the "factor" field of EmberPlus-Glow.ParameterContents.</param> /// <param name="log">A callback function invoked to log formula compilation errors.</param> /// <returns>The converted value.</returns> /// <remarks>This method caches formula compilation results in a global cache. This method is thread-safe.</remarks> public GlowValue Convert(GlowValue value, string formula, int? factor, Action<string> log = null) { if(String.IsNullOrEmpty(formula) == false) { if(value.Type == GlowParameterType.Integer || value.Type == GlowParameterType.Real) { var result = ValueConversion.CompileFormula(formula, log); if(result.Success) { if(value.Type == GlowParameterType.Integer) value = result.Formula.Eval(value.Integer); else value = result.Formula.Eval(value.Real); } } } else { if(factor != null && value.Type == GlowParameterType.Real) value = new GlowValue((long)(value.Real * (double)factor.Value)); } return value; }
void ConvertValue(string elementName, GlowValue value, XmlWriter writer) { var typeStr = ConvertParameterType(value.Type); if (typeStr != null) { writer.WriteStartElement(elementName); writer.WriteAttributeString("type", typeStr); writer.WriteString(value.ToString(CultureInfo.InvariantCulture)); writer.WriteEndElement(); } }
public void NotifyParameterValueChanged(int[] parameterPath, GlowValue value) { var glowParam = new GlowQualifiedParameter(parameterPath) { Value = value, }; var glow = GlowRootElementCollection.CreateRoot(); glow.Insert(glowParam); OnGlowRootReady(new GlowRootReadyArgs(glow, null)); }
GlowValue ConvertValue(XElement xml) { var type = ConvertParameterType(xml.Attribute("type").Value); if (type != null) { GlowValue value; if (GlowValue.TryParse(xml.Value, type.Value, CultureInfo.InvariantCulture, out value)) { return(value); } } return(null); }
/// <summary> /// Creates an ember/glow tree which can be used to issue /// a parameter value change to the remote host. /// </summary> /// <returns>The GlowContainer object that is the root of /// a glow tree that mirrors the element tree up this element, /// having a GlowParameter object as the single leaf.</returns> public GlowContainer SetParameterValue(GlowValue value) { if (Type != ElementType.Parameter) { throw new InvalidOperationException(); } var glow = new GlowParameter(Number) { Value = value, }; // this builds a QualifiedNode or QualifiedParameter return(BuildQualified(Parent, glow)); // this builds a complete glow tree using the Node and // Parameter types (more verbose). //return BuildGlowTree(Parent, glow); }
/// <summary> /// Converts a value received from the provider. /// </summary> /// <param name="value">The value to convert</param> /// <param name="format">The stream descriptor format as defined by the "format" field of /// EmberPlus-Glow.StreamDescription. Only applies if value was received through octet /// string streaming. Pass null if not present.</param> /// <param name="offset">The stream descriptor offset as defined by the "offset" field of /// EmberPlus-Glow.StreamDescription. Only applies if value was received through octet /// string streaming. Pass null if not present.</param> /// <param name="formula">The formula string as returned by the ProviderToConsumer /// property of class EmberLib.Glow.GlowFormulaSource. Pass null if not present.</param> /// <param name="factor">A factor used to convert integer value into fixed-point /// real values as defined by the "factor" field of EmberPlus-Glow.ParameterContents.</param> /// <param name="log">A callback function invoked to log formula compilation errors.</param> /// <returns>The converted value.</returns> /// <remarks>This method caches formula compilation results in a global cache. This method is thread-safe.</remarks> public GlowValue Convert(GlowValue value, int?format, int?offset, string formula, int?factor, Action <string> log = null) { if (format != null && offset != null && value.Type == GlowParameterType.Octets) { value = ValueConversion.ValueFromOctets(value.Octets, format.Value, offset.Value); } if (String.IsNullOrEmpty(formula) == false) { if (value.Type == GlowParameterType.Integer || value.Type == GlowParameterType.Real) { var result = ValueConversion.CompileFormula(formula, log); if (result.Success) { if (value.Type == GlowParameterType.Integer) { value = result.Formula.Eval(value.Integer); } else { value = result.Formula.Eval(value.Real); } } } } else { if (factor != null && value.Type == GlowParameterType.Integer) { value = new GlowValue(value.Integer / (double)factor.Value); } } return(value); }
void CreateGlowParameter(Item item, int[] path, int?number, int fields, GlowElementCollectionBase parent) { var glowValue = null as GlowValue; var isWriteable = false; if ((fields & GlowFieldFlags.Value) != 0) { var valueKind = item.Parent.Key.GetValueKind(item.Name); var value = item.Parent.Key.GetValue(item.Name); switch (valueKind) { case RegistryValueKind.Binary: glowValue = new GlowValue((byte[])value); break; case RegistryValueKind.DWord: glowValue = new GlowValue((long)(int)value); isWriteable = true; break; case RegistryValueKind.ExpandString: glowValue = new GlowValue((string)value); break; case RegistryValueKind.MultiString: glowValue = new GlowValue(String.Join("\n", (string[])value)); break; case RegistryValueKind.QWord: glowValue = new GlowValue((long)value); isWriteable = true; break; case RegistryValueKind.String: glowValue = new GlowValue((string)value); isWriteable = true; break; } } if (number != null) { path = path.Concat(new[] { number.Value }).ToArray(); } var glow = new GlowQualifiedParameter(path); if ((fields & GlowFieldFlags.Identifier) != 0) { glow.Identifier = item.Identifier; } if ((fields & GlowFieldFlags.Description) != 0 && String.IsNullOrEmpty(item.Name) == false) { glow.Description = item.Name; } if (fields == GlowFieldFlags.All && isWriteable) { glow.Access = GlowAccess.ReadWrite; } if (glowValue != null) { glow.Value = glowValue; } parent.Insert(glow); }
public async Task <GlowInvocationResult> Invoke(GlowInvocation invocation) { GlowValue[] arguments; int? invocationId; if (invocation == null) { if (Arguments != null) { throw new ArgumentException("Function with parameters called without arguments!"); } arguments = null; invocationId = null; } else { var argumentValues = invocation.ArgumentValues; arguments = argumentValues != null ? argumentValues.ToArray() : null; // ********************************************** // Added code // Functions without parameters can be defined in two (2) ways. Either 'args=null' or 'args=empty list' // - EmberViewer v1.6.2 sends args=null when function parameter is missing // - EmberViewer v2.4.0 sends args=empty list when function parameter is missing // So that both models will work, we create an empty list if args=null // and we create args=null if args=empty list if (Arguments != null && Arguments.Length == 0 && arguments == null) { arguments = new GlowValue[0]; } if (Arguments == null && arguments != null && arguments.Length == 0) { arguments = null; } // ************************************************ invocationId = invocation.InvocationId; AssertValueTypes(arguments, Arguments); } if (invocationId == null && HasResult) { throw new ArgumentException("Function with result called without invocation id!"); } Debug.WriteLine($"Invoking function {IdentifierPath}"); var result = await _coreFunc(arguments); AssertValueTypes(result, Result); if (invocationId != null) { var invocationResult = GlowInvocationResult.CreateRoot(invocationId.Value); if (result != null) { invocationResult.ResultValues = result; } return(invocationResult); } return(null); }
public async Task <GlowInvocationResult> Invoke(GlowInvocation invocation) { GlowValue[] arguments; int? invocationId; if (invocation == null) { if (Arguments != null) { throw new ArgumentException("Function with parameters called without arguments!"); } arguments = null; invocationId = null; } else { var argumentValues = invocation.ArgumentValues; arguments = argumentValues != null ? argumentValues.ToArray() : null; // ******* Tillagd kod **************************** // Funktioner utan parametrar kan definieras på två sätt. Antingen args=null eller args=tom lista. // Men EmberViewer v1.6.2 skickar args=null då funktionsparametrar saknas // och EmberViewer v2.4.0 skickar args=tom lista då funktionsparametrar saknas // För att båda modellerna ska fungera skapar vi tom lista om arg=null men förväntas vara tom lista // och argumenten sätts till null om argumenten förväntas vara null men är tom lista. if (Arguments != null && Arguments.Length == 0 && arguments == null) { arguments = new GlowValue[0]; } if (Arguments == null && arguments != null && arguments.Length == 0) { arguments = null; } // ************************************************ invocationId = invocation.InvocationId; AssertValueTypes(arguments, Arguments); } if (invocationId == null && HasResult) { throw new ArgumentException("Function with result called without invocation id!"); } Log.Debug($"Invoking function {IdentifierPath}"); var result = await _coreFunc(arguments); AssertValueTypes(result, Result); if (invocationId != null) { var invocationResult = GlowInvocationResult.CreateRoot(invocationId.Value); if (result != null) { invocationResult.ResultValues = result; } return(invocationResult); } return(null); }
/// <summary> /// Runs the application loop: read a line of input from stdin, /// and execute the command matching the input. /// </summary> void Run() { // write initial "dir" command to get the root's children. _endPoint.Write(_rootElement.GetDirectory()); WriteWaitingPrompt(); string input; while ((input = Console.ReadLine()) != "quit") { var match = null as Match; if (input == "dir") { _endPoint.Write(_cursor.GetDirectory()); WriteWaitingPrompt(); } else if (input == "cd..") { if (_cursor.Parent != null) { _cursor = _cursor.Parent; } WritePrompt(); } else if ((match = Regex.Match(input, @"^cd (.*)\r?$")).Success) { var elem = GetElement(match.Groups[1].Value); if (elem != null) { _cursor = elem; } WritePrompt(); } else if ((match = Regex.Match(input, @"^print (.*)\r?$")).Success) { var elem = GetElement(match.Groups[1].Value); if (elem != null) { Console.Write(elem.Xml); } WritePrompt(); } else if ((match = Regex.Match(input, @"^set (.*)\s?'(.*)'\r?$")).Success) { var elem = GetElement(match.Groups[1].Value.Trim()); var isOk = false; if (elem != null && elem.Type == ElementType.Parameter) { GlowValue value; if (GlowValue.TryParse( match.Groups[2].Value, elem.ParameterType, CultureInfo.CurrentCulture, out value)) { _endPoint.Write(elem.SetParameterValue(value)); WriteWaitingPrompt(); isOk = true; } } if (isOk == false) { WritePrompt(); } } else if (input == "?" || input == "help") { Console.WriteLine("cd <id> - change to node with identifier <id>"); Console.WriteLine("cd.. - change to current parent node"); Console.WriteLine("dir - list children of current node"); Console.WriteLine("print <id> - print node with identifier <id>"); Console.WriteLine("set <id> '<v>' - set value of parameter with identifier <id> to <v>"); Console.WriteLine("quit - exit program"); Console.WriteLine(); Console.WriteLine("for <id> you can also write '.' for current node"); WritePrompt(); } else if (String.IsNullOrEmpty(input) == false) { Console.WriteLine("Unknown command"); WritePrompt(); } else { WritePrompt(); } } }