Search in sources :

Example 11 with ListNumber

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;
}
Also used : VNumberArray(org.diirt.vtype.VNumberArray) ListNumber(org.diirt.util.array.ListNumber) VTable(org.diirt.vtype.VTable) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ListDouble(org.diirt.util.array.ListDouble) VNumber(org.diirt.vtype.VNumber)

Example 12 with ListNumber

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);
}
Also used : ArrayPVDispatcher(org.csstudio.display.builder.runtime.pv.ArrayPVDispatcher) VNumberArray(org.diirt.vtype.VNumberArray) RuntimePV(org.csstudio.display.builder.runtime.pv.RuntimePV) ListNumber(org.diirt.util.array.ListNumber) Listener(org.csstudio.display.builder.runtime.pv.ArrayPVDispatcher.Listener) List(java.util.List) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 13 with ListNumber

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();
    }
}
Also used : VNumberArray(org.diirt.vtype.VNumberArray) ListNumber(org.diirt.util.array.ListNumber) VString(org.diirt.vtype.VString)

Example 14 with ListNumber

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 -&gt; VBoolean</li>
 *   <li>Number -&gt; corresponding VNumber</li>
 *   <li>String -&gt; VString</li>
 *   <li>number array -&gt; corresponding VNumberArray</li>
 *   <li>ListNumber -&gt; corresponding VNumberArray</li>
 *   <li>List -&gt; 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;
    }
}
Also used : ListNumber(org.diirt.util.array.ListNumber) ListNumber(org.diirt.util.array.ListNumber) ArrayList(java.util.ArrayList) List(java.util.List) ListBoolean(org.diirt.util.array.ListBoolean)

Example 15 with ListNumber

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());
    }
}
Also used : ListNumber(org.diirt.util.array.ListNumber) DataBufferByte(java.awt.image.DataBufferByte) BufferedImage(java.awt.image.BufferedImage)

Aggregations

ListNumber (org.diirt.util.array.ListNumber)29 VNumberArray (org.diirt.vtype.VNumberArray)13 List (java.util.List)10 VString (org.diirt.vtype.VString)9 ArrayList (java.util.ArrayList)6 ArrayDouble (org.diirt.util.array.ArrayDouble)5 ArrayInt (org.diirt.util.array.ArrayInt)5 ListInt (org.diirt.util.array.ListInt)5 VEnumArray (org.diirt.vtype.VEnumArray)5 VNumber (org.diirt.vtype.VNumber)4 VTable (org.diirt.vtype.VTable)4 VType (org.diirt.vtype.VType)4 Test (org.junit.Test)4 AbstractList (java.util.AbstractList)3 ListDouble (org.diirt.util.array.ListDouble)3 VEnum (org.diirt.vtype.VEnum)3 VStringArray (org.diirt.vtype.VStringArray)3 BufferedImage (java.awt.image.BufferedImage)2 Background (javafx.scene.layout.Background)2 BackgroundFill (javafx.scene.layout.BackgroundFill)2