2022-08-11 11:07:28 +00:00
|
|
|
(function() {
|
|
|
|
//from 0 to max, the higher the closer to fire (as in a progress bar)
|
2024-10-25 08:36:51 +00:00
|
|
|
function getAlarmValue(a) {
|
|
|
|
let min = Math.round(require('sched').getTimeToAlarm(a)/(60*1000));
|
2022-08-11 11:07:28 +00:00
|
|
|
if(!min) return 0; //not active or more than a day
|
|
|
|
return getAlarmMax(a)-min;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getAlarmMax(a) {
|
|
|
|
if(a.timer)
|
|
|
|
return Math.round(a.timer/(60*1000));
|
|
|
|
//minutes cannot be more than a full day
|
|
|
|
return 1440;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getAlarmIcon(a) {
|
|
|
|
if(a.on) {
|
2024-10-25 08:36:51 +00:00
|
|
|
if(a.timer) return atob("GBiBAAAAAAAAAAAAAAf/4Af/4AGBgAGBgAGBgAD/AAD/AAB+AAA8AAA8AAB+AADnAADDAAGBgAGBgAGBgAf/4Af/4AAAAAAAAAAAAA==");
|
|
|
|
if(a.date) return atob("GBiBAAAAAAAAAAAAAA//8B//+BgAGBgAGBgAGB//+B//+B//+B/++B/8+B/5+B8z+B+H+B/P+B//+B//+B//+A//8AAAAAAAAAAAAA==");
|
|
|
|
return atob("GBiBAAAAAAAAAAYAYA4AcBx+ODn/nAP/wAf/4A/n8A/n8B/n+B/n+B/n+B/n+B/h+B/4+A/+8A//8Af/4AP/wAH/gAB+AAAAAAAAAA==");
|
2022-08-11 11:07:28 +00:00
|
|
|
} else {
|
2024-10-25 08:36:51 +00:00
|
|
|
if(a.timer) return atob("GBiBAAAAAAAAAAAAAAf/4Af/4AGBgAGBgAGBgAD/AAD/AAB+AAA8AAA8AAB+AADkeADB/gGBtgGDAwGDhwfzhwfzAwABtgAB/gAAeA==");
|
|
|
|
if(a.date) return atob("GBgBAAAAAAAAAAAAD//wH//4GAAYGAAYGAAYH//4H//4H//4H/74H/wAH/gAHzB4H4H+H8m2H/MDH/OHH/OHD/MDAAG2AAH+AAB4");
|
|
|
|
return atob("GBiBAAAAAAAAAAYAYA4AcBx+ODn/nAP/wAf/4A/n8A/n8B/n+B/n+B/nAB/mAB/geB/5/g/5tg/zAwfzhwPzhwHzAwB5tgAB/gAAeA==");
|
2022-08-11 11:07:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getAlarmText(a){
|
|
|
|
if(a.timer) {
|
2022-12-01 20:03:15 +00:00
|
|
|
if(!a.on) return /*LANG*/"off";
|
2024-10-25 08:36:51 +00:00
|
|
|
let time = Math.round(require('sched').getTimeToAlarm(a)/(60*1000));
|
2022-08-11 11:07:28 +00:00
|
|
|
if(time > 60)
|
|
|
|
time = Math.round(time / 60) + "h";
|
|
|
|
else
|
|
|
|
time += "m";
|
|
|
|
return time;
|
|
|
|
}
|
2024-10-06 10:16:09 +00:00
|
|
|
if(a.date){
|
|
|
|
const d = new Date(a.date);
|
|
|
|
return `${d.getDate()} ${require("locale").month(d, 1)}`;
|
|
|
|
}
|
2022-08-11 11:07:28 +00:00
|
|
|
return require("time_utils").formatTime(a.t);
|
|
|
|
}
|
|
|
|
|
2022-11-25 07:59:07 +00:00
|
|
|
//workaround for sorting undefined values
|
|
|
|
function getAlarmOrder(a) {
|
2024-10-25 08:36:51 +00:00
|
|
|
let val = require('sched').getTimeToAlarm(a);
|
2022-11-25 07:59:07 +00:00
|
|
|
if(typeof val == "undefined") return 86400*1000;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2023-01-07 14:37:22 +00:00
|
|
|
/*
|
|
|
|
* Returns the array [interval, switchTimeout]
|
|
|
|
* `interval` is the refresh rate (hourly or per minute)
|
|
|
|
* `switchTimeout` is the time before the refresh rate should change (or expiration)
|
|
|
|
*/
|
|
|
|
function getRefreshIntervals(a) {
|
|
|
|
const minute = 60 * 1000;
|
|
|
|
const halfhour = 30 * minute;
|
|
|
|
const hour = 2 * halfhour;
|
2024-10-25 08:36:51 +00:00
|
|
|
let msecs = require('sched').getTimeToAlarm(a);
|
2023-01-07 14:37:22 +00:00
|
|
|
if(typeof msecs == "undefined" || msecs == 0)
|
|
|
|
return [];
|
|
|
|
if(msecs > hour) { //refresh every half an hour
|
|
|
|
let remain = (msecs+minute) % halfhour;
|
|
|
|
if(remain < 27 * minute && remain != 0) //first align period (some tolerance)
|
|
|
|
return [ halfhour, remain ];
|
|
|
|
return [ halfhour, msecs - hour ];
|
|
|
|
} else { //refresh every minute
|
|
|
|
//alarms just need the progress bar refreshed, no need every minute
|
|
|
|
if(!a.timer) return [];
|
|
|
|
return [ minute, msecs ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _doInterval(interval) {
|
|
|
|
return setTimeout(()=>{
|
|
|
|
this.emit("redraw");
|
|
|
|
this.interval = setInterval(()=>{
|
|
|
|
this.emit("redraw");
|
|
|
|
}, interval);
|
|
|
|
}, interval);
|
|
|
|
}
|
|
|
|
function _doSwitchTimeout(a, switchTimeout) {
|
|
|
|
return setTimeout(()=>{
|
|
|
|
this.emit("redraw");
|
|
|
|
clearInterval(this.interval);
|
|
|
|
this.interval = undefined;
|
|
|
|
var tmp = getRefreshIntervals(a);
|
|
|
|
var interval = tmp[0];
|
|
|
|
var switchTimeout = tmp[1];
|
|
|
|
if(!interval) return;
|
|
|
|
this.interval = _doInterval.call(this, interval);
|
|
|
|
this.switchTimeout = _doSwitchTimeout.call(this, a, switchTimeout);
|
|
|
|
}, switchTimeout);
|
|
|
|
}
|
|
|
|
|
2024-10-25 08:36:51 +00:00
|
|
|
// read the file direct here to avoid loading sched library (saves 10ms!)
|
|
|
|
var all = /*require('sched').getAlarms()*/require("Storage").readJSON("sched.json",1)||[];
|
2022-08-11 11:07:28 +00:00
|
|
|
//get only alarms not created by other apps
|
|
|
|
var alarmItems = {
|
2022-12-01 20:03:15 +00:00
|
|
|
name: /*LANG*/"Alarms",
|
2024-10-25 08:36:51 +00:00
|
|
|
img: getAlarmIcon({on:1}),
|
2022-11-25 17:28:20 +00:00
|
|
|
dynamic: true,
|
2024-09-19 13:07:09 +00:00
|
|
|
items: all.filter(a=>!a.appid)
|
2024-10-25 08:36:51 +00:00
|
|
|
//.sort((a,b)=>require('sched').getTimeToAlarm(a)-require('sched').getTimeToAlarm(b))
|
2022-11-25 07:59:07 +00:00
|
|
|
.sort((a,b)=>getAlarmOrder(a)-getAlarmOrder(b))
|
2024-10-09 20:54:45 +00:00
|
|
|
.map(a => ({
|
2022-08-11 11:07:28 +00:00
|
|
|
name: null,
|
2022-11-25 17:28:20 +00:00
|
|
|
hasRange: true,
|
2022-08-11 11:07:28 +00:00
|
|
|
get: () => ({ text: getAlarmText(a), img: getAlarmIcon(a),
|
2022-11-25 17:28:20 +00:00
|
|
|
v: getAlarmValue(a), min:0, max:getAlarmMax(a)}),
|
2023-01-07 14:37:22 +00:00
|
|
|
show: function() {
|
|
|
|
var tmp = getRefreshIntervals(a);
|
|
|
|
var interval = tmp[0];
|
|
|
|
var switchTimeout = tmp[1];
|
|
|
|
if(!interval) return;
|
|
|
|
this.interval = _doInterval.call(this, interval);
|
|
|
|
this.switchTimeout = _doSwitchTimeout.call(this, a, switchTimeout);
|
|
|
|
},
|
|
|
|
hide: function() {
|
2023-01-19 19:22:59 +00:00
|
|
|
if (this.interval) clearInterval(this.interval);
|
|
|
|
if (this.switchTimeout) clearTimeout(this.switchTimeout);
|
2023-01-07 14:37:22 +00:00
|
|
|
this.interval = undefined;
|
|
|
|
this.switchTimeout = undefined;
|
|
|
|
},
|
2024-09-19 13:07:09 +00:00
|
|
|
run: function() {
|
2024-09-22 20:55:11 +00:00
|
|
|
if (a.date) return; // ignore events
|
2024-09-19 13:07:09 +00:00
|
|
|
a.on = !a.on;
|
2024-10-23 20:19:56 +00:00
|
|
|
a.last = 0;
|
2024-10-25 08:36:51 +00:00
|
|
|
if(a.on && a.timer) require('sched').resetTimer(a);
|
2024-09-19 13:07:09 +00:00
|
|
|
this.emit("redraw");
|
2024-10-25 08:36:51 +00:00
|
|
|
require('sched').setAlarms(all);
|
|
|
|
require('sched').reload(); // schedule/unschedule the alarm
|
2024-09-19 13:07:09 +00:00
|
|
|
}
|
2022-08-11 11:07:28 +00:00
|
|
|
})),
|
|
|
|
};
|
|
|
|
return alarmItems;
|
|
|
|
})
|