This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

javascript lp file
#1
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
Reply
#2
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>
Reply
#3
Ok.

I've seen that event has no tag neither name info, is there any way to get it inside .lp file?
Reply
#4
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>
Reply
#5
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.
Reply
#6
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>
Reply
#7
Perfect, thanks!
Reply


Forum Jump: