50b44508c08a749473d0db1556b45d15159e2886
[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                         public void run() {
105                                 iotRemCall = new IoTRemoteCall(WeatherInfoInterface.class, 
106                                         weatherInfo, iotDevAdd.getDestinationPortNumber(),
107                                         IoTDeviceAddress.getLocalHostAddress());
108                         }
109                 });
110                 workerThread.start();
111                 System.out.println("DEBUG: Started IoTRemoteCall object!!!");
112
113                 callbackThread = new Thread(new Runnable() {
114                         public void run() {
115                                 doCallbacks();
116                         }
117                 });
118                 callbackThread.start();
119                 System.out.println("DEBUG: Do Callbacks!!!");
120         }
121
122         /**
123          * Stop() function to stop threads
124          */
125         public void stop() {
126                 doEnd.set(true);
127
128                 try {
129                         callbackThread.join();
130                         workerThread.join();
131                 } catch (Exception e) {
132                         e.printStackTrace();
133                 }
134         }
135
136         /**
137          * Register callbacks
138          */
139         public void registerCallback(WeatherGatewaySmartCallback _c) {
140                 listPGWCallback.add(_c);
141         }
142
143         /**
144          * Do callbacks
145          */
146         private void doCallbacks() {
147
148                 while (!doEnd.get()) {
149                         // Only call back if there is new data
150                         if (weatherInfo.isNewDataAvailable()) {
151                                 System.out.println("We get into doCallbacks!");
152                                 System.out.println("weatherInfo.isNewDataAvailable(): " + weatherInfo.isNewDataAvailable());
153                                 for (WeatherGatewaySmartCallback c : listPGWCallback) {
154                                         //try {
155                                                 //c.informationRetrieved(this);
156                                                 c.informationRetrieved(this.getInchesPerWeek(), this.getWeatherZipCode(), this.getDaysToWaterOn(), this.getInchesPerMinute());
157                                         //} catch (RemoteException ex) {
158                                         //      ex.printStackTrace();
159                                         //}
160                                         // We have read data - set this back to false
161                                 }
162                                 weatherInfo.setNewDataAvailable(false);
163                         }
164                 }
165         }
166
167         /**
168          * Simply return this.dInchesPerWeek
169          */
170         public double getInchesPerWeek() {
171
172                 return weatherInfo.getInchesPerWeek();
173         }
174
175         /**
176          * Simply return this.iWeatherZipCode
177          */
178         public int getWeatherZipCode() {
179
180                 return weatherInfo.getWeatherZipCode();
181         }
182
183         /**
184          * Simply return this.iDaysToWaterOn
185          */
186         public int getDaysToWaterOn() {
187
188                 return weatherInfo.getDaysToWaterOn();
189         }
190
191         /**
192          * Simply return this.dInchesPerMinute
193          */
194         public double getInchesPerMinute() {
195
196                 return weatherInfo.getInchesPerMinute();
197         }
198
199
200 //      public static void main(String[] args) throws UnknownHostException, RemoteException {
201
202 //              @LocalRemote WeatherPhoneGateway wpg = new @LocalRemote WeatherPhoneGateway();
203 //              wpg.init();
204 //              wpg.start();
205 //      }
206 }