-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
106 lines (90 loc) · 2.98 KB
/
node_helper.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
/* MagicMirror²
* Module: TouchButton
*
* By Tom Hirschberger
* MIT Licensed.
*/
const NodeHelper = require('node_helper')
const spawnSync = require('child_process').spawnSync
const fs = require('fs')
const path = require('path')
const scriptsDir = path.join(__dirname, '/scripts')
module.exports = NodeHelper.create({
start: function () {
this.started = false
this.configs = {}
},
runButtonAction: function(moduleId, buttonConfig, buttonId) {
const self = this
console.log(self.name+": Do action(s) of button: "+buttonConfig.name)
let output = null
let errOut = null
let returnCode = null
if(typeof buttonConfig.command !== "undefined"){
let curCommand = buttonConfig.command
// if(curCommand.startsWith("./")){
// curCommand = scriptsDir+"/"+curCommand
// }
let args = []
if(typeof buttonConfig.args !== "undefined"){
if(Array.isArray(buttonConfig.args)){
args = buttonConfig.args
} else {
args = buttonConfig.args.split(" ")
}
}
let curEncoding = "utf8"
if(typeof buttonConfig.encoding !== "undefined"){
curEncoding = buttonConfig.encoding
}
let options = {
shell: true,
encoding: curEncoding,
cwd: scriptsDir,
}
if(typeof buttonConfig["timeout"] !== "undefined"){
options["timeout"] = buttonConfig["timeout"]
}
try {
// console.log("Running "+curCommand + " with args: "+args.toString())
let spawnOutput = spawnSync(curCommand, args, options)
returnCode = spawnOutput.status
output = spawnOutput.stdout
errOut = spawnOutput.stderr
// console.log("ReturnCode: "+returnCode)
// console.log("Output: "+output)
// console.log("ErrOut: "+errOut)
} catch (error) {
console.log(error)
}
}
if(typeof buttonConfig.notification !== "undefined"){
if (typeof buttonConfig.payload !== "undefined"){
this.sendSocketNotification("SEND_NOTIFICATION", {"moduleId": moduleId, "notification":buttonConfig.notification, "payload": buttonConfig.payload})
} else {
this.sendSocketNotification("SEND_NOTIFICATION", {"moduleId": moduleId, "notification":buttonConfig.notification})
}
}
if (returnCode != null){
this.sendSocketNotification("RESULT", {
moduleId: moduleId,
id: buttonId,
code: returnCode,
err: errOut,
out: output
})
}
},
// let output = execSync(curScript+" "+curArgs)
socketNotificationReceived: function (notification, payload) {
const self = this
if (notification === 'CONFIG') {
self.configs[payload[0]] = payload[1]
self.started = true
} else if (notification === 'BUTTON_PRESSED' ){
self.runButtonAction(payload.moduleId, self.configs[payload.moduleId].buttons[payload.id], payload.id)
} else {
console.log(this.name + ': Received Notification: ' + notification)
}
}
})