Remove weather data dependency

This commit is contained in:
Samer Albahra
2015-09-20 22:30:45 -05:00
parent 64cb5ce436
commit 6fe876cfc5
2 changed files with 36 additions and 8 deletions

View File

@@ -11,7 +11,9 @@
"dotenv": "^1.2.0", "dotenv": "^1.2.0",
"express": "^4.13.0", "express": "^4.13.0",
"grunt": "^0.4.5", "grunt": "^0.4.5",
"moment-timezone": "^0.4.0",
"mongoose": "^4.0.6", "mongoose": "^4.0.6",
"suncalc": "^1.6.0",
"xml2js": "^0.4.9" "xml2js": "^0.4.9"
}, },
"devDependencies": { "devDependencies": {

View File

@@ -1,6 +1,8 @@
var http = require( "http" ), var http = require( "http" ),
parseXML = require( "xml2js" ).parseString, parseXML = require( "xml2js" ).parseString,
Cache = require( "../models/Cache" ), Cache = require( "../models/Cache" ),
SunCalc = require( "suncalc" ),
moment = require( "moment-timezone" ),
// Define regex filters to match against location // Define regex filters to match against location
filters = { filters = {
@@ -29,7 +31,7 @@ function resolveCoordinates( location, callback ) {
if ( typeof data.RESULTS === "object" && data.RESULTS.length ) { if ( typeof data.RESULTS === "object" && data.RESULTS.length ) {
// If it is, reply with an array containing the GPS coordinates // If it is, reply with an array containing the GPS coordinates
callback( [ data.RESULTS[0].lat, data.RESULTS[0].lon ] ); callback( [ data.RESULTS[0].lat, data.RESULTS[0].lon ], moment().tz( data.RESULTS[0].tz ).utcOffset() );
} else { } else {
// Otherwise, indicate no data was found // Otherwise, indicate no data was found
@@ -462,14 +464,31 @@ exports.getWeather = function( req, res ) {
// Attempt to resolve provided location to GPS coordinates when it does not match // Attempt to resolve provided location to GPS coordinates when it does not match
// a GPS coordinate or Weather Underground location using Weather Underground autocomplete // a GPS coordinate or Weather Underground location using Weather Underground autocomplete
resolveCoordinates( location, function( result ) { resolveCoordinates( location, function( result, timezone ) {
if ( result === false ) { if ( result === false ) {
res.send( "Error: Unable to resolve location" ); res.send( "Error: Unable to resolve location" );
return; return;
} }
location = result; location = result;
getWeatherData( location, finishRequest ); getWeatherData( location, function( weather ) {
if ( !weather ) {
var tzOffset = getTimezone( timezone, "minutes" ),
// Calculate sunrise and sunset since Weather Underground does not provide it
sunData = SunCalc.getTimes( new Date(), location[0], location[1] );
sunData.sunrise.setUTCMinutes( sunData.sunrise.getUTCMinutes() + tzOffset );
sunData.sunset.setUTCMinutes( sunData.sunset.getUTCMinutes() + tzOffset );
weather = {
timezone: timezone,
sunrise: ( sunData.sunrise.getUTCHours() * 60 + sunData.sunrise.getUTCMinutes() ),
sunset: ( sunData.sunset.getUTCHours() * 60 + sunData.sunset.getUTCMinutes() )
};
}
finishRequest( weather );
} );
} ); } );
} }
}; };
@@ -528,12 +547,19 @@ function validateValues( keys, array ) {
// The timezone output is formatted for OpenSprinkler Unified firmware. // The timezone output is formatted for OpenSprinkler Unified firmware.
function getTimezone( time, format ) { function getTimezone( time, format ) {
// Match the provided time string against a regex for parsing var hour, minute, tz;
time = time.match( filters.time ) || time.match( filters.timezone );
var hour = parseInt( time[7] + time[8] ), if ( typeof time === "number" ) {
minute = parseInt( time[9] ), hour = Math.floor( time / 60);
tz; minute = time % 60;
} else {
// Match the provided time string against a regex for parsing
time = time.match( filters.time ) || time.match( filters.timezone );
hour = parseInt( time[7] + time[8] );
minute = parseInt( time[9] );
}
if ( format === "minutes" ) { if ( format === "minutes" ) {
tz = ( hour * 60 ) + minute; tz = ( hour * 60 ) + minute;