use of org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort in project FAAAST-Service by FraunhoferIOSB.
the class OpcUaAssetConnection method parseNodeId.
private NodeId parseNodeId(String nodeId) {
Optional<String> ns = Stream.of(nodeId.split(NODE_ID_SEPARATOR)).filter(x -> x.startsWith(NS_PREFIX)).findFirst();
int namespaceIndex = 0;
if (ns.isPresent()) {
String namespace = ns.get().replace(NS_PREFIX, "");
try {
namespaceIndex = Integer.parseUnsignedInt(namespace);
} catch (NumberFormatException ex) {
UShort actualNamespaceIndex = client.getNamespaceTable().getIndex(namespace);
if (actualNamespaceIndex == null) {
throw new RuntimeException(String.format("could not resolve namespace '%s'", namespace));
}
namespaceIndex = actualNamespaceIndex.intValue();
}
} else {
System.out.println("no namespace provided for node. Using default (ns=0)");
}
return NodeId.parse(nodeId.replace(ns.get(), NS_PREFIX + namespaceIndex));
}
use of org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort in project milo by eclipse.
the class ExpandedNodeId method parse.
/**
* Parse {@code s} into an {@link ExpandedNodeId}.
*
* @param s the String to parse.
* @return an {@link ExpandedNodeId}.
* @throws UaRuntimeException if parsing fails.
*/
public static ExpandedNodeId parse(String s) {
try {
String[] parts = s.split(";");
NodeId nodeId = NodeId.parse(parts[parts.length - 1]);
UInteger serverIndex = UInteger.MIN;
UShort namespaceIndex = UShort.MIN;
String namespaceUri = null;
Object identifier = nodeId.getIdentifier();
for (String part : parts) {
String[] ss = part.split("=", 2);
if ("svr".equals(ss[0])) {
serverIndex = uint(Integer.parseInt(ss[1]));
} else if ("ns".equals(ss[0])) {
namespaceIndex = ushort(Integer.parseInt(ss[1]));
} else if ("nsu".equals(ss[0])) {
namespaceUri = ss[1];
}
}
return new ExpandedNodeId(namespaceIndex, namespaceUri, identifier, serverIndex);
} catch (Throwable t) {
throw new UaRuntimeException(StatusCodes.Bad_NodeIdInvalid, t);
}
}
use of org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort 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.unsigned.UShort in project milo by eclipse.
the class UaNode method setProperty.
public <T> void setProperty(QualifiedProperty<T> property, T value) {
UShort namespaceIndex = context.getServer().getNamespaceTable().getIndex(property.getNamespaceUri());
if (namespaceIndex == null) {
throw new IllegalArgumentException("property belongs to unregistered " + "namespace: " + property.getNamespaceUri());
}
VariableNode node = getPropertyNode(property).orElseGet(() -> {
String browseName = property.getBrowseName();
NodeId propertyNodeId = new NodeId(getNodeId().getNamespaceIndex(), String.format("%s.%s", getNodeId().getIdentifier().toString(), browseName));
PropertyTypeNode propertyNode = new PropertyTypeNode(context, propertyNodeId, new QualifiedName(namespaceIndex, browseName), LocalizedText.english(browseName), LocalizedText.NULL_VALUE, uint(0), uint(0));
NodeId dataType = property.getDataType().toNodeId(context.getNamespaceTable()).orElse(NodeId.NULL_VALUE);
propertyNode.setDataType(dataType);
propertyNode.setValueRank(property.getValueRank());
propertyNode.setArrayDimensions(property.getArrayDimensions());
propertyNode.addReference(new Reference(propertyNode.getNodeId(), Identifiers.HasTypeDefinition, Identifiers.PropertyType.expanded(), true));
addProperty(propertyNode);
context.getNodeManager().addNode(propertyNode);
return propertyNode;
});
node.setValue(new DataValue(new Variant(value)));
}
use of org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort in project milo by eclipse.
the class OpcUaXmlStreamDecoder method readDataValue.
@Override
public DataValue readDataValue(String field) throws UaSerializationException {
if (currentNode(field)) {
Node node = currentNode;
Map<String, Node> children = nodeMap(currentNode.getChildNodes());
Variant value = Variant.NULL_VALUE;
StatusCode statusCode = StatusCode.GOOD;
DateTime sourceTimestamp = null;
UShort sourcePicoseconds = null;
DateTime serverTimestamp = null;
UShort serverPicoseconds = null;
try {
Node valueNode = children.get("Value");
if (valueNode != null) {
currentNode = valueNode;
value = readVariant("Value");
}
Node statusCodeNode = children.get("StatusCode");
if (statusCodeNode != null) {
currentNode = statusCodeNode;
statusCode = readStatusCode("StatusCode");
}
Node sourceTimestampNode = children.get("SourceTimestamp");
if (sourceTimestampNode != null) {
currentNode = sourceTimestampNode;
sourceTimestamp = readDateTime("SourceTimestamp");
}
Node sourcePicosecondsNode = children.get("SourcePicoseconds");
if (sourcePicosecondsNode != null) {
currentNode = sourcePicosecondsNode;
sourcePicoseconds = readUInt16("SourcePicoseconds");
}
Node serverTimestampNode = children.get("ServerTimestamp");
if (serverTimestampNode != null) {
currentNode = serverTimestampNode;
serverTimestamp = readDateTime("ServerTimestamp");
}
Node serverPicosecondsNode = children.get("ServerPicoseconds");
if (serverPicosecondsNode != null) {
currentNode = serverPicosecondsNode;
serverPicoseconds = readUInt16("ServerPicoseconds");
}
return new DataValue(value, statusCode, sourceTimestamp, sourcePicoseconds, serverTimestamp, serverPicoseconds);
} finally {
currentNode = node.getNextSibling();
}
} else {
return new DataValue(Variant.NULL_VALUE);
}
}
Aggregations