Files
opensprinkler-weather/server.js
2015-07-02 19:19:56 -05:00

63 lines
1.9 KiB
JavaScript

var express = require( "express" ),
weather = require( "./routes/weather.js" ),
mongoose = require( "mongoose" ),
Cache = require( "./models/Cache" ),
CronJob = require( "cron" ).CronJob,
port = process.env.PORT || 3000;
app = express();
if ( !process.env.PORT ) {
require( "dotenv" ).load();
}
// Connect to local MongoDB instance
mongoose.connect( "localhost" );
// If the database connection cannot be established, throw an error
mongoose.connection.on( "error", function() {
console.error( "MongoDB Connection Error. Please make sure that MongoDB is running." );
} );
// Handle requests matching /weatherID.py where ID corresponds to the
// weather adjustment method selector.
// This endpoint is considered deprecated and supported for prior firmware
app.get( /weather(\d+)\.py/, weather.getWeather );
// Handle 404 error
app.use( function( req, res ) {
res.status( 404 );
res.send( "Error: Request not found" );
} );
// Start listening on the service port
var server = app.listen( port, "127.0.0.1", function() {
console.log( "OpenSprinkler Weather Service now listening on port %s", port );
} );
// Schedule a cronjob daily to consildate the weather cache data, runs daily
new CronJob( "0 * * * * *", function() {
// Find all records in the weather cache
Cache.find( {}, function( err, records ) {
// Cycle through each record
records.forEach( function( record ){
// If the record contains any unaveraged data, then process the record
if ( record.currentHumidityCount > 0 ) {
// Average the humidity by dividing the total over the total data points collected
record.yesterdayHumidity = record.currentHumidityTotal / record.currentHumidityCount;
// Reset the current humidity data for the new day
record.currentHumidityTotal = 0;
record.currentHumidityCount = 0;
// Save the record in the database
record.save();
}
} );
} );
}, null, true, "UTC" );