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 /** WeatherPhoneProxy that uses IoTRemoteCall and WeatherInfo class
22 * to get information from a phone app
24 * @author Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
28 public class WeatherPhoneGateway implements WeatherGateway {
31 * PhoneGateway class properties
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;
41 @config private IoTSet<IoTDeviceAddress> ph_address;
46 public WeatherPhoneGateway() {
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());
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);
68 callbackThread = null;
73 * Start() function to start threads
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
84 iotRemCall = new IoTRemoteCall(WeatherInfoInterface.class,
85 weatherInfo, iotDevAdd.getDestinationPortNumber(),
86 IoTDeviceAddress.getLocalHostAddress());
89 iotRemCall = new IoTRemoteCall(WeatherInfoInterface.class,
90 weatherInfo, iotDevAdd.getDestinationPortNumber());
94 System.out.println("DEBUG: Started IoTRemoteCall object!!!");
96 callbackThread = new Thread(new Runnable() {
101 callbackThread.start();
102 System.out.println("DEBUG: Do Callbacks!!!");
106 * Stop() function to stop threads
112 callbackThread.join();
114 } catch (Exception e) {
122 public void registerCallback(WeatherGatewaySmartCallback _c) {
123 listPGWCallback.add(_c);
129 private void doCallbacks() {
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());
139 // We have read data - set this back to false
140 weatherInfo.setNewDataAvailable(false);
146 * Simply return this.dInchesPerWeek
148 public double getInchesPerWeek() {
150 return weatherInfo.getInchesPerWeek();
154 * Simply return this.iWeatherZipCode
156 public int getWeatherZipCode() {
158 return weatherInfo.getWeatherZipCode();
162 * Simply return this.iDaysToWaterOn
164 public int getDaysToWaterOn() {
166 return weatherInfo.getDaysToWaterOn();
170 * Simply return this.dInchesPerMinute
172 public double getInchesPerMinute() {
174 return weatherInfo.getInchesPerMinute();