Show difference of last measurement to pressure average of the the last three hours and an arrow indicating the trend in the widget

master
Marco Heiming 2022-06-22 11:55:07 +02:00
parent 574736f3ff
commit 2dce374a18
6 changed files with 240 additions and 214 deletions

View File

@ -3,3 +3,4 @@
0.03: Fix crash
0.04: Use Prompt with dismiss and pause
Improve barometer value median calculation
0.05: Show difference of last measurement to pressure average of the the last three hours and an arrow indicating the trend in the widget

View File

@ -19,7 +19,9 @@ Get a notification when the pressure reaches defined thresholds.
* Pause delay: Same as Dismiss delay but longer (useful for meetings and such). From 30 to 240 min
## Widget
The widget shows two rows: pressure value of last measurement and pressure average of the the last three hours.
The widget shows two rows:
1. pressure value of last measurement
2. difference of last measurement to pressure average of the the last three hours and an arrow indicating the trend
## Creator
Marco ([myxor](https://github.com/myxor))

View File

@ -9,6 +9,7 @@
"tags": "tool,barometer",
"supports": ["BANGLEJS2"],
"readme": "README.md",
"screenshots": [{"url":"screenshot-dark.png"}],
"storage": [
{"name":"widbaroalarm.wid.js","url":"widget.js"},
{"name":"widbaroalarm.settings.js","url":"settings.js"},

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

View File

@ -11,18 +11,14 @@
let settings;
function loadSettings() {
settings = Object.assign(
storage.readJSON("widbaroalarm.default.json", true) || {},
storage.readJSON(SETTINGS_FILE, true) || {}
);
settings =
Object.assign(storage.readJSON("widbaroalarm.default.json", true) || {},
storage.readJSON(SETTINGS_FILE, true) || {});
}
loadSettings();
function setting(key) {
return settings[key];
}
function setting(key) { return settings[key]; }
function saveSetting(key, value) {
settings[key] = value;
@ -31,19 +27,17 @@
const interval = setting("interval");
let history3 = storage.readJSON(LOG_FILE, true) || []; // history of recent 3 hours
let history3 =
storage.readJSON(LOG_FILE, true) || []; // history of recent 3 hours
function showAlarm(body, key) {
if (body == undefined) return;
if (body == undefined)
return;
stop = true;
E.showPrompt(body, {
title : "Pressure alarm",
buttons: {
"Ok": 1,
"Dismiss": 2,
"Pause": 3
}
buttons : {"Ok" : 1, "Dismiss" : 2, "Pause" : 3}
}).then(function(v) {
const tsNow = Math.round(Date.now() / 1000); // seconds
@ -51,19 +45,20 @@
saveSetting(key, tsNow);
}
if (v == 2) {
// save timestamp of the future so that we do not warn again for the same event until then
// save timestamp of the future so that we do not warn again for the same
// event until then
saveSetting(key, tsNow + 60 * setting('dismissDelayMin'));
}
if (v == 3) {
// save timestamp of the future so that we do not warn again for the same event until then
// save timestamp of the future so that we do not warn again for the same
// event until then
saveSetting(key, tsNow + 60 * setting('pauseDelayMin'));
}
stop = false;
load();
});
if (setting("buzz") &&
!(storage.readJSON('setting.json', 1) || {}).quiet) {
if (setting("buzz") && !(storage.readJSON('setting.json', 1) || {}).quiet) {
Bangle.buzz();
}
@ -73,22 +68,19 @@
}, 20000);
}
function doWeNeedToWarn(key) {
const tsNow = Math.round(Date.now() / 1000); // seconds
return setting(key) == 0 || setting(key) < tsNow;
}
function checkForAlarms(pressure) {
if (pressure == undefined || pressure <= 0) return;
if (pressure == undefined || pressure <= 0)
return;
let alreadyWarned = false;
const ts = Math.round(Date.now() / 1000); // seconds
const d = {
"ts": ts,
"p": pressure
};
const d = {"ts" : ts, "p" : pressure};
// delete entries older than 3h
for (let i = 0; i < history3.length; i++) {
@ -105,7 +97,8 @@
// Is below the alarm threshold?
if (pressure <= setting("min")) {
if (!doWeNeedToWarn("lastLowWarningTs")) {
showAlarm("Pressure low: " + Math.round(pressure) + " hPa", "lastLowWarningTs");
showAlarm("Pressure low: " + Math.round(pressure) + " hPa",
"lastLowWarningTs");
alreadyWarned = true;
}
} else {
@ -117,7 +110,8 @@
// Is above the alarm threshold?
if (pressure >= setting("max")) {
if (doWeNeedToWarn("lastHighWarningTs")) {
showAlarm("Pressure high: " + Math.round(pressure) + " hPa", "lastHighWarningTs");
showAlarm("Pressure high: " + Math.round(pressure) + " hPa",
"lastHighWarningTs");
alreadyWarned = true;
}
} else {
@ -146,7 +140,9 @@
if (diffPressure > drop3halarm) {
if (doWeNeedToWarn("lastDropWarningTs")) {
showAlarm((Math.round(diffPressure * 10) / 10) + " hPa/3h from " +
Math.round(oldestPressure) + " to " + Math.round(pressure) + " hPa", "lastDropWarningTs");
Math.round(oldestPressure) + " to " +
Math.round(pressure) + " hPa",
"lastDropWarningTs");
}
} else {
saveSetting("lastDropWarningTs", 0);
@ -160,7 +156,9 @@
if (diffPressure > raise3halarm) {
if (doWeNeedToWarn("lastRaiseWarningTs")) {
showAlarm((Math.round(diffPressure * 10) / 10) + " hPa/3h from " +
Math.round(oldestPressure) + " to " + Math.round(pressure) + " hPa", "lastRaiseWarningTs");
Math.round(oldestPressure) + " to " +
Math.round(pressure) + " hPa",
"lastRaiseWarningTs");
}
} else {
saveSetting("lastRaiseWarningTs", 0);
@ -176,8 +174,11 @@
// write data to storage
storage.writeJSON(LOG_FILE, history3);
// calculate 3h average for widget
if (history3.length > 0) {
calculcate3hAveragePressure();
}
function calculcate3hAveragePressure() {
if (history3 != undefined && history3.length > 0) {
let sum = 0;
for (let i = 0; i < history3.length; i++) {
sum += history3[i]["p"];
@ -188,7 +189,6 @@
}
}
/*
turn on barometer power
take multiple measurements
@ -197,11 +197,13 @@
turn off barometer power
*/
function check() {
if (stop) return;
if (stop)
return;
const MEDIANLENGTH = 20;
Bangle.setBarometerPower(true, "widbaroalarm");
Bangle.on('pressure', function(e) {
while (currentPressures.length > MEDIANLENGTH) currentPressures.pop();
while (currentPressures.length > MEDIANLENGTH)
currentPressures.pop();
currentPressures.unshift(e.pressure);
median = currentPressures.slice().sort();
@ -215,9 +217,7 @@
}
});
setTimeout(function() {
turnOff();
}, 10000);
setTimeout(function() { turnOff(); }, 10000);
}
function turnOff() {
@ -225,9 +225,7 @@
Bangle.setBarometerPower(false, "widbaroalarm");
}
function reload() {
check();
}
function reload() { check(); }
function draw() {
if (global.WIDGETS != undefined && typeof global.WIDGETS === "object") {
@ -241,21 +239,46 @@
g.reset();
if (setting("show")) {
g.setFont("6x8", 1).setFontAlign(1, 0);
const x = this.x, y = this.y;
if (medianPressure == undefined) {
// trigger a new check
check();
const x = this.x,
y = this.y;
g.drawString("...", x + 24, y + 6);
setTimeout(function() {
g.setFont("6x8", 1).setFontAlign(1, 0);
g.drawString(Math.round(medianPressure), x + 24, y + 6);
}, 10000);
} else {
g.drawString(Math.round(medianPressure), this.x + 24, this.y + 6);
// lets load last value from log (if available)
if (history3.length > 0) {
medianPressure = history3[history3.length - 1]["p"];
}
if (threeHourAvrPressure != undefined && threeHourAvrPressure > 0) {
g.drawString(Math.round(threeHourAvrPressure), this.x + 24, this.y + 6 + 10);
g.drawString("...", x + 24, y + 6);
g.setFont("6x8", 1).setFontAlign(1, 0);
g.drawString(Math.round(medianPressure), x + 24, y + 6);
} else {
g.drawString(Math.round(medianPressure), x + 24, y + 6);
}
if (threeHourAvrPressure == undefined) {
calculcate3hAveragePressure();
}
if (threeHourAvrPressure != undefined) {
if (medianPressure != undefined) {
let icon;
const diff = threeHourAvrPressure - medianPressure;
if (diff == 0) {
icon = atob("DAyBAAAAAAAAIAEAGD+AEAIAAAAAAA=="); // allow right
} else {
if (diff < 0) {
icon = atob("DAyBAAAAAAAAfAHALATAgBAAAAAAAA=="); // arrow up
} else {
icon = atob("DAyBAAAAAAABAAgATALAHAHAeAAAAA=="); // arrow down
}
}
g.drawImage(icon, x, y + 6 + 4);
g.drawString((diff < 0 ? "+" : "-" ) + Math.round(diff), x + 24, y + 6 + 10);
}
}
}
}
@ -264,5 +287,4 @@
setInterval(check, interval * 60000);
}
draw();
})();