Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION]: websocket usage for data updates #1590

Open
kheinrich188 opened this issue Jan 28, 2025 · 5 comments
Open

[QUESTION]: websocket usage for data updates #1590

kheinrich188 opened this issue Jan 28, 2025 · 5 comments
Labels

Comments

@kheinrich188
Copy link

kheinrich188 commented Jan 28, 2025

Hi there,

I’m experiencing an issue when trying to use the new WebSocket option in FUXA to update data more frequently than the 1-second interval provided by the WebAPI.

Here’s what I’ve done:

  1. Switched my WebAPI connection to an “internal” connection to retain the tags.
  2. Created a server-side script to fetch the initial data using the WebAPI.
  3. Set up the WebSocket connection to handle subsequent updates.

However, the behavior isn’t as expected. When fetching tag data by ID using $getTag(), it always returns null.

Example API Response:

From my WebAPI:

{
    "t_8f60fd-f8073b": "1",
    "t_cb52db-724556": "3",
    "t_a2d094-5596cc": "1"
}

The WebSocket returns the following:

42/fuxa,
[
    "fuxa-data",
    {
        "tagId": "t_9bece6-11c1c5",
        "data": "-21.2713623046875"
    }
]

When I try to then setTag(tagId, data) I always get setValue not supported!

Despite successfully retaining tag IDs and verifying their values using the template function $getTag(), attempting to access tags via $getTag('t_8f60fd-f8073b') consistently returns null.

console.log($getTag('t_8f60fd-f8073b' /* stHMIInterface - iCurrentUnit */));

Environment:
FUXA Version: 1.2.3

Expected Behavior:

  • Only have an internal device with tags.
  • $setTag to set the data to a internal device tag to get rid of the webapi connection

Please let me know if I’m missing something in the setup or if this is a bug. Any guidance would be much appreciated!

Thank you!

Script that I configured as 1s startup delay on server:

const readDataFromWebsocket = () => {
  // Handle successful connection
  ws.on('open', () => {
    console.log('WebSocket connection established');
    ws.send('40/fuxa');
  });

  ws.on('message', (message) => {
     try {
        const messageStr = message.toString();
        if (messageStr.startsWith('42/fuxa,')) {
          const parsedData = JSON.parse(messageStr.slice(8)); // Strip off the socket prefix "42/fuxa"
          const tagId = parsedData[1].tagId;
          const value = parsedData[1].data;
          console.log('tagId', tagId);
          console.log('value', value);
          $setTag(tagId, value);
        } else {
          console.log('Raw message:', messageStr);
        }
      } catch (error) {
        console.log('Error processing message from WebSocket server:', error);
      }
  });

  ws.on('error', (error) => {
    console.log('WebSocket error:', error);
  });

  ws.on('close', () => {
    console.log('WebSocket connection closed');
    //TODO: Attempt to reconnect after a delay if the connection is closed
  });
}

const getInitialFetchData = () => {
	const options = {
    hostname: hostname,
    port: port,
    path: getPath,
    method: 'GET',
  };

  const req = http.request(options, (res) => {
    let responseData = '';

    res.on('data', (chunk) => {
      responseData += chunk;
    });

    res.on('end', () => {
      const data = JSON.parse(responseData);
      if (data) {
        Object.entries(data).forEach(([tagId, value]) => {
           $setTag(tagId, value);
        });
        
        readDataFromWebsocket()
      } else {
        console.log('empty data');
      }
    });
  });

  req.on('error', (error) => {
    console.log('Error');
    console.log(JSON.stringify(error));
  });

  req.end();

}

getInitialFetchData()

@MatthewReed303
Copy link
Collaborator

@kheinrich188 see the Wiki on websockets I wrote up a little while a go on a project I was working on with Node-Red comms via websocket https://github.com/frangoteam/FUXA/wiki/HowTo-WebSockets

@kheinrich188
Copy link
Author

@MatthewReed303 I saw it and follow the steps but for any reason I t will not work to set the tags. Did you set your tags as an internal connection?

@MatthewReed303
Copy link
Collaborator

@kheinrich188 I used the Fuxa Server Tags and OPC-UA connection, any reason for internal? That's front end only, so only in the clients web browser.

@kheinrich188
Copy link
Author

kheinrich188 commented Jan 28, 2025

@MatthewReed303 Ok so I need to keep the WebAPI Connection as it is. Then I do a 1s get update of all data until the script and websocket connection is established then I think I can stop the device fetching every 1s and update the data with the websocket data?

@kheinrich188
Copy link
Author

As I see it is not allowed to set values when the connection has an address right?

 this.setValue = async function (tagId, value) {
        if (apiProperty.ownFlag && data.tags[tagId]) {
      ....
       }
}

@unocelli can you help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants