Logic Machine Forum
Periodically Refresh Current Visualization - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Periodically Refresh Current Visualization (/showthread.php?tid=4152)



Periodically Refresh Current Visualization - jamesng - 18.07.2022

Hi 

I use the following JavaScript to periodically refresh the LogicMachine user interface on our iPad Clients running in a Safari browser session - we were finding that they would loose connection to the LogicMachine Server over time (either due to network dropouts or users using another app on the iPad and returning the disconnected LogicMachine Safari browser session to the foreground)

The refresh works really well however is there a way to capture the current visualisation id and to modify the reload part of the JS to reload the browser window back to that page / plan?  The current refresh always takes one back to the starting page.

Code:
$(document).ready(function(){
    setInterval(function(){ window.location.reload(true); },60*60000);
});

Many thanks

James


RE: Periodically Refresh Current Visualization - admin - 18.07.2022

There's an automatic reconnect algorithm that should trigger after several seconds if a disconnect happens. Try waiting around 15 seconds or so, does it start working afterwards?

You can use Page Visibility API to force reload in case the page was hidden then shown again.
Code:
$(function() {
  var hidden;

  document.addEventListener('visibilitychange', function() {
    var state = document.visibilityState;
    var planid = window.currentPlanId;
    
    if (state == 'visible' && hidden && planid) {
      window.location = '/scada-vis/#' + planid;
      window.location.reload();
    }
    else if (state == 'hidden') {
      hidden = true;
    }
  }, false);
});

Another possible solution is to send a ping command after the page is shown again. This can speed up the disconnect detection. It should also be a bit faster compared to the whole page reload. See if it works for you.
Code:
$(function() {
  var hidden;

  document.addEventListener('visibilitychange', function() {
    var state = document.visibilityState;
    var lb = window.localbus;
    
    if (state == 'visible' && hidden && lb && lb.ws) {
      lb.ws.send('ping');
    }
    else if (state == 'hidden') {
      hidden = true;
    }
  }, false);
});



RE: Periodically Refresh Current Visualization - jamesng - 19.07.2022

Hi

Using page visibility to force a reload is working really well.

I also tried the sending the ping, however the UI remained unresponsive even after 15 seconds and the CBUS loads couldn't be controlled.

Thanks for the solution!

Kind Regards
James