Esempio n. 1
0
		TsCDaBrowseElement[] Browse(TsCDaServer server)
		{
			TsCDaBrowseFilters filters;
			List<TsCDaBrowseElement> elementList;
			TsCDaBrowseElement[] elements;
			TsCDaBrowsePosition position;
			OpcItem path = new OpcItem();

			filters = new TsCDaBrowseFilters();
			filters.BrowseFilter = TsCDaBrowseFilter.All;
			filters.ReturnAllProperties = true;
			filters.ReturnPropertyValues = true;

			elementList = new List<TsCDaBrowseElement>();

			elements = server.Browse(path, filters, out position);

			foreach (var item in elements)
			{
				item.ItemPath = OpcDaTag.ROOT + OpcDaTag.SPLITTER + item.ItemName;
				elementList.Add(item);

				if (!item.IsItem)
				{
					path = new OpcItem(item.ItemPath, item.Name);
					BrowseChildren(path, filters, elementList, server);
				}

			}
			return elementList.ToArray();
		}
        /// <summary>
        /// Fetches the set of data filters from the server.
        /// </summary>
        /// <param name="server">The server object</param>
        public TsCCpxComplexItem[] GetDataFilters(TsCDaServer server)
        {
            // not a valid operation for data filter items.
            if (_unfilteredItemID != null)
            {
                return(null);
            }

            // data filters not supported by the item.
            if (_filterItem == null)
            {
                return(null);
            }

            TsCDaBrowsePosition position = null;

            try
            {
                // browse any existing filter instances.
                TsCDaBrowseFilters filters = new TsCDaBrowseFilters {
                    ElementNameFilter = null, BrowseFilter = TsCDaBrowseFilter.Item, ReturnAllProperties = false, PropertyIDs = CPX_PROPERTIES, ReturnPropertyValues = true
                };

                TsCDaBrowseElement[] elements = server.Browse(_filterItem, filters, out position);

                // nothing found.
                if (elements == null || elements.Length == 0)
                {
                    return(new TsCCpxComplexItem[0]);
                }

                // contruct an array of complex data items for each available data filter.
                ArrayList dataFilters = new ArrayList(elements.Length);

                Array.ForEach(elements, element =>
                {
                    TsCCpxComplexItem item = new TsCCpxComplexItem();
                    if (item.Init(element))
                    {
                        dataFilters.Add(item);
                    }
                });

                // return the set of available data filters.
                return((TsCCpxComplexItem[])dataFilters.ToArray(typeof(TsCCpxComplexItem)));
            }
            finally
            {
                if (position != null)
                {
                    position.Dispose();
                    position = null;
                }
            }
        }
