diff --git a/apps/weather/app.js b/apps/weather/app.js index 923e63bbf..7f42e60bb 100644 --- a/apps/weather/app.js +++ b/apps/weather/app.js @@ -16,24 +16,24 @@ var layout = new Layout({type:"v", bgCol: g.theme.bg, c: [ render: l => { if (!current || current.uv === undefined) return; const uv = Math.min(parseInt(current.uv), 11); // Cap at 11 - + // UV color thresholds: [max_value, color] based on WHO standards const colors = [[2,"#0F0"], [5,"#FF0"], [7,"#F80"], [10,"#F00"], [11,"#F0F"]]; const color = colors.find(c => uv <= c[0])[1]; - + // Setup and measure label g.setFont("6x8").setFontAlign(-1, 0); const label = "UV: "; const labelW = g.stringWidth(label); - + // Calculate centered position (4px block + 1px spacing) * blocks - last spacing const totalW = labelW + uv * 5 - (uv > 0 ? 1 : 0); const x = l.x + (l.w - totalW) / 2; const y = l.y + l.h+6; - + // Draw label g.setColor(g.theme.fg).drawString(label, x, y); - + // Draw UV blocks g.setColor(color); for (let i = 0; i < uv; i++) { @@ -58,7 +58,7 @@ var layout = new Layout({type:"v", bgCol: g.theme.bg, c: [ {type: "txt", font: "6x8", pad: 2, halign: 1, label: /*LANG*/"Hum:"}, {type: "txt", font: "9%", pad: 2, halign: 1, id: "hum", label: "000%"}, ]}, - + {filly: 1}, {type: "txt", font: "6x8", pad: 2, halign: -1, label: /*LANG*/"Wind"}, {type: "h", halign: -1, c: [ @@ -79,7 +79,7 @@ var layout = new Layout({type:"v", bgCol: g.theme.bg, c: [ ]}, {lazy: true}); function formatDuration(millis) { - let pluralize = (n, w) => n + " " + w + (n == 1 ? "" : "s"); + let pluralize = (n, w) => `${n} ${w}${n === 1 ? "" : "s"}`; if (millis < 60000) return /*LANG*/"< 1 minute"; if (millis < 3600000) return pluralize(Math.floor(millis/60000), /*LANG*/"minute"); if (millis < 86400000) return pluralize(Math.floor(millis/3600000), /*LANG*/"hour"); @@ -98,11 +98,11 @@ function draw() { }else{ layout.feelslike.label = feelsLikeTemp[1]+feelsLikeTemp[2]; } - - layout.hum.label = current.hum+"%"; + + layout.hum.label = `${current.hum}%`; const wind = locale.speed(current.wind).match(/^(\D*\d*)(.*)$/); layout.wind.label = wind[1]; - layout.windUnit.label = wind[2] + " " + (current.wrose||'').toUpperCase(); + layout.windUnit.label = `${wind[2]} ${(current.wrose||'').toUpperCase()}`; layout.cond.label = current.txt.charAt(0).toUpperCase()+(current.txt||'').slice(1); layout.loc.label = current.loc; layout.updateTime.label = `${formatDuration(Date.now() - current.time)} ago`; // How to autotranslate this and similar? diff --git a/apps/weather/clkinfo.js b/apps/weather/clkinfo.js index eaf9556e0..b60aa6daa 100644 --- a/apps/weather/clkinfo.js +++ b/apps/weather/clkinfo.js @@ -1,5 +1,4 @@ - -(function() { +(() => { var weather; var weatherLib = require("weather"); @@ -8,9 +7,9 @@ if(weather){ weather.temp = require("locale").temp(weather.temp-273.15); weather.feels = require("locale").temp(weather.feels-273.15); - weather.hum = weather.hum + "%"; + weather.hum = `${weather.hum}%`; weather.wind = require("locale").speed(weather.wind).match(/^(\D*\d*)(.*)$/); - weather.wind = Math.round(weather.wind[1]) + "kph"; + weather.wind = `${Math.round(weather.wind[1])}kph`; } else { weather = { temp: "?", diff --git a/apps/weather/lib.js b/apps/weather/lib.js index 9505cbafe..18f149c15 100644 --- a/apps/weather/lib.js +++ b/apps/weather/lib.js @@ -1,5 +1,5 @@ -const storage = require('Storage'); -const B2 = process.env.HWVERSION===2; +const storage = require("Storage"); +const B2 = process.env.HWVERSION === 2; let expiryTimeout; function scheduleExpiry(json) { @@ -7,7 +7,7 @@ function scheduleExpiry(json) { clearTimeout(expiryTimeout); expiryTimeout = undefined; } - let expiry = "expiry" in json ? json.expiry : 2*3600000; + let expiry = "expiry" in json ? json.expiry : 2 * 3600000; if (json.weather && json.weather.time && expiry) { let t = json.weather.time + expiry - Date.now(); expiryTimeout = setTimeout(update, t); @@ -15,7 +15,7 @@ function scheduleExpiry(json) { } function update(weatherEvent) { - let json = storage.readJSON('weather.json')||{}; + let json = storage.readJSON("weather.json") || {}; if (weatherEvent) { let weather = weatherEvent.clone(); @@ -24,92 +24,90 @@ function update(weatherEvent) { if (weather.wdir != null) { // Convert numeric direction into human-readable label let deg = weather.wdir; - while (deg<0 || deg>360) { - deg = (deg+360)%360; + while (deg < 0 || deg > 360) { + deg = (deg + 360) % 360; } - weather.wrose = ['n','ne','e','se','s','sw','w','nw','n'][Math.floor((deg+22.5)/45)]; + weather.wrose = ["n", "ne", "e", "se", "s", "sw", "w", "nw", "n"][Math.floor((deg + 22.5) / 45)]; } json.weather = weather; - } - else { + } else { delete json.weather; } - - storage.write('weather.json', json); + storage.write("weather.json", json); scheduleExpiry(json); exports.emit("update", json.weather); } exports.update = update; const _GB = global.GB; global.GB = (event) => { - if (event.t==="weather") update(event); + if (event.t === "weather") update(event); if (_GB) setTimeout(_GB, 0, event); }; -exports.get = function() { - return (storage.readJSON('weather.json')||{}).weather; -} +exports.get = () => { + return (storage.readJSON("weather.json") || {}).weather; +}; -scheduleExpiry(storage.readJSON('weather.json')||{}); +scheduleExpiry(storage.readJSON("weather.json") || {}); function getPalette(monochrome, ovr) { var palette; - if(monochrome) { + if (monochrome) { palette = { - sun: '#FFF', - cloud: '#FFF', - bgCloud: '#FFF', - rain: '#FFF', - lightning: '#FFF', - snow: '#FFF', - mist: '#FFF', - background: '#000' + 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' + 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' + 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' + 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' + sun: "#FC0", + cloud: "#000", + bgCloud: "#777", + rain: "#07F", + lightning: "#FC0", + snow: "#CCC", + mist: "#CCC", }; } } @@ -117,10 +115,10 @@ function getPalette(monochrome, ovr) { return palette; } -exports.getColor = function(code) { +exports.getColor = (code) => { const codeGroup = Math.round(code / 100); const palette = getPalette(0, g); - const cloud = g.blendColor(palette.cloud, palette.bgCloud, .5); //theme independent + const cloud = g.blendColor(palette.cloud, palette.bgCloud, 0.5); //theme independent switch (codeGroup) { case 2: return g.blendColor(cloud, palette.lightning, .5); case 3: return palette.rain; @@ -144,7 +142,7 @@ exports.getColor = function(code) { } default: return cloud; } -} +}; /** * @@ -158,9 +156,9 @@ exports.getColor = function(code) { * @param ovr Graphics instance (or undefined for g) * @param monochrome If true, produce a monochromatic icon */ -exports.drawIcon = function(cond, x, y, r, ovr, monochrome) { +exports.drawIcon = (cond, x, y, r, ovr, monochrome) => { var palette; - if(!ovr) ovr = g; + if (!ovr) ovr = g; palette = getPalette(monochrome, ovr); @@ -257,7 +255,7 @@ exports.drawIcon = function(cond, x, y, r, ovr, monochrome) { function drawSnow(x, y, r) { function rotatePoints(points, pivotX, pivotY, angle) { - for(let i = 0; i {}; txt = txt.toLowerCase(); @@ -336,7 +334,8 @@ exports.drawIcon = function(cond, x, y, r, ovr, monochrome) { if (txt.includes("few clouds")) return drawFewClouds; if (txt.includes("scattered clouds")) return drawCloud; if (txt.includes("clouds")) return drawBrokenClouds; - if (txt.includes("mist") || + if ( + txt.includes("mist") || txt.includes("smoke") || txt.includes("haze") || txt.includes("sand") || @@ -344,16 +343,17 @@ exports.drawIcon = function(cond, x, y, r, ovr, monochrome) { txt.includes("fog") || txt.includes("ash") || txt.includes("squalls") || - txt.includes("tornado")) { + txt.includes("tornado") + ) { return drawMist; } return drawUnknown; } /* - * Choose weather icon to display based on weather conditition code - * https://openweathermap.org/weather-conditions#Weather-Condition-Codes-2 - */ + * Choose weather icon to display based on weather condition code + * https://openweathermap.org/weather-conditions#Weather-Condition-Codes-2 + */ function chooseIconByCode(code) { const codeGroup = Math.round(code / 100); switch (codeGroup) { @@ -382,16 +382,15 @@ exports.drawIcon = function(cond, x, y, r, ovr, monochrome) { } function chooseIcon(cond) { - if (typeof (cond)==="object") { + if (typeof cond === "object") { if ("code" in cond) return chooseIconByCode(cond.code); if ("txt" in cond) return chooseIconByTxt(cond.txt); - } else if (typeof (cond)==="number") { + } else if (typeof cond === "number") { return chooseIconByCode(cond.code); - } else if (typeof (cond)==="string") { + } else if (typeof cond === "string") { return chooseIconByTxt(cond.txt); } return drawUnknown; } chooseIcon(cond)(x, y, r); - }; diff --git a/apps/weather/settings.js b/apps/weather/settings.js index 7e0bb24c8..9f39c00df 100644 --- a/apps/weather/settings.js +++ b/apps/weather/settings.js @@ -1,31 +1,31 @@ -(function(back) { - const storage = require('Storage'); - let settings = storage.readJSON('weather.json', 1) || {}; +(back) => { + const storage = require("Storage"); + let settings = storage.readJSON("weather.json", 1) || {}; function save(key, value) { settings[key] = value; - storage.write('weather.json', settings); + storage.write("weather.json", settings); } E.showMenu({ - '': { 'title': 'Weather' }, - 'Expiry': { - value: "expiry" in settings ? settings["expiry"] : 2*3600000, + "": { "title": "Weather" }, + "Expiry": { + value: "expiry" in settings ? settings.expiry : 2 * 3600000, min: 0, - max : 24*3600000, - step: 15*60000, - format: x => { - if (x == 0) return "none"; - if (x < 3600000) return Math.floor(x/60000) + "m"; - if (x < 86400000) return Math.floor(x/36000)/100 + "h"; + max: 24 * 3600000, + step: 15 * 60000, + format: (x) => { + if (x === 0) return "none"; + if (x < 3600000) return `${Math.floor(x / 60000)}m`; + if (x < 86400000) return `${Math.floor(x / 36000) / 100}h`; }, - onchange: x => save('expiry', x), + onchange: (x) => save("expiry", x), }, - 'Hide Widget': { + "Hide Widget": { value: "hide" in settings ? settings.hide : false, onchange: () => { - settings.hide = !settings.hide - save('hide', settings.hide); + settings.hide = !settings.hide; + save("hide", settings.hide); }, }, - '< Back': back, + "< Back": back, }); -}) +}; diff --git a/apps/weather/widget.js b/apps/weather/widget.js index 2905d776b..be0ca838f 100644 --- a/apps/weather/widget.js +++ b/apps/weather/widget.js @@ -1,53 +1,53 @@ (() => { - const weather = require('weather'); - + const weather = require("weather"); + var dirty = false; - + let settings; function loadSettings() { - settings = require('Storage').readJSON('weather.json', 1) || {}; + settings = require("Storage").readJSON("weather.json", 1) || {}; } function setting(key) { if (!settings) { loadSettings(); } const DEFAULTS = { - 'expiry': 2*3600000, - 'hide': false + "expiry": 2*3600000, + "hide": false }; return (key in settings) ? settings[key] : DEFAULTS[key]; } weather.on("update", w => { - if (setting('hide')) return; + if (setting("hide")) return; if (w) { - if (!WIDGETS["weather"].width) { - WIDGETS["weather"].width = 20; + if (!WIDGETS.weather.width) { + WIDGETS.weather.width = 20; Bangle.drawWidgets(); } else if (Bangle.isLCDOn()) { - WIDGETS["weather"].draw(); + WIDGETS.weather.draw(); } else { dirty = true; } } else { - WIDGETS["weather"].width = 0; + WIDGETS.weather.width = 0; Bangle.drawWidgets(); } }); - Bangle.on('lcdPower', on => { - if (on && dirty && !setting('hide')) { - WIDGETS["weather"].draw(); + Bangle.on("lcdPower", on => { + if (on && dirty && !setting("hide")) { + WIDGETS.weather.draw(); dirty = false; } }); - WIDGETS["weather"] = { + WIDGETS.weather = { area: "tl", - width: weather.get() && !setting('hide') ? 20 : 0, + width: weather.get() && !setting("hide") ? 20 : 0, draw: function() { - if (setting('hide')) return; + if (setting("hide")) return; const w = weather.get(); if (!w) return; g.reset(); @@ -56,17 +56,17 @@ weather.drawIcon(w, this.x+10, this.y+8, 7.5); } if (w.temp) { - let t = require('locale').temp(w.temp-273.15); // applies conversion + let t = require("locale").temp(w.temp-273.15); // applies conversion t = t.match(/[\d\-]*/)[0]; // but we have no room for units g.reset(); g.setFontAlign(0, 1); // center horizontally at bottom of widget - g.setFont('6x8', 1); + g.setFont("6x8", 1); g.drawString(t, this.x+10, this.y+24); } }, - reload:function() { + reload:() => { loadSettings(); - WIDGETS["weather"].redraw(); + WIDGETS.weather.redraw(); }, }; })();