Move weather-based rain delay into it's own adjustment method

The previous implementation caused the scale to change to zero for
user’s not using a weather adjustment method. This is not ideal since
the water scale will remain at 0% until there is user intervention.

Now it will be a user selectable method which uses a user-configurable
delay value which defaults to 24 hours.
This commit is contained in:
Samer Albahra
2015-07-30 21:23:19 -07:00
parent abb37cc2bb
commit 58bc020f34
2 changed files with 40 additions and 13 deletions

View File

@@ -272,23 +272,12 @@ function calculateWeatherScale( adjustmentMethod, adjustmentOptions, weather ) {
// Checks if the weather data meets any of the restrictions set by OpenSprinkler. // Checks if the weather data meets any of the restrictions set by OpenSprinkler.
// Restrictions prevent any watering from occurring and are similar to 0% watering level. // Restrictions prevent any watering from occurring and are similar to 0% watering level.
// //
// All queries will return a restrict flag if the current weather indicates rain. // All queries will return a scale of 0 if the current weather indicates rain.
// //
// California watering restriction prevents watering if precipitation over two days is greater // California watering restriction prevents watering if precipitation over two days is greater
// than 0.01" over the past 48 hours. // than 0.01" over the past 48 hours.
function checkWeatherRestriction( adjustmentValue, weather ) { function checkWeatherRestriction( adjustmentValue, weather ) {
// Define all the weather codes that indicate rain
var adverseCodes = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 35, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47 ],
adverseWords = [ "flurries", "sleet", "rain", "sleet", "snow", "tstorms" ];
if ( ( weather.iconCode && adverseCodes.indexOf( weather.iconCode ) !== -1 ) || ( weather.icon && adverseWords.indexOf( weather.icon ) !== -1 ) ) {
// If the current weather indicates rain, add a restrict flag to the weather script indicating
// the controller should not water.
return true;
}
var californiaRestriction = ( adjustmentValue >> 7 ) & 1; var californiaRestriction = ( adjustmentValue >> 7 ) & 1;
if ( californiaRestriction ) { if ( californiaRestriction ) {
@@ -303,6 +292,23 @@ function checkWeatherRestriction( adjustmentValue, weather ) {
return false; return false;
} }
// Checks if the weather indicates it is raining and returns a boolean representation
function checkRainStatus( weather ) {
// Define all the weather codes that indicate rain
var adverseCodes = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 35, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47 ],
adverseWords = [ "flurries", "sleet", "rain", "sleet", "snow", "tstorms" ];
if ( ( weather.iconCode && adverseCodes.indexOf( weather.iconCode ) !== -1 ) || ( weather.icon && adverseWords.indexOf( weather.icon ) !== -1 ) ) {
// If the current weather indicates rain, add a rain delay flag to the weather script indicating
// the controller should not water.
return true;
}
return false;
}
// API Handler when using the weatherX.py where X represents the // API Handler when using the weatherX.py where X represents the
// adjustment method which is encoded to also carry the watering // adjustment method which is encoded to also carry the watering
// restriction and therefore must be decoded // restriction and therefore must be decoded
@@ -328,14 +334,31 @@ exports.getWeather = function( req, res ) {
return; return;
} }
var scale = calculateWeatherScale( adjustmentMethod, adjustmentOptions, weather ); var scale = calculateWeatherScale( adjustmentMethod, adjustmentOptions, weather ),
rainDelay = -1;
// Check for any user-set restrictions and change the scale to 0 if the criteria is met
if ( checkWeatherRestriction( req.params[0], weather ) ) { if ( checkWeatherRestriction( req.params[0], weather ) ) {
scale = 0; scale = 0;
} }
// If any weather adjustment is being used, check the rain status
if ( adjustmentMethod > 0 && checkRainStatus( weather ) ) {
// If it is raining and the user has weather-based rain delay as the adjustment method then apply the specified delay
if ( adjustmentMethod === 2 ) {
rainDelay = ( adjustmentOptions && adjustmentOptions.hasOwnProperty( "r" ) ) ? adjustmentOptions.r : 1440;
} else {
// For any other adjustment method, apply a scale of 0 (as the scale will revert when the rain stops)
scale = 0;
}
}
var data = { var data = {
scale: scale, scale: scale,
rd: rainDelay,
tz: getTimezone( weather.timezone ), tz: getTimezone( weather.timezone ),
sunrise: weather.sunrise, sunrise: weather.sunrise,
sunset: weather.sunset, sunset: weather.sunset,
@@ -347,6 +370,7 @@ exports.getWeather = function( req, res ) {
res.json( data ); res.json( data );
} else { } else {
res.send( "&scale=" + data.scale + res.send( "&scale=" + data.scale +
"&rd=" + data.rd +
"&tz=" + data.tz + "&tz=" + data.tz +
"&sunrise=" + data.sunrise + "&sunrise=" + data.sunrise +
"&sunset=" + data.sunset + "&sunset=" + data.sunset +

View File

@@ -2,6 +2,7 @@
"WSI": { "WSI": {
"01002": { "01002": {
"scale": 83, "scale": 83,
"rd": -1,
"tz": 32, "tz": 32,
"sunrise": 324, "sunrise": 324,
"sunset": 1226, "sunset": 1226,
@@ -11,6 +12,7 @@
"WU": { "WU": {
"01002": { "01002": {
"scale": 61, "scale": 61,
"rd": -1,
"tz": 32, "tz": 32,
"sunrise": 324, "sunrise": 324,
"sunset": 1228, "sunset": 1228,
@@ -20,6 +22,7 @@
"noWeather": { "noWeather": {
"01002": { "01002": {
"scale": -1, "scale": -1,
"rd": -1,
"tz": 32, "tz": 32,
"sunrise": 324, "sunrise": 324,
"sunset": 1226, "sunset": 1226,