/// <summary>
        /// Updates a data filter.
        /// </summary>
        /// <param name="server">The server object</param>
        /// <param name="filterValue">The value of the filter</param>
        public void UpdateDataFilter(TsCDaServer server, string filterValue)
        {
            // not a valid operation for non data filter items.
            if (_unfilteredItemID == null)
            {
                throw new OpcResultException(new OpcResult((int)OpcResult.Cpx.E_FILTER_ERROR.Code, OpcResult.FuncCallType.SysFuncCall, null), "Cannot update the data filter for this item.");
            }

            // create the value to write.
            TsCDaItemValue item = new TsCDaItemValue(this)
            {
                Value = filterValue, Quality = TsCDaQuality.Bad, QualitySpecified = false, Timestamp = DateTime.MinValue, 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 update data filter.");
            }

            // update locale copy of the filter value.
            DataFilterValue = filterValue;
        }
        /// <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;
                }
            }
        }
Example #3
0
        /// <summary>
        /// Executes a write request for the current set if items.
        /// </summary>
        public void DoWrite()
        {
            try
            {
                // get the selected items
                TsCDaItemValue[] items = ItemsCTRL.GetItems();

                // write to server.
                OpcItemResult[] results = null;

                if (m_subscription != null)
                {
                    if (m_synchronous)
                    {
                        results = m_subscription.Write(items);
                    }
                    else
                    {
                        results = new AsyncRequestDlg().ShowDialog(m_subscription, items);

                        if (results == null)
                        {
                            return;
                        }
                    }
                }
                else
                {
                    // add dummy client handles to test that they get returned properly.
                    foreach (TsCDaItemValue item in items)
                    {
                        item.ClientHandle = item.ItemName;
                    }

                    results = m_server.Write(items);
                }

                // create a list of item value results.
                ArrayList values = new ArrayList();

                for (int ii = 0; ii < items.Length; ii++)
                {
                    TsCDaItemValueResult value = new TsCDaItemValueResult(items[ii]);

                    value.ItemName       = results[ii].ItemName;
                    value.ItemPath       = results[ii].ItemPath;
                    value.ClientHandle   = results[ii].ClientHandle;
                    value.ServerHandle   = results[ii].ServerHandle;
                    value.Result         = results[ii].Result;
                    value.DiagnosticInfo = results[ii].DiagnosticInfo;

                    values.Add(value);
                }

                // save results.
                m_values = (TsCDaItemValueResult[])values.ToArray(typeof(TsCDaItemValueResult));

                BackBTN.Enabled     = true;
                NextBTN.Enabled     = false;
                CancelBTN.Visible   = false;
                DoneBTN.Visible     = true;
                OptionsBTN.Visible  = false;
                ItemsCTRL.Visible   = false;
                ResultsCTRL.Visible = true;

                // display results.
                ResultsCTRL.Initialize(m_server, (m_subscription != null)?m_subscription.Locale:m_server.Locale, m_values);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }