From 0cc4e318f8fbb8cfecdc44e0a5f09e47dbea2e3c Mon Sep 17 00:00:00 2001 From: Gabriele Monaco Date: Sat, 7 Jan 2023 16:00:21 +0100 Subject: [PATCH] weather: redraw clock_info on update and provide color field --- apps/weather/ChangeLog | 1 + apps/weather/clkinfo.js | 73 ++++++++++++------ apps/weather/lib.js | 151 +++++++++++++++++++++++-------------- apps/weather/metadata.json | 2 +- modules/clock_info.js | 3 +- 5 files changed, 148 insertions(+), 82 deletions(-) diff --git a/apps/weather/ChangeLog b/apps/weather/ChangeLog index 4b70d3531..0010a58fd 100644 --- a/apps/weather/ChangeLog +++ b/apps/weather/ChangeLog @@ -20,3 +20,4 @@ 0.21: Updated clkinfo icon. 0.22: Automatic translation of strings, some left untranslated. 0.23: Update clock_info to avoid a redraw +0.24: Redraw clock_info on update and provide color field for condition diff --git a/apps/weather/clkinfo.js b/apps/weather/clkinfo.js index f40924e06..ef3b7d139 100644 --- a/apps/weather/clkinfo.js +++ b/apps/weather/clkinfo.js @@ -1,28 +1,36 @@ (function() { - var weather = { - temp: "?", - hum: "?", - wind: "?", - txt: "?", - }; + var weather; + var weatherLib = require("weather"); - var weatherJson = require("Storage").readJSON('weather.json'); - if(weatherJson !== undefined && weatherJson.weather !== undefined){ - weather = weatherJson.weather; + function updateWeather() { + weather = weatherLib.get(); + if(weather){ weather.temp = require("locale").temp(weather.temp-273.15); weather.hum = weather.hum + "%"; weather.wind = require("locale").speed(weather.wind).match(/^(\D*\d*)(.*)$/); weather.wind = Math.round(weather.wind[1]) + "kph"; + } else { + weather = { + temp: "?", + hum: "?", + wind: "?", + txt: "?", + }; + } } + updateWeather(); function weatherIcon(code) { var ovr = Graphics.createArrayBuffer(24,24,1,{msb:true}); - require("weather").drawIcon({code:code},12,12,12,ovr); + weatherLib.drawIcon({code:code},12,12,12,ovr); var img = ovr.asImage(); img.transparent = 0; - //for (var i=0;i ({ text: weather.temp, img: weatherIcon(weather.code), + color: weatherLib.getColor(weather.code), v: parseInt(weather.temp), min: -30, max: 55}), - show: function() {}, - hide: function () {} + show: function() { + this.updater = _updater.bind(this); + weatherLib.on("update", this.updater); + }, + hide: function () { weatherLib.removeListener("update", this.updater); } }, { name: "condition", get: () => ({ text: weather.txt, img: weatherIcon(weather.code), + color: weatherLib.getColor(weather.code), v: weather.code}), - show: function() {}, - hide: function () {} + show: function() { + this.updater = _updater.bind(this); + weatherLib.on("update", this.updater); + }, + hide: function () { weatherLib.removeListener("update", this.updater); } }, { name: "temperature", hasRange : true, get: () => ({ text: weather.temp, img: atob("GBiBAAA8AAB+AADnAADDAADDAADDAADDAADDAADbAADbAADbAADbAADbAADbAAHbgAGZgAM8wAN+wAN+wAM8wAGZgAHDgAD/AAA8AA=="), v: parseInt(weather.temp), min: -30, max: 55}), - show: function() {}, - hide: function () {} + show: function() { + this.updater = _updater.bind(this); + weatherLib.on("update", this.updater); + }, + hide: function () { weatherLib.removeListener("update", this.updater); } }, { name: "humidity", hasRange : true, get: () => ({ text: weather.hum, img: atob("GBiBAAAEAAAMAAAOAAAfAAAfAAA/gAA/gAI/gAY/AAcfAA+AQA+A4B/A4D/B8D/h+D/j+H/n/D/n/D/n/B/H/A+H/AAH/AAD+AAA8A=="), v: parseInt(weather.hum), min: 0, max: 100}), - show: function() {}, - hide: function () {} + show: function() { + this.updater = _updater.bind(this); + weatherLib.on("update", this.updater); + }, + hide: function () { weatherLib.removeListener("update", this.updater); } }, { name: "wind", hasRange : true, get: () => ({ text: weather.wind, img: atob("GBiBAAHgAAPwAAYYAAwYAAwMfAAY/gAZh3/xg//hgwAAAwAABg///g//+AAAAAAAAP//wH//4AAAMAAAMAAYMAAYMAAMcAAP4AADwA=="), v: parseInt(weather.wind), min: 0, max: 118}), - show: function() {}, - hide: function () {} + show: function() { + this.updater = _updater.bind(this); + weatherLib.on("update", this.updater); + }, + hide: function () { weatherLib.removeListener("update", this.updater); } }, ] }; diff --git a/apps/weather/lib.js b/apps/weather/lib.js index 8c59fd3e3..14ca77ec6 100644 --- a/apps/weather/lib.js +++ b/apps/weather/lib.js @@ -53,6 +53,98 @@ exports.get = function() { scheduleExpiry(storage.readJSON('weather.json')||{}); +function getPalette(monochrome, ovr) { + var palette; + if(monochrome) { + palette = { + sun: '#FFF', + cloud: '#FFF', + bgCloud: '#FFF', + rain: '#FFF', + lightning: '#FFF', + snow: '#FFF', + mist: '#FFF', + background: '#000' + }; + } else { + if (B2) { + if (ovr.theme.dark) { + palette = { + sun: '#FF0', + cloud: '#FFF', + bgCloud: '#777', // dithers on B2, but that's ok + rain: '#0FF', + lightning: '#FF0', + snow: '#FFF', + mist: '#FFF' + }; + } else { + palette = { + sun: '#FF0', + cloud: '#777', // dithers on B2, but that's ok + bgCloud: '#000', + rain: '#00F', + lightning: '#FF0', + snow: '#0FF', + mist: '#0FF' + }; + } + } else { + if (ovr.theme.dark) { + palette = { + sun: '#FE0', + cloud: '#BBB', + bgCloud: '#777', + rain: '#0CF', + lightning: '#FE0', + snow: '#FFF', + mist: '#FFF' + }; + } else { + palette = { + sun: '#FC0', + cloud: '#000', + bgCloud: '#777', + rain: '#07F', + lightning: '#FC0', + snow: '#CCC', + mist: '#CCC' + }; + } + } + } + return palette; +} + +exports.getColor = function(code) { + const codeGroup = Math.round(code / 100); + const palette = getPalette(0, g); + const cloud = g.blendColor(palette.cloud, palette.bgCloud, .5); //theme independent + switch (codeGroup) { + case 2: return g.blendColor(cloud, palette.lightning, .5); + case 3: return palette.rain; + case 5: + switch (code) { + case 511: return palette.snow; + case 520: return g.blendColor(palette.rain, palette.sun, .5); + case 521: return g.blendColor(palette.rain, palette.sun, .5); + case 522: return g.blendColor(palette.rain, palette.sun, .5); + case 531: return g.blendColor(palette.rain, palette.sun, .5); + default: return palette.rain; + } + case 6: return palette.snow; + case 7: return palette.mist; + case 8: + switch (code) { + case 800: return palette.sun; + case 801: return palette.sun; + case 802: return cloud; + default: return cloud; + } + default: return cloud; + } +} + /** * * @param cond Weather condition, as one of: @@ -71,63 +163,8 @@ exports.drawIcon = function(cond, x, y, r, ovr) { ovr = g; monochrome=0; } - if(monochrome) { - palette = { - sun: '#FFF', - cloud: '#FFF', - bgCloud: '#FFF', - rain: '#FFF', - lightning: '#FFF', - snow: '#FFF', - mist: '#FFF', - background: '#000' - }; - } else - if (B2) { - if (ovr.theme.dark) { - palette = { - sun: '#FF0', - cloud: '#FFF', - bgCloud: '#777', // dithers on B2, but that's ok - rain: '#0FF', - lightning: '#FF0', - snow: '#FFF', - mist: '#FFF' - }; - } else { - palette = { - sun: '#FF0', - cloud: '#777', // dithers on B2, but that's ok - bgCloud: '#000', - rain: '#00F', - lightning: '#FF0', - snow: '#0FF', - mist: '#0FF' - }; - } - } else { - if (ovr.theme.dark) { - palette = { - sun: '#FE0', - cloud: '#BBB', - bgCloud: '#777', - rain: '#0CF', - lightning: '#FE0', - snow: '#FFF', - mist: '#FFF' - }; - } else { - palette = { - sun: '#FC0', - cloud: '#000', - bgCloud: '#777', - rain: '#07F', - lightning: '#FC0', - snow: '#CCC', - mist: '#CCC' - }; - } - } + + palette = getPalette(monochrome, ovr); function drawSun(x, y, r) { ovr.setColor(palette.sun); diff --git a/apps/weather/metadata.json b/apps/weather/metadata.json index 77ca37721..bcb2fe109 100644 --- a/apps/weather/metadata.json +++ b/apps/weather/metadata.json @@ -1,7 +1,7 @@ { "id": "weather", "name": "Weather", - "version": "0.23", + "version": "0.24", "description": "Show Gadgetbridge weather report", "icon": "icon.png", "screenshots": [{"url":"screenshot.png"}], diff --git a/modules/clock_info.js b/modules/clock_info.js index b28271e9c..0a6035ccf 100644 --- a/modules/clock_info.js +++ b/modules/clock_info.js @@ -15,8 +15,9 @@ Note that each item is an object with: { 'text' // the text to display for this item - 'short' : (optional) a shorter text to display for this item (at most 6 characters) + 'short' // optional: a shorter text to display for this item (at most 6 characters) 'img' // optional: a 24x24px image to display for this item + 'color' // optional: a color string (like "#ffffff") to color the icon in compatible clocks 'v' // (if hasRange==true) a numerical value 'min','max' // (if hasRange==true) a minimum and maximum numerical value (if this were to be displayed as a guage) }