-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_helper.js
156 lines (144 loc) · 5.71 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* Copyright (c) 2016 Justin Young
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
const NodeHelper = require('node_helper');
const bootstrap = require('./magic-mirror-voice-bootstrap');
const alexaJavaClientFactory = require('./alexa-java-client/alexa-java-client');
const sphinxJavaClientFactory = require('./sphinx-java-client/sphinx-java-client');
var isLoaded = false;
var runtime = {
isLoading: false,
isRegistered: false,
isAlexaSpeaking: false,
isAlexaListening: false,
registrationCode: undefined,
registrationUrl: undefined
};
var alexaJavaClient = undefined;
var sphinxJavaClient = undefined;
var alexaListeningTimeout;
module.exports = NodeHelper.create({
socketNotificationReceived: function(notification, payload) {
var self = this;
if (notification === 'INIT') {
self.config = payload;
runtime.isLoading = true;
self.sendSocketNotification('UPDATE_DOM', runtime);
if (!isLoaded) {
isLoaded = true;
bootstrap(self.config).then(function(java) {
alexaJavaClient = alexaJavaClientFactory.get();
alexaJavaClient.boot(java, self.config, {
onRegistrationCode: function(registrationCode) {
self.handleRegistrationCode(registrationCode);
},
onAccessToken: function(token) {
self.handleAccessToken(token);
},
onAlexaCompleted: function() {
},
onAlexaSpeechStarted: function() {
self.handleAlexaSpeechStarted();
},
onAlexaSpeechFinished: function() {
self.handleAlexaSpeechFinished();
}
});
sphinxJavaClient = sphinxJavaClientFactory.get();
sphinxJavaClient.boot(java, self.config, {
handleCommand: function(command) {
self.handleCommand(command);
}
}).then(function() {
sphinxJavaClient.listen();
self.heartBeat();
});
runtime.isLoading = false;
self.sendSocketNotification('UPDATE_DOM', runtime);
});
}
}
},
// Recover from any failures by checking Sphinx every 30 seconds
heartBeat: function() {
setInterval(function() {
// There is a state the module can get into where the Alexa client listen() has been called but Alexa does not
// trigger speech started. This timeout will clear this state is if occurs for more than 30 seconds.
if (runtime.isAlexaListening) {
alexaListeningTimeout = setTimeout(function() {
runtime.isAlexaListening = false;
}, 30);
}
if (!runtime.isAlexaSpeaking && !runtime.isAlexaListening) {
sphinxJavaClient.isListening().then(function(isListening) {
if (!runtime.isAlexaSpeaking && !runtime.isAlexaListening) {
if (!isListening) {
sphinxJavaClient.listen();
}
}
});
}
}, 1000 * 30);
},
handleRegistrationCode: function(registrationCode) {
var self = this;
runtime.registrationCode = registrationCode;
runtime.registrationUrl = self.config.companion.serviceUrl + '/provision/' + runtime.registrationCode;
console.info('Register Alexa Java Client by navigating to ' + runtime.registrationUrl);
self.sendSocketNotification('UPDATE_DOM', runtime);
},
handleAccessToken: function(token) {
var self = this;
runtime.isRegistered = true;
console.info('Alexa Java Client registered with token: ' + token);
self.sendSocketNotification('UPDATE_DOM', runtime);
},
handleAlexaSpeechStarted: function() {
runtime.isAlexaSpeaking = true;
runtime.isAlexaListening = false;
clearImmediate(alexaListeningTimeout);
sphinxJavaClient.stop();
},
handleAlexaSpeechFinished: function() {
runtime.isAlexaSpeaking = false;
runtime.isAlexaTriggered = false;
sphinxJavaClient.listen();
},
handleCommand: function(requestedCommand) {
var self = this;
runtime.isAlexaListening = false;
clearImmediate(alexaListeningTimeout);
for (var commandKey in self.config.sphinx.commands) {
if (self.config.sphinx.commands.hasOwnProperty(commandKey)) {
if (commandKey.toLowerCase() === requestedCommand.toLowerCase()) {
var command = self.config.sphinx.commands[commandKey];
var action = command.action;
if (action === 'alexa') {
if (runtime.isRegistered) {
runtime.isAlexaListening = true;
alexaJavaClient.triggerAlexa();
}
} else if (action == 'sendNotification') {
self.sendSocketNotification('SEND_NOTIFICATION', command);
}
}
}
}
if (!runtime.isAlexaListening) {
sphinxJavaClient.listen();
}
}
});