Search in sources :

Example 6 with PlcRuntimeException

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);
    }
}
Also used : PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) PlcSubscriptionRequest(org.apache.plc4x.java.api.messages.PlcSubscriptionRequest) PlcSubscriptionResponse(org.apache.plc4x.java.api.messages.PlcSubscriptionResponse) PlcSubscriptionHandle(org.apache.plc4x.java.api.model.PlcSubscriptionHandle) PlcConnectionException(org.apache.plc4x.java.api.exceptions.PlcConnectionException) ExecutionException(java.util.concurrent.ExecutionException) PlcDriverManager(org.apache.plc4x.java.PlcDriverManager)

Example 7 with PlcRuntimeException

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);
}
Also used : BigInteger(java.math.BigInteger) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException)

Example 8 with PlcRuntimeException

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));
    }
}
Also used : PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException)

Example 9 with PlcRuntimeException

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);
    }
}
Also used : PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) IOException(java.io.IOException)

Example 10 with PlcRuntimeException

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);
        }
    }
}
Also used : PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) XMLStreamException(javax.xml.stream.XMLStreamException) EndElement(javax.xml.stream.events.EndElement)

Aggregations

PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)95 BigInteger (java.math.BigInteger)17 CompletableFuture (java.util.concurrent.CompletableFuture)14 PlcResponseCode (org.apache.plc4x.java.api.types.PlcResponseCode)14 PlcValue (org.apache.plc4x.java.api.value.PlcValue)14 ResponseItem (org.apache.plc4x.java.spi.messages.utils.ResponseItem)14 Duration (java.time.Duration)13 PlcField (org.apache.plc4x.java.api.model.PlcField)13 PlcConnectionException (org.apache.plc4x.java.api.exceptions.PlcConnectionException)10 HasConfiguration (org.apache.plc4x.java.spi.configuration.HasConfiguration)10 ParseException (org.apache.plc4x.java.spi.generation.ParseException)10 RequestTransactionManager (org.apache.plc4x.java.spi.transaction.RequestTransactionManager)10 Collections (java.util.Collections)9 DefaultPlcReadRequest (org.apache.plc4x.java.spi.messages.DefaultPlcReadRequest)9 DefaultPlcWriteRequest (org.apache.plc4x.java.spi.messages.DefaultPlcWriteRequest)9 IOException (java.io.IOException)8 org.apache.plc4x.java.modbus.readwrite (org.apache.plc4x.java.modbus.readwrite)8 DefaultPlcReadResponse (org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse)8 ModbusField (org.apache.plc4x.java.modbus.base.field.ModbusField)7 PlcList (org.apache.plc4x.java.spi.values.PlcList)7