Use median calculation instead of average to get valid measurements
parent
1e7b9f1b65
commit
cf365dc451
|
|
@ -1,51 +1,47 @@
|
||||||
(function() {
|
(function() {
|
||||||
let lastPressure;
|
let medianPressure;
|
||||||
let avrPressure;
|
let threeHourAvrPressure;
|
||||||
let threeHourAvrPressure;
|
let currentPressures = [];
|
||||||
|
|
||||||
const LOG_FILE = "widbaroalarm.log.json";
|
const LOG_FILE = "widbaroalarm.log.json";
|
||||||
const SETTINGS_FILE = "widbaroalarm.json";
|
const SETTINGS_FILE = "widbaroalarm.json";
|
||||||
const storage = require('Storage');
|
const storage = require('Storage');
|
||||||
let settings = Object.assign(
|
let settings = Object.assign(
|
||||||
storage.readJSON("widbaroalarm.default.json", true) || {},
|
storage.readJSON("widbaroalarm.default.json", true) || {},
|
||||||
storage.readJSON(SETTINGS_FILE, true) || {}
|
storage.readJSON(SETTINGS_FILE, true) || {}
|
||||||
);
|
);
|
||||||
|
|
||||||
function setting(key) {
|
function setting(key) {
|
||||||
return settings[key];
|
return settings[key];
|
||||||
}
|
|
||||||
const interval = setting("interval");
|
|
||||||
|
|
||||||
let history3 = storage.readJSON(LOG_FILE, true) || []; // history of recent 3 hours
|
|
||||||
|
|
||||||
function showAlarm(body, title) {
|
|
||||||
if (body == undefined) return;
|
|
||||||
|
|
||||||
require("notify").show({
|
|
||||||
title: title || "Pressure",
|
|
||||||
body: body,
|
|
||||||
icon: require("heatshrink").decompress(atob("jEY4cA///gH4/++mkK30kiWC4H8x3BGDmSGgYDCgmSoEAg3bsAIDpAIFkmSpMAm3btgIFDQwIGNQpTYkAIJwAHEgMoCA0JgMEyBnBCAW3KoQQDhu3oAIH5JnDBAW24IIBEYm2EYwACBCIACA"))
|
|
||||||
});
|
|
||||||
|
|
||||||
if (setting("buzz") &&
|
|
||||||
!(storage.readJSON('setting.json', 1) || {}).quiet) {
|
|
||||||
Bangle.buzz();
|
|
||||||
}
|
}
|
||||||
}
|
const interval = setting("interval");
|
||||||
|
|
||||||
let alreadyWarned = false;
|
let history3 = storage.readJSON(LOG_FILE, true) || []; // history of recent 3 hours
|
||||||
|
|
||||||
function baroHandler(data) {
|
function showAlarm(body, title) {
|
||||||
if (data === undefined) {
|
if (body == undefined) return;
|
||||||
setTimeout(() => Bangle.getPressure().then(baroHandler), 500);
|
|
||||||
} else {
|
require("notify").show({
|
||||||
lastPressure = Math.round(data.pressure);
|
title: title || "Pressure",
|
||||||
if (lastPressure == undefined || lastPressure <= 0) return;
|
body: body,
|
||||||
|
icon: require("heatshrink").decompress(atob("jEY4cA///gH4/++mkK30kiWC4H8x3BGDmSGgYDCgmSoEAg3bsAIDpAIFkmSpMAm3btgIFDQwIGNQpTYkAIJwAHEgMoCA0JgMEyBnBCAW3KoQQDhu3oAIH5JnDBAW24IIBEYm2EYwACBCIACA"))
|
||||||
|
});
|
||||||
|
|
||||||
|
if (setting("buzz") &&
|
||||||
|
!(storage.readJSON('setting.json', 1) || {}).quiet) {
|
||||||
|
Bangle.buzz();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let alreadyWarned = false;
|
||||||
|
|
||||||
|
function checkForAlarms(pressure) {
|
||||||
|
if (pressure == undefined || pressure <= 0) return;
|
||||||
|
|
||||||
const ts = Math.round(Date.now() / 1000); // seconds
|
const ts = Math.round(Date.now() / 1000); // seconds
|
||||||
const d = {
|
const d = {
|
||||||
"ts": ts,
|
"ts": ts,
|
||||||
"p": lastPressure
|
"p": pressure
|
||||||
};
|
};
|
||||||
|
|
||||||
// delete entries older than 3h
|
// delete entries older than 3h
|
||||||
|
|
@ -63,101 +59,127 @@
|
||||||
// write data to storage
|
// write data to storage
|
||||||
storage.writeJSON(LOG_FILE, history3);
|
storage.writeJSON(LOG_FILE, history3);
|
||||||
|
|
||||||
// we need at least three entries for reliable detection
|
if (setting("lowalarm") && pressure <= setting("min")) {
|
||||||
if (history3.length >= 3) {
|
showAlarm("Pressure low: " + Math.round(pressure) + " hPa");
|
||||||
|
alreadyWarned = true;
|
||||||
|
}
|
||||||
|
if (setting("highalarm") && pressure >= setting("max")) {
|
||||||
|
showAlarm("Pressure high: " + Math.round(pressure) + " hPa");
|
||||||
|
alreadyWarned = true;
|
||||||
|
}
|
||||||
|
|
||||||
// calculate average of recent three entries
|
if (!alreadyWarned) {
|
||||||
avrPressure = (history3[history3.length - 1]["p"] + history3[history3.length - 2]["p"] + history3[history3.length - 3]["p"]) / 3;
|
// 3h change detection
|
||||||
|
const drop3halarm = setting("drop3halarm");
|
||||||
|
const raise3halarm = setting("raise3halarm");
|
||||||
|
if (drop3halarm > 0 || raise3halarm > 0) {
|
||||||
|
// we need at least 30min of data for reliable detection
|
||||||
|
if (history3[0]["ts"] > ts - (30 * 60)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (setting("lowalarm") && avrPressure <= setting("min")) {
|
// Get oldest entry:
|
||||||
showAlarm("Pressure low: " + Math.round(avrPressure) + " hPa");
|
const oldestPressure = history3[0]["p"];
|
||||||
alreadyWarned = true;
|
if (oldestPressure != undefined && oldestPressure > 0) {
|
||||||
}
|
const diff = oldestPressure - pressure;
|
||||||
if (setting("highalarm") && avrPressure >= setting("max")) {
|
|
||||||
showAlarm("Pressure high: " + Math.round(avrPressure) + " hPa");
|
|
||||||
alreadyWarned = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!alreadyWarned) {
|
// drop alarm
|
||||||
// 3h change detection
|
if (drop3halarm > 0 && oldestPressure > pressure) {
|
||||||
const drop3halarm = setting("drop3halarm");
|
if (Math.abs(diff) > drop3halarm) {
|
||||||
const raise3halarm = setting("raise3halarm");
|
showAlarm((Math.round(Math.abs(diff) * 10) / 10) + " hPa/3h from " +
|
||||||
if (drop3halarm > 0 || raise3halarm > 0) {
|
Math.round(oldestPressure) + " to " + Math.round(pressure) + " hPa", "Pressure drop");
|
||||||
// we need at least 30min of data for reliable detection
|
|
||||||
if (history3[0]["ts"] > ts - (30 * 60)) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Average of oldest three entries
|
// raise alarm
|
||||||
const oldestAvgPressure = (history3[0]["p"] + history3[1]["p"] + history3[2]["p"]) / 3;
|
if (raise3halarm > 0 && oldestPressure < pressure) {
|
||||||
if (oldestAvgPressure != undefined && oldestAvgPressure > 0) {
|
if (Math.abs(diff) > raise3halarm) {
|
||||||
const diff = oldestAvgPressure - avrPressure;
|
showAlarm((Math.round(Math.abs(diff) * 10) / 10) + " hPa/3h from " +
|
||||||
|
Math.round(oldestPressure) + " to " + Math.round(pressure) + " hPa", "Pressure raise");
|
||||||
// drop alarm
|
|
||||||
if (drop3halarm > 0 && oldestAvgPressure > avrPressure) {
|
|
||||||
if (Math.abs(diff) > drop3halarm) {
|
|
||||||
showAlarm((Math.round(Math.abs(diff) * 10) / 10) + " hPa/3h from " +
|
|
||||||
Math.round(oldestAvgPressure) + " to " + Math.round(avrPressure) + " hPa",
|
|
||||||
"Pressure drop"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// raise alarm
|
|
||||||
if (raise3halarm > 0 && oldestAvgPressure < avrPressure) {
|
|
||||||
if (Math.abs(diff) > raise3halarm) {
|
|
||||||
showAlarm((Math.round(Math.abs(diff) * 10) / 10) + " hPa/3h from " +
|
|
||||||
Math.round(oldestAvgPressure) + " to " + Math.round(avrPressure) + " hPa",
|
|
||||||
"Pressure raise"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// calculate 3h average for widget
|
|
||||||
let sum = 0;
|
|
||||||
for (let i = 0; i < history3.length; i++) {
|
|
||||||
sum += history3[i]["p"];
|
|
||||||
}
|
|
||||||
threeHourAvrPressure = sum / history3.length;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function check() {
|
// calculate 3h average for widget
|
||||||
Bangle.getPressure().then(baroHandler);
|
let sum = 0;
|
||||||
|
for (let i = 0; i < history3.length; i++) {
|
||||||
|
sum += history3[i]["p"];
|
||||||
}
|
}
|
||||||
|
threeHourAvrPressure = sum / history3.length;
|
||||||
|
}
|
||||||
|
|
||||||
function reload() {
|
|
||||||
check();
|
|
||||||
|
function baroHandler(data) {
|
||||||
|
if (data) {
|
||||||
|
const pressure = Math.round(data.pressure);
|
||||||
|
if (pressure == undefined || pressure <= 0) return;
|
||||||
|
currentPressures.push(pressure);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function draw() {
|
/*
|
||||||
g.reset();
|
turn on barometer power
|
||||||
if (setting("show") && lastPressure != undefined) {
|
take 5 measurements
|
||||||
g.setFont("6x8", 1).setFontAlign(1, 0);
|
sort the results
|
||||||
g.drawString(Math.round(lastPressure), this.x + 24, this.y + 6);
|
take the middle one (median)
|
||||||
if (threeHourAvrPressure != undefined && threeHourAvrPressure > 0) {
|
turn off barometer power
|
||||||
g.drawString(Math.round(threeHourAvrPressure), this.x + 24, this.y + 6 + 10);
|
*/
|
||||||
}
|
function check() {
|
||||||
}
|
Bangle.setBarometerPower(true, "widbaroalarm");
|
||||||
}
|
|
||||||
|
|
||||||
if (global.WIDGETS != undefined && typeof WIDGETS === "object") {
|
|
||||||
WIDGETS["baroalarm"] = {
|
|
||||||
width: setting("show") ? 24 : 0,
|
|
||||||
reload: reload,
|
|
||||||
area: "tr",
|
|
||||||
draw: draw
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Let's delay the first check a bit
|
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
check();
|
currentPressures = [];
|
||||||
if (interval > 0) {
|
|
||||||
setInterval(check, interval * 60000);
|
Bangle.getPressure().then(baroHandler);
|
||||||
|
Bangle.getPressure().then(baroHandler);
|
||||||
|
Bangle.getPressure().then(baroHandler);
|
||||||
|
Bangle.getPressure().then(baroHandler);
|
||||||
|
Bangle.getPressure().then(baroHandler);
|
||||||
|
|
||||||
|
setTimeout(function() {
|
||||||
|
Bangle.setBarometerPower(false, "widbaroalarm");
|
||||||
|
|
||||||
|
currentPressures.sort();
|
||||||
|
|
||||||
|
// take median value
|
||||||
|
medianPressure = currentPressures[3];
|
||||||
|
checkForAlarms(medianPressure);
|
||||||
|
}, 1000);
|
||||||
|
}, 500);
|
||||||
|
}
|
||||||
|
|
||||||
|
function reload() {
|
||||||
|
check();
|
||||||
|
}
|
||||||
|
|
||||||
|
function draw() {
|
||||||
|
g.reset();
|
||||||
|
if (setting("show") && medianPressure != undefined) {
|
||||||
|
g.setFont("6x8", 1).setFontAlign(1, 0);
|
||||||
|
g.drawString(Math.round(medianPressure), this.x + 24, this.y + 6);
|
||||||
|
if (threeHourAvrPressure != undefined && threeHourAvrPressure > 0) {
|
||||||
|
g.drawString(Math.round(threeHourAvrPressure), this.x + 24, this.y + 6 + 10);
|
||||||
}
|
}
|
||||||
}, 10000);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (global.WIDGETS != undefined && typeof WIDGETS === "object") {
|
||||||
|
WIDGETS["baroalarm"] = {
|
||||||
|
width: setting("show") ? 24 : 0,
|
||||||
|
reload: reload,
|
||||||
|
area: "tr",
|
||||||
|
draw: draw
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Let's delay the first check a bit
|
||||||
|
setTimeout(function() {
|
||||||
|
check();
|
||||||
|
if (interval > 0) {
|
||||||
|
setInterval(check, interval * 60000);
|
||||||
|
}
|
||||||
|
}, 5000);
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue