From 5ae87ae125e19b4379a492efc968b07d8ae21439 Mon Sep 17 00:00:00 2001 From: Marco Heiming Date: Sat, 1 Jan 2022 11:56:40 +0100 Subject: [PATCH] Use weather condition code for icon selection --- apps.json | 2 +- apps/weatherClock/ChangeLog | 1 + apps/weatherClock/app.js | 30 +++++++++++++++++++++++++++++- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/apps.json b/apps.json index e5e9f8f02..64732f580 100644 --- a/apps.json +++ b/apps.json @@ -4778,7 +4778,7 @@ { "id": "weatherClock", "name": "Weather Clock", - "version": "0.04", + "version": "0.05", "description": "A clock which displays current weather conditions (requires Gadgetbridge and Weather apps).", "icon": "app.png", "screenshots": [{"url":"screens/screen1.png"}], diff --git a/apps/weatherClock/ChangeLog b/apps/weatherClock/ChangeLog index 72b53ebbf..a6a12c297 100644 --- a/apps/weatherClock/ChangeLog +++ b/apps/weatherClock/ChangeLog @@ -2,3 +2,4 @@ 0.02: Minor layout format tweak so it uses less memory and draws ok on Bangle.js 1 (#1012) 0.03: Minor layout extra spaces. 0.04: Layout now compatible with Bangle.js 2 +0.05: Use weather condition code for icon selection diff --git a/apps/weatherClock/app.js b/apps/weatherClock/app.js index 799f9abc6..1a7f53f05 100644 --- a/apps/weatherClock/app.js +++ b/apps/weatherClock/app.js @@ -53,6 +53,29 @@ function chooseIcon(condition) { return cloudIcon; } +/* +* 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 stormIcon; + case 3: return rainIcon; + case 5: return rainIcon; + case 6: return snowIcon; + case 7: return cloudIcon; + case 8: + switch (code) { + case 800: return sunIcon; + case 801: return partSunIcon; + default: return cloudIcon; + } + break; + default: return cloudIcon; + } +} + /** Get weather stored in json file by weather app. */ @@ -105,7 +128,12 @@ function draw() { var currentWeather = weatherJson.weather; const temp = locale.temp(currentWeather.temp-273.15).match(/^(\D*\d*)(.*)$/); clockLayout.temp.label = temp[1] + " " + temp[2]; - clockLayout.weatherIcon.src = chooseIcon(currentWeather.txt); + const code = currentWeather.code || -1; + if (code > 0) { + clockLayout.weatherIcon.src = chooseIconByCode(code); + } else { + clockLayout.weatherIcon.src = chooseIcon(currentWeather.txt); + } const wind = locale.speed(currentWeather.wind).match(/^(\D*\d*)(.*)$/); clockLayout.wind.label = wind[1] + " " + wind[2] + " " + (currentWeather.wrose||'').toUpperCase(); }