/** * Adds a listener for the read events. * * @param listener the new listener * @return this expression */ public PVConfiguration<R, W> readListener(PVReaderListener<? super R> listener) { pvReaderConfiguration.readListener(listener); return this; }
public void v3_genericReaderSwitchOnType() { final PVReader<VType> pvReader = PVManager.read(vType("channelName")) .readListener(new PVReaderListener<VType>() { @Override public void pvChanged(PVReaderEvent<VType> event) { // We can switch on the full type if (event.getPvReader().getValue() instanceof VDouble) { VDouble vDouble = (VDouble) event.getPvReader().getValue(); // Do something with a VDouble } // ... } }) .maxRate(ofMillis(100)); }
private void addPV(final String name) { final JTextField field = new JTextField(); getContentPane().add(field); final PVReader<Object> pv = PVManager.read(channel(name)) .readListener(new PVReaderListener<Object>() { @Override public void pvChanged(PVReaderEvent<Object> event) { field.setText(format.format(event.getPvReader().getValue())); } }) .maxRate(TimeDuration.ofHertz(50)); pvs.add(pv); }
public void b6_handlingErrorsOnNotification() { PVReader<Object> pvReader = PVManager.read(channel("channelName")) .readListener(new PVReaderListener<Object>() { @Override public void pvChanged(PVReaderEvent<Object> event) { // By default, read exceptions are made available // on the reader itself. // This will give you only the last exception, so if // more then one exception was generated after the last read, // some will be lost. Exception ex = event.getPvReader().lastException(); // Note that taking the exception, clears it // so next call you'll get null. if (event.getPvReader().lastException() == null) { // Always true } } }) .maxRate(ofMillis(100)); }
public void v4_readArrays() { // Reads a numeric array of any type (double, float, int, ...) // This allows to work on any array type without having to create // bindings for each. final PVReader<VNumberArray> pvReader = PVManager.read(vNumberArray("channelName")) .readListener(new PVReaderListener<VNumberArray>() { public void pvChanged(PVReaderEvent<VNumberArray> event) { if (event.isValueChanged()) { // New value VNumberArray value = event.getPvReader().getValue(); ListNumber data = value.getData(); for (int i = 0; i < data.size(); i++) { // Get the double representation of the value, // converting it if needed double iValue = data.getDouble(i); System.out.println(iValue); } } } }) .maxRate(ofMillis(100)); }
public void b8_readTimeout() { // If after 5 seconds no new value comes (i.e. pvReader.getValue() == null) // then a timeout is sent. PVManager will _still_ try to connect, // until pvReader.close() is called. // The timeout will be notified only on the first connection. final PVReader<Object> pvReader = PVManager.read(channel("channelName")) .timeout(ofSeconds(5)) .readListener(new PVReaderListener<Object>() { @Override public void pvChanged(PVReaderEvent<Object> event) { // Timeout are passed as exceptions. This allows you to // treat them as any other error conditions. Exception ex = event.getPvReader().lastException(); if (ex instanceof TimeoutException) { System.out.println("Didn't connected after 5 seconds"); } } }) .maxRate(ofMillis(100)); } }
public void v5_assemblingNumericArrayFromScalars() { List<String> channelNames = Arrays.asList("channel1", "channel2", "channel3", "channel4"); // Reads a list of different numeric channels as a single array. // The channels can be of any numeric type (double, float, int, ...) final PVReader<VNumberArray> pvReader = PVManager.read( vNumberArrayOf(latestValueOf(vNumbers(channelNames)))) .readListener(new PVReaderListener<VNumberArray>() { public void pvChanged(PVReaderEvent<VNumberArray> event) { if (event.isValueChanged()) { // Do something with the value System.out.println(event.getPvReader().getValue()); } } }) .maxRate(ofMillis(100)); }
public static void main(String[] args) throws Exception { //System.setProperty("com.cosylab.epics.caj.CAJContext.max_array_bytes", "20000000"); PVManager.setDefaultDataSource(new SimulationDataSource()); final PVReader<Object> reader = PVManager.read(channel("gaussian()")) .readListener(new PVReaderListener<Object>() { @Override public void pvChanged(PVReaderEvent<Object> event) { System.out.println("Monitor called"); } }) .maxRate(Duration.ofMillis(10)); Thread.sleep(10000); reader.close(); System.out.println("After five seconds"); Thread.sleep(10000); System.out.println("After another five seconds"); PVManager.getDefaultDataSource().close(); System.out.println("Done"); } }
private static void testVFloatArraySupport() throws Exception { PVReader<VFloatArray> pv = PVManager.read(vFloatArray(doublePV)) .readListener(new PVReaderListener<VFloatArray>() { @Override public void pvChanged(PVReaderEvent<VFloatArray> event) { PVReader<VFloatArray> pv = event.getPvReader(); System.out.println(pv.getValue().getData() + " " + Date.from(pv.getValue().getTimestamp()) + " " + pv.getValue().getAlarmSeverity()); } }) .maxRate(ofHertz(10)); Thread.sleep(10000); pv.close(); }
private static void testVStringArraySupport() throws Exception { final PVReader<VStringArray> pv = PVManager.read(vStringArray(doublePV)) .readListener(new PVReaderListener<VStringArray>() { @Override public void pvChanged(PVReaderEvent<VStringArray> event) { PVReader<VStringArray> pv = event.getPvReader(); System.out.println(pv.getValue().getData() + " " + Date.from(pv.getValue().getTimestamp()) + " " + pv.getValue().getAlarmSeverity()); } }) .maxRate(ofHertz(10)); Thread.sleep(10000); pv.close(); }
private static void testVIntArraySupport() throws Exception { final PVReader<VIntArray> pv = PVManager.read(vIntArray(doublePV)) .readListener(new PVReaderListener<VIntArray>() { @Override public void pvChanged(PVReaderEvent<VIntArray> event) { PVReader<VIntArray> pv = event.getPvReader(); logException(pv.lastException()); System.out.println(pv.getValue().getData() + " " + Date.from(pv.getValue().getTimestamp()) + " " + pv.getValue().getAlarmSeverity()); } }) .maxRate(ofHertz(10)); Thread.sleep(10000); pv.close(); }
private static void testVByteArraySupport() throws Exception { PVReader<VByteArray> pv = PVManager.read(vByteArray(doublePV)) .readListener(new PVReaderListener<VByteArray>() { @Override public void pvChanged(PVReaderEvent<VByteArray> event) { PVReader<VByteArray> pv = event.getPvReader(); System.out.println(pv.getValue().getData() + " " + Date.from(pv.getValue().getTimestamp()) + " " + pv.getValue().getAlarmSeverity()); } }) .maxRate(ofHertz(10)); Thread.sleep(10000); pv.close(); }
private static void testVShortArraySupport() throws Exception { final PVReader<VShortArray> pv = PVManager.read(vShortArray(doublePV)) .readListener(new PVReaderListener<VShortArray>() { @Override public void pvChanged(PVReaderEvent<VShortArray> event) { PVReader<VShortArray> pv = event.getPvReader(); System.out.println(pv.getValue().getData() + " " + Date.from(pv.getValue().getTimestamp()) + " " + pv.getValue().getAlarmSeverity()); } }) .maxRate(ofHertz(10)); Thread.sleep(10000); pv.close(); }
private static void testVDoubleSupport() throws Exception { final PVReader<VDouble> pv = PVManager.read(vDouble(doublePV)) .readListener(new PVReaderListener<VDouble>() { @Override public void pvChanged(PVReaderEvent<VDouble> event) { PVReader<VDouble> pv = event.getPvReader(); System.out.println(pv.getValue().getValue() + " " + Date.from(pv.getValue().getTimestamp()) + " " + pv.getValue().getAlarmSeverity()); } }) .maxRate(ofHertz(10)); Thread.sleep(10000); pv.close(); }
private static void testVIntSupport() throws Exception { final PVReader<VInt> pv = PVManager.read(vInt(intPV)) .readListener(new PVReaderListener<VInt>() { @Override public void pvChanged(PVReaderEvent<VInt> event) { PVReader<VInt> pv = event.getPvReader(); System.out.println(pv.getValue().getValue() + " " + Date.from(pv.getValue().getTimestamp()) + " " + pv.getValue().getAlarmSeverity()); } }) .maxRate(ofHertz(10)); Thread.sleep(10000); pv.close(); }
private static void testVDoubleArraySupport() throws Exception { PVReader<VDoubleArray> pv = PVManager.read(vDoubleArray(doubleArrayPV)) .readListener(new PVReaderListener<VDoubleArray>() { @Override public void pvChanged(PVReaderEvent<VDoubleArray> event) { PVReader<VDoubleArray> pv = event.getPvReader(); logException(pv.lastException()); if (pv.getValue() != null) { System.out.println(pv.getValue().getData() + " " + Date.from(pv.getValue().getTimestamp()) + " " + pv.getValue().getAlarmSeverity()); } } }) .maxRate(ofHertz(10)); Thread.sleep(10000); pv.close(); }
private static void testVStringSupport() throws Exception { final PVReader<VString> pv = PVManager.read(vString(stringPV)) .readListener(new PVReaderListener<VString>() { @Override public void pvChanged(PVReaderEvent<VString> event) { PVReader<VString> pv = event.getPvReader(); logException(pv.lastException()); if (pv.getValue() != null) { System.out.println(pv.getValue().getValue() + " " + Date.from(pv.getValue().getTimestamp()) + " " + pv.getValue().getAlarmSeverity()); } } }) .maxRate(ofHertz(10)); Thread.sleep(10000); pv.close(); }
private static void testVEnumSupport() throws Exception { final PVReader<VEnum> pv = PVManager.read(vEnum(enumPV)) .readListener(new PVReaderListener<VEnum>() { @Override public void pvChanged(PVReaderEvent<VEnum> event) { PVReader<VEnum> pv = event.getPvReader(); logException(pv.lastException()); if (pv.getValue() != null) { System.out.println(pv.getValue().getValue() + " " + Date.from(pv.getValue().getTimestamp()) + " " + pv.getValue().getAlarmSeverity()); } } }) .maxRate(ofHertz(10)); Thread.sleep(10000); pv.close(); } }
public static void main(String[] args) throws InterruptedException { int nPvs = 5000; SetupUtil.defaultCASetup(); for (int i = 0; i < nPvs; i++) { PVReader<VType> reader = PVManager.read(ExpressionLanguage.vType("sim://noise")) .readListener(new PVReaderListener<VType>() { @Override public void pvChanged(PVReaderEvent<VType> event) { counter.incrementAndGet(); } }).maxRate(ofHertz(50)); readers.add(reader); } Thread.sleep(60000); for (PVReader<VType> pVReader : readers) { pVReader.close(); } } }
public void b2_readAllValues() { // Read channel "channelName" up to every 100 ms, and get all // the new values from the last notification. PVReader<List<Object>> pvReader = PVManager .read(newValuesOf(channel("channelName"))) .readListener(new PVReaderListener<List<Object>>() { @Override public void pvChanged(PVReaderEvent<List<Object>> event) { // Do something with each value for (Object newValue : event.getPvReader().getValue()) { System.out.println(newValue); } } }) .maxRate(ofMillis(100)); // Remember to close pvReader.close(); // newValuesOf limits the values in the queue, to protect memory // consumption in problematic circumstances. The default is 1000 elements, // which you can override. See all options in the ExpressionLanguage class. }