51 lines
1.3 KiB
JavaScript
51 lines
1.3 KiB
JavaScript
/**
|
|
* Created by Martin on 31/03/2016.
|
|
*/
|
|
const Forecast = require('forecast.io');
|
|
const STRING = require('string');
|
|
const logger = require('log4js').getLogger('weather');
|
|
|
|
const forecastOptions = {
|
|
'APIKey': '9ad2a41d420f3cf4960571bb886f710c', 'units': 'uk2'
|
|
};
|
|
|
|
module.exports = {
|
|
'newDoGetWeather': doGetWeather
|
|
|
|
};
|
|
|
|
function doGetWeather() {
|
|
'use strict';
|
|
|
|
return new Promise((resolve, reject) => {
|
|
logger.info('New Retrieving weather..');
|
|
const j = {};
|
|
const forecast = new Forecast(forecastOptions);
|
|
forecast.get(55.95, -4.566667,
|
|
{'units': 'uk2'},
|
|
(err, res, data) => {
|
|
if (err)
|
|
return reject(err);
|
|
|
|
const tempMin = parseInt(data.daily.data[0].temperatureMin, 10);
|
|
const tempMax = parseInt(data.daily.data[0].temperatureMax, 10);
|
|
|
|
j.currently = data.currently.summary;
|
|
j.today = data.daily.data[0].summary;
|
|
j.later = data.daily.summary;
|
|
j.alerts = data.alerts || {};
|
|
j.data = data;
|
|
|
|
const fs = STRING(j.currently).endsWith('.') ? '' : '.';
|
|
if (tempMax === tempMin)
|
|
j.currently += `${fs } Around ${ tempMin.toString() } degrees.`;
|
|
|
|
else
|
|
j.currently += `${fs } Around ${ tempMin.toString() } to ${ tempMax.toString() } degrees.`;
|
|
|
|
// logger.debug(j);
|
|
return resolve(j);
|
|
});
|
|
});
|
|
}
|