Search in sources :

Example 1 with Serializable

use of org.apache.plc4x.java.spi.utils.Serializable in project plc4x by apache.

the class DefaultPlcReadRequest method serialize.

@Override
public void serialize(WriteBuffer writeBuffer) throws SerializationException {
    writeBuffer.pushContext("PlcReadRequest");
    writeBuffer.pushContext("fields");
    for (Map.Entry<String, PlcField> fieldEntry : fields.entrySet()) {
        String fieldName = fieldEntry.getKey();
        writeBuffer.pushContext(fieldName);
        PlcField field = fieldEntry.getValue();
        if (!(field instanceof Serializable)) {
            throw new RuntimeException("Error serializing. Field doesn't implement XmlSerializable");
        }
        ((Serializable) field).serialize(writeBuffer);
        writeBuffer.popContext(fieldName);
    }
    writeBuffer.popContext("fields");
    writeBuffer.popContext("PlcReadRequest");
}
Also used : Serializable(org.apache.plc4x.java.spi.utils.Serializable) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) PlcField(org.apache.plc4x.java.api.model.PlcField) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with Serializable

use of org.apache.plc4x.java.spi.utils.Serializable in project plc4x by apache.

the class DefaultPlcSubscriptionRequest method serialize.

@Override
public void serialize(WriteBuffer writeBuffer) throws SerializationException {
    writeBuffer.pushContext("PlcSubscriptionRequest");
    writeBuffer.pushContext("fields");
    for (Map.Entry<String, PlcSubscriptionField> fieldEntry : fields.entrySet()) {
        String fieldName = fieldEntry.getKey();
        writeBuffer.pushContext(fieldName);
        PlcField field = fieldEntry.getValue();
        if (!(field instanceof Serializable)) {
            throw new RuntimeException("Error serializing. Field doesn't implement XmlSerializable");
        }
        ((Serializable) field).serialize(writeBuffer);
        writeBuffer.popContext(fieldName);
    }
    writeBuffer.popContext("fields");
    writeBuffer.popContext("PlcSubscriptionRequest");
}
Also used : PlcSubscriptionField(org.apache.plc4x.java.api.model.PlcSubscriptionField) DefaultPlcSubscriptionField(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField) Serializable(org.apache.plc4x.java.spi.utils.Serializable) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) PlcField(org.apache.plc4x.java.api.model.PlcField)

Example 3 with Serializable

use of org.apache.plc4x.java.spi.utils.Serializable in project plc4x by apache.

the class WriteBufferBoxBased method writeVirtual.

@Override
public void writeVirtual(String logicalName, Object value, WithWriterArgs... writerArgs) throws SerializationException {
    String additionalStringRepresentation = extractAdditionalStringRepresentation(writerArgs).map(s -> " " + s).orElse("");
    AsciiBox virtualBox;
    if (value instanceof String) {
        virtualBox = asciiBoxWriterLight.boxString(logicalName, String.format("%s%s", value, additionalStringRepresentation), 0);
    } else if (value instanceof Float) {
        Float number = (Float) value;
        virtualBox = asciiBoxWriterLight.boxString(logicalName, String.format("%f%s", number, additionalStringRepresentation), 0);
    } else if (value instanceof Double) {
        Double number = (Double) value;
        virtualBox = asciiBoxWriterLight.boxString(logicalName, String.format("%f%s", number, additionalStringRepresentation), 0);
    } else if (value instanceof Number) {
        // TODO: adjust rendering
        Number number = (Number) value;
        virtualBox = asciiBoxWriterLight.boxString(logicalName, String.format("0x%x %d%s", number, number, additionalStringRepresentation), 0);
    } else if (value instanceof Boolean) {
        virtualBox = asciiBoxWriterLight.boxString(logicalName, String.format("b%d %b%s", (Boolean) value ? 1 : 0, value, additionalStringRepresentation), 0);
    } else if (value instanceof Enum) {
        Enum<?> enumValue = (Enum<?>) value;
        virtualBox = asciiBoxWriterLight.boxString(logicalName, String.format("%s%s", enumValue.name(), additionalStringRepresentation), 0);
    } else if (value instanceof Serializable) {
        Serializable serializable = (Serializable) value;
        try {
            WriteBufferBoxBased writeBuffer = new WriteBufferBoxBased(asciiBoxWriterLight, asciiBoxWriterLight, true, true);
            serializable.serialize(writeBuffer);
            virtualBox = asciiBoxWriterLight.boxBox(logicalName, writeBuffer.getBox(), 0);
        } catch (SerializationException e) {
            virtualBox = asciiBoxWriterLight.boxString(logicalName, e.getMessage(), 0);
        }
    } else {
        virtualBox = asciiBoxWriterLight.boxString(logicalName, "un-renderable", 0);
    }
    boxes.offerLast(Either.left(virtualBox));
}
Also used : BigDecimal(java.math.BigDecimal) Hex(org.apache.plc4x.java.spi.utils.hex.Hex) Either(io.vavr.control.Either) AsciiBoxWriter(org.apache.plc4x.java.spi.utils.ascii.AsciiBoxWriter) Serializable(org.apache.plc4x.java.spi.utils.Serializable) BigInteger(java.math.BigInteger) LinkedList(java.util.LinkedList) Deque(java.util.Deque) StringUtils(org.apache.commons.lang3.StringUtils) Collections(java.util.Collections) AsciiBox(org.apache.plc4x.java.spi.utils.ascii.AsciiBox) Serializable(org.apache.plc4x.java.spi.utils.Serializable) AsciiBox(org.apache.plc4x.java.spi.utils.ascii.AsciiBox)

