Adjustment to do initializationof secure cloud later than the sensor/ZigBee initializ...
[iot2.git] / benchmarks / Java / HomeSecurityController / HomeSecurityController.java
index c146c6f61e4190cae0ea02309672f256f19972ef..3cf6c366d9881ca4ccce1cf651e9a1696bc888e7 100644 (file)
@@ -43,7 +43,7 @@ public class HomeSecurityController implements SmartthingsSensorCallback, Smartt
        private static final int MOTION_TIME_THRESHOLD = 60;    // in seconds
        private static final int CAMERA_FPS = 15;
        private static final int CHECK_TIME_WAIT = 1;                   // in seconds
-       private static final int SECOND_TO_TURN_ON = 60;                // in seconds
+       private static final int SECOND_TO_TURN_ON = -1;                // in seconds
        private static final int SECOND_TO_TURN_OFF = 1;                // in seconds
        private static final int LOCK_DOOR = 0;
        private static final int UNLOCK_DOOR = 1;
@@ -135,34 +135,15 @@ public class HomeSecurityController implements SmartthingsSensorCallback, Smartt
                        // Get and init the IoTCloud server address
                        // Setup table
                        t1 = new Table(BASE_URL, PASSWORD, LOCAL_MACHINE_ID, LISTENING_PORT);
-                   t1.initTable();
-                       // Setup is done somewhere else, we just do rebuild()
-                       //t1.rebuild();
+                       t1.initTable();
                        System.out.println("DEBUG: Table initialized!");
                        // Initialize sensors!
-                       // TODO: Still deciding whether to initialize all devices here or inside each init method
                        int id = 0;
                        // Initialize alarms! One alarm for now
                        for(AlarmSmart alarm : alarmSet.values()) {
                                createKeyIoTCloud("alarm", NOT_ACTIVE);
                                System.out.println("DEBUG: Setting alarm to NOT-ACTIVE!");
                        }
-                       // TODO: Just use alarm for now as a status to cloud
-                       /*for(SmartthingsSensorSmart smartSensor : smartSensorsSet.values()) {
-                               
-                               createKeyIoTCloud("sensor" + Integer.toString(id++), NOT_ACTIVE);
-                               System.out.println("DEBUG: Setting sensor" + id + " to NOT-ACTIVE!");                           
-                       }
-                       // Initialize cameras! One camera for now...
-                       for(CameraSmart cam : camSet.values()) {
-                               createKeyIoTCloud("camera", NOT_ACTIVE);
-                               System.out.println("DEBUG: Setting camera to NOT-ACTIVE!");
-                       }
-                       int doorId = 0;
-                       for(SmartthingsActuatorSmart doorlock : doorlockSet.values()) {
-                               createKeyIoTCloud("doorlock" + Integer.toString(doorId), NOT_ACTIVE);
-                               System.out.println("DEBUG: Setting doorlock" + id + " to NOT-ACTIVE!");
-                       }*/
 
                } catch(Exception e) {
                        e.printStackTrace();
@@ -201,7 +182,7 @@ public class HomeSecurityController implements SmartthingsSensorCallback, Smartt
         *   @return [void] None.
         */
        private void updateIoTCloud(String key, IoTString val) {
-
+               // No key creation here!
                try {
                        IoTString iotKey = new IoTString(key);
                        t1.update();
@@ -252,10 +233,10 @@ public class HomeSecurityController implements SmartthingsSensorCallback, Smartt
                                System.out.println("DEBUG: Initialized smartthings sensor! ID: " + sensorId + " Room ID: " + rm.getRoomID());
                                senDetectStatus.put(sensorId, false);
                                System.out.println("DEBUG: Initialized sensor detection to false!");
+                               System.out.println("DEBUG: Now sensor ID is being set!");
                                // Initialize IoTCloud
-//                             createKeyIoTCloud("sensor" + Integer.toString(sensorId), NOT_ACTIVE);
-//                             System.out.println("DEBUG: Setting sensor" + sensorId + " to NOT-ACTIVE!");
                                sen.setId(sensorId++);
+                               System.out.println("DEBUG: Set sensor ID to: " + sensorId + "!");
                                sen.registerCallback(this);
                                System.out.println("DEBUG: Registered sensor callback!");
                        } catch (Exception e) {
@@ -296,10 +277,6 @@ public class HomeSecurityController implements SmartthingsSensorCallback, Smartt
 
                        // Initialize detection to false
                        camDetectStatus.put(cam, false);
-
-                       // Initialize IoTCloud
-//                     createKeyIoTCloud("camera", NOT_ACTIVE);
-//                     System.out.println("DEBUG: Setting camera to NOT-ACTIVE!");
                }
        }
 
@@ -313,18 +290,10 @@ public class HomeSecurityController implements SmartthingsSensorCallback, Smartt
                // Get and init the alarm (this single alarm set can serve multiple zones / rooms)
                Iterator alarmIt = alarmSet.iterator();
                AlarmSmart alm = (AlarmSmart) alarmIt.next();
-               // Initialize IoTCloud - only 1 alarm
-//             createKeyIoTCloud("alarm", NOT_ACTIVE);
-//             System.out.println("DEBUG: Setting alarm to NOT-ACTIVE!");
                // Initialize the alarm controller, do it here since it only needs to be done once per controller
                try {
                        alm.init();
                        System.out.println("DEBUG: Initialized alarm!");
-                       // TODO: Check that this initialization works for multiple times - might be that setZone() only works once!
-                       //for (RoomSmart room : roomSet.values()) {
-                       //      turnOffAlarms(room.getRoomID());
-                       //      System.out.println("DEBUG: Initialized alarm for room (turn off): " + room.getRoomID());
-                       //}
                } catch (Exception e) {
                        e.printStackTrace();
                }
@@ -347,9 +316,6 @@ public class HomeSecurityController implements SmartthingsSensorCallback, Smartt
                                System.out.println("DEBUG: Initialized doorlock! ID: " + doorlockId);
                                doorlockStatus.put(doorlockId, false);
                                System.out.println("DEBUG: Initialized doorlock status to false!");
-                               // Initialize IoTCloud
-//                             createKeyIoTCloud("doorlock" + Integer.toString(doorlockId), NOT_ACTIVE);
-//                             System.out.println("DEBUG: Setting doorlock to NOT-ACTIVE!");
                                doorlock.setId(doorlockId++);
                                doorlock.registerCallback(this);
                                System.out.println("DEBUG: Registered doorlock callback!");
@@ -409,11 +375,9 @@ public class HomeSecurityController implements SmartthingsSensorCallback, Smartt
                if(_activeValue) {
                        System.out.println("DEBUG: Sensor " + sensorId + " is detecting something: " + _activeValue);
                        senDetectStatus.put(_sensorId, true);
-                       //updateIoTCloud(sensor, ACTIVE);
                } else {
-                       //System.out.println("DEBUG: Sensor " + sensorId + " is not detecting something: " + _activeValue);
+                       System.out.println("DEBUG: Sensor " + sensorId + " is not detecting something: " + _activeValue);
                        senDetectStatus.put(_sensorId, false);
-                       //updateIoTCloud(sensor, NOT_ACTIVE);
                }
        }
 
