use of org.csstudio.display.builder.runtime.pv.RuntimePVListener in project org.csstudio.display.builder by kasemir.
the class XYPlotWidgetRuntime method stop.
@Override
public void stop() {
// Disconnect Marker PVs and listeners
for (Map.Entry<WidgetProperty<?>, WidgetPropertyListener<?>> entry : marker_prop_listeners.entrySet()) entry.getKey().removePropertyListener(entry.getValue());
marker_prop_listeners.clear();
for (Map.Entry<RuntimePV, RuntimePVListener> entry : marker_pv_listeners.entrySet()) entry.getKey().removeListener(entry.getValue());
marker_pv_listeners.clear();
for (RuntimePV pv : marker_pvs) {
removePV(pv);
PVFactory.releasePV(pv);
}
marker_pvs.clear();
for (PVNameToValueBinding binding : bindings) binding.dispose();
super.stop();
}
use of org.csstudio.display.builder.runtime.pv.RuntimePVListener in project org.csstudio.display.builder by kasemir.
the class PVFactoryTest method testPVFactory.
@Test
public void testPVFactory() throws Exception {
final RuntimePV pv = PVFactory.getPV("loc://test(3.14)");
try {
// vtype.pv uses the base name, without initializer
assertThat(pv.getName(), equalTo("loc://test"));
final CountDownLatch updates = new CountDownLatch(1);
final AtomicReference<Number> number = new AtomicReference<>();
RuntimePVListener listener = new RuntimePVListener() {
@Override
public void valueChanged(RuntimePV pv, VType value) {
System.out.println(pv.getName() + " = " + value);
number.set(VTypeUtil.getValueNumber(value));
updates.countDown();
}
};
pv.addListener(listener);
updates.await();
assertThat(number.get(), equalTo(3.14));
} finally {
PVFactory.releasePV(pv);
}
}
use of org.csstudio.display.builder.runtime.pv.RuntimePVListener in project org.csstudio.display.builder by kasemir.
the class PVUtil method createPV.
/**
* Create a PV.
*
* <p>Ideally, scripts use the PVs of widgets or the script 'input' PVs,
* where the display runtime handles the PV connection.
* In rare cases it might be necessary to create a PV in a script,
* in which case the script <b>must</b> then also release the PV.
*
* @param pv_name Mame of the PV
* @param timeout Connection timeout in milliseconds
* @return PV
* @throws Exception on error
* @see #releasePV(RuntimePV)
*/
public static RuntimePV createPV(final String pv_name, final int timeout_ms) throws Exception {
final CountDownLatch connected = new CountDownLatch(1);
final RuntimePVListener await_connection = new RuntimePVListener() {
@Override
public void valueChanged(final RuntimePV pv, final VType value) {
if (value != null)
connected.countDown();
}
};
final RuntimePV pv = PVFactory.getPV(pv_name);
pv.addListener(await_connection);
try {
if (!connected.await(timeout_ms, TimeUnit.MILLISECONDS)) {
PVFactory.releasePV(pv);
throw new Exception("Failed to connect to '" + pv_name + "' within " + timeout_ms + " ms");
}
} finally {
pv.removeListener(await_connection);
}
return pv;
}
use of org.csstudio.display.builder.runtime.pv.RuntimePVListener in project org.csstudio.display.builder by kasemir.
the class ImageWidgetRuntime method bindROI.
/**
* Bind an ROI PV to an ROI value
* @param name_prop Property for the PV name
* @param value_prop Property for the value
*/
private void bindROI(final WidgetProperty<String> name_prop, final WidgetProperty<Double> value_prop) {
final String pv_name = name_prop.getValue();
if (pv_name.isEmpty())
return;
logger.log(Level.FINER, "Connecting {0} to ROI PV {1}", new Object[] { widget, pv_name });
try {
final RuntimePV pv = PVFactory.getPV(pv_name);
addPV(pv);
roi_pvs.add(pv);
// Write value changes to the PV
final WidgetPropertyListener<Double> prop_listener = (prop, old, value) -> {
try {
if (value == VTypeUtil.getValueNumber(pv.read()).doubleValue())
return;
// System.out.println("Writing " + value_prop + " to PV " + pv_name);
pv.write(value);
} catch (Exception ex) {
logger.log(Level.WARNING, "Error writing ROI value to PV " + pv_name, ex);
}
};
value_prop.addPropertyListener(prop_listener);
roi_prop_listeners.put(value_prop, prop_listener);
// Write PV updates to the value
final RuntimePVListener pv_listener = new RuntimePVListener() {
@Override
public void valueChanged(final RuntimePV pv, final VType value) {
final double number = VTypeUtil.getValueNumber(value).doubleValue();
if (number == value_prop.getValue())
return;
// System.out.println("Writing from PV " + pv_name + " to " + value_prop);
value_prop.setValue(number);
}
};
pv.addListener(pv_listener);
roi_pv_listeners.put(pv, pv_listener);
} catch (Exception ex) {
logger.log(Level.WARNING, "Error connecting ROI PV " + pv_name, ex);
}
}
use of org.csstudio.display.builder.runtime.pv.RuntimePVListener in project org.csstudio.display.builder by kasemir.
the class ImageWidgetRuntime method stop.
@Override
public void stop() {
// Disconnect ROI PVs and listeners
for (Map.Entry<WidgetProperty<?>, WidgetPropertyListener<?>> entry : roi_prop_listeners.entrySet()) entry.getKey().removePropertyListener(entry.getValue());
roi_prop_listeners.clear();
for (Map.Entry<RuntimePV, RuntimePVListener> entry : roi_pv_listeners.entrySet()) entry.getKey().removeListener(entry.getValue());
roi_pv_listeners.clear();
for (RuntimePV pv : roi_pvs) {
removePV(pv);
PVFactory.releasePV(pv);
}
roi_pvs.clear();
// Disconnect cursor info PV
if (x_pv != null && y_pv != null) {
y_pv.removeListener(cursor_pv_listener);
x_pv.removeListener(cursor_pv_listener);
}
if (unbind(cursor_pv))
widget.runtimePropCursorInfo().removePropertyListener(cursor_info_listener);
boolean x_or_y = unbind(y_pv);
x_or_y |= unbind(x_pv);
if (x_or_y)
widget.runtimePropCrosshair().removePropertyListener(crosshair_listener);
y_pv = x_pv = cursor_pv = null;
super.stop();
}
Aggregations