From ca4970bb9edbb7f3136b3378b8f50bce48ef71fb Mon Sep 17 00:00:00 2001 From: Rebecca Williams Date: Thu, 9 May 2024 16:54:26 +0100 Subject: [PATCH] Add support for b64flt, b64byt and b64srt Addresses discussion in Issue #21 to support different array types. --- src/main/java/pvws/ws/Vtype2Json.java | 101 +++++++++++++++++++++++++- src/main/webapp/js/pvws.js | 24 ++++++ 2 files changed, 122 insertions(+), 3 deletions(-) diff --git a/src/main/java/pvws/ws/Vtype2Json.java b/src/main/java/pvws/ws/Vtype2Json.java index de6ad98..48fcef2 100644 --- a/src/main/java/pvws/ws/Vtype2Json.java +++ b/src/main/java/pvws/ws/Vtype2Json.java @@ -12,6 +12,8 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.DoubleBuffer; +import java.nio.FloatBuffer; +import java.nio.ShortBuffer; import java.nio.IntBuffer; import java.nio.charset.Charset; import java.text.DecimalFormat; @@ -32,6 +34,7 @@ import org.epics.vtype.VFloatArray; import org.epics.vtype.VNumber; import org.epics.vtype.VNumberArray; +import org.epics.vtype.VShortArray; import org.epics.vtype.VString; import org.epics.vtype.VType; @@ -68,15 +71,17 @@ else if (value instanceof VString) else if (value instanceof VEnum) handleEnum(g, (VEnum) value, last_value); else if (value instanceof VByteArray) - handleLongString(g, (VByteArray) value); + handleBytes(g, (VNumberArray) value, last_value); // Serialize double and float arrays as b64dbl else if (value instanceof VDoubleArray) handleDoubles(g, (VNumberArray) value, last_value); else if (value instanceof VFloatArray) - handleDoubles(g, (VNumberArray) value, last_value); + handleFloats(g, (VNumberArray) value, last_value); + else if (value instanceof VShortArray) + handleShorts(g, (VNumberArray) value, last_value); - // Serialize remaining number arrays (int, short) as b64int + // Serialize remaining number arrays (int) as b64int else if (value instanceof VNumberArray) handleInts(g, (VNumberArray) value, last_value); @@ -234,6 +239,96 @@ private static void handleDoubles(final JsonGenerator g, final VNumberArray valu } + private static void handleFloats(final JsonGenerator g, final VNumberArray value, final VType last_value) throws Exception + { + final AlarmSeverity severity = value.getAlarm().getSeverity(); + if (last_value == null) + { + // Initially, add complete metadata + g.writeStringField("vtype", VType.typeOf(value).getSimpleName()); + handleDisplay(g, value.getDisplay()); + // Initial severity + g.writeStringField("severity", severity.name()); + } + else + { + // Add severity if it changed + if ((last_value instanceof VNumber) && + ((VNumber) last_value).getAlarm().getSeverity() != severity) + g.writeStringField("severity", severity.name()); + } + + final ListNumber data = value.getData(); + final int N = data.size(); + final ByteBuffer buf = ByteBuffer.allocate(N * Float.BYTES); + buf.order(ByteOrder.LITTLE_ENDIAN); + final FloatBuffer fltbuf = buf.asFloatBuffer(); + for (int i=0; i