1 package iotcode.WeatherPhoneGateway;
3 // Java standard library
4 import java.util.ArrayList;
5 import java.util.concurrent.atomic.AtomicBoolean;
6 import java.util.Iterator;
8 import java.net.UnknownHostException;
11 import java.rmi.Remote;
12 import java.rmi.RemoteException;
15 import iotruntime.stub.IoTRemoteCall;
16 import iotruntime.slave.IoTSet;
17 import iotruntime.slave.IoTDeviceAddress;
18 import iotcode.annotation.*;
19 import iotcode.interfaces.*;
21 // Checker annotations
22 //import iotchecker.qual.*;
24 /** WeatherPhoneProxy that uses IoTRemoteCall and WeatherInfo class
25 * to get information from a phone app
27 * @author Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
31 public class WeatherPhoneGateway implements WeatherGateway {
34 * PhoneGateway class properties
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;
44 @config private IoTSet<IoTDeviceAddress> ph_address;
49 /*public WeatherPhoneGateway() throws RemoteException, UnknownHostException {
51 iotDevAdd = new IoTDeviceAddress("192.168.2.101", 1234, 8000);
52 weatherInfo = new WeatherInfo();
54 // Launch IoTRemoteCall server in a separate thread
55 workerThread = new Thread(new Runnable() {
57 iotRemCall = new IoTRemoteCall(WeatherInfoInterface.class,
58 weatherInfo, iotDevAdd.getDestinationPortNumber());
63 System.out.println("PhoneGateway is started");
66 public WeatherPhoneGateway() {
75 Iterator it = ph_address.iterator();
76 iotDevAdd = (IoTDeviceAddress) it.next();
78 // iotDevAdd = new IoTDeviceAddress("192.168.2.101", 1234, 8000);
79 // } catch (Exception ex) {
81 System.out.println("Address: " + iotDevAdd.getCompleteAddress());
82 System.out.println("Source port: " + iotDevAdd.getSourcePortNumber());
83 System.out.println("Destination port: " + iotDevAdd.getDestinationPortNumber());
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);
92 callbackThread = null;
97 * Start() function to start threads
102 // Launch IoTRemoteCall server in a separate thread
103 workerThread = new Thread(new Runnable() {
105 iotRemCall = new IoTRemoteCall(WeatherInfoInterface.class,
106 weatherInfo, iotDevAdd.getDestinationPortNumber(),
107 IoTDeviceAddress.getLocalHostAddress());
110 workerThread.start();
111 System.out.println("DEBUG: Started IoTRemoteCall object!!!");
113 callbackThread = new Thread(new Runnable() {
118 callbackThread.start();
119 System.out.println("DEBUG: Do Callbacks!!!");
123 * Stop() function to stop threads
129 callbackThread.join();
131 } catch (Exception e) {
139 public void registerCallback(WeatherGatewaySmartCallback _c) {
140 listPGWCallback.add(_c);
146 private void doCallbacks() {
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) {
155 //c.informationRetrieved(this);
156 c.informationRetrieved(this.getInchesPerWeek(), this.getWeatherZipCode(), this.getDaysToWaterOn(), this.getInchesPerMinute());
157 //} catch (RemoteException ex) {
158 // ex.printStackTrace();
160 // We have read data - set this back to false
162 weatherInfo.setNewDataAvailable(false);
168 * Simply return this.dInchesPerWeek
170 public double getInchesPerWeek() {
172 return weatherInfo.getInchesPerWeek();
176 * Simply return this.iWeatherZipCode
178 public int getWeatherZipCode() {
180 return weatherInfo.getWeatherZipCode();
184 * Simply return this.iDaysToWaterOn
186 public int getDaysToWaterOn() {
188 return weatherInfo.getDaysToWaterOn();
192 * Simply return this.dInchesPerMinute
194 public double getInchesPerMinute() {
196 return weatherInfo.getInchesPerMinute();
200 // public static void main(String[] args) throws UnknownHostException, RemoteException {
202 // @LocalRemote WeatherPhoneGateway wpg = new @LocalRemote WeatherPhoneGateway();