cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
844
Views
0
Helpful
1
Replies

[xAPI] how to fetch data in an array and reuse value

Sydd
Level 1
Level 1

Hi all,

I'm trying to fetch data from xstatus Audio Input LocalInput for a specific use.

With SSH :

*s Audio Input LocalInput 48 AGC: "On"
*s Audio Input LocalInput 48 Channels: 1
*s Audio Input LocalInput 48 Connector 49: "Microphone.1"
*s Audio Input LocalInput 48 Connector 50: "Microphone.2"
*s Audio Input LocalInput 48 Connector 51: "Microphone.3"
*s Audio Input LocalInput 48 Connector 52: "Microphone.4"
*s Audio Input LocalInput 48 Connector 53: "Microphone.5"
*s Audio Input LocalInput 48 Connector 54: "Microphone.6"
*s Audio Input LocalInput 48 Connector 55: "Microphone.7"
*s Audio Input LocalInput 48 Connector 56: "Microphone.8"
*s Audio Input LocalInput 48 Direct: "Off"
*s Audio Input LocalInput 48 MixerMode: "GainShared"
*s Audio Input LocalInput 48 Mute: "Off"
*s Audio Input LocalInput 48 Name: "Microphone"
*s Audio Input LocalInput 50 AGC: "Off"

What I need is the Id of the LocalInput Group as in the audio-console.

 

Here is my function :

function listenAudioMgmt(event) {
 xapi.event.on('UserInterface Extensions Widget Action', (event) => {
   
    if (event.Type === 'pressed' && event.WidgetId === 'audioMgmt') {
      const msg = `idWidget=${event.WidgetId} / type=${event.Type} / value=${event.Value}`;
      console.log(msg);
      
      switch (event.Value) {
        case 'audio_offLine0':
          console.log('entering audio management : off-line presentation');
          //getAudioGroupId();
          
          xapi.status
            .get('Audio Input LocalInput')
            .then( listLocalInput => {
          
            console.log('toto');
            console.log(listLocalInput[0].Name)
                
            for ( let i = 0; i < listLocalInput.length; i++) {
              const listInput = listLocalInput[i];
              console.log(`LocalInput ${i}: ${listInput.id}: ${listInput.Name}`);
                  
              if ( listInput.Name === 'Microphone' ) {
                const inputId = listInput.id;
                console.log(`Got microphone group Id: ${inputId} / ${listInput.id}`);
              }
                  
            }
                
          });
    
          console.log(`Toto: ${inputId}`);
          
          break;
      }
    }
  });
  
}

the console.log(`Toto: ${inputId}`); at the end returns this error : ReferenceError: identifier 'inputId' undefined

This is because it is evaluated before the result of xapi.status.get('Audio Input LocalInput').

 

How can I store this value definitely for a later use ? I've tried to execute at the beginning of the function, same error.

Do I need to dig more into javascript promises ?

 

 

1 Accepted Solution

Accepted Solutions

Sydd
Level 1
Level 1

Hi all,

 

I found a solution by using promises.

A quick example below :

 

const xapi = require('xapi');

//=====
var inputMicId = new Promise(
  function (resolve, reject) {
    xapi.status
      .get('Audio Input LocalInput')
      .then( listLocalInput => {

        for ( let i = 0; i < listLocalInput.length; i++) {
          if ( listLocalInput[i].Name === 'Microphone' ) {
            var inputId = listLocalInput[i].id;
            break;
          }
        }
      resolve(inputId);
      });
  }
);

//=====
var outputLoudSpeakerId = new Promise(
  function (resolve, reject) {
    xapi.status
      .get('Audio Output LocalOutput')
      .then( listLocalOutput => {

        for ( let i = 0; i < listLocalOutput.length; i++) {
          if ( listLocalOutput[i].Name === 'Loudspeaker' ) {
            var outputId = listLocalOutput[i].id;
            break;
          }
      }
      resolve(outputId);
      });
  }
);

