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