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