use of org.eclipse.milo.opcua.stack.core.types.structured.SimpleAttributeOperand in project milo by eclipse.
the class ManagedSubscriptionTest method eventChangeListener.
@Test
public void eventChangeListener() throws UaException, InterruptedException {
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
subscription.addChangeListener(new ChangeListener() {
@Override
public void onEventReceived(List<ManagedEventItem> eventItems, List<Variant[]> eventFields) {
if (eventItems.get(0).getNodeId().equals(Identifiers.Server)) {
latch1.countDown();
}
}
});
subscription.addEventChangeListener((eventItems, eventFields) -> {
if (eventItems.get(0).getNodeId().equals(Identifiers.Server)) {
latch2.countDown();
}
});
EventFilter eventFilter = new EventFilter(new SimpleAttributeOperand[] { new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "EventId") }, AttributeId.Value.uid(), null), new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "Time") }, AttributeId.Value.uid(), null), new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "Message") }, AttributeId.Value.uid(), null) }, new ContentFilter(null));
ManagedEventItem eventItem = subscription.createEventItem(Identifiers.Server, eventFilter);
assertTrue(eventItem.getStatusCode().isGood());
assertTrue(latch1.await(5, TimeUnit.SECONDS));
assertTrue(latch2.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.milo.opcua.stack.core.types.structured.SimpleAttributeOperand in project milo by eclipse.
the class EventSubscriptionExample method run.
@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
// synchronous connect
client.connect().get();
// create a subscription and a monitored item
UaSubscription subscription = client.getSubscriptionManager().createSubscription(1000.0).get();
ReadValueId readValueId = new ReadValueId(Identifiers.Server, AttributeId.EventNotifier.uid(), null, QualifiedName.NULL_VALUE);
// client handle must be unique per item
UInteger clientHandle = uint(clientHandles.getAndIncrement());
EventFilter eventFilter = new EventFilter(new SimpleAttributeOperand[] { new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "EventId") }, AttributeId.Value.uid(), null), new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "EventType") }, AttributeId.Value.uid(), null), new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "Severity") }, AttributeId.Value.uid(), null), new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "Time") }, AttributeId.Value.uid(), null), new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "Message") }, AttributeId.Value.uid(), null) }, new ContentFilter(null));
MonitoringParameters parameters = new MonitoringParameters(clientHandle, 0.0, ExtensionObject.encode(client.getStaticSerializationContext(), eventFilter), uint(10), true);
MonitoredItemCreateRequest request = new MonitoredItemCreateRequest(readValueId, MonitoringMode.Reporting, parameters);
List<UaMonitoredItem> items = subscription.createMonitoredItems(TimestampsToReturn.Both, newArrayList(request)).get();
// do something with the value updates
UaMonitoredItem monitoredItem = items.get(0);
final AtomicInteger eventCount = new AtomicInteger(0);
monitoredItem.setEventConsumer((item, vs) -> {
logger.info("Event Received from {}", item.getReadValueId().getNodeId());
for (int i = 0; i < vs.length; i++) {
logger.info("\tvariant[{}]: {}", i, vs[i].getValue());
}
if (eventCount.incrementAndGet() == 3) {
future.complete(client);
}
});
}
use of org.eclipse.milo.opcua.stack.core.types.structured.SimpleAttributeOperand in project milo by eclipse.
the class EventContentFilter method validateFilterElement.
private static ContentFilterElementResult validateFilterElement(@NotNull FilterContext context, @NotNull ContentFilterElement filterElement) {
FilterOperator filterOperator = filterElement.getFilterOperator();
if (!Operators.SUPPORTED_OPERATORS.contains(filterOperator)) {
return new ContentFilterElementResult(new StatusCode(StatusCodes.Bad_FilterOperatorUnsupported), new StatusCode[0], new DiagnosticInfo[0]);
}
ExtensionObject[] xos = filterElement.getFilterOperands();
if (xos == null || xos.length == 0) {
return new ContentFilterElementResult(new StatusCode(StatusCodes.Bad_FilterOperandCountMismatch), new StatusCode[0], new DiagnosticInfo[0]);
}
FilterOperand[] operands = new FilterOperand[xos.length];
StatusCode[] operandStatusCodes = new StatusCode[xos.length];
for (int i = 0; i < xos.length; i++) {
Object operand = xos[i].decodeOrNull(context.getServer().getSerializationContext());
if (operand instanceof FilterOperand) {
operands[i] = (FilterOperand) operand;
if (operand instanceof SimpleAttributeOperand) {
try {
validateSimpleOperand(context, (SimpleAttributeOperand) operand);
operandStatusCodes[i] = StatusCode.GOOD;
} catch (ValidationException e) {
operandStatusCodes[i] = e.getStatusCode();
}
} else if (operand instanceof ElementOperand) {
operandStatusCodes[i] = StatusCode.GOOD;
} else if (operand instanceof LiteralOperand) {
operandStatusCodes[i] = StatusCode.GOOD;
} else {
// includes AttributeOperand and any unknown/unhandle subclasses
operandStatusCodes[i] = new StatusCode(StatusCodes.Bad_FilterOperandInvalid);
}
} else {
operandStatusCodes[i] = new StatusCode(StatusCodes.Bad_FilterOperandInvalid);
}
}
StatusCode operatorStatus = StatusCode.GOOD;
try {
Operator<?> operator = getOperator(filterOperator);
operator.validate(context, operands);
} catch (ValidationException e) {
operatorStatus = e.getStatusCode();
}
return new ContentFilterElementResult(operatorStatus, operandStatusCodes, new DiagnosticInfo[0]);
}
use of org.eclipse.milo.opcua.stack.core.types.structured.SimpleAttributeOperand in project milo by eclipse.
the class ManagedSubscriptionTest method createAndDeleteEventItem.
@Test
public void createAndDeleteEventItem() throws UaException {
EventFilter eventFilter = new EventFilter(new SimpleAttributeOperand[] { new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "EventId") }, AttributeId.Value.uid(), null), new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "Time") }, AttributeId.Value.uid(), null), new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "Message") }, AttributeId.Value.uid(), null) }, new ContentFilter(null));
ManagedEventItem eventItem = subscription.createEventItem(Identifiers.Server, eventFilter);
assertTrue(eventItem.getStatusCode().isGood());
subscription.deleteEventItem(eventItem);
assertFalse(subscription.getEventItems().contains(eventItem));
}
use of org.eclipse.milo.opcua.stack.core.types.structured.SimpleAttributeOperand in project milo by eclipse.
the class EventContentFilter method validateSelectClauses.
private static SelectClauseValidationResult validateSelectClauses(FilterContext context, SimpleAttributeOperand[] selectClauses) {
List<StatusCode> statusCodes = new ArrayList<>();
List<DiagnosticInfo> diagnosticInfos = new ArrayList<>();
for (SimpleAttributeOperand select : selectClauses) {
try {
validateSimpleOperand(context, select);
statusCodes.add(StatusCode.GOOD);
diagnosticInfos.add(DiagnosticInfo.NULL_VALUE);
} catch (ValidationException e) {
statusCodes.add(e.getStatusCode());
diagnosticInfos.add(e.getDiagnosticInfo());
} catch (Throwable t) {
LOGGER.error("Unexpected error validating operand: {}", select, t);
statusCodes.add(new StatusCode(StatusCodes.Bad_InternalError));
diagnosticInfos.add(DiagnosticInfo.NULL_VALUE);
}
}
return new SelectClauseValidationResult(statusCodes.toArray(new StatusCode[0]), diagnosticInfos.toArray(new DiagnosticInfo[0]));
}
Aggregations