silvrgit/lib/today/weather.js

51 lines
1.6 KiB
JavaScript
Raw Normal View History

2016-03-31 16:12:25 +00:00
/**
* Created by Martin on 31/03/2016.
*/
2017-03-22 15:48:36 +00:00
const Forecast = require('forecast.io');
const STRING = require('string');
const logger = require('log4js').getLogger();
2016-03-31 16:12:25 +00:00
2017-03-22 15:48:36 +00:00
const forecastOptions = {
2016-09-09 13:21:31 +00:00
APIKey: '9ad2a41d420f3cf4960571bb886f710c', units: 'uk2'
2016-03-31 16:12:25 +00:00
};
module.exports = {
newDoGetWeather: function() {
'use strict';
return new Promise(function(resolve, reject) {
logger.info('New Retrieving weather..');
2017-03-22 15:48:36 +00:00
const j = {};
const forecast = new Forecast(forecastOptions);
2016-10-05 14:09:12 +00:00
forecast.get(55.95, -4.566667,
2016-03-31 16:12:25 +00:00
{units: 'uk2'},
function(err, res, data) {
if (err) {
return reject(err);
}
2017-03-22 15:48:36 +00:00
const tempMin = parseInt(data.daily.data[0].temperatureMin);
const tempMax = parseInt(data.daily.data[0].temperatureMax);
2016-03-31 16:12:25 +00:00
j.currently = data.currently.summary;
j.today = data.daily.data[0].summary;
j.later = data.daily.summary;
j.alerts = data.alerts || {};
j.data = data;
2017-03-22 15:48:36 +00:00
const fs = STRING(j.currently).endsWith('.') ? '' : '.';
if (tempMax === tempMin) {
2016-03-31 16:12:25 +00:00
j.currently += fs + ' Around ' + tempMin.toString() + ' degrees.';
}
else {
j.currently += fs + ' Around ' + tempMin.toString() + ' to ' + tempMax.toString() + ' degrees.';
}
// logger.debug(j);
return resolve(j);
});
});
}
2016-09-09 13:21:31 +00:00
};