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.*;
22 /** GPSPhoneGateway that uses IoTRemoteCall and PhoneInfo class
23 * to get information from a phone app
25 * @author Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
29 public class GPSPhoneGateway implements GPSGateway {
32 * PhoneGateway class properties
34 private PhoneInfo phoneInfo;
35 private IoTRemoteCall iotRemCall;
36 private List<GPSGatewaySmartCallback> listPGWCallback;
37 private AtomicBoolean doEnd;
38 private Thread callbackThread;
39 private Thread workerThread;
40 private IoTDeviceAddress iotDevAdd;
42 @config private IoTSet<IoTDeviceAddress> gps_address;
47 public GPSPhoneGateway() throws RemoteException {
56 Iterator it = gps_address.iterator();
57 iotDevAdd = (IoTDeviceAddress) it.next();
58 System.out.println("Address: " + iotDevAdd.getCompleteAddress());
59 System.out.println("Source port: " + iotDevAdd.getSourcePortNumber());
60 System.out.println("Destination port: " + iotDevAdd.getDestinationPortNumber());
63 phoneInfo = new PhoneInfo();
64 listPGWCallback = new ArrayList<GPSGatewaySmartCallback>();
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(PhoneInfoInterface.class,
85 phoneInfo, iotDevAdd.getDestinationPortNumber(),
86 IoTDeviceAddress.getLocalHostAddress());
89 iotRemCall = new IoTRemoteCall(PhoneInfoInterface.class,
90 phoneInfo, iotDevAdd.getDestinationPortNumber());
94 System.out.println("GPSPhoneGateway: Worker thread started!");
96 callbackThread = new Thread(new Runnable() {
101 callbackThread.start();
102 System.out.println("GPSPhoneGateway: Callback thread started!");
106 * Stop() function to stop threads
112 callbackThread.join();
114 } catch (Exception e) {
122 public void registerCallback(GPSGatewaySmartCallback _c) {
123 listPGWCallback.add(_c);
129 private void doCallbacks() {
131 while (!doEnd.get()) {
133 for (GPSGatewaySmartCallback c : listPGWCallback) {
135 // Only call back if there is new data
136 if (phoneInfo.isNewRoomIDAvailable()) {
138 System.out.println("GPSPhoneGateway: new room ID available - call back!");
140 //c.newRoomIDRetrieved(this);
141 c.newRoomIDRetrieved(this.getRoomID());
142 //this.setNewRoomIDAvailable(false);
144 // Set back to false after reading
145 phoneInfo.setNewRoomIDAvailable(false);
147 } else if (phoneInfo.isNewRingStatusAvailable()) {
149 System.out.println("GPSPhoneGateway: new ring status available - call back!");
151 c.newRingStatusRetrieved(this.getRingStatus());
153 // Set back to false after reading
154 phoneInfo.setNewRingStatusAvailable(false);
161 * Simply return phoneInfo.iRoomIdentifier
163 public int getRoomID() {
165 return phoneInfo.getRoomID();
169 * Simply return phoneInfo.bRingStatus
171 public boolean getRingStatus() {
173 return phoneInfo.getRingStatus();
177 * Set phoneInfo.bNewRoomIDAvail
179 public void setNewRoomIDAvailable(boolean bValue) {
181 phoneInfo.setNewRoomIDAvailable(bValue);
185 * Set phoneInfo.bNewRingStatusAvail
187 public void setNewRingStatusAvailable(boolean bValue) {
189 phoneInfo.setNewRingStatusAvailable(bValue);