Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


use functions from external LUA


emeceuy

Recommended Posts

HI ! 

 

I have some functions that I use repeatelly in various scenes.

 

Insted of pasting those functions in each scene, is it posible to write them in an external lua so I can use them from scenes?

 

Regards! Marcelo.

Link to comment
Share on other sites

Please login or register to see this code.

Create a quickapp that will hold your functions (e.g.library), and call them directly. Using these calls you can run functions from other quickapps, but cant get back values, as hub.call never returns values if its calling other quickapp's functions.

hub.call(<libraryID>,<functionname>,<paramaters>)

Edited by Neo Andersson
Link to comment
Share on other sites

Hi @emeceuy ,

 

It is possible to run functions in other scenes, but this is restricted only on passing some values to that functions, but not getting back result value, at least not directly but only through global variables. Getting back result from external function is tricky since it depends on delays, time needed for external code to start and time needed for that code to do some action and then save result in global variable.

 

I use that solution in

Please login or register to see this link.

where I need to do https call and get some data from server. Since VD's can't do https calls it requires scene to do that.

VD therefore call scene to run and sends some arguments to the scene. After call to the scene VD code checks if scene is running and waits for scene to stop and then continues with execution of its own code which then reads values get by scene from the global variable. If there is more than one VD that makes call to the same scene, then VD first checks if scene is already run by another VD and waits for scene to stop before making its own call.

 

Another example is function that sends messages. I use one scene that does that and it is called by all VD's and scenes as necessary. Since messages are stored in VD's and scenes own global variables, all that message scene needs is to receive calling VD or scene ID number to know which message to send.

 

If above is not what you are asking about, then can you be more precise on what type of common code you are referring?

 

BTW @Neo Andersson  is talking about solution on HC3 with Quick Apps. I haven't yet tested some of my ideas on HC3, but since it is possible to call specific function on QA and send it some values, then it is possible to also send calling QA ID among those values so that QA which receives call knows which QA send request and therefore it can call it back and send some value. Problem with this solution is how to synchronize those calls between QA.

 

Link to comment
Share on other sites

On the HC3 we can achieve something that is close to a shared library with functions. The approach I use in fibaroExtra allows a QA to call any global Lua function in another QA, synchronously with returning values.

The QA with the global functions just declare their functions (assume in this example the deviceID is 801)

Ex.

Please login or register to see this code.

and then the QA need to define a QuickApp function to make it possible to call the functions

Please login or register to see this code.

 

The calling QA(s) then needs to create a remote function, specifying the deviceID of the QA with the function(s), and the global Lua name of the function to be called.

Please login or register to see this code.

The add function will timeout after 3s if the other QA doesn't answer (can be changed). If the remote add function crashes, the local add call will re-throw the error.

So, the big advantage here is that the calls are synchronous, meaning that add in this case return the value, like any local Lua function.

The drawback is of course that the caller will "wait" until the library function returns (or times out)

The library functions will only serve one client call at the time - there is no parallell execution in QAs (one could rewrite QuickApp:RPC_CALL to kind of work around that...)

There is of course a possibility to make the calls asynchronous with callbacks, like net.HTTPClient works, but that of course complicates things and requires another 

code structure to deal with asynchronous events...

 

What is the overhead? 

I measured 100 remote calls to the add function. The add function doesn't do very much and is thus very fast, so I assume most of the time spent will be the overhead of the call.

...and it took just under 8ms per call. That could be a price worth paying...

 

Oh, I forgot. The QA that calls the library functions need to declare the fibaro.rpc function, like this

Please login or register to see this code.

 

Please login or register to see this link.

are a standard feature of Lua and safe to use but unfortunately, Fibaro, have removed them from the Lua functions we get in a QA (or Scene).

With coroutines we could have coded this much more efficient and avoided the blocking call and wasted less cpu cycles... UI buttons could also respond while we do remote calls etc...

Edited by jgab
  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...
  • Topic Author
  • 20 minutes ago, emeceuy said:

    Excelent !!! Thank you all very much !!! will try and return with feedback !!! :)

    As aditional information, I use HC2...

     

    I see dont have much options !

    Link to comment
    Share on other sites

    • 3 months later...

    Are those files loaded on different TTS objects? The names would suggest that as they contain a GUID. There's no need to do that.

    Files, you want to require should be placed somewhere on your file system (e.g. not at the path where the TTS object scripts are located). You then have to set the directory where the files are located in the Atom TTS plugin settings, so it knows where to look for required files.

    Please login or register to see this link.

    Edited by soran28h
    Link to comment
    Share on other sites

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Reply to this topic...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...