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


Remote calls, global functions


jgab

Recommended Posts

Here is another "poor mans solution" to global functions.

The fibaro:startScene is a well known model for calling functions in other scenes. In addition, this approach allows for return values (synchronous) and an easy way to declare the functions (calling and called).

 

In the example, inside main() we declare 3 remote functions that are defined in another scene (sceneID 24 in this case)

...and then we can use them as ordinary calls later inside main()

The usage of main() is just to allow for the needed functions for remote calls to be declared later in the scene...

Please login or register to see this code.

Minified version of remoteFun (incl. callRemote, require and import)

Please login or register to see this code.

The scene "exporting" the remote functions (or the "library scene")  looks like this

Please login or register to see this code.

We use the scene log to pickup the values so we don't need fibaro globals or cause race conditions.

The library scene needs to set max running instances to 10 (that's the maximum allowed parallel calls to the library)

It's a busy wait but that's the only way to get a synchronous call...

Edit1: can handle functions with multiple return values

Edit2: can handle errors in library funs - error is re-thrown in client scene

Edit3: Log roll-over improved

ToDo: Log roll-over mostly works, tried 10000 calls (~20 roll-overs) without a missed call-back, however not 100% sure...

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

This is good stuff, thanks for sharing!

I've been doing this in a much less elegant way, post only (no return values), using json dispatch tables in global vars to map server scenes to symbolic names.

 

Link to comment
Share on other sites

It is mind blowing! I played with this and I feel that fibaro will be never the same! 

Thank you @jgab ! I m on track for my first public scene :) 

 

