[sleeplogalarm] Corrections, prevent retrigger

master
storm64 2022-11-12 01:42:31 +01:00
parent bc99316979
commit 111a194e57
2 changed files with 20 additions and 15 deletions

View File

@ -7,16 +7,15 @@ function getNextAlarm(allAlarms, from, to, withId) {
a.idx = idx;
return a;
});
// return next
return sched.getActiveAlarms(
// filter for active alarms in range
allAlarms.filter(a => a.on && !a.timer && a.t >= from && a.t < to)
).map(a => {
// add time to alarm
// return next active alarms in range
return allAlarms.filter(
a => a.on && !a.timer && a.t >= from && a.t < to
).map(a => { // add time to alarm
a.tTo = sched.getTimeToAlarm(a);
return a;
}).filter(a => a.tTo // filter non active alarms
// sort to get next alarm first
}).sort((a, b) => a.tTo - b.tTo)[0] || {};
).sort((a, b) => a.tTo - b.tTo)[0] || {};
}
exports = {
@ -44,7 +43,7 @@ exports = {
if (typeof (global.sleeplog || {}).onChange !== "object") return;
// read settings to calculate alarm range
var settings = this.getSettings();
var settings = exports.getSettings();
// set the alarm time
this.time = getNextAlarm(sched.getAlarms(), settings.from * 36E5, settings.to * 36E5).t;
@ -55,6 +54,9 @@ exports = {
// set widget width if not hidden
if (!this.hidden) this.width = 8;
// abort if already alarmed for this alarm
if (sleeplog.onChange.sleeplogalarm == this.time) return;
// insert sleeplogalarm function to onChange
sleeplog.onChange.sleeplogalarm = function (data) {
// abort if not changed from deep sleep to light sleep or awake
@ -74,7 +76,7 @@ exports = {
// trigger function
trigger: function(now, tNow) {
// read settings
var settings = this.getSettings();
var settings = exports.getSettings();
// read all alarms
var allAlarms = sched.getAlarms();
@ -82,6 +84,9 @@ exports = {
// find first active alarm
var alarm = getNextAlarm(sched.getAlarms(), settings.from * 36E5, settings.to * 36E5, settings.disableOnAlarm);
// clear sleeplog.onChange function and set alarm time to prevent resetting for this alarm
sleeplog.onChange.sleeplogalarm = alarm.t;
// return if no alarm is found
if (!alarm) return;

View File

@ -1,7 +1,7 @@
// check if enabled in settings
if ((require("Storage").readJSON("sleeplogalarm.settings.json", true) || {enabled: true}).enabled) {
// read settings
var settings = require("sleeplogalarm").getSettings();
settings = require("sleeplogalarm").getSettings(); // is undefined if used with var
// insert neccessary settings into widget
WIDGETS.sleeplogalarm = {
@ -13,17 +13,17 @@ if ((require("Storage").readJSON("sleeplogalarm.settings.json", true) || {enable
// draw zzz
g.reset().setColor(settings.color).drawImage(atob("BwoBD8SSSP4EEEDg"), this.x + 1, this.y);
// call function to draw the time of alarm if a alarm is found
if (this.time) this.drawTime(this.time);
if (this.time) this.drawTime(this.time + 1);
},
drawTime: () => {},
reload: require("sleeplogalarm").widReload()
reload: require("sleeplogalarm").widReload
};
// add function to draw the time of alarm if enabled
if (this.drawTime) WIDGETS.sleeplogalarm.drawTime = function(time) {
if (settings.drawTime) WIDGETS.sleeplogalarm.drawTime = function(time) {
// directly include Font4x5Numeric
g.setFontCustom(atob("CAZMA/H4PgvXoK1+DhPg7W4P1uCEPg/X4O1+AA=="), 46, atob("AgQEAgQEBAQEBAQE"), 5).setFontAlign(1, 1);
g.drawString(0|(time / 36E5), this.x + this.width + 1, this.y + 12);
g.drawString(0|(time / 36E5), this.x + this.width + 1, this.y + 17);
g.drawString(0|((time / 36E5)%1 * 60), this.x + this.width + 1, this.y + 23);
};