Reverting local HTTP gateway back from HTTPS to HTTP; there was a change regarding...
[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 // Checker annotations
22 //import iotchecker.qual.*;
23
24 /** WeatherPhoneProxy that uses IoTRemoteCall and WeatherInfo class
25  *  to get information from a phone app
26  *
27  * @author      Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
28  * @version     1.0                
29  * @since       2016-04-26
30  */
31 public class WeatherPhoneGateway implements WeatherGateway {
32
33         /**
34          * PhoneGateway class properties
35          */
36         private WeatherInfo weatherInfo;
37         private IoTRemoteCall iotRemCall;
38         private List<WeatherGatewaySmartCallback> listPGWCallback;
39         private AtomicBoolean doEnd;
40         private Thread callbackThread;
41         private Thread workerThread;
42         private IoTDeviceAddress iotDevAdd;
43
44         @config private IoTSet<IoTDeviceAddress> ph_address;
45
46         /**
47          * Constructor
48          */
49         /*public WeatherPhoneGateway() throws RemoteException, UnknownHostException {
50
51                 iotDevAdd = new IoTDeviceAddress("192.168.2.101", 1234, 8000);
52                 weatherInfo = new WeatherInfo();
53
54                 // Launch IoTRemoteCall server in a separate thread
55                 workerThread = new Thread(new Runnable() {
56                         public void run() {
57                                 iotRemCall = new IoTRemoteCall(WeatherInfoInterface.class, 
58                                         weatherInfo, iotDevAdd.getDestinationPortNumber());
59                         }
60                 });
61                 workerThread.start();
62
63                 System.out.println("PhoneGateway is started");
64         
65         }*/
66         public WeatherPhoneGateway() {
67         }
68
69         /**
70          * Init() function
71          */
72         public void init() {
73
74                 // Get address
75                 Iterator it = ph_address.iterator();
76                 iotDevAdd = (IoTDeviceAddress) it.next();
77 //              try {
78 //                      iotDevAdd = new IoTDeviceAddress("192.168.2.101", 1234, 8000);
79 //              } catch (Exception ex) {
80 //              }
81                 System.out.println("Address: " + iotDevAdd.getCompleteAddress());
82                 System.out.println("Source port: " + iotDevAdd.getSourcePortNumber());
83                 System.out.println("Destination port: " + iotDevAdd.getDestinationPortNumber());
84
85                 // Get server
86                 weatherInfo = new WeatherInfo();
87                 System.out.println("DEBUG: Is new data available: " + weatherInfo.isNewDataAvailable());
88                 listPGWCallback = new ArrayList<WeatherGatewaySmartCallback>();
89                 doEnd = new AtomicBoolean(false);
90
91                 // Threads
92                 callbackThread = null;
93                 workerThread = null;
94         }
95
96         /**
97          * Start() function to start threads
98          */
99         public void start() {
100                 doEnd.set(false);
101
102                 // Launch IoTRemoteCall server in a separate thread
103                 workerThread = new Thread(new Runnable() {
104                         /* TODO: We revert back to HTTP because of new scheme for TLS context in Android 7
105                         This disrupts the usual setting for self-signed certificate
106                         See this link for more info: https://github.com/owntracks/android/issues/481
107                         public void run() {
108                                 iotRemCall = new IoTRemoteCall(WeatherInfoInterface.class, 
109                                         weatherInfo, iotDevAdd.getDestinationPortNumber(),
110                                         IoTDeviceAddress.getLocalHostAddress());
111                         }*/
112                         public void run() {
113                                 iotRemCall = new IoTRemoteCall(WeatherInfoInterface.class, 
114                                 weatherInfo, iotDevAdd.getDestinationPortNumber());
115                         }
116                 });
117                 workerThread.start();
118                 System.out.println("DEBUG: Started IoTRemoteCall object!!!");
119
120                 callbackThread = new Thread(new Runnable() {
121                         public void run() {
122                                 doCallbacks();
123                         }
124                 });
125                 callbackThread.start();
126                 System.out.println("DEBUG: Do Callbacks!!!");
127         }
128
129         /**
130          * Stop() function to stop threads
131          */
132         public void stop() {
133                 doEnd.set(true);
134
135                 try {
136                         callbackThread.join();
137                         workerThread.join();
138                 } catch (Exception e) {
139                         e.printStackTrace();
140                 }
141         }
142
143         /**
144          * Register callbacks
145          */
146         public void registerCallback(WeatherGatewaySmartCallback _c) {
147                 listPGWCallback.add(_c);
148         }
149
150         /**
151          * Do callbacks
152          */
153         private void doCallbacks() {
154
155                 while (!doEnd.get()) {
156                         // Only call back if there is new data
157                         if (weatherInfo.isNewDataAvailable()) {
158                                 System.out.println("We get into doCallbacks!");
159                                 System.out.println("weatherInfo.isNewDataAvailable(): " + weatherInfo.isNewDataAvailable());
160                                 for (WeatherGatewaySmartCallback c : listPGWCallback) {
161                                         //try {
162                                                 //c.informationRetrieved(this);
163                                                 c.informationRetrieved(this.getInchesPerWeek(), this.getWeatherZipCode(), this.getDaysToWaterOn(), this.getInchesPerMinute());
164                                         //} catch (RemoteException ex) {
165                                         //      ex.printStackTrace();
166                                         //}
167                                         // We have read data - set this back to false
168                                 }
169                                 weatherInfo.setNewDataAvailable(false);
170                         }
171                 }
172         }
173
174         /**
175          * Simply return this.dInchesPerWeek
176          */
177         public double getInchesPerWeek() {
178
179                 return weatherInfo.getInchesPerWeek();
180         }
181
182         /**
183          * Simply return this.iWeatherZipCode
184          */
185         public int getWeatherZipCode() {
186
187                 return weatherInfo.getWeatherZipCode();
188         }
189
190         /**
191          * Simply return this.iDaysToWaterOn
192          */
193         public int getDaysToWaterOn() {
194
195                 return weatherInfo.getDaysToWaterOn();
196         }
197
198         /**
199          * Simply return this.dInchesPerMinute
200          */
201         public double getInchesPerMinute() {
202
203                 return weatherInfo.getInchesPerMinute();
204         }
205
206
207 //      public static void main(String[] args) throws UnknownHostException, RemoteException {
208
209 //              @LocalRemote WeatherPhoneGateway wpg = new @LocalRemote WeatherPhoneGateway();
210 //              wpg.init();
211 //              wpg.start();
212 //      }
213 }