1
0
Fork 0
BangleApps/apps/alarm/lib.js

46 lines
1.8 KiB
JavaScript
Raw Normal View History

2022-03-31 14:32:26 +00:00
// Return an array of all alarms
exports.getAlarms = function() {
return require("Storage").readJSON("alarm.json",1)||[];
};
// Return an alarm object based on ID
exports.getAlarm = function(id) {
var alarms = require("Storage").readJSON("alarm.json",1)||[];
return alarms.find(a=>a.id==id);
};
// Set an alarm object based on ID. Leave 'alarm' undefined to remove it
exports.setAlarm = function(id, alarm) {
var alarms = require("Storage").readJSON("alarm.json",1)||[];
alarms = alarms.filter(a=>a.id!=id);
if (alarm !== undefined) {
alarm.id = id;
if (alarm.dow===undefined) alarm.dow = 0b1111111;
if (alarm.on!==false) alarm.on=true;
if (alarm.timer) { // if it's a timer, set the start time as a time from *now*
var time = new Date();
var currentTime = (time.getHours()*3600000)+(time.getMinutes()*60000)+(time.getSeconds()*1000);
alarm.t = currentTime + alarm.timer;
}
}
alarms.push(alarm);
require("Storage").writeJSON("alarm.json", alarms);
};
/// Get time until the given alarm (object). Return undefined if alarm not enabled, or if 86400000 or more, alarm could me *more* than a day in the future
exports.getTimeToAlarm = function(alarm, time) {
if (!alarm) return undefined;
if (!time) time = new Date();
var active = alarm.on && (alarm.dow>>time.getDay())&1;
if (!active) return undefined;
var currentTime = (time.getHours()*3600000)+(time.getMinutes()*60000)+(time.getSeconds()*1000);
var t = alarm.t-currentTime;
if (alarm.last == time.getDate() || t < -60000) t += 86400000;
return t;
};
/// Force a reload of the current alarms and widget
exports.reload = function() {
eval(require("Storage").read("alarm.boot.js"));
if (WIDGETS["alarm"]) {
WIDGETS["alarm"].reload();
Bangle.drawWidgets();
}
};