Files
UA-.NETStandard/SampleApplications/Samples/Controls/Subscriptions/NotificationMessageListCtrl.cs
T

356 lines
11 KiB
C#

/* Copyright (c) 1996-2016, OPC Foundation. All rights reserved.
The source code in this file is covered under a dual-license scenario:
- RCL: for OPC Foundation members in good-standing
- GPL V2: everybody else
RCL license terms accompanied with this source code. See http://opcfoundation.org/License/RCL/1.00/
GNU General Public License as published by the Free Software Foundation;
version 2 of the License are accompanied with this source code. See http://opcfoundation.org/License/GPLv2
This source code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
using System;
using System.Collections.Generic;
using System.Reflection;
using Opc.Ua.Client;
using Opc.Ua.Client.Controls;
using Windows.UI.Xaml.Controls;
namespace Opc.Ua.Sample.Controls
{
public partial class NotificationMessageListCtrl : Opc.Ua.Client.Controls.BaseListCtrl
{
#region Constructors
/// <summary>
/// Initializes the object with default values.
/// </summary>
public NotificationMessageListCtrl()
{
MaxMessageCount = 10;
InitializeComponent();
//SetColumns(m_ColumnNames);
//ItemsLV.Sorting = SortOrder.Descending;
m_SessionNotification = new NotificationEventHandler(Session_Notification);
}
#endregion
#region Private Fields
private Session m_session;
private Subscription m_subscription;
private NotificationEventHandler m_SessionNotification;
private int m_maxMessageCount;
#if TODO
/// <summary>
/// The columns to display in the control.
/// </summary>
private readonly object[][] m_ColumnNames = new object[][]
{
new object[] { "Subscription", HorizontalAlignment.Left, null },
new object[] { "Message ID", HorizontalAlignment.Center, null },
new object[] { "Publish Time", HorizontalAlignment.Center, null },
new object[] { "Notifications", HorizontalAlignment.Center, null },
new object[] { "Data Changes", HorizontalAlignment.Center, null },
new object[] { "EventTypes", HorizontalAlignment.Center, null }
};
#endif
#endregion
#region Public Interface
/// <summary>
/// The maximum number of messages displayed in the control.
/// </summary>
public int MaxMessageCount
{
get { return m_maxMessageCount; }
set { m_maxMessageCount = value; }
}
/// <summary>
/// Clears the contents of the control,
/// </summary>
public void Clear()
{
ItemsLV.Items.Clear();
//AdjustColumns();
}
/// <summary>
/// Initializes the control with the session/subscription indicated.
/// </summary>
public void Initialize(Session session, Subscription subscription)
{
// do nothing if nothing has changed.
if (Object.ReferenceEquals(session, m_session) && Object.ReferenceEquals(subscription, m_subscription))
{
return;
}
// subscription to event notifications.
if (!Object.ReferenceEquals(session, m_session))
{
if (m_session != null)
{
m_session.Notification -= m_SessionNotification;
}
if (session != null)
{
session.Notification += m_SessionNotification;
}
}
Clear();
m_session = session;
m_subscription = subscription;
// nothing to do if no session provided.
if (m_session == null)
{
return;
}
List<ItemData> tags = new List<ItemData>();
// display only items for current subscription.
if (subscription != null)
{
foreach (NotificationMessage item in subscription.Notifications)
{
tags.Insert(0, new ItemData(subscription, item));
}
}
// display all notifications for all subscriptions.
else
{
foreach (Subscription item1 in m_session.Subscriptions)
{
foreach (NotificationMessage item2 in item1.Notifications)
{
tags.Insert(0, new ItemData(item1, item2));
}
}
}
// update control.
Update(tags);
}
#endregion
#region ItemData Class
/// <summary>
/// Stores the data associated with a list view item.
/// </summary>
private class ItemData
{
public Subscription Subscription;
public NotificationMessage NotificationMessage;
public ItemData(
Subscription subscription,
NotificationMessage notificationMessage)
{
Subscription = subscription;
NotificationMessage = notificationMessage;
}
}
#endregion
#region Overridden Methods
/// <see cref="BaseListCtrl.EnableMenuItems" />
protected override void EnableMenuItems(ListViewItem clickedItem)
{
#if TODO
if (m_session != null)
{
OptionsMI.Enabled = true;
ClearMI.Enabled = true;
RepublishMI.Enabled = m_subscription != null;
if (clickedItem != null)
{
ItemData itemData = clickedItem.Tag as ItemData;
if (itemData != null)
{
ViewMI.Enabled = true;
DeleteMI.Enabled = true;
}
}
}
#endif
}
/// <see cref="BaseListCtrl.UpdateItem" />
protected override void UpdateItem(ListViewItem listItem, object item)
{
ItemData itemData = item as ItemData;
if (itemData == null)
{
base.UpdateItem(listItem, item);
return;
}
#if TODO
listItem.SubItems[0].Text = String.Format("{0}", itemData.Subscription.DisplayName);
listItem.SubItems[1].Text = String.Format("{0}", itemData.NotificationMessage.SequenceNumber);
listItem.SubItems[2].Text = String.Format("{0:HH:mm:ss.fff}", itemData.NotificationMessage.PublishTime.ToLocalTime());
#endif
int events = 0;
int datachanges = 0;
int notifications = 0;
foreach (ExtensionObject notification in itemData.NotificationMessage.NotificationData)
{
notifications++;
if (ExtensionObject.IsNull(notification))
{
continue;
}
DataChangeNotification datachangeNotification = notification.Body as DataChangeNotification;
if (datachangeNotification != null)
{
datachanges += datachangeNotification.MonitoredItems.Count;
}
EventNotificationList EventNotification = notification.Body as EventNotificationList;
if (EventNotification != null)
{
events += EventNotification.Events.Count;
}
}
#if TODO
listItem.SubItems[3].Text = String.Format("{0}", notifications);
listItem.SubItems[4].Text = String.Format("{0}", datachanges);
listItem.SubItems[5].Text = String.Format("{0}", events);
#endif
listItem.Tag = item;
}
#endregion
private void Update(List<ItemData> tags)
{
if (tags.Count > MaxMessageCount)
{
tags.RemoveRange(MaxMessageCount, tags.Count-MaxMessageCount);
}
BeginUpdate();
foreach (ItemData tag in tags)
{
AddItem(tag);
}
EndUpdate();
}
private void Session_Notification(Session sender, NotificationEventArgs e)
{
try
{
if (m_subscription != null)
{
if (!Object.ReferenceEquals(m_subscription, e.Subscription))
{
return;
}
}
// get the current control contents.
List<ItemData> tags = new List<ItemData>();
foreach (ListViewItem item in ItemsLV.Items)
{
ItemData tag = item.Tag as ItemData;
if (tag != null)
{
tags.Add(tag);
}
}
tags.Insert(0, new ItemData(e.Subscription, e.NotificationMessage));
// update control.
Update(tags);
}
catch (Exception exception)
{
GuiUtils.HandleException(String.Empty, GuiUtils.CallerName(), exception);
}
}
private void ViewMI_Click(object sender, EventArgs e)
{
try
{
}
catch (Exception exception)
{
GuiUtils.HandleException(String.Empty, GuiUtils.CallerName(), exception);
}
}
private void DeleteMI_Click(object sender, EventArgs e)
{
try
{
for (int ii = 0; ii < ItemsLV.SelectedItems.Count;)
{
ItemsLV.SelectedItems.RemoveAt(ii);
}
}
catch (Exception exception)
{
GuiUtils.HandleException(String.Empty, GuiUtils.CallerName(), exception);
}
}
private void ClearMI_Click(object sender, EventArgs e)
{
try
{
Clear();
}
catch (Exception exception)
{
GuiUtils.HandleException(String.Empty, GuiUtils.CallerName(), exception);
}
}
private void RepublishMI_Click(object sender, EventArgs e)
{
try
{
if (m_subscription == null)
{
return;
}
#if TODO
NotificationMessage message = new RepublishNotificationMessageDlg().ShowDialog(m_subscription);
if (message != null)
{
ListViewItem listItem = AddItem(new ItemData(m_subscription, message));
listItem.ForeColor = Color.Red;
}
#endif
}
catch (Exception exception)
{
GuiUtils.HandleException(String.Empty, GuiUtils.CallerName(), exception);
}
}
}
}