use of org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte in project milo by eclipse.
the class AddressSpace method newViewNode.
private UaViewNode newViewNode(NodeId nodeId, List<DataValue> attributeValues) throws UaException {
DataValue nodeIdDataValue = attributeValues.get(0);
StatusCode nodeIdStatusCode = nodeIdDataValue.getStatusCode();
if (nodeIdStatusCode != null && nodeIdStatusCode.isBad()) {
throw new UaException(nodeIdStatusCode);
}
try {
NodeClass nodeClass = NodeClass.from((Integer) attributeValues.get(1).getValue().getValue());
Preconditions.checkArgument(nodeClass == NodeClass.View, "expected NodeClass.View, got NodeClass." + nodeClass);
QualifiedName browseName = (QualifiedName) attributeValues.get(2).getValue().getValue();
LocalizedText displayName = (LocalizedText) attributeValues.get(3).getValue().getValue();
LocalizedText description = getAttributeOrNull(attributeValues.get(4), LocalizedText.class);
UInteger writeMask = getAttributeOrNull(attributeValues.get(5), UInteger.class);
UInteger userWriteMask = getAttributeOrNull(attributeValues.get(6), UInteger.class);
Boolean containsNoLoops = (Boolean) attributeValues.get(7).getValue().getValue();
UByte eventNotifier = (UByte) attributeValues.get(8).getValue().getValue();
return new UaViewNode(client, nodeId, nodeClass, browseName, displayName, description, writeMask, userWriteMask, containsNoLoops, eventNotifier);
} catch (Throwable t) {
throw UaException.extract(t).orElse(new UaException(StatusCodes.Bad_UnexpectedError, t));
}
}
use of org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte 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.UByte 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;
}
}
use of org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte in project milo by eclipse.
the class AttributeWriter method validateDataType.
private static DataValue validateDataType(OpcUaServer server, NodeId dataType, DataValue value) throws UaException {
Variant variant = value.getValue();
if (variant == null)
return value;
Object o = variant.getValue();
if (o == null)
throw new UaException(StatusCodes.Bad_TypeMismatch);
Class<?> valueClass = o.getClass().isArray() ? ArrayUtil.getType(o) : o.getClass();
Class<?> expectedClass = getExpectedClass(server, dataType, valueClass);
if (expectedClass != null) {
LOGGER.debug("dataTypeId={}, valueClass={}, expectedClass={}", dataType, valueClass.getSimpleName(), expectedClass.getSimpleName());
if (!expectedClass.isAssignableFrom(valueClass)) {
// Writing a ByteString to a UByte[] is explicitly allowed by the spec.
if (o instanceof ByteString && expectedClass == UByte.class) {
ByteString byteString = (ByteString) o;
return new DataValue(new Variant(byteString.uBytes()), value.getStatusCode(), value.getSourceTime(), value.getServerTime());
} else if (expectedClass == Variant.class) {
// Allow writing anything to a Variant
return value;
} else {
throw new UaException(StatusCodes.Bad_TypeMismatch);
}
}
} else {
throw new UaException(StatusCodes.Bad_TypeMismatch);
}
return value;
}
use of org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte in project milo by eclipse.
the class ByteConversionsTest method testConversions.
@Test
public void testConversions() {
for (Object[] conversion : CONVERSIONS) {
UByte b = (UByte) conversion[0];
Object expected = conversion[1];
BuiltinDataType targetType = BuiltinDataType.fromBackingClass(expected.getClass());
assertNotNull(targetType);
assertEquals(explicitConversion(b, targetType), expected);
}
}
Aggregations