1 package IrrigationController;
3 // Standard Java Packages
6 import java.util.ArrayList;
7 import java.util.concurrent.locks.Lock;
8 import java.util.concurrent.locks.ReentrantLock;
10 import java.util.concurrent.ConcurrentHashMap;
14 import java.rmi.RemoteException;
15 import java.rmi.server.UnicastRemoteObject;
17 // IoT Driver Packages
18 import iotcode.interfaces.*;
20 // Checker annotations
21 //import iotchecker.qual.*;
23 /** Class LawnState that represents the state of the lawn, also help calculate if the lawn needs to be watered.
25 * @author Ali Younis <ayounis @ uci.edu>
30 public class LawnState extends UnicastRemoteObject implements MotionDetectionCallback, MoistureSensorCallback {
33 /*******************************************************************************************************************************************
37 *******************************************************************************************************************************************/
38 private static final long MAX_TIME_BETWEEN_WATERING_SESSIONS = 4 * 24 * 60 * 60; // 5 days
39 private static final int MAX_DAYS_RAINED_RECORD = 20;
40 private static final int RAINED_RECENTLY_DAYS_INTERVAL = 1;
41 private static final long TWENTY_FIVE_HOURS = 25 * 60 * 60;
42 private static final long TWO_HOURS = 2 * 60 * 60;
44 private static final long NEW_MOTION_THRESHOLD = 2 * 60; // 2 minutes
45 private static final long AMOUNT_OF_MOTION_FOR_ACTIVE = 60 * 60; // 1 hour
46 private static final long AMOUNT_OF_TIME_FOR_ACTIVE_TO_HOLD = 7 * 24 * 60 * 60; // 1 week
48 private static final double MOISTURE_LEVEL_FOR_NORMAL_WATERING = 25; // Percentage
49 private static final double MOISTURE_LEVEL_FOR_EMERGENCY_WATERING = 5; // Percentage
50 private static final double MOISTURE_LEVEL_FOR_NO_WATERING = 80; // Percentage
52 /*******************************************************************************************************************************************
56 *******************************************************************************************************************************************/
57 private boolean isInHibernationMode = false;
58 private Date lastTimeWatered = null;
59 private boolean didWaterSinceLastSchedualedDate = false;
60 private List<Date> daysRained = new ArrayList<Date>();
61 private int daysToWaterOn = 0;
62 private LawnSmart iotLawnObject;
63 private MotionDetection motionDetector;
64 private double inchesPerMinute = 0;
65 private double inchesPerWeek = 0;
66 private double timePerWatering = 0;
67 private double timePerWeek = 0;
68 private double timeWateredSoFar = 0;
69 private SprinklerSmart sprinkler;
71 private Date lastMotionDetectedTime = null;
72 private Date startOfThisMotion = null;
73 private Date lastUpdateDate = null;
74 private Lock mutex = new ReentrantLock();
75 private long totalMotionOnLawn = 0;
76 private long numberOfMotionsOnLawnToday = 0;
77 private boolean lawnIsActive = false;
78 private Date lawnBecameActiceDate = null;
79 private Map<Integer, Double> moistureSensorReadings =
80 new ConcurrentHashMap<Integer, Double>();
81 private Map<Integer, Date> moistureSensorUpdateTimes =
82 new ConcurrentHashMap<Integer, Date>();
85 // 0th bit = Monday, 1th bit = Tuesday ext
86 public LawnState(LawnSmart _l, int _daysToWaterOn, MotionDetection _mo,
87 double _inchesPerMinute, double _inchesPerWeek, SprinklerSmart _sprinkler,
88 int _zone, Set<MoistureSensorSmart> _moistureSensors) throws RemoteException {
90 daysToWaterOn = _daysToWaterOn;
91 inchesPerMinute = _inchesPerMinute;
92 inchesPerWeek = _inchesPerWeek;
93 sprinkler = _sprinkler;
96 // register the callback with self
98 _mo.registerCallback(this);
100 // register callback to self
101 for (MoistureSensorSmart sen : _moistureSensors) {
104 sen.registerCallback(this);
105 } catch (Exception e) {
110 // parse the days that we are going to water on
111 int numberOfDaysForWatering = 0;
112 for (int i = 0; i < 7; i++) {
113 if ((daysToWaterOn & (1 << i)) > 0) {
114 numberOfDaysForWatering++;
118 // calculate lawn watering water amounts
119 timePerWeek = _inchesPerWeek / _inchesPerMinute;
120 timePerWatering = timePerWeek / (double)numberOfDaysForWatering;
123 /*******************************************************************************************************************************************
127 *******************************************************************************************************************************************/
130 /** Method to update the lawn state, updates lawn activity state based on activity timeout
132 * @param _currentDate [Date], the current date and time.
134 * @return [void] None.
136 public void updateLawn(Date _currentDate) {
137 if (lastUpdateDate != null) {
139 // check if we already did an update today
140 if ((lastUpdateDate.getDate() == _currentDate.getDate())
141 && (lastUpdateDate.getMonth() == _currentDate.getMonth())
142 && (lastUpdateDate.getYear() == _currentDate.getYear())) {
147 lastUpdateDate = _currentDate;
149 // lawn was active at some time so check if it can be deemed inactive because
150 // time has passed and it has not been active in that time
151 if (lawnBecameActiceDate != null) {
152 long timeElapsed = (_currentDate.getTime() - lawnBecameActiceDate.getTime()) / 1000;
154 if (timeElapsed >= AMOUNT_OF_TIME_FOR_ACTIVE_TO_HOLD) {
155 lawnBecameActiceDate = null;
156 lawnIsActive = false;
161 // check activity of lawn
162 boolean isActiveLawn = false;
165 if (totalMotionOnLawn >= AMOUNT_OF_MOTION_FOR_ACTIVE) {
169 // reset motion counters
170 totalMotionOnLawn = 0;
171 numberOfMotionsOnLawnToday = 0;
173 } catch (Exception e) {
183 lawnBecameActiceDate = _currentDate;
188 /** Method to test if this lawn is active or not.
190 * @return [Boolean] lawn is active.
192 public boolean lawnHasSufficientMotion() {
197 /** Method to test if this lawn should be watered or not right now.
198 * Lawn urgently needs to be watered right now.
200 * @param _currentDate [Date], the current date and time.
202 * @return [Boolean] lawn does need watering.
204 public boolean needsWateringUrgently(Date _currentDate) {
206 // get difference between now and last time watered
207 long timeElapsed = (_currentDate.getTime() - lastTimeWatered.getTime()) / 1000;
209 // needs watering now urgently
210 if (timeElapsed >= MAX_TIME_BETWEEN_WATERING_SESSIONS) {
214 // calculate the average moisture readings of all the
215 // sensors in this lawn
216 double averageMoistureValue = getAverageMoistureReading();
217 System.out.println("DEBUG: Average moisture value: " + averageMoistureValue);
218 // is a valid average
219 if (averageMoistureValue != -1) {
220 // moisture is very low so we need to water now!
221 if (averageMoistureValue <= MOISTURE_LEVEL_FOR_EMERGENCY_WATERING) {
223 } else if (averageMoistureValue >= MOISTURE_LEVEL_FOR_NO_WATERING) {
224 // moisture is high so no need to water
233 /** Method to test if this lawn should be watered or not
235 * @param _currentDate [Date], the current date and time.
237 * @return [Boolean] lawn does need watering.
239 public boolean needsWatering(Date _currentDate) {
241 // only check if we have watered since the last date
242 if (didWaterSinceLastSchedualedDate) {
243 // get the day of the week from the date and convert it to be
244 // 0=Monday, 1=Sunday, ....
245 int dayOfWeek = _currentDate.getDay();
246 dayOfWeek = (dayOfWeek - 1) % 7;
248 // Calculate what we should mask out days to water byte to see if it is a 1
249 int mask = (1 << dayOfWeek);
252 int shouldWaterToday = daysToWaterOn & mask;
254 // if the post masked data is 0 then we should not water today since that bit was not set to 1
255 // do not water today
256 if (shouldWaterToday == 0) {
262 // it is a scheduled day so we need to water soon;
263 didWaterSinceLastSchedualedDate = false;
265 // check if it rained in the last little bit so there is no need to water this grass right now.
266 if (didRainRecently(_currentDate, RAINED_RECENTLY_DAYS_INTERVAL)) {
270 // The grass was never watered before so water now
271 if (lastTimeWatered == null) {
275 // calculate the average moisture readings of all the
276 // sensors in this lawn
277 double averageMoistureValue = getAverageMoistureReading();
279 // is a valid average
280 if (averageMoistureValue != -1) {
281 // moisture is low enough to need to water now
282 if (averageMoistureValue <= MOISTURE_LEVEL_FOR_NORMAL_WATERING) {
284 } else if (averageMoistureValue >= MOISTURE_LEVEL_FOR_NO_WATERING) {
285 // moisture is high so no need to water
290 // if got here then no condition says we should not water today so we should
291 // water the grass today
296 /** Method to get the date of the last time the lawn was watered
298 * @return [Date] date of last watering.
300 public Date getLastTimeWatered() {
301 return lastTimeWatered;
304 /** Method to keep track of the last few times it rained on this lawn
306 * @param _dateOfRain [Date], the date of the rain.
308 * @return [void] None.
310 public void rainedOnDate(Date _dateOfRain) {
312 // the grass was technically watered on this day
313 lastTimeWatered = _dateOfRain;
315 didWaterSinceLastSchedualedDate = true;
317 // it rained on this date
318 daysRained.add(_dateOfRain);
320 // only keep the last 20 days that it rained
321 if (daysRained.size() > 20) {
322 daysRained.remove(0);
328 /** Method to water lawn, calculates how much to water and sends water signal to controller
330 * @param _currentDate [Date], the current date and time.
332 * @return [void] None.
334 public void waterLawn(Date _currentDate) {
335 lastTimeWatered = _currentDate;
336 didWaterSinceLastSchedualedDate = true;
338 // get the day of the week from the date and convert it to be
339 // 0=Monday, 1=Sunday, ....
340 int dayOfWeek = _currentDate.getDay();
341 dayOfWeek = (dayOfWeek - 1) % 7;
344 // check if it is the last day to water for this week
345 boolean isLastDay = true;
346 for (int i = 6; i > dayOfWeek; i--) {
347 int mask = (1 << dayOfWeek);
349 int shouldWaterToday = daysToWaterOn & mask;
351 if (shouldWaterToday != 0) {
358 int secondsToWater = 0;
361 // last day of week to water so water the remaining amount
362 double minutesToWater = timePerWeek - timeWateredSoFar;
363 timeWateredSoFar = 0;
364 secondsToWater = (int)((double)(minutesToWater * 60));
368 // if it is not the last day then just water a normal amount
369 timeWateredSoFar += timePerWatering;
370 secondsToWater = (int)((double)(timePerWatering * 60));
374 System.out.println("DEBUG: We water the lawn!!! Zone: " + zone + " Seconds to water: " + secondsToWater);
375 sprinkler.setZone(zone, true, secondsToWater);
376 } catch (Exception e) {
382 /** Method callback from the motion detection callback interface, processes the motion
383 * to see how long the motion was and saves that motion.
385 * @param _md [MotionDetection], motion detector with the motion
387 * @return [void] None.
389 public void motionDetected(long timeStampOfLastMotion) {
391 Date currMotTime = new Date(timeStampOfLastMotion);
393 if (lastMotionDetectedTime == null) {
394 lastMotionDetectedTime = currMotTime;
397 if (startOfThisMotion == null) {
398 startOfThisMotion = currMotTime;
401 long timeElapsed = (currMotTime.getTime() - lastMotionDetectedTime.getTime()) / 1000;
403 if (timeElapsed >= NEW_MOTION_THRESHOLD) {
406 long motiontime = (lastMotionDetectedTime.getTime() - startOfThisMotion.getTime()) / 1000;
407 totalMotionOnLawn += motiontime;
408 numberOfMotionsOnLawnToday++;
411 } catch (Exception e) {
418 startOfThisMotion = currMotTime;
421 lastMotionDetectedTime = currMotTime;
425 /** Callback method for when a new moisture reading is available.
426 * Called when a new reading is ready by the sensor and the sensor
427 * can be checked for the frame data.
429 * @param _sensor [MoistureSensor] .
431 * @return [void] None.
433 public void newReadingAvailable(int sensorId, float moisture, long timeStampOfLastReading) {
435 moistureSensorReadings.put(sensorId, (double) moisture);
436 moistureSensorUpdateTimes.put(sensorId, new Date(timeStampOfLastReading));
440 /*******************************************************************************************************************************************
444 *******************************************************************************************************************************************/
446 /** Method to check if it rained recently in the near past.
448 * @param _numberOfDaysInPast [long], number of days in the past to check if it rained recently.
449 * @param _currentDate [Date], the current date and time.
451 * @return [boolean] weather it rained recently or not.
453 private boolean didRainRecently(Date _currentDate, long _numberOfDaysInPast) {
455 // it never rained before
456 if (daysRained.size() == 0) {
460 // convert the days to seconds for calculation
461 long numberOfSecondsInPast = _numberOfDaysInPast * 24 * 60 * 60;
463 // go through all the stored days that it rained on
464 for (Date d : daysRained) {
466 // check the difference time and convert to seconds.
467 long numberOfSecondsDifference = (_currentDate.getTime() - d.getTime()) / 1000;
469 // if it rained in the last specified time then return true
470 if (numberOfSecondsDifference < numberOfSecondsInPast) {
479 /** Method calculate the average moisture readings of the most recent moisture reading of each sensor
480 * if that reading is not stale
482 * @return [double] average value of moisture readings.
484 private double getAverageMoistureReading() {
486 Date currentTime = new Date();
488 int numberOfReadings = 0;
490 for (Integer sen : moistureSensorReadings.keySet()) {
492 // check the timestamp of the watering of the lawn
493 Date readingTimestamp = moistureSensorUpdateTimes.get(sen);
495 System.out.println("DEBUG: Sensor reading time stamp: " + readingTimestamp.getTime());
496 System.out.println("DEBUG: Current time stamp: " + currentTime.getTime());
497 System.out.println("Time elapsed: " + (currentTime.getTime() - readingTimestamp.getTime()) / 1000);
499 long timeElapsedSinceLastWatering = (currentTime.getTime() - readingTimestamp.getTime()) / 1000;
501 // if reading is old then dont use it since it is noise
502 if (timeElapsedSinceLastWatering > TWO_HOURS) {
508 total += moistureSensorReadings.get(sen);
510 System.out.println("DEBUG: Sensor reading value: " + moistureSensorReadings.get(sen) + " with total: " + total);
514 // if no readings were valid then return -1 so that we can signal that moisture cannot be used for now
515 if (numberOfReadings == 0) {
519 // return the calculated average of all the recent moisture readings
520 return total / (double)numberOfReadings;