Пример #1
0
        /// <summary>
        /// Displays the dialog.
        /// </summary>
        /// <param name="server">The server instance.</param>
        /// <param name="itemCollection">The <see cref="System.EventArgs"/> Collection of items to display.</param>
        public void Show(Session server, List <NodeId> lstNodeIds)
        {
            if (server == null)
            {
                throw new ArgumentNullException("server");
            }

            // Set server,
            m_Session = server;

            // Add the items to the listview.
            foreach (NodeId node in lstNodeIds)
            {
                // build WriteValueInfo
                WriteValueInfo info = new WriteValueInfo();
                info.NodeId = node;

                // Create ListViewItem
                ListViewItem item = new ListViewItem("");

                // add subitems for nodeId and value
                item.SubItems.Add(node.ToString());
                item.SubItems.Add("");

                // Set tag on ListViewItem
                item.Tag = info;

                // add item to listview
                this.listView.Items.Add(item);
            }

            // Fit the width of the columns to header size.
            this.listView.Columns[0].Width = -2;
            this.listView.Columns[1].Width = -2;
            this.listView.Columns[2].Width = -2;

            // Read the attributes
            ReadAttributes();

            // read values
            UpdateCurrentValues();

            // Display the control,
            Show();

            // and bring it to front.
            BringToFront();
        }
Пример #2
0
        /// <summary>
        /// Reads and displays the new values.
        /// </summary>
        private void ReadAttributes()
        {
            // Prepare call to ClientAPI.
            int i = 0;

            foreach (ListViewItem item in this.listView.Items)
            {
                ReadValueIdCollection nodesToRead = new ReadValueIdCollection();
                WriteValueInfo        info        = item.Tag as WriteValueInfo;

                // DataType
                nodesToRead.Add(new ReadValueId()
                {
                    NodeId      = info.NodeId,
                    AttributeId = Attributes.DataType
                });
                // ValueRank
                nodesToRead.Add(new ReadValueId()
                {
                    NodeId      = info.NodeId,
                    AttributeId = Attributes.ValueRank
                });
                // ArrayDimension
                nodesToRead.Add(new ReadValueId()
                {
                    NodeId      = info.NodeId,
                    AttributeId = Attributes.ArrayDimensions
                });

                // Call to ClientAPI.
                List <DataValue> readResults = new List <DataValue>();

                try
                {
                    readResults = m_Session.Read(
                        nodesToRead,
                        0,
                        TimestampsToReturn.Both,
                        null);

                    if (StatusCode.IsGood(readResults[0].StatusCode) && StatusCode.IsGood(readResults[1].StatusCode))
                    {
                        // datatype
                        info.DataType = TypeUtils.GetBuiltInType((NodeId)readResults[0].Value, m_Session.Cache);

                        // value rank
                        info.ValueRank = (int)readResults[1].Value;

                        // array dimension
                        if (info.ValueRank == ValueRanks.Scalar)
                        {
                            info.ArrayDimensions = null;
                            info.AttributesRead  = true;
                        }
                        else if (info.ValueRank == ValueRanks.OneDimension)
                        {
                            info.ArrayDimensions = readResults[1].Value as List <uint>;
                            info.AttributesRead  = true;
                        }
                        // can't handle
                        else
                        {
                            info.Error = true;
                        }
                    }
                    else
                    {
                        info.Error = true;
                    }

                    // Update status label.
                    lblStatus.Text = "Reading attributes succeeded.";
                }

                catch (Exception e)
                {
                    // Update status label.
                    lblStatus.Text = "An exception occured while reading attributes: "
                                     + e.Message;
                }
                i++;
            }
        }
Пример #3
0
        /// <summary>
        /// Writes the values.
        /// </summary>
        private void WriteValues()
        {
            try
            {
                // Prepare call to ClientAPI.
                List <WriteValue> nodesToWrite = new List <WriteValue>();
                List <StatusCode> results      = null;
                int        index        = 0;
                List <int> lstUsedIndex = new List <int>();

                // fill write values
                foreach (ListViewItem item in this.listView.Items)
                {
                    WriteValueInfo writeValInfo = item.Tag as WriteValueInfo;

                    if (writeValInfo.AttributesRead == true && writeValInfo.Error == false)
                    {
                        try
                        {
                            // convert string to type
                            if (writeValInfo.ValueRank == ValueRanks.Scalar)
                            {
                                DataValue dataValue = new DataValue();
                                dataValue.Value = TypeUtils.Cast(this.listView.Items[index].SubItems[0].Text, writeValInfo.DataType);

                                nodesToWrite.Add(new WriteValue()
                                {
                                    NodeId      = writeValInfo.NodeId,
                                    Value       = dataValue,
                                    AttributeId = Attributes.Value
                                });

                                lstUsedIndex.Add(index);
                            }
                            else if (writeValInfo.ValueRank == ValueRanks.OneDimension)
                            {
                                DataValue dataValue = new DataValue();
                                dataValue.Value = TypeUtils.Cast(this.listView.Items[index].SubItems[0].Text, writeValInfo.DataType);

                                nodesToWrite.Add(new WriteValue()
                                {
                                    NodeId      = writeValInfo.NodeId,
                                    Value       = dataValue,
                                    AttributeId = Attributes.Value
                                });

                                lstUsedIndex.Add(index);
                            }
                        }
                        catch (Exception exception)
                        {
                            this.listView.Items[index].SubItems[0].Text = exception.Message;
                        }
                    }
                    index++;
                }

                // Call to ClientAPI.
                results = m_Session.Write(
                    nodesToWrite,
                    null);

                // Update status label.
                lblStatus.Text = "Writing values succeeded.";
            }
            catch (Exception e)
            {
                // Update status label.
                lblStatus.Text = "An exception occured while writing values: " + e.Message;
            }
        }