From fdc519c3df64c555b9b98dedfbe65b2ff2918863 Mon Sep 17 00:00:00 2001 From: Marco Heiming Date: Tue, 4 Jan 2022 11:58:04 +0100 Subject: [PATCH 1/4] Intial try to use weather condition code for icon selection --- apps/weather/app.js | 3 ++- apps/weather/lib.js | 49 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/apps/weather/app.js b/apps/weather/app.js index 8c8526fbd..5c6b29b64 100644 --- a/apps/weather/app.js +++ b/apps/weather/app.js @@ -9,7 +9,7 @@ var layout = new Layout({type:"v", bgCol: g.theme.bg, c: [ {filly: 1}, {type: "h", filly: 0, c: [ {type: "custom", width: g.getWidth()/2, height: g.getWidth()/2, valign: -1, txt: "unknown", id: "icon", - render: l => weather.drawIcon(l.txt, l.x+l.w/2, l.y+l.h/2, l.w/2-5)}, + render: l => weather.drawIcon(l, l.x+l.w/2, l.y+l.h/2, l.w/2-5)}, {type: "v", fillx: 1, c: [ {type: "h", pad: 2, c: [ {type: "txt", font: "18%", id: "temp", label: "000"}, @@ -47,6 +47,7 @@ function formatDuration(millis) { function draw() { layout.icon.txt = current.txt; + layout.icon.cond = current.code; const temp = locale.temp(current.temp-273.15).match(/^(\D*\d*)(.*)$/); layout.temp.label = temp[1]; layout.tempUnit.label = temp[2]; diff --git a/apps/weather/lib.js b/apps/weather/lib.js index 7cb9a9f9b..f86a51a18 100644 --- a/apps/weather/lib.js +++ b/apps/weather/lib.js @@ -16,7 +16,7 @@ function scheduleExpiry(json) { function update(weatherEvent) { let json = storage.readJSON('weather.json')||{}; - + if (weatherEvent) { let weather = weatherEvent.clone(); delete weather.t; @@ -55,7 +55,7 @@ scheduleExpiry(storage.readJSON('weather.json')||{}); exports.drawIcon = function(cond, x, y, r) { var palette; - + if (B2) { if (g.theme.dark) { palette = { @@ -101,7 +101,7 @@ exports.drawIcon = function(cond, x, y, r) { }; } } - + function drawSun(x, y, r) { g.setColor(palette.sun); g.fillCircle(x, y, r); @@ -280,5 +280,46 @@ exports.drawIcon = function(cond, x, y, r) { return drawUnknown; } - chooseIcon(cond)(x, y, r); + /* + * Choose weather icon to display based on weather conditition code + * https://openweathermap.org/weather-conditions#Weather-Condition-Codes-2 + */ + function chooseIconByCode(code) { + const codeGroup = Math.round(code / 100); + switch (codeGroup) { + case 2: return drawThunderstorm; + case 3: return drawRain; + case 5: + switch (code) { + case 520: return drawShowerRain; + case 521: return drawShowerRain; + case 522: return drawShowerRain; + case 531: return drawShowerRain; + default: return drawRain; + } + break; + case 6: return drawSnow; + case 7: return drawMist; + case 8: + switch (code) { + case 800: return drawSun; + case 801: return drawFewClouds; + case 802: return drawCloud; + default: return drawBrokenClouds; + } + break; + default: return drawUnknown; + } + } + + const code = cond.code || -1; + if (code > 0) { + chooseIconByCode(code.code)(x, y, r); + } else { + chooseIcon(cond.txt)(x, y, r); + } + + console.log(cond); + + }; From 144f4d31969d1306a193570bad2c6b2697e01c39 Mon Sep 17 00:00:00 2001 From: Marco Heiming Date: Tue, 4 Jan 2022 12:03:23 +0100 Subject: [PATCH 2/4] Fix naming of variable --- apps/weather/app.js | 2 +- apps/weather/lib.js | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/apps/weather/app.js b/apps/weather/app.js index 5c6b29b64..efd9b0209 100644 --- a/apps/weather/app.js +++ b/apps/weather/app.js @@ -47,7 +47,7 @@ function formatDuration(millis) { function draw() { layout.icon.txt = current.txt; - layout.icon.cond = current.code; + layout.icon.code = current.code; const temp = locale.temp(current.temp-273.15).match(/^(\D*\d*)(.*)$/); layout.temp.label = temp[1]; layout.tempUnit.label = temp[2]; diff --git a/apps/weather/lib.js b/apps/weather/lib.js index f86a51a18..b1c306d58 100644 --- a/apps/weather/lib.js +++ b/apps/weather/lib.js @@ -312,14 +312,11 @@ exports.drawIcon = function(cond, x, y, r) { } } - const code = cond.code || -1; - if (code > 0) { - chooseIconByCode(code.code)(x, y, r); + if (cond.code > 0) { + chooseIconByCode(cond.code)(x, y, r); } else { chooseIcon(cond.txt)(x, y, r); } - console.log(cond); - }; From 8b0498b195e765d33ff04cdec918cb0a0410ac06 Mon Sep 17 00:00:00 2001 From: Marco Heiming Date: Tue, 4 Jan 2022 12:10:15 +0100 Subject: [PATCH 3/4] debug --- apps/weather/lib.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/apps/weather/lib.js b/apps/weather/lib.js index b1c306d58..8ffd2037f 100644 --- a/apps/weather/lib.js +++ b/apps/weather/lib.js @@ -291,6 +291,7 @@ exports.drawIcon = function(cond, x, y, r) { case 3: return drawRain; case 5: switch (code) { + case 511: return drawSnow; case 520: return drawShowerRain; case 521: return drawShowerRain; case 522: return drawShowerRain; @@ -312,7 +313,9 @@ exports.drawIcon = function(cond, x, y, r) { } } - if (cond.code > 0) { + console.log("weather cond", cond); + + if (cond.code && cond.code > 0) { chooseIconByCode(cond.code)(x, y, r); } else { chooseIcon(cond.txt)(x, y, r); From fe747934c8a53e3f1d83d6c70665891915cbcffb Mon Sep 17 00:00:00 2001 From: Marco Heiming Date: Tue, 4 Jan 2022 12:16:05 +0100 Subject: [PATCH 4/4] Update changelog and bump version --- apps.json | 2 +- apps/weather/ChangeLog | 1 + apps/weather/lib.js | 2 -- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/apps.json b/apps.json index ab6089e65..a440e9b15 100644 --- a/apps.json +++ b/apps.json @@ -845,7 +845,7 @@ { "id": "weather", "name": "Weather", - "version": "0.13", + "version": "0.14", "description": "Show Gadgetbridge weather report", "icon": "icon.png", "screenshots": [{"url":"screenshot.png"}], diff --git a/apps/weather/ChangeLog b/apps/weather/ChangeLog index fb6b28bf6..910cd4658 100644 --- a/apps/weather/ChangeLog +++ b/apps/weather/ChangeLog @@ -10,3 +10,4 @@ 0.11: Bangle.js 2 support 0.12: Allow hiding the widget 0.13: Tweak Bangle.js 2 light theme colors +0.14: Use weather condition code for icon selection diff --git a/apps/weather/lib.js b/apps/weather/lib.js index 8ffd2037f..8afdfe6df 100644 --- a/apps/weather/lib.js +++ b/apps/weather/lib.js @@ -313,8 +313,6 @@ exports.drawIcon = function(cond, x, y, r) { } } - console.log("weather cond", cond); - if (cond.code && cond.code > 0) { chooseIconByCode(cond.code)(x, y, r); } else {