Cleaning up benchmarks and drivers code.
[iot2.git] / benchmarks / drivers / Java / WeatherPhoneGateway / WeatherPhoneGateway.java
1 package iotcode.WeatherPhoneGateway;
2
3 // Java standard library
4 import java.util.ArrayList;
5 import java.util.concurrent.atomic.AtomicBoolean;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.net.UnknownHostException;
9
10 // RMI Packages
11 import java.rmi.Remote;
12 import java.rmi.RemoteException;
13
14 // IoTRuntime library
15 import iotruntime.stub.IoTRemoteCall;
16 import iotruntime.slave.IoTSet;
17 import iotruntime.slave.IoTDeviceAddress;
18 import iotcode.annotation.*;
19 import iotcode.interfaces.*;
20
21 /** WeatherPhoneProxy that uses IoTRemoteCall and WeatherInfo class
22  *  to get information from a phone app
23  *
24  * @author      Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
25  * @version     1.0                
26  * @since       2016-04-26
27  */
28 public class WeatherPhoneGateway implements WeatherGateway {
29
30         /**
31          * PhoneGateway class properties
32          */
33         private WeatherInfo weatherInfo;
34         private IoTRemoteCall iotRemCall;
35         private List<WeatherGatewaySmartCallback> listPGWCallback;
36         private AtomicBoolean doEnd;
37         private Thread callbackThread;
38         private Thread workerThread;
39         private IoTDeviceAddress iotDevAdd;
40
41         @config private IoTSet<IoTDeviceAddress> ph_address;
42
43         /**
44          * Constructor
45          */
46         public WeatherPhoneGateway() {
47         }
48
49         /**
50          * Init() function
51          */
52         public void init() {
53
54                 // Get address
55                 Iterator it = ph_address.iterator();
56                 iotDevAdd = (IoTDeviceAddress) it.next();
57                 System.out.println("Address: " + iotDevAdd.getCompleteAddress());
58                 System.out.println("Source port: " + iotDevAdd.getSourcePortNumber());
59                 System.out.println("Destination port: " + iotDevAdd.getDestinationPortNumber());
60
61                 // Get server
62                 weatherInfo = new WeatherInfo();
63                 System.out.println("DEBUG: Is new data available: " + weatherInfo.isNewDataAvailable());
64                 listPGWCallback = new ArrayList<WeatherGatewaySmartCallback>();
65                 doEnd = new AtomicBoolean(false);
66
67                 // Threads
68                 callbackThread = null;
69                 workerThread = null;
70         }
71
72         /**
73          * Start() function to start threads
74          */
75         public void start() {
76                 doEnd.set(false);
77
78                 // Launch IoTRemoteCall server in a separate thread
79                 workerThread = new Thread(new Runnable() {
80                         /* TODO: We revert back to HTTP because of new scheme for TLS context in Android 7
81                         This disrupts the usual setting for self-signed certificate
82                         See this link for more info: https://github.com/owntracks/android/issues/481
83                         public void run() {
84                                 iotRemCall = new IoTRemoteCall(WeatherInfoInterface.class, 
85                                         weatherInfo, iotDevAdd.getDestinationPortNumber(),
86                                         IoTDeviceAddress.getLocalHostAddress());
87                         }*/
88                         public void run() {
89                                 iotRemCall = new IoTRemoteCall(WeatherInfoInterface.class, 
90                                 weatherInfo, iotDevAdd.getDestinationPortNumber());
91                         }
92                 });
93                 workerThread.start();
94                 System.out.println("DEBUG: Started IoTRemoteCall object!!!");
95
96                 callbackThread = new Thread(new Runnable() {
97                         public void run() {
98                                 doCallbacks();
99                         }
100                 });
101                 callbackThread.start();
102                 System.out.println("DEBUG: Do Callbacks!!!");
103         }
104
105         /**
106          * Stop() function to stop threads
107          */
108         public void stop() {
109                 doEnd.set(true);
110
111                 try {
112                         callbackThread.join();
113                         workerThread.join();
114                 } catch (Exception e) {
115                         e.printStackTrace();
116                 }
117         }
118
119         /**
120          * Register callbacks
121          */
122         public void registerCallback(WeatherGatewaySmartCallback _c) {
123                 listPGWCallback.add(_c);
124         }
125
126         /**
127          * Do callbacks
128          */
129         private void doCallbacks() {
130
131                 while (!doEnd.get()) {
132                         // Only call back if there is new data
133                         if (weatherInfo.isNewDataAvailable()) {
134                                 System.out.println("We get into doCallbacks!");
135                                 System.out.println("weatherInfo.isNewDataAvailable(): " + weatherInfo.isNewDataAvailable());
136                                 for (WeatherGatewaySmartCallback c : listPGWCallback) {
137                                         c.informationRetrieved(this.getInchesPerWeek(), this.getWeatherZipCode(), this.getDaysToWaterOn(), this.getInchesPerMinute());
138                                 }
139                                 // We have read data - set this back to false
140                                 weatherInfo.setNewDataAvailable(false);
141                         }
142                 }
143         }
144
145         /**
146          * Simply return this.dInchesPerWeek
147          */
148         public double getInchesPerWeek() {
149
150                 return weatherInfo.getInchesPerWeek();
151         }
152
153         /**
154          * Simply return this.iWeatherZipCode
155          */
156         public int getWeatherZipCode() {
157
158                 return weatherInfo.getWeatherZipCode();
159         }
160
161         /**
162          * Simply return this.iDaysToWaterOn
163          */
164         public int getDaysToWaterOn() {
165
166                 return weatherInfo.getDaysToWaterOn();
167         }
168
169         /**
170          * Simply return this.dInchesPerMinute
171          */
172         public double getInchesPerMinute() {
173
174                 return weatherInfo.getInchesPerMinute();
175         }
176 }