Wrap code within a closure function
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
// Define regex filters to match against location
|
||||
var http = require( "http" ),
|
||||
( function() {
|
||||
|
||||
// Define regex filters to match against location
|
||||
var http = require( "http" ),
|
||||
filters = {
|
||||
gps: /^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/,
|
||||
pws: /^(?:pws|icao):/,
|
||||
@@ -7,9 +9,9 @@ var http = require( "http" ),
|
||||
time: /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})([+-])(\d{2})(\d{2})/
|
||||
};
|
||||
|
||||
// Generic HTTP request handler that parses the URL and uses the
|
||||
// native Node.js http module to perform the request
|
||||
function httpRequest( url, callback ) {
|
||||
// Generic HTTP request handler that parses the URL and uses the
|
||||
// native Node.js http module to perform the request
|
||||
function httpRequest( url, callback ) {
|
||||
url = url.match( filters.url );
|
||||
|
||||
var options = {
|
||||
@@ -35,16 +37,16 @@ function httpRequest( url, callback ) {
|
||||
// If the HTTP request fails, return false
|
||||
callback( false );
|
||||
} );
|
||||
}
|
||||
}
|
||||
|
||||
// Converts IP string to integer
|
||||
function ipToInt( ip ) {
|
||||
// Converts IP string to integer
|
||||
function ipToInt( ip ) {
|
||||
ip = ip.split( "." );
|
||||
return ( ( ( ( ( ( +ip[0] ) * 256 ) + ( +ip[1] ) ) * 256 ) + ( +ip[2] ) ) * 256 ) + ( +ip[3] );
|
||||
}
|
||||
}
|
||||
|
||||
// Takes a PWS or ICAO location and resolves the GPS coordinates
|
||||
function getPWSCoordinates( location, weatherUndergroundKey, callback ) {
|
||||
// Takes a PWS or ICAO location and resolves the GPS coordinates
|
||||
function getPWSCoordinates( location, weatherUndergroundKey, callback ) {
|
||||
var url = "http://api.wunderground.com/api/" + weatherUndergroundKey +
|
||||
"/conditions/forecast/q/" + encodeURIComponent( location ) + ".json";
|
||||
|
||||
@@ -58,11 +60,11 @@ function getPWSCoordinates( location, weatherUndergroundKey, callback ) {
|
||||
callback( false );
|
||||
}
|
||||
} );
|
||||
}
|
||||
}
|
||||
|
||||
// If location does not match GPS or PWS/ICAO, then attempt to resolve
|
||||
// location using Weather Underground autocomplete API
|
||||
function resolveCoordinates( location, callback ) {
|
||||
// If location does not match GPS or PWS/ICAO, then attempt to resolve
|
||||
// location using Weather Underground autocomplete API
|
||||
function resolveCoordinates( location, callback ) {
|
||||
var url = "http://autocomplete.wunderground.com/aq?h=0&query=" +
|
||||
encodeURIComponent( location );
|
||||
|
||||
@@ -72,11 +74,11 @@ function resolveCoordinates( location, callback ) {
|
||||
callback( [ data.RESULTS[0].lat, data.RESULTS[0].lon ] );
|
||||
}
|
||||
} );
|
||||
}
|
||||
}
|
||||
|
||||
// Accepts a time string formatted in ISO-8601 and returns the timezone.
|
||||
// The timezone output is formatted for OpenSprinkler Unified firmware.
|
||||
function getTimezone( time ) {
|
||||
// Accepts a time string formatted in ISO-8601 and returns the timezone.
|
||||
// The timezone output is formatted for OpenSprinkler Unified firmware.
|
||||
function getTimezone( time ) {
|
||||
time = time.match( filters.time );
|
||||
|
||||
var hour = parseInt( time[7] + time[8] ),
|
||||
@@ -86,10 +88,10 @@ function getTimezone( time ) {
|
||||
hour = hour + ( hour >=0 ? minute : -minute );
|
||||
|
||||
return ( ( hour + 12 ) * 4 ) >> 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Retrieve weather data to complete the weather request
|
||||
function getWeatherData( location, callback ) {
|
||||
// Retrieve weather data to complete the weather request
|
||||
function getWeatherData( location, callback ) {
|
||||
|
||||
// Get the API key from the environment variables
|
||||
var WSI_API_KEY = process.env.WSI_API_KEY,
|
||||
@@ -102,10 +104,10 @@ function getWeatherData( location, callback ) {
|
||||
httpRequest( url, function( data ) {
|
||||
callback( JSON.parse( data ) );
|
||||
} );
|
||||
}
|
||||
}
|
||||
|
||||
// Calculates the resulting water scale using the provided weather data, adjustment method and options
|
||||
function calculateWeatherScale( adjustmentMethod, adjustmentOptions, weather ) {
|
||||
// Calculates the resulting water scale using the provided weather data, adjustment method and options
|
||||
function calculateWeatherScale( adjustmentMethod, adjustmentOptions, weather ) {
|
||||
|
||||
// Calculate the average temperature
|
||||
var temp = ( weather.observation.imperial.temp_max_24hour + weather.observation.imperial.temp_min_24hour ) / 2,
|
||||
@@ -148,10 +150,10 @@ function calculateWeatherScale( adjustmentMethod, adjustmentOptions, weather ) {
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Function to return the sunrise and sunset times from the weather reply
|
||||
function getSunData( weather ) {
|
||||
// Function to return the sunrise and sunset times from the weather reply
|
||||
function getSunData( weather ) {
|
||||
|
||||
// Sun times must be converted from strings into date objects and processed into minutes from midnight
|
||||
var sunrise = weather.observation.sunrise.match( filters.time ),
|
||||
@@ -161,13 +163,13 @@ function getSunData( weather ) {
|
||||
parseInt( sunrise[4] ) * 60 + parseInt( sunrise[5] ),
|
||||
parseInt( sunset[4] ) * 60 + parseInt( sunset[5] )
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// 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.
|
||||
// California watering restriction prevents watering if precipitation over two days is greater
|
||||
// than 0.01" over the past 48 hours.
|
||||
function checkWeatherRestriction( adjustmentValue, 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.
|
||||
// California watering restriction prevents watering if precipitation over two days is greater
|
||||
// than 0.01" over the past 48 hours.
|
||||
function checkWeatherRestriction( adjustmentValue, weather ) {
|
||||
var californiaRestriction = ( adjustmentValue >> 7 ) & 1;
|
||||
|
||||
if ( californiaRestriction ) {
|
||||
@@ -180,12 +182,16 @@ function checkWeatherRestriction( adjustmentValue, weather ) {
|
||||
}
|
||||
|
||||
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
|
||||
exports.getWeather = function( req, res ) {
|
||||
// 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
|
||||
exports.getWeather = function( req, res ) {
|
||||
|
||||
// The adjustment method is encoded by the OpenSprinkler firmware and must be
|
||||
// parsed. This allows the adjustment method and the restriction type to both
|
||||
// be saved in the same byte.
|
||||
var adjustmentMethod = req.params[0] & ~( 1 << 7 ),
|
||||
adjustmentOptions = req.query.wto,
|
||||
location = req.query.loc,
|
||||
@@ -267,5 +273,6 @@ exports.getWeather = function( req, res ) {
|
||||
getWeatherData( location, finishRequest );
|
||||
} );
|
||||
}
|
||||
};
|
||||
};
|
||||
} )();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user