Button in a layout to open another layout

Answered

Comments

8 comments

  • Avatar
    Ichiro

    Hi Gil,

    Thanks for your question and interest in Nx integration.

    Are you saying you want to have a "software trigger" to do some actions for you?
    May you share which video you have seen for such use cases?

    Thanks.

    1
    Comment actions Permalink
  • Avatar
    Gil MARCHAL

    Hello,
    It's in this video https://www.youtube.com/watch?v=pZPellijnUw&t=252s
    The button where it says "Access Bat A" is an html page and when clicked on it, another layout opens.
    Thank you

    0
    Comment actions Permalink
  • Avatar
    Andrey Terentyev

    Hi,

    The approach is the following.

    1. Create a layout to be opened.

    2. Create an Event Rule

    When: Generic Event

    Either "Source contains", or "Caption contains" or "Description contains" having a pattern to identify and distinguish the layout to be opened.

    Action: Open layout 

    Click "Layout" and select layout to be opened.

    3. In the JS code of the web page invoke the /api/createEvent Server API with the "caption" parameter set to "Camera".

    1
    Comment actions Permalink
  • Avatar
    Gil MARCHAL

    Hi Andrey Terentyev
    Thank you for your precious help,
    I created the rule but I can't make js code that works :/
    Do you have an example?
    Thank you

    0
    Comment actions Permalink
  • Avatar
    Andrey Terentyev

    Hi,

    Generally, for invoking Server REST API you need to authenticate. 

    In JS API there is no need to do that, because GUI client works with already authenticated user creds.

    In version 5.1, for getting auth token use Auth API

    Authentication API

    This section contains specification for the authentication API.

    vms.auth object

    Contains methods to retrieve authentication data.

    Method

    async sessionToken() 

    Returns a user session token or empty string if it is inaccessible (e.g. the user prohibited token transfer).

    Could be used to authorize the rest api calls to the currently connected system.

    If the cloud user is not part of the system OR if system is not connected a cloud at all, local bearer token will be returned.

    Method

    async cloudToken()

    Returns a cloud refresh token or empty string if it is inaccessible (e.g. the user prohibited token transfer).

    This token can be used to obtain an access token (using cloud) with the specified scope.

    Returns the token even if the Cloud user is not part of the currently connected system(local user login). 

    Returns nothing with no error message if Client is not logged into the cloud.

    Method

    async cloudSystemId()

    Returns the cloud system id or empty string if the system is not connected to cloud.

    Method

    async cloudHost()

    Returns the cloud host.

    Example

    const token = await window.vms.auth.sessionToken();

    Code examples are found in js_api_example of nx_open_integrations repo on GitHub.

    Once the token is retrieved, you can invoke Server APIs

    https://meta.nxvms.com/doc/developers/api-tool/main?type=1&system=4

    Code example, although a bit outdated, can be found in node_js/src/mediaserverApi.ts. What needs to be refactored is updating the MediaserverApi class with token auth schema support.

    1
    Comment actions Permalink
  • Avatar
    Gil MARCHAL

    Hi Andrey Terentyev

    I tried this JS code but it is probably wrong or incomplete

    <script>
    function sendRequest() {
      $.ajax({
        url: 'https://192.168.1.138:7001/api/createEvent',
        type: 'post',
        data: JSON.stringify({ caption: nereides }),
        headers: { 'Content-Type': 'application/json' },
        dataType: 'json',
        success: function (data) {
          console.log(data);
        }
      });
    }
    </script>

    If in JS there is no need to authenticate I prefer to use this method but I don't know how to form the code.
    Do you have a simple example of JS code?

    Thank you

    0
    Comment actions Permalink
  • Avatar
    Andrey Terentyev

    Hello Gil,

    What is the error message you get when execute this code?

    0
    Comment actions Permalink
  • Avatar
    Gil MARCHAL

    Hi Andrey Terentyev

    I modified the code:

    <script>
    function sendRequest() {
      const boutonValue = 'CARTE';
      const requestData = { caption: boutonValue };

      fetch('https://srvnx:7001/api/createEvent', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify(requestData)
      })
        .then(response => {
          if (response.status !== 200) {
            console.error('Erreur de statut HTTP :', response.status);
            return;
          }
          return response.json(); // Parse la réponse JSON
        })
        .then(data => {
          if (data) {
            console.log(data);
          } else {
            console.error('Réponse vide ou non valide.');
          }
        })
        .catch(error => {
          console.error('Erreur lors de la requête :', error.message);
        });
    }
    </script>

    and I get the error

    POST https://srvnx:7001/api/createEvent 401 (Unauthorized)

    0
    Comment actions Permalink

Please sign in to leave a comment.