/// <summary>
        /// Reads ADS Array Symbol value(s) into instance of type T[].
        /// </summary>
        /// <typeparam name="T">Type (class or struct Array definition)</typeparam>
        /// <param name="Symbol">ADS array symbol to read</param>
        /// <returns>Object array of type T with values read from Symbol</returns>
        public static T[] DeserializeArrayObject <T>(this ISymbol Symbol) where T : new()
        {
            // cast to IValueSymbol type
            IValueSymbol symbol = (IValueSymbol)Symbol;

            // instantiate new object array of symbol count size
            var obj = new T[symbol.SubSymbols.Count];

            // if primitive / non-struct type
            if (symbol.IsPrimitiveType)
            {
                // cast to appropriate type and return
                return((T[])symbol.ReadValue());
            }
            else
            {
                // iterate through each symbol array element
                for (int i = 0; i < symbol.SubSymbols.Count; i++)
                {
                    // call DeserializeObject on each element
                    obj[i] = DeserializeObject <T>(symbol.SubSymbols[i]);
                }
            }

            // return generated array of type T
            return(obj);
        }
        public static ExpandoObject GetStruct(this ISymbol Symbol)
        {
            // cast to IValueSymbol
            IValueSymbol symbol = (IValueSymbol)Symbol;

            // create dynamic expandable object to hold data
            dynamic myobj = new ExpandoObject();

            // primitive types
            if (symbol.IsPrimitiveType)
            {
                ((IDictionary <string, object>)myobj).Add(symbol.InstanceName, symbol.ReadValue());
                //return myobj;
            }
            else
            {
                // array of structs
                //***********NOT WORKING************
                if (symbol.Category == DataTypeCategory.Array)
                {
                    // declare sub element array for parallelization
                    // calling dict.Add() in parallel loop will result in
                    // out-of-order elements
                    var sub = new KeyValuePair <string, object> [symbol.SubSymbols.Count];
                    // loop through struct array and call recursively
                    for (int i = 0; i < symbol.SubSymbols.Count; i++)
                    {
                        sub[i] = new KeyValuePair <string, object>(symbol.SubSymbols[i].InstanceName, symbol.SubSymbols[i].GetStruct());
                        //((IDictionary<string, object>)myobj).Add(symbol.InstanceName, symbol.SubSymbols[i].GetStruct());
                    }
                    ((IDictionary <string, object>)myobj).Add(symbol.InstanceName, sub);
                    //return myobj;
                    //// add loaded KVPs into dictionary
                    //foreach (KeyValuePair<string, object> pair in sub)
                    //{
                    //    dict.Add(pair.Key, pair.Value);
                    //}
                }
                // struct
                else if (symbol.Category == DataTypeCategory.Struct)
                {
                    int i = 0;
                    foreach (IValueSymbol symb in symbol.SubSymbols)
                    {
                        if (symb.IsPrimitiveType)
                        {
                            ((IDictionary <string, object>)myobj).Add(symb.InstanceName, symb.ReadValue());
                        }
                        else
                        {
                            ((IDictionary <string, object>)myobj).Add(symb.InstanceName, symb.GetStruct());
                        }
                        i++;
                    }
                }
            }
            return(myobj);
        }
        /// <summary>
        /// Reads ADS Symbol into Dictionary of key/value pairs.
        /// Experimental. Very slow.
        /// </summary>
        /// <param name="Symbol">ADS symbol to read</param>
        /// <returns>Dictionary of key/value pairs</returns>
        public static Dictionary <string, object> ToDictionary(this ISymbol Symbol)
        {
            // cast to IValueSymbol
            IValueSymbol symbol = (IValueSymbol)Symbol;

            // create new dictionary
            Dictionary <string, object> dict = new Dictionary <string, object>();

            // primitive types
            if (symbol.IsPrimitiveType)
            {
                dict.Add(symbol.InstanceName, symbol.ReadValue());
            }
            else
            {
                // array of structs
                if (symbol.Category == DataTypeCategory.Array)
                {
                    // declare sub element array for parallelization
                    // calling dict.Add() in parallel loop will result in
                    // out-of-order elements
                    var sub = new KeyValuePair <string, object> [symbol.SubSymbols.Count];

                    // loop through struct array and call recursively
                    for (int i = 0; i < symbol.SubSymbols.Count; i++)
                    {
                        sub[i] = new KeyValuePair <string, object>(symbol.SubSymbols[i].InstanceName, symbol.SubSymbols[i].ToDictionary());
                    }

                    // add loaded KVPs into dictionary
                    foreach (KeyValuePair <string, object> pair in sub)
                    {
                        dict.Add(pair.Key, pair.Value);
                    }
                }
                // struct
                else if (symbol.Category == DataTypeCategory.Struct)
                {
                    foreach (IValueSymbol symb in symbol.SubSymbols)
                    {
                        if (symb.IsPrimitiveType)
                        {
                            dict.Add(symb.InstanceName, symb.ReadValue());
                        }
                        else
                        {
                            dict.Add(symb.InstanceName, symb.ToDictionary());
                        }
                    }
                }
            }


            return(dict);
        }
