-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreminder-service.js
More file actions
287 lines (250 loc) · 9.43 KB
/
Copy pathreminder-service.js
File metadata and controls
287 lines (250 loc) · 9.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
const cron = require('node-cron');
const notifier = require('node-notifier');
const fs = require('fs');
class ReminderService {
constructor(configFile = 'reminders-config.json') {
this.configFile = configFile;
this.config = this.loadConfig();
this.scheduledJobs = new Map();
}
loadConfig() {
try {
if (fs.existsSync(this.configFile)) {
const rawData = fs.readFileSync(this.configFile, 'utf8');
return JSON.parse(rawData);
}
} catch (error) {
console.error('Error loading reminder config:', error.message);
}
return {
medication: {
enabled: false,
reminders: []
},
mentalHealth: {
enabled: false,
journalTime: '20:00',
checkInTime: '09:00'
},
aws: {
enabled: false,
studyTime: '19:00'
}
};
}
saveConfig() {
try {
fs.writeFileSync(this.configFile, JSON.stringify(this.config, null, 2));
return true;
} catch (error) {
console.error('Error saving reminder config:', error.message);
return false;
}
}
// Medication Reminders
enableMedicationReminders(medications) {
this.config.medication.enabled = true;
this.config.medication.reminders = medications.map(med => ({
id: med.id,
name: med.name,
dosage: med.dosage,
time: med.scheduledTime,
frequency: med.frequency
}));
this.saveConfig();
this.scheduleMedicationReminders();
console.log('\n✓ Medication reminders enabled!');
console.log(` ${medications.length} medication(s) will send reminders at scheduled times`);
return true;
}
scheduleMedicationReminders() {
// Clear existing medication reminders
this.scheduledJobs.forEach((job, key) => {
if (key.startsWith('med-')) {
job.stop();
this.scheduledJobs.delete(key);
}
});
if (!this.config.medication.enabled) return;
this.config.medication.reminders.forEach(reminder => {
const [hour, minute] = reminder.time.split(':');
const cronExpression = `${minute} ${hour} * * *`; // Daily at scheduled time
const job = cron.schedule(cronExpression, () => {
this.sendNotification(
'Medication Reminder',
`Time to take ${reminder.name} (${reminder.dosage})`
);
console.log(`\n💊 Medication Reminder: ${reminder.name} (${reminder.dosage})`);
console.log(` Scheduled time: ${reminder.time}`);
});
this.scheduledJobs.set(`med-${reminder.id}`, job);
});
console.log(` Scheduled ${this.config.medication.reminders.length} medication reminder(s)`);
}
disableMedicationReminders() {
this.config.medication.enabled = false;
this.saveConfig();
// Stop all medication reminder jobs
this.scheduledJobs.forEach((job, key) => {
if (key.startsWith('med-')) {
job.stop();
this.scheduledJobs.delete(key);
}
});
console.log('\n✓ Medication reminders disabled');
return true;
}
// Mental Health Reminders
enableMentalHealthReminders(journalTime = '20:00', checkInTime = '09:00') {
this.config.mentalHealth.enabled = true;
this.config.mentalHealth.journalTime = journalTime;
this.config.mentalHealth.checkInTime = checkInTime;
this.saveConfig();
this.scheduleMentalHealthReminders();
console.log('\n✓ Mental health reminders enabled!');
console.log(` Journal prompt: ${journalTime}`);
console.log(` Daily check-in: ${checkInTime}`);
return true;
}
scheduleMentalHealthReminders() {
// Clear existing mental health reminders
['mh-journal', 'mh-checkin'].forEach(key => {
if (this.scheduledJobs.has(key)) {
this.scheduledJobs.get(key).stop();
this.scheduledJobs.delete(key);
}
});
if (!this.config.mentalHealth.enabled) return;
// Journal reminder
const [jHour, jMinute] = this.config.mentalHealth.journalTime.split(':');
const journalJob = cron.schedule(`${jMinute} ${jHour} * * *`, () => {
this.sendNotification(
'Journal Reminder',
'Take a moment to reflect on your day and write in your journal'
);
console.log('\n📝 Journal Reminder: Time to write in your journal');
});
this.scheduledJobs.set('mh-journal', journalJob);
// Check-in reminder
const [cHour, cMinute] = this.config.mentalHealth.checkInTime.split(':');
const checkinJob = cron.schedule(`${cMinute} ${cHour} * * *`, () => {
this.sendNotification(
'Daily Check-in',
'How are you feeling today? Log your mood and check in with yourself'
);
console.log('\n🧠 Daily Check-in: Time to log your mood and check in');
});
this.scheduledJobs.set('mh-checkin', checkinJob);
}
disableMentalHealthReminders() {
this.config.mentalHealth.enabled = false;
this.saveConfig();
['mh-journal', 'mh-checkin'].forEach(key => {
if (this.scheduledJobs.has(key)) {
this.scheduledJobs.get(key).stop();
this.scheduledJobs.delete(key);
}
});
console.log('\n✓ Mental health reminders disabled');
return true;
}
// AWS Study Reminders
enableAWSReminders(studyTime = '19:00') {
this.config.aws.enabled = true;
this.config.aws.studyTime = studyTime;
this.saveConfig();
this.scheduleAWSReminders();
console.log('\n✓ AWS study reminders enabled!');
console.log(` Study time: ${studyTime} (daily)`);
return true;
}
scheduleAWSReminders() {
if (this.scheduledJobs.has('aws-study')) {
this.scheduledJobs.get('aws-study').stop();
this.scheduledJobs.delete('aws-study');
}
if (!this.config.aws.enabled) return;
const [hour, minute] = this.config.aws.studyTime.split(':');
const studyJob = cron.schedule(`${minute} ${hour} * * *`, () => {
this.sendNotification(
'AWS Study Time',
'Time for your daily AWS Cloud Practitioner study session!'
);
console.log('\n☁️ AWS Study Reminder: Time for your daily study session');
});
this.scheduledJobs.set('aws-study', studyJob);
}
disableAWSReminders() {
this.config.aws.enabled = false;
this.saveConfig();
if (this.scheduledJobs.has('aws-study')) {
this.scheduledJobs.get('aws-study').stop();
this.scheduledJobs.delete('aws-study');
}
console.log('\n✓ AWS study reminders disabled');
return true;
}
// Send notification (cross-platform)
sendNotification(title, message) {
notifier.notify({
title: title,
message: message,
sound: true,
wait: false,
timeout: 10
});
}
// Show current reminder status
showStatus() {
console.log('\n📅 Reminder Status');
console.log('═'.repeat(60));
console.log('\n💊 Medication Reminders:');
if (this.config.medication.enabled) {
console.log(' Status: ✓ Enabled');
console.log(` Active reminders: ${this.config.medication.reminders.length}`);
this.config.medication.reminders.forEach(r => {
console.log(` • ${r.name} at ${r.time}`);
});
} else {
console.log(' Status: ✗ Disabled');
}
console.log('\n🧠 Mental Health Reminders:');
if (this.config.mentalHealth.enabled) {
console.log(' Status: ✓ Enabled');
console.log(` Journal prompt: ${this.config.mentalHealth.journalTime}`);
console.log(` Daily check-in: ${this.config.mentalHealth.checkInTime}`);
} else {
console.log(' Status: ✗ Disabled');
}
console.log('\n☁️ AWS Study Reminders:');
if (this.config.aws.enabled) {
console.log(' Status: ✓ Enabled');
console.log(` Study time: ${this.config.aws.studyTime}`);
} else {
console.log(' Status: ✗ Disabled');
}
console.log('\n═'.repeat(60));
}
// Start all enabled reminders
startAll() {
if (this.config.medication.enabled) {
this.scheduleMedicationReminders();
}
if (this.config.mentalHealth.enabled) {
this.scheduleMentalHealthReminders();
}
if (this.config.aws.enabled) {
this.scheduleAWSReminders();
}
console.log('\n✓ All enabled reminders started');
}
// Stop all reminders
stopAll() {
this.scheduledJobs.forEach((job, key) => {
job.stop();
});
this.scheduledJobs.clear();
console.log('\n✓ All reminders stopped');
}
}
module.exports = ReminderService;