/// <summary> /// Raised when a publish response arrives from the server. /// </summary> /// <param name="session">The target of the event.</param> /// <param name="e">The <see cref="Opc.Ua.Client.NotifacationEventArgs"/>Instance containing the event data.</param> private void Session_Notification(Session session, NotificationEventArgs e) { NotificationMessage message = e.NotificationMessage; // Check for keep alive. if (message.NotificationData.Count == 0) { return; } // Get the data changes (oldest to newest). foreach (MonitoredItemNotification datachange in message.GetDataChanges(false)) { // Lookup the monitored item. MonitoredItem monitoredItem = e.Subscription.FindItemByClientHandle(datachange.ClientHandle); if (monitoredItem == null) { continue; } ClientMonitoredItemData clientData = monitoredItem.Handle as ClientMonitoredItemData; clientData.callback(clientData.clientHandle, datachange.Value); } }
/// <summary> /// Creates a monitored item and adds it to the existing subscription. /// </summary> /// <param name="variableNodeId">The according nodeid.</param> /// <param name="clientHandle">The handle of the client registering items.</param> /// <param name="callback">The callback to retrieve value changes.</param> /// <param name="samplingRate">The requested sampling rate.</param> /// <param name="serverHandle">The handle of the item.</param> public void AddDataMonitoredItem(NodeId variableNodeId, object clientHandle, valueChanged callback, uint samplingRate, out object serverHandle) { serverHandle = null; try { MonitoredItem monitoredItem = new MonitoredItem(m_Subscription.DefaultItem); ClientMonitoredItemData clientData = new ClientMonitoredItemData(); clientData.callback = callback; clientData.clientHandle = clientHandle; // Monitored item settings: monitoredItem.StartNodeId = variableNodeId; monitoredItem.AttributeId = Attributes.Value; monitoredItem.MonitoringMode = MonitoringMode.Reporting; monitoredItem.SamplingInterval = (int)samplingRate; // Affects the read cycle between UA Server and data source monitoredItem.QueueSize = 1; monitoredItem.DiscardOldest = false; monitoredItem.Handle = clientData; // Add item to subscription. m_Subscription.AddItem(monitoredItem); // Call the server and apply any changes to the state of the subscription or monitored items. m_Subscription.ApplyChanges(); // Check result of add. if (monitoredItem.Status.Error != null && StatusCode.IsBad(monitoredItem.Status.Error.StatusCode)) { throw ServiceResultException.Create( monitoredItem.Status.Error.StatusCode.Code, "Creation of data monitored item failed"); } serverHandle = monitoredItem; myMonitorItemServerHandlerList.Add(serverHandle); } catch (Exception e) { throw e; } }