-
Notifications
You must be signed in to change notification settings - Fork 0
/
eink_schedule.js
82 lines (69 loc) · 3.07 KB
/
eink_schedule.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
var schedule = require('node-schedule'); //Module to Schedules Functions
var dateFormat = require('dateformat'); //Pretty Date Formats
//Import eInk Module
var eink = require('./eink');
//Import Reddit Module
var reddit = require('./reddit');
const sqlite3 = require('sqlite3').verbose();
let db = new sqlite3.Database('motiframe.db', (err) => {
if (err) {
return console.error(err.message);
}
console.log('[INFO] Connected to SQlite database - Schedule Module');
});
/*
schedule.scheduleJob('* * 1 * *', function(){ // this for one hour
schedule.scheduleJob('* 5 * * *', function(){ // this for 5 minutes
* * * * * *
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ |
│ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun)
│ │ │ │ └───── month (1 - 12)
│ │ │ └────────── day of month (1 - 31)
│ │ └─────────────── hour (0 - 23)
│ └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)
*/
//Checking Images Status
function CheckNextImage() {
let sql = 'SELECT * FROM images WHERE current_image="1" ORDER BY id LIMIT 1;'; //Selecting current image
db.all(sql, [], (err, images) => {
if (err) {
throw err;
}
if (images.length == 0) {
console.log('[ACTION] New DB -> Setting new Image');
db.run('UPDATE images SET current_image=1 WHERE id=1;');
//Here setup first image
}
else {
eink.PushImage('./public/images/' + images[0].eInk_filename); //Sending next Image to eInk
console.log('Sending image to eInk: ' + './public/images/' + images[0].eInk_filename);
var nextImageId = images[0].id + 1;
console.log('[ACTION] Resetting current image: ' + images[0].filename);
db.run('UPDATE images SET current_image=0 WHERE id=?', [images[0].id]); //Resetting current image
db.run('UPDATE images SET current_image=1 WHERE id=?', nextImageId, function (err) {
if (err) {
throw err;
}
if (this.changes == 0) {
console.log('[INFO] Last image, restarting count.');
//db.run('UPDATE images SET current_image=1 WHERE id=1'); //Resetting current image
}
});
}
});
};
//reddit.UpdateImages();
//Showing images every 'x' in the eInk Display
var p = schedule.scheduleJob('*/5 * * * *', function () { // 5 minutes later (bob sponge voice)
var now = dateFormat(Date.now());
CheckNextImage();
console.log('[5] minutes: ' + now);
});
//Showing images every 'x' in the eInk Display
var d = schedule.scheduleJob('0 7 * * *', function () { // Update Images every day at 7:00 AM (0 7 * * *)
var now = dateFormat(Date.now());
reddit.UpdateImages();
console.log('[Daily Update]: ' + now);
});