forked from MMRIZE/MMM-GroveGestures
/
node_helper.js
108 lines (99 loc) · 2.86 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
107
108
const path = require("path")
const exec = require("child_process").exec
const {PythonShell} = require("python-shell")
var NodeHelper = require('node_helper')
module.exports = NodeHelper.create({
start: function() {
this.config = {}
this.gestureSequence = ""
this.commandTimer = null
this.shell = null
},
stop: function() {
this.log("[GESTURE] Finishing...")
if (this.shell) {
this.shell.end()
}
},
socketNotificationReceived: function (noti, payload) {
switch (noti) {
case "INIT":
this.job(payload)
break
case "SHELLEXEC":
exec(payload, (e,so,se)=>{
this.log("[GESTURE] Shell command: " + payload)
if (e) console.log(e)
})
break
}
},
log : function(obj) {
if (this.config.verbose) {
console.log(obj)
}
},
job: function(config) {
this.config = config
var map = this.config.gestureMapFromTo
var py = path.resolve(__dirname, "py", "gesture_print.py")
var option = {
mode: "text",
pythonPath:this.config.pythonPath,
pythonOptions: ['-u'],
}
this.shell = new PythonShell(py, option)
this.shell.on("message", (message)=>{
this.log("[GESTURE] ORIGIN:" + message)
var gesture = null
gesture = (map.hasOwnProperty(message)) ? map[message] : null
if (gesture) {
this.gestureProcess(gesture)
}
})
this.shell.on("error", (message)=>{
this.shell.end()
if (!message.traceback.search("KeyboardInterrupt")) {
this.log(message)
} else {
this.log("Keyboard Interrupted")
}
this.log("[GESTURE] Gesture script is finished.")
})
this.shell.on("close", ()=>{
setTimeout(()=>{
this.log("[GESTURE] Python script is terminated. It will restart soon.")
this.job(config)
}, 500)
})
},
gestureProcess: function(gesture) {
clearTimeout(this.commandTimer)
this.commandTimer = null
if (gesture == this.config.cancelGesture) {
this.log("[GESTURE] Cancel: " + this.gestureSequence)
this.sendSocketNotification("CANCEL", {
last: gesture,
sequence: this.gestureSequence,
})
this.gestureSequence = ""
clearTimeout(this.commandTimer)
} else {
this.gestureSequence += (this.gestureSequence) ? ("-" + gesture) : gesture
this.log("[GESTURE] Ongoing: " + this.gestureSequence)
this.sendSocketNotification("ONGOING", {
last:gesture,
sequence: this.gestureSequence,
})
this.log("[GESTURE] Timer reset")
this.commandTimer = setTimeout(()=>{
this.log("[GESTURE] Finish: " + this.gestureSequence)
this.sendSocketNotification("FINISH", {
last:gesture,
sequence: this.gestureSequence,
})
this.gestureSequence = ""
}, this.config.recognitionTimeout)
}
},
})