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");
}
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");
}
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));
}
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");
}
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");
}
Aggregations