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,
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})/
};
// 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";
@@ -27,11 +25,11 @@
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 ) {
// Generate URL for autocomplete request
var url = "http://autocomplete.wunderground.com/aq?h=0&query=" +
@@ -53,10 +51,10 @@
callback( false );
}
} );
}
}
// 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,
@@ -84,17 +82,17 @@
} );
updateCache( location, weather );
} catch (err) {
} catch ( err ) {
// Otherwise indicate the request failed
callback( false );
}
} );
}
}
/*
// Retrieve the historical weather data for the provided location
function getYesterdayWeatherData( location, callback ) {
// Retrieve the historical weather data for the provided location
function getYesterdayWeatherData( location, callback ) {
// Get the API key from the environment variables
var WSI_HISTORY_KEY = process.env.WSI_HISTORY_KEY,
@@ -116,10 +114,11 @@
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
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
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,
@@ -189,19 +188,19 @@
}
return -1;
}
}
// 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.
//
// 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.
//
// 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
// 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];
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 ) {
@@ -222,12 +221,12 @@
}
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
@@ -280,14 +279,14 @@
// 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
remoteAddress = remoteAddress.split(",")[0];
remoteAddress = remoteAddress.split( "," )[0];
// Parse weather adjustment options
try {
// Reconstruct JSON string from deformed controller output
adjustmentOptions = JSON.parse( "{" + adjustmentOptions + "}" );
} catch (err) {
} catch ( err ) {
// If the JSON is not valid, do not incorporate weather adjustment options
adjustmentOptions = false;
@@ -336,11 +335,11 @@
getWeatherData( location, finishRequest );
} );
}
};
};
// 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 = {
@@ -366,11 +365,11 @@
// If the HTTP request fails, return false
callback( false );
} );
}
}
// 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 ) {
// Match the provided time string against a regex for parsing
time = time.match( filters.time );
@@ -380,13 +379,13 @@
// Convert the timezone into the OpenSprinkler encoded format
minute = ( minute / 15 >> 0 ) / 4;
hour = hour + ( hour >=0 ? minute : -minute );
hour = hour + ( hour >= 0 ? minute : -minute );
return ( ( hour + 12 ) * 4 ) >> 0;
}
}
// 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 are parsed from string against a regex to identify the timezone
var sunrise = weather.observation.sunrise.match( filters.time ),
@@ -398,30 +397,29 @@
parseInt( sunrise[4] ) * 60 + parseInt( sunrise[5] ),
parseInt( sunset[4] ) * 60 + parseInt( sunset[5] )
];
}
}
// 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] );
}
}
/*
function f2c( temp ) {
function f2c( temp ) {
return ( temp - 32 ) * 5 / 9;
}
}
function mm2in( x ) {
function mm2in( x ) {
return x * 0.03937008;
}
}
function ft2m( x ) {
function ft2m( x ) {
return x * 0.3048;
}
}
// Resolves the Month / Day / Year of a Date object
function toUSDate( date ){
// Resolves the Month / Day / Year of a Date object
function toUSDate( date ){
return ( date.getMonth() + 1 ) + "/" + date.getDate() + "/" + date.getFullYear();
}
}
*/
} )();