Adjusting back into the original lengthy version of IrrigationController; for testing...
[iot2.git] / benchmarks / Java / IrrigationController / IrrigationController.java
index c06646ff0f87a9b608fcb8b894ab4da10f0786c9..a646ff1ab13e3858581c669d6b9ebce0db27dd10 100644 (file)
@@ -31,15 +31,10 @@ public class IrrigationController extends UnicastRemoteObject implements Weather
        **
        *******************************************************************************************************************************************/
        // private static final int NUMBER_OF_TIMES_PER_WEEK_TO_WATER = 2;
-       //TODO: Change these back to normal - this is just for testing to make it awake all the time
-//     private static final int TIME_HOURS_TO_WATER_GRASS = 7;         // 7 am
-       private static final int TIME_HOURS_TO_WATER_GRASS = 3;
-//     private static final int TIME_MINUTES_TO_WATER_GRASS = 30;      // 30 minutes
-       private static final int TIME_MINUTES_TO_WATER_GRASS = 30;
-//     private static final int TIME_TO_RECOVER_GRASS_FOR = 8 * 24 * 60 * 60;  // 8 days
-       private static final int TIME_TO_RECOVER_GRASS_FOR = 10;
-//     private static final int TIME_TO_HIBERNATE_GRASS_FOR = 30 * 24 * 60 * 60;               // 30 days
-       private static final int TIME_TO_HIBERNATE_GRASS_FOR = 10;
+       private static final int TIME_HOURS_TO_WATER_GRASS = 7;         // 7 am
+       private static final int TIME_MINUTES_TO_WATER_GRASS = 30;      // 30 minutes
+       private static final int TIME_TO_RECOVER_GRASS_FOR = 8 * 24 * 60 * 60;  // 8 days
+       private static final int TIME_TO_HIBERNATE_GRASS_FOR = 30 * 24 * 60 * 60;               // 30 days
        public static final int CAMERA_FPS = 15;                                                                                                        // In frames per second
 
 
@@ -147,12 +142,12 @@ public class IrrigationController extends UnicastRemoteObject implements Weather
                        // Seconds since the start of the day to start the watering
                        long secondsForWateringStart = (TIME_HOURS_TO_WATER_GRASS * 3600) + (TIME_MINUTES_TO_WATER_GRASS * 60);
 
-//                     System.out.println("beginingOfToday " + beginingOfToday);
-//                     System.out.println("secondsSinceStartOfDay " + secondsSinceStartOfDay);
-//                     System.out.println("secondsForWateringStart " + secondsForWateringStart);
+                       System.out.println("beginingOfToday " + beginingOfToday);
+                       System.out.println("secondsSinceStartOfDay " + secondsSinceStartOfDay);
+                       System.out.println("secondsForWateringStart " + secondsForWateringStart);
 
                        // check if the current time is within the start watering interval
-                       /*if ((secondsSinceStartOfDay < secondsForWateringStart) || (secondsSinceStartOfDay > (secondsForWateringStart + (60 * 60)))) {
+                       if ((secondsSinceStartOfDay < secondsForWateringStart) || (secondsSinceStartOfDay > (secondsForWateringStart + (60 * 60)))) {
                                System.out.println("Sleep for 10 minutes.. ");
                                try {
                                        //Thread.sleep(10 * 60 * 1000);                                         // sleep for 10 minutes
@@ -162,11 +157,11 @@ public class IrrigationController extends UnicastRemoteObject implements Weather
                                }
 
                                continue;
-                       }*/
+                       }
 
                        // check if we already checked if we should water today
                        // we only need to do this once per day
-                       /*if ((dayOfLastCheck == currentDate.getDate()) && (monthOfLastCheck == currentDate.getMonth())) {
+                       if ((dayOfLastCheck == currentDate.getDate()) && (monthOfLastCheck == currentDate.getMonth())) {
                                System.out.println("Sleep for 1 hour...");
                                try {
                                        Thread.sleep(60 * 60 * 1000);                                           // sleep for an hour
@@ -175,7 +170,7 @@ public class IrrigationController extends UnicastRemoteObject implements Weather
                                }
 
                                continue;
-                       }*/
+                       }
 
                        // we decided to check if we should water today so save the fact that we chose to water on this day
                        dayOfLastCheck = currentDate.getDate();
