use of org.eclipse.milo.opcua.stack.core.types.builtin.DateTime in project milo by eclipse.
the class ReadNodeExample method run.
@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
// synchronous connect
client.connect().get();
// Get a typed reference to the Server object: ServerNode
ServerTypeNode serverNode = (ServerTypeNode) client.getAddressSpace().getObjectNode(Identifiers.Server, Identifiers.ServerType);
// Read properties of the Server object...
String[] serverArray = serverNode.getServerArray();
String[] namespaceArray = serverNode.getNamespaceArray();
logger.info("ServerArray={}", Arrays.toString(serverArray));
logger.info("NamespaceArray={}", Arrays.toString(namespaceArray));
// Read the value of attribute the ServerStatus variable component
ServerStatusDataType serverStatus = serverNode.getServerStatus();
logger.info("ServerStatus={}", serverStatus);
// Get a typed reference to the ServerStatus variable
// component and read value attributes individually
ServerStatusTypeNode serverStatusNode = serverNode.getServerStatusNode();
BuildInfo buildInfo = serverStatusNode.getBuildInfo();
DateTime startTime = serverStatusNode.getStartTime();
DateTime currentTime = serverStatusNode.getCurrentTime();
ServerState state = serverStatusNode.getState();
logger.info("ServerStatus.BuildInfo={}", buildInfo);
logger.info("ServerStatus.StartTime={}", startTime);
logger.info("ServerStatus.CurrentTime={}", currentTime);
logger.info("ServerStatus.State={}", state);
future.complete(client);
}
use of org.eclipse.milo.opcua.stack.core.types.builtin.DateTime in project milo by eclipse.
the class JsonStructureCodec method opcUaToMemberTypeScalar.
@Override
protected JsonElement opcUaToMemberTypeScalar(String name, Object value, String typeName) {
if (value == null) {
return JsonNull.INSTANCE;
} else if (value instanceof Number) {
if (value instanceof UByte) {
return new JsonPrimitive(((UByte) value).shortValue());
} else if (value instanceof UShort) {
return new JsonPrimitive(((UShort) value).intValue());
} else if (value instanceof UInteger) {
return new JsonPrimitive(((UInteger) value).longValue());
} else if (value instanceof ULong) {
return new JsonPrimitive(((ULong) value).toBigInteger());
} else {
return new JsonPrimitive((Number) value);
}
} else if (value instanceof Boolean) {
return new JsonPrimitive((Boolean) value);
} else if (value instanceof String) {
return new JsonPrimitive((String) value);
} else if (value instanceof Character) {
return new JsonPrimitive((Character) value);
} else if (value instanceof JsonElement) {
return (JsonElement) value;
} else if (value instanceof DateTime) {
return new JsonPrimitive(((DateTime) value).getUtcTime());
} else if (value instanceof UUID) {
return new JsonPrimitive(value.toString());
} else if (value instanceof LocalizedText) {
return gson.toJsonTree(value);
} else if (value instanceof QualifiedName) {
return gson.toJsonTree(value);
} else if (value instanceof ByteString) {
ByteString byteString = (ByteString) value;
byte[] bs = byteString.bytesOrEmpty();
JsonArray array = new JsonArray();
for (Byte b : bs) {
array.add(new JsonPrimitive(b));
}
return array;
} else if (value instanceof XmlElement) {
String fragment = ((XmlElement) value).getFragment();
return fragment != null ? new JsonPrimitive(fragment) : JsonNull.INSTANCE;
} else if (value instanceof NodeId) {
String nodeId = ((NodeId) value).toParseableString();
return new JsonPrimitive(nodeId);
} else if (value instanceof ExpandedNodeId) {
String xNodeId = ((ExpandedNodeId) value).toParseableString();
return new JsonPrimitive(xNodeId);
} else if (value instanceof StatusCode) {
long code = ((StatusCode) value).getValue();
return new JsonPrimitive(code);
} else {
throw new RuntimeException("could not create JsonElement for value: " + value);
}
}
use of org.eclipse.milo.opcua.stack.core.types.builtin.DateTime in project milo by eclipse.
the class AttributeWriter method writeAttribute.
public static void writeAttribute(AttributeContext context, UaServerNode node, AttributeId attributeId, DataValue value, @Nullable String indexRange) throws UaException {
AttributeContext internalContext = new AttributeContext(context.getServer());
NodeClass nodeClass = node.getNodeClass();
if (attributeId == AttributeId.Value && nodeClass == NodeClass.Variable) {
Set<AccessLevel> accessLevels = getAccessLevels(node, internalContext);
if (!accessLevels.contains(AccessLevel.CurrentWrite)) {
throw new UaException(StatusCodes.Bad_NotWritable);
}
Set<AccessLevel> userAccessLevels = getUserAccessLevels(node, context);
if (!userAccessLevels.contains(AccessLevel.CurrentWrite)) {
throw new UaException(StatusCodes.Bad_UserAccessDenied);
}
} else {
WriteMask writeMask = writeMaskForAttribute(attributeId);
Set<WriteMask> writeMasks = getWriteMasks(node, internalContext);
if (!writeMasks.contains(writeMask)) {
throw new UaException(StatusCodes.Bad_NotWritable);
}
Set<WriteMask> userWriteMasks = getUserWriteMasks(node, context);
if (!userWriteMasks.contains(writeMask)) {
throw new UaException(StatusCodes.Bad_UserAccessDenied);
}
}
Variant updateVariant = value.getValue();
if (indexRange != null) {
NumericRange range = NumericRange.parse(indexRange);
DataValue current = node.getAttribute(internalContext, attributeId);
Variant currentVariant = current.getValue();
Object valueAtRange = NumericRange.writeToValueAtRange(currentVariant, updateVariant, range);
updateVariant = new Variant(valueAtRange);
}
DateTime sourceTime = value.getSourceTime();
DateTime serverTime = value.getServerTime();
value = new DataValue(updateVariant, value.getStatusCode(), (sourceTime == null || sourceTime.isNull()) ? DateTime.now() : sourceTime, (serverTime == null || serverTime.isNull()) ? DateTime.now() : serverTime);
if (attributeId == AttributeId.Value) {
NodeId dataType = extract(node.getAttribute(internalContext, AttributeId.DataType));
if (dataType != null) {
value = validateDataType(context.getServer(), dataType, value);
}
Integer valueRank = extract(node.getAttribute(internalContext, AttributeId.ValueRank));
if (valueRank == null)
valueRank = 0;
if (valueRank > 0) {
UInteger[] arrayDimensions = extract(node.getAttribute(context, AttributeId.ArrayDimensions));
validateArrayType(valueRank, arrayDimensions, value);
}
}
node.setAttribute(context, attributeId, value);
}
use of org.eclipse.milo.opcua.stack.core.types.builtin.DateTime in project milo by eclipse.
the class SessionDiagnosticsVariable method onStartup.
@Override
protected void onStartup() {
ServerDiagnosticsTypeNode diagnosticsNode = (ServerDiagnosticsTypeNode) server.getAddressSpaceManager().getManagedNode(Identifiers.Server_ServerDiagnostics).orElseThrow(() -> new NoSuchElementException("NodeId: " + Identifiers.Server_ServerDiagnostics));
diagnosticsEnabled.set(diagnosticsNode.getEnabledFlag());
attributeObserver = (node, attributeId, value) -> {
if (attributeId == AttributeId.Value) {
DataValue dataValue = (DataValue) value;
Object o = dataValue.getValue().getValue();
if (o instanceof Boolean) {
diagnosticsEnabled.set((Boolean) o);
}
}
};
diagnosticsNode.getEnabledFlagNode().addAttributeObserver(attributeObserver);
node.getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject xo = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getSessionDiagnosticsDataType());
return new DataValue(new Variant(xo));
}));
node.getSessionIdNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
NodeId value = session.getSessionDiagnostics().getSessionId();
return new DataValue(new Variant(value));
}));
node.getSessionNameNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
String value = session.getSessionDiagnostics().getSessionName();
return new DataValue(new Variant(value));
}));
node.getClientDescriptionNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getClientDescription());
return new DataValue(new Variant(value));
}));
node.getServerUriNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
String value = session.getSessionDiagnostics().getServerUri();
return new DataValue(new Variant(value));
}));
node.getEndpointUrlNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
String value = session.getSessionDiagnostics().getEndpointUrl();
return new DataValue(new Variant(value));
}));
node.getLocaleIdsNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
String[] value = session.getSessionDiagnostics().getLocaleIds();
return new DataValue(new Variant(value));
}));
node.getActualSessionTimeoutNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
Double value = session.getSessionDiagnostics().getActualSessionTimeout();
return new DataValue(new Variant(value));
}));
node.getMaxResponseMessageSizeNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
UInteger value = session.getSessionDiagnostics().getMaxResponseMessageSize();
return new DataValue(new Variant(value));
}));
node.getClientConnectionTimeNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
DateTime value = session.getSessionDiagnostics().getClientConnectionTime();
return new DataValue(new Variant(value));
}));
node.getClientLastContactTimeNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
DateTime value = session.getSessionDiagnostics().getClientLastContactTime();
return new DataValue(new Variant(value));
}));
node.getCurrentSubscriptionsCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
UInteger value = session.getSessionDiagnostics().getCurrentSubscriptionsCount();
return new DataValue(new Variant(value));
}));
node.getCurrentMonitoredItemsCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
UInteger value = session.getSessionDiagnostics().getCurrentMonitoredItemsCount();
return new DataValue(new Variant(value));
}));
node.getCurrentPublishRequestsInQueueNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
UInteger value = session.getSessionDiagnostics().getCurrentPublishRequestsInQueue();
return new DataValue(new Variant(value));
}));
node.getTotalRequestCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getTotalRequestCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getUnauthorizedRequestCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
UInteger value = uint(session.getSessionDiagnostics().getUnauthorizedRequestCount().longValue());
return new DataValue(new Variant(value));
}));
node.getReadCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getReadCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getHistoryReadCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getHistoryReadCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getWriteCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getWriteCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getHistoryUpdateCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getHistoryUpdateCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getCallCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getCallCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getCreateMonitoredItemsCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getCreateMonitoredItemsCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getModifyMonitoredItemsCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getModifyMonitoredItemsCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getSetMonitoringModeCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getSetMonitoringModeCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getSetTriggeringCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getSetTriggeringCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getDeleteMonitoredItemsCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getDeleteMonitoredItemsCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getCreateSubscriptionCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getCreateSubscriptionCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getModifySubscriptionCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getModifySubscriptionCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getSetPublishingModeCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getSetPublishingModeCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getPublishCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getPublishCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getRepublishCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getRepublishCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getTransferSubscriptionsCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getTransferSubscriptionsCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getDeleteSubscriptionsCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getDeleteSubscriptionsCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getAddNodesCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getAddNodesCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getAddReferencesCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getAddReferencesCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getDeleteNodesCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getDeleteNodesCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getDeleteReferencesCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getDeleteReferencesCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getBrowseCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getBrowseCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getBrowseNextCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getBrowseNextCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getTranslateBrowsePathsToNodeIdsCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getTranslateBrowsePathsToNodeIdsCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getQueryFirstCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getQueryFirstCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getQueryNextCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getQueryNextCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getRegisterNodesCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getRegisterNodesCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
node.getUnregisterNodesCountNode().getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
ExtensionObject value = ExtensionObject.encode(server.getSerializationContext(), session.getSessionDiagnostics().getUnregisterNodesCount().getServiceCounter());
return new DataValue(new Variant(value));
}));
}
use of org.eclipse.milo.opcua.stack.core.types.builtin.DateTime in project milo by eclipse.
the class OpcUaSubscriptionManager method onPublishComplete.
private void onPublishComplete(PublishResponse response, AtomicLong pendingCount) {
logger.debug("onPublishComplete() response for subscriptionId={}", response.getSubscriptionId());
UInteger subscriptionId = response.getSubscriptionId();
OpcUaSubscription subscription = subscriptions.get(subscriptionId);
if (subscription == null) {
WatchdogTimer watchdogTimer = watchdogTimers.remove(subscriptionId);
if (watchdogTimer != null)
watchdogTimer.cancel();
pendingCount.getAndUpdate(p -> (p > 0) ? p - 1 : 0);
maybeSendPublishRequests();
return;
}
WatchdogTimer watchdogTimer = watchdogTimers.get(subscriptionId);
if (watchdogTimer != null)
watchdogTimer.kick();
NotificationMessage notificationMessage = response.getNotificationMessage();
long sequenceNumber = notificationMessage.getSequenceNumber().longValue();
long expectedSequenceNumber = subscription.getLastSequenceNumber() + 1;
if (sequenceNumber > expectedSequenceNumber) {
logger.warn("[id={}] expected sequence={}, received sequence={}. Calling Republish service...", subscriptionId, expectedSequenceNumber, sequenceNumber);
processingQueue.pause();
processingQueue.submitToHead(() -> onPublishComplete(response, pendingCount));
republish(subscriptionId, expectedSequenceNumber, sequenceNumber).whenComplete((dataLost, ex) -> {
if (ex != null) {
logger.debug("Republish failed: {}", ex.getMessage(), ex);
subscriptionListeners.forEach(l -> l.onNotificationDataLost(subscription));
subscription.getNotificationListeners().forEach(l -> l.onNotificationDataLost(subscription));
} else {
// Republish succeeded, possibly with some data loss, resume processing.
if (dataLost) {
subscriptionListeners.forEach(l -> l.onNotificationDataLost(subscription));
subscription.getNotificationListeners().forEach(l -> l.onNotificationDataLost(subscription));
}
}
subscription.setLastSequenceNumber(sequenceNumber - 1);
processingQueue.resume();
});
return;
}
if (notificationMessage.getNotificationData() != null && notificationMessage.getNotificationData().length > 0) {
// Set last sequence number only if this isn't a keep-alive
subscription.setLastSequenceNumber(sequenceNumber);
}
UInteger[] availableSequenceNumbers = response.getAvailableSequenceNumbers();
synchronized (subscription.availableAcknowledgements) {
subscription.availableAcknowledgements.clear();
if (availableSequenceNumbers != null && availableSequenceNumbers.length > 0) {
Collections.addAll(subscription.availableAcknowledgements, availableSequenceNumbers);
}
}
if (logger.isDebugEnabled() && availableSequenceNumbers != null) {
String[] seqStrings = Arrays.stream(availableSequenceNumbers).map(sequence -> String.format("id=%s/seq=%s", subscriptionId, sequence)).toArray(String[]::new);
logger.debug("[id={}] PublishResponse sequence={}, available sequences={}", subscriptionId, sequenceNumber, Arrays.toString(seqStrings));
}
DateTime publishTime = notificationMessage.getPublishTime();
logger.debug("onPublishComplete(), subscriptionId={}, sequenceNumber={}, publishTime={}", subscriptionId, notificationMessage.getSequenceNumber(), publishTime);
deliverNotificationMessage(subscription, notificationMessage).thenRunAsync(() -> {
pendingCount.getAndUpdate(p -> (p > 0) ? p - 1 : 0);
maybeSendPublishRequests();
}, client.getConfig().getExecutor());
}
Aggregations