-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathnode_helper.js
309 lines (271 loc) · 8.45 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//
// Module : MMM-AssistantMk2
//
"use strict"
const path = require("path")
const record = require("node-record-lpcm16")
const Speaker = require("speaker")
const GoogleAssistant = require("google-assistant")
const exec = require("child_process").exec
const fs = require("fs")
const wav = require("wav")
var NodeHelper = require("node_helper")
module.exports = NodeHelper.create({
start: function () {
console.log(this.name + " started");
this.config = {}
},
initializeAfterLoading: function (config) {
this.config = config
},
socketNotificationReceived: function (notification, payload) {
switch(notification) {
case "INIT":
this.initializeAfterLoading(payload)
this.sendSocketNotification("INITIALIZED")
break
case "START":
this.activate(payload)
this.sendSocketNotification("STARTED")
break
}
},
activate: function(payload) {
var transcriptionHook = this.config.transcriptionHook
var cfgInstance = {
auth:{
keyFilePath : path.resolve(__dirname, this.config.auth.keyFilePath),
savedTokensPath : path.resolve(__dirname, "profiles/" + payload.profileFile),
},
conversation : {
audio : this.config.audio,
lang : payload.lang,
deviceModelId : this.config.deviceModelId,
deviceId : this.config.deviceInstanceId,
deviceLocation : this.config.deviceLocation,
screen : {
isOn: this.config.useScreen
}
},
}
var assistant = new GoogleAssistant(cfgInstance.auth)
var startConversation = (conversation) => {
let openMicAgain = false
let foundHook = []
let foundAction = null
let foundVideo = null
let foundVideoList = null
let audioError = 0
if (this.config.audio.encodingOut == "MP3") {
var mp3File = path.resolve(__dirname, "temp.mp3")
var wstream = fs.createWriteStream(mp3File)
}
// setup the conversation
conversation
// send the audio buffer to the speaker
.on("audio-data", (data) => {
if (this.config.audio.encodingOut == "MP3") {
wstream.on('finish', ()=>{
//console.log('file has been written')
wstream.end()
})
try {
wstream.write(data)
//console.log("writing")
} catch (error) {
//wstream.end()
//console.log(error)
console.log("Some error happens. Try again.")
this.sendSocketNotification("ERROR", "AUDIO_ERROR")
}
} else {
try {
speaker.write(data)
} catch (error) {
if (audioError == 0) {
speaker.end() //
//console.log(error)
console.log("Some error happens. Try again.")
this.sendSocketNotification("ERROR", "AUDIO_ERROR")
}
audioError++
}
}
})
// done speaking, close the mic
.on("end-of-utterance", () => {
console.log("end-of-utterance")
record.stop()
})
// just to spit out to the console what was said (as we say it)
.on("transcription", (data) => {
console.log("Transcription:", data.transcription, " --- Done:", data.done)
this.sendSocketNotification("TRANSCRIPTION", data)
if (data.done) {
for (var k in transcriptionHook) {
if (transcriptionHook.hasOwnProperty(k)) {
var v = transcriptionHook[k];
var found = data.transcription.match(new RegExp(v, "ig"))
if (found !== null) {
foundHook.push(k)
}
}
}
}
})
// what the assistant said back. But currently, GAS doesn"t return text response with screenOut at same time (maybe.)
.on("response", text => {
console.log("Assistant Text Response:", text)
})
// if we"ve requested a volume level change, get the percentage of the new level
// But I"ll not support this feature.
.on("volume-percent", (percent) => {
console.log("Volume control... Not yet supported")
})
// the device needs to complete an action
.on("device-action", (action) => {
console.log("Device Action:", action)
if (typeof action["inputs"] !== "undefined") {
//this.sendSocketNotification("NOT_SUPPORTED")
var intent = action.inputs[0].payload.commands
console.log("execution", action.inputs[0].payload.commands[0].execution[0])
foundAction = action.inputs[0].payload.commands
}
})
// once the conversation is ended, see if we need to follow up
.on("ended", (error, continueConversation) => {
var payload = {
"foundHook": foundHook,
"foundAction": foundAction,
"foundVideo": foundVideo,
"foundVideoList": foundVideoList,
"error": null,
"continue": false
}
if (error) {
console.log("Conversation Ended Error:", error)
payload.error = error
} else if (continueConversation) {
openMicAgain = true
payload.continue = true
} else {
console.log("Conversation Completed")
}
speaker.end() //
if (this.config.audio.encodingOut == "MP3") {
wstream.end()
exec(this.config.audio.mp3Player + ' ' + mp3File, (err, stdout, stderr) => {
//console.log(err, stdout, stderr)
this.sendSocketNotification("TURN_OVER", payload)
})
} else {
this.sendSocketNotification("TURN_OVER", payload)
}
})
.on("screen-data", (screen) => {
var self = this
var file = require("fs")
var filePath = path.resolve(__dirname,"temp_screen.html")
var str = screen.data.toString("utf8")
str = str.replace("html,body{", "html,body{zoom:" + this.config.screenZoom + ";")
// TODO:I'll put some code here for web scrapping for contents reading.
//For Image Search
//https://www.google.com/search?tbm=isch
var re = new RegExp("(tbm=isch[^<]*)", "ig")
var isch = re.exec(str)
//console.log("image:", isch)
var contents = file.writeFile(filePath, str,
(error) => {
if (error) {
console.log("Error:- " + error);
}
this.sendSocketNotification("SCREEN", str)
}
)
//www.youtube.com/watch?v=8O5pkFdi93k
var re = new RegExp("youtube\.com\/watch\\?v\=([0-9a-zA-Z\-\_]+)", "ig")
var youtubeVideo = re.exec(str)
if (youtubeVideo) {
console.log("video found:", youtubeVideo[1])
foundVideo = youtubeVideo[1]
}
//(m.youtube.com - https://m.youtube.com/playlist?list=PLCdlJaCXfCUC30qzcup9L2DO9mv703eox)
var re = new RegExp("youtube\.com\/playlist\\?list\=([a-zA-Z0-9\-\_]+)", "ig")
var youtubeList = re.exec(str)
if (youtubeList) {
console.log("video list found:", youtubeList[1])
foundVideoList = youtubeList[1]
}
})
// catch any errors
.on("error", (error) => {
speaker.end() //
console.log("Conversation Error:", error)
this.sendSocketNotification("CONVERSATION_ERROR", error)
})
var mic = record.start(this.config.record)
mic.on("data", (data) => {
try {
conversation.write(data)
} catch (err) {
console.log("mic error:", err)
}
})
// setup the speaker
var speaker = new Speaker({
channels: 1,
sampleRate: cfgInstance.conversation.audio.sampleRateOut,
});
speaker
.on("open", () => {
this.sendSocketNotification("SPEAKING_START")
})
.on("close", () => {
this.sendSocketNotification("SPEAKING_END")
if (openMicAgain) {
this.sendSocketNotification("CONTINUOUS_TURN")
assistant.start(cfgInstance.conversation)
} else {
// do nothing
}
})
}
assistant
.on("ready", () => {
// start a conversation!
console.log("assistant ready")
this.sendSocketNotification("ASSISTANT_READY")
var wavReader = fs.createReadStream(path.resolve(__dirname, "resources/ding.wav")).pipe(wav.Reader())
var buffer = null;
wavReader.on('format', (format) => {
//console.log("Playing wav.", format)
wavReader.on('data', (chunk) => {
if(buffer)
buffer = Buffer.concat([buffer, chunk])
else
buffer = chunk
} )
.on( 'end', function() {
var s = new Speaker(format)
try {
s.write(buffer)
setTimeout(()=>{
s.end()
assistant.start(cfgInstance.conversation)
}, 500)
} catch (error) {
s.end()
console.log(error)
console.log("Some error happens. Try again.")
}
} )
} )
//assistant.start(cfgInstance.conversation)
})
.on("started", startConversation)
.on("error", (error) => {
console.log("Assistant Error:", error)
this.sendSocketNotification("ASSISTANT_ERROR", error)
})
},
})