]> rtime.felk.cvut.cz Git - orte.git/blob - orte/Robot_Demo/src/org/ocera/orte/demo/PwrVoltageSubscribe.java
ROBOT_DEMO: implement voltage monitor
[orte.git] / orte / Robot_Demo / src / org / ocera / orte / demo / PwrVoltageSubscribe.java
1 package org.ocera.orte.demo;
2
3 import java.util.concurrent.locks.ReentrantReadWriteLock;
4 import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
5 import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
6
7 import org.ocera.orte.DomainApp;
8 import org.ocera.orte.Subscription;
9 import org.ocera.orte.SubscriptionCallback;
10 import org.ocera.orte.types.MessageData;
11 import org.ocera.orte.types.NtpTime;
12 import org.ocera.orte.types.RecvInfo;
13 import org.ocera.orte.types.SubsProp;
14
15 import android.os.Bundle;
16 import android.os.Handler;
17 import android.os.Message;
18
19 public class PwrVoltageSubscribe extends SubscriptionCallback{
20
21     public final static int IMMEDIATE    = 0x02;
22     public final static int BEST_EFFORTS = 0x01;
23
24     static Handler handler;
25     
26         private Subscription sub;
27         private PwrVoltageType voltagemsg;
28         private DomainApp domainApp;
29         private SubsProp subProps;
30         
31         private boolean isCancelled = true;
32         private final ReentrantReadWriteLock controlRrwl = new ReentrantReadWriteLock(true);
33         private final ReadLock rcLock = controlRrwl.readLock();
34         private final WriteLock wcLock = controlRrwl.writeLock();
35         
36         public PwrVoltageSubscribe(DomainApp domainApp, Handler dialogHandler) {
37                 handler = dialogHandler;
38                 this.domainApp = domainApp;
39                 
40             NtpTime deadline      = new NtpTime(0,1288490189);
41             NtpTime minSeparation = new NtpTime(1);
42             
43             voltagemsg = new PwrVoltageType(domainApp, "pwr_voltage");
44                 
45             subProps = new SubsProp(voltagemsg.getTopic(),
46                                                         "pwr_voltage",                                  
47                                                         minSeparation,  
48                                                         deadline,
49                                                         IMMEDIATE,
50                                                         BEST_EFFORTS,
51                                                         0);
52         }
53         
54         public void start() {
55                 wcLock.lock();
56                 try {
57                         isCancelled = false;
58                         sub = domainApp.createSubscription(subProps, voltagemsg, this);
59                 } finally {
60                         wcLock.unlock();
61                 }
62         }
63         
64         public void cancel() {
65                 wcLock.lock();
66                 try {
67                         isCancelled = true;
68                         sub.destroy();
69                 } finally {
70                         wcLock.unlock();
71                 }
72         }
73         
74         public boolean isCancelled() {
75                 rcLock.lock();
76                 try {
77                         return isCancelled;
78                 } finally {
79                         rcLock.unlock();
80                 }
81         }
82         
83     public void callback(RecvInfo info, MessageData msg) {
84         Message message = handler.obtainMessage();
85         Bundle bundle = new Bundle();
86         
87         bundle.putDoubleArray("voltages", ((PwrVoltageType)msg).voltage.clone());
88         message.setData(bundle);
89         handler.sendMessage(message);
90     }
91 }