Search in sources :

Example 11 with PlcRuntimeException

use of org.apache.plc4x.java.api.exceptions.PlcRuntimeException in project plc4x by apache.

the class WriteBufferXmlBased method pushContext.

@Override
public void pushContext(String logicalName, WithWriterArgs... writerArgs) {
    try {
        indent();
        depth++;
        StartElement startElement = xmlEventFactory.createStartElement("", "", logicalName);
        xmlEventWriter.add(startElement);
        if (isToBeRenderedAsList(writerArgs)) {
            Attribute isListAttribute = xmlEventFactory.createAttribute(rwIsListKey, "true");
            xmlEventWriter.add(isListAttribute);
        }
        newLine();
    } catch (XMLStreamException e) {
        throw new PlcRuntimeException(e);
    }
    stack.push(logicalName);
}
Also used : StartElement(javax.xml.stream.events.StartElement) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) XMLStreamException(javax.xml.stream.XMLStreamException) Attribute(javax.xml.stream.events.Attribute)

Example 12 with PlcRuntimeException

use of org.apache.plc4x.java.api.exceptions.PlcRuntimeException in project plc4x by apache.

the class PlcList method serialize.

@Override
public void serialize(WriteBuffer writeBuffer) throws SerializationException {
    writeBuffer.pushContext("PlcList");
    for (PlcValue listItem : listItems) {
        if (!(listItem instanceof Serializable)) {
            throw new PlcRuntimeException("Error serializing. List item doesn't implement XmlSerializable");
        }
        ((Serializable) listItem).serialize(writeBuffer);
    }
    writeBuffer.popContext("PlcList");
}
Also used : PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) PlcValue(org.apache.plc4x.java.api.value.PlcValue) Serializable(org.apache.plc4x.java.spi.utils.Serializable)

Example 13 with PlcRuntimeException

use of org.apache.plc4x.java.api.exceptions.PlcRuntimeException in project plc4x by apache.

the class ReadBufferJsonBased method readInt.

@Override
public int readInt(String logicalName, int bitLength, WithReaderArgs... readerArgs) throws ParseException {
    logicalName = sanitizeLogicalName(logicalName);
    move(bitLength);
    Map element = getElement(logicalName);
    validateAttr(logicalName, element, rwIntKey, bitLength);
    Integer value = (Integer) element.get(logicalName);
    if (value == null) {
        throw new PlcRuntimeException(String.format(REQUIRED_ELEMENT_NOT_FOUND, logicalName, stack.peek()));
    }
    return value;
}
Also used : BigInteger(java.math.BigInteger) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException)

Example 14 with PlcRuntimeException

use of org.apache.plc4x.java.api.exceptions.PlcRuntimeException in project plc4x by apache.

the class ReadBufferJsonBased method getElement.

private Map getElement(String logicalName) {
    logicalName = sanitizeLogicalName(logicalName);
    Object peek = stack.peek();
    Map element;
    if (peek instanceof List) {
        List elementList = (List) stack.pop();
        element = (Map) elementList.get(0);
        if (elementList.size() < 2) {
            stack.push(Collections.emptyList());
        } else {
            elementList.remove(0);
            stack.push(elementList);
        }
        return element;
    } else if (peek instanceof Map) {
        return (Map) peek;
    } else {
        throw new PlcRuntimeException(String.format("Invalid state at %s with %s", logicalName, peek));
    }
}
Also used : PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException)

Example 15 with PlcRuntimeException

use of org.apache.plc4x.java.api.exceptions.PlcRuntimeException in project plc4x by apache.

the class ReadBufferJsonBased method readShort.

@Override
public short readShort(String logicalName, int bitLength, WithReaderArgs... readerArgs) throws ParseException {
    logicalName = sanitizeLogicalName(logicalName);
    move(bitLength);
    Map element = getElement(logicalName);
    validateAttr(logicalName, element, rwIntKey, bitLength);
    Integer value = (Integer) element.get(logicalName);
    if (value == null) {
        throw new PlcRuntimeException(String.format(REQUIRED_ELEMENT_NOT_FOUND, logicalName, stack.peek()));
    }
    return value.shortValue();
}
Also used : BigInteger(java.math.BigInteger) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException)

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