BangleApps/apps/alarm/alarm.js

87 lines
2.4 KiB
JavaScript
Raw Normal View History

2020-02-28 15:34:31 +00:00
// Chances are boot0.js got run already and scheduled *another*
// 'load(alarm.js)' - so let's remove it first!
2022-03-31 12:48:45 +00:00
if (Bangle.ALARM) {
clearInterval(Bangle.ALARM);
delete Bangle.ALARM;
}
// time in ms -> { hrs, mins }
function decodeTime(t) {
t = 0|t; // sanitise
var hrs = 0|(t/3600000);
return { hrs : hrs, mins : Math.round((t-hrs*3600000)/60000) };
}
// time in { hrs, mins } -> ms
function encodeTime(o) {
return o.hrs*3600000 + o.mins*60000;
}
2020-02-28 15:34:31 +00:00
function formatTime(t) {
2022-03-31 12:48:45 +00:00
var o = decodeTime(t);
return o.hrs+":"+("0"+o.mins).substr(-2);
2020-02-28 15:34:31 +00:00
}
2022-03-31 12:48:45 +00:00
function getCurrentTime() {
2020-02-28 15:34:31 +00:00
var time = new Date();
2022-03-31 12:48:45 +00:00
return (
time.getHours() * 3600000 +
time.getMinutes() * 60000 +
time.getSeconds() * 1000
);
2020-02-28 15:34:31 +00:00
}
function showAlarm(alarm) {
2022-03-31 12:48:45 +00:00
var msg = alarm.timer ? formatTime(alarm.timer) : formatTime(alarm.t);
2020-02-28 15:34:31 +00:00
var buzzCount = 10;
if (alarm.msg)
msg += "\n"+alarm.msg;
Bangle.loadWidgets();
Bangle.drawWidgets();
2020-02-28 15:34:31 +00:00
E.showPrompt(msg,{
2021-12-15 10:11:02 +00:00
title:alarm.timer ? /*LANG*/"TIMER!" : /*LANG*/"ALARM!",
buttons : {/*LANG*/"Sleep":true,/*LANG*/"Ok":false} // default is sleep so it'll come back in 10 mins
2020-02-28 15:34:31 +00:00
}).then(function(sleep) {
buzzCount = 0;
if (sleep) {
2022-03-31 12:48:45 +00:00
if(alarm.ot===undefined) alarm.ot = alarm.t;
alarm.t += 10*60*1000; // 10 minutes
2020-02-28 15:34:31 +00:00
} else {
alarm.last = (new Date()).getDate();
2022-03-31 12:48:45 +00:00
if (alarm.ot!==undefined) {
alarm.t = alarm.ot;
delete alarm.ot;
2020-06-01 17:22:04 +00:00
}
2020-02-28 15:34:31 +00:00
if (!alarm.rp) alarm.on = false;
}
require("Storage").write("alarm.json",JSON.stringify(alarms));
load();
});
function buzz() {
2022-04-01 12:27:37 +00:00
require("buzz").pattern(alarm.vibrate===undefined?"..":alarm.vibrate).then(function() {
if (buzzCount--)
setTimeout(buzz, 3000);
else if(alarm.as) { // auto-snooze
buzzCount = 10;
setTimeout(buzz, 600000);
}
2020-02-28 15:34:31 +00:00
});
}
2022-04-01 12:27:37 +00:00
if ((require('Storage').readJSON('setting.json',1)||{}).quiet>1) return;
2020-02-28 15:34:31 +00:00
buzz();
}
// Check for alarms
var day = (new Date()).getDate();
2022-03-31 12:48:45 +00:00
var currentTime = getCurrentTime()+10000; // get current time - 10s in future to ensure we alarm if we've started the app a tad early
var alarms = require("Storage").readJSON("alarm.json",1)||[];
2022-03-31 12:48:45 +00:00
var active = alarms.filter(a=>a.on&&(a.t<currentTime)&&(a.last!=day));
2020-02-28 15:34:31 +00:00
if (active.length) {
// if there's an alarm, show it
2022-03-31 12:48:45 +00:00
active = active.sort((a,b)=>a.t-b.t);
2020-02-28 15:34:31 +00:00
showAlarm(active[0]);
} else {
// otherwise just go back to default app
setTimeout(load, 100);
2020-02-28 15:34:31 +00:00
}