@@ -187,11 +182,11 @@ public class IrrigationController extends UnicastRemoteObject implements Weather
                        }
                        // check if we are in hibernation mode and do the correct loop action
                        if (isHibernationMode) {
-//                             System.out.println("Hibernation mode!");
+                               System.out.println("Hibernation mode!");
                                // If we are in hibernation mode then use the hibernation loop code
                                wateringHibernationLoop(currentDate);
                        } else {
-//                             System.out.println("Normal mode!");
+                               System.out.println("Normal mode!");
                                // Using the normal watering loop code
                                wateringNormalLoop(currentDate);
                        }
@@ -210,15 +205,6 @@ public class IrrigationController extends UnicastRemoteObject implements Weather
        public void informationRetrieved(double _inchesPerWeek, int _weatherZipCode, int _daysToWaterOn, double _inchesPerMinute) {
 
                System.out.println("DEBUG: Information is retrieved from phone!!!");
-               /*try {
-                       // get the parameters that the interface (phone app) reads from the user
-                       inchesPerWeek = _wgw.getInchesPerWeek();
-                       weatherZipCode = _wgw.getWeatherZipCode();
-                       daysToWaterOn = _wgw.getDaysToWaterOn();
-                       inchesPerMinute.add(_wgw.getInchesPerMinute());
-               } catch(RemoteException ex) {
-                       ex.printStackTrace();
-               }*/
 
                inchesPerWeek = _inchesPerWeek;
                weatherZipCode = _weatherZipCode;
@@ -264,11 +250,6 @@ public class IrrigationController extends UnicastRemoteObject implements Weather
                                e.printStackTrace();
                        }
                }
-               // TODO: Use a phone input interface later
-               //inchesPerWeek = 20.00;
-               //weatherZipCode = 92612;
-               //daysToWaterOn = 255;
-               //inchesPerMinute.add(1.50);
 
                System.out.println("DEBUG: inchesPerWeek: " + inchesPerWeek);
                System.out.println("DEBUG: weatherZipCode: " + weatherZipCode);
@@ -282,21 +263,17 @@ public class IrrigationController extends UnicastRemoteObject implements Weather
 
                // Setup the cameras, start them all and assign each one a motion detector
                for (CameraSmart cam : cameraSet.values()) {
-                       //try {
-                               // initialize the camera, might need to setup some stuff internally
-                               cam.init();
 
-                               // set the camera parameters.
-                               cam.setFPS(CAMERA_FPS);
-                               cam.setResolution(Resolution.RES_VGA);
+                       // initialize the camera, might need to setup some stuff internally
+                       cam.init();
 
-                               // Start the camera (example is start the HTTP stream if it is a network camera)
-                               cam.start();
-                               System.out.println("DEBUG: Init camera! " + cam.toString());
-                       //} catch (RemoteException e) {
-                       //      e.printStackTrace();
-                       //}
+                       // set the camera parameters.
+                       cam.setFPS(CAMERA_FPS);
+                       cam.setResolution(Resolution.RES_VGA);
 
+                       // Start the camera (example is start the HTTP stream if it is a network camera)
+                       cam.start();
+                       System.out.println("DEBUG: Init camera! " + cam.toString());
                }
 
                // counter so that we can match the lawn inches per min data with the specific lawn
@@ -314,12 +291,7 @@ public class IrrigationController extends UnicastRemoteObject implements Weather
                                Iterator camIt = cameras.iterator();
                                CameraSmart cam = (CameraSmart)camIt.next();
                                System.out.println("DEBUG: Registering callback to camera: " + cam.toString());
-                               //try {
-                                       // setup the callback
-                                       cam.registerCallback(mo);
-                               //} catch (RemoteException e) {
-                               //      e.printStackTrace();
-                               //}
+                               cam.registerCallback(mo);
                        }
 
                        // we also only need 1 sprinkler controller per lawn so grab the first one
@@ -330,6 +302,8 @@ public class IrrigationController extends UnicastRemoteObject implements Weather
                        // init the sprinkler controller, do it here since it only needs to be done once per controller
                        try {
                                spr.init();
+                               // Wait until sprinkler is active
+                               Thread.sleep(30000);
                        } catch (Exception e) {
                                e.printStackTrace();
                        }
