Skip to content

Commit

Permalink
Merge pull request #383 from chughts/mute
Browse files Browse the repository at this point in the history
Various Configuration options for STT Streaming Mode
  • Loading branch information
chughts authored Mar 5, 2018
2 parents f3b5439 + 4a58461 commit 32f72bf
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 42 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ Node-RED Watson Nodes for IBM Cloud

<a href="https://cla-assistant.io/watson-developer-cloud/node-red-node-watson"><img src="https://cla-assistant.io/readme/badge/watson-developer-cloud/node-red-node-watson" alt="CLA assistant" /></a>


### New in version 0.6.6
- Added Mute option for STT Node warning status when running in Streaming mode
- Discard option for STT Node Streaming listening events.
- Added Auto-Connect mode for STT Node - Will keep connection to service alive,
else connection is restored on demand.
- Fix to Discovery Query Builder Node to return schema options.

### New in version 0.6.5
- Endpoint can now be specified in Natural Language Classifier Node

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-node-watson",
"version": "0.6.5",
"version": "0.6.6",
"description": "A collection of Node-RED nodes for IBM Watson services",
"dependencies": {
"async": "^1.5.2",
Expand All @@ -15,7 +15,7 @@
"word-count": "^0.2.2",
"is-docx": "^0.0.3",
"stream-to-array" : "^2.3.0",
"ws" : "^4.0.0"
"ws" : "^4.1.0"
},
"repository": {
"type": "git",
Expand Down
13 changes: 7 additions & 6 deletions services/discovery/v1-query-builder.html
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
// can be obtained from the service.
disQB.hideAll = function() {
$('#credentials-not-found').show();
cv1wm.hideSelectedFields(fields);
//disQB.hideSelectedFields(fields);
$('#something-went-wrong').hide();
$('#node-input-environment').parent().hide();
$('#node-input-collection').parent().hide();
Expand Down Expand Up @@ -609,7 +609,7 @@


// This is the on edit prepare function, which will be invoked everytime the dialog is shown.
function oneditprepare() {
function disQBoneditprepare() {
disQB.hideAll();
disQB.restoreHidden();

Expand All @@ -620,6 +620,7 @@
$('.credentials').toggle(!service);
// for some reason the getJSON resets the vars so need to restoreHidden again
// so again.
disQB.have_credentials = true;
disQB.restoreHidden();
if (!service) {
// no bound service, so check local settings to see if they are valid
Expand All @@ -637,7 +638,7 @@
$('#credentials-check').hide();
})
}
// credentials can be set during the oneditprepare process, so check again.
// credentials can be set during the disQBoneditprepare process, so check again.
if (disQB.have_credentials || disQB.using_local_credentials)
{
$('#credentials-check').hide();
Expand All @@ -654,7 +655,7 @@
}

// Save the values in the dyanmic lists to the hidden fields.
function oneditsave(){
function disQoneditsave(){
disQB.environment_selected = $('#node-input-environment').val();
$('#node-input-environmenthidden').val(disQB.environment_selected);

Expand Down Expand Up @@ -736,8 +737,8 @@
labelStyle: function() {
return this.name ? "node_label_italic" : "";
},
oneditprepare: oneditprepare,
oneditsave: oneditsave
oneditprepare: disQBoneditprepare,
oneditsave: disQoneditsave
});
})();
</script>
13 changes: 6 additions & 7 deletions services/discovery/v1-query-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,16 @@ module.exports = function(RED) {
username: sUsername ? sUsername : req.query.un,
password: sPassword ? sPassword : req.query.pwd,
url: req.query.endpoint ? req.query.endpoint : sEndpoint,
version_date: '2017-09-01',
version_date: '2017-11-07',
headers: {
'User-Agent': pkg.name + '-' + pkg.version
}
});

