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)); }
public void datasourcesInSWT_CSSTudio() { // In CS-Studio, you should never change the default configuration programmatically // or you will create problems for other applications. All configuration // shoul be done on your own readers/writers. // If you are developing user interfaces in SWT, you will want to route the notifications on the SWT thread. // Import from here // import static org.csstudio.utility.pvmanager.ui.SWTUtil.*; // When creating a pv, remember to ask for notification on the SWT thread PVReader<?> pvReader = PVManager.read(channel("test")).notifyOn(swtThread()).maxRate(ofMillis(100)); }
public void b7_logAllErrors() { // Handling errors within the listener gives you all the advantages // of pvmanager, but it throttles the errors you receive. In most cases // this is good: if a reader connects to 100 broken channels, you // don't get flooded with 100 exceptions. In cases where you // want _all_ the exceptions, instead, you can route them to your // exception handling mechanism. // In this example, all read exceptions will be passed to the exception handler // on the thread that it generates them. The handler, therefore, // must be thread safe. final PVReader<Object> pvReader = PVManager.read(channel("channelName")) .routeExceptionsTo(new ExceptionHandler() { public void handleException(Exception ex) { System.out.println("Error: " + ex.getMessage()); } }).maxRate(ofMillis(100)); }
/** * Sets the rate of scan of the expression and creates the actual {@link PVReader} * object that can be monitored through listeners. * * @param rate the minimum time distance (i.e. the maximum rate) between two different notifications * @return the PVReader */ public PVReader<T> maxRate(Duration rate) { maxRateAndValidate(rate); preparePvReader(); PVDirector<T> director = prepareDirector(this); prepareDecoupler(director, this); return pv; }
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)); } }
private void pvChange(final int i) { if (pvs.get(i) != null) { pvs.get(i).close(); imagePanels.get(i).setImage(null); plots.set(i, null); } if (pvFields.get(i).getText() == null || pvFields.get(i).getText().trim().isEmpty()) { return; } SparklineGraph2DExpression plot = sparklineGraphOf(formula(pvFields.get(i).getText()), null, null); plots.set(i, plot); plots.get(i).update(plots.get(i).newUpdate().imageHeight(imagePanels.get(i).getHeight()) .imageWidth(imagePanels.get(i).getWidth())); pvs.set(i, PVManager.read(plots.get(i)) .notifyOn(swingEDT()) .readListener(new PVReaderListener<Graph2DResult>() { @Override public void pvChanged(PVReaderEvent<Graph2DResult> event) { setLastError(event.getPvReader().lastException()); if (event.getPvReader().getValue() != null) { BufferedImage image = ValueUtil.toImage(event.getPvReader().getValue().getImage()); imagePanels.get(i).setImage(image); } } }) .maxRate(ofHertz(50))); }
/** * Creates the pv such that writes are synchronous and read notifications * comes at most at the rate specified. * * @param period minimum time between read notifications * @return a new PV */ public PV<R, W> synchWriteAndMaxReadRate(Duration period) { PVReader<R> pvReader = pvReaderConfiguration.maxRate(period); PVWriter<W> pvWriter = pvWriterConfiguration.sync(); return new PV<R, W>(pvReader, pvWriter); }
/** * Sets a timeout for both reader and writer. * * @param timeout duration of the timeout * @return this expression */ @Override public PVConfiguration<R, W> timeout(Duration timeout) { pvReaderConfiguration.timeout(timeout); pvWriterConfiguration.timeout(timeout); return this; }
/** * 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; }
@Override public PVConfiguration<R, W> notifyOn(Executor onThread) { pvReaderConfiguration.notifyOn(onThread); pvWriterConfiguration.notifyOn(onThread); return this; }
/** * Forwards exception to the given exception handler. No thread switch * is done, so the handler is notified on the thread where the exception * was thrown. * <p> * Giving a custom exception handler will disable the default handler, * so {@link PV#lastException() } and {@link PV#lastWriteException() } * is no longer set and no notification * is done. * * @param exceptionHandler an exception handler * @return this */ public PVConfiguration<R, W> routeExceptionsTo(ExceptionHandler exceptionHandler) { pvReaderConfiguration.routeExceptionsTo(exceptionHandler); pvWriterConfiguration.routeExceptionsTo(exceptionHandler); return this; }
/** * Reads the given expression, and returns an object to configure the parameters * for the read. * * @param <T> type of the read payload * @param pvExpression the expression to read * @return the read configuration */ public static <T> PVReaderConfiguration<T> read(DesiredRateExpression<T> pvExpression) { return new PVReaderConfiguration<T>(pvExpression); }
private void validateReaderConfiguration() { if (maxRate.getSeconds() < 0 && maxRate.getNano() < 5000000) { throw new IllegalArgumentException("Current implementation limits the rate to >5ms or <200Hz (requested " + maxRate + "s)"); } checkDataSourceAndThreadSwitch(); }
@Override public PVConfiguration<R, W> from(DataSource dataSource) { pvReaderConfiguration.from(dataSource); pvWriterConfiguration.from(dataSource); return this; }
protected void reconnect() { if (pv != null) { pv.close(); imagePanel.setImage(null); graph = null; } if (dataFormulaField.getSelectedItem() == null || dataFormulaField.getSelectedItem().toString().trim().isEmpty()) { return; } graph = createExpression(dataFormulaField.getSelectedItem().toString()); graph.update(graph.newUpdate().imageHeight(imagePanel.getHeight()) .imageWidth(imagePanel.getWidth())); pv = PVManager.read(graph) .notifyOn(swingEDT()) .readListener(new PVReaderListener<Graph2DResult>() { @Override public void pvChanged(PVReaderEvent<Graph2DResult> event) { setLastError(pv.lastException()); if (pv.getValue() != null) { BufferedImage image = ValueUtil.toImage(pv.getValue().getImage()); imagePanel.setImage(image); } } }) .maxRate(ofHertz(50)); }
startTime = System.currentTimeMillis(); pv = PVManager.read(formula(pvName.getText())) .timeout(ofSeconds(5)) .readListener(new PVReaderListener<Object>() { @Override public void pvChanged(PVReaderEvent<Object> event) { .maxRate(TimeDuration.ofHertz(10)); total = System.currentTimeMillis() - startTime; if (total > 1) {
public static void profile(int nPvs) throws Exception { List<PVReader<Object>> pvs = new ArrayList<>(); long timeStart = System.currentTimeMillis(); for (int i = 0; i < nPvs; i++) { //PVReader<Object> pv = PVManager.read(constant(new Object())).maxRate(TimeDuration.TimeDuration.ofHertz(50)); PVReader<Object> pv = PVManager.read(channel("channel " + i)).maxRate(TimeDuration.ofHertz(50)); pvs.add(pv); } long startTime = System.currentTimeMillis() - timeStart; double avgLoad = measureLoad(5); System.out.println(nPvs + " " + startTime + " " + avgLoad); for (PVReader<Object> pv : pvs) { pv.close(); } waitForZeroLoad(5); } }
/** * Specifies a timeout, with a different message for the read and the write. * * @param timeout time before notification * @param readMessage exception message for the read timeout * @param writeMessage exception message for the write timeout * @return this */ public PVConfiguration<R, W> timeout(Duration timeout, String readMessage, String writeMessage) { pvReaderConfiguration.timeout(timeout, readMessage); pvWriterConfiguration.timeout(timeout, writeMessage); return this; }
/** * Reads the given expression, and returns an object to configure the parameters * for the read. At each notification it will return the latest value, * even if more had been received from the last notification. * * @param <T> type of the read payload * @param pvExpression the expression to read * @return the read configuration */ public static <T> PVReaderConfiguration<T> read(SourceRateExpression<T> pvExpression) { return new PVReaderConfiguration<T>(ExpressionLanguage.latestValueOf(pvExpression)); }
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)); }