Ejemplo n.º 1
0
        /// <summary>
        /// Handles a method call.
        /// </summary>
        private ServiceResult DoGenerateRandomValues(
            ISystemContext context,
            MethodState method,
            IList <object> inputArguments,
            IList <object> outputArguments)
        {
            ComDaClientManager system = (ComDaClientManager)this.SystemContext.SystemHandle;
            ComDaClient        client = system.SelectClient((ServerSystemContext)context, false);

            Opc.Ua.Test.DataGenerator generator = new Opc.Ua.Test.DataGenerator(null);

            IDaElementBrowser browser = client.CreateBrowser((string)method.Handle);

            // create write requests.
            WriteRequestCollection requests = new WriteRequestCollection();

            try
            {
                for (DaElement element = browser.Next(); element != null; element = browser.Next())
                {
                    if (element.ElementType == DaElementType.Branch)
                    {
                        continue;
                    }

                    // generate a random value of the correct data tyoe.
                    bool   isArray    = false;
                    NodeId dataTypeId = ComUtils.GetDataTypeId(element.DataType, out isArray);

                    object value = generator.GetRandom(
                        dataTypeId,
                        (isArray)?ValueRanks.OneDimension:ValueRanks.Scalar,
                        null,
                        context.TypeTable);

                    // create a request.
                    requests.Add(new Opc.Ua.Com.Client.WriteRequest(element.ItemId, new DataValue(new Variant(value)), 0));
                }
            }
            finally
            {
                browser.Dispose();
            }

            // write values.
            client.Write(requests);

            return(ServiceResult.Good);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Finds the item id for the parent of the element.
        /// </summary>
        /// <param name="itemId">The item id.</param>
        /// <returns>The item id for the parent of the element.</returns>
        public string FindElementParentId(string itemId)
        {
            if (String.IsNullOrEmpty(itemId))
            {
                return(null);
            }

            // check in cache.
            DaElement element = null;

            lock (m_cache)
            {
                if (m_cache.TryGetValue(itemId, out element))
                {
                    if (element.ParentId != null)
                    {
                        return(element.ParentId);
                    }
                }
            }

            // try extracting the name by parsing the item id.
            string name     = null;
            string parentId = null;

            if (ParseItemId(itemId, out name, out parentId))
            {
                element = CreateElement(itemId, name, parentId);
            }

            // need to do it the hard way by searching the address space.
            else
            {
                IDaElementBrowser browser = CreateBrowser(itemId);
                element = browser.Find(itemId, false);
                browser.Dispose();
            }

            // save element in the cache.
            if (element != null)
            {
                SaveElementInCache(element);
            }

            return(element.ParentId);
        }