2020-04-21 21:18:21 +00:00
|
|
|
(() => {
|
2020-05-24 09:21:24 +00:00
|
|
|
const weather = require('weather');
|
2021-09-19 18:28:56 +00:00
|
|
|
let current = weather.get();
|
2020-05-24 09:21:24 +00:00
|
|
|
|
2020-05-23 04:44:31 +00:00
|
|
|
function formatDuration(millis) {
|
|
|
|
let pluralize = (n, w) => n + " " + w + (n == 1 ? "" : "s");
|
2020-06-07 02:26:46 +00:00
|
|
|
if (millis < 60000) return "< 1 minute";
|
2020-05-23 04:44:31 +00:00
|
|
|
if (millis < 3600000) return pluralize(Math.floor(millis/60000), "minute");
|
|
|
|
if (millis < 86400000) return pluralize(Math.floor(millis/3600000), "hour");
|
|
|
|
return pluralize(Math.floor(millis/86400000), "day");
|
|
|
|
}
|
|
|
|
|
2020-05-24 09:21:24 +00:00
|
|
|
function draw() {
|
2020-04-21 21:18:21 +00:00
|
|
|
g.reset();
|
2021-09-18 17:59:22 +00:00
|
|
|
g.clearRect(0, 24, 239, 239);
|
2020-04-21 21:18:21 +00:00
|
|
|
|
2021-09-19 18:28:56 +00:00
|
|
|
weather.drawIcon(current.txt, 65, 90, 55);
|
2020-04-21 21:18:21 +00:00
|
|
|
const locale = require("locale");
|
|
|
|
|
2021-09-18 17:59:22 +00:00
|
|
|
g.reset();
|
2020-04-21 21:18:21 +00:00
|
|
|
|
2021-09-19 18:28:56 +00:00
|
|
|
const temp = locale.temp(current.temp-273.15).match(/^(\D*\d*)(.*)$/);
|
2020-04-21 21:18:21 +00:00
|
|
|
let width = g.setFont("Vector", 40).stringWidth(temp[1]);
|
|
|
|
width += g.setFont("Vector", 20).stringWidth(temp[2]);
|
|
|
|
g.setFont("Vector", 40).setFontAlign(-1, -1, 0);
|
|
|
|
g.drawString(temp[1], 180-width/2, 70);
|
|
|
|
g.setFont("Vector", 20).setFontAlign(1, -1, 0);
|
|
|
|
g.drawString(temp[2], 180+width/2, 70);
|
|
|
|
|
|
|
|
g.setFont("6x8", 1);
|
|
|
|
g.setFontAlign(-1, 0, 0);
|
|
|
|
g.drawString("Humidity", 135, 130);
|
|
|
|
g.setFontAlign(1, 0, 0);
|
2021-09-19 18:28:56 +00:00
|
|
|
g.drawString(current.hum+"%", 225, 130);
|
|
|
|
if ('wind' in current) {
|
2021-06-18 18:16:00 +00:00
|
|
|
g.setFontAlign(-1, 0, 0);
|
|
|
|
g.drawString("Wind", 135, 142);
|
|
|
|
g.setFontAlign(1, 0, 0);
|
2021-09-19 18:28:56 +00:00
|
|
|
g.drawString(locale.speed(current.wind)+' '+current.wrose.toUpperCase(), 225, 142);
|
2021-06-18 18:16:00 +00:00
|
|
|
}
|
2020-04-21 21:18:21 +00:00
|
|
|
|
|
|
|
g.setFont("6x8", 2).setFontAlign(0, 0, 0);
|
2021-09-19 18:28:56 +00:00
|
|
|
g.drawString(current.loc, 120, 170);
|
2020-04-21 21:18:21 +00:00
|
|
|
|
|
|
|
g.setFont("6x8", 1).setFontAlign(0, 0, 0);
|
2021-09-19 18:28:56 +00:00
|
|
|
g.drawString(current.txt.charAt(0).toUpperCase()+current.txt.slice(1), 120, 190);
|
2020-04-21 21:18:21 +00:00
|
|
|
|
2020-06-07 02:26:46 +00:00
|
|
|
drawUpdateTime();
|
2020-05-23 04:44:31 +00:00
|
|
|
|
2020-04-21 21:18:21 +00:00
|
|
|
g.flip();
|
|
|
|
}
|
|
|
|
|
2020-05-24 09:21:24 +00:00
|
|
|
function drawUpdateTime() {
|
2021-09-19 18:28:56 +00:00
|
|
|
if (!current || !current.time) return;
|
|
|
|
let text = `Last update received ${formatDuration(Date.now() - current.time)} ago`;
|
2020-05-23 04:44:31 +00:00
|
|
|
g.reset();
|
2021-09-18 17:59:22 +00:00
|
|
|
g.clearRect(0, 202, 239, 210);
|
|
|
|
g.setFont("6x8", 1).setFontAlign(0, 0, 0);
|
2020-05-23 04:44:31 +00:00
|
|
|
g.drawString(text, 120, 206);
|
|
|
|
}
|
|
|
|
|
2020-05-24 09:21:24 +00:00
|
|
|
function update() {
|
2021-09-19 18:28:56 +00:00
|
|
|
current = weather.get();
|
2021-06-18 17:27:50 +00:00
|
|
|
NRF.removeListener("connect", update);
|
2021-09-19 18:28:56 +00:00
|
|
|
if (current) {
|
2020-05-24 09:21:24 +00:00
|
|
|
draw();
|
2021-06-18 17:27:50 +00:00
|
|
|
} else if (NRF.getSecurityStatus().connected) {
|
|
|
|
E.showMessage("Weather unknown\n\nIs Gadgetbridge\nweather reporting\nset up on your\nphone?");
|
2020-05-24 09:21:24 +00:00
|
|
|
} else {
|
2021-06-18 17:27:50 +00:00
|
|
|
E.showMessage("Weather unknown\n\nGadgetbridge\nnot connected");
|
|
|
|
NRF.on("connect", update);
|
2020-05-23 04:44:31 +00:00
|
|
|
}
|
2020-05-24 09:21:24 +00:00
|
|
|
}
|
2020-04-21 21:18:21 +00:00
|
|
|
|
2020-06-07 02:26:46 +00:00
|
|
|
let interval = setInterval(drawUpdateTime, 60000);
|
2020-05-23 04:44:31 +00:00
|
|
|
Bangle.on('lcdPower', (on) => {
|
|
|
|
if (interval) {
|
|
|
|
clearInterval(interval);
|
|
|
|
interval = undefined;
|
|
|
|
}
|
|
|
|
if (on) {
|
|
|
|
drawUpdateTime();
|
2020-06-07 02:26:46 +00:00
|
|
|
interval = setInterval(drawUpdateTime, 60000);
|
2020-05-23 04:44:31 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-24 09:21:24 +00:00
|
|
|
weather.on("update", update);
|
2020-04-21 21:18:21 +00:00
|
|
|
|
2021-06-18 17:27:50 +00:00
|
|
|
update();
|
2020-04-21 21:18:21 +00:00
|
|
|
|
|
|
|
// Show launcher when middle button pressed
|
2021-09-14 18:57:57 +00:00
|
|
|
Bangle.setUI("clock");
|
2020-05-24 09:21:24 +00:00
|
|
|
|
|
|
|
Bangle.loadWidgets();
|
|
|
|
Bangle.drawWidgets();
|
2020-04-21 21:18:21 +00:00
|
|
|
})()
|