Search in sources :

Example 1 with ArrayPVDispatcher

use of org.csstudio.display.builder.runtime.pv.ArrayPVDispatcher 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 2 with ArrayPVDispatcher

use of org.csstudio.display.builder.runtime.pv.ArrayPVDispatcher in project org.csstudio.display.builder by kasemir.

the class ArrayWidgetRuntime method start.

@Override
public void start() throws Exception {
    super.start();
    RuntimePV pv = getPrimaryPV().orElse(null);
    if (pv != null)
        dispatcher = new ArrayPVDispatcher(pv, pvid, assign_pv_names);
    for (final Widget child : widget.runtimeChildren().getValue()) RuntimeUtil.startRuntime(child);
    widget.runtimeChildren().addPropertyListener(children_listener);
}
Also used : ArrayPVDispatcher(org.csstudio.display.builder.runtime.pv.ArrayPVDispatcher) RuntimePV(org.csstudio.display.builder.runtime.pv.RuntimePV) ArrayWidget(org.csstudio.display.builder.model.widgets.ArrayWidget) GroupWidget(org.csstudio.display.builder.model.widgets.GroupWidget) Widget(org.csstudio.display.builder.model.Widget)

Example 3 with ArrayPVDispatcher

use of org.csstudio.display.builder.runtime.pv.ArrayPVDispatcher in project org.csstudio.display.builder by kasemir.

the class ArrayPVDispatcherTest method testScalarPVDispatcher.

/**
 * Test double-typed scalar PV
 */
@Test
public void testScalarPVDispatcher() throws Exception {
    // Not really an array..
    final RuntimePV array_pv = PVFactory.getPV("loc://no_array(3.14)");
    // 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, "element123456_", dispatch_listener);
    // Await initial set of per-element PVs
    got_element_pvs.await();
    assertThat(element_pvs.get().size(), equalTo(1));
    assertThat(VTypeUtil.getValueNumber(element_pvs.get().get(0).read()).doubleValue(), equalTo(3.14));
    // Change array -> Observe update of per-element PV
    System.out.println("Updating 'array'");
    array_pv.write(47.11);
    dump(element_pvs.get());
    assertThat(VTypeUtil.getValueNumber(element_pvs.get().get(0).read()).doubleValue(), equalTo(47.11));
    // Change per-element PV -> Observe update of 'array'
    System.out.println("Updating per-element PV for element [2]");
    element_pvs.get().get(0).write(11.47);
    final Number value = VTypeUtil.getValueNumber(array_pv.read());
    System.out.println("'Array': " + value);
    assertThat(value, equalTo(11.47));
    // Close dispatcher
    dispatcher.close();
    // Close the array PV
    PVFactory.releasePV(array_pv);
}
Also used : ArrayPVDispatcher(org.csstudio.display.builder.runtime.pv.ArrayPVDispatcher) RuntimePV(org.csstudio.display.builder.runtime.pv.RuntimePV) Listener(org.csstudio.display.builder.runtime.pv.ArrayPVDispatcher.Listener) ListNumber(org.diirt.util.array.ListNumber) List(java.util.List) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with ArrayPVDispatcher

use of org.csstudio.display.builder.runtime.pv.ArrayPVDispatcher in project org.csstudio.display.builder by kasemir.

the class ArrayPVDispatcherTest method testStringArray.

/**
 * Test double-typed array PV
 */
@Test
public void testStringArray() throws Exception {
    final RuntimePV array_pv = PVFactory.getPV("loc://an_array(\"One\", \"Two\", \"Three\")");
    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) {
            element_pvs.set(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.getValueString(element_pvs.get().get(0).read(), false), equalTo("One"));
    assertThat(VTypeUtil.getValueString(element_pvs.get().get(1).read(), false), equalTo("Two"));
    assertThat(VTypeUtil.getValueString(element_pvs.get().get(2).read(), false), equalTo("Three"));
    // Change array -> Observe update of per-element PV
    System.out.println("Updating array");
    array_pv.write(new String[] { "Uno", "Due", "Another", "Vier" });
    dump(element_pvs.get());
    assertThat(VTypeUtil.getValueString(element_pvs.get().get(0).read(), false), equalTo("Uno"));
    assertThat(VTypeUtil.getValueString(element_pvs.get().get(3).read(), false), equalTo("Vier"));
    // Change per-element PV -> Observe update of array
    System.out.println("Updating per-element PV for element [2]");
    element_pvs.get().get(2).write("Hello");
    final List<String> array_value = ((VStringArray) array_pv.read()).getData();
    System.out.println("Array: " + array_value);
    assertThat(array_value.get(2), equalTo("Hello"));
    // Close dispatcher
    dispatcher.close();
    // Close the array PV
    PVFactory.releasePV(array_pv);
}
Also used : ArrayPVDispatcher(org.csstudio.display.builder.runtime.pv.ArrayPVDispatcher) RuntimePV(org.csstudio.display.builder.runtime.pv.RuntimePV) Listener(org.csstudio.display.builder.runtime.pv.ArrayPVDispatcher.Listener) List(java.util.List) AtomicReference(java.util.concurrent.atomic.AtomicReference) VStringArray(org.diirt.vtype.VStringArray) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

ArrayPVDispatcher (org.csstudio.display.builder.runtime.pv.ArrayPVDispatcher)4 RuntimePV (org.csstudio.display.builder.runtime.pv.RuntimePV)4 List (java.util.List)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Listener (org.csstudio.display.builder.runtime.pv.ArrayPVDispatcher.Listener)3 Test (org.junit.Test)3 ListNumber (org.diirt.util.array.ListNumber)2 Widget (org.csstudio.display.builder.model.Widget)1 ArrayWidget (org.csstudio.display.builder.model.widgets.ArrayWidget)1 GroupWidget (org.csstudio.display.builder.model.widgets.GroupWidget)1 VNumberArray (org.diirt.vtype.VNumberArray)1 VStringArray (org.diirt.vtype.VStringArray)1