/// <summary> /// Shows a detailed view for array values. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> //TODO make it visible after implementing complex values. private void m_CMS_View_Click(object sender, System.EventArgs e) { if (m_ItemListLV.SelectedItems.Count > 0) { ListViewItem listItem = m_ItemListLV.SelectedItems[0]; object tag = listItem.Tag; if (tag != null && tag.GetType() == typeof(OpcDa::ItemValueResult)) { OpcDa::ItemValueResult item = (OpcDa::ItemValueResult)tag; if (item.Value != null) { ComplexItem complexItem = ComplexTypeCache.GetComplexItem(item); if (complexItem != null) { EditComplexValueDlg dialog = (EditComplexValueDlg)m_viewers[listItem]; if (dialog == null) { m_viewers[listItem] = dialog = new EditComplexValueDlg(); dialog.Disposed += new System.EventHandler(OnViewerClosed); } dialog.ShowDialog(complexItem, item.Value, true, false); } else if (item.Value.GetType().IsArray) { new EditArrayDlg().ShowDialog(item.Value, true); } } } } }
/// <summary> /// Handles the MouseDown event of the m_ItemListLV control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param> private void m_ItemListLV_MouseDown(object sender, MouseEventArgs e) { // ignore left button actions. if (e.Button != MouseButtons.Right) { return; } // disable everything. m_CMS_View.Enabled = false; // selects the item that was right clicked on. ListViewItem clickedItem = m_ItemListLV.GetItemAt(e.X, e.Y); // no item clicked on - do nothing. if (clickedItem == null) { return; } // force selection to clicked node. clickedItem.Selected = true; return; //TODO implement complex data and remove above return if (m_ItemListLV.SelectedItems.Count == 1) { if (clickedItem.Tag != null && clickedItem.Tag.GetType() == typeof(OpcDa::ItemValueResult)) { OpcDa::ItemValueResult item = (OpcDa::ItemValueResult)clickedItem.Tag; if (item.Value != null) { m_CMS_View.Enabled = ((ComplexTypeCache.GetComplexItem(item) != null) || (item.Value.GetType().IsArray)); } } } }
/// <summary> /// Returns the value of the specified field. /// </summary> private object GetFieldValue(OpcDa::ItemValueResult item, int fieldID) { object fieldValue = null; switch (fieldID) { case ITEM_NAME: { return(item.ItemName); } case ITEM_PATH: { return(item.ItemPath); } // case CLIENT_HANDLE: { return Opc.Convert.ToString(item.ClientHandle); } // case SERVER_HANDLE: { return Opc.Convert.ToString(item.ServerHandle); } case VALUE: { return(Opc.Convert.ToString(item.Value)); } case DATA_TYPE: { return((item.Value != null) ? item.Value.GetType() : fieldValue); } case QUALITY_BITS: { return((item.QualitySpecified && item.Quality.QualityBits != OpcDa::qualityBits.good) ? item.Quality.QualityBits : fieldValue); } case LIMIT_BITS: { return((item.QualitySpecified && item.Quality.LimitBits != OpcDa::limitBits.none) ? item.Quality.LimitBits : fieldValue); } case VENDOR_BITS: { return((item.QualitySpecified && item.Quality.VendorBits != 0) ? item.Quality.VendorBits : fieldValue); } case TIMESTAMP: { return((item.TimestampSpecified) ? item.Timestamp : fieldValue); } case ERROR: { return(GetErrorText(item.ResultID)); } } return(null); }
/// <summary> /// Shows a detailed view for array values. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void ViewMI_Click(object sender, System.EventArgs e) { if (ItemListLV.SelectedItems.Count > 0) { object tag = ItemListLV.SelectedItems[0].Tag; if (tag != null && tag.GetType() == typeof(OpcDa::ItemValueResult)) { OpcDa::ItemValueResult item = (OpcDa::ItemValueResult)tag; if (item.Value != null) { ComplexItem complexItem = ComplexTypeCache.GetComplexItem(item); if (complexItem != null) { new EditComplexValueDlg().ShowDialog(complexItem, item.Value, true, true); } else if (item.Value.GetType().IsArray) { new EditArrayDlg().ShowDialog(item.Value, true); } } } } }
/// <summary> /// Adds an item to the list view. /// </summary> /// <param name="subscriptionHandle">The subscription handle.</param> /// <param name="item">The item.</param> /// <param name="replaceableItem">The replaceable item.</param> private void AddValue(object subscriptionHandle, OpcDa::ItemValueResult item, ref ListViewItem replaceableItem) { string quality = ""; // format quality. if (item.QualitySpecified) { quality += item.Quality.QualityBits.ToString(); if (item.Quality.LimitBits != OpcDa::limitBits.none) { quality += ", "; quality += item.Quality.LimitBits.ToString(); } if (item.Quality.VendorBits != 0) { quality += System.String.Format(", {0:X}", item.Quality.VendorBits); } } // format columns. string[] columns = new string[] { item.ItemName, item.ItemPath, Opc.Convert.ToString(item.Value), (item.Value != null)?Opc.Convert.ToString(item.Value.GetType()):"", quality, (item.TimestampSpecified)?Opc.Convert.ToString(item.Timestamp):"", GetErrorText(subscriptionHandle, item.ResultID) }; // update an existing item. if (replaceableItem != null) { for (int ii = 0; ii < replaceableItem.SubItems.Count; ii++) { replaceableItem.SubItems[ii].Text = columns[ii]; } replaceableItem.Tag = item; replaceableItem.ForeColor = Color.Empty; // update detail view dialog. EditComplexValueDlg dialog = (EditComplexValueDlg)m_viewers[replaceableItem]; if (dialog != null) { dialog.UpdateValue(item.Value); } return; } // create a new list view item. replaceableItem = new ListViewItem(columns, (int)ImageListLibrary.Icons.IMAGE_TAG); replaceableItem.Tag = item; // insert after any existing item value with the same client handle. for (int ii = m_ItemListLV.Items.Count - 1; ii >= 0; ii--) { OpcDa::ItemValueResult previous = (OpcDa::ItemValueResult)m_ItemListLV.Items[ii].Tag; if (previous.ClientHandle != null && previous.ClientHandle.Equals(item.ClientHandle)) { m_ItemListLV.Items.Insert(ii + 1, replaceableItem); return; } } m_ItemListLV.Items.Add(replaceableItem); }
/// <summary> /// Called when a data update event is raised by a subscription. /// </summary> private void OnDataChange(object subscriptionHandle, object requestHandle, OpcDa::ItemValueResult[] values) { // ensure processing is done on the control's main thread. if (InvokeRequired) { BeginInvoke(new OpcDa::DataChangedEventHandler(OnDataChange), new object[] { subscriptionHandle, requestHandle, values }); return; } try { // find subscription. ArrayList existingItemList = (ArrayList)m_items[subscriptionHandle]; // check if subscription is still valid. if (existingItemList == null) { return; } // change all existing item values for the subscription to 'grey'. // this indicates an update arrived but the value did not change. foreach (ListViewItem listItem in existingItemList) { if (listItem.ForeColor != Color.Gray) { listItem.ForeColor = Color.Gray; } } // do nothing more if only a keep alive callback. if (values == null || values.Length == 0) { OnKeepAlive(subscriptionHandle); return; } if (UpdateEvent != null) { UpdateEvent(subscriptionHandle, values); } // update list view. ArrayList newListItems = new ArrayList(); foreach (OpcDa::ItemValueResult value in values) { // item value should never have a null client handle. if (value.ClientHandle == null) { continue; } // this item can be updated with new values instead of inserting/removing items. ListViewItem replacableItem = null; // remove existing items. if (!m_CMS_KeepValues.Checked) { // search list of existing items for previous values for this item. ArrayList updatedItemList = new ArrayList(existingItemList.Count); foreach (ListViewItem listItem in existingItemList) { OpcDa::ItemValueResult previous = (OpcDa::ItemValueResult)listItem.Tag; if (!value.ClientHandle.Equals(previous.ClientHandle)) { updatedItemList.Add(listItem); continue; } if (replacableItem != null) { EditComplexValueDlg dialog = (EditComplexValueDlg)m_viewers[replacableItem]; if (dialog != null) { dialog.Close(); m_viewers.Remove(replacableItem); } replacableItem.Remove(); } replacableItem = listItem; } // the updated list has all values for the current item removed. existingItemList = updatedItemList; } // add value to list. AddValue(subscriptionHandle, value, ref replacableItem); // save new list item. if (replacableItem != null) { newListItems.Add(replacableItem); } } // add new items to existing item list. existingItemList.AddRange(newListItems); m_items[subscriptionHandle] = existingItemList; // adjust column widths. for (int ii = 0; ii < m_ItemListLV.Columns.Count; ii++) { if (ii != VALUE && ii != QUALITY) { m_ItemListLV.Columns[ii].Width = -2; } } } catch (System.Exception e) { OnException(subscriptionHandle, e); } }
/// <summary> /// Executes a write request for the current set if items. /// </summary> private void DoWrite() { try { // get the selected items OpcDa::ItemValue[] items = ItemsCTRL.GetItems(); // write to server. Opc.IdentifiedResult[] 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 (OpcDa::ItemValue 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++) { OpcDa::ItemValueResult value = new OpcDa::ItemValueResult(items[ii]); value.ItemName = results[ii].ItemName; value.ItemPath = results[ii].ItemPath; value.ClientHandle = results[ii].ClientHandle; value.ServerHandle = results[ii].ServerHandle; value.ResultID = results[ii].ResultID; value.DiagnosticInfo = results[ii].DiagnosticInfo; values.Add(value); } // save results. m_values = (OpcDa::ItemValueResult[])values.ToArray(typeof(OpcDa::ItemValueResult)); 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); } }