Skip to content

Commit

Permalink
fix code climate, created helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
fewieden committed Apr 27, 2017
1 parent ec1d343 commit f06577d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 68 deletions.
6 changes: 3 additions & 3 deletions DEVELOPER.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ As soon as you receive the notification `ALL_MODULES_STARTED` from the core syst
* notification: `REGISTER_VOICE_MODULE`
* payload: Object with `mode` (string) and `sentence` (array) properties

### Example
### Example commands registration

```javascript
notificationReceived: function (notification, payload, sender) {
Expand All @@ -42,7 +42,7 @@ When the user is in the mode of your module, you will receive the following noti
* notification: `VOICE_YOURMODE`
* payload: String with all detected words.

### Example
### Example commands recognition

```javascript
notificationReceived: function (notification, payload, sender) {
Expand Down Expand Up @@ -70,7 +70,7 @@ When the mode of MMM-voice gets changed it will send a broadcast `VOICE_MODE_CHA

This gets handy e.g. to revert your manipulations on the DOM.

### Example
### Example mode change

```javascript
notificationReceived: function (notification, payload, sender) {
Expand Down
136 changes: 71 additions & 65 deletions node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ module.exports = NodeHelper.create({
help: false,
words: [],

start() {
console.log(`Starting module helper: ${this.name}`);
this.time = this.config.timeout * 1000;
},

socketNotificationReceived(notification, payload) {
if (notification === 'START') {
this.config = payload.config;
Expand All @@ -34,38 +39,25 @@ module.exports = NodeHelper.create({

fillWords() {
// create array
const array = this.config.keyword.split(' ');
let words = this.config.keyword.split(' ');
const temp = bytes.q.split(' ');
for (let i = 0; i < temp.length; i += 1) {
array.push(temp[i]);
}
words = words.concat(temp);
for (let i = 0; i < this.modules.length; i += 1) {
const mode = this.modules[i].mode.split(' ');
for (let m = 0; m < mode.length; m += 1) {
array.push(mode[m]);
}
words = words.concat(mode);
for (let n = 0; n < this.modules[i].sentences.length; n += 1) {
const sentences = this.modules[i].sentences[n].split(' ');
for (let x = 0; x < sentences.length; x += 1) {
array.push(sentences[x]);
}
words = words.concat(sentences);
}
}

// sort array
array.sort();

// filter duplicates
let i = 0;
while (i < array.length) {
if (array[i] === array[i + 1]) {
array.splice(i + 1, 1);
} else {
i += 1;
}
}
words = words.filter((item, index, data) => data.indexOf(item) === index);

this.words = array;
// sort array
words.sort();

this.words = words;
},

checkFiles() {
Expand Down Expand Up @@ -136,67 +128,81 @@ module.exports = NodeHelper.create({

startPocketsphinx() {
console.log(`${this.name}: Starting pocketsphinx.`);
this.time = this.config.timeout * 1000;

this.ps = new Psc({
setId: this.name,
verbose: true,
microphone: this.config.microphone
});

this.ps.on('data', (data) => {
if (typeof data === 'string') {
if (this.config.debug) {
console.log(data);
this.sendSocketNotification('DEBUG', data);
}
if (data.includes(this.config.keyword) || this.listening) {
this.listening = true;
this.sendSocketNotification('LISTENING');
if (this.timer) {
clearTimeout(this.timer);
}
this.timer = setTimeout(() => {
this.listening = false;
this.sendSocketNotification('SLEEPING');
}, this.time);
} else {
return;
}
this.ps.on('data', this.handleData);

let cleanData = this.cleanData(data);
if (this.config.debug) {
this.ps.on('debug', this.logDebug);
}

for (let i = 0; i < this.modules.length; i += 1) {
const n = cleanData.indexOf(this.modules[i].mode);
if (n === 0) {
this.mode = this.modules[i].mode;
cleanData = cleanData.substr(n + this.modules[i].mode.length).trim();
break;
}
this.ps.on('error', this.logError);

this.sendSocketNotification('READY');
},

handleData(data) {
if (typeof data === 'string') {
if (this.config.debug) {
console.log(`${this.name} has recognized: ${data}`);
this.sendSocketNotification('DEBUG', data);
}
if (data.includes(this.config.keyword) || this.listening) {
this.listening = true;
this.sendSocketNotification('LISTENING');
if (this.timer) {
clearTimeout(this.timer);
}
this.timer = setTimeout(() => {
this.listening = false;
this.sendSocketNotification('SLEEPING');
}, this.time);
} else {
return;
}

if (this.mode) {
this.sendSocketNotification('VOICE', { mode: this.mode, sentence: cleanData });
if (this.mode === 'VOICE') {
this.checkCommands(cleanData);
}
let cleanData = this.cleanData(data);

for (let i = 0; i < this.modules.length; i += 1) {
const n = cleanData.indexOf(this.modules[i].mode);
if (n === 0) {
this.mode = this.modules[i].mode;
cleanData = cleanData.substr(n + this.modules[i].mode.length).trim();
break;
}
}
});

if (this.config.debug) {
this.ps.on('debug', (data) => {
fs.appendFile('modules/MMM-voice/debug.log', data);
});
if (this.mode) {
this.sendSocketNotification('VOICE', { mode: this.mode, sentence: cleanData });
if (this.mode === 'VOICE') {
this.checkCommands(cleanData);
}
}
}
},

this.ps.on('error', (error) => {
if (error) {
fs.appendFile('modules/MMM-voice/error.log', error);
this.sendSocketNotification('ERROR', error);
logDebug(data) {
fs.appendFile('modules/MMM-voice/debug.log', data, (err) => {
if (err) {
console.log(`${this.name}: Couldn't save error to log file!`);
}
});
},

this.sendSocketNotification('READY');
logError(error) {
if (error) {
fs.appendFile('modules/MMM-voice/error.log', `${error}\n`, (err) => {
if (err) {
console.log(`${this.name}: Couldn't save error to log file!`);
}
this.sendSocketNotification('ERROR', error);
});
}
},

cleanData(data) {
Expand Down

0 comments on commit f06577d

Please sign in to comment.