use of org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.ULong 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.ULong in project milo by eclipse.
the class SetPositionMethod method invoke.
@Override
protected Variant[] invoke(AbstractMethodInvocationHandler.InvocationContext context, Variant[] inputValues) throws UaException {
UInteger fileHandle = (UInteger) inputValues[0].getValue();
ULong position = (ULong) inputValues[1].getValue();
invoke(context, fileHandle, position);
return new Variant[] {};
}
use of org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.ULong in project milo by eclipse.
the class OpcUaBinaryStreamDecoder method readUInt64Array.
@Override
public ULong[] readUInt64Array(String field) throws UaSerializationException {
int length = readInt32();
if (length == -1) {
return null;
} else {
checkArrayLength(length);
ULong[] values = new ULong[length];
for (int i = 0; i < length; i++) {
values[i] = readUInt64(field);
}
return values;
}
}
use of org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.ULong in project milo by eclipse.
the class GetPositionMethod method invoke.
@Override
protected Variant[] invoke(AbstractMethodInvocationHandler.InvocationContext context, Variant[] inputValues) throws UaException {
UInteger fileHandle = (UInteger) inputValues[0].getValue();
Out<ULong> position = new Out<ULong>();
invoke(context, fileHandle, position);
return new Variant[] { new Variant(position.get()) };
}
use of org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.ULong in project plc4x by apache.
the class Plc4xCommunication method getValue.
public DataValue getValue(AttributeFilterContext.GetAttributeContext ctx, String tag, String connectionString) {
PlcConnection connection = null;
try {
// Check if we just polled the connection and it failed. Wait for the backoff counter to expire before we try again.
if (failedConnectionList.containsKey(connectionString)) {
if (System.currentTimeMillis() > failedConnectionList.get(connectionString) + DEFAULT_RETRY_BACKOFF) {
failedConnectionList.remove(connectionString);
} else {
logger.debug("Waiting for back off timer - " + ((failedConnectionList.get(connectionString) + DEFAULT_RETRY_BACKOFF) - System.currentTimeMillis()) + " ms left");
return BAD_RESPONSE;
}
}
// Try to connect to PLC
try {
connection = driverManager.getConnection(connectionString);
logger.debug(connectionString + " Connected");
} catch (PlcConnectionException e) {
logger.error("Failed to connect to device, error raised - " + e);
failedConnectionList.put(connectionString, System.currentTimeMillis());
return BAD_RESPONSE;
}
if (!connection.getMetadata().canRead()) {
logger.error("This connection doesn't support reading.");
try {
connection.close();
} catch (Exception exception) {
logger.warn("Closing connection failed with error - " + exception);
}
return BAD_RESPONSE;
}
long timeout = DEFAULT_TIMEOUT;
if (monitoredList.containsKey(ctx.getNode().getNodeId())) {
timeout = (long) monitoredList.get(ctx.getNode().getNodeId()).getSamplingInterval() * 1000;
}
// Create a new read request:
// - Give the single item requested an alias name
PlcReadRequest.Builder builder = connection.readRequestBuilder();
builder.addItem("value-1", tag);
PlcReadRequest readRequest = builder.build();
PlcReadResponse response = null;
try {
response = readRequest.execute().get(timeout, TimeUnit.MICROSECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
logger.warn(e + " Occurred while reading value, using timeout of " + timeout / 1000 + "ms");
try {
connection.close();
} catch (Exception exception) {
logger.warn("Closing connection failed with error - " + exception);
}
return BAD_RESPONSE;
}
DataValue resp = BAD_RESPONSE;
for (String fieldName : response.getFieldNames()) {
if (response.getResponseCode(fieldName) == PlcResponseCode.OK) {
int numValues = response.getNumberOfValues(fieldName);
if (numValues == 1) {
if (response.getObject(fieldName) instanceof BigInteger) {
resp = new DataValue(new Variant(ulong((BigInteger) response.getObject(fieldName))), StatusCode.GOOD);
} else {
resp = new DataValue(new Variant(response.getObject(fieldName)), StatusCode.GOOD);
}
} else {
Object array = null;
if (response.getObject(fieldName, 0) instanceof BigInteger) {
array = Array.newInstance(ULong.class, numValues);
} else {
array = Array.newInstance(response.getObject(fieldName, 0).getClass(), numValues);
}
for (int i = 0; i < numValues; i++) {
if (response.getObject(fieldName, i) instanceof BigInteger) {
Array.set(array, i, ulong((BigInteger) response.getObject(fieldName, i)));
} else {
Array.set(array, i, response.getObject(fieldName, i));
}
}
resp = new DataValue(new Variant(array), StatusCode.GOOD);
}
}
}
try {
connection.close();
} catch (Exception e) {
failedConnectionList.put(connectionString, System.currentTimeMillis());
logger.warn("Closing connection failed with error " + e);
}
return resp;
} catch (Exception e) {
logger.warn("General error reading value " + e.getStackTrace()[0].toString());
if (connection != null) {
try {
connection.close();
} catch (Exception ex) {
// Do Nothing
}
}
return BAD_RESPONSE;
}
}
Aggregations