-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
48 lines (40 loc) · 1.57 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
'use strict';
const NodeHelper = require('node_helper');
const PythonShell = require('python-shell');
var pythonStarted = false
module.exports = NodeHelper.create({
python_start: function () {
const self = this;
const pyshell = new PythonShell('modules/' + this.name + '/server.py', { mode: 'json'});
pyshell.on('message', function (message) {
if (message.hasOwnProperty('status')){
console.log("[" + self.name + "] " + message.status);
}
if (message.hasOwnProperty('login')){
console.log(message);
var user = message.login.user > 0 ? message.login.user : 0;
console.log("[" + self.name + "] " + "User " + self.config.users[user] + " with confidence " + message.login.confidence + " logged in.");
self.sendSocketNotification('user', {action: "login", user: user, confidence: message.login.confidence});
}
if (message.hasOwnProperty('logout')){
var user = message.logout.user > 0 ? message.logout.user : 0;
console.log("[" + self.name + "] " + "User " + self.config.users[user] + " logged out.");
self.sendSocketNotification('user', {action: "logout", user: user});
}
});
pyshell.end(function (err) {
if (err) throw err;
console.log("[" + self.name + "] " + 'finished running...');
});
},
// Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload) {
if(notification === 'CONFIG') {
this.config = payload
if(!pythonStarted) {
pythonStarted = true;
this.python_start();
};
};
}
});