/// <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; } } }
/// <summary> /// Continues a browse operation with previously specified search criteria. /// </summary> /// <param name="position">An object containing the browse operation state information.</param> /// <returns>The set of elements found.</returns> public virtual TsCDaBrowseElement[] BrowseNext(ref TsCDaBrowsePosition position) { lock (this) { if (server_ == null) { throw new NotConnectedException(); } string methodName = "IOPCBrowse.Browse"; // check for valid position object. if (position == null || position.GetType() != typeof(BrowsePosition)) { throw new BrowseCannotContinueException(); } BrowsePosition pos = (BrowsePosition)position; // check for valid continuation point. if (pos == null || pos.ContinuationPoint == null || pos.ContinuationPoint == "") { throw new BrowseCannotContinueException(); } // initialize arguments. int count = 0; int moreElements = 0; OpcItem itemID = ((BrowsePosition)position).ItemID; TsCDaBrowseFilters filters = ((BrowsePosition)position).Filters; IntPtr pContinuationPoint = Marshal.StringToCoTaskMemUni(pos.ContinuationPoint); IntPtr pElements = IntPtr.Zero; // invoke COM method. try { IOPCBrowse server = BeginComCall <IOPCBrowse>(methodName, true); server.Browse( (itemID != null && itemID.ItemName != null) ? itemID.ItemName : "", ref pContinuationPoint, filters.MaxElementsReturned, Interop.GetBrowseFilter(filters.BrowseFilter), (filters.ElementNameFilter != null) ? filters.ElementNameFilter : "", (filters.VendorFilter != null) ? filters.VendorFilter : "", (filters.ReturnAllProperties) ? 1 : 0, (filters.ReturnPropertyValues) ? 1 : 0, (filters.PropertyIDs != null) ? filters.PropertyIDs.Length : 0, Interop.GetPropertyIDs(filters.PropertyIDs), out moreElements, out count, out pElements); } catch (Exception e) { ComCallError(methodName, e); throw Technosoftware.DaAeHdaClient.Com.Interop.CreateException(methodName, e); } finally { EndComCall(methodName); } // unmarshal results. TsCDaBrowseElement[] elements = Interop.GetBrowseElements(ref pElements, count, true); pos.ContinuationPoint = Marshal.PtrToStringUni(pContinuationPoint); Marshal.FreeCoTaskMem(pContinuationPoint); // check if more no results exist. if (moreElements == 0 && (pos.ContinuationPoint == null || pos.ContinuationPoint == "")) { position = null; } // process results. ProcessResults(elements, filters.PropertyIDs); return(elements); } }
/// <summary> /// Fetches the children of a branch that meet the filter criteria. /// </summary> /// <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> /// <returns>The set of elements found.</returns> public virtual TsCDaBrowseElement[] Browse( OpcItem itemId, TsCDaBrowseFilters filters, out TsCDaBrowsePosition position) { if (filters == null) { throw new ArgumentNullException(nameof(filters)); } lock (this) { if (server_ == null) { throw new NotConnectedException(); } string methodName = "IOPCBrowse.Browse"; position = null; // initialize arguments. int count = 0; int moreElements = 0; IntPtr pContinuationPoint = IntPtr.Zero; IntPtr pElements = IntPtr.Zero; // invoke COM method. try { IOPCBrowse server = BeginComCall <IOPCBrowse>(methodName, true); server.Browse( (itemId != null && itemId.ItemName != null) ? itemId.ItemName : "", ref pContinuationPoint, filters.MaxElementsReturned, Interop.GetBrowseFilter(filters.BrowseFilter), (filters.ElementNameFilter != null) ? filters.ElementNameFilter : "", (filters.VendorFilter != null) ? filters.VendorFilter : "", (filters.ReturnAllProperties) ? 1 : 0, (filters.ReturnPropertyValues) ? 1 : 0, (filters.PropertyIDs != null) ? filters.PropertyIDs.Length : 0, Interop.GetPropertyIDs(filters.PropertyIDs), out moreElements, out count, out pElements); } catch (Exception e) { ComCallError(methodName, e); throw Technosoftware.DaAeHdaClient.Com.Interop.CreateException(methodName, e); } finally { EndComCall(methodName); } // unmarshal results. TsCDaBrowseElement[] elements = Interop.GetBrowseElements(ref pElements, count, true); string continuationPoint = Marshal.PtrToStringUni(pContinuationPoint); Marshal.FreeCoTaskMem(pContinuationPoint); // check if more results exist. if (moreElements != 0 || (continuationPoint != null && continuationPoint != "")) { // allocate new browse position object. position = new BrowsePosition(itemId, filters, continuationPoint); } // process results. ProcessResults(elements, filters.PropertyIDs); return(elements); } }
/// <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; } } }
/// <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. 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); } }