[sleeplogalarm] Finish transfer to lib

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

View File

@ -1,26 +1,10 @@
(function(back) {
// define settings filename
var filename = "sleeplogalarm.settings.json";
// 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) || {});
// read settings
var settings = require("sleeplogalarm").getSettings();
// write change to storage
function writeSetting() {
require("Storage").writeJSON(filename, settings);
require("Storage").writeJSON("sleeplogalarm.settings.json", settings);
}
// show widget menu

View File

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