/// <summary>Continues a browse operation with previously specified search criteria.</summary> /// <returns>The set of elements found.</returns> /// <requirements>OPC XML-DA Server or OPC Data Access Server V2.x / V3.x</requirements> /// <param name="position">An object containing the browse operation state information.</param> public TsCDaBrowseElement[] BrowseNext(ref Da.TsCDaBrowsePosition position) { if (_server == null) { throw new NotConnectedException(); } return(((ITsDaServer)_server).BrowseNext(ref position)); }
/// <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); } Da.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; } } }
/// <summary>Fetches the children of a branch that meet the filter criteria.</summary> /// <returns>The set of elements found.</returns> /// <requirements>OPC XML-DA Server or OPC Data Access Server V2.x / V3.x</requirements> /// <param name="itemID">The identifier of branch which is the target of the search.</param> /// <param name="filters">The filters to use to limit the set of child elements returned.</param> /// <param name="position">An object used to continue a browse that could not be completed.</param> public TsCDaBrowseElement[] Browse( OpcItem itemID, TsCDaBrowseFilters filters, out Da.TsCDaBrowsePosition position) { if (_server == null) { throw new NotConnectedException(); } return(((ITsDaServer)_server).Browse(itemID, filters, out position)); }
/// <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. OpcServer selection = FindServer(node); if (!typeof(TsCDaServer).IsInstanceOfType(selection)) { return; } TsCDaServer server = (TsCDaServer)selection; // 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. 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); } }
/// <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; } Da.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); } Da.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); } Da.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; } } }