Remove weather data dependency
This commit is contained in:
@@ -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": {
|
||||||
|
|||||||
@@ -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 ) {
|
||||||
|
|
||||||
|
var hour, minute, tz;
|
||||||
|
|
||||||
|
if ( typeof time === "number" ) {
|
||||||
|
hour = Math.floor( time / 60);
|
||||||
|
minute = time % 60;
|
||||||
|
} else {
|
||||||
|
|
||||||
// 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.match( filters.timezone );
|
time = time.match( filters.time ) || time.match( filters.timezone );
|
||||||
|
|
||||||
var hour = parseInt( time[7] + time[8] ),
|
hour = parseInt( time[7] + time[8] );
|
||||||
minute = parseInt( time[9] ),
|
minute = parseInt( time[9] );
|
||||||
tz;
|
}
|
||||||
|
|
||||||
if ( format === "minutes" ) {
|
if ( format === "minutes" ) {
|
||||||
tz = ( hour * 60 ) + minute;
|
tz = ( hour * 60 ) + minute;
|
||||||
|
|||||||
Reference in New Issue
Block a user