use of org.eclipse.milo.opcua.sdk.server.items.MonitoredEventItem in project milo by eclipse.
the class SubscriptionManager method createMonitoredItem.
private BaseMonitoredItem<?> createMonitoredItem(MonitoredItemCreateRequest request, Subscription subscription, TimestampsToReturn timestamps, Map<NodeId, AttributeGroup> attributeGroups) throws UaException {
NodeId nodeId = request.getItemToMonitor().getNodeId();
UInteger attributeId = request.getItemToMonitor().getAttributeId();
QualifiedName dataEncoding = request.getItemToMonitor().getDataEncoding();
if (!AttributeId.isValid(attributeId)) {
throw new UaException(StatusCodes.Bad_AttributeIdInvalid);
}
if (dataEncoding.isNotNull()) {
if (!AttributeId.Value.isEqual(attributeId)) {
throw new UaException(StatusCodes.Bad_DataEncodingInvalid);
}
if (!dataEncoding.equals(DEFAULT_BINARY_ENCODING) && !dataEncoding.equals(DEFAULT_XML_ENCODING)) {
throw new UaException(StatusCodes.Bad_DataEncodingUnsupported);
}
}
AttributeGroup attributeGroup = attributeGroups.get(nodeId);
if (attributeId.equals(AttributeId.EventNotifier.uid())) {
UByte eventNotifier = attributeGroup.getEventNotifier();
// Verify that the SubscribeToEvents bit is set
if (eventNotifier == null || (eventNotifier.intValue() & 1) == 0) {
throw new UaException(StatusCodes.Bad_AttributeIdInvalid);
}
Object filterObject = request.getRequestedParameters().getFilter().decode(server.getSerializationContext());
MonitoringFilter filter = validateEventItemFilter(filterObject, attributeGroup);
UInteger requestedQueueSize = request.getRequestedParameters().getQueueSize();
AtomicReference<UInteger> revisedQueueSize = new AtomicReference<>(requestedQueueSize);
try {
server.getAddressSpaceManager().onCreateEventItem(request.getItemToMonitor(), requestedQueueSize, revisedQueueSize::set);
} catch (Throwable t) {
throw new UaException(StatusCodes.Bad_InternalError, t);
}
MonitoredEventItem monitoredEventItem = new MonitoredEventItem(server, session, uint(subscription.nextItemId()), subscription.getId(), request.getItemToMonitor(), request.getMonitoringMode(), timestamps, request.getRequestedParameters().getClientHandle(), 0.0, revisedQueueSize.get(), request.getRequestedParameters().getDiscardOldest());
monitoredEventItem.installFilter(filter);
return monitoredEventItem;
} else {
if (attributeId.equals(AttributeId.Value.uid())) {
UByte accessLevel = attributeGroup.getAccessLevel();
if (accessLevel == null)
accessLevel = ubyte(0);
UByte userAccessLevel = attributeGroup.getUserAccessLevel();
if (userAccessLevel == null)
userAccessLevel = ubyte(0);
EnumSet<AccessLevel> accessLevels = AccessLevel.fromValue(accessLevel);
EnumSet<AccessLevel> userAccessLevels = AccessLevel.fromValue(userAccessLevel);
if (!accessLevels.contains(AccessLevel.CurrentRead)) {
throw new UaException(StatusCodes.Bad_NotReadable);
}
if (!userAccessLevels.contains(AccessLevel.CurrentRead)) {
throw new UaException(StatusCodes.Bad_UserAccessDenied);
}
}
// Validate the requested index range by parsing it.
String indexRange = request.getItemToMonitor().getIndexRange();
if (indexRange != null)
NumericRange.parse(indexRange);
Double minimumSamplingInterval = -1.0;
try {
minimumSamplingInterval = attributeGroup.getMinimumSamplingInterval();
if (minimumSamplingInterval == null) {
minimumSamplingInterval = server.getConfig().getLimits().getMinSupportedSampleRate();
}
} catch (UaException e) {
if (e.getStatusCode().getValue() != StatusCodes.Bad_AttributeIdInvalid) {
throw e;
}
}
MonitoringFilter filter = MonitoredDataItem.DEFAULT_FILTER;
try {
ExtensionObject filterXo = request.getRequestedParameters().getFilter();
if (filterXo != null && !filterXo.isNull()) {
Object filterObject = filterXo.decode(server.getSerializationContext());
filter = validateDataItemFilter(filterObject, attributeId, attributeGroup);
}
} catch (UaSerializationException e) {
logger.debug("error decoding MonitoringFilter", e);
throw new UaException(StatusCodes.Bad_MonitoredItemFilterInvalid, e);
}
double requestedSamplingInterval = getSamplingInterval(subscription, minimumSamplingInterval, request.getRequestedParameters().getSamplingInterval());
UInteger requestedQueueSize = request.getRequestedParameters().getQueueSize();
AtomicReference<Double> revisedSamplingInterval = new AtomicReference<>(requestedSamplingInterval);
AtomicReference<UInteger> revisedQueueSize = new AtomicReference<>(requestedQueueSize);
try {
server.getAddressSpaceManager().onCreateDataItem(request.getItemToMonitor(), requestedSamplingInterval, requestedQueueSize, (rsi, rqs) -> {
revisedSamplingInterval.set(rsi);
revisedQueueSize.set(rqs);
});
} catch (Throwable t) {
throw new UaException(StatusCodes.Bad_InternalError, t);
}
MonitoredDataItem monitoredDataItem = new MonitoredDataItem(server, session, uint(subscription.nextItemId()), subscription.getId(), request.getItemToMonitor(), request.getMonitoringMode(), timestamps, request.getRequestedParameters().getClientHandle(), revisedSamplingInterval.get(), revisedQueueSize.get(), request.getRequestedParameters().getDiscardOldest());
monitoredDataItem.installFilter(filter);
return monitoredDataItem;
}
}
Aggregations