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() {
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
108 iotRemCall = new IoTRemoteCall(WeatherInfoInterface.class,
109 weatherInfo, iotDevAdd.getDestinationPortNumber(),
110 IoTDeviceAddress.getLocalHostAddress());
113 iotRemCall = new IoTRemoteCall(WeatherInfoInterface.class,
114 weatherInfo, iotDevAdd.getDestinationPortNumber());
117 workerThread.start();
118 System.out.println("DEBUG: Started IoTRemoteCall object!!!");
120 callbackThread = new Thread(new Runnable() {
125 callbackThread.start();
126 System.out.println("DEBUG: Do Callbacks!!!");
130 * Stop() function to stop threads
136 callbackThread.join();
138 } catch (Exception e) {
146 public void registerCallback(WeatherGatewaySmartCallback _c) {
147 listPGWCallback.add(_c);
153 private void doCallbacks() {
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) {
162 //c.informationRetrieved(this);
163 c.informationRetrieved(this.getInchesPerWeek(), this.getWeatherZipCode(), this.getDaysToWaterOn(), this.getInchesPerMinute());
164 //} catch (RemoteException ex) {
165 // ex.printStackTrace();
167 // We have read data - set this back to false
169 weatherInfo.setNewDataAvailable(false);
175 * Simply return this.dInchesPerWeek
177 public double getInchesPerWeek() {
179 return weatherInfo.getInchesPerWeek();
183 * Simply return this.iWeatherZipCode
185 public int getWeatherZipCode() {
187 return weatherInfo.getWeatherZipCode();
191 * Simply return this.iDaysToWaterOn
193 public int getDaysToWaterOn() {
195 return weatherInfo.getDaysToWaterOn();
199 * Simply return this.dInchesPerMinute
201 public double getInchesPerMinute() {
203 return weatherInfo.getInchesPerMinute();
207 // public static void main(String[] args) throws UnknownHostException, RemoteException {
209 // @LocalRemote WeatherPhoneGateway wpg = new @LocalRemote WeatherPhoneGateway();