@@ -430,11 +394,9 @@ public class HomeSecurityController implements SmartthingsSensorCallback, Smartt
                if(_activeValue) {
                        System.out.println("DEBUG: Actuator " + _sensorId + " is detecting something: " + _activeValue);
                        doorlockStatus.put(_sensorId, true);
-                       //updateIoTCloud(actuator, ACTIVE);
                } else {
                        //System.out.println("DEBUG: Actuator " + _sensorId + " is not detecting something: " + _activeValue);
                        doorlockStatus.put(_sensorId, false);
-                       //updateIoTCloud(actuator, NOT_ACTIVE);
                }
        }
 
@@ -470,15 +432,13 @@ public class HomeSecurityController implements SmartthingsSensorCallback, Smartt
                        System.out.println("DEBUG: Camera detected something!");
                        for(CameraSmart cam : cameras) {
                                camDetectStatus.put(cam, true);
-                               //updateIoTCloud("camera", ACTIVE);
                        }
                } else {
 
                        // No motion was detected
-                       //System.out.println("DEBUG: Camera didn't detect anything!");
+                       System.out.println("DEBUG: Camera didn't detect anything!");
                        for(CameraSmart cam : cameras) {
                                camDetectStatus.put(cam, false);
-                               //updateIoTCloud("camera", NOT_ACTIVE);
                        }
                }
        }
@@ -492,7 +452,7 @@ public class HomeSecurityController implements SmartthingsSensorCallback, Smartt
                // Get and init the alarm (this single alarm set can serve multiple zones / rooms)
                Iterator alarmIt = alarmSet.iterator();
                AlarmSmart alm = (AlarmSmart) alarmIt.next();
-               alm.setZone(zoneId, true, SECOND_TO_TURN_OFF);
+               alm.setZone(zoneId, true, SECOND_TO_TURN_ON);
                updateIoTCloud("alarm", ACTIVE);
        }
 
@@ -507,7 +467,7 @@ public class HomeSecurityController implements SmartthingsSensorCallback, Smartt
                Iterator alarmIt = alarmSet.iterator();
                AlarmSmart alm = (AlarmSmart) alarmIt.next();
                // Turn this alarm off indefinitely
-               alm.setZone(zoneId, false, SECOND_TO_TURN_ON);
+               alm.setZone(zoneId, false, SECOND_TO_TURN_OFF);
                updateIoTCloud("alarm", NOT_ACTIVE);
        }
 
@@ -526,7 +486,6 @@ public class HomeSecurityController implements SmartthingsSensorCallback, Smartt
                        int doorId = doorlock.getId();
                        System.out.println("DEBUG: Lock doorlock! ID: " + doorId);
                        doorlockStatus.put(doorId, true);
-                       //updateIoTCloud("doorlock" + doorId, ACTIVE);
                }
        }
 
@@ -573,7 +532,6 @@ public class HomeSecurityController implements SmartthingsSensorCallback, Smartt
                        for (SmartthingsSensorSmart sensor : roomSensorRelation.get(room)) {
 
                                // Get the right sensor and the right detection status (true or false)
-                               //System.out.println("ABOUT TO DETECT: Reading sensor: " + sensor.getId());
                                if (senDetectStatus.get(sensor.getId())) {
                                        zoneId = room.getRoomID();
                                        turnOnAlarms(zoneId);
@@ -639,20 +597,11 @@ public class HomeSecurityController implements SmartthingsSensorCallback, Smartt
         */
        public void init() {
 
-               // Initialize IoTCloud server
-               initIoTCloudServer();
-
                // Iterate over the set of rooms
                for (RoomSmart rm : roomSet.values()) {
 
                        // Init all Smartthings sensors
                        initSmartthingsSensors(rm);
-                       //try {
-                       //      Thread.sleep(5000);
-                       //} catch (Exception e) {
-                       //      e.printStackTrace();
-                       //}
-
                }
                // Init all doorlocks
                initDoorLocks();
@@ -663,6 +612,9 @@ public class HomeSecurityController implements SmartthingsSensorCallback, Smartt
                // Init all cameras
                initCameras();
 
+               // Initialize IoTCloud server
+               initIoTCloudServer();
+
                System.out.println("DEBUG: Initialized all devices! Now starting detection loop!");
 
                // Run the main loop that will keep checking the sensors and cameras periodically