-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathnode_helper.js
More file actions
81 lines (74 loc) · 2.21 KB
/
node_helper.js
File metadata and controls
81 lines (74 loc) · 2.21 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
const NodeHelper = require("node_helper");
const exec = require("util").promisify(require("child_process").exec);
const Log = require("../../js/logger");
const path = require("path");
module.exports = NodeHelper.create({
/**
* @param platform
*/
initMonitor (platform) {
this.platform = platform;
this.activateMonitor()
.then(() => Log.info("monitor has been initially activated."))
.catch((result) => Log.error(`error activating monitor initially: ${result.stderr}.`));
},
/**
* Get the command script path based on platform option
*/
getCommandScript () {
const platform = this.platform || "x11";
return path.join(__dirname, `monitor-commands-${platform}.sh`);
},
/**
*
*/
async activateMonitor () {
const scriptPath = this.getCommandScript();
const isMonitorOn = await this.isMonitorOn();
if (!isMonitorOn) {
await exec(`bash ${scriptPath} on`);
}
},
/**
*
*/
async deactivateMonitor () {
const scriptPath = this.getCommandScript();
const isMonitorOn = await this.isMonitorOn();
if (isMonitorOn) {
await exec(`bash ${scriptPath} off`);
}
},
/**
* @returns {Promise<boolean>}
*/
async isMonitorOn () {
const scriptPath = this.getCommandScript();
const result = await exec(`bash ${scriptPath} status`);
Log.info(`monitor is currently ${ result.stdout.trim()}.`);
return result.stdout.trim() === "ON";
},
/**
*
* @param notification
* @param payload
*/
socketNotificationReceived (notification, payload) {
if (notification === "INIT_MONITOR" && payload) {
Log.info("initialising monitor.");
this.initMonitor(payload);
}
if (notification === "ACTIVATE_MONITOR") {
Log.info("activating monitor.");
this.activateMonitor()
.then(() => Log.info("monitor has been activated."))
.catch((result) => Log.error(`error activating monitor: ${result.stderr}`));
}
if (notification === "DEACTIVATE_MONITOR") {
Log.info("deactivating monitor");
this.deactivateMonitor()
.then(() => Log.info("monitor has been deactivated."))
.catch((result) => Log.error(`error deactivating monitor: ${result.stderr}`));
}
}
});