Link to comment
Share on other sites

  • Topic Author
  • 7 minutes ago, FSE said:

    It is mind blowing! I played with this and I feel that fibaro will be never the same! 

    Thank you @jgab ! I m on track for my first public scene :) 

     

     

    Time for a disclaimer, and I did mention it in the original post - log roll-over. 

    Every 500 log entry (or close to it) the scen log is cleared (to not have infinite long logs and save memory). However, that means that when the ~500th function return value is written it will be cleared and the caller will not find it - returning nil.

    I have an idea how to get around that and is doing some tests - will come back with a fix soon.

    Link to comment
    Share on other sites

    Can we get more on "clever" find the id of Global scene?

    I know you posted the way,using key, but as I suspect you will put it on another level including also the Global scene. :)

     

    Link to comment
    Share on other sites

  • Topic Author
  • 5 minutes ago, FSE said:

    Can we get more on "clever" find the id of Global scene?

    I know you posted the way,using key, but as I suspect you will put it on another level including also the Global scene. :)

     

    It would be cool to have a model where library scenes where installed from a remote community repository, like Linux and apt-get

    • Like 2
    Link to comment
    Share on other sites

    I m not a coder but I suppose it is real hard to be done. I hope people from Fibaro community start building RFCs for scenes,VDs and have a common place to upload/share them. Also a mechanism to grep them direct from HomeCenter would be what everyone wanted. So I am not the only dreamer here. :)

     

    Link to comment
    Share on other sites

    :) I'm so excited so i place a wish list

    Request list:

    return on main scene the logs from Global Scene.

    Call Global Scene from a VD

    Store Global Scene functions to Global Variable table

     

    Edited by FSE
    Link to comment
    Share on other sites

    Here comes my first try to build a Global Scene that calculate SHA2 digest: SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/224, SHA-512/256 and a bonus: MD5, SHA-1, HMAC, BASE64 Encode, BASE64 Decode

     

    HOW TO USE IT

    As @jgab example you need on your the scene you need to declare a remote function like this

     

    Please login or register to see this code.

    As I am not a real coder, I m missing how to use HMAC for now. I need a way to benchmark the scene for debugging reasons.

    Also I like to create global table with the functions of scene, so to be easy to search for a specific function using global variables. This can be done by pressing "start" button on scene and call another Global scene that will validate/build/edit global Variable table.

    IMHO is time for big players to join their powers and put the requirements for global scenes. Most people here will follow the rules and will stop re-invent wheel for every scene/VD. :)

     

    Please login or register to see this attachment.

    Link to comment
    Share on other sites

  • Topic Author
  • A variation of 'remoteFun' is to have require/import

    Please login or register to see this code.

    Then we can do like this in the client

    Please login or register to see this code.

    i.e. you get the functions in a table, similar to how Lua modules with require works (e.g. json etc). The advantage is that the function names will not collide with the names of other functions in the scene.

    Alternativerly, use 'import' to declare them as global functions in the scene.

    Please login or register to see this code.

    To make this work you need to "export" the functions from the global scene. You do that by having a comment listing the functions you want to export.

    Ex.

    Please login or register to see this code.

    So, how do we find out that the library scene has id 24? I would propose a "Library VD" that can import library scenes and provide a name registry.

    On 6/9/2019 at 12:54 PM, FSE said:

    :) I'm so excited so i place a wish list

    Request list:

    return on main scene the logs from Global Scene.

    The "Global Scene" you mean the "Library scene", the scene where the functions are declared?

    Why return the logs from that? I will add so that errors are returned to the calling function.

    Quote

    Call Global Scene from a VD

    That should be doable. Just translate the api.get.

    Quote

    Store Global Scene functions to Global Variable table

    Think it's better to declare them in the library scene as in my last post.

     

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

    I'm away from my fibato to test new scene structure.

    About logging, I did not had any return of logs from library scene to the client scene. They return nil from SHA2 library scene.

    My thoughts if they required to be saved on GlobalVariableTable:

    IHMO it is brilliant to have EXPORT part on library. I like it a lot. Still I miss some other things that may come on future. 

    Building more complex library Scenes: Need somehow to know about dependencies  from other library scenes. Also version numbers for every library scenes and compatibility with version of dependencies. A good scenario could be a hybrid system with EXPORT and global Variables.

    A skeleton of GlobalSceneTable could be like this. Abilities could be populated by EXPORT automatically when Start button is pressed on Library Scene. 

    Please login or register to see this code.

     

    Thinking library  will get bigger, people can have the ability to have same library scenes multiple times with different version numbers that can be compatible with their code.

    Another minor issue  there is no fixing lowercase/uppercase of function name. 

    Edited by FSE
    Link to comment
    Share on other sites

    On 6/9/2019 at 12:23 PM, jgab said:

    It would be cool to have a model where library scenes

    github ;) 

    Link to comment
    Share on other sites

    Just now, 10der said:

    github ;) 

    That I was thinking about right now!!! :)

    I have something like this in my mind

    Please login or register to see this link.

    Link to comment
    Share on other sites

    And, you know, I look at this and thinking what we are perverts/masochists :) 

    Fibaro should (must) was implemented this feature at the same hour when Fibaro developers disable require.

    :( 

    Link to comment
    Share on other sites

    1 minute ago, 10der said:

    And, you know, I look at this and thinking what we are perverts/masochists :) 

    Fibaro should (must) was implemented this feature at the same hour when Fibaro developers disable require.

    :( 

    Fibaro want us to re-invent wheel. That is the reason that open communities for home automation do massive development instead of us, as we need to build our own tools...

    (I feel masochist also) :(

    Never the less, Fibaro let us to use LUA (HC2 only ), so that is something.....

    • Thanks 1
    Link to comment
    Share on other sites

  • Topic Author
  • 36 minutes ago, FSE said:

    Fibaro want us to re-invent wheel. That is the reason that open communities for home automation do massive development instead of us, as we need to build our own tools...

    (I feel masochist also) :(

    Never the less, Fibaro let us to use LUA (HC2 only ), so that is something.....

     

    It’s the challenge that makes it interesting ;)

     

    I’ve already implementing auto update of ER scenes from my GitHub so it’s doable.

    An issue is how to trust the code that you pull down...

    Link to comment
    Share on other sites

    a question: Can I understand if the call of library scene was done by scene or VD? I like to reverse search the VD/scene name that made the call on the library scene.

    Link to comment
    Share on other sites

  • Topic Author
  • 10 minutes ago, FSE said:

    a question: Can I understand if the call of library scene was done by scene or VD? I like to reverse search the VD/scene name that made the call on the library scene.

    First question is of course why you need that?

    Second you could add it as an argument to the remote calls.

    Third, you could add it as a third argument to 'fibaro:startScene(id,{urlencode(json.encode({fun,args})),tag,<scene or VD identifier>})', but you also need to change the library code to accept fibaro:args() with 3 arguments.

    Link to comment
    Share on other sites

     

    @T.Konopka I suppose we will need soon a checkbox  "This scene is hidden in the system" on Scenes as they will get massive

    Edited by FSE
    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...