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.

Js variables & Lua variables
#1
Hello !

I am actually able to put a lua value into a js variable using a piece of code that i found on this forum :

var js_variable = '<? write(lua_variable) ?>'

If someone could explain to me what is this write function ... and why don't we need the ';' here ?


Also, i would like to do the opposite but when i try something like :

'<? lua_variable ?>' = js_variable;

That does not work... 
Thx for help !
Reply
#2
Hi,

This is a sample how to write var into JS as client side LUA and yes you need ; if you have more lines.

You can't write back like this as the data is just a result from the server with option to have additional LUA data in it. The data is static so if you want new data you have to request it from the server again.

Sending back JS to LUA must be done by http post with arguments or json form data to the .lp file and handle the posted data in LUA on server also from .lp file. You can include your user libs to the .lp and have full bidirectional communication by get/post data between server and client.

You can use all LUA functions inside the .lp including sockets and ports and that allows you to do http/udp/tcp/bacnet/modbus/enocean requests from your clients javascript engine. Offcourse your .lp file must handle all the requests and translate it to LUA requests or JSON posts back to the client.

There is also a option to use the websocket connection by adding the localbus.js and that allows you to read and write to the knx engine. This way you can have live events without polling or posting all the time.

BR,

Erwin
Reply
#3
(17.03.2017, 16:12)Jonathan Wrote: var js_variable = '<? write(lua_variable) ?>'

Which lua variable did you connect with your js script? Is this lua variable from .lp which you created before or is this a current value of some global variable which is available from every script like storage?
Reply
#4
Thx for your response Erwin

buuuudzik, it's a variable from a .lua Smile

(17.03.2017, 17:19)Erwin van der Zwart Wrote: There is also a option to use the websocket connection by adding the localbus.js and that allows you to read and write to the knx engine. This way you can have live events without polling or posting all the time.

I haven't found any documentation on this, could you explain me which functions can be used with localbus ?
Reply
#5
Hi,

You can use the localbus like this:

1) First you need to include the JS lib

Code:
<head>
 <script src="/apps/js/localbus.js.gz"></script>
</head>

2) You need to start (init) the localbus

Code:
<script>
    (function() {
        if (typeof localbus !== 'undefined') {
            localbus.init();
        }
    });
</script>

3) Now you can listen to all or a single KNX events like this

Code:
<script>
 $(function() {
    // listen to all groupwrite telegrams
    localbus.listen('groupwrite', function(event) {
      console.log(event);
    });

    // listen to a single object
      localbus.listen('object', '1/1/1', function(value) {
      console.log(value);
    });
 });
</script>

4) Or update / write to a object like this

Code:
<script>
 $(function() {  
   localbus.write('1/1/1', true);  // same as grp.write('1/1/1', true)
   // or
  localbus.update('1/1/2', 42); // same as grp.update('1/1/2', 42)
 });
</script>

5) You can also listen to storage change events like this

Code:
<script>
 $(function() {  
   localbus.listen('storage', 'YourStorageName', function(value){
   console.log(value);
   });
 });
</script>

Don't know if we can also simply write to the storage by using this, maybe admin can say something about that

Code:
<script>
 $(function() {
   localbus.write('storage', 'YourStorageName', "your string"); // can we write to storage?
   // or
   localbus.write('storage', 'YourStorageFolder:YourStorageName', "your string"); // can we use the storage folders to read write to?
   // or
   localbus.write('storage', 'YourStorageName', {value1:10, value2:20, value3:30}); // can we write a json object or must this be a LUA table string?
 });
</script>

Edit: Tested yesterday evening the above option to write to storage and is not possible as far as i can tell... 

BR,

Erwin
Reply
#6
Tyvm Erwin, this will be helpful !
I don't want to ask you too many things, but could you also explain me how do you do in Lua for retrieving a POST variable ? I can manage to post with $.post but i don't know how to get them back in lua...
Reply
#7
Have a look at apps docs here (LP scripts section): http://forum.logicmachine.net/showthread.php?tid=85
Reply
#8
It's all good, tyvm Smile
Reply


Forum Jump: