-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.js
50 lines (43 loc) · 2.48 KB
/
weather.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// weather.js
// call the packages we need
var request = require('request'); // https://github.com/mikeal/request
module.exports = {
lookupCurrentWeather: function (location, shouldicycle, callback) {
var url = 'http://api.openweathermap.org/data/2.5/weather?q=' + location + '&units=metric';
request({ uri : url, json : true }, function (error, response, body) {
// console.log('Weather for ' + location + ' ' + response.statusCode + ' ' + body.length);
if (error) return callback(error);
if (body.cod !== 200) {
return callback({ code: parseInt(body.cod, 10), message: body.message });
}
shouldicycle.city = body.name + ', ' + body.sys.country;
shouldicycle.temp = Math.round(body.main.temp);
shouldicycle.symbol = body.weather[0].icon;
shouldicycle.windDegree = Math.round(body.wind.deg);
shouldicycle.windDirection = degToCompass(shouldicycle.windDegree);
shouldicycle.windSpeed = Math.round(body.wind.speed);
return callback();
});
},
lookupAirQuality: function (airQuality, shouldicycle, callback) {
var url = 'http://api.erg.kcl.ac.uk/AirQuality/Hourly/MonitoringIndex/SiteCode=' + airQuality.toUpperCase() + '/Json';
request({ uri : url, json : true }, function (error, response, body) {
// console.log('Air quality for ' + airQuality + ' ' + response.statusCode + ' ' + JSON.stringify(body));
if (error) return callback(error);
if (!body) return callback({ code: 404, message: "Pollution " + airQuality + " not found." });
if (body.HourlyAirQualityIndex && body.HourlyAirQualityIndex.LocalAuthority && body.HourlyAirQualityIndex.LocalAuthority.Site && body.HourlyAirQualityIndex.LocalAuthority.Site.species) {
var site = body.HourlyAirQualityIndex.LocalAuthority.Site;
site.species.forEach(function(species) {
var pollution = { name: site['@SiteCode'], speciesCode: species['@SpeciesCode'], airQualityIndex: species['@AirQualityIndex'] };
shouldicycle.pollution.push(pollution);
});
}
return callback();
});
}
}
var degToCompass = function (num) {
var val = Math.floor((num / 22.5) + 0.5);
var arr = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"];
return arr[(val % 16)];
}