From 9b4351ae64cbaae7c7a5c23eb6e10d82daa44d83 Mon Sep 17 00:00:00 2001 From: inversion-nl Date: Tue, 10 May 2016 10:59:45 +0200 Subject: [PATCH 01/13] Attempt 1 to solve api issues --- api.js | 2 +- settings/index.html | 26 +++++++++++++------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/api.js b/api.js index 040a9ae..41cd38f 100644 --- a/api.js +++ b/api.js @@ -26,8 +26,8 @@ module.exports = [ return Homey.log("API: Wunderground request error: " + response); } else { Homey.log("API: Weather response received"); + // Return weather request - Homey.log("temp: " + response.current_observation.temp_c) var temp = response.current_observation.temp_c; var city = response.current_observation.display_location.city; var country = response.current_observation.display_location.country; diff --git a/settings/index.html b/settings/index.html index c1ca196..ed70d6c 100644 --- a/settings/index.html +++ b/settings/index.html @@ -73,10 +73,10 @@

- + From c78353cc8853a8f92afd0bc83c34ee8ae012f955 Mon Sep 17 00:00:00 2001 From: Inversion-NL Date: Tue, 10 May 2016 22:15:13 +0200 Subject: [PATCH 04/13] Update README.md --- README.md | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index db93aa2..dc14c18 100644 --- a/README.md +++ b/README.md @@ -1,33 +1,40 @@ -## nl.inversion.wunderground -Weather Underground app for Homey +##Weather Underground app for Homey -# Features Want to trigger your blinds when it's hot outside? Want to close your windows screen when the wind picks up? +Want to be notified when the levels of UV become high risk? Weather Underground app helps you accomplish these! -# Flow triggers: +#Flow triggers * Temperature has changed * Humidity has changed * Temperature is above an certain degree of Celcius/Fahrenheit * Temperature is below an certain degree of Celcius/Fahrenheit -* Humidity is above an certain degree of Celcius/Fahrenheit -* Humidity is below an certain degree of Celcius/Fahrenheit +* Humidity is above an certain percentage +* Humidity is below an certain percentage * UV is above an certain threshold * UV is below an certain threshold +* Wind is above an certain speed +* Wind is below an certain speed +* Wind gust is above an certain speed +* Wind gust is below an certain speed -# Flow conditions: +#Flow conditions * Temperature is above an certain degree of Celcius/Fahrenheit * Temperature is below an certain degree of Celcius/Fahrenheit * Humidity is above an certain degree of Celcius/Fahrenheit * Humidity is below an certain degree of Celcius/Fahrenheit * UV is above an certain threshold * UV is below an certain threshold +* Wind is above an certain speed +* Wind is below an certain speed +* Wind gust is above an certain speed +* Wind gust is below an certain speed -# Insights: +#Insights * Temperature * Humidity * Feels like @@ -42,10 +49,21 @@ Weather Underground app helps you accomplish these! * Precepation for today -# Future functions: +#Settings +* You can use your own API key, (available here: https://www.wunderground.com/weather/api it's free!) so you can update up to every 10 minutes. +If you don't have a key the app uses my key and will update every 60 minutes. +* Use Homey's location or a custom location +* Celsius and Fahrenheit support + + +#Future functions * Read Weather forecast * Trigger/condition on current weather condition (Cloudy, Raining etc) * Trigger on weather alerts * Average/high/low temperature on this date * Triggers and conditions for precipation 1hr/today -* Much more! \ No newline at end of file +* Much more! + + +#Source +https://github.com/Inversion-NL/nl.inversion.wunderground From 3afe2b44078eefca1af0133aff0e94e291d7c094 Mon Sep 17 00:00:00 2001 From: inversion-nl Date: Tue, 10 May 2016 23:39:45 +0200 Subject: [PATCH 05/13] Fix TypeError: Cannot read property 'relative_humidity' of undefined at null. (/app.js:347:59) --- app.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/app.js b/app.js index f151b8a..313e82f 100644 --- a/app.js +++ b/app.js @@ -404,11 +404,9 @@ var self = { // Get weather data wunderground.conditions().request(address, function(err, response) { - if (err) { - // Catch error - Homey.log("Wunderground request error: " + response); - return Homey.error(response); - } else { + + if (!err && response) { + // Cut % sign var hum = response.current_observation.relative_humidity; var hum_float = parseFloat(hum.substr(0, (hum.length -1))); @@ -549,6 +547,11 @@ var self = { self.addInsightsEntry("precip_1hr", weatherData.precip_1hr); self.addInsightsEntry("uv", weatherData.uv); self.addInsightsEntry("visibility", weatherData.visibility); + + } else { + // Catch error + Homey.log("Wunderground request error: " + response); + return Homey.error(response); } } ) From 17acd062c7bd7bd89e81c5573f939bb631e383ef Mon Sep 17 00:00:00 2001 From: inversion-nl Date: Wed, 11 May 2016 13:22:04 +0200 Subject: [PATCH 06/13] Add support for auto units Fix bug in settings when no api key specified Add alert to settings when testing successfull --- app.js | 37 +++++++++++++++++++------------------ locales/en.json | 1 + locales/nl.json | 1 + settings/index.html | 15 ++++++++++++++- 4 files changed, 35 insertions(+), 19 deletions(-) diff --git a/app.js b/app.js index 313e82f..a853d78 100644 --- a/app.js +++ b/app.js @@ -1,13 +1,12 @@ "use strict"; var Wunderground = require('wundergroundnode'); -var locale = Homey.manager('i18n').getLanguage(); var wunderground; var defaultUpdateTime = 60; var maxLocationGetTries = 3; -var insightsLogs = - [ +var insightsLogs = +[ "temp", "hum", "feelslike", @@ -22,8 +21,8 @@ var insightsLogs = "visibility" ]; -var units_imperial = false; -var units_metric = true; + + var interval; var update_frequenty = defaultUpdateTime; var locationGetCounter = 1; @@ -58,7 +57,6 @@ var self = { Homey.log("Initializing Weather Underground"); Homey.log(""); - Homey.log("Locale: " + locale); self.checkInsightsLogs(); self.checkSettings(); @@ -115,13 +113,15 @@ var self = { // Check if user provided a key in settings var myKey = Homey.manager('settings').get('wundergroundKey'); - units_imperial = Homey.manager('settings').get('units_imperial'); - units_metric = Homey.manager('settings').get('units_metric'); - Homey.log("units_imperial: " + units_imperial); - Homey.log("units_metric: " + units_metric); + var units_metric = Homey.manager('settings').get('units_metric'); + var units_imperial = Homey.manager('settings').get('units_imperial'); + var units_auto = Homey.manager('settings').get('units_auto'); + var homey_units = Homey.manager('i18n').getUnits(); - if (!value_exist(units_imperial)) units_imperial = false; - if (!value_exist(units_metric)) units_metric = true; + if (units_auto && !value_exist(homey_units) && homey_units != "") { + if (homey_units == 'metric') units_metric = true; + else units_metric = false; + } else units_metric = true; var usePersonalKey = false; if (!value_exist(myKey) || myKey == "") { @@ -158,14 +158,11 @@ var self = { } // Set default values - var country = 'Netherlands' - var city = 'Amsterdam' - address = country + '/' + city; var autolocation = true; // Get user settings - country = Homey.manager('settings').get('country'); - city = Homey.manager('settings').get('city'); + var country = Homey.manager('settings').get('country'); + var city = Homey.manager('settings').get('city'); autolocation = Homey.manager('settings').get('autolocation'); // Check user settings @@ -197,6 +194,7 @@ var self = { locationGetCounter = 0; } } else if (value_exist(country) && value_exist(city) && country != "" && city != "") { + address = "Netherlands/Amsterdam" Homey.log("Using country and city for location"); address = country + '/' + city; } else { @@ -949,14 +947,17 @@ function testWU(callback, args) { var Wunderground = require('wundergroundnode'); var wundergroundKey = args.body.wundergroundKey; - var wunderground = new Wunderground(wundergroundKey); var address = "Netherlands/Amsterdam"; if (wundergroundKey == "" || wundergroundKey == null) { Homey.log("Weather underground key is empty, using Inversion key"); wundergroundKey = Homey.env.WUNDERGROUND_KEY; + } else { + Homey.log('Using user Weather Underground key'); } + var wunderground = new Wunderground(wundergroundKey); + // Get weather data wunderground.conditions().request(address, function(err, response) { diff --git a/locales/en.json b/locales/en.json index 99b64bb..291c0d5 100644 --- a/locales/en.json +++ b/locales/en.json @@ -11,6 +11,7 @@ "location" : { "location" : "Location", + "units_auto" : "Automatic", "units_label" : "Units", "units_metric" : "Metric", "units_imperial" : "Imperial", diff --git a/locales/nl.json b/locales/nl.json index 84637e9..ff2fcf1 100644 --- a/locales/nl.json +++ b/locales/nl.json @@ -11,6 +11,7 @@ "location" : { "location" : "Locatie", + "units_auto" : "Automatisch", "units_label" : "Eenheden", "units_metric" : "Metrisch", "units_imperial" : "Imperiaal", diff --git a/settings/index.html b/settings/index.html index f523667..244c7bb 100644 --- a/settings/index.html +++ b/settings/index.html @@ -29,7 +29,10 @@


- + + +
+
@@ -128,6 +131,14 @@

} }) + Homey.get('units_auto', function(err, units_auto) { + if (err) { + Homey.error(err) + } else { + document.getElementById('units_auto').checked = units_auto + } + }) + Homey.get('units_metric', function(err, units_metric) { if (err) { Homey.error(err) @@ -159,6 +170,7 @@

Homey.set('wundergroundKey', document.getElementById('wundergroundKey').value); Homey.set('units_imperial', document.getElementById('units_imperial').checked); Homey.set('units_metric', document.getElementById('units_metric').checked); + Homey.set('units_auto', document.getElementById('units_auto').checked); setTimeout(function() { button_save.disabled = false; @@ -194,6 +206,7 @@

var weather_string = "It's " + temp + " degrees Celsius in " + city + ", " + country; document.getElementById('testresult').innerHTML = weather_string; + Homey.alert('Settings successfully tested. Please use the save button to save your settings.') } else { document.getElementById('testresult').innerHTML = "Error fetching weather data with the provided API key."; return Homey.alert('Error fetching weather data with the provided API key.'); From 0b5bae2bfcddea0c4a5096730a1fa429c5f5d560 Mon Sep 17 00:00:00 2001 From: inversion-nl Date: Wed, 11 May 2016 22:08:33 +0200 Subject: [PATCH 07/13] Add some logging --- app.js | 290 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 148 insertions(+), 142 deletions(-) diff --git a/app.js b/app.js index a853d78..2415be3 100644 --- a/app.js +++ b/app.js @@ -5,6 +5,7 @@ var wunderground; var defaultUpdateTime = 60; var maxLocationGetTries = 3; +var units_metric; var insightsLogs = [ "temp", @@ -62,6 +63,7 @@ var self = { self.checkSettings(); // Listen for triggers and conditions + Homey.log("Registering trigger and condition listeners") Homey.manager('flow').on('trigger.temp_above', self.tempAbove); Homey.manager('flow').on('condition.temp_above', self.tempAbove); Homey.manager('flow').on('trigger.temp_below', self.tempBelow); @@ -88,6 +90,7 @@ var self = { Homey.manager('flow').on('condition.windgust_below', self.windgustBelow); // Listen for changes in settings + Homey.log("Registering settings listener") Homey.manager('settings').on('set', self.settingsChanged); // Get location @@ -113,7 +116,7 @@ var self = { // Check if user provided a key in settings var myKey = Homey.manager('settings').get('wundergroundKey'); - var units_metric = Homey.manager('settings').get('units_metric'); + units_metric = Homey.manager('settings').get('units_metric'); var units_imperial = Homey.manager('settings').get('units_imperial'); var units_auto = Homey.manager('settings').get('units_auto'); var homey_units = Homey.manager('i18n').getUnits(); @@ -403,155 +406,158 @@ var self = { // Get weather data wunderground.conditions().request(address, function(err, response) { - if (!err && response) { - - // Cut % sign - var hum = response.current_observation.relative_humidity; - var hum_float = parseFloat(hum.substr(0, (hum.length -1))); - - // Use correct user units - if (units_metric) { - var temp = parseFloat(response.current_observation.temp_c); - var feelslike = parseFloat(response.current_observation.feelslike_c); - var dewpoint = parseFloat(response.current_observation.dewpoint_c); - var pressure = parseFloat(response.current_observation.pressure_mb); - var wind = parseFloat(response.current_observation.wind_kph); - var wind_gust = parseFloat(response.current_observation.wind_gust_kph); - var visibility = parseFloat(response.current_observation.visibility_km); - var precip_1hr = parseFloat(response.current_observation.precip_1hr_metric); - var precip_today = parseFloat(response.current_observation.precip_today_metric); - } else { - var temp = parseFloat(response.current_observation.temp_f); - var feelslike = parseFloat(response.current_observation.feelslike_f); - var dewpoint = parseFloat(response.current_observation.dewpoint_f); - var pressure = parseFloat(response.current_observation.pressure_in); - var wind = parseFloat(response.current_observation.wind_mph); - var wind_gust = parseFloat(response.current_observation.wind_gust_mph); - var visibility = parseFloat(response.current_observation.visibility_mi); - var precip_1hr = parseFloat(response.current_observation.precip_1hr_in); - var precip_today = parseFloat(response.current_observation.precip_today_in); - } - - // Reset values they where not a number or below zero - if (precip_1hr == "NaN") precip_1hr = 0; - if (precip_today == "NaN") precip_today = 0; - var uv = parseFloat(response.current_observation.UV); - if (uv < 0) uv = 0; - - weatherData = { - city: response.current_observation.display_location.city, - country: response.current_observation.display_location.country, - weather_descr: response.current_observation.weather, - relative_humidity: hum_float, - observation_epoch: response.current_observation.observation_epoch, - wind_degrees: parseFloat(response.current_observation.wind_degrees), - wind_dir: response.current_observation.wind_dir, - uv: uv, - temp: temp, - feelslike: feelslike, - dewpoint: dewpoint, - pressure: pressure, - wind: wind, - wind_gust: wind_gust, - visibility: visibility, - precip_1hr: precip_1hr, - precip_today: precip_today - }; - - Homey.log("Current time: " + new Date()); - Homey.log("Observation time: " + epochToString(weatherData.observation_epoch)); - Homey.log("Weather data:"); - Homey.log(weatherData); - - // Temperature triggers and conditions - if (value_exist(weatherData.temp)) { - - // Determine if the temp has changed - if (!value_exist(oldTemp)){ - Homey.log("No oldTemp value exists, maybe it's the first start of app"); - // First time update after reboot/install - oldTemp = weatherData.temp; - } else if (diff(oldTemp, weatherData.temp) >= 1) { - // Only trigger when difference is equal or more then 1 degree - Homey.log("oldTemp: " + oldTemp + " temp: " + weatherData.temp); - oldTemp = weatherData.temp; - self.tempChanged(weatherData.temp, weatherData.relative_humidity, weatherData.weather_descr); - } - - // Start trigger - self.tempAboveBelow(weatherData.temp, weatherData.relative_humidity, weatherData.weather_descr); - } else { - // No temperature data available! - Homey.log("Temperature is undefined!") + Homey.log('err:', err); + Homey.log('response:'. response); + + if (!err && response) { + + // Cut % sign + var hum = response.current_observation.relative_humidity; + var hum_float = parseFloat(hum.substr(0, (hum.length -1))); + + // Use correct user units + if (units_metric) { + var temp = parseFloat(response.current_observation.temp_c); + var feelslike = parseFloat(response.current_observation.feelslike_c); + var dewpoint = parseFloat(response.current_observation.dewpoint_c); + var pressure = parseFloat(response.current_observation.pressure_mb); + var wind = parseFloat(response.current_observation.wind_kph); + var wind_gust = parseFloat(response.current_observation.wind_gust_kph); + var visibility = parseFloat(response.current_observation.visibility_km); + var precip_1hr = parseFloat(response.current_observation.precip_1hr_metric); + var precip_today = parseFloat(response.current_observation.precip_today_metric); + } else { + var temp = parseFloat(response.current_observation.temp_f); + var feelslike = parseFloat(response.current_observation.feelslike_f); + var dewpoint = parseFloat(response.current_observation.dewpoint_f); + var pressure = parseFloat(response.current_observation.pressure_in); + var wind = parseFloat(response.current_observation.wind_mph); + var wind_gust = parseFloat(response.current_observation.wind_gust_mph); + var visibility = parseFloat(response.current_observation.visibility_mi); + var precip_1hr = parseFloat(response.current_observation.precip_1hr_in); + var precip_today = parseFloat(response.current_observation.precip_today_in); + } + + // Reset values they where not a number or below zero + if (precip_1hr == "NaN") precip_1hr = 0; + if (precip_today == "NaN") precip_today = 0; + var uv = parseFloat(response.current_observation.UV); + if (uv < 0) uv = 0; + + weatherData = { + city: response.current_observation.display_location.city, + country: response.current_observation.display_location.country, + weather_descr: response.current_observation.weather, + relative_humidity: hum_float, + observation_epoch: response.current_observation.observation_epoch, + wind_degrees: parseFloat(response.current_observation.wind_degrees), + wind_dir: response.current_observation.wind_dir, + uv: uv, + temp: temp, + feelslike: feelslike, + dewpoint: dewpoint, + pressure: pressure, + wind: wind, + wind_gust: wind_gust, + visibility: visibility, + precip_1hr: precip_1hr, + precip_today: precip_today + }; + + Homey.log("Current time: " + new Date()); + Homey.log("Observation time: " + epochToString(weatherData.observation_epoch)); + Homey.log("Weather data:"); + Homey.log(weatherData); + + // Temperature triggers and conditions + if (value_exist(weatherData.temp)) { + + // Determine if the temp has changed + if (!value_exist(oldTemp)){ + Homey.log("No oldTemp value exists, maybe it's the first start of app"); + // First time update after reboot/install + oldTemp = weatherData.temp; + } else if (diff(oldTemp, weatherData.temp) >= 1) { + // Only trigger when difference is equal or more then 1 degree + Homey.log("oldTemp: " + oldTemp + " temp: " + weatherData.temp); + oldTemp = weatherData.temp; + self.tempChanged(weatherData.temp, weatherData.relative_humidity, weatherData.weather_descr); } - // Humidity triggers and conditions - if (value_exist(weatherData.relative_humidity)) { - // Determine if the hum has changed - if (!value_exist(oldHum)){ - // First time update after reboot/install - oldHum = weatherData.relative_humidity; - } else if (diff(oldHum, weatherData.relative_humidity) >= 1) { - // Only trigger when difference is equal or more then 1 percent - Homey.log("oldHum: " + oldHum + " hum: " + weatherData.relative_humidity); - oldHum = weatherData.relative_humidity; - self.humChanged(weatherData.temp, weatherData.relative_humidity, weatherData.weather_descr); - } + // Start trigger + self.tempAboveBelow(weatherData.temp, weatherData.relative_humidity, weatherData.weather_descr); + } else { + // No temperature data available! + Homey.log("Temperature is undefined!") + } - // Start trigger - self.humAboveBelow(weatherData.temp, weatherData.relative_humidity, weatherData.weather_descr); - } else { - // No temperature data available! - Homey.log("Humidity is undefined!") + // Humidity triggers and conditions + if (value_exist(weatherData.relative_humidity)) { + // Determine if the hum has changed + if (!value_exist(oldHum)){ + // First time update after reboot/install + oldHum = weatherData.relative_humidity; + } else if (diff(oldHum, weatherData.relative_humidity) >= 1) { + // Only trigger when difference is equal or more then 1 percent + Homey.log("oldHum: " + oldHum + " hum: " + weatherData.relative_humidity); + oldHum = weatherData.relative_humidity; + self.humChanged(weatherData.temp, weatherData.relative_humidity, weatherData.weather_descr); } - - // UV triggers and conditions - if (value_exist(weatherData.uv)) { - // Start trigger - self.uvAboveBelow(weatherData.uv); - } else { - // No temperature data available! - Homey.log("UV is undefined!") - } - - // Wind triggers and conditions - if (value_exist(weatherData.wind)) { - // Start trigger - self.windAboveBelow(weatherData.wind); - } else { - // No temperature data available! - Homey.log("Wind is undefined!") - } - - // Wind gust triggers and conditions - if (value_exist(weatherData.wind_gust)) { - // Start trigger - self.windgustAboveBelow(weatherData.wind_gust); - } else { - // No temperature data available! - Homey.log("Wind_gust is undefined!") - } - - // Add data to insights - self.addInsightsEntry("temp", weatherData.temp); - self.addInsightsEntry("hum", hum_float); - self.addInsightsEntry("feelslike", weatherData.feelslike); - self.addInsightsEntry("pressure", weatherData.pressure); - self.addInsightsEntry("wind", weatherData.wind); - self.addInsightsEntry("wind_gust", weatherData.wind_gust); - self.addInsightsEntry("wind_degrees", weatherData.wind_degrees); - self.addInsightsEntry("dewpoint", weatherData.dewpoint); - self.addInsightsEntry("precip_today", weatherData.precip_today); - self.addInsightsEntry("precip_1hr", weatherData.precip_1hr); - self.addInsightsEntry("uv", weatherData.uv); - self.addInsightsEntry("visibility", weatherData.visibility); + // Start trigger + self.humAboveBelow(weatherData.temp, weatherData.relative_humidity, weatherData.weather_descr); } else { - // Catch error - Homey.log("Wunderground request error: " + response); - return Homey.error(response); + // No temperature data available! + Homey.log("Humidity is undefined!") } + + // UV triggers and conditions + if (value_exist(weatherData.uv)) { + // Start trigger + self.uvAboveBelow(weatherData.uv); + } else { + // No temperature data available! + Homey.log("UV is undefined!") + } + + // Wind triggers and conditions + if (value_exist(weatherData.wind)) { + // Start trigger + self.windAboveBelow(weatherData.wind); + } else { + // No temperature data available! + Homey.log("Wind is undefined!") + } + + // Wind gust triggers and conditions + if (value_exist(weatherData.wind_gust)) { + // Start trigger + self.windgustAboveBelow(weatherData.wind_gust); + } else { + // No temperature data available! + Homey.log("Wind_gust is undefined!") + } + + // Add data to insights + self.addInsightsEntry("temp", weatherData.temp); + self.addInsightsEntry("hum", hum_float); + self.addInsightsEntry("feelslike", weatherData.feelslike); + self.addInsightsEntry("pressure", weatherData.pressure); + self.addInsightsEntry("wind", weatherData.wind); + self.addInsightsEntry("wind_gust", weatherData.wind_gust); + self.addInsightsEntry("wind_degrees", weatherData.wind_degrees); + self.addInsightsEntry("dewpoint", weatherData.dewpoint); + self.addInsightsEntry("precip_today", weatherData.precip_today); + self.addInsightsEntry("precip_1hr", weatherData.precip_1hr); + self.addInsightsEntry("uv", weatherData.uv); + self.addInsightsEntry("visibility", weatherData.visibility); + + } else { + // Catch error + Homey.log("Wunderground request error: " + response); + return Homey.error(response); } + } ) }, From e54efd74f1d12394ac3d39cef705d0d354d04320 Mon Sep 17 00:00:00 2001 From: inversion-nl Date: Wed, 11 May 2016 22:16:08 +0200 Subject: [PATCH 08/13] fix hum placeholder wrong text --- app.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app.json b/app.json index 3dbb95a..18caa91 100644 --- a/app.json +++ b/app.json @@ -328,8 +328,8 @@ "type": "number", "placeholder" : { - "en": "degrees celsius", - "nl": "graden celsius" + "en": "humidity (%)", + "nl": "Luchtvochtigheid (%)" } } ] From d7cd970154a77fdae971c47f361024ae10608213 Mon Sep 17 00:00:00 2001 From: inversion-nl Date: Wed, 11 May 2016 22:16:46 +0200 Subject: [PATCH 09/13] Version to 003 --- app.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.json b/app.json index 18caa91..5493683 100644 --- a/app.json +++ b/app.json @@ -9,7 +9,7 @@ "en": "Informs you about the weather", "nl": "Geeft je informatie over het weer" }, - "version": "0.0.1", + "version": "0.0.3", "compatibility": "0.x || 1.x", "category": "internet", "author": { From 08f671f12e2fbc1c4ec3123d30a939626945558d Mon Sep 17 00:00:00 2001 From: inversion-nl Date: Wed, 11 May 2016 22:26:04 +0200 Subject: [PATCH 10/13] Fix typo --- app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.js b/app.js index 2415be3..255c37b 100644 --- a/app.js +++ b/app.js @@ -407,7 +407,7 @@ var self = { wunderground.conditions().request(address, function(err, response) { Homey.log('err:', err); - Homey.log('response:'. response); + Homey.log('response:', response); if (!err && response) { From 3e54cf3ec6188a2b5bd2682377866009e30d36a0 Mon Sep 17 00:00:00 2001 From: inversion-nl Date: Thu, 12 May 2016 01:19:30 +0200 Subject: [PATCH 11/13] Add more tests for settings Refactor language layout resource files Clean up settings page --- api.js | 14 ++++++- app.js | 58 ++++++++++++++++++++--------- locales/en.json | 20 ++++++---- locales/nl.json | 22 ++++++----- settings/index.html | 91 ++++++++++++++++++++++++++++++++++----------- 5 files changed, 148 insertions(+), 57 deletions(-) diff --git a/api.js b/api.js index d6ba2d7..ccaa77b 100644 --- a/api.js +++ b/api.js @@ -2,12 +2,24 @@ module.exports = [ { description: 'Test wunderground connection', method: 'POST', - path: '/test/connection/', + path: '/get/weather/', fn: function(callback, args) { Homey.log(""); Homey.log("API: Incoming POST on /test/wunderground/"); Homey.app.testWU(callback, args); } + }, + + { + description: 'Get Homey\'s location', + method: 'POST', + path: '/get/location/', + fn: function(callback, args) { + Homey.log(""); + Homey.log("API: Incoming POST on /get/location/"); + + Homey.app.getlocation(callback, args); + } } ] \ No newline at end of file diff --git a/app.js b/app.js index 255c37b..d9740e8 100644 --- a/app.js +++ b/app.js @@ -94,9 +94,9 @@ var self = { Homey.manager('settings').on('set', self.settingsChanged); // Get location - self.getLocation(function (result) { + self.getLocation(function(err, location) { // Update weather right now and schedule every user defined minutes - self.updateWeather(function(difMinute){}); + self.updateWeather(); self.scheduleWeather(update_frequenty); }); }, @@ -106,7 +106,7 @@ var self = { Homey.log("Schedule weather every " + update_frequenty + " minutes"); interval = setInterval(trigger_update.bind(this), update_frequenty * 60 * 1000); // To minutes function trigger_update() { - self.updateWeather(function(difMinute){}); + self.updateWeather(); }; }, @@ -180,8 +180,8 @@ var self = { if (locationGetCounter <= maxLocationGetTries) { Homey.log("Fetching location, try " + locationGetCounter + " of " + maxLocationGetTries) locationGetCounter++; - self.getLocation(function(lat, lon) { - Homey.log("Location found, start checking settings"); + self.getLocation(function(err, location) { + Homey.log("Location found, check settings"); self.checkSettings(); return; }); @@ -222,7 +222,7 @@ var self = { // If key has changed if (settingname == 'updateFrequenty') { - // If the frequenty is change we have to cancel the current interval and schedule a new + // If the frequenty is changed we have to cancel the current interval and schedule a new self.checkSettings(); Homey.log("Clearing current interval: " + interval); clearInterval(interval); @@ -378,21 +378,25 @@ var self = { //get location getLocation: function(callback) { Homey.log(""); + Homey.log("getLocation"); Homey.manager('geolocation').on('location', function (location) { Homey.log("Homey location changed"); Homey.log(location); lat = location.latitude; lon = location.longitude; + callback(null, location); }); Homey.manager('geolocation').getLocation(function(err, location) { if (typeof location.latitude == 'undefined' || location.latitude == 0) { + Homey.log("Location is undefined"); callback(new Error("location is undefined")); return; } else { + Homey.log("location found:", location); lat = location.latitude; lon = location.longitude; - callback(lat, lon); + callback(null, location); } }); }, @@ -952,10 +956,13 @@ function testWU(callback, args) { Homey.log("TestWU API call"); var Wunderground = require('wundergroundnode'); - var wundergroundKey = args.body.wundergroundKey; - var address = "Netherlands/Amsterdam"; + var wundergroundKey = args.body.wundergroundkey; + var address = args.body.address; + Homey.log('Testing for location:', address); + if (wundergroundKey == "" || wundergroundKey == null) { + Homey.log("wundergroundkey:", wundergroundkey); Homey.log("Weather underground key is empty, using Inversion key"); wundergroundKey = Homey.env.WUNDERGROUND_KEY; } else { @@ -970,17 +977,36 @@ function testWU(callback, args) { var error = false; var err_msg = ''; + Homey.log('response', response); + try { + // If error is in the response, something must have gone wrong err_msg = response.response.error.description; error = true; } catch(err) { + // No error message found so this looks good error = false; } + + if (!error) { + // If still okay, let's test something else + try { + // Let's test for another possible error + var temp = response.current_observation.temp_c; + error = false; + } catch(err) { + // That's gone wrong, let's create our own error message + response = { response: + { error: + { description: 'Undefined error, please try city and country without spaces' } } } + error = true; + } + } if (!err && !error) { Homey.log("Weather response received"); - // Return weather request + // Return specific data var temp = response.current_observation.temp_c; var city = response.current_observation.display_location.city; var country = response.current_observation.display_location.country; @@ -989,17 +1015,13 @@ function testWU(callback, args) { } else { // Catch error - Homey.log("Wunderground request error:", response); - - if (value_exist(response.response.error.description)) { - callback (true, response.response.error.description); - } else { - callback (true, response); - } + Homey.log("Wunderground request error"); + callback (null, response); } }); } module.exports = self; -module.exports.testWU = testWU; \ No newline at end of file +module.exports.testWU = testWU; +module.exports.getlocation = self.getLocation; \ No newline at end of file diff --git a/locales/en.json b/locales/en.json index 291c0d5..363931f 100644 --- a/locales/en.json +++ b/locales/en.json @@ -2,26 +2,30 @@ "settings" : { "app" : { "title" : "Weather Underground settings", - "description" : "On this page, you will be able to adjust the settings of the Weather Underground app.", "wunderground" : { "wunderground_title" : "Weather Underground settings", - "key" : "Personal key" + "key" : "Personal key", + "update_frequenty" : "Update every (min)", + "updateFrequenty_explanation" : "When no personal Weather Underground API key is specified, the update frequenty is 60 minutes minimal." }, - "location" : { - "location" : "Location", + "generic" : { + "units_title" : "Generic", "units_auto" : "Automatic", "units_label" : "Units", "units_metric" : "Metric", "units_imperial" : "Imperial", + "unitChange_explanation": "If the units are changed, all current Insights logs will be deleted!" + }, + + "location" : { + "location" : "Location", "autolocation" : "Use Homey's location", "manualLocation_label" : "Manual location", - "manualLocation_explanation" : "If there is no city and country specified, Homey's location is used.", + "manualLocation_explanation" : "Use the fields below to specify an location for the weather data.", "city" : "City", - "country" : "Country", - "update_frequenty" : "Update every (min)", - "updateFrequenty_explanation" : "When no personal Weather Underground API key is specified, the update frequenty is 60 minutes minimal." + "country" : "Country" }, "button" : { diff --git a/locales/nl.json b/locales/nl.json index ff2fcf1..9005a2d 100644 --- a/locales/nl.json +++ b/locales/nl.json @@ -2,26 +2,30 @@ "settings" : { "app" : { "title" : "Weather Underground instellingen", - "description" : "Op deze pagina kun je de instellingen van Weather Underground aanpassen.", "wunderground" : { "wunderground_title" : "Weather Underground instellingen", - "key" : "Persoonlijke key" + "key" : "Persoonlijke key", + "update_frequenty" : "Bijwerken elke (min)", + "updateFrequenty_explanation" : "Als er geen gebruik wordt gemaakt van een persoonlijke Weather Underground API key is de minimum bijwerk tijd 60 minuten." }, - "location" : { - "location" : "Locatie", + "generic" : { + "units_title" : "Algemeen", "units_auto" : "Automatisch", "units_label" : "Eenheden", "units_metric" : "Metrisch", "units_imperial" : "Imperiaal", + "unitChange_explanation": "Als eenheden aangepast wordt zullen alle bestaande Insights logs verwijderd worden!" + }, + + "location" : { + "location" : "Locatie", "autolocation" : "Gebruik Homey's locatie", - "manualLocation_label" : "Handmatige location", - "manualLocation_explanation" : "Als er geen stad en land ingegeven zijn wordt de locatie van Homey gebruikt voor de weergegevens.", + "manualLocation_label" : "Handmatige locatie", + "manualLocation_explanation" : "Gebruik onderstaande velden om handmatig een locatie op te geven voor de weersinformatie.", "city" : "Stad", - "country" : "Land", - "update_frequenty" : "Bijwerken elke (min)", - "updateFrequenty_explanation" : "Als er geen gebruik wordt gemaakt van een persoonlijke Weather Underground API key is de minimum bijwerk tijd 60 minuten." + "country" : "Land" }, "button" : { diff --git a/settings/index.html b/settings/index.html index 244c7bb..4f69e4e 100644 --- a/settings/index.html +++ b/settings/index.html @@ -6,13 +6,11 @@

-

-
  • - +
    @@ -21,38 +19,42 @@


    - +
    -

    +

    +
    + +
    + + +
    - +
    - +
    - +
    - -
    + +

    - + -

    -

    @@ -74,7 +76,7 @@

  • - +

    @@ -172,6 +174,8 @@

    Homey.set('units_metric', document.getElementById('units_metric').checked); Homey.set('units_auto', document.getElementById('units_auto').checked); + Homey.alert('Settings saved') + setTimeout(function() { button_save.disabled = false; }, 3500); @@ -184,20 +188,64 @@

    var button_save = document.getElementById('button_save'); button_test.disabled = true; button_save.disabled = true; + + var autolocation = document.getElementById('autolocation').checked; + var wundergroundkey = document.getElementById('wundergroundKey').value - console.log("Settings: testing settings"); + console.log("Testing settings"); + + if (autolocation) { + console.log("Auto location is enabled, fetching location"); + Homey.api('POST', '/get/location/', { + }, function(err, location) { + if (!err) { + console.log("Location response received:", location); + var address = location.latitude + "," + location.longitude; + getWeather(wundergroundkey, address); + } else { + // Oeps, something went wrong here + Homey.alert('Unable to get Homey\'s location, please try again or specify a location manually.') + } + + }); + } else { + var city = document.getElementById('city').value; + var country = document.getElementById('country').value; + + var address = country + "/" + city + getWeather(wundergroundkey, address); + } + } - Homey.api( 'POST', '/test/connection/', { + function getWeather(wundergroundkey, address) { + + console.log("getWeather"); + Homey.api('POST', '/get/weather/', { - "wundergroundKey": document.getElementById('wundergroundKey').value + "wundergroundkey": wundergroundkey, + "address" : address }, function(err, result) { - console.log("Settings: response received"); + console.log("Weather response received"); button_test.disabled = false; - if (!err) { + var error = false; + var err_msg; + try { + // If error is in the response, something must have gone wrong + err_msg = result.response.error.description; + console.log('Error:', err_msg); + error = true; + } catch(err) { + // No error message found so this looks good + console.log('No error message found'); + error = false; + } + + if (!err && !error) { + // Alright, no errors, let's rock! button_save.disabled = false; var temp = result.temp; @@ -208,8 +256,9 @@

    document.getElementById('testresult').innerHTML = weather_string; Homey.alert('Settings successfully tested. Please use the save button to save your settings.') } else { - document.getElementById('testresult').innerHTML = "Error fetching weather data with the provided API key."; - return Homey.alert('Error fetching weather data with the provided API key.'); + // Let's display the error message in a alert dialog + document.getElementById('testresult').innerHTML = err_msg; + return Homey.alert(err_msg); } }); } From f77452a6327e0c33212adc3b28b88cc057d72724 Mon Sep 17 00:00:00 2001 From: inversion-nl Date: Thu, 12 May 2016 10:04:30 +0200 Subject: [PATCH 12/13] Future proof compatiblity --- app.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.json b/app.json index 5493683..4b6de00 100644 --- a/app.json +++ b/app.json @@ -10,7 +10,7 @@ "nl": "Geeft je informatie over het weer" }, "version": "0.0.3", - "compatibility": "0.x || 1.x", + "compatibility": ">=0.8.29", "category": "internet", "author": { "name": "Inversion NL", From a94daf9606b839329118f86e40b8633eb7fc420a Mon Sep 17 00:00:00 2001 From: inversion-nl Date: Fri, 13 May 2016 22:20:34 +0200 Subject: [PATCH 13/13] Add translations to messages --- locales/en.json | 12 +++++++++--- locales/nl.json | 12 +++++++++--- settings/index.html | 8 ++++---- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/locales/en.json b/locales/en.json index 363931f..1d4fc43 100644 --- a/locales/en.json +++ b/locales/en.json @@ -24,8 +24,8 @@ "autolocation" : "Use Homey's location", "manualLocation_label" : "Manual location", "manualLocation_explanation" : "Use the fields below to specify an location for the weather data.", - "city" : "City", - "country" : "Country" + "city" : "City (or empty when using station ID)", + "country" : "Country (or station ID)" }, "button" : { @@ -35,7 +35,13 @@ "log" : { "title" : "Test log:" - } + }, + + "messages" : { + "testing" : "Testing, please wait...", + "unable_get_location " : "Unable to get Homey's location, please try again or specify a location manually.", + "settings_saved" : "Settings successfully tested. Please use the save button to save your settings." + } } } } \ No newline at end of file diff --git a/locales/nl.json b/locales/nl.json index 9005a2d..ad57b00 100644 --- a/locales/nl.json +++ b/locales/nl.json @@ -24,8 +24,8 @@ "autolocation" : "Gebruik Homey's locatie", "manualLocation_label" : "Handmatige locatie", "manualLocation_explanation" : "Gebruik onderstaande velden om handmatig een locatie op te geven voor de weersinformatie.", - "city" : "Stad", - "country" : "Land" + "city" : "Stad (of leeg als station ID gebruikt wordt)", + "country" : "Land (of station ID)" }, "button" : { @@ -35,7 +35,13 @@ "log" : { "title" : "Test log:" - } + }, + + "messages" : { + "testing" : "Bezig met testen, moment geduld aub...", + "unable_get_location " : "Niet gelukt om Homey's locatie te krijgen, probeer het opnieuw of geef een handmatige locatie op.", + "settings_success_tested" : "Instellingen succesvol getest. Gebruik de opslaan-knop om je instellingen op te slaan." + } } } } \ No newline at end of file diff --git a/settings/index.html b/settings/index.html index 4f69e4e..8237447 100644 --- a/settings/index.html +++ b/settings/index.html @@ -66,7 +66,7 @@


    -

    +
    @@ -182,7 +182,7 @@

    } function test() { - document.getElementById('testresult').innerHTML = "Testing, please wait..."; + document.getElementById('testresult').innerHTML = __('settings.app.messages.testing'); var button_test = document.getElementById('button_test'); var button_save = document.getElementById('button_save'); @@ -204,7 +204,7 @@

    getWeather(wundergroundkey, address); } else { // Oeps, something went wrong here - Homey.alert('Unable to get Homey\'s location, please try again or specify a location manually.') + Homey.alert(__('settings.app.messages.unable_get_location')) } }); @@ -254,7 +254,7 @@

    var weather_string = "It's " + temp + " degrees Celsius in " + city + ", " + country; document.getElementById('testresult').innerHTML = weather_string; - Homey.alert('Settings successfully tested. Please use the save button to save your settings.') + Homey.alert(__('settings.app.messages.settings_success_tested')) } else { // Let's display the error message in a alert dialog document.getElementById('testresult').innerHTML = err_msg;