Example 4 with Serializable

use of org.apache.plc4x.java.spi.utils.Serializable 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 5 with Serializable

use of org.apache.plc4x.java.spi.utils.Serializable in project plc4x by apache.

the class DefaultPlcWriteRequest method serialize.

@Override
public void serialize(WriteBuffer writeBuffer) throws SerializationException {
    writeBuffer.pushContext("PlcWriteRequest");
    writeBuffer.pushContext("fields");
    for (Map.Entry<String, FieldValueItem> fieldEntry : fields.entrySet()) {
        FieldValueItem fieldValueItem = fieldEntry.getValue();
        String fieldName = fieldEntry.getKey();
        writeBuffer.pushContext(fieldName);
        PlcField field = fieldValueItem.getField();
        if (!(field instanceof Serializable)) {
            throw new RuntimeException("Error serializing. Field doesn't implement XmlSerializable");
        }
        ((Serializable) field).serialize(writeBuffer);
        final PlcValue value = fieldValueItem.getValue();
        if (value instanceof PlcList) {
            PlcList list = (PlcList) value;
            for (PlcValue plcValue : list.getList()) {
                String plcValueString = plcValue.getString();
                writeBuffer.writeString("value", plcValueString.getBytes(StandardCharsets.UTF_8).length * 8, StandardCharsets.UTF_8.name(), plcValueString);
            }
        } else {
            String plcValueString = value.getString();
            writeBuffer.writeString("value", plcValueString.getBytes(StandardCharsets.UTF_8).length * 8, StandardCharsets.UTF_8.name(), plcValueString);
        }
        writeBuffer.popContext(fieldName);
    }
    writeBuffer.popContext("fields");
    writeBuffer.popContext("PlcWriteRequest");
}
Also used : PlcList(org.apache.plc4x.java.spi.values.PlcList) Serializable(org.apache.plc4x.java.spi.utils.Serializable) PlcValue(org.apache.plc4x.java.api.value.PlcValue) FieldValueItem(org.apache.plc4x.java.spi.messages.utils.FieldValueItem) PlcField(org.apache.plc4x.java.api.model.PlcField) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Aggregations

Serializable (org.apache.plc4x.java.spi.utils.Serializable)7 Map (java.util.Map)4 PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)4 PlcField (org.apache.plc4x.java.api.model.PlcField)3 PlcValue (org.apache.plc4x.java.api.value.PlcValue)3 LinkedHashMap (java.util.LinkedHashMap)2 Either (io.vavr.control.Either)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 Collections (java.util.Collections)1 Deque (java.util.Deque)1 LinkedList (java.util.LinkedList)1 TreeMap (java.util.TreeMap)1 StringUtils (org.apache.commons.lang3.StringUtils)1 PlcSubscriptionField (org.apache.plc4x.java.api.model.PlcSubscriptionField)1 PlcResponseCode (org.apache.plc4x.java.api.types.PlcResponseCode)1 FieldValueItem (org.apache.plc4x.java.spi.messages.utils.FieldValueItem)1 DefaultPlcSubscriptionField (org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField)1 AsciiBox (org.apache.plc4x.java.spi.utils.ascii.AsciiBox)1 AsciiBoxWriter (org.apache.plc4x.java.spi.utils.ascii.AsciiBoxWriter)1