1 package iotcode.GPSPhoneGateway;
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 /** GPSPhoneGateway that uses IoTRemoteCall and PhoneInfo class
25 * to get information from a phone app
27 * @author Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
31 public class GPSPhoneGateway implements GPSGateway {
34 * PhoneGateway class properties
36 private PhoneInfo phoneInfo;
37 private IoTRemoteCall iotRemCall;
38 private List<GPSGatewaySmartCallback> listPGWCallback;
39 private AtomicBoolean doEnd;
40 private Thread callbackThread;
41 private Thread workerThread;
42 private IoTDeviceAddress iotDevAdd;
44 @config private IoTSet<IoTDeviceAddress> gps_address;
49 public GPSPhoneGateway() throws RemoteException {
58 Iterator it = gps_address.iterator();
59 iotDevAdd = (IoTDeviceAddress) it.next();
61 // iotDevAdd = new IoTDeviceAddress("192.168.2.100", 1234, 8000);
62 // } catch (Exception ex) {
64 System.out.println("Address: " + iotDevAdd.getCompleteAddress());
65 System.out.println("Source port: " + iotDevAdd.getSourcePortNumber());
66 System.out.println("Destination port: " + iotDevAdd.getDestinationPortNumber());
69 phoneInfo = new PhoneInfo();
70 listPGWCallback = new ArrayList<GPSGatewaySmartCallback>();
71 doEnd = new AtomicBoolean(false);
74 callbackThread = null;
79 * Start() function to start threads
84 // Launch IoTRemoteCall server in a separate thread
85 workerThread = new Thread(new Runnable() {
86 /* TODO: We revert back to HTTP because of new scheme for TLS context in Android 7
87 This disrupts the usual setting for self-signed certificate
88 See this link for more info: https://github.com/owntracks/android/issues/481
90 iotRemCall = new IoTRemoteCall(PhoneInfoInterface.class,
91 phoneInfo, iotDevAdd.getDestinationPortNumber(),
92 IoTDeviceAddress.getLocalHostAddress());
95 iotRemCall = new IoTRemoteCall(PhoneInfoInterface.class,
96 phoneInfo, iotDevAdd.getDestinationPortNumber());
100 System.out.println("GPSPhoneGateway: Worker thread started!");
102 callbackThread = new Thread(new Runnable() {
107 callbackThread.start();
108 System.out.println("GPSPhoneGateway: Callback thread started!");
112 * Stop() function to stop threads
118 callbackThread.join();
120 } catch (Exception e) {
128 public void registerCallback(GPSGatewaySmartCallback _c) {
129 listPGWCallback.add(_c);
135 private void doCallbacks() {
137 while (!doEnd.get()) {
139 for (GPSGatewaySmartCallback c : listPGWCallback) {
142 // Only call back if there is new data
143 if (phoneInfo.isNewRoomIDAvailable()) {
145 System.out.println("GPSPhoneGateway: new room ID available - call back!");
147 //c.newRoomIDRetrieved(this);
148 c.newRoomIDRetrieved(this.getRoomID());
149 //this.setNewRoomIDAvailable(false);
151 // Set back to false after reading
152 phoneInfo.setNewRoomIDAvailable(false);
154 } else if (phoneInfo.isNewRingStatusAvailable()) {
156 System.out.println("GPSPhoneGateway: new ring status available - call back!");
158 //c.newRingStatusRetrieved(this);
159 c.newRingStatusRetrieved(this.getRingStatus());
160 //this.setNewRingStatusAvailable(false);
162 // Set back to false after reading
163 phoneInfo.setNewRingStatusAvailable(false);
166 //} catch (RemoteException ex) {
167 // ex.printStackTrace();
174 * Simply return phoneInfo.iRoomIdentifier
176 public int getRoomID() {
178 return phoneInfo.getRoomID();
182 * Simply return phoneInfo.bRingStatus
184 public boolean getRingStatus() {
186 return phoneInfo.getRingStatus();
190 * Set phoneInfo.bNewRoomIDAvail
192 public void setNewRoomIDAvailable(boolean bValue) {
194 phoneInfo.setNewRoomIDAvailable(bValue);
198 * Set phoneInfo.bNewRingStatusAvail
200 public void setNewRingStatusAvailable(boolean bValue) {
202 phoneInfo.setNewRingStatusAvailable(bValue);
205 /* public static void main(String[] args) throws UnknownHostException, RemoteException {
207 @LocalRemote GPSPhoneGateway gpg = new @LocalRemote GPSPhoneGateway();