//------------------
function listenAudioMgmt(event) {
// listen actions
xapi.event.on('UserInterface Extensions Widget Action', (event) => {

  if (event.Type === 'pressed' && event.WidgetId === 'audioMgmt') {
    const msg = `idWidget=${event.WidgetId} / type=${event.Type} / value=${event.Value}`;
    console.log(msg);

    switch (event.Value) {

      case 'audio_offLine0':
        console.log('entering audio management : off-line presentation');
        inputMicId
          .then(function(MicId) {

            outputLoudSpeakerId
              .then(function(SpeakerId) {
                console.log('inputId: ' + MicId);
                console.log('outputId: ' + SpeakerId);
              });
        });
        break;
      }

}

I'm using this to connect the microphone group to the quadcam's speaker output. Useful in a hall where a Room Kit Pro will be installed so that the local audience will hear the lecturer as will the remote audience. In this way, all sound signals go through the Room Kit. If there is a better solution, please tell me.

 

If someone wants the full code, it's attached to this reply. I still have to factorize the code because similar things are done several times. Please be indulgent, I'm far from a javascript expert.

 

I wonder if there is a way to factorize the promises by passing ti two parameters : path and searchString :

var inputMicId = new Promise(
  function (resolve, reject) {
    xapi.status
      .get('PATH')
      .then( list=> {

        for ( let i = 0; i < list.length; i++) {
          if ( list[i].Name === 'SEARCHSTRING' ) {
            var Id= list[i].id;
            break;
          }
        }
      resolve(id);
      });
  }
);

Thank you for reading.

View solution in original post

1 Reply 1

Sydd
Level 1
Level 1

Hi all,

 

I found a solution by using promises.

A quick example below :

 

const xapi = require('xapi');

//=====
var inputMicId = new Promise(
  function (resolve, reject) {
    xapi.status
      .get('Audio Input LocalInput')
      .then( listLocalInput => {

        for ( let i = 0; i < listLocalInput.length; i++) {
          if ( listLocalInput[i].Name === 'Microphone' ) {
            var inputId = listLocalInput[i].id;
            break;
          }
        }
      resolve(inputId);
      });
  }
);

//=====
var outputLoudSpeakerId = new Promise(
  function (resolve, reject) {
    xapi.status
      .get('Audio Output LocalOutput')
      .then( listLocalOutput => {

        for ( let i = 0; i < listLocalOutput.length; i++) {
          if ( listLocalOutput[i].Name === 'Loudspeaker' ) {
            var outputId = listLocalOutput[i].id;
            break;
          }
      }
      resolve(outputId);
      });
  }
);

//------------------
function listenAudioMgmt(event) {
// listen actions
xapi.event.on('UserInterface Extensions Widget Action', (event) => {

  if (event.Type === 'pressed' && event.WidgetId === 'audioMgmt') {
    const msg = `idWidget=${event.WidgetId} / type=${event.Type} / value=${event.Value}`;
    console.log(msg);

    switch (event.Value) {

      case 'audio_offLine0':
        console.log('entering audio management : off-line presentation');
        inputMicId
          .then(function(MicId) {

            outputLoudSpeakerId
              .then(function(SpeakerId) {
                console.log('inputId: ' + MicId);
                console.log('outputId: ' + SpeakerId);
              });
        });
        break;
      }

}

I'm using this to connect the microphone group to the quadcam's speaker output. Useful in a hall where a Room Kit Pro will be installed so that the local audience will hear the lecturer as will the remote audience. In this way, all sound signals go through the Room Kit. If there is a better solution, please tell me.

 

If someone wants the full code, it's attached to this reply. I still have to factorize the code because similar things are done several times. Please be indulgent, I'm far from a javascript expert.

 

I wonder if there is a way to factorize the promises by passing ti two parameters : path and searchString :

var inputMicId = new Promise(
  function (resolve, reject) {
    xapi.status
      .get('PATH')
      .then( list=> {

        for ( let i = 0; i < list.length; i++) {
          if ( list[i].Name === 'SEARCHSTRING' ) {
            var Id= list[i].id;
            break;
          }
        }
      resolve(id);
      });
  }
);

Thank you for reading.