Esempio n. 3
0
        //----------------------------------------------------------------------------------------------------------------------
        // btnConnect_Click
        //-----------------------
        // This method tries to connect to the specified OPC Server.
        // If connected successfully, this method also registers the name of the client with the server and
        // adds an OPC Group.
        //----------------------------------------------------------------------------------------------------------------------
        private void btnConnect_Click(object sender, EventArgs e)
        {
            TsCDaBrowseElement[] BrowserElement;
            TsCDaBrowsePosition  BrowserPos;
            OpcItem Path = new OpcItem("");

            _filter.BrowseFilter         = TsCDaBrowseFilter.All;
            _filter.ReturnAllProperties  = true;
            _filter.ReturnPropertyValues = true;

            try
            {
                Cursor = Cursors.WaitCursor;
                if (!_myServer.IsConnected)
                {
                    // No connection to server, connect to it
                    _textBoxServerUrl.Enabled = false;

                    _myServer.Connect(_textBoxServerUrl.Text);          // Connect now with Server
                    BrowserElement = _myServer.Browse(Path, _filter, out BrowserPos);

                    /// All succeeded, update buttons and text fields
                    _textBoxServerUrl.Enabled = false;
                    _btnConnect.Text          = "Disconnect";
                    BrowseCTRL.ShowSingleServer(_myServer, _filter);
                    PropertiesCTRL.Initialize(null);
                }
                else
                {
                    // connection to server exists, disconnect to it
                    _textBoxServerUrl.Enabled = true;

                    BrowseCTRL.Clear();
                    PropertiesCTRL.Initialize(null);
                    _myServer.Disconnect();
                    _btnConnect.Text = "Connect";
                }
                Cursor = Cursors.Default;                                                       // Set default cursor
            }
            catch (OpcResultException exe)
            {
                Cursor = Cursors.Default;                                                       /// Set default cursor
                MessageBox.Show(exe.Message, exe.Source, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                _textBoxServerUrl.Enabled = true;
                _btnConnect.Text          = "Connect";
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;                                                       /// Set default cursor
                MessageBox.Show(ex.Message, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                _textBoxServerUrl.Enabled = true;
                _btnConnect.Text          = "Connect";
            }
        }
        /// <summary>
        /// Browses for children of the element at the current node.
        /// </summary>
        private void Browse(TreeNode node)
        {
            try
            {
                // get the server for the current node.
                TsCDaServer server = FindServer(node);

                // get the current element to use for a browse.
                TsCDaBrowseElement parent = null;
                OpcItem            itemID = null;

                if (node.Tag != null && node.Tag.GetType() == typeof(TsCDaBrowseElement))
                {
                    parent = (TsCDaBrowseElement)node.Tag;
                    itemID = new OpcItem(parent.ItemPath, parent.ItemName);
                }

                // clear the node children.
                node.Nodes.Clear();

                // add properties
                if (parent != null && parent.Properties != null)
                {
                    foreach (TsCDaItemProperty property in parent.Properties)
                    {
                        AddItemProperty(node, property);
                    }
                }

                // begin a browse.
                OpcClientSdk.Da.TsCDaBrowsePosition position = null;
                TsCDaBrowseElement[] elements = server.Browse(itemID, m_filters, out position);

                // add children.
                if (elements != null)
                {
                    foreach (TsCDaBrowseElement element in elements)
                    {
                        AddBrowseElement(node, element);
                    }

                    node.Expand();
                }

                // loop until all elements have been fetched.
                while (position != null)
                {
                    DialogResult result = MessageBox.Show(
                        "More items meeting search criteria exist. Continue browse?",
                        "Browse Items",
                        MessageBoxButtons.YesNo);

                    if (result == DialogResult.No)
                    {
                        break;
                    }

                    // fetch next batch of elements,.
                    elements = server.BrowseNext(ref position);

                    // add children.
                    if (elements != null)
                    {
                        foreach (TsCDaBrowseElement element in elements)
                        {
                            AddBrowseElement(node, element);
                        }

                        node.Expand();
                    }
                }

                // send notification that property list changed.
                if (ElementSelected != null)
                {
                    if (node.Tag.GetType() == typeof(TsCDaBrowseElement))
                    {
                        ElementSelected((TsCDaBrowseElement)node.Tag);
                    }
                    else
                    {
                        ElementSelected(null);
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
Esempio n. 5
0
		void BrowseChildren(OpcItem opcItem, TsCDaBrowseFilters filters,
			IList<TsCDaBrowseElement> elementList, TsCDaServer server)
		{
			TsCDaBrowsePosition position;
			OpcItem path;

			var elements = server.Browse(opcItem, filters, out position);

			if (elements != null)
			{
				foreach (var item in elements)
				{
					item.ItemPath = opcItem.ItemPath + OpcDaTag.SPLITTER + item.ItemName;
					elementList.Add(item);

					if (!item.IsItem)
					{
						path = new OpcItem(item.ItemPath, item.ItemName);
						BrowseChildren(path, filters, elementList, server);
					}
				}
			}
		}
        /// <summary>
        /// Fetches the item id for the data filters items and stores it in the internal cache.
        /// </summary>
        /// <param name="server">The server object</param>
        public void GetDataFilterItem(TsCDaServer server)
        {
            _filterItem = null;

            // not a valid operation for data filter items.
            if (_unfilteredItemID != null)
            {
                return;
            }

            TsCDaBrowsePosition position = null;

            try
            {
                OpcItem itemID = new OpcItem(this);

                // browse any existing filter instances.
                TsCDaBrowseFilters filters = new TsCDaBrowseFilters {
                    ElementNameFilter = CPX_DATA_FILTERS, BrowseFilter = TsCDaBrowseFilter.All, ReturnAllProperties = false, PropertyIDs = null, ReturnPropertyValues = false
                };

                TsCDaBrowseElement[] elements = null;

                // browse for the 'CPX' branch first.
                if (_unconvertedItemID == null)
                {
                    filters.ElementNameFilter = CPX_BRANCH;

                    elements = server.Browse(itemID, filters, out position);

                    // nothing found.
                    if (elements == null || elements.Length == 0)
                    {
                        return;
                    }

                    // release the position object.
                    if (position != null)
                    {
                        position.Dispose();
                        position = null;
                    }

                    // update the item for the next browse operation.
                    itemID = new OpcItem(elements[0].ItemPath, elements[0].ItemName);

                    filters.ElementNameFilter = CPX_DATA_FILTERS;
                }

                // browse for the 'DataFilters' branch.
                elements = server.Browse(itemID, filters, out position);

                // nothing found.
                if (elements == null || elements.Length == 0)
                {
                    return;
                }

                _filterItem = new OpcItem(elements[0].ItemPath, elements[0].ItemName);
            }
            finally
            {
                if (position != null)
                {
                    position.Dispose();
                    position = null;
                }
            }
        }
        /// <summary>
        /// Creates a new data filter.
        /// </summary>
        /// <param name="server">The server object</param>
        /// <param name="filterName">The name of the filter</param>
        /// <param name="filterValue">The value of the filter</param>
        public TsCCpxComplexItem CreateDataFilter(TsCDaServer server, string filterName, string filterValue)
        {
            // not a valid operation for data filter items.
            if (_unfilteredItemID != null)
            {
                return(null);
            }

            // data filters not supported by the item.
            if (_filterItem == null)
            {
                return(null);
            }

            TsCDaBrowsePosition position = null;

            try
            {
                // write the desired filter to the server.
                TsCDaItemValue item = new TsCDaItemValue(_filterItem);

                // create the filter parameters document.
                using (StringWriter ostrm = new StringWriter())
                {
                    using (XmlTextWriter writer = new XmlTextWriter(ostrm))
                    {
                        writer.WriteStartElement("DataFilters");
                        writer.WriteAttributeString("Name", filterName);
                        writer.WriteString(filterValue);
                        writer.WriteEndElement();
                        writer.Close();
                    }
                    // create the value to write.
                    item.Value = ostrm.ToString();
                }
                item.Quality            = TsCDaQuality.Bad;
                item.QualitySpecified   = false;
                item.Timestamp          = DateTime.MinValue;
                item.TimestampSpecified = false;

                // write the value.
                OpcItemResult[] result = server.Write(new TsCDaItemValue[] { item });

                if (result == null || result.Length == 0)
                {
                    throw new OpcResultException(new OpcResult((int)OpcResult.E_FAIL.Code, OpcResult.FuncCallType.SysFuncCall, null), "Unexpected results returned from server.");
                }

                if (result[0].Result.Failed())
                {
                    throw new OpcResultException(new OpcResult((int)OpcResult.Cpx.E_FILTER_ERROR.Code, OpcResult.FuncCallType.SysFuncCall, null), "Could not create new data filter.");
                }

                // browse for new data filter item.
                TsCDaBrowseFilters filters = new TsCDaBrowseFilters {
                    ElementNameFilter = filterName, BrowseFilter = TsCDaBrowseFilter.Item, ReturnAllProperties = false, PropertyIDs = CPX_PROPERTIES, ReturnPropertyValues = true
                };

                TsCDaBrowseElement[] elements = server.Browse(_filterItem, filters, out position);

                // nothing found.
                if (elements == null || elements.Length == 0)
                {
                    throw new OpcResultException(new OpcResult((int)OpcResult.Cpx.E_FILTER_ERROR.Code, OpcResult.FuncCallType.SysFuncCall, null), "Could not browse to new data filter.");
                }

                TsCCpxComplexItem filterItem = new TsCCpxComplexItem();

                if (!filterItem.Init(elements[0]))
                {
                    throw new OpcResultException(new OpcResult((int)OpcResult.Cpx.E_FILTER_ERROR.Code, OpcResult.FuncCallType.SysFuncCall, null), "Could not initialize to new data filter.");
                }

                // return the new data filter.
                return(filterItem);
            }
            finally
            {
                if (position != null)
                {
                    position.Dispose();
                    position = null;
                }
            }
        }
        /// <summary>
        /// Fetches the set of type conversions from the server.
        /// </summary>
        /// <param name="server">The server object</param>
        public TsCCpxComplexItem[] GetTypeConversions(TsCDaServer server)
        {
            // only the root item can have type conversions.
            if (_unconvertedItemID != null || _unfilteredItemID != null)
            {
                return(null);
            }

            TsCDaBrowsePosition position = null;

            try
            {
                // look for the 'CPX' branch.
                TsCDaBrowseFilters filters = new TsCDaBrowseFilters {
                    ElementNameFilter = CPX_BRANCH, BrowseFilter = TsCDaBrowseFilter.Branch, ReturnAllProperties = false, PropertyIDs = null, ReturnPropertyValues = false
                };

                TsCDaBrowseElement[] elements = server.Browse(this, filters, out position);

                // nothing found.
                if (elements == null || elements.Length == 0)
                {
                    return(null);
                }

                // release the browse position object.
                if (position != null)
                {
                    position.Dispose();
                    position = null;
                }

                // browse for type conversions.
                OpcItem itemID = new OpcItem(elements[0].ItemPath, elements[0].ItemName);

                filters.ElementNameFilter    = null;
                filters.BrowseFilter         = TsCDaBrowseFilter.Item;
                filters.ReturnAllProperties  = false;
                filters.PropertyIDs          = CPX_PROPERTIES;
                filters.ReturnPropertyValues = true;

                elements = server.Browse(itemID, filters, out position);

                // nothing found.
                if (elements == null || elements.Length == 0)
                {
                    return(new TsCCpxComplexItem[0]);
                }

                // contruct an array of complex data items for each available conversion.
                ArrayList conversions = new ArrayList(elements.Length);

                Array.ForEach(elements, element =>
                {
                    if (element.Name != CPX_DATA_FILTERS)
                    {
                        TsCCpxComplexItem item = new TsCCpxComplexItem();
                        if (item.Init(element))
                        {
                            // check if data filters supported for type conversion.
                            item.GetDataFilterItem(server);
                            conversions.Add(item);
                        }
                    }
                });

                // return the set of available conversions.
                return((TsCCpxComplexItem[])conversions.ToArray(typeof(TsCCpxComplexItem)));
            }
            finally
            {
                if (position != null)
                {
                    position.Dispose();
                    position = null;
                }
            }
        }