use of org.apache.plc4x.java.api.exceptions.PlcRuntimeException in project plc4x by apache.
the class WaterTankService method connectToDevice.
protected void connectToDevice() {
try {
// Connect to the device
connection = new PlcDriverManager().getConnection(connectionString);
// Check if subscriptions are supported by this connection.
if (!connection.getMetadata().canSubscribe()) {
throw new PlcRuntimeException("This driver doesn't support subscribing");
}
// Prepare a subscription request.
final PlcSubscriptionRequest subscriptionRequest = connection.subscriptionRequestBuilder().addChangeOfStateField(WATER_LEVEL, addressStringWaterLevel).build();
// Execute the request.
PlcSubscriptionResponse syncResponse = subscriptionRequest.execute().get();
// Attach handlers for the incoming data.
for (String subscriptionName : syncResponse.getFieldNames()) {
final PlcSubscriptionHandle subscriptionHandle = syncResponse.getSubscriptionHandle(subscriptionName);
subscriptionHandle.register(new WaterLevelHandler());
}
} catch (PlcConnectionException e) {
throw new PlcRuntimeException("Error connecting to device", e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new PlcRuntimeException("Error subscribing for data", e);
} catch (ExecutionException e) {
throw new PlcRuntimeException("Error subscribing for data", e);
}
}
use of org.apache.plc4x.java.api.exceptions.PlcRuntimeException in project plc4x by apache.
the class ReadBufferJsonBased method readUnsignedLong.
@Override
public long readUnsignedLong(String logicalName, int bitLength, WithReaderArgs... readerArgs) throws ParseException {
logicalName = sanitizeLogicalName(logicalName);
move(bitLength);
Map element = getElement(logicalName);
validateAttr(logicalName, element, rwUintKey, bitLength);
Integer value = (Integer) element.get(logicalName);
if (value == null) {
throw new PlcRuntimeException(String.format(REQUIRED_ELEMENT_NOT_FOUND, logicalName, stack.peek()));
}
return Long.valueOf(value);
}
use of org.apache.plc4x.java.api.exceptions.PlcRuntimeException in project plc4x by apache.
the class ReadBufferXmlBased method validateAttr.
private void validateAttr(String logicalName, Iterator<Attribute> attr, String dataType, int bitLength) {
boolean dataTypeValidated = false;
boolean bitLengthValidate = false;
while (attr.hasNext()) {
Attribute attribute = attr.next();
if (attribute.getName().getLocalPart().equals(rwDataTypeKey)) {
if (!attribute.getValue().equals(dataType)) {
throw new PlcRuntimeException(String.format("%s: Unexpected dataType '%s'. Want '%s'", logicalName, attribute.getValue(), dataType));
}
dataTypeValidated = true;
} else if (attribute.getName().getLocalPart().equals(rwBitLengthKey)) {
if (!attribute.getValue().equals(Integer.toString(bitLength))) {
throw new PlcRuntimeException(String.format("%s: Unexpected bitLength '%s'. Want '%d'", logicalName, attribute.getValue(), bitLength));
}
bitLengthValidate = true;
}
}
if (!dataTypeValidated) {
throw new PlcRuntimeException(String.format("%s: required attribute '%s' missing", logicalName, rwDataTypeKey));
}
if (!bitLengthValidate) {
throw new PlcRuntimeException(String.format("%s: required attribute '%s' missing", logicalName, rwBitLengthKey));
}
}
use of org.apache.plc4x.java.api.exceptions.PlcRuntimeException in project plc4x by apache.
the class WriteBufferJsonBased method popContext.
@Override
public void popContext(String logicalName, WithWriterArgs... writerArgs) {
try {
if (isToBeRenderedAsList(writerArgs)) {
generator.writeEndArray();
} else {
generator.writeEndObject();
if (generator.getOutputContext().getParent().inArray()) {
generator.writeEndObject();
}
}
depth--;
if (depth == 0) {
generator.writeEndObject();
generator.flush();
}
} catch (IOException e) {
throw new PlcRuntimeException(e);
}
}
use of org.apache.plc4x.java.api.exceptions.PlcRuntimeException in project plc4x by apache.
the class WriteBufferXmlBased method popContext.
@Override
public void popContext(String logicalName, WithWriterArgs... writerArgs) {
try {
depth--;
indent();
EndElement endElement = xmlEventFactory.createEndElement("", "", logicalName);
xmlEventWriter.add(endElement);
if (depth != 0) {
// We don't want an extra newline at the end so we write only if we are not at the end
newLine();
}
} catch (XMLStreamException e) {
throw new PlcRuntimeException(e);
}
String context = stack.pop();
if (!context.equals(logicalName)) {
throw new PlcRuntimeException("Unexpected pop context '" + context + '\'' + ". Expected '" + logicalName + '\'');
}
if (stack.isEmpty()) {
try {
xmlEventWriter.close();
} catch (XMLStreamException e) {
throw new PlcRuntimeException(e);
}
}
}
Aggregations