1
2
3
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
24
25
26
27
28 public class BasicExamples {
29
30 public void b1_readLatestValue() {
31
32
33
34
35
36 PVReader<Object> pvReader = PVManager.read(channel("channelName"))
37 .readListener(new PVReaderListener<Object>() {
38 @Override
39 public void pvChanged(PVReaderEvent<Object> event) {
40
41 Object newValue = event.getPvReader().getValue();
42 System.out.println(newValue);
43 }
44 })
45 .maxRate(ofMillis(100));
46
47
48
49
50
51
52 pvReader.close();
53
54
55
56
57
58
59
60 }
61
62 public void b2_readAllValues() {
63
64
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
72 for (Object newValue : event.getPvReader().getValue()) {
73 System.out.println(newValue);
74 }
75 }
76 })
77 .maxRate(ofMillis(100));
78
79
80 pvReader.close();
81
82
83
84
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
98
99 pvWriter.write("New value");
100
101
102 pvWriter.close();
103 }
104
105 public void b4_synchronousWrite() {
106 PVWriter<Object> pvWriter = PVManager.write(channel("channelName")).sync();
107
108 pvWriter.write("New value");
109 System.out.println("Write finished");
110
111
112 pvWriter.close();
113 }
114
115 public void b5_readAndWrite() {
116
117 PV<Object, Object> pv = PVManager.readAndWrite(channel("channelName"))
118 .readListener(new PVReaderListener<Object>() {
119 @Override
120 public void pvChanged(PVReaderEvent<Object> event) {
121
122 Object newValue = event.getPvReader().getValue();
123 System.out.println(newValue);
124 }
125 })
126 .asynchWriteAndMaxReadRate(ofMillis(10));
127 pv.write("New value");
128
129
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
139
140
141
142
143 Exception ex = event.getPvReader().lastException();
144
145
146
147 if (event.getPvReader().lastException() == null) {
148
149 }
150 }
151 })
152 .maxRate(ofMillis(100));
153 }
154
155 public void b7_logAllErrors() {
156
157
158
159
160
161
162
163
164
165
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
176
177
178
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
185
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 }