Exemple #4
0
        private int readArraySize()
        {
            int          num             = 0;
            IValueSymbol arraySizeSymbol = (IValueSymbol)this.ArraySizeSymbol;

            if (arraySizeSymbol != null)
            {
                try
                {
                    num = PrimitiveTypeConverter.Convert <int>(arraySizeSymbol.ReadValue());
                }
                catch (Exception)
                {
                }
            }
            return(num);
        }
Exemple #5
0
        public static IObservable <object> PollValues(this IValueSymbol symbol, IObservable <Unit> trigger)
        {
            Func <Unit, object> func = o => symbol.ReadValue();

            return(Observable.Select <Unit, object>(trigger, func));
        }
        /// <summary>
        /// Reads ADS Symbol value(s) into instance of type T.
        /// </summary>
        /// <typeparam name="T">Type (class or structure definition)</typeparam>
        /// <param name="Symbol">ADS structure Symbol to read</param>
        /// <returns>Object of type T with values read from Symbol</returns>
        public static T DeserializeObject <T>(this ISymbol Symbol) where T : new()
        {
            // cast to IValueSymbol type
            IValueSymbol symbol = (IValueSymbol)Symbol;

            // instantiate new object
            var obj = new T();

            // if primitive / non-struct type
            if (symbol.IsPrimitiveType)
            {
                // cast to appropriate type and return
                return((T)symbol.ReadValue());
            }
            else
            {
                // loop through subsymbols in struct
                foreach (IValueSymbol vSymb in symbol.SubSymbols)
                {
                    // check if T object has matching property
                    var property = typeof(T).GetProperty(vSymb.InstanceName);
                    if (property != null)
                    {
                        // if symbol is primitive
                        if (vSymb.IsPrimitiveType)
                        {
                            // set object property value from sub symbol
                            property.SetValue(obj, vSymb.ReadValue());
                        }
                        else
                        {
                            // if sub symbol is array type
                            if (vSymb.Category == DataTypeCategory.Array)
                            {
                                // get object array property type
                                Type propertyType = property.PropertyType;
                                // get array element type
                                Type elementType = property.PropertyType.GetElementType();

                                // instantiate new array of property type
                                var propVal = Activator.CreateInstance(propertyType, new object[] { vSymb.SubSymbols.Count });

                                // initialize generic method for recursive call (reflection)
                                MethodInfo method = typeof(SymbolExtensions).GetMethod("DeserializeArrayObject",
                                                                                       BindingFlags.Public | BindingFlags.Static);
                                MethodInfo generic = method.MakeGenericMethod(elementType);

                                // call array method recursively
                                propVal = generic.Invoke(null, new object[] { vSymb });

                                // set property value
                                property.SetValue(obj, propVal);
                            }
                            else
                            {
                                // get object property type
                                Type propertyType = property.PropertyType;

                                // create instance of property
                                var propVal = Activator.CreateInstance(propertyType);

                                // initialize generic method for recursive call (reflection)
                                MethodInfo method = typeof(SymbolExtensions).GetMethod("DeserializeObject",
                                                                                       BindingFlags.Public | BindingFlags.Static);
                                MethodInfo generic = method.MakeGenericMethod(propertyType);

                                // call method recursively
                                propVal = generic.Invoke(null, new object[] { vSymb });

                                // set property value
                                property.SetValue(obj, propVal);
                            }
                        }
                    }
                }
            }

            // return generated instance of type T
            return(obj);
        }
        /// <summary>
        /// Reads value of primitive-type symbol
        /// </summary>
        /// <param name="SymbolPath">Symbol path</param>
        /// <returns>Value of symbol</returns>
        public object ReadPrimitiveSymbol(string SymbolPath)
        {
            IValueSymbol symbol = (IValueSymbol)_symbolLoader.Symbols[SymbolPath];

            return(symbol.ReadValue());
        }
        /// <summary>
        /// Reads value of primitive-type symbol
        /// </summary>
        /// <typeparam name="T">Symbol primitive type</typeparam>
        /// <param name="SymbolPath">Symbol path</param>
        /// <returns>Value of symbol</returns>
        public T ReadPrimitiveSymbol <T>(string SymbolPath)
        {
            IValueSymbol symbol = (IValueSymbol)_symbolLoader.Symbols[SymbolPath];

            return((T)symbol.ReadValue());
        }