From b0a43413c8d828acc5376d8f21795aae4db224fb Mon Sep 17 00:00:00 2001 From: Alessandro Cocco Date: Wed, 20 Apr 2022 14:12:46 +0200 Subject: [PATCH] [Scheduler] Export new functions - newDefaultAlarm - newDefaultTimer - get/setSettings --- apps/sched/README.md | 25 +++++++++++++------- apps/sched/lib.js | 55 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 8 deletions(-) diff --git a/apps/sched/README.md b/apps/sched/README.md index ed08139eb..319778067 100644 --- a/apps/sched/README.md +++ b/apps/sched/README.md @@ -53,21 +53,27 @@ use too much RAM. It can be used as follows: ``` -// add/update an existing alarm +// Get a new alarm with default values +let alarm = require("sched").newDefaultAlarm(); + +// Get a new timer with default values +let timer = require("sched").newDefaultTimer(); + +// Add/update an existing alarm require("sched").setAlarm("mytimer", { msg : "Wake up", - timer : 10*60*1000, // 10 Minutes + timer : 10 * 60 * 1000 // 10 minutes }); // Ensure the widget and alarm timer updates to schedule the new alarm properly require("sched").reload(); // Get the time to the next alarm for us -var timeToNext = require("sched").getTimeToAlarm(require("sched").getAlarm("mytimer")); -// timeToNext===undefined if no alarm or alarm disabled +let timeToNext = require("sched").getTimeToAlarm(require("sched").getAlarm("mytimer")); +// timeToNext === undefined if no alarm or alarm disabled -// delete an alarm +// Delete an alarm require("sched").setAlarm("mytimer", undefined); -// reload after deleting... +// Reload after deleting require("sched").reload(); // Or add an alarm that runs your own code - in this case @@ -76,12 +82,15 @@ require("sched").reload(); require("sched").setAlarm("customrunner", { appid : "myapp", js : "load('setting.app.js')", - timer : 1*60*1000, // 1 Minute + timer : 1 * 60 * 1000 // 1 minute }); // If you have been specifying `appid` you can also find any alarms that // your app has created with the following: -require("sched").getAlarms().filter(a=>a.appid=="myapp"); +require("sched").getAlarms().filter(a => a.appid == "myapp"); + +// Get the scheduler settings +let settings = require("sched").getSettings(); ``` If your app requires alarms, you can specify that the alarms app needs to diff --git a/apps/sched/lib.js b/apps/sched/lib.js index 48094c86f..bfad1ac2d 100644 --- a/apps/sched/lib.js +++ b/apps/sched/lib.js @@ -52,3 +52,58 @@ exports.reload = function() { Bangle.drawWidgets(); } }; +// Factory that creates a new alarm with default values +exports.newDefaultAlarm = function () { + const settings = exports.getSettings(); + + let alarm = { + t: 12 * 3600000, // Default to 12:00 + on: true, + rp: false, // repeat not the default + as: settings.defaultAutoSnooze || false, + dow: 0b1111111, + last: 0, + vibrate: settings.defaultAlarmPattern, + }; + + delete settings; + + return alarm; +} +// Factory that creates a new timer with default values +exports.newDefaultTimer = function () { + const settings = exports.getSettings(); + + let timer = { + timer: 5 * 60 * 1000, // 5 minutes + on: true, + rp: false, + as: false, + dow: 0b1111111, + last: 0, + vibrate: settings.defaultTimerPattern + } + + delete settings; + + return timer; +}; +// Return the scheduler settings +exports.getSettings = function () { + return Object.assign( + { + unlockAtBuzz: false, + defaultSnoozeMillis: 600000, // 10 minutes + defaultAutoSnooze: false, + buzzCount: 10, + buzzIntervalMillis: 3000, // 3 seconds + defaultAlarmPattern: "..", + defaultTimerPattern: ".." + }, + require("Storage").readJSON("sched.settings.json", true) || {} + ); +} +// Write the updated settings back to storage +exports.setSettings = function(settings) { + require("Storage").writeJSON("sched.settings.json", settings); +}; \ No newline at end of file