diff --git a/package.json b/package.json index b415d89..be73cc4 100644 --- a/package.json +++ b/package.json @@ -2,12 +2,14 @@ "name": "os-weather-service", "description": "OpenSprinkler Weather Service", "version": "0.0.1", + "repository": "https://github.com/OpenSprinkler/Weather-Adjustments/", "dependencies": { "dotenv": "^1.2.0", "express": "^4.13.0", "grunt": "^0.4.5", "grunt-contrib-compress": "^0.13.0", "grunt-contrib-jshint": "^0.11.2", - "grunt-jscs": "^1.8.0" + "grunt-jscs": "^1.8.0", + "xml2js": "^0.4.9" } } diff --git a/routes/weather.js b/routes/weather.js index 0dd0aa6..256f2db 100644 --- a/routes/weather.js +++ b/routes/weather.js @@ -1,7 +1,8 @@ ( function() { // Define regex filters to match against location - var http = require( "http" ), + var http = require( "http" ), + parseXML = require( "xml2js" ).parseString, filters = { gps: /^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/, pws: /^(?:pws|icao):/, @@ -45,6 +46,11 @@ return ( ( ( ( ( ( +ip[0] ) * 256 ) + ( +ip[1] ) ) * 256 ) + ( +ip[2] ) ) * 256 ) + ( +ip[3] ); } + // Resolves the Month / Day / Year of a Date object + Date.prototype.toUSDate = function(){ + return ( this.getMonth() + 1 ) + "/" + this.getDate() + "/" + this.getFullYear(); + }; + // Takes a PWS or ICAO location and resolves the GPS coordinates function getPWSCoordinates( location, weatherUndergroundKey, callback ) { var url = "http://api.wunderground.com/api/" + weatherUndergroundKey + @@ -106,6 +112,27 @@ } ); } + // Retrieve the historical weather data for the provided location + function getYesterdayWeatherData( location, callback ) { + + // Get the API key from the environment variables + var WSI_HISTORY_KEY = process.env.WSI_HISTORY_KEY, + today = new Date(), + yesterday = new Date( today.getTime() - 1000 * 60 * 60 * 24 ), + + // Generate URL using WSI Cleaned History API in Imperial units showing daily average values + url = "http://cleanedobservations.wsi.com/CleanedObs.svc/GetObs?ID=" + WSI_HISTORY_KEY + + "&Lat=" + location[0] + "&Long=" + location[1] + + "&Req=davg&startdate=" + yesterday.toUSDate() + "&enddate=" + today.toUSDate() + "&TS=LST"; + + // Perform the HTTP request to retrieve the weather data + httpRequest( url, function( xml ) { + parseXML( xml, function ( err, result ) { + callback( result.WeatherResponse.WeatherRecords[0].WeatherData[0].$ ); + }); + } ); + } + // Calculates the resulting water scale using the provided weather data, adjustment method and options function calculateWeatherScale( adjustmentMethod, adjustmentOptions, weather ) {