@@ -368,8 +342,6 @@ public class IrrigationController extends UnicastRemoteObject implements Weather
 
                // get the weather data for the next little bit
                List<DayWeather> weatherData = weatherGrabber.getWeatherData();
-               // TODO: Replace this with the WeatherGrabber.getWeatherData() above
-//             List<DayWeather> weatherData = new ArrayList<DayWeather>();
 
                // Go through each lawn and check if we should water it and if we should, water it
                for (LawnState ls : lawns) {
@@ -390,7 +362,7 @@ public class IrrigationController extends UnicastRemoteObject implements Weather
                // if we are in recovery mode then run the recovery action
                // we are still in hibernation mode but we need to recover the grass
                if (isInHibernationRecoveryMode) {
-//                     System.out.println("DEBUG: Recovery mode!");
+                       System.out.println("DEBUG: Recovery mode!");
                        hibernationRecoveryLoop(_currentDate);
                        return;
                }
@@ -402,7 +374,7 @@ public class IrrigationController extends UnicastRemoteObject implements Weather
                        // start recovery mode
                        isInHibernationRecoveryMode = true;
                        hibernationRecoveryModeStartDate = null;
-//                     System.out.println("DEBUG: We enter recovery mode for the first time!");
+                       System.out.println("DEBUG: We enter recovery mode for the first time!");
                        // first cycle of recovery
                        hibernationRecoveryLoop(_currentDate);
                        return;
@@ -420,7 +392,7 @@ public class IrrigationController extends UnicastRemoteObject implements Weather
                        if (!lawnHasMotion) {
                                continue;
                        }
-//                     System.out.println("DEBUG: We water the lawn! (wateringHibernationLoop)");
+                       System.out.println("DEBUG: We water the lawn! (wateringHibernationLoop)");
                        // water specific lawn since it has motion
                        waterLawn(ls, _currentDate, weatherData);
                }
@@ -446,7 +418,7 @@ public class IrrigationController extends UnicastRemoteObject implements Weather
                // we have been in recovery mode long enough
                if (elapsedTime >= TIME_TO_RECOVER_GRASS_FOR) {
 
-//                     System.out.println("DEBUG: We have been in recovery mode long enough!");
+                       System.out.println("DEBUG: We have been in recovery mode long enough!");
                        // reset the recovery mode
                        isInHibernationRecoveryMode = false;
                        hibernationRecoveryModeStartDate = null;
@@ -468,7 +440,7 @@ public class IrrigationController extends UnicastRemoteObject implements Weather
                // Go through each lawn and check if we should water it and if we should, water it
                for (LawnState ls : lawns) {
 
-//                     System.out.println("DEBUG: We water the lawn! (hibernationRecoveryLoop)");
+                       System.out.println("DEBUG: We water the lawn! (hibernationRecoveryLoop)");
                        // water specific lawn since it has motion
                        waterLawn(ls, _currentDate, weatherData);
                }
@@ -489,10 +461,6 @@ public class IrrigationController extends UnicastRemoteObject implements Weather
                // check if today or tomorrow is a wet day
                boolean todayIsWetDay = _weatherData.get(0).getIsWetDay();
                boolean tomorrowIsWetDay = _weatherData.get(1).getIsWetDay();
-               // TODO: Remove this later - hack the values for now!!!
-//             boolean todayIsWetDay = false;
-//             boolean tomorrowIsWetDay = false;
-
                // lawn cannot wait anymore for water so water not
                boolean lawnNeedsWaterNow = _ls.needsWateringUrgently(_currentDate);
                if (lawnNeedsWaterNow) {
@@ -500,10 +468,9 @@ public class IrrigationController extends UnicastRemoteObject implements Weather
                        System.out.println("DEBUG: Is wet day? " + todayIsWetDay);
                        System.out.println("DEBUG: Tomorrow is wet day? " + tomorrowIsWetDay);
                        // if it is not going to rain today then water the lawn
-                       // TODO: Put this back to uncommented!!! Only for testing!!!
-//                     if (!todayIsWetDay) {
+                       if (!todayIsWetDay) {
                                _ls.waterLawn(_currentDate);
-//                     }
+                       }
                        return;
                }