-
Notifications
You must be signed in to change notification settings - Fork 11
/
Hello-Lucy.js
293 lines (251 loc) · 9.36 KB
/
Hello-Lucy.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
"use strict";
var importedSentences; /** Prepare variable to be populated from import. */
function readTextFile(file, callback) {
var rawFile = new XMLHttpRequest();
rawFile.overrideMimeType("application/json");
rawFile.open("GET", file, true);
rawFile.onreadystatechange = function() {
if (rawFile.readyState === 4 && rawFile.status == "200") {
callback(rawFile.responseText);
}
};
rawFile.send(null);
}
const sfn= document.currentScript.src.substring(7,document.currentScript.src.lastIndexOf(document.currentScript.src.slice(7).includes('//')?'//':'/'))
const localPath= sfn.substring(sfn.indexOf('modules'))
const ModuleName=localPath.substring(localPath.lastIndexOf("/")+1)
//console.log("localPath="+localPath+" modulename="+ModuleName);
readTextFile(localPath+"/sentences.json", function(text){
var tempImport = JSON.parse(text);
importedSentences = tempImport;
// console.log("SENTENCE: "+importedSentences);
});
////////////////////////////////////////////////////
Module.register(ModuleName, {
icon: "fa-microphone-slash",
pulsing: true,
help: false,
timeout: null,
lucy: {
mode: "Say, 'Hello Lucy'",
sentences: []
},
modules: [],
previouslyHidden: [],
defaults: {
timeout: 15, // time listening for a command/sentence when mic pulsing
defaultOnStartup: "Hello-Lucy",
keyword: "HELLO LUCY", // keyword to activate listening for a command/sentence
standByMethod: "DPMS", // 'DPMS' = anything else than RPi or 'PI'
sounds: ["a.mp3", "b.mp3", "c.mp3" , "d.mp3"], // welcome sounds at startup randomly chosen
startHideAll: false, // if true, all modules start as hidden
microphone: "default", //
speed: 1000, // transition speed between show/no-show/show in milliseconds
pageOneModules: ["Hello-Lucy"], // default modules to show on page one/startup
pageTwoModules: [], // modules to show on page two
pageThreeModules: [], // modules to show on page two
pageFourModules: [], // modules to show on page two
pageFiveModules: [], // modules to show on page two
pageSixModules: [], // modules to show on page two
pageSevenModules: [], // modules to show on page two
pageEightModules: [], // modules to show on page two
pageNineModules: [], // modules to show on page two
pageTenModules: [], // modules to show on page two
greetingSounds: [ "a.mp3", "b.mp3", "c.mp3" , "d.mp3"], // randomized greeting sounds
confirmationSound: "ding.mp3", // when command is accepted. use your own or default
debug: false, // get debug information in console
},
poweredOff: false,
start() {
var combinedSentences = importedSentences.concat(this.lucy.sentences);
this.lucy.sentences = combinedSentences;
//Log.info(`Starting module: ${this.name}`);
this.mode = this.translate("INIT");
this.modules.push(this.lucy);
//Log.info(`${this.name} is waiting for lucy command registrations.`);
},
getStyles() {
return ["font-awesome.css", "Hello-Lucy.css"];
},
getTranslations() {
return {
en: "translations/en.json",
de: "translations/de.json",
id: "translations/id.json"
};
},
// audible confirmation sound that Lucy understood the command
playConfirmationSound() {
var audio = new Audio(localPath+"/sounds/" + this.config.confirmationSound);
audio.play();
},
// randomized greeting sounds
playGreetingSound() {
var random_file = this.config.sounds[Math.floor(Math.random() * this.config.sounds.length)];
var audio = new Audio(localPath+"/sounds/"+random_file);
audio.play();
},
getDom() {
//Log.log("lucy entered getDom")
const wrapper = document.createElement("div");
const lucy = document.createElement("div");
lucy.classList.add("small", "align-left");
const modeSpan = document.createElement("span");
const icon = document.createElement("i");
icon.classList.add("fa", this.icon, "icon");
if (this.pulsing) {
icon.classList.add("pulse");
modeSpan.innerHTML = "Command Please:"
}
else
modeSpan.innerHTML = this.mode;
lucy.appendChild(icon);
lucy.appendChild(modeSpan);
if (this.config.debug) {
const debug = document.createElement("div");
debug.innerHTML = this.debugInformation;
lucy.appendChild(debug);
}
const modules = document.querySelectorAll(".module");
for (let i = 0; i < modules.length; i += 1) {
if (!modules[i].classList.contains(this.name)) {
if (this.help) {
modules[i].classList.add(`${this.name}-blur`);
} else {
modules[i].classList.remove(`${this.name}-blur`);
}
}
}
if (this.help) {
lucy.classList.add(`${this.name}-blur`);
const modal = document.createElement("div");
modal.classList.add("modal");
this.appendHelp(modal);
wrapper.appendChild(modal);
}
wrapper.appendChild(lucy);
return wrapper;
},
notificationReceived(notification, payload, sender) {
//Log.log("lucy module notification received="+notification)
var self=this;
if (notification === "DOM_OBJECTS_CREATED") {
//Log.log("lucy module sending start")
this.sendSocketNotification("START", { config: this.config, modules: this.modules });
} else if (notification === "REGISTER_LUCY_MODULE") {
if (Object.prototype.hasOwnProperty.call(payload, "mode") && Object.prototype.hasOwnProperty.call(payload, "sentences")) {
this.modules.push(payload);
}
}
// randomly chosen startup greeting
if (notification === "DOM_OBJECTS_CREATED") {
this.playGreetingSound();
}
// @TheStigh to manage hide/show modules on startup
if (this.config.startHideAll) {
if (notification === "DOM_OBJECTS_CREATED") {
MM.getModules().enumerate((module) => {
module.hide(1000);
});
}
}
if (notification === "DOM_OBJECTS_CREATED"){
var showOnStart = MM.getModules().withClass(self.config.pageOneModules);
showOnStart.enumerate(function(module) {
var callback = function(){};
module.show(self.config.speed, callback);
});
}
if (notification === "DOM_OBJECTS_CREATED"){
var showOnStart = MM.getModules().withClass(self.config.defaultOnStartup);
showOnStart.enumerate(function(module) {
var callback = function(){};
module.show(self.config.speed, callback);
});
}
// to show page one on alert @TheStigh
if (notification === "SHOW_ALERT") {
var showOnStart = MM.getModules().withClass(self.config.pageOneModules);
showOnStart.enumerate(function(module) {
var callback = function(){};
module.show(self.config.speed, callback);
});
}
},
socketNotificationReceived(notification, payload) {
if (notification === "READY") {
this.icon = "fa-microphone";
this.mode = this.translate("NO_MODE")+ "'Hello Lucy'";
this.pulsing = false;
} else if (notification === "LISTENING") {
this.pulsing = true;
} else if (notification === "SLEEPING") {
this.pulsing = false;
} else if (notification === "ERROR") {
this.mode = notification;
} else if (notification === "LUCY") {
for (let i = 0; i < this.modules.length; i += 1) {
if (payload.mode === this.modules[i].mode) {
if (this.mode !== payload.mode) {
this.help = false;
this.sendNotification(`${notification}_MODE_CHANGED`, { old: this.mode, new: payload.mode });
this.mode = payload.mode;
}
if (this.mode !== "LUCY") {
this.sendNotification(`${notification}_${payload.mode}`, payload.sentence);
}
break;
}
}
} else if (notification === "HIDE_MODULES") {
this.playConfirmationSound();
MM.getModules().enumerate((module) => {
module.hide(1000);
});
} else if (notification === "SHOW_MODULES") {
this.playConfirmationSound();
MM.getModules().enumerate((module) => {
module.show(1000);
});
} else if (notification === "DEBUG") {
this.debugInformation = payload;
} else if (notification === "MODULE_STATUS") {
this.playConfirmationSound();
var hide = MM.getModules().withClass(payload.hide);
hide.enumerate(function(module) {
//Log.log("Hide "+ module.name);
var callback = function(){};
module.hide(self.config.speed, callback);
});
var show = MM.getModules().withClass(payload.show);
show.enumerate(function(module) {
//Log.log("Show "+ module.name);
var callback = function(){};
module.show(self.config.speed, callback);
});
} else if (notification === "MODULE_UPDATE") {
this.sendNotification(payload);
//console.log("sendNoti received :"+payload);
} else if (notification === "CLOSE_HELP") {
this.help = false;
} else if (notification === "OPEN_HELP") {
this.help = true;
}
this.updateDom(300);
},
appendHelp(appendTo) {
const listLabel = document.createElement("div");
listLabel.classList.add("medium");
// heading for help screen
listLabel.innerHTML = `${this.translate("Lucy Commands")}:`;
appendTo.appendChild(listLabel);
const list = document.createElement("ul");
for (let i = 0; i < this.lucy.sentences.length; i += 1) {
const item = document.createElement("li");
list.classList.add("xsmall");
item.innerHTML = this.lucy.sentences[i];
list.appendChild(item);
}
appendTo.appendChild(list);
}
});