-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_helper.js
110 lines (94 loc) · 4.38 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
109
110
//
// Module : MMM-GamepadEvents
//
var exec = require('child_process').exec;
module.exports = NodeHelper.create({
start: function () {
this.config = null;
},
initAfterLoading: function (config) {
this.config = config;
},
socketNotificationReceived: function (notification, payload) {
switch (notification) {
case 'INIT':
this.initAfterLoading(payload);
break;
case 'EXEC_COMBINATION':
this.execCombination(payload.controller, payload.controllerHistory);
break;
}
},
execCombination: function(controller, controllerHistory) {
var combinationsExecuted = [];
var canSendEvent;
var alreadyPressed;
var message;
this.config.combinations.forEach((combination) => {
if ('gamepad' in combination) {
canSendEvent = true;
alreadyPressed = true;
if ('buttons' in combination.gamepad) {
combination.gamepad.buttons.forEach((idButton) => {
if (!controller['buttons'][idButton]['pressed'] ||
combinationsExecuted.some((combinationExecuted) => {
return 'buttons' in combinationExecuted.gamepad.buttons &&
combinationExecuted.gamepad.buttons.includes(idButton)
;
})
) {
canSendEvent = false;
} else if (!controllerHistory || !controllerHistory['buttons'][idButton]['pressed']) {
alreadyPressed = false;
}
});
}
if (canSendEvent) {
if ('axis' in combination.gamepad && 'axisValue' in combination.gamepad) {
if (combination.gamepad.axis in controller['axes'] &&
combination.gamepad.axisValue === controller['axes'][combination.gamepad.axis].axisValue
) {
if (!controller['axes'][combination.gamepad.axis]['pressed'] ||
combinationsExecuted.some((combinationExecuted) => {
return 'axis' in combinationExecuted.gamepad &&
'axisValue' in combinationExecuted.gamepad &&
combinationExecuted.gamepad.axis === combination.gamepad.axis &&
combinationExecuted.gamepad.axisValue === combination.gamepad.axisValue
;
})
) {
canSendEvent = false;
} else if (!controllerHistory || !controllerHistory['axes'][combination.gamepad.axis]['pressed']) {
alreadyPressed = false;
}
} else {
canSendEvent = false;
}
}
}
if (canSendEvent) {
if (!alreadyPressed ||
('repeatEvent' in combination && combination.repeatEvent)
) {
message = '';
if ('event' in combination) {
this.sendSocketNotification('CALL_EVENT', combination);
message = 'Event : ' + combination.event;
if ('param' in combination) {
message += '(' + combination.param + ')';
}
} else if ('command' in combination) {
exec(combination.command, function (error, stdout, stderr) {
if (stdout) console.log(stdout);
if (stderr) console.log(stderr);
});
message = 'Command : ' + combination.command;
}
this.sendSocketNotification('SHOW_NOTIF', message);
}
combinationsExecuted.push(combination);
}
}
});
}
});