Files
opensprinkler-weather/server.ts
2019-05-11 16:21:30 -04:00

55 lines
1.6 KiB
TypeScript

const packageJson = require( "../package.json" ),
express = require( "express" ),
weather = require( "./routes/weather.js" ),
local = require( "./routes/local.js" ),
cors = require( "cors" );
let host = process.env.HOST || "127.0.0.1",
port = process.env.PORT || 3000,
pws = process.env.PWS || "none",
app = express();
if ( !process.env.HOST || !process.env.PORT || !process.env.LOCAL_PWS ) {
require( "dotenv" ).load();
host = process.env.HOST || host;
port = process.env.PORT || port;
pws = process.env.PWS || pws;
}
// 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.getWateringData );
app.get( /(\d+)/, weather.getWateringData );
// Handle requests matching /weatherData
app.options( /weatherData/, cors() );
app.get( /weatherData/, cors(), weather.getWeatherData );
// Endpoint to stream Weather Underground data from local PWS
if ( pws === "WU" ) {
app.get( "/weatherstation/updateweatherstation.php", local.captureWUStream );
}
app.get( "/", function( req, res ) {
res.send( packageJson.description + " v" + packageJson.version );
} );
// Handle 404 error
app.use( function( req, res ) {
res.status( 404 );
res.send( "Error: Request not found" );
} );
// Start listening on the service port
app.listen( port, host, function() {
console.log( "%s now listening on %s:%s", packageJson.description, host, port );
if (pws !== "none" ) {
console.log( "%s now listening for local weather stream", packageJson.description );
}
} );
exports.app = app;
exports.pws = pws;