Intial try to use weather condition code for icon selection

master
Marco Heiming 2022-01-04 11:58:04 +01:00
parent e869ffe415
commit fdc519c3df
2 changed files with 47 additions and 5 deletions

View File

@ -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];

View File

@ -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);
};