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.HashMap;
8   import java.util.Map;
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 static org.epics.util.time.TimeDuration.*;
17  
18  /**
19   * This is the code from the examples in the docs, to make sure it
20   * actually compiles
21   *
22   * @author carcassi
23   */
24  public class MultipleChannelExamples {
25      
26      public void m1_readMultipleChannels() {
27          // Read a map with the channels named "one", "two" and "three"
28          PVReader<Map<String, Object>> pvReader = PVManager
29              .read(mapOf(latestValueOf(channels("one", "two", "three"))))
30              .readListener(new PVReaderListener<Map<String, Object>>() {
31                  @Override
32                  public void pvChanged(PVReaderEvent<Map<String, Object>> event) {
33                      // Print the values if any
34                      Map<String, Object> map = event.getPvReader().getValue();
35                      if (map != null) {
36                          System.out.println("one: " + map.get("one") +
37                                  " - two: " + map.get("two") + 
38                                  " - three: " + map.get("three"));
39                      }
40                  }
41              })
42              .maxRate(ofMillis(100));
43          
44          // Remember to close
45          pvReader.close();
46          
47          // Note that when using a composite datasource, the channels can be
48          //from different sources (e.g. "sim://noise" and "ca://mypv"). 
49      }
50      
51      public void m2_readMultipleChannels() {
52          // Write a map to the channels named "one", "two" and "three"
53          PVWriter<Map<String, Object>> pvWriter = PVManager
54                  .write(mapOf(channels("one", "two", "three")))
55                  .async();
56          
57          // Prepare the 3 values
58          Map<String, Object> values = new HashMap<String, Object>();
59          values.put("one", 1.0);
60          values.put("two", 2.0);
61          values.put("three", "run");
62          
63          // Write
64          pvWriter.write(values);
65          
66          // Remember to close
67          pvWriter.close();
68          
69          // Note that when using a composite datasource, the channels can be
70          //from different sources (e.g. "sim://noise" and "ca://mypv"). 
71      }
72      
73      public void m3_readWriteMultipleChannels() {
74          // Read and write a map to the channels named "one", "two" and "three"
75          PV<Map<String, Object>, Map<String, Object>> pv = PVManager
76              .readAndWrite(mapOf(latestValueOf(channels("one", "two", "three"))))
77              .asynchWriteAndMaxReadRate(ofMillis(100));
78          
79          // Do something
80          // ...
81          
82          // Remember to close
83          pv.close();
84      }
85      
86      public void m4_renameChannels() {
87          // Read a map with the channels "one", "two" and "three"
88          // reffered in the map as "setpoint", "readback" and "difference"
89          PVReader<Map<String, Object>> pvReader = PVManager
90              .read(mapOf(latestValueOf(channel("one").as("setpoint")
91                           .and(channel("two").as("readback"))
92                           .and(channel("three").as("difference")))))
93              .readListener(new PVReaderListener<Map<String, Object>>() {
94                  @Override
95                  public void pvChanged(PVReaderEvent<Map<String, Object>> event) {
96                      // Print the values if any
97                      Map<String, Object> map = event.getPvReader().getValue();
98                      if (map != null) {
99                          System.out.println("setpoint: " + map.get("setpoint") +
100                                 " - readback: " + map.get("readback") + 
101                                 " - difference: " + map.get("difference"));
102                     }
103                 }
104             })
105             .maxRate(ofMillis(100));
106         
107         // Remember to close
108         pvReader.close();
109         
110         // Any expression however created can be renamed.
111     }
112     
113     public void m5_writeOrdering() {
114         // Write a map to the channels named "one", "two" and "three"
115         // Write "two" after "one" and write "three" after "two"
116         PVWriter<Map<String, Object>> pvWriter = PVManager.write(
117                 mapOf(channel("one")
118                       .and(channel("two").after("one"))
119                       .and(channel("three").after("two")))).async();
120         
121         // Do something
122         // ...
123         
124         // Remember to close
125         pvWriter.close();
126     }
127 }