Remove unnecessary IIFE

https://stackoverflow.com/questions/21531329/are-node-js-modules-need-to
-be-wrapped-inside-the-module-pattern
This commit is contained in:
Samer Albahra
2015-07-03 01:10:59 -05:00
parent 69d4c9ca4d
commit 687e3c6ca7

View File

@@ -1,6 +1,4 @@
( function() { var http = require( "http" ),
var http = require( "http" ),
// parseXML = require( "xml2js" ).parseString, // parseXML = require( "xml2js" ).parseString,
Cache = require( "../models/Cache" ), Cache = require( "../models/Cache" ),
@@ -12,8 +10,8 @@
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})/
}; };
// 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";
@@ -27,11 +25,11 @@
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 ) {
// Generate URL for autocomplete request // Generate URL for autocomplete request
var url = "http://autocomplete.wunderground.com/aq?h=0&query=" + var url = "http://autocomplete.wunderground.com/aq?h=0&query=" +
@@ -53,10 +51,10 @@
callback( false ); callback( false );
} }
} ); } );
} }
// 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,
@@ -84,17 +82,17 @@
} ); } );
updateCache( location, weather ); updateCache( location, weather );
} catch (err) { } catch ( err ) {
// Otherwise indicate the request failed // Otherwise indicate the request failed
callback( false ); callback( false );
} }
} ); } );
} }
/* /*
// Retrieve the historical weather data for the provided location // Retrieve the historical weather data for the provided location
function getYesterdayWeatherData( location, callback ) { function getYesterdayWeatherData( location, callback ) {
// Get the API key from the environment variables // Get the API key from the environment variables
var WSI_HISTORY_KEY = process.env.WSI_HISTORY_KEY, var WSI_HISTORY_KEY = process.env.WSI_HISTORY_KEY,
@@ -116,10 +114,11 @@
callback( result.WeatherResponse.WeatherRecords[0].WeatherData[0].$ ); callback( result.WeatherResponse.WeatherRecords[0].WeatherData[0].$ );
}); });
} ); } );
} }
*/ */
// Update weather cache record in the local database
function updateCache( location, weather ) { // Update weather cache record in the local database
function updateCache( location, weather ) {
// Search for a cache record for the provided location // Search for a cache record for the provided location
Cache.findOne( { location: location }, function( err, record ) { Cache.findOne( { location: location }, function( err, record ) {
@@ -142,10 +141,10 @@
} }
} ); } );
} }
// 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,
@@ -189,19 +188,19 @@
} }
return -1; return -1;
} }
// 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 restrict flag 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 // 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]; 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 ];
if ( adverseCodes.indexOf( weather.observation.icon_code ) !== -1 ) { if ( adverseCodes.indexOf( weather.observation.icon_code ) !== -1 ) {
@@ -222,12 +221,12 @@
} }
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 // The adjustment method is encoded by the OpenSprinkler firmware and must be
// parsed. This allows the adjustment method and the restriction type to both // parsed. This allows the adjustment method and the restriction type to both
@@ -280,14 +279,14 @@
// X-Forwarded-For header may contain more than one IP address and therefore // X-Forwarded-For header may contain more than one IP address and therefore
// the string is split against a comma and the first value is selected // the string is split against a comma and the first value is selected
remoteAddress = remoteAddress.split(",")[0]; remoteAddress = remoteAddress.split( "," )[0];
// Parse weather adjustment options // Parse weather adjustment options
try { try {
// Reconstruct JSON string from deformed controller output // Reconstruct JSON string from deformed controller output
adjustmentOptions = JSON.parse( "{" + adjustmentOptions + "}" ); adjustmentOptions = JSON.parse( "{" + adjustmentOptions + "}" );
} catch (err) { } catch ( err ) {
// If the JSON is not valid, do not incorporate weather adjustment options // If the JSON is not valid, do not incorporate weather adjustment options
adjustmentOptions = false; adjustmentOptions = false;
@@ -336,11 +335,11 @@
getWeatherData( location, finishRequest ); getWeatherData( location, finishRequest );
} ); } );
} }
}; };
// 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 = {
@@ -366,11 +365,11 @@
// If the HTTP request fails, return false // If the HTTP request fails, return false
callback( false ); callback( false );
} ); } );
} }
// 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 ) {
// Match the provided time string against a regex for parsing // Match the provided time string against a regex for parsing
time = time.match( filters.time ); time = time.match( filters.time );
@@ -380,13 +379,13 @@
// Convert the timezone into the OpenSprinkler encoded format // Convert the timezone into the OpenSprinkler encoded format
minute = ( minute / 15 >> 0 ) / 4; minute = ( minute / 15 >> 0 ) / 4;
hour = hour + ( hour >=0 ? minute : -minute ); hour = hour + ( hour >= 0 ? minute : -minute );
return ( ( hour + 12 ) * 4 ) >> 0; return ( ( hour + 12 ) * 4 ) >> 0;
} }
// 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 are parsed from string against a regex to identify the timezone // Sun times are parsed from string against a regex to identify the timezone
var sunrise = weather.observation.sunrise.match( filters.time ), var sunrise = weather.observation.sunrise.match( filters.time ),
@@ -398,30 +397,29 @@
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] )
]; ];
} }
// 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] );
} }
/* /*
function f2c( temp ) { function f2c( temp ) {
return ( temp - 32 ) * 5 / 9; return ( temp - 32 ) * 5 / 9;
} }
function mm2in( x ) { function mm2in( x ) {
return x * 0.03937008; return x * 0.03937008;
} }
function ft2m( x ) { function ft2m( x ) {
return x * 0.3048; return x * 0.3048;
} }
// Resolves the Month / Day / Year of a Date object // Resolves the Month / Day / Year of a Date object
function toUSDate( date ){ function toUSDate( date ){
return ( date.getMonth() + 1 ) + "/" + date.getDate() + "/" + date.getFullYear(); return ( date.getMonth() + 1 ) + "/" + date.getDate() + "/" + date.getFullYear();
} }
*/ */
} )();