From 58bc020f3408798c4510385090e2d37836d4d8a5 Mon Sep 17 00:00:00 2001 From: Samer Albahra Date: Thu, 30 Jul 2015 21:23:19 -0700 Subject: [PATCH] Move weather-based rain delay into it's own adjustment method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- routes/weather.js | 50 ++++++++++++++++++++++++++++++++++------------ test/expected.json | 3 +++ 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/routes/weather.js b/routes/weather.js index 12de6f0..b811ea7 100644 --- a/routes/weather.js +++ b/routes/weather.js @@ -272,23 +272,12 @@ function calculateWeatherScale( adjustmentMethod, adjustmentOptions, weather ) { // 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. // -// 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 // than 0.01" over the past 48 hours. 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; if ( californiaRestriction ) { @@ -303,6 +292,23 @@ function checkWeatherRestriction( adjustmentValue, weather ) { 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 // adjustment method which is encoded to also carry the watering // restriction and therefore must be decoded @@ -328,14 +334,31 @@ exports.getWeather = function( req, res ) { 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 ) ) { 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 = { scale: scale, + rd: rainDelay, tz: getTimezone( weather.timezone ), sunrise: weather.sunrise, sunset: weather.sunset, @@ -347,6 +370,7 @@ exports.getWeather = function( req, res ) { res.json( data ); } else { res.send( "&scale=" + data.scale + + "&rd=" + data.rd + "&tz=" + data.tz + "&sunrise=" + data.sunrise + "&sunset=" + data.sunset + diff --git a/test/expected.json b/test/expected.json index 63b8146..731f53a 100644 --- a/test/expected.json +++ b/test/expected.json @@ -2,6 +2,7 @@ "WSI": { "01002": { "scale": 83, + "rd": -1, "tz": 32, "sunrise": 324, "sunset": 1226, @@ -11,6 +12,7 @@ "WU": { "01002": { "scale": 61, + "rd": -1, "tz": 32, "sunrise": 324, "sunset": 1228, @@ -20,6 +22,7 @@ "noWeather": { "01002": { "scale": -1, + "rd": -1, "tz": 32, "sunrise": 324, "sunset": 1226,