1
0
Fork 0

Fix issues with alarm scheduling

master
Gordon Williams 2020-02-28 15:34:31 +00:00
parent 17010f62a2
commit 1354227b6a
6 changed files with 83 additions and 65 deletions

View File

@ -2,7 +2,7 @@
{ "id": "boot",
"name": "Bootloader",
"icon": "bootloader.png",
"version":"0.06",
"version":"0.07",
"description": "This is needed by Bangle.js to automatically load the clock, menu, widgets and settings",
"tags": "tool,system",
"type":"bootloader",
@ -93,11 +93,12 @@
"name": "Default Alarm",
"shortName":"Alarms",
"icon": "app.png",
"version":"0.01",
"version":"0.02",
"description": "Set and respond to alarms",
"tags": "tool,alarm,widget",
"storage": [
{"name":"alarm.app.js","url":"app.js"},
{"name":"alarm.js","url":"alarm.js"},
{"name":"alarm.json","content":"[]"},
{"name":"alarm.img","url":"app-icon.js","evaluate":true},
{"name":"alarm.wid.js","url":"widget.js"}

View File

@ -1 +1,2 @@
0.01: New App!
0.02: Fix issues with alarm scheduling

60
apps/alarm/alarm.js Normal file
View File

@ -0,0 +1,60 @@
// Chances are boot0.js got run already and scheduled *another*
// 'load(alarm.js)' - so let's remove it first!
clearInterval();
function formatTime(t) {
var hrs = 0|t;
var mins = Math.round((t-hrs)*60);
return hrs+":"+("0"+mins).substr(-2);
}
function getCurrentHr() {
var time = new Date();
return time.getHours()+(time.getMinutes()/60);
}
function showAlarm(alarm) {
var msg = formatTime(alarm.hr);
var buzzCount = 10;
if (alarm.msg)
msg += "\n"+alarm.msg;
E.showPrompt(msg,{
title:"ALARM!",
buttons : {"Sleep":true,"Ok":false} // default is sleep so it'll come back in 10 mins
}).then(function(sleep) {
buzzCount = 0;
if (sleep) {
alarm.hr += 10/60; // 10 minutes
} else {
alarm.last = (new Date()).getDate();
if (!alarm.rp) alarm.on = false;
}
require("Storage").write("alarm.json",JSON.stringify(alarms));
load();
});
function buzz() {
Bangle.buzz(100).then(()=>{
setTimeout(()=>{
Bangle.buzz(100).then(function() {
if (buzzCount--)
setTimeout(buzz, 3000);
});
},100);
});
}
buzz();
}
// Check for alarms
var day = (new Date()).getDate();
var hr = getCurrentHr();
var alarms = require("Storage").readJSON("alarm.json")||[];
var active = alarms.filter(a=>a.on&&(a.hr<hr)&&(a.last!=day));
if (active.length) {
// if there's an alarm, show it
active = active.sort((a,b)=>a.hr-b.hr);
showAlarm(active[0]);
} else {
// otherwise just go back to default app
load();
}

View File

@ -13,7 +13,7 @@ var alarms = require("Storage").readJSON("alarm.json")||[];
function formatTime(t) {
var hrs = 0|t;
var mins = 0|((t-hrs)*60);
var mins = Math.round((t-hrs)*60);
return hrs+":"+("0"+mins).substr(-2);
}
@ -47,7 +47,7 @@ function editAlarm(alarmIndex) {
if (!newAlarm) {
var a = alarms[alarmIndex];
hrs = 0|a.hr;
mins = 0|((a.hr-hrs)*60);
mins = Math.round((a.hr-hrs)*60);
en = a.on;
repeat = a.rp;
}
@ -55,13 +55,11 @@ function editAlarm(alarmIndex) {
'': { 'title': 'Alarms' },
'Hours': {
value: hrs,
min: 0,
max: 23,
onchange: v=>{if (v<0)v=23;if (v>23)v=0;hrs=v;this.value=v;}
onchange: function(v){if (v<0)v=23;if (v>23)v=0;hrs=v;this.value=v;} // no arrow fn -> preserve 'this'
},
'Minutes': {
value: mins,
onchange: v=>{if (v<0)v=59;if (v>59)v=0;mins=v;this.value=v;}
onchange: function(v){if (v<0)v=59;if (v>59)v=0;mins=v;this.value=v;} // no arrow fn -> preserve 'this'
},
'Enabled': {
value: en,
@ -77,8 +75,8 @@ function editAlarm(alarmIndex) {
function getAlarm() {
var hr = hrs+(mins/60);
var day = 0;
// If alarm is for tomorrow not today, set day
if (hr > getCurrentHr())
// If alarm is for tomorrow not today (eg, in the past), set day
if (hr < getCurrentHr())
day = (new Date()).getDate();
// Save alarm
return {
@ -103,47 +101,4 @@ function editAlarm(alarmIndex) {
return E.showMenu(menu);
}
function showAlarm(alarm) {
var msg = formatTime(alarm.hr);
var buzzCount = 10;
if (alarm.msg)
msg += "\n"+alarm.msg;
E.showPrompt(msg,{
title:"ALARM!",
buttons : {"Sleep":true,"Ok":false} // default is sleep so it'll come back in 10 mins
}).then(function(sleep) {
buzzCount = 0;
if (sleep) {
alarm.hr += 10/60; // 10 minutes
} else {
alarm.last = (new Date()).getDate();
if (!alarm.rp) alarm.on = false;
}
require("Storage").write("alarm.json",JSON.stringify(alarms));
load();
});
function buzz() {
Bangle.buzz(100).then(()=>{
setTimeout(()=>{
Bangle.buzz(100).then(function() {
if (buzzCount--)
setTimeout(buzz, 3000);
});
},100);
});
}
buzz();
}
// Check for alarms
var day = (new Date()).getDate();
var hr = getCurrentHr();
var active = alarms.filter(a=>a.on&&(a.hr<hr)&&(a.last!=day));
if (active.length) {
// if there's an alarm, show it
active = active.sort((a,b)=>a.hr-b.hr);
showAlarm(active[0]);
} else {
// otherwise show the main menu
showMainMenu();
}
showMainMenu();

View File

@ -3,3 +3,4 @@
0.04: Add alarm functionality
0.05: Add Welcome screen on boot
0.06: Disable GPS time log messages, add (default=1) setting to hide log messages
0.07: Fix issues with alarm scheduling

View File

@ -31,20 +31,20 @@ function checkAlarm() {
var active = alarms.filter(a=>a.on&&(a.last!=time.getDate()));
if (active.length) {
active = active.sort((a,b)=>a.hr-b.hr);
var hr = time.getHours()+(time.getMinutes()/60);
if (!require('Storage').read("alarm.app.js")) {
var hr = time.getHours()+(time.getMinutes()/60)+(time.getSeconds()/3600);
if (!require('Storage').read("alarm.js")) {
console.log("No alarm app!");
require('Storage').write('alarm.json',"[]")
} else {
if (active[0].hr < hr) {
// fire alarm now
load("alarm.app.js");
} else {
// execute alarm at the correct time
setTimeout(function() {
load("alarm.app.js");
},3600000*(active[0].hr-hr));
}
var t = 3600000*(active[0].hr-hr);
if (t<1000) t=1000;
/* execute alarm at the correct time. We avoid execing immediately
since this code will get called AGAIN when alarm.js is loaded. alarm.js
will then clearInterval() to get rid of this call so it can proceed
normally. */
setTimeout(function() {
load("alarm.js");
},t);
}
}
}