Skip to content

Commit

Permalink
Add support for b64flt, b64byt and b64srt
Browse files Browse the repository at this point in the history
Addresses discussion in Issue ornl-epics#21 to support different array types.
  • Loading branch information
rjwills28 committed May 9, 2024
1 parent d20de3b commit ca4970b
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 3 deletions.
101 changes: 98 additions & 3 deletions src/main/java/pvws/ws/Vtype2Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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<N; ++i)
fltbuf.put(data.getFloat(i));
g.writeStringField("b64flt", Base64.getEncoder().encodeToString(buf.array()));
}


private static void handleShorts(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 * Short.BYTES);
buf.order(ByteOrder.LITTLE_ENDIAN);
final ShortBuffer srtbuf = buf.asShortBuffer();
for (int i=0; i<N; ++i)
srtbuf.put(data.getShort(i));
g.writeStringField("b64srt", Base64.getEncoder().encodeToString(buf.array()));
}


private static void handleBytes(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());
}

// Convert into Base64 int64 array
final ListNumber data = value.getData();
final int N = data.size();
final ByteBuffer buf = ByteBuffer.allocate(N * Byte.BYTES);
buf.order(ByteOrder.LITTLE_ENDIAN);
for (int i=0; i<N; ++i)
buf.put(data.getByte(i));
g.writeStringField("b64byt", Base64.getEncoder().encodeToString(buf.array()));
}


private static void handleInts(final JsonGenerator g, final VNumberArray value, final VType last_value) throws Exception
{
final AlarmSeverity severity = value.getAlarm().getSeverity();
Expand Down
24 changes: 24 additions & 0 deletions src/main/webapp/js/pvws.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ class PVWS
// console.log(JSON.stringify(jm.value));
delete jm.b64dbl;
}
else if (jm.b64flt !== undefined)
{
let bytes = toByteArray(jm.b64flt);
jm.value = new Float32Array(bytes.buffer);
// Convert to plain array
jm.value = Array.prototype.slice.call(jm.value);
delete jm.b64flt;
}
else if (jm.b64srt !== undefined)
{
let bytes = toByteArray(jm.b64srt);
jm.value = new Int16Array(bytes.buffer);
// Convert to plain array
jm.value = Array.prototype.slice.call(jm.value);
delete jm.b64srt;
}
else if (jm.b64int !== undefined)
{
let bytes = toByteArray(jm.b64int);
Expand All @@ -111,6 +127,14 @@ class PVWS
jm.value = Array.prototype.slice.call(jm.value);
delete jm.b64int;
}
else if (jm.b64byt !== undefined)
{
let bytes = toByteArray(jm.b64byt);
jm.value = new Uint8Array(bytes.buffer);
// Convert to plain array, if necessary
jm.value = Array.prototype.slice.call(jm.value);
delete jm.b64byt;
}

// Merge received data with last known value
let value = this.values[jm.pv];
Expand Down

0 comments on commit ca4970b

Please sign in to comment.