Exemplo n.º 1
0
        /// <summary>
        /// Populates the tree with the event attributes supported by the category.
        /// </summary>
        private void ShowEventAttributes(TreeNodeCollection nodes, Technosoftware.DaAeHdaClient.Ae.TsCAeCategory category)
        {
            Technosoftware.DaAeHdaClient.Ae.TsCAeAttribute[] attributes = null;

            // fetch attributes.
            try
            {
                attributes = mServer_.QueryEventAttributes(category.ID);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return;
            }

            // add attributes to tree.
            foreach (Technosoftware.DaAeHdaClient.Ae.TsCAeAttribute attribute in attributes)
            {
                // create node.
                TreeNode node = new TreeNode(attribute.Name)
                {
                    ImageIndex         = Resources.IMAGE_EXPLODING_BOX,
                    SelectedImageIndex = Resources.IMAGE_EXPLODING_BOX,
                    Tag = attribute
                };

                // add to tree.
                nodes.Add(node);
            }
        }
        /// <summary>
        /// Populates the tree with the event attributes supported by the category.
        /// </summary>
        private void ShowEventAttributes(TreeNodeCollection nodes, OpcClientSdk.Ae.TsCAeCategory category)
        {
            OpcClientSdk.Ae.TsCAeAttribute[] attributes = null;

            // fetch attributes.
            try
            {
                attributes = m_server.QueryEventAttributes(category.ID);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return;
            }

            // add attributes to tree.
            foreach (OpcClientSdk.Ae.TsCAeAttribute attribute in attributes)
            {
                // create node.
                TreeNode node = new TreeNode(attribute.Name);

                node.ImageIndex         = Resources.IMAGE_EXPLODING_BOX;
                node.SelectedImageIndex = Resources.IMAGE_EXPLODING_BOX;
                node.Tag = attribute;

                // add to tree.
                nodes.Add(node);
            }
        }
        /// <summary>
        /// Populates the tree view with the attributes.
        /// </summary>
        private void FetchAttributes(TreeNode parent, TsCAeServer server, int categoryID)
        {
            OpcClientSdk.Ae.TsCAeAttribute[] attributes = server.QueryEventAttributes(categoryID);

            foreach (OpcClientSdk.Ae.TsCAeAttribute attribute in attributes)
            {
                string label = String.Format(
                    "[{0}] {1} ({2})",
                    attribute.ID,
                    attribute.Name,
                    OpcClientSdk.OpcConvert.ToString(attribute.DataType));

                TreeNode node = new TreeNode(label);

                node.ImageIndex         = Resources.IMAGE_EXPLODING_BOX;
                node.SelectedImageIndex = Resources.IMAGE_EXPLODING_BOX;
                node.Tag = attribute;

                parent.Nodes.Add(node);
            }
        }
        /// <summary>
        /// Populates the tree view with the attributes.
        /// </summary>
        private void FetchAttributes(TreeNode parent, TsCAeServer server, int categoryId)
        {
            Technosoftware.DaAeHdaClient.Ae.TsCAeAttribute[] attributes = server.QueryEventAttributes(categoryId);

            foreach (Technosoftware.DaAeHdaClient.Ae.TsCAeAttribute attribute in attributes)
            {
                string label = String.Format(
                    "[{0}] {1} ({2})",
                    attribute.ID,
                    attribute.Name,
                    Technosoftware.DaAeHdaClient.OpcConvert.ToString(attribute.DataType));

                TreeNode node = new TreeNode(label)
                {
                    ImageIndex         = Resources.IMAGE_EXPLODING_BOX,
                    SelectedImageIndex = Resources.IMAGE_EXPLODING_BOX,
                    Tag = attribute
                };

                parent.Nodes.Add(node);
            }
        }
        /// <summary>
        /// Find attributes for condition by searching all categories.
        /// </summary>
        private void FindAttributes()
        {
            try
            {
                Technosoftware.DaAeHdaClient.Ae.TsCAeCategory[] categories = mServer_.QueryEventCategories((int)TsCAeEventType.Condition);

                for (int ii = 0; ii < categories.Length; ii++)
                {
                    // fetch conditions for category.
                    string[] conditions = mServer_.QueryConditionNames(categories[ii].ID);

                    // check if this is the category containing the current condition.
                    bool found = false;

                    for (int jj = 0; jj < conditions.Length; jj++)
                    {
                        if (conditions[jj] == mCondition_)
                        {
                            mCategoryId_ = categories[ii].ID;
                            found        = true;
                            break;
                        }
                    }

                    // fetch the attributes when found.
                    if (found)
                    {
                        mAttributes_ = mServer_.QueryEventAttributes(categories[ii].ID);
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return;
            }
        }
        public void Run()
        {
            try
            {
                const string serverUrl = "opcae://localhost/Technosoftware.AeSample";

                Console.WriteLine();
                Console.WriteLine("Simple OPC AE Client based on the OPC AE Client SDK .NET Standard");
                Console.WriteLine("-----------------------------------------------------------------");
                Console.Write("   Press <Enter> to connect to "); Console.WriteLine(serverUrl);
                Console.ReadLine();
                Console.WriteLine("   Please wait...");

                TsCAeServer myAeServer = new TsCAeServer();

                // Connect to the server
                myAeServer.Connect(serverUrl);

                Console.WriteLine("   Connected, press <Enter> to create an active subscription and press <Enter>");
                Console.WriteLine("   again to deactivate the subscription. This stops the reception of new events.");
                Console.ReadLine();

                TsCAeSubscriptionState state = new TsCAeSubscriptionState {
                    Active = true, BufferTime = 0, MaxSize = 0, ClientHandle = 100, Name = "Simple Event Subscription"
                };

                TsCAeCategory[]  categories = myAeServer.QueryEventCategories((int)TsCAeEventType.Condition);
                TsCAeAttribute[] attributes = null;
                attributes = myAeServer.QueryEventAttributes(categories[0].ID);

                int[] attributeIDs = new int[2];

                attributeIDs[0] = attributes[0].ID;
                attributeIDs[1] = attributes[1].ID;

                TsCAeSubscription subscription;
                subscription = (TsCAeSubscription)myAeServer.CreateSubscription(state);
                subscription.DataChangedEvent += OnDataChangedEvent;
                subscription.SelectReturnedAttributes(categories[0].ID, attributeIDs);
                Console.ReadLine();

                subscription.DataChangedEvent -= OnDataChangedEvent;
                Console.WriteLine("   Subscription deactivated, press <Enter> to remove the subscription and disconnect from the server.");

                subscription.Dispose();                                                                 // Remove subscription
                myAeServer.Disconnect();                                                                // Disconnect from server
                myAeServer.Dispose();                                                                   // Dispose server object

                Console.ReadLine();
                Console.WriteLine("   Disconnected from the server.");
                Console.WriteLine();
            }
            catch (OpcResultException e)
            {
                Console.WriteLine(String.Format("   {0}", e.Message));
                Console.ReadLine();
                return;
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("   {0}", e.Message));
                Console.ReadLine();
                return;
            }
        }