console.log('Getting Discovery Environments');
discovery.getEnvironments({}, function(err, response) {
if (err) {
console.log(err);
res.json(err);
} else {
console.log('Returning Envrionments');
res.json(response.environments ? response.environments : response);
}
});
Expand All @@ -67,7 +64,7 @@ module.exports = function(RED) {
username: sUsername ? sUsername : req.query.un,
password: sPassword ? sPassword : req.query.pwd,
url: req.query.endpoint ? req.query.endpoint : sEndpoint,
version_date: '2017-09-01',
version_date: '2017-11-07',
headers: {
'User-Agent': pkg.name + '-' + pkg.version
}
Expand All @@ -92,7 +89,7 @@ module.exports = function(RED) {
username: sUsername ? sUsername : req.query.un,
password: sPassword ? sPassword : req.query.pwd,
url: req.query.endpoint ? req.query.endpoint : sEndpoint,
version_date: '2017-09-01',
version_date: '2017-11-07',
headers: {
'User-Agent': pkg.name + '-' + pkg.version
}
Expand All @@ -101,7 +98,9 @@ module.exports = function(RED) {
discovery.query({
environment_id: req.query.environment_id,
collection_id: req.query.collection_id,
query: 'text:a,text:ibm',
//query: 'text:a,text:ibm',
// Need a Query that will return some data!
query: 'text:"Trump",text:"IBM",text:"Watson"',
count: 1
},
function(err, response) {
Expand Down
98 changes: 77 additions & 21 deletions services/speech_to_text/v1.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,24 @@
<label for="node-input-streaming-mode" style="width: 70%;"> Streaming Mode</label>
</div>

<div class="form-row">
<label>&nbsp;</label>
<input type="checkbox" id="node-input-streaming-mute" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-streaming-mute" style="width: 70%;"> Mute in Streaming Mode</label>
</div>

<div class="form-row">
<label>&nbsp;</label>
<input type="checkbox" id="node-input-discard-listening" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-discard-listening" style="width: 70%;"> Discard Listening events from STT</label>
</div>

<div class="form-row">
<label>&nbsp;</label>
<input type="checkbox" id="node-input-auto-connect" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-auto-connect" style="width: 70%;"> Attempt auto connect on connection close</label>
</div>

<div class="form-row">
<label>&nbsp;</label>
<input type="checkbox" id="node-input-speakerlabels"style="display: inline-block; width: auto; vertical-align: top;"></input>
Expand Down Expand Up @@ -148,6 +166,8 @@
is as per the WebSocket input for Speech To Text, with an action of either
<code>start</code> or <code>stop</code> or an audio blob. No token is needed
as the node takes care of that step.</p>
<p>>The Mute option allows for the supression of session timeout messages.</p>
<p>>The Discard Listening option allows for listening events from STT to be discarded.</p>
<p>For more information about the Speech To Text service, read the <a href="https://www.ibm.com/watson/services/speech-to-text/">documentation</a>.</p>
</script>

Expand Down Expand Up @@ -186,41 +206,61 @@
return self.indexOf(value) === index;
}

stt.showSelectedFields = function(fields) {
for (i = 0; i < fields.length; i++) {
$(fields[i]).parent().show();
}
}

stt.hideSelectedFields = function(fields) {
for (i = 0; i < fields.length; i++) {
$(fields[i]).parent().hide();
}
}


// Function to be used at the start, as don't want to expose any fields, unless the models are
// available. The models can only be fetched if the credentials are available.
stt.hideEverything = function () {
if (!stt.models) {
var fields = [];
$('#credentials-not-found').show();
$('label#node-label-message').parent().hide();
$('input#node-input-alternatives').parent().hide();
$('input#node-input-speakerlabels').parent().hide();
$('input#node-input-smartformatting').parent().hide();
$('select#node-input-lang').parent().hide();
$('select#node-input-band').parent().hide();
$('select#node-input-langcustom').parent().hide();

fields.push('#node-label-message'
+ ', #node-input-alternatives'
+ ', #node-input-speakerlabels'
+ ', #node-input-smartformatting'
+ ', #node-input-lang'
+ ', #node-input-band'
+ ', #node-input-langcustom');
stt.hideSelectedFields(fields);
}
}

// Check if there is a model then can show the fields.
// available. The models can only be fetched if the credentials are available.
stt.VisibilityCheck = function () {
var showFields = [];
var hideFields = [];
if (stt.models) {
$('label#node-label-message').parent().hide();
$('input#node-input-alternatives').parent().show();
$('input#node-input-speakerlabels').parent().show();
$('input#node-input-smartformatting').parent().show();
$('select#node-input-lang').parent().show();
//$('select#node-input-langcustom').parent().show();
$('select#node-input-band').parent().show();
showFields.push('#node-input-alternatives'
+ ', #node-input-speakerlabels'
+ ', #node-input-smartformatting'
+ ', #node-input-lang'
+ ', #node-input-band');

hideFields.push('#node-label-message');
} else {
$('label#node-label-message').parent().hide();
$('input#node-input-alternatives').parent().hide();
$('input#node-input-speakerlabels').parent().hide();
$('input#node-input-smartformatting').parent().hide();
$('select#node-input-lang').parent().hide();
$('select#node-input-langcustom').parent().hide();
$('select#node-input-band').parent().hide();
hideFields.push('#node-label-message'
+ ', #node-input-alternatives'
+ ', #node-input-speakerlabels'
+ ', #node-input-smartformatting'
+ ', #node-input-langcustom'
+ ', #node-input-lang'
+ ', #node-input-band');
}
stt.hideSelectedFields(hideFields);
stt.showSelectedFields(showFields);
}


Expand Down Expand Up @@ -365,6 +405,20 @@
}
});

$('#node-input-streaming-mode').change(function () {
var fields = [];
fields.push('#node-input-streaming-mute'
+ ', #node-input-discard-listening'
+ ', #node-input-auto-connect');

var checked = $('#node-input-streaming-mode').prop('checked')
if (checked) {
stt.showSelectedFields(fields);
} else {
stt.hideSelectedFields(fields);
}
});

$('#node-input-flushcache').click(function () {
stt.flushCache();
});
Expand Down Expand Up @@ -542,6 +596,8 @@
password: {value: ''},
'payload-response' :{value: false},
'streaming-mode' :{value: false},
'streaming-mute' :{value: true},
'discard-listening' :{value: false},
'default-endpoint' :{value: true},
'service-endpoint' :{value: 'https://stream.watsonplatform.net/speech-to-text/api'}
},
Expand Down
31 changes: 25 additions & 6 deletions services/speech_to_text/v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = function (RED) {
payloadutils = require('../../utilities/payload-utils'),
sttV1 = require('watson-developer-cloud/speech-to-text/v1'),
authV1 = require('watson-developer-cloud/authorization/v1'),
muteMode = true, discardMode = false, autoConnect = true,
username = '', password = '', sUsername = '', sPassword = '',
endpoint = '',
sEndpoint = 'https://stream.watsonplatform.net/speech-to-text/api',
Expand Down Expand Up @@ -133,6 +134,10 @@ module.exports = function (RED) {
function configCheck() {
var message = '';

muteMode = config['streaming-mute'];
discardMode = config['discard-listening'];
autoConnect = config['auto-connect'];

if (!config.lang) {
message = 'Missing audio language configuration, unable to process speech.';
} else if (!config.band) {
Expand Down Expand Up @@ -397,7 +402,10 @@ module.exports = function (RED) {
// Force Expiry of Token, as that is the only Error
// response from the service that we have seen.
// report the error for verbose testing
payloadutils.reportError(node,newMsg,d.error);
if (!muteMode) {
payloadutils.reportError(node,newMsg,d.error);
}

token = null;
getToken(determineService())
.then(() => {
Expand All @@ -406,7 +414,9 @@ module.exports = function (RED) {
} else if (d && d.state && 'listening' === d.state) {
socketListening = true;
// Added for verbose testing
node.send(newMsg);
if (!discardMode) {
node.send(newMsg);
}
//resolve();
} else {
node.send(newMsg);
Expand All @@ -418,12 +428,20 @@ module.exports = function (RED) {
websocket = null;
socketListening = false;
// console.log('STT Socket disconnected');
setTimeout(connectIfNeeded, 1000);
if (!muteMode) {
var newMsg = {payload : 'STT Connection Close Event'};
payloadutils.reportError(node,newMsg,'STT Socket Connection closed');
}
if (autoConnect) {
setTimeout(connectIfNeeded, 1000);
}
});

ws.on('error', (err) => {
socketListening = false;
payloadutils.reportError(node,newMsg,err);
if (!muteMode) {
payloadutils.reportError(node,newMsg,err);
}
// console.log('Error Detected');
if (initialConnect) {
//reject(err);
Expand Down Expand Up @@ -472,8 +490,9 @@ module.exports = function (RED) {
//websocket.send(a.data);
websocket.send(a.data, (error) => {
if (error) {
payloadutils.reportError(node,{},error);
} else {
if (!muteMode) {
payloadutils.reportError(node,{},error);
}
}
});
}
Expand Down

0 comments on commit 32f72bf

Please sign in to comment.