public static void WriteValues(this IValueSymbol symbol, IObservable <object> valueObservable, Action <Exception> errorHandler, CancellationToken cancel) { Action <object> action = delegate(object o) { try { symbol.WriteValue(o); } catch (Exception exception) { errorHandler(exception); } }; Action <Exception> action2 = ex => errorHandler(ex); ObservableExtensions.Subscribe <object>(valueObservable, action, action2, delegate { }, cancel); }
public static IDisposable WriteValues(this IValueSymbol symbol, IObservable <object> valueObservable, Action <Exception> errorHandler) { Action <object> action = delegate(object o) { try { symbol.WriteValue(o); } catch (Exception exception) { errorHandler(exception); } }; Action <Exception> action2 = delegate(Exception ex) { errorHandler(ex); }; return(ObservableExtensions.Subscribe <object>(valueObservable, action, action2, delegate { })); }
/// <summary> /// Writes to ADS symbol from custom object type. /// </summary> /// <param name="Symbol">ADS Symbol to write</param> /// <param name="Data">User defined object (class or struct with matching properties) to write to Symbol</param> public static void SerializeObject(this ISymbol Symbol, object Object) { // cast to IValueSymbol type (for read/write) IValueSymbol symbol = (IValueSymbol)Symbol; // if primitive type if (symbol.IsPrimitiveType) { // write object to symbol value symbol.WriteValue(Object); } else { // symbol is array of non-primitive (struct array) if (symbol.Category == DataTypeCategory.Array) { // validate Object parameter is array if (Object.GetType().IsArray) { // cast Object to array var for indexing var arr = (Array)Object; // loop through elements (with upper bounds check) for (int i = 0; i < Math.Max(symbol.SubSymbols.Count, arr.Length); i++) { // get Object element, call recursively if (arr.GetValue(i) != null) { SerializeObject(symbol.SubSymbols[i], arr.GetValue(i)); } } } else { // array type mismatch exception throw new Exception("Type mismatch: Object parameter is not array type."); } } else { foreach (ISymbol symb in symbol.SubSymbols) { // get IValueSymbol IValueSymbol vSymb = (IValueSymbol)symb; // check if Object parameter contains matching property var property = Object.GetType().GetProperty(vSymb.InstanceName); if (property != null) { // if symbol is primitive if (vSymb.IsPrimitiveType) { // write object property value to symbol vSymb.WriteValue(property.GetValue(Object)); } else { // if sub symbol is array type if (vSymb.Category == DataTypeCategory.Array) { // cast property to array var for indexing var arr = (Array)property.GetValue(Object); // loop through elements (with upper bounds check) for (int i = 0; i < Math.Max(vSymb.SubSymbols.Count, arr.Length); i++) { // if property array index is initialized, call recursively if (arr.GetValue(i) != null) { SerializeObject(vSymb.SubSymbols[i], arr.GetValue(i)); } } } else { // call recursively on struct SerializeObject(vSymb, property.GetValue(Object)); } } } } } } }
/// <summary> /// Writes Value to primitive-type symbol /// </summary> /// <param name="SymbolPath">Symbol path</param> /// <param name="Value">Value to write</param> public void WritePrimitiveSymbol(string SymbolPath, object Value) { IValueSymbol symbol = (IValueSymbol)_symbolLoader.Symbols[SymbolPath]; symbol.WriteValue(Value); }