BangleApps/apps/chargent/boot.js

46 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-01-16 14:50:15 +00:00
(() => {
2023-01-29 17:00:47 +00:00
const pin = process.env.HWVERSION === 2 ? D3 : D30;
2023-01-16 14:50:15 +00:00
var id;
2023-07-01 08:48:43 +00:00
function gent(charging) {
2023-01-16 14:50:15 +00:00
if (charging) {
if (!id) {
var max = 0;
2023-02-01 14:08:39 +00:00
var cnt = 0;
var sum = 0;
var lim = (require('Storage').readJSON('chargent.json', true) || {}).limit || 0;
2023-01-16 14:50:15 +00:00
id = setInterval(() => {
2023-02-01 14:08:39 +00:00
var val = analogRead(pin);
if (max < val) {
max = val;
cnt = 1;
sum = val;
2023-01-16 14:50:15 +00:00
} else {
2023-02-01 14:08:39 +00:00
cnt++;
sum += val;
}
if (10 < cnt || (lim && lim <= max)) { // 10 * 30s == 5 min // TODO ? customizable
if (!lim) {
lim = sum / cnt;
require('Storage').writeJSON('chargent.json', {limit: lim});
2023-01-16 14:50:15 +00:00
}
const onHide = () => { if(id) id = clearInterval(id) };
require('notify').show({id: 'chargent', title: 'Charged', onHide });
2023-02-01 14:08:39 +00:00
// TODO ? customizable
Bangle.buzz(500);
setTimeout(() => Bangle.buzz(500), 1000);
2023-01-16 14:50:15 +00:00
}
2023-04-06 21:51:16 +00:00
}, 3e4);
2023-01-16 14:50:15 +00:00
}
} else {
if (id) {
2023-04-06 21:51:16 +00:00
id = clearInterval(id);
require('notify').hide({id: 'chargent'});
2023-01-16 14:50:15 +00:00
}
}
2023-07-01 08:48:43 +00:00
}
Bangle.on('charging', gent);
if (Bangle.isCharging()) gent(true);
2023-01-16 14:50:15 +00:00
})();