-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
366 lines (295 loc) · 11.4 KB
/
app.js
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
const express = require('express');
const bodyParser = require('body-parser');
const { exec } = require('child_process');
const fs = require('fs');
const path = require('path');
const multer = require('multer');
const cors = require('cors');
const cron = require('node-cron');
const player = require('play-sound')({ player: 'mpg123' });
const app = express();
const PORT = 3000;
// NEW: Added task management
const cronTasks = new Map();
app.use(cors());
app.use(bodyParser.json());
app.use(express.static('public'));
// Paths for JSON files
const schedulesFilePath = path.join(__dirname, 'schedules.json');
const soundsFilePath = path.join(__dirname, 'sounds.json');
// Read data from JSON files
const readData = (filePath) => {
if (fs.existsSync(filePath)) {
const rawData = fs.readFileSync(filePath);
return JSON.parse(rawData);
}
return [];
};
// Write data to JSON files
const writeData = (filePath, data) => {
fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
};
// Ensure uploads folder exists
const uploadDir = path.join(__dirname, 'public', 'uploads');
if (!fs.existsSync(uploadDir)) fs.mkdirSync(uploadDir, { recursive: true });
// Configure Multer for file uploads
const storage = multer.diskStorage({
destination: uploadDir,
filename: (req, file, cb) => {
const uniqueName = Date.now() + '-' + file.originalname.replace(/\s+/g, '-');
cb(null, uniqueName);
}
});
const upload = multer({ storage });
// Helper function to play sound
// function playSound(soundPath) {
// const fullPath = path.join(__dirname, 'public', soundPath);
// console.log(`Playing sound: ${fullPath}`);
// player.play(fullPath, (err) => {
// if (err) {
// console.error('Error playing sound with play-sound:', err);
// console.error('play-sound error code:', err.code); // Log the error code
// console.error('play-sound error signal:', err.signal); // Log the signal
// // Fallback to ffplay
// exec(`ffplay -nodisp -autoexit "${fullPath}"`, (error, stdout, stderr) => {
// if (error) {
// console.error('Error playing with ffplay:', error);
// if (stderr) console.error('ffplay stderr:', stderr);
// } else {
// console.log('ffplay output:', stdout);
// }
// });
// }
// });
// }
// function playSound(soundPath) {
// const fullPath = path.join(__dirname, 'public', soundPath); // Construct the full file path
// console.log(`Playing sound with ffplay: ${fullPath}`);
// // Run the ffplay command as the current user (not root)
// exec(`sudo -E -u kuvi41 ffplay -nodisp -autoexit "${fullPath}"`, (error, stdout, stderr) => {
// if (error) {
// console.error('Error playing with ffplay:', error);
// }
// if (stderr) {
// console.error('ffplay error:', stderr);
// }
// if (stdout) {
// console.log('ffplay output:', stdout);
// }
// });
// }
function playSound(soundPath) {
const fullPath = path.join(__dirname, 'public', soundPath); // Construct the full file path
console.log(`Playing sound with ffplay: ${fullPath}`);
// Run the ffplay command as the current user (not root)
exec(`sudo -E -u kuvi41 sh -c "mpg123 ${fullPath}"`, (error, stdout, stderr) => {
if (error) {
console.error('Error playing with ffplay:', error);
}
if (stderr) {
console.error('ffplay error:', stderr);
}
if (stdout) {
console.log('ffplay output:', stdout);
}
});
}
// NEW: Separate function to schedule a single bell
// function scheduleBell(schedule) {
// const [hour, minute] = schedule.time.split(':');
// const dayMap = {
// 'Sunday': 0, 'Monday': 1, 'Tuesday': 2, 'Wednesday': 3,
// 'Thursday': 4, 'Friday': 5, 'Saturday': 6
// };
// const cronExpression = `${minute} ${hour} * * ${dayMap[schedule.day]}`;
// console.log(`Scheduling bell for ${schedule.day} at ${schedule.time} with sound ${schedule.sound}`);
// try {
// // Stop existing task if it exists
// if (cronTasks.has(schedule.id)) {
// cronTasks.get(schedule.id).stop();
// cronTasks.delete(schedule.id);
// }
// // Create and store new task
// const task = cron.schedule(cronExpression, () => {
// console.log(`Triggering bell for ${schedule.day} at ${schedule.time}`);
// playSound(schedule.sound);
// });
// cronTasks.set(schedule.id, task);
// } catch (error) {
// console.error(`Error scheduling bell for ID ${schedule.id}:`, error);
// }
// }
// const cronTasks = new Map(); // Ensuring cronTasks is initialized
function scheduleBell(schedule) {
const [hour, minute] = schedule.time.split(':');
const dayMap = {
'Sunday': 0, 'Monday': 1, 'Tuesday': 2, 'Wednesday': 3,
'Thursday': 4, 'Friday': 5, 'Saturday': 6
};
let cronExpression;
// Check if the schedule is for weekdays (Monday to Thursday)
if (schedule.day === 'Weekdays') {
// Cron expression for Monday to Thursday
cronExpression = `${minute} ${hour} * * 1-4`; // 1-4 represents Monday to Thursday
} else {
// Regular single day cron expression
cronExpression = `${minute} ${hour} * * ${dayMap[schedule.day]}`;
}
console.log(`Scheduling bell for ${schedule.day} at ${schedule.time} with sound ${schedule.sound}`);
try {
// Stop existing task if it exists
if (cronTasks.has(schedule.id)) {
cronTasks.get(schedule.id).stop();
cronTasks.delete(schedule.id);
}
// Create and store new task
const task = cron.schedule(cronExpression, () => {
console.log(`Triggering bell for ${schedule.day} at ${schedule.time}`);
playSound(schedule.sound);
});
cronTasks.set(schedule.id, task);
} catch (error) {
console.error(`Error scheduling bell for ID ${schedule.id}:`, error);
}
}
// MODIFIED: Updated scheduleAllBells function
const scheduleAllBells = () => {
// Stop all existing tasks
for (const [id, task] of cronTasks.entries()) {
task.stop();
cronTasks.delete(id);
}
// Schedule all bells from the loaded data
const schedules = readData(schedulesFilePath);
schedules.forEach(schedule => {
scheduleBell(schedule);
});
};
// API to get all available sounds
app.get('/sounds', (req, res) => {
const sounds = readData(soundsFilePath);
res.json(sounds);
});
// API to upload MP3
app.post('/upload-sound', upload.single('sound'), (req, res) => {
if (!req.file) return res.status(400).send('No file uploaded.');
const soundPath = `/uploads/${req.file.filename}`;
const sounds = readData(soundsFilePath);
const newSound = { id: Date.now(), filename: req.file.filename, originalname: req.file.originalname, filepath: soundPath };
sounds.push(newSound);
writeData(soundsFilePath, sounds);
res.status(201).json(newSound);
});
// MODIFIED: Updated schedule creation
app.post('/schedule', (req, res) => {
const { day, time, soundPath } = req.body;
const schedules = readData(schedulesFilePath);
const newSchedule = { id: Date.now(), day, time, sound: soundPath };
schedules.push(newSchedule);
writeData(schedulesFilePath, schedules);
// Schedule the new bell immediately
scheduleBell(newSchedule);
res.status(201).json(newSchedule);
});
// API to get all schedules
app.get('/schedules', (req, res) => {
const schedules = readData(schedulesFilePath);
res.json(schedules);
});
// MODIFIED: Updated schedule deletion
app.delete('/schedule/:id', (req, res) => {
const { id } = req.params;
const schedules = readData(schedulesFilePath);
// Remove the schedule and stop the cron task
const index = schedules.findIndex(schedule => schedule.id === parseInt(id));
if (index !== -1) {
schedules.splice(index, 1);
writeData(schedulesFilePath, schedules);
// Stop the cron task
if (cronTasks.has(parseInt(id))) {
cronTasks.get(parseInt(id)).stop();
cronTasks.delete(parseInt(id));
}
res.status(200).send("Deleted successfully.");
} else {
res.status(404).send("Schedule not found.");
}
});
// MODIFIED: Updated schedule update
app.put('/schedule/:id', (req, res) => {
const { id } = req.params;
const { day, time, soundPath } = req.body;
const schedules = readData(schedulesFilePath);
const schedule = schedules.find(s => s.id === parseInt(id));
if (schedule) {
schedule.day = day;
schedule.time = time;
schedule.sound = soundPath;
writeData(schedulesFilePath, schedules);
// Reschedule the updated bell
scheduleBell(schedule);
res.status(200).send("Updated successfully.");
} else {
res.status(404).send("Schedule not found.");
}
});
let customTime = new Date(); // Initial system time is set to the current date and time.
app.get('/current-time', (req, res) => {
// Get the custom system time or the actual system time if it's not set
res.json({ currentTime: customTime });
});
app.post('/set-system-time', (req, res) => {
const { newTime } = req.body;
if (!newTime) {
return res.status(400).send("New time must be provided.");
}
// Validate time format (ISO string or other formats)
const parsedTime = new Date(newTime);
if (isNaN(parsedTime)) {
return res.status(400).send("Invalid time format.");
}
// Command for Linux/macOS
const command = `sudo date --set="${parsedTime.toISOString()}"`;
exec(command, (error, stdout, stderr) => {
if (error) {
return res.status(500).send(`Error setting system time: ${stderr || error.message}`);
}
res.status(200).send(`System time updated to: ${parsedTime}`);
});
});
// Test endpoint to trigger sound immediately
app.post('/test-sound', (req, res) => {
const { soundPath } = req.body;
playSound(soundPath);
res.status(200).send("Playing sound...");
});
// API to delete a sound file
app.delete('/delete-sound/:id', (req, res) => {
const { id } = req.params;
// Read the current sounds data
const sounds = readData(soundsFilePath);
// Find the sound file by its ID
const soundIndex = sounds.findIndex(sound => sound.id === parseInt(id));
if (soundIndex !== -1) {
// Get the sound file details
const soundToDelete = sounds[soundIndex];
// Delete the file from the server
const filePath = path.join(__dirname, 'public', soundToDelete.filepath);
fs.unlink(filePath, (err) => {
if (err) {
console.error('Error deleting file:', err);
return res.status(500).send('Error deleting the file.');
}
// Remove the sound from the JSON file
sounds.splice(soundIndex, 1);
writeData(soundsFilePath, sounds);
res.status(200).send('Sound file deleted successfully.');
});
} else {
res.status(404).send('Sound file not found.');
}
});
// Schedule all existing bells on server start
scheduleAllBells();
// Start server
app.listen(PORT, () => console.log(`Server running on http://localhost:${PORT}`));