Add the ability to parse XML from WSI history API

This commit is contained in:
Samer Albahra
2015-07-01 19:57:01 -05:00
parent 664cc3666a
commit 712792679a
2 changed files with 31 additions and 2 deletions

View File

@@ -2,12 +2,14 @@
"name": "os-weather-service", "name": "os-weather-service",
"description": "OpenSprinkler Weather Service", "description": "OpenSprinkler Weather Service",
"version": "0.0.1", "version": "0.0.1",
"repository": "https://github.com/OpenSprinkler/Weather-Adjustments/",
"dependencies": { "dependencies": {
"dotenv": "^1.2.0", "dotenv": "^1.2.0",
"express": "^4.13.0", "express": "^4.13.0",
"grunt": "^0.4.5", "grunt": "^0.4.5",
"grunt-contrib-compress": "^0.13.0", "grunt-contrib-compress": "^0.13.0",
"grunt-contrib-jshint": "^0.11.2", "grunt-contrib-jshint": "^0.11.2",
"grunt-jscs": "^1.8.0" "grunt-jscs": "^1.8.0",
"xml2js": "^0.4.9"
} }
} }

View File

@@ -2,6 +2,7 @@
// Define regex filters to match against location // Define regex filters to match against location
var http = require( "http" ), var http = require( "http" ),
parseXML = require( "xml2js" ).parseString,
filters = { filters = {
gps: /^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/, gps: /^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/,
pws: /^(?:pws|icao):/, pws: /^(?:pws|icao):/,
@@ -45,6 +46,11 @@
return ( ( ( ( ( ( +ip[0] ) * 256 ) + ( +ip[1] ) ) * 256 ) + ( +ip[2] ) ) * 256 ) + ( +ip[3] ); 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 // Takes a PWS or ICAO location and resolves the GPS coordinates
function getPWSCoordinates( location, weatherUndergroundKey, callback ) { function getPWSCoordinates( location, weatherUndergroundKey, callback ) {
var url = "http://api.wunderground.com/api/" + weatherUndergroundKey + 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 // Calculates the resulting water scale using the provided weather data, adjustment method and options
function calculateWeatherScale( adjustmentMethod, adjustmentOptions, weather ) { function calculateWeatherScale( adjustmentMethod, adjustmentOptions, weather ) {