Logic Machine Forum
javascript lp file - 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: javascript lp file (/showthread.php?tid=4306)



javascript lp file - jmir - 20.10.2022

Hi,

Is it possible to use javascript grp.taglisten(tag_name, callback_function, all_events); function inside a .lp file?

I'm trying to do it and i'm getting this error from the browser:
Uncaught ReferenceError: grp is not defined

Thanks


RE: javascript lp file - admin - 20.10.2022

grp.taglisten is only available in the visualization context. In apps/.lp files you can only listen to all group addresses. This only works for authorized users.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Localbus</title>
  <script src="/apps/js/jquery.js.gz"></script>
  <script src="/scada/vis/busdecode.js.gz"></script>
  <script src="/apps/js/localbus.js.gz"></script>
</head>
<body>
<script>
$(function() {
  localbus.init();
  localbus.listen('groupwrite', function(event) {
    console.log(event.dst, event.value);
  });
});
</script>
</body>
</html>



RE: javascript lp file - jmir - 20.10.2022

Ok.

I've seen that event has no tag neither name info, is there any way to get it inside .lp file?


RE: javascript lp file - admin - 20.10.2022

This way you can filter events by a single tag:
Code:
<?
require('apps')

objs = grp.tag('TAGNAME')

tagobjs = {}
for _, obj in ipairs(objs) do
  tagobjs[ #tagobjs + 1 ] = obj.address
end
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Localbus</title>
  <script src="/apps/js/jquery.js.gz"></script>
  <script src="/scada/vis/busdecode.js.gz"></script>
  <script src="/apps/js/localbus.js.gz"></script>
</head>
<body>
<script>
$(function() {
  var tagobjs = <?=json.encode(tagobjs)?>;

  localbus.init();
  localbus.listen('groupwrite', function(event) {
    if (tagobjs.indexOf(event.dst) >= 0) {
      console.log(event.dst, event.value);
    }
  });
});
</script>
</body>
</html>



RE: javascript lp file - jmir - 21.10.2022

Thanks it works!

But is there any way to get group address values when loading the page? I'd like to get them when opening the page and not wait until one of them is sent to the bus.


RE: javascript lp file - admin - 21.10.2022

Try this:
Code:
<?
require('apps')

objs = grp.tag('TAGNAME')

tagobjs = {}
for _, obj in ipairs(objs) do
  tagobjs[ #tagobjs + 1 ] = obj.address
end
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Localbus</title>
  <script src="/apps/js/jquery.js.gz"></script>
  <script src="/scada/vis/busdecode.js.gz"></script>
  <script src="/apps/js/localbus.js.gz"></script>
</head>
<body>
<script>
$(function() {
  var tagobjs = <?=json.encode(tagobjs)?>;

  function getvalue(addr) {
    var id = localbus.encodega(addr);
    return localbus.object[ id ];
  }

  $(localbus).on('connect', function() {
    console.log('connected');
    console.log(getvalue('0/0/1'));
  });

  localbus.init();
  localbus.listen('groupwrite', function(event) {
    if (tagobjs.indexOf(event.dst) >= 0) {
      console.log(event.dst, event.value);
    }
  });
});
</script>
</body>
</html>



RE: javascript lp file - jmir - 21.10.2022

Perfect, thanks!