Skip to content

Commit 6e4ce39

Browse files
committed
schedclock: handle firstDayOfWeek; set last day on fired alarms; display 12h format
1 parent cc09111 commit 6e4ce39

File tree

2 files changed

+62
-15
lines changed

2 files changed

+62
-15
lines changed

apps/schedclock/lib.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const APP_ID = "schedclock";
55
* Called directly by an alarm to load a specific clock face
66
* @param {string} faceSrc - Source file of the clock face to load (e.g. "myclock.js")
77
**/
8-
exports.loadFace = function(faceSrc) {
8+
const setClock = function(faceSrc) {
99
const settings = require("Storage").readJSON("setting.json", 1) || {};
1010
// Only change the clock if it's different
1111
if (faceSrc && settings.clock !== faceSrc) {
@@ -22,6 +22,23 @@ exports.loadFace = function(faceSrc) {
2222
}
2323
};
2424

25+
/**
26+
* Handle alarms and resetting them
27+
* @param {number} index Index of the alarm that went off
28+
* @param {string} clock Clockface
29+
*/
30+
exports.onAlarm = function(index, clock) {
31+
const date = new Date();
32+
const Sched = require("sched");
33+
const alarm = Sched.getAlarm(`${APP_ID}.${index}`);
34+
alarm.last = date.getDate(); // prevent second run on the same day
35+
// -- Don't think alarm.date is required, but just in case
36+
//date.setDate(alarm.last + 1);
37+
//alarm.date = date.toLocalISOString().slice(0,10);
38+
Sched.setAlarm(alarm.id, alarm);
39+
setClock(clock);
40+
};
41+
2542
/**
2643
* Function to sync all alarms in the scheduler with the settings file.
2744
* Called every time settings are changed; maybe a bit excessive, but keeps things simple.
@@ -39,21 +56,28 @@ exports.syncAlarms = function() {
3956
// If the app is disabled, we're done.
4057
if (!settings.enabled) return;
4158

59+
const time = new Date();
60+
const currentTime = (time.getHours()*3600000)+(time.getMinutes()*60000)+(time.getSeconds()*1000);
61+
4262
// Add a new alarm for each setting item
4363
settings.sched.forEach((item, index) => {
4464

4565
if (item.hour === undefined) return;
4666

67+
const schedTime = (item.hour * 3600000) + (item.minute * 60000);
68+
const today = time.getDate();
69+
const tomorrow = today + 1;
70+
4771
// Create the new alarm object and save it using a unique ID.
4872
Sched.setAlarm(`${APP_ID}.${index}`, {
73+
t: schedTime, // time in milliseconds since midnight
4974
on: true,
50-
appid: APP_ID,
51-
t: (item.hour * 3600000) + (item.minute * 60000), // time in milliseconds since midnight
75+
rp: true,
76+
last: (schedTime > currentTime) ? today : tomorrow,
5277
dow: item.dow,
5378
hidden: true,
54-
rp: {interval: "week"},
55-
js: `require('${APP_ID}.lib.js').loadFace('${item.face}')`,
79+
appid: APP_ID,
80+
js: `require('${APP_ID}.lib.js').onAlarm(${index},'${item.face}')`,
5681
});
5782
});
5883
};
59-

apps/schedclock/settings.js

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
*/
1414

1515
const SETTINGS_FILE = "schedclock.settings.json";
16-
const daysOfWeek = require("date_utils").dows(1).concat([/*LANG*/"Every Day", /*LANG*/"Weekdays", /*LANG*/"Weekends"]);
1716
// Bitmasks for special day selection for sched.json
1817
const BIN_WORKDAYS = 0b0111110; // 62 - MTWTF
1918
const BIN_WEEKEND = 0b1000001; // 65 - SuSa
@@ -23,6 +22,16 @@
2322
const IND_WORKDAYS = 8;
2423
const IND_WEEKEND = 9;
2524

25+
const daysOfWeek = (function() {
26+
const firstDayOfWeek = (require("Storage").readJSON("setting.json", true) || {}).firstDayOfWeek || 0;
27+
const daysOfWeek = require("date_utils").dows(1);
28+
if (!firstDayOfWeek) {
29+
// Move Sunday from end to start of week
30+
daysOfWeek.unshift(daysOfWeek.splice(-1, 1)[0]);
31+
}
32+
return daysOfWeek.concat([/*LANG*/"Every Day", /*LANG*/"Weekdays", /*LANG*/"Weekends"]);
33+
})();
34+
2635
/**
2736
* Function to load settings
2837
* @returns {SettingsType} settings object
@@ -99,7 +108,7 @@
99108
const faceName = (clockFaces.find(f => f.src === item.face) || {name: /*LANG*/"Unknown"}).name;
100109
const dow = binaryToDow(item.dow);
101110
const dayName = daysOfWeek[dow === undefined ? IND_EVERY_DAY : dow];
102-
const timeStr = ("0"+item.hour).slice(-2) + ":" + ("0"+item.minute).slice(-2);
111+
const timeStr = require("locale").time(new Date(1999, 1, 1, item.hour, item.minute, 0),1)
103112
menu[`${dayName} ${timeStr} - ${faceName}`] = () => editScheduleItem(index);
104113
});
105114

@@ -119,9 +128,9 @@
119128
case IND_WORKDAYS: return BIN_WORKDAYS;
120129
case IND_WEEKEND: return BIN_WEEKEND;
121130
default:
122-
return 1 << index; // Single day (0=Sun, 1=Mon, ..., 6=Sat)
131+
return 1 << (index + 1); // Single day (0=Sun, 1=Mon, ..., 6=Sat)
123132
}
124-
}
133+
};
125134

126135
/**
127136
* Get the index in daysOfWeek from a binary day-of-week bitmask
@@ -133,14 +142,17 @@
133142
case BIN_EVERY_DAY: return IND_EVERY_DAY;
134143
case BIN_WORKDAYS: return IND_WORKDAYS;
135144
case BIN_WEEKEND: return IND_WEEKEND;
136-
}
137-
// Check each single day (0=Sun, 1=Mon, ..., 6=Sat)
138-
for (let i = 0; i < 7; i++) {
139-
if (b === (1 << i)) return i;
145+
case 1: return 0;
146+
case 2: return 1;
147+
case 4: return 2;
148+
case 8: return 3;
149+
case 16: return 4;
150+
case 32: return 5;
151+
case 64: return 6;
140152
}
141153
// Bitmask was something we don't handle yet, default to everyday for now
142154
return IND_EVERY_DAY;
143-
}
155+
};
144156

145157
/**
146158
* Function to edit a schedule item (or add a new one if index is -1)
@@ -177,6 +189,17 @@
177189
value: currentItem.hour,
178190
min: 0,
179191
max: 23,
192+
format: v => {
193+
// Format as 12h time if user has that set
194+
const meridean = require("locale").meridian(new Date(1999, 1, 1, v, 0, 0),1);
195+
if (meridean) {
196+
return (v>12)
197+
? v-12 + meridean
198+
: ((v===0)?12:v) + meridean;
199+
} else {
200+
return v;
201+
}
202+
},
180203
onchange: v => { currentItem.hour = v; }
181204
},
182205
/*LANG*/"Minute": {

0 commit comments

Comments
 (0)