silvrgit/lib/weather.js
2017-09-28 00:17:07 +01:00

56 lines
1.4 KiB
JavaScript

/**
* Created by marti on 14/02/2016.
*/
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');
const forecastOptions = {
'APIKey': '0657dc0d81c037cbc89ca88e383b6bbf',
'units': 'uk2'
};
const file = `${__dirname }/` + 'data.json';
function saveData(d) {
jsonfile.writeFileSync(file, d);
}
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 || {};
});
}
function doGetOpenWeather() {
return new Promise((resolve, reject) => {
weather.getWeatherForecastForDays(5, function(err, wData) {
if (err)
return reject(err);
else
return resolve(wData);
});
});
}
module.exports.doGetWeatherOutlook = doGetWeatherOutlook;
module.exports.doGetOpenWeather = doGetOpenWeather;