[sleeplogalarm] Finish transfer to lib

master
storm64 2022-11-12 00:16:39 +01:00
parent f253d344fb
commit e92bd01fc8
3 changed files with 28 additions and 50 deletions

View File

@ -19,11 +19,6 @@ function getNextAlarm(allAlarms, from, to, withId) {
}).sort((a, b) => a.tTo - b.tTo)[0] || {}; }).sort((a, b) => a.tTo - b.tTo)[0] || {};
} }
// calculate a time from its date
function dateToTime(date) {
return ((date.getHours() * 60 + date.getMinutes()) * 60 + date.getSeconds()) * 1000;
}
exports = { exports = {
// function to read settings with defaults // function to read settings with defaults
getSettings: function() { getSettings: function() {
@ -68,7 +63,7 @@ exports = {
// get settings from widget, now and calculate time of now // get settings from widget, now and calculate time of now
var settings = WIDGETS.sleeplogalarm; var settings = WIDGETS.sleeplogalarm;
var now = new Date(); var now = new Date();
var tNow = dateToTime(now); var tNow = ((now.getHours() * 60 + now.getMinutes()) * 60 + now.getSeconds()) * 1000;
// execute trigger function if inside the alarm range // execute trigger function if inside the alarm range
if (tNow >= settings.time - settings.earlier * 6E4 && if (tNow >= settings.time - settings.earlier * 6E4 &&
@ -78,32 +73,25 @@ exports = {
// trigger function // trigger function
trigger: function(now, tNow) { trigger: function(now, tNow) {
// define settings // read settings
var settings = this.getSettings(); var settings = this.getSettings();
// calculate then date
var then = new Date(now + settings.earlier * 6E4);
// read all alarms // read all alarms
var allAlarms = sched.getAlarms(); var allAlarms = sched.getAlarms();
// find first active alarm // find first active alarm
var alarm = firstActiveAlarm(allAlarms); var alarm = getNextAlarm(sched.getAlarms(), settings.from * 36E5, settings.to * 36E5, settings.disableOnAlarm);
// return if no alarm is found // return if no alarm is found
if (!alarm) return; if (!alarm) return;
// disable early triggered alarm if set and now and then on the same day // get date of the alarm
if (settings.disableOnAlarm && now.getDate() === then.getDate()) { var aDate = new Date(now + alarm.tTo).getDate();
// add indexes to find alarm to temporary disable
allAlarms = allAlarms.map((a, idx) => { // disable earlier triggered alarm if set and on the same day
a.idx = idx; if (settings.disableOnAlarm && now.getDate() === aDate) {
return a; // set alarms last to today
}); allAlarms[alarm.idx].last = aDate;
// get index of first active alarm
var idx = firstActiveAlarm(allAlarms).idx;
// set this alarms last to then
allAlarms[idx].last = then.getDate();
// remove added indexes // remove added indexes
allAlarms = allAlarms.map(a => { allAlarms = allAlarms.map(a => {
delete a.idx; delete a.idx;
@ -116,7 +104,7 @@ exports = {
id: "sleeplog", id: "sleeplog",
appid: "sleeplog", appid: "sleeplog",
on: true, on: true,
t: (((now.getHours() * 60 + now.getMinutes()) * 60 + now.getSeconds()) * 1000), t: tNow,
dow: 127, dow: 127,
msg: settings.msg + (settings.msgAsPrefix ? alarm.msg || "" : ""), msg: settings.msg + (settings.msgAsPrefix ? alarm.msg || "" : ""),
vibrate: settings.vibrate || alarm.vibrate, vibrate: settings.vibrate || alarm.vibrate,

View File

@ -1,26 +1,10 @@
(function(back) { (function(back) {
// define settings filename // read settings
var filename = "sleeplogalarm.settings.json"; var settings = require("sleeplogalarm").getSettings();
// define settings
var settings = Object.assign({
enabled: true,
hide: false,
drawRange: true,
color: g.theme.dark ? 65504 : 31, // yellow or blue
from: 4, // 0400
to: 8, // 0800
earlier: 30,
disableOnAlarm: false, // !!! not available if alarm is at the next day
msg: "...\n",
msgAsPrefix: true,
vibrate: "..",
as: true
}, require("Storage").readJSON(filename, true) || {});
// write change to storage // write change to storage
function writeSetting() { function writeSetting() {
require("Storage").writeJSON(filename, settings); require("Storage").writeJSON("sleeplogalarm.settings.json", settings);
} }
// show widget menu // show widget menu

View File

@ -1,5 +1,7 @@
// check if enabled in settings // check if enabled in settings
if ((require("Storage").readJSON("sleeplogalarm.settings.json", true) || {enabled: true}).enabled) { if ((require("Storage").readJSON("sleeplogalarm.settings.json", true) || {enabled: true}).enabled) {
// read settings
var settings = require("sleeplogalarm").getSettings();
// insert neccessary settings into widget // insert neccessary settings into widget
WIDGETS.sleeplogalarm = { WIDGETS.sleeplogalarm = {
@ -11,16 +13,20 @@ if ((require("Storage").readJSON("sleeplogalarm.settings.json", true) || {enable
earlier: settings.earlier, earlier: settings.earlier,
draw: function () { draw: function () {
// draw zzz // draw zzz
g.reset().setColor(this.color).drawImage(atob("BwoBD8SSSP4EEEDg"), this.x + 1, this.y); g.reset().setColor(settings.color).drawImage(atob("BwoBD8SSSP4EEEDg"), this.x + 1, this.y);
// draw time of alarm if enabled // call function to draw the time of alarm if a alarm is found
if (this.drawTime && this.time) { if (this.time) this.drawTime(this.time);
},
drawTime: () => {},
reload: require("sleeplogalarm").widReload()
};
// add function to draw the time of alarm if enabled
if (this.drawTime) WIDGETS.sleeplogalarm.drawTime = function(time) {
// directly include Font4x5Numeric // directly include Font4x5Numeric
g.setFontCustom(atob("CAZMA/H4PgvXoK1+DhPg7W4P1uCEPg/X4O1+AA=="), 46, atob("AgQEAgQEBAQEBAQE"), 5).setFontAlign(1, 1); g.setFontCustom(atob("CAZMA/H4PgvXoK1+DhPg7W4P1uCEPg/X4O1+AA=="), 46, atob("AgQEAgQEBAQEBAQE"), 5).setFontAlign(1, 1);
g.drawString(0|(this.time / 36E5), this.x + this.width + 1, this.y + 12); g.drawString(0|(time / 36E5), this.x + this.width + 1, this.y + 12);
g.drawString(0|((this.time / 36E5)%1 * 60), this.x + this.width + 1, this.y + 23); g.drawString(0|((time / 36E5)%1 * 60), this.x + this.width + 1, this.y + 23);
}
},
reload: require("sleeplogalarm").widReload()
}; };
// load widget // load widget