mirror of https://github.com/espruino/BangleApps
qmsched: Simplify scheduling logic
Instead of keeping track when a schedule last fired, just check if the time is in the future.pull/784/head
parent
25ef3cfb0f
commit
1d649a58fd
|
@ -3245,7 +3245,7 @@
|
||||||
"name": "Quiet Mode Schedule",
|
"name": "Quiet Mode Schedule",
|
||||||
"shortName":"Quiet Mode",
|
"shortName":"Quiet Mode",
|
||||||
"icon": "app.png",
|
"icon": "app.png",
|
||||||
"version":"0.01",
|
"version":"0.02",
|
||||||
"description": "Automatically turn Quiet Mode on or off at set times",
|
"description": "Automatically turn Quiet Mode on or off at set times",
|
||||||
"readme": "README.md",
|
"readme": "README.md",
|
||||||
"tags": "tool",
|
"tags": "tool",
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
0.01: First version
|
0.01: First version
|
||||||
|
0.02: Simplify scheduling logic
|
||||||
|
|
|
@ -5,18 +5,25 @@ const modeNames = ["Off", "Alarms", "Silent"];
|
||||||
let scheds = require("Storage").readJSON("qmsched.json", 1);
|
let scheds = require("Storage").readJSON("qmsched.json", 1);
|
||||||
/*scheds = [
|
/*scheds = [
|
||||||
{ hr : 6.5, // hours + minutes/60
|
{ hr : 6.5, // hours + minutes/60
|
||||||
last : 0, // last day of the month we fired on - so we don't switch twice in one day!
|
|
||||||
mode : 1, // quiet mode (0/1/2)
|
mode : 1, // quiet mode (0/1/2)
|
||||||
}
|
}
|
||||||
];*/
|
];*/
|
||||||
if (!scheds) {
|
if (!scheds) {
|
||||||
// set default schedule on first load of app
|
// set default schedule on first load of app
|
||||||
scheds = [
|
scheds = [
|
||||||
{"hr": 8, "mode": 0, "last": 25},
|
{"hr": 8, "mode": 0},
|
||||||
{"hr": 22, "mode": 1, "last": 25},
|
{"hr": 22, "mode": 1},
|
||||||
];
|
];
|
||||||
require("Storage").writeJSON("qmsched.json", scheds);
|
require("Storage").writeJSON("qmsched.json", scheds);
|
||||||
}
|
}
|
||||||
|
if (scheds.length && scheds.some(s => "last" in s)) {
|
||||||
|
// cleanup: remove "last" values (used by old versions)
|
||||||
|
scheds = scheds.map(s => {
|
||||||
|
delete s.last;
|
||||||
|
return s;
|
||||||
|
});
|
||||||
|
require("Storage").writeJSON("qmsched.json", scheds);
|
||||||
|
}
|
||||||
|
|
||||||
function formatTime(t) {
|
function formatTime(t) {
|
||||||
const hrs = 0|t;
|
const hrs = 0|t;
|
||||||
|
@ -24,11 +31,6 @@ function formatTime(t) {
|
||||||
return (" "+hrs).substr(-2)+":"+("0"+mins).substr(-2);
|
return (" "+hrs).substr(-2)+":"+("0"+mins).substr(-2);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCurrentHr() {
|
|
||||||
const time = new Date();
|
|
||||||
return time.getHours()+(time.getMinutes()/60)+(time.getSeconds()/3600);
|
|
||||||
}
|
|
||||||
|
|
||||||
function showMainMenu() {
|
function showMainMenu() {
|
||||||
const menu = {
|
const menu = {
|
||||||
"": {"title": "Quiet Mode"},
|
"": {"title": "Quiet Mode"},
|
||||||
|
@ -36,8 +38,8 @@ function showMainMenu() {
|
||||||
value: (require("Storage").readJSON("setting.json", 1) || {}).quiet|0,
|
value: (require("Storage").readJSON("setting.json", 1) || {}).quiet|0,
|
||||||
format: v => modeNames[v],
|
format: v => modeNames[v],
|
||||||
onchange: function(v) {
|
onchange: function(v) {
|
||||||
if (v<0) v = 2;
|
if (v<0) {v = 2;}
|
||||||
if (v>2) v = 0;
|
if (v>2) {v = 0;}
|
||||||
require("qmsched").setMode(v);
|
require("qmsched").setMode(v);
|
||||||
this.value = v;
|
this.value = v;
|
||||||
},
|
},
|
||||||
|
@ -71,8 +73,8 @@ function showEditMenu(index) {
|
||||||
"Hours": {
|
"Hours": {
|
||||||
value: hrs,
|
value: hrs,
|
||||||
onchange: function(v) {
|
onchange: function(v) {
|
||||||
if (v<0) v = 23;
|
if (v<0) {v = 23;}
|
||||||
if (v>23) v = 0;
|
if (v>23) {v = 0;}
|
||||||
hrs = v;
|
hrs = v;
|
||||||
this.value = v;
|
this.value = v;
|
||||||
}, // no arrow fn -> preserve 'this'
|
}, // no arrow fn -> preserve 'this'
|
||||||
|
@ -80,8 +82,8 @@ function showEditMenu(index) {
|
||||||
"Minutes": {
|
"Minutes": {
|
||||||
value: mins,
|
value: mins,
|
||||||
onchange: function(v) {
|
onchange: function(v) {
|
||||||
if (v<0) v = 59;
|
if (v<0) {v = 59;}
|
||||||
if (v>59) v = 0;
|
if (v>59) {v = 0;}
|
||||||
mins = v;
|
mins = v;
|
||||||
this.value = v;
|
this.value = v;
|
||||||
}, // no arrow fn -> preserve 'this'
|
}, // no arrow fn -> preserve 'this'
|
||||||
|
@ -90,24 +92,17 @@ function showEditMenu(index) {
|
||||||
value: mode,
|
value: mode,
|
||||||
format: v => modeNames[v],
|
format: v => modeNames[v],
|
||||||
onchange: function(v) {
|
onchange: function(v) {
|
||||||
if (v<0) v = 2;
|
if (v<0) {v = 2;}
|
||||||
if (v>2) v = 0;
|
if (v>2) {v = 0;}
|
||||||
mode = v;
|
mode = v;
|
||||||
this.value = v;
|
this.value = v;
|
||||||
}, // no arrow fn -> preserve 'this'
|
}, // no arrow fn -> preserve 'this'
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
function getSched() {
|
function getSched() {
|
||||||
const hr = hrs+(mins/60);
|
|
||||||
let day = 0;
|
|
||||||
// If schedule is for tomorrow not today (eg, in the past), set day
|
|
||||||
if (hr<getCurrentHr()) {
|
|
||||||
day = (new Date()).getDate();
|
|
||||||
}
|
|
||||||
return {
|
return {
|
||||||
hr: hr,
|
hr: hrs+(mins/60),
|
||||||
mode: mode,
|
mode: mode,
|
||||||
last: day,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
menu["> Save"] = function() {
|
menu["> Save"] = function() {
|
||||||
|
|
|
@ -1,24 +1,19 @@
|
||||||
// apply Quiet Mode schedules
|
// apply Quiet Mode schedules
|
||||||
(function qm() {
|
(function qm() {
|
||||||
let scheds = require("Storage").readJSON("qmsched.json", 1) || [];
|
let scheds = require("Storage").readJSON("qmsched.json", 1) || [];
|
||||||
if (!scheds.length) return;
|
if (!scheds.length) { return;}
|
||||||
let next,idx;
|
|
||||||
scheds.forEach(function(s, i) {
|
|
||||||
if (!next || (s.hr+s.last*24)<(next.hr+next.last*24)) {
|
|
||||||
next = s;
|
|
||||||
idx = i;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
const now = new Date(),
|
const now = new Date(),
|
||||||
hr = now.getHours()+(now.getMinutes()/60)+(now.getSeconds()/3600);
|
hr = now.getHours()+(now.getMinutes()/60)+(now.getSeconds()/3600); // current (decimal) hour
|
||||||
let t = 3600000*(next.hr-hr);
|
scheds.sort((a, b) => a.hr-b.hr);
|
||||||
if (next.last===now.getDate()) t += 86400000;
|
const tday = scheds.filter(s => s.hr>hr), // scheduled for today
|
||||||
|
tmor = scheds.filter(s => s.hr<=hr); // scheduled for tomorrow
|
||||||
|
const next = tday.length ? tday[0] : tmor[0],
|
||||||
|
mode = next.mode;
|
||||||
|
let t = 3600000*(next.hr-hr); // timeout in milliseconds
|
||||||
|
if (t<0) {t += 86400000;} // scheduled for tomorrow: add a day
|
||||||
/* update quiet mode at the correct time. */
|
/* update quiet mode at the correct time. */
|
||||||
setTimeout(function() {
|
setTimeout(() => {
|
||||||
let scheds = require("Storage").readJSON("qmsched.json", 1) || [];
|
require("qmsched").setMode(mode);
|
||||||
require("qmsched").setMode(scheds[idx].mode);
|
|
||||||
scheds[idx].last = (new Date()).getDate();
|
|
||||||
require("Storage").writeJSON("qmsched.json", scheds);
|
|
||||||
qm(); // schedule next update
|
qm(); // schedule next update
|
||||||
}, t);
|
}, t);
|
||||||
})();
|
})();
|
||||||
|
|
Loading…
Reference in New Issue