View Javadoc
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.Arrays;
8   import java.util.List;
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 static org.epics.pvmanager.vtype.ExpressionLanguage.*;
16  import org.epics.util.array.ListDouble;
17  import org.epics.util.array.ListNumber;
18  import static org.epics.util.time.TimeDuration.*;
19  import org.epics.vtype.Alarm;
20  import org.epics.vtype.Display;
21  import org.epics.vtype.Time;
22  import org.epics.vtype.VDouble;
23  import org.epics.vtype.VNumber;
24  import org.epics.vtype.VNumberArray;
25  import org.epics.vtype.VTable;
26  import org.epics.vtype.VType;
27  import org.epics.vtype.ValueUtil;
28  
29  /**
30   * Examples for using vType expressions.
31   *
32   * @author carcassi
33   */
34  public class VTypeExamples {
35      
36      public void v1_readNumericType() {
37          // Let's statically import so the code looks cleaner
38          // import static org.epics.pvmanager.data.ExpressionLanguage.*;
39   
40          // Read and Write a vNumber
41          // Note that the read type is different form the write type
42          final PV<VNumber, Number> pv = PVManager.readAndWrite(vNumber("currentRB"))
43              .readListener(new PVReaderListener<VNumber>() {
44                  @Override
45                  public void pvChanged(PVReaderEvent<VNumber> event) {
46                      VNumber value = event.getPvReader().getValue();
47                      if (value != null) {
48                          System.out.println(value.getValue() + " " + value.getAlarmSeverity());
49                      }
50                  }
51              })
52              .asynchWriteAndMaxReadRate(ofMillis(10));
53          pv.write(1.0);
54          
55          // Remember to close
56          pv.close();
57          
58          // For a full list of types, refer to org.epics.pvmanager.data.ExpressionLanguage
59      }
60      
61      public void v2_genericReaderExtractParialInformation() {
62          final PVReader<VType> pvReader = PVManager.read(vType("channelName"))
63              .readListener(new PVReaderListener<VType>() {
64                  @Override
65                  public void pvChanged(PVReaderEvent<VType> event) {
66                      VType value = event.getPvReader().getValue();
67                      // We can extract the different aspect of the read object,
68                      // so that we can work on them separately
69  
70                      // This returns the interface implemented (VDouble, VInt, ...)
71                      Class<?> type = ValueUtil.typeOf(value);
72                      // Extracts the alarm if present
73                      Alarm alarm = ValueUtil.alarmOf(value);
74                      // Extracts the time if present
75                      Time time = ValueUtil.timeOf(value);
76                      // Extracts a numeric value if present
77                      Double number = ValueUtil.numericValueOf(value);
78                      // Extract display information if present
79                      Display display = ValueUtil.displayOf(value);
80  
81                      // setAlarm(alarm);
82                      // setTime(time);
83                  }
84              })
85              .maxRate(ofMillis(10));
86      }
87      
88      public void v3_genericReaderSwitchOnType() {
89          final PVReader<VType> pvReader = PVManager.read(vType("channelName"))
90              .readListener(new PVReaderListener<VType>() {
91                  @Override
92                  public void pvChanged(PVReaderEvent<VType> event) {
93                      // We can switch on the full type
94                      if (event.getPvReader().getValue() instanceof VDouble) {
95                          VDouble vDouble = (VDouble) event.getPvReader().getValue();
96                          // Do something with a VDouble
97                      }
98                      // ...
99                  }
100             })
101             .maxRate(ofMillis(100));
102     }
103     
104     public void v4_readArrays() {
105         // Reads a numeric array of any type (double, float, int, ...)
106         // This allows to work on any array type without having to create
107         // bindings for each.
108         final PVReader<VNumberArray> pvReader = PVManager.read(vNumberArray("channelName"))
109             .readListener(new PVReaderListener<VNumberArray>() {
110                 public void pvChanged(PVReaderEvent<VNumberArray> event) {
111                     if (event.isValueChanged()) {
112                         // New value
113                         VNumberArray value = event.getPvReader().getValue();
114                         ListNumber data = value.getData();
115                         for (int i = 0; i < data.size(); i++) {
116                             // Get the double representation of the value,
117                             // converting it if needed
118                             double iValue = data.getDouble(i);
119                             System.out.println(iValue);
120                         }
121                     }
122                 }
123             })
124             .maxRate(ofMillis(100));
125     }
126     
127     public void v5_assemblingNumericArrayFromScalars() {
128         List<String> channelNames = Arrays.asList("channel1", "channel2", "channel3", "channel4");
129         // Reads a list of different numeric channels as a single array.
130         // The channels can be of any numeric type (double, float, int, ...)
131         final PVReader<VNumberArray> pvReader = PVManager.read(
132                 vNumberArrayOf(latestValueOf(vNumbers(channelNames))))
133             .readListener(new PVReaderListener<VNumberArray>() {
134                 public void pvChanged(PVReaderEvent<VNumberArray> event) {
135                     if (event.isValueChanged()) {
136                         // Do something with the value
137                         System.out.println(event.getPvReader().getValue());
138                     }
139                 }
140             })
141             .maxRate(ofMillis(100));
142     }
143     
144     public void v6_assemblingTables() {
145         // You can assemble a table by giving a desired rate expression for each cell, 
146         // organizing them by column. You can use constant expressions for 
147         // labels or values that do not change. 
148         List<String> names = Arrays.asList("one", "two", "trhee");
149         PVReader<VTable> pvReader = PVManager
150                 .read(vTable(column("Names", vStringConstants(names)),
151                              column("Values", latestValueOf(channels(names)))))
152                 .readListener(new PVReaderListener<VTable>() {
153                     @Override
154                     public void pvChanged(PVReaderEvent<VTable> pvReader) {
155                         VTable vTable = pvReader.getPvReader().getValue();
156                         // First column is the names
157                         @SuppressWarnings("unchecked")
158                         List<String> names = (List<String>) vTable.getColumnData(0);
159                         // Second column is the values
160                         ListDouble values = (ListDouble) vTable.getColumnData(1);
161                         // ...
162                     }
163                 })
164                 .maxRate(ofMillis(100));
165     }
166 }