示例#1
0
        /// <summary>
        /// Adds the meta data of requested variables to the dictionary
        /// </summary>
        internal static bool AddPlcObjects(ITreeNode plcObj, IDictionary <string, Tuple <int, PlcObject> > plcObjects, IEnumerable <string> values, string prefix = "", int offset = 0)
        {
            var updated = false;

            foreach (var value in values.Where(x => !plcObjects.ContainsKey(x)))
            {
                var baseOffset = offset;
                var item       = value == "This" ? plcObj as PlcObject : plcObj.Get(new PlcMetaDataTreePath(value), ref baseOffset) as PlcObject;
                if (item == null)
                {
                    ExceptionThrowHelper.ThrowInvalidVariableException($"{plcObj.Name}.{value}");
                }

                var key = prefix + value;

                if (!plcObjects.ContainsKey(key))
                {
                    try
                    {
                        plcObjects.Add(key, new Tuple <int, PlcObject>(baseOffset, item));
                    }
                    catch (Exception)
                    {
                        // This could throw an exception if another thread has already added the object.
                        // but we could ignore this, becuase this would be the same object
                    }
                    updated = true;
                }

                if (item is PlcStruct plcStruct)
                {
                    updated = AddPlcObjects(plcStruct, plcObjects, plcStruct.Childs.Select(child => child.Name), key + ".", baseOffset) || updated;
                }
            }
            return(updated);
        }