/// <summary> /// Checks if the subscription is ready to publish and returns a notification message. /// </summary> public NotificationMessage Publish() { lock (m_lock) { long currentTime = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond; // check of it is time for a publish. if (m_lastPublishTime + m_publishingInterval < currentTime) { ListOfMonitoredItemNotification notifications = new ListOfMonitoredItemNotification(); ListOfDiagnosticInfo diagnosticInfos = new ListOfDiagnosticInfo(); // check each monitored item for data changes to send. foreach (MonitoredItem monitoredItem in m_monitoredItems.Values) { while (monitoredItem.Values.Count > 0) { MonitoredItemNotification notification = new MonitoredItemNotification(); notification.ClientHandle = monitoredItem.Parameters.ClientHandle; notification.Value = monitoredItem.Values.Dequeue(); notifications.Add(notification); diagnosticInfos.Add(monitoredItem.DiagnosticInfos.Dequeue()); } } // check if any notifications were found. if (notifications.Count > 0) { // subscriptions can produce different types of notifications so the notification parameter // is an extensible parameter. This means the object must be manually serialized and wrapped in // an ExtensionObject which specifies the type of data contained in the Body. The complete // UA SDK takes care this housekeeping and will serialize extensible parameters automatically. DataChangeNotification body = new DataChangeNotification(); body.MonitoredItems = notifications; body.DiagnosticInfos = diagnosticInfos; ExtensionObject extension = new ExtensionObject( new ExpandedNodeId(Objects.DataChangeNotification_Encoding_DefaultXml), body); // construct the message and assign a new sequence number. NotificationMessage message = new NotificationMessage(); message.SequenceNumber = ++m_nextSequenceNumber; message.PublishTime = DateTime.UtcNow; message.NotificationData = new ListOfExtensionObject(); message.NotificationData.Add(extension); m_lastPublishTime = currentTime; m_nextKeepAliveTime = (long)(currentTime + m_publishingInterval * m_keepAliveCount); return message; } } // check if it is time for a keep alive. if (m_nextKeepAliveTime < currentTime) { NotificationMessage message = new NotificationMessage(); message.SequenceNumber = m_nextSequenceNumber; message.PublishTime = DateTime.UtcNow; message.NotificationData = new ListOfExtensionObject(); m_nextKeepAliveTime = (long)(currentTime + m_publishingInterval * m_keepAliveCount); return message; } return null; } }
/// <summary> /// Checks if the subscription is ready to publish and returns a notification message. /// </summary> public NotificationMessage Publish() { lock (m_lock) { long currentTime = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond; // check of it is time for a publish. if (m_lastPublishTime + m_publishingInterval < currentTime) { ListOfMonitoredItemNotification notifications = new ListOfMonitoredItemNotification(); ListOfDiagnosticInfo diagnosticInfos = new ListOfDiagnosticInfo(); // check each monitored item for data changes to send. foreach (MonitoredItem monitoredItem in m_monitoredItems.Values) { while (monitoredItem.Values.Count > 0) { MonitoredItemNotification notification = new MonitoredItemNotification(); notification.ClientHandle = monitoredItem.Parameters.ClientHandle; notification.Value = monitoredItem.Values.Dequeue(); notifications.Add(notification); diagnosticInfos.Add(monitoredItem.DiagnosticInfos.Dequeue()); } } // check if any notifications were found. if (notifications.Count > 0) { // subscriptions can produce different types of notifications so the notification parameter // is an extensible parameter. This means the object must be manually serialized and wrapped in // an ExtensionObject which specifies the type of data contained in the Body. The complete // UA SDK takes care this housekeeping and will serialize extensible parameters automatically. DataChangeNotification body = new DataChangeNotification(); body.MonitoredItems = notifications; body.DiagnosticInfos = diagnosticInfos; ExtensionObject extension = new ExtensionObject( new ExpandedNodeId(Objects.DataChangeNotification_Encoding_DefaultXml), body); // construct the message and assign a new sequence number. NotificationMessage message = new NotificationMessage(); message.SequenceNumber = ++m_nextSequenceNumber; message.PublishTime = DateTime.UtcNow; message.NotificationData = new ListOfExtensionObject(); message.NotificationData.Add(extension); m_lastPublishTime = currentTime; m_nextKeepAliveTime = (long)(currentTime + m_publishingInterval * m_keepAliveCount); return(message); } } // check if it is time for a keep alive. if (m_nextKeepAliveTime < currentTime) { NotificationMessage message = new NotificationMessage(); message.SequenceNumber = m_nextSequenceNumber; message.PublishTime = DateTime.UtcNow; message.NotificationData = new ListOfExtensionObject(); m_nextKeepAliveTime = (long)(currentTime + m_publishingInterval * m_keepAliveCount); return(message); } return(null); } }