Add local weather caching mechanism

This commit is contained in:
Samer Albahra
2015-07-02 19:19:56 -05:00
parent 199399ff5e
commit 66b4d1ccbd
6 changed files with 122 additions and 24 deletions

View File

@@ -2,6 +2,7 @@
var http = require( "http" ),
parseXML = require( "xml2js" ).parseString,
Cache = require( "../models/Cache" ),
// Define regex filters to match against location
filters = {
@@ -126,9 +127,20 @@
httpRequest( url, function( data ) {
try {
var weather = JSON.parse( data );
// Return the data to the callback function if successful
callback( JSON.parse( data ) );
location = location.join( "," );
Cache.findOne( { location: location }, function( err, record ) {
if ( record && record.hasOwnProperty( "yesterdayHumidity" ) ) {
weather.yesterdayHumidity = record.yesterdayHumidity;
}
// Return the data to the callback function if successful
callback( weather );
} );
updateCache( location, weather );
} catch (err) {
// Otherwise indicate the request failed
@@ -163,6 +175,28 @@
} );
}
// 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 ) {
// If a record is found update the data and save it
if ( record ) {
record.currentHumidityTotal += weather.observation.imperial.rh;
record.currentHumidityCount++;
record.save();
} else {
// If no cache record is found, generate a new one and save it
new Cache( {
currentHumidityTotal: weather.observation.imperial.rh,
currentHumidityCount: 1
} ).save();
}
} );
}
// Calculates the resulting water scale using the provided weather data, adjustment method and options
function calculateWeatherScale( adjustmentMethod, adjustmentOptions, weather ) {