1 /**
2 * Copyright (C) 2010-14 pvmanager developers. See COPYRIGHT.TXT
3 * All rights reserved. Use is subject to license terms. See LICENSE.TXT
4 */
5 package org.epics.pvmanager.sample;
6
7 import java.util.List;
8 import org.epics.pvmanager.ExceptionHandler;
9 import static org.epics.pvmanager.ExpressionLanguage.*;
10 import org.epics.pvmanager.PV;
11 import org.epics.pvmanager.PVManager;
12 import org.epics.pvmanager.PVReader;
13 import org.epics.pvmanager.PVReaderEvent;
14 import org.epics.pvmanager.PVReaderListener;
15 import org.epics.pvmanager.PVWriter;
16 import org.epics.pvmanager.PVWriterEvent;
17 import org.epics.pvmanager.PVWriterListener;
18 import org.epics.pvmanager.TimeoutException;
19 import org.epics.pvmanager.WriteFunction;
20 import static org.epics.util.time.TimeDuration.*;
21
22 /**
23 * This is the code from the examples in the docs, to make sure it
24 * actually compiles
25 *
26 * @author carcassi
27 */
28 public class BasicExamples {
29
30 public void b1_readLatestValue() {
31 // Let's statically import so the code looks cleaner
32 // import static org.epics.pvmanager.ExpressionLanguage.*;
33 // import static org.epics.util.time.TimeDuration.*;
34
35 // Get updates from channel "channelName" up to every 100 ms
36 PVReader<Object> pvReader = PVManager.read(channel("channelName"))
37 .readListener(new PVReaderListener<Object>() {
38 @Override
39 public void pvChanged(PVReaderEvent<Object> event) {
40 // Do something with each value
41 Object newValue = event.getPvReader().getValue();
42 System.out.println(newValue);
43 }
44 })
45 .maxRate(ofMillis(100));
46
47 // Note that "channel" returns an object. You can always substitute it for
48 // a more precise operator to have guarantees you can process the value
49 // (e.g. vType("channelName"), vNumber("channelName"), ...)
50
51 // Remember to close
52 pvReader.close();
53
54 // The interval between updates can be specified in different units
55 // (e.g. ms, sec, min, hour, hz). Check the documentation at org.epics.pvmanager.util.TimeDuration.
56
57 // IMPORTANT: you _must_ keep a reference to your reader so that you can
58 // close it later. If you don't, pvmanager will consider that reader "lost"
59 // and it will close it automatically.
60 }
61
62 public void b2_readAllValues() {
63 // Read channel "channelName" up to every 100 ms, and get all
64 // the new values from the last notification.
65 PVReader<List<Object>> pvReader = PVManager
66 .read(newValuesOf(channel("channelName")))
67 .readListener(new PVReaderListener<List<Object>>() {
68
69 @Override
70 public void pvChanged(PVReaderEvent<List<Object>> event) {
71 // Do something with each value
72 for (Object newValue : event.getPvReader().getValue()) {
73 System.out.println(newValue);
74 }
75 }
76 })
77 .maxRate(ofMillis(100));
78
79 // Remember to close
80 pvReader.close();
81
82 // newValuesOf limits the values in the queue, to protect memory
83 // consumption in problematic circumstances. The default is 1000 elements,
84 // which you can override. See all options in the ExpressionLanguage class.
85 }
86
87 public void b3_asynchronousWrite() {
88 PVWriter<Object> pvWriter = PVManager.write(channel("channelName"))
89 .writeListener(new PVWriterListener<Object>() {
90 public void pvChanged(PVWriterEvent<Object> event) {
91 if (event.isWriteSucceeded()) {
92 System.out.println("Write finished");
93 }
94 }
95 })
96 .async();
97 // This will return right away, and the notification will be sent
98 // on the listener
99 pvWriter.write("New value");
100
101 // Remember to close
102 pvWriter.close();
103 }
104
105 public void b4_synchronousWrite() {
106 PVWriter<Object> pvWriter = PVManager.write(channel("channelName")).sync();
107 // This will block until the write is done
108 pvWriter.write("New value");
109 System.out.println("Write finished");
110
111 // Remember to close
112 pvWriter.close();
113 }
114
115 public void b5_readAndWrite() {
116 // A PV is both a PVReader and a PVWriter
117 PV<Object, Object> pv = PVManager.readAndWrite(channel("channelName"))
118 .readListener(new PVReaderListener<Object>() {
119 @Override
120 public void pvChanged(PVReaderEvent<Object> event) {
121 // Do something with each value
122 Object newValue = event.getPvReader().getValue();
123 System.out.println(newValue);
124 }
125 })
126 .asynchWriteAndMaxReadRate(ofMillis(10));
127 pv.write("New value");
128
129 // Remember to close
130 pv.close();
131 }
132
133 public void b6_handlingErrorsOnNotification() {
134 PVReader<Object> pvReader = PVManager.read(channel("channelName"))
135 .readListener(new PVReaderListener<Object>() {
136 @Override
137 public void pvChanged(PVReaderEvent<Object> event) {
138 // By default, read exceptions are made available
139 // on the reader itself.
140 // This will give you only the last exception, so if
141 // more then one exception was generated after the last read,
142 // some will be lost.
143 Exception ex = event.getPvReader().lastException();
144
145 // Note that taking the exception, clears it
146 // so next call you'll get null.
147 if (event.getPvReader().lastException() == null) {
148 // Always true
149 }
150 }
151 })
152 .maxRate(ofMillis(100));
153 }
154
155 public void b7_logAllErrors() {
156 // Handling errors within the listener gives you all the advantages
157 // of pvmanager, but it throttles the errors you receive. In most cases
158 // this is good: if a reader connects to 100 broken channels, you
159 // don't get flooded with 100 exceptions. In cases where you
160 // want _all_ the exceptions, instead, you can route them to your
161 // exception handling mechanism.
162
163 // In this example, all read exceptions will be passed to the exception handler
164 // on the thread that it generates them. The handler, therefore,
165 // must be thread safe.
166 final PVReader<Object> pvReader = PVManager.read(channel("channelName"))
167 .routeExceptionsTo(new ExceptionHandler() {
168 public void handleException(Exception ex) {
169 System.out.println("Error: " + ex.getMessage());
170 }
171 }).maxRate(ofMillis(100));
172 }
173
174 public void b8_readTimeout() {
175 // If after 5 seconds no new value comes (i.e. pvReader.getValue() == null)
176 // then a timeout is sent. PVManager will _still_ try to connect,
177 // until pvReader.close() is called.
178 // The timeout will be notified only on the first connection.
179 final PVReader<Object> pvReader = PVManager.read(channel("channelName"))
180 .timeout(ofSeconds(5))
181 .readListener(new PVReaderListener<Object>() {
182 @Override
183 public void pvChanged(PVReaderEvent<Object> event) {
184 // Timeout are passed as exceptions. This allows you to
185 // treat them as any other error conditions.
186 Exception ex = event.getPvReader().lastException();
187 if (ex instanceof TimeoutException) {
188 System.out.println("Didn't connected after 5 seconds");
189 }
190 }
191 })
192 .maxRate(ofMillis(100));
193 }
194 }