-
Notifications
You must be signed in to change notification settings - Fork 28
/
schedules.js
55 lines (46 loc) · 1.47 KB
/
schedules.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
var schedule = require('node-schedule');
const exec = require("child_process").exec;
var schedules = class {
constructor(config, logger) {
this.turnOnHour = config.turnOnHour;
this.turnOffHour = config.turnOffHour;
this.logger = logger;
this.opts = {timeout: 15000};
var self = this;
//generate schedule for turning the monitor on
this.monitorOnSchedule = schedule.scheduleJob('* * ' + this.turnOnHour.toString() + ' * * *', function() {
self.turnMonitorOn();
});
//generate schedule for turning the monitor off
this.monitorOffSchedule = schedule.scheduleJob('* * ' + this.turnOffHour.toString() + ' * * *', function() {
self.turnMonitorOff();
});
this.logger.info('Scheduler started ...')
}
//execute command for turning the monitor on
turnMonitorOn() {
var self = this;
exec("tvservice --preferred && sudo chvt 6 && sudo chvt 7", self.opts, function(error, stdout, stderr) {
self.checkForExecError(error, stdout, stderr);
});
}
//execute command for turning the monitor off
turnMonitorOff() {
var self = this;
exec("tvservice -o", self.opts, function(error, stdout, stderr) {
self.checkForExecError(error, stdout, stderr);
});
}
//check for execution error
checkForExecError(error, stdout, stderr, res) {
console.log(stdout);
console.log(stderr);
if (error) {
console.log(error);
return;
}
}
}
if (typeof module !== "undefined") {
module.exports = schedules;
}