use of org.eclipse.milo.opcua.stack.core.types.structured.StatusChangeNotification in project milo by eclipse.
the class OpcUaSubscriptionManager method deliverNotificationMessage.
private CompletableFuture<Unit> deliverNotificationMessage(OpcUaSubscription subscription, NotificationMessage notificationMessage) {
CompletableFuture<Unit> delivered = new CompletableFuture<>();
subscription.getNotificationSemaphore().acquire().thenAccept(permit -> deliveryQueue.submit(() -> {
try {
Map<UInteger, OpcUaMonitoredItem> items = subscription.getItemsByClientHandle();
List<ExtensionObject> notificationData = l(notificationMessage.getNotificationData());
if (notificationData.isEmpty()) {
subscriptionListeners.forEach(listener -> listener.onKeepAlive(subscription, notificationMessage.getPublishTime()));
subscription.getNotificationListeners().forEach(listener -> listener.onKeepAliveNotification(subscription, notificationMessage.getPublishTime()));
}
for (ExtensionObject xo : notificationData) {
Object o = xo.decode(client.getStaticSerializationContext());
if (o instanceof DataChangeNotification) {
DataChangeNotification dcn = (DataChangeNotification) o;
List<MonitoredItemNotification> monitoredItemNotifications = l(dcn.getMonitoredItems());
int notificationCount = monitoredItemNotifications.size();
logger.debug("Received {} MonitoredItemNotifications", notificationCount);
for (MonitoredItemNotification min : monitoredItemNotifications) {
logger.trace("MonitoredItemNotification: clientHandle={}, value={}", min.getClientHandle(), min.getValue());
OpcUaMonitoredItem item = items.get(min.getClientHandle());
if (item != null)
item.onValueArrived(min.getValue());
else
logger.warn("no item for clientHandle=" + min.getClientHandle());
}
if (notificationCount == 0) {
subscriptionListeners.forEach(listener -> listener.onKeepAlive(subscription, notificationMessage.getPublishTime()));
subscription.getNotificationListeners().forEach(listener -> listener.onKeepAliveNotification(subscription, notificationMessage.getPublishTime()));
} else {
if (!subscription.getNotificationListeners().isEmpty()) {
List<UaMonitoredItem> monitoredItems = new ArrayList<>();
List<DataValue> dataValues = new ArrayList<>();
for (MonitoredItemNotification n : monitoredItemNotifications) {
UaMonitoredItem item = subscription.getItemsByClientHandle().get(n.getClientHandle());
if (item != null) {
monitoredItems.add(item);
dataValues.add(n.getValue());
}
}
subscription.getNotificationListeners().forEach(listener -> listener.onDataChangeNotification(subscription, monitoredItems, dataValues, notificationMessage.getPublishTime()));
}
}
} else if (o instanceof EventNotificationList) {
EventNotificationList enl = (EventNotificationList) o;
List<EventFieldList> eventFieldLists = l(enl.getEvents());
for (EventFieldList efl : eventFieldLists) {
logger.trace("EventFieldList: clientHandle={}, values={}", efl.getClientHandle(), Arrays.toString(efl.getEventFields()));
OpcUaMonitoredItem item = items.get(efl.getClientHandle());
if (item != null)
item.onEventArrived(efl.getEventFields());
}
if (!subscription.getNotificationListeners().isEmpty()) {
List<UaMonitoredItem> monitoredItems = new ArrayList<>();
List<Variant[]> eventFields = new ArrayList<>();
for (EventFieldList efl : eventFieldLists) {
UaMonitoredItem item = subscription.getItemsByClientHandle().get(efl.getClientHandle());
if (item != null) {
monitoredItems.add(item);
eventFields.add(efl.getEventFields());
}
}
subscription.getNotificationListeners().forEach(listener -> listener.onEventNotification(subscription, monitoredItems, eventFields, notificationMessage.getPublishTime()));
}
} else if (o instanceof StatusChangeNotification) {
StatusChangeNotification scn = (StatusChangeNotification) o;
logger.debug("StatusChangeNotification: {}", scn.getStatus());
subscriptionListeners.forEach(listener -> listener.onStatusChanged(subscription, scn.getStatus()));
subscription.getNotificationListeners().forEach(listener -> listener.onStatusChangedNotification(subscription, scn.getStatus()));
if (scn.getStatus().getValue() == StatusCodes.Bad_Timeout) {
subscriptions.remove(subscription.getSubscriptionId());
}
}
}
} finally {
permit.release();
delivered.complete(Unit.VALUE);
}
}));
return delivered;
}
use of org.eclipse.milo.opcua.stack.core.types.structured.StatusChangeNotification in project milo by eclipse.
the class Subscription method returnStatusChangeNotification.
void returnStatusChangeNotification(ServiceRequest service, StatusCode status) {
StatusChangeNotification statusChange = new StatusChangeNotification(status, null);
UInteger sequenceNumber = uint(nextSequenceNumber());
NotificationMessage notificationMessage = new NotificationMessage(sequenceNumber, DateTime.now(), new ExtensionObject[] { ExtensionObject.encode(serializationContext, statusChange) });
ResponseHeader header = service.createResponseHeader();
PublishResponse response = new PublishResponse(header, subscriptionId, new UInteger[0], false, notificationMessage, service.attr(KEY_ACK_RESULTS).get(), new DiagnosticInfo[0]);
service.setResponse(response);
logger.debug("[id={}] returned StatusChangeNotification ({}) sequenceNumber={}.", subscriptionId, status, sequenceNumber);
}
Aggregations