Jump to content
Guides for the Forum Read more... ×
Poradniki na Forum Read more... ×
  • 0
xirilius

Global Functions in Fibaro HC2 LUA?

Question

Hello,

 

Is it posible in HC2 LUA to create global functions?

I use o lot of functions over and over again and have to include them in every scene

 

Is it possible to create functions in a centralized scene and use them from every other scene?

 

Example:

 

------------------------------------------------------------------------------------

GlobalFunctions

------------------------------------------------------------------------------------

global function BeepXseconds(x)

   fibaro:call(42, "turnOn")

   fibaro:sleep(x * 1000)

   fibaro:call(42, "turnOff")

end

 

global function MyDebug(text, color)

   -- read GlobalGlob

  if DebugGlob = true then   

       -- do something with formatting text, add OS.date and add colors

      fibaro:debug(text)

   end

end

------------------------------------------------------------------------------------

 

 

now i can use the global functions in other scenes

 

------------------------------------------------------------------------------------

scene 161 - TurnAlarmOn

------------------------------------------------------------------------------------

-- do something

MyDebug("Turn Alarm on", "red")

BeepXseconds(5)

-- do something

------------------------------------------------------------------------------------

 

 

 

 

------------------------------------------------------------------------------------

scene 254 - Doorbell

------------------------------------------------------------------------------------

-- do something

MyDebug("Someone is ringing", "BLUE")

BeepXseconds(2)

-- do something

------------------------------------------------------------------------------------

 

 

 

AND OFCOURSE I CAN CREATE SCENES AND USE THEM LIKE GLOBAL FUNCTIONS

BUT PARSING VARIABLES IS NOT THAT SIMPLE,  I THEN HAVE TO USE GLOBAL VARIABLES

 

REAL GLOBAL FUNCTIONS (LIKE A LIBRARY) WOULD BE GREAT !!!

 

 

Edited by xirilius

Share this post


Link to post
Share on other sites

15 answers to this question

Recommended Posts

  • 1
5 hours ago, xirilius said:

AND OFCOURSE I CAN CREATE SCENES AND USE THEM LIKE GLOBAL FUNCTIONS

BUT PARSING VARIABLES IS NOT THAT SIMPLE,  I THEN HAVE TO USE GLOBAL VARIABLES

 

Not necessarily by using global variables, you can pass parameters to a scene.

 

Suppose you want to pass an  ID and a Value to another scene. Sending scene:

Please login or register to see this code.

 

Receiving scene (this has ID 207):

Please login or register to see this code.

 

  • Thanks 1

Share this post


Link to post
Share on other sites
  • 0

i am not realy understand.

tell me if i am rong

you want a global that have al the globals you need and refer them in a scene, something like the Hometable??

Share this post


Link to post
Share on other sites
  • 0
  • Inquirer
  • No, i want to create functions who are accessible from every scene

    Share this post


    Link to post
    Share on other sites
    • 0

    As far as I know your only option is to store the "global function" some way (e.g. json formated) in a global variable and import it where it is needed. 

    Share this post


    Link to post
    Share on other sites
    • 0

    @petergebruers hi!

     

    can help me?

     

    scene id 123

     

    Please login or register to see this code.

    calling

    Please login or register to see this code.

    my question is -how I can get function result?

    Your workaround OK for calling, but FIBARO how that deaf can't understand what all users wish declaring a global function... 

     

    PS: please do not recommend me "remove tonsils through the anus..."  ;D  I want simple get a result from a function...

     

    Share this post


    Link to post
    Share on other sites
    • 0

    So, this is one of the reasons that I'm working with an 'event' based model when programming my scenes. I have scenes that send requests to 'library' scenes that perform some functions and return results. The implementation uses startScene and attaches the scene ID of the calling scene so the 'library' scene knows where to return the answer. However, it becomes asynchronous so it is easier if the whole scene logic is based on events. Here is an example that calls a scene that looks up users in Apple's iCloud. (the iCloud code is not included)

     

    I have a new approach with a 'scripting' language that can hide the asynchronous nature of the call and just allow for ex. "location=callILocator(credentials)"... I will shows some examples in a later post in the thread below.

     

     

    • Thanks 1

    Share this post


    Link to post
    Share on other sites
    • 0

    dear @jgab you as always cool :) 

    I am learning LUA by your code, it's true. but now I am simply asking to implement easily including user library in any scenes / VDs for example via LUA FIBARO header 

    Share this post


    Link to post
    Share on other sites
    • 0
    Just now, 10der said:

    dear @jgab you as always cool :) 

    I am learning LUA by your code, it's true. but now I am simply asking to implement easily including user library in any scenes / VDs for example via LUA FIBARO header 

    Yes, you're right. Fibaro should find a way for shared user libraries. The best way would be to support Lua's 'require', but they would need to find a place to store the code - some new type of scene, call it library? The other issue is possible code/scene bloat as people would start to include a lot of 'good to have' code... :-) 

    Share this post


    Link to post
    Share on other sites
    • 0

     

    Please login or register to see this code.

     

    Edited by 10der

    Share this post


    Link to post
    Share on other sites
    • 0

    Haven't tested this really, but this is a concept of how a homemade 'require' could work.

    require(sceneID,module_name) checks if the  calling scene has the same version of the module as is in the scene with sceneID. If yes it just returns the local module, otherwise it patches in the module code from the other scene and  aborts. If the scene has autostart it will restart with the new code.

    When require is called the module code needs to have loaded, that's why we call a 'main' after the scene is fully loaded.

    If scene 320 looks like this (the Library scene) ( a library scene can have many modules)

    Please login or register to see this code.

    and scene 319 looks like this (the client scene), it will patch in the new module from 320

    Please login or register to see this code.

     

    Edited by jgab

    Share this post


    Link to post
    Share on other sites
    • 0
    1 minute ago, jgab said:

     

    Please login or register to see this code.

     

     

     

    Aaaah! @Fibaro  Do you even know what you're doing? :oops:

     

    Share this post


    Link to post
    Share on other sites
    • 0

    the regexp parsing of module is a bit too simplistic. e.g. can't have mismatching brackets in strings inside the module code.

    A trick here is to use the _ENV table that holds all available globals and functions. Normally Lua has a _G table, but here we have a more limited table _ENV. However, it is still useful for calling functions or accessing variables given their names. The closest to meta-programming we have in the HC2 environment..

    Share this post


    Link to post
    Share on other sites
    • 0

    What do you mean? __fibarosceneid only defined in scenes, so this only works in scenes 

    Share this post


    Link to post
    Share on other sites
    • 0

    sorry, I am still talking about cost and how owner HC2 developers trying to find workarounds for require

    Don't mind me. I was just leaving.

    Share this post


    Link to post
    Share on other sites
    • -1
    11 minutes ago, jgab said:

    the regexp parsing of module is a bit too simplistic. e.g. can't have mismatching brackets in strings inside the module code.

    A trick here is to use the _ENV table that holds all available globals and functions. Normally Lua has a _G table, but here we have a more limited table _ENV. However, it is still useful for calling functions or accessing variables given their names. The closest to meta-programming we have in the HC2 environment..

     

    @jgab it's really cool but

     

    Please login or register to see this code.

    :(

    Share this post


    Link to post
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

    ×