use of org.diirt.util.array.ListNumber in project org.csstudio.display.builder by kasemir.
the class ValueUtil method getTable.
/**
* Get a table from PV
*
* <p>Ideally, the PV holds a {@link VTable},
* and the returned data is then the table's data.
*
* <p>If the PV is a scalar, a table with a single cell is returned.
* <p>If the PV is an array, a table with one column is returned.
*
* @param value Value of a PV
* @return List of rows, where each row contains either String or Number cells
*/
@SuppressWarnings("rawtypes")
public static List<List<Object>> getTable(final VType value) {
final List<List<Object>> data = new ArrayList<>();
if (value instanceof VTable) {
final VTable table = (VTable) value;
final int rows = table.getRowCount();
final int cols = table.getColumnCount();
// Extract 2D string matrix for data
for (int r = 0; r < rows; ++r) {
final List<Object> row = new ArrayList<>(cols);
for (int c = 0; c < cols; ++c) {
final Object col_data = table.getColumnData(c);
if (col_data instanceof List)
row.add(Objects.toString(((List) col_data).get(r)));
else if (col_data instanceof ListDouble)
row.add(((ListDouble) col_data).getDouble(r));
else if (col_data instanceof ListNumber)
row.add(((ListNumber) col_data).getLong(r));
else
row.add(Objects.toString(col_data));
}
data.add(row);
}
} else if (value instanceof VNumberArray) {
final ListNumber numbers = ((VNumberArray) value).getData();
final int num = numbers.size();
for (int i = 0; i < num; ++i) data.add(Arrays.asList(numbers.getDouble(i)));
} else if (value instanceof VNumber)
data.add(Arrays.asList(((VNumber) value).getValue()));
else
data.add(Arrays.asList(Objects.toString(value)));
return data;
}
use of org.diirt.util.array.ListNumber in project org.csstudio.display.builder by kasemir.
the class ArrayPVDispatcherTest method testArrayPVDispatcher.
/**
* Test double-typed array PV
*/
@Test
public void testArrayPVDispatcher() throws Exception {
// Array PV. It's elements are to be dispatched into separate PVs
final RuntimePV array_pv = PVFactory.getPV("loc://an_array(1.0, 2.0, 3, 4)");
// The per-element PVs that will be bound to the array
final AtomicReference<List<RuntimePV>> element_pvs = new AtomicReference<>();
final CountDownLatch got_element_pvs = new CountDownLatch(1);
// Listener to the ArrayPVDispatcher
final Listener dispatch_listener = new Listener() {
@Override
public void arrayChanged(final List<RuntimePV> pvs) {
System.out.println("Per-element PVs: ");
element_pvs.set(pvs);
dump(pvs);
got_element_pvs.countDown();
}
};
final ArrayPVDispatcher dispatcher = new ArrayPVDispatcher(array_pv, "elementA247FE_", dispatch_listener);
// Await initial set of per-element PVs
got_element_pvs.await();
assertThat(VTypeUtil.getValueNumber(element_pvs.get().get(0).read()).doubleValue(), equalTo(1.0));
assertThat(VTypeUtil.getValueNumber(element_pvs.get().get(1).read()).doubleValue(), equalTo(2.0));
assertThat(VTypeUtil.getValueNumber(element_pvs.get().get(2).read()).doubleValue(), equalTo(3.0));
assertThat(VTypeUtil.getValueNumber(element_pvs.get().get(3).read()).doubleValue(), equalTo(4.0));
// Change array -> Observe update of per-element PV
System.out.println("Updating array");
array_pv.write(new double[] { 1.0, 22.5, 3, 4 });
dump(element_pvs.get());
// On one hand, this changed only one array element.
// On the other hand, it's a new array value with a new time stamp.
// Unclear if the array dispatcher should detect this and only update
// the per-element PVs that really have a new value, or all.
// Currently it updates all, but the test is satisfied with just one update:
assertThat(VTypeUtil.getValueNumber(element_pvs.get().get(1).read()).doubleValue(), equalTo(22.5));
// Change per-element PV -> Observe update of array
System.out.println("Updating per-element PV for element [2]");
element_pvs.get().get(2).write(30.7);
// Test assumes a local array PV which immediately reflects the change.
// A "real" PV can have a delay between writing a new value and receiving the update.
final ListNumber array_value = ((VNumberArray) array_pv.read()).getData();
System.out.println("Array: " + array_value);
assertThat(array_value.getDouble(2), equalTo(30.7));
// Close dispatcher
dispatcher.close();
// Close the array PV
PVFactory.releasePV(array_pv);
}
use of org.diirt.util.array.ListNumber in project yamcs-studio by yamcs.
the class VTypeHelper method formatNumberArray.
private static String formatNumberArray(FormatEnum formatEnum, VNumberArray pmArray, int precision) {
ListNumber data = ((VNumberArray) pmArray).getData();
if (formatEnum == FormatEnum.STRING) {
final byte[] bytes = new byte[data.size()];
// Copy bytes until end _or_ '\0'
int len = 0;
while (len < bytes.length) {
final byte b = data.getByte(len);
if (b == 0)
break;
else
bytes[len++] = b;
}
return new String(bytes, 0, len);
} else {
if (data.size() <= 0)
// $NON-NLS-1$
return "[]";
int displayPrecision = calculatePrecision(pmArray, precision);
StringBuilder sb = new StringBuilder(data.size());
sb.append(formatScalarNumber(formatEnum, data.getDouble(0), displayPrecision));
for (int i = 1; i < data.size(); i++) {
sb.append(ARRAY_ELEMENT_SEPARATOR);
sb.append(formatScalarNumber(formatEnum, data.getDouble(i), displayPrecision));
if (i >= MAX_FORMAT_VALUE_COUNT) {
sb.append(ARRAY_ELEMENT_SEPARATOR);
// $NON-NLS-1$
sb.append("...");
sb.append(formatScalarNumber(formatEnum, data.getDouble(data.size() - 1), displayPrecision));
// $NON-NLS-1$
sb.append(" ");
// $NON-NLS-1$
sb.append("[");
sb.append(data.size());
// $NON-NLS-1$
sb.append("]");
break;
}
}
return sb.toString();
}
}
use of org.diirt.util.array.ListNumber in project yamcs-studio by yamcs.
the class ValueFactory method toVType.
/**
* Converts a standard java type to VTypes. Returns null if no conversion
* is possible.
* <p>
* Types are converted as follow:
* <ul>
* <li>Boolean -> VBoolean</li>
* <li>Number -> corresponding VNumber</li>
* <li>String -> VString</li>
* <li>number array -> corresponding VNumberArray</li>
* <li>ListNumber -> corresponding VNumberArray</li>
* <li>List -> if all elements are String, VStringArray</li>
* </ul>
*
* @param javaObject the value to wrap
* @param alarm the alarm
* @param time the time
* @param display the display
* @return the new VType value
*/
public static VType toVType(Object javaObject, Alarm alarm, Time time, Display display) {
if (javaObject instanceof Number) {
return ValueFactory.newVNumber((Number) javaObject, alarm, time, display);
} else if (javaObject instanceof String) {
return newVString((String) javaObject, alarm, time);
} else if (javaObject instanceof Boolean) {
return newVBoolean((Boolean) javaObject, alarm, time);
} else if (javaObject instanceof byte[] || javaObject instanceof short[] || javaObject instanceof int[] || javaObject instanceof long[] || javaObject instanceof float[] || javaObject instanceof double[]) {
return newVNumberArray(ListNumbers.toListNumber(javaObject), alarm, time, display);
} else if (javaObject instanceof ListNumber) {
return newVNumberArray((ListNumber) javaObject, alarm, time, display);
} else if (javaObject instanceof String[]) {
return newVStringArray(Arrays.asList((String[]) javaObject), alarm, time);
} else if (javaObject instanceof List) {
boolean matches = true;
List list = (List) javaObject;
for (Object object : list) {
if (!(object instanceof String)) {
matches = false;
}
}
if (matches) {
@SuppressWarnings("unchecked") List<String> newList = (List<String>) list;
return newVStringArray(Collections.unmodifiableList(newList), alarm, time);
} else {
return null;
}
} else {
return null;
}
}
use of org.diirt.util.array.ListNumber in project yamcs-studio by yamcs.
the class ValueUtil method toImage.
/**
* Converts a VImage to an AWT BufferedImage, so that it can be displayed. The content of the vImage buffer is
* copied, so further changes to the VImage will not modify the BufferedImage.
*
* @param vImage
* the image to be converted
* @return a new BufferedImage
*/
public static BufferedImage toImage(VImage vImage) {
if (vImage.getVImageType() == VImageType.TYPE_3BYTE_BGR) {
BufferedImage image = new BufferedImage(vImage.getWidth(), vImage.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
ListNumber data = vImage.getData();
for (int i = 0; i < data.size(); i++) {
((DataBufferByte) image.getRaster().getDataBuffer()).getData()[i] = data.getByte(i);
}
return image;
} else {
throw new UnsupportedOperationException("No support for creating a BufferedImage from Image Type: " + vImage.getVImageType());
}
}
Aggregations