@@ -25,6 +25,7 @@ module.exports = function (RED) {
2525 payloadutils = require ( '../../utilities/payload-utils' ) ,
2626 sttutils = require ( './stt-utils' ) ,
2727 AuthV1 = require ( 'watson-developer-cloud/authorization/v1' ) ,
28+ AuthIAMV1 = require ( 'watson-developer-cloud/iam-token-manager/v1' ) ,
2829 muteMode = true , discardMode = false , autoConnect = true ,
2930 username = '' , password = '' , sUsername = '' , sPassword = '' ,
3031 apikey = '' , sApikey = '' ,
@@ -270,15 +271,30 @@ module.exports = function (RED) {
270271 }
271272
272273 function determineTokenService ( stt ) {
273- let tokenService = new AuthV1 ( stt . getCredentials ( ) ) ;
274+ let tokenService = null ;
275+
276+ if ( apikey ) {
277+ // console.log('API Key stuff to go here');
278+ // tokenService = new AuthV1(stt.getCredentials());
279+ // console.log('the keys that we have are ', stt.getCredentials());
280+ // let creds = {}; // stt.getCredentials();
281+ // creds.iamApikey = apikey;
282+ // console.log('Creating token with endpoint ', endpoint);
283+ // tokenService = new AuthIAMV1.IamTokenManagerV1({iamApikey : apikey, iamUrl: endpoint});
284+ tokenService = new AuthIAMV1 . IamTokenManagerV1 ( { iamApikey : apikey } ) ;
285+
286+ } else {
287+ // console.log('Standard Key');
288+ tokenService = new AuthV1 ( stt . getCredentials ( ) ) ;
289+ }
274290
275291 // Streaming - IAM Key fudge.
276292 // Check if the token service options have the header set. If not then
277293 // create them. This will stop the function from crashing the app,
278294 // altough it will still fail authentication.
279- if ( ! tokenService . _options . headers ) {
280- tokenService . _options . headers = { } ;
281- }
295+ // if (!tokenService._options.headers) {
296+ // tokenService._options.headers = {};
297+ // }
282298 return tokenService ;
283299 }
284300
@@ -321,6 +337,7 @@ module.exports = function (RED) {
321337 function getToken ( stt ) {
322338 var p = new Promise ( function resolver ( resolve , reject ) {
323339 var now = Math . floor ( Date . now ( ) / 1000 ) ;
340+
324341 var tokenService = determineTokenService ( stt ) ;
325342
326343 if ( tokenPending ) {
@@ -335,11 +352,13 @@ module.exports = function (RED) {
335352 tokenPending = true ;
336353 tokenService . getToken ( function ( err , res ) {
337354 if ( err ) {
355+ // console.log('Error getting token ', err);
338356 reject ( err ) ;
339357 } else {
340358 tokenPending = false ;
341359 tokenTime = now ;
342360 token = res ;
361+ // console.log('We have the token ', token);
343362 resolve ( ) ;
344363 }
345364 } ) ;
@@ -368,14 +387,19 @@ module.exports = function (RED) {
368387 + '?watson-token=' + token + '&model=' + model ;
369388 }
370389
390+ // console.log('wsURI is : ', wsURI);
391+
371392 if ( ! websocket && ! socketCreationInProcess ) {
372393 socketCreationInProcess = true ;
394+ // console.log('Attempting creation of web socket');
373395 var ws = new WebSocket ( wsURI ) ;
396+ // console.log('Setting up listeners');
374397 ws . on ( 'open' , ( ) => {
398+ // console.log('Socket is open');
375399 ws . send ( JSON . stringify ( startPacket ) ) ;
376400 websocket = ws ;
377401 socketCreationInProcess = false ;
378- //resolve();
402+ // resolve();
379403 } ) ;
380404
381405 ws . on ( 'message' , ( data ) => {
@@ -393,7 +417,7 @@ module.exports = function (RED) {
393417 if ( ! muteMode ) {
394418 payloadutils . reportError ( node , newMsg , d . error ) ;
395419 }
396-
420+ // console.log('Fetching token');
397421 token = null ;
398422 getToken ( determineService ( ) )
399423 . then ( ( ) => {
@@ -428,11 +452,13 @@ module.exports = function (RED) {
428452 ws . on ( 'error' , ( err ) => {
429453 socketListening = false ;
430454 if ( ! muteMode ) {
455+ var newMsg = { payload : 'STT Connection Error' } ;
456+ console . log ( 'Socket Error ' , err ) ;
431457 payloadutils . reportError ( node , newMsg , err ) ;
432458 }
433459 // console.log('Error Detected');
434460 if ( initialConnect ) {
435- //reject(err);
461+ // reject(err);
436462 }
437463 } ) ;
438464
@@ -495,10 +521,13 @@ module.exports = function (RED) {
495521 sendTheStack ( ) ;
496522 if ( audioData && audioData . action ) {
497523 if ( 'data' === audioData . action ) {
524+ // console.log('Sending data');
498525 websocket . send ( audioData . data , ( error ) => {
499526 if ( error ) {
527+ // console.log('Error Sending data ', error);
500528 reject ( error ) ;
501529 } else {
530+ // console.log('Data was sent');
502531 resolve ( ) ;
503532 }
504533 } ) ;
@@ -517,7 +546,7 @@ module.exports = function (RED) {
517546 . then ( ( ) => {
518547 switch ( audioData . action ) {
519548 case 'start' :
520- //return Promise.reject ('Its a start');
549+ // console.log ('Its a start');
521550 return processSTTSocketStart ( true ) ;
522551 case 'stop' :
523552 delay = 2000 ;
@@ -526,6 +555,7 @@ module.exports = function (RED) {
526555 // Add a Delay to allow the listening thread to kick in
527556 // Delays for Stop is longer, so that it doesn't get actioned
528557 // before the audio buffers.
558+ // console.log('We have data');
529559 setTimeout ( ( ) => {
530560 if ( socketListening ) {
531561 return sendAudioSTTSocket ( audioData ) ;
@@ -581,7 +611,7 @@ module.exports = function (RED) {
581611
582612 node . status ( { } ) ;
583613
584- let service = null ;
614+ let sttService = null ;
585615
586616 // Now perform checks on the input and parameters, to make sure that all
587617 // is in place before the service is invoked.
@@ -599,15 +629,15 @@ module.exports = function (RED) {
599629 return getService ( ) ;
600630 } )
601631 . then ( ( s ) => {
602- service = s ;
632+ sttService = s ;
603633 return processInput ( msg ) ;
604634 } )
605635 . then ( ( audioData ) => {
606636 node . status ( { fill :'blue' , shape :'dot' , text :'requesting' } ) ;
607637 if ( config [ 'streaming-mode' ] ) {
608- return performStreamSTT ( service , audioData ) ;
638+ return performStreamSTT ( sttService , audioData ) ;
609639 } else {
610- return performSTT ( service , audioData ) ;
640+ return performSTT ( sttService , audioData ) ;
611641 }
612642 } )
613643 . then ( ( data ) => {
0 commit comments