Skip to content

Commit

Permalink
More clear error messaging on missing config file. Parse weather API …
Browse files Browse the repository at this point in the history
…data and sanitize / properly convert to metric if necessary
  • Loading branch information
imbrianj committed Oct 30, 2017
1 parent 4000be3 commit 95fc9e2
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fs.stat(configFile, function (err, data) {
cert;

if (err) {
console.log('\x1b[31mError\x1b[0m: No controllers found. Is your config file setup correctly?');
console.log('\x1b[31mError\x1b[0m: Config file could not be read. Ensure you edit the config/config.js or provide the correct path.');
process.exit(1);
}

Expand Down
2 changes: 1 addition & 1 deletion cache/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1509238927989
1509336842320
44 changes: 34 additions & 10 deletions devices/weather/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = (function () {
* @fileoverview Basic weather information, courtesy of Yahoo.
*/
return {
version : 20151207,
version : 20171029,

readOnly: true,

Expand Down Expand Up @@ -84,6 +84,29 @@ module.exports = (function () {
return Math.round((f - 32) / 1.8);
},

/**
* Accept a raw forecast object and celsius flag. Parse through, sanitizing
* and converting values as necessary.
*/
formatForecast : function (forecast, celsius) {
var util = require(__dirname + '/../../lib/sharedUtil').util,
formatted = [],
i = 0;

for (i; i < forecast.length; i += 1) {
formatted.push({
code : util.sanitize(forecast[i].code),
date : util.sanitize(forecast[i].date),
day : util.sanitize(forecast[i].day),
high : util.sanitize(celsius ? this.fToC(forecast[i].high) : parseInt(forecast[i].high, 10)),
low : util.sanitize(celsius ? this.fToC(forecast[i].low) : parseInt(forecast[i].low, 10)),
text : util.sanitize(forecast[i].text)
});
}

return formatted;
},

/**
* Accept sunrise and sunset times as deliverd from the API - and determine
* what the current sun phase is. Can either be "Day" or "Night".
Expand Down Expand Up @@ -147,7 +170,8 @@ module.exports = (function () {
});

response.once('end', function () {
var weatherData = {},
var util = require(__dirname + '/../../lib/sharedUtil').util,
weatherData = {},
errMessage = 'err',
temp,
data,
Expand All @@ -174,14 +198,14 @@ module.exports = (function () {
temp = that.fToC(temp);
}

weatherData = { 'city' : city.location.city,
'temp' : temp,
'text' : city.item.condition.text,
'humidity' : city.atmosphere.humidity,
'sunrise' : city.astronomy.sunrise,
'sunset' : city.astronomy.sunset,
'code' : city.item.condition.code,
'forecast' : city.item.forecast,
weatherData = { 'city' : util.sanitize(city.location.city),
'temp' : util.sanitize(parseInt(temp, 10)),
'text' : util.sanitize(city.item.condition.text),
'humidity' : util.sanitize(parseInt(city.atmosphere.humidity, 10)),
'sunrise' : util.sanitize(city.astronomy.sunrise),
'sunset' : util.sanitize(city.astronomy.sunset),
'code' : util.sanitize(parseInt(city.item.condition.code, 10)),
'forecast' : that.formatForecast(city.item.forecast, weather.celsius),
'phase' : that.findSunPhase(city.astronomy.sunrise, city.astronomy.sunset)
};
}
Expand Down
2 changes: 1 addition & 1 deletion js/combo.min.js

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions tests/unit/devices/weather/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,42 @@ exports.weatherControllerTest = {

test.deepEqual(weatherController.fToC(57), 14);

test.done();
},

formatForecast : function (test) {
'use strict';

var weatherController = require(__dirname + '/../../../../devices/weather/controller'),
forecast = [{
code : '10',
date : '30 Oct 2017',
day : 'Mon',
high : '58',
low : '44',
text : 'Partly Cloudy'
},
{
code : '11',
date : '31 Oct 2017',
day : 'Tue',
high : '55',
low : '40',
text : 'Spooky'
}],
results = weatherController.formatForecast(forecast, false),
metricResults = weatherController.formatForecast(forecast, true);

test.strictEqual(results[0].high, 58);
test.strictEqual(results[0].low, 44);
test.strictEqual(results[1].high, 55);
test.strictEqual(results[1].low, 40);

test.strictEqual(metricResults[0].high, 14);
test.strictEqual(metricResults[0].low, 7);
test.strictEqual(metricResults[1].high, 13);
test.strictEqual(metricResults[1].low, 4);

test.done();
}
};

0 comments on commit 95fc9e2

Please sign in to comment.