silvrgit/lib/weather.js

56 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-03-08 21:52:21 +00:00
/**
* Created by marti on 14/02/2016.
*/
2017-03-22 15:48:36 +00:00
const http = require('http');
const request = require('request');
const Forecast = require('forecast.io');
const util = require('util');
const jsonfile = require('jsonfile');
const fs = require('fs');
const logger = require('log4js').getLogger();
const weather = require('openweather-apis');
weather.setAPPID('936a0ed9eb23b95cf08fc9f693c24264');
weather.setLang('en');
weather.setCity('Glasgow City');
2016-03-08 21:52:21 +00:00
2017-03-22 15:48:36 +00:00
const forecastOptions = {
'APIKey': '0657dc0d81c037cbc89ca88e383b6bbf',
'units': 'uk2'
2016-03-08 21:52:21 +00:00
};
const file = `${__dirname }/` + 'data.json';
2017-03-22 15:48:36 +00:00
2016-03-08 21:52:21 +00:00
function saveData(d) {
jsonfile.writeFileSync(file, d);
2016-03-08 21:52:21 +00:00
}
function doGetWeatherOutlook () {
logger.info('Retrieving weather..');
const j = {};
const forecast = new Forecast(forecastOptions);
forecast.get(55.8582846, -4.2593033, { 'units': 'uk2' }, (err, res, data) => {
if (err) throw err;
logger.debug(util.inspect(data));
saveData(data);
j.currently = data.currently.summary;
j.today = data.daily.data[0].summary;
j.later = data.daily.summary;
j.alerts = data.alerts || {};
});
}
2016-03-08 21:52:21 +00:00
function doGetOpenWeather() {
return new Promise((resolve, reject) => {
weather.getWeatherForecastForDays(5, function(err, wData) {
if (err)
return reject(err);
else
return resolve(wData);
});
});
}
2016-03-08 21:52:21 +00:00
module.exports.doGetWeatherOutlook = doGetWeatherOutlook;
module.exports.doGetOpenWeather = doGetOpenWeather;