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


Recommended Posts

Posted

1. Working on a quiet large quickapp (>1000 lines of code, HC3) I've make a stupid mistake: some functions has at his end setTimeout command. This functions are used to update interface, registering some operations and so on. At the other hand - I invoke some of the functions to update interface "on demand". As a result more and more timers was set by setTimeout, and a "timer leak" occurs... Nothing bad happened, I correct this mistake.

My question is: how many timers can be set? Is there a limit of them? And is it possible to retrieve in QA information about already "ticking" timers (how many, time remaining, code to execute etc).

 

2. As I know in general Fibaro is asynchronous. But maybe some built-in function is and other is not?...

if on example we have the following:

Please login or register to see this code.

and in the next few lines we test the value 'a'. After few millisecond, there's no guarantee that hub.getValue() answers until tested. Is such test unreliable? And to what value 'a' is initialised?

If correct how to protect from such situations - when I need to do something with received value at once?

Of course getValue is only an example. It can be also hub.call(....) and then test some property and many more.

 

3. Is there any differences between calling hub.function() and fibaro.function()? For me it working the same way...

 

4. What is the difference with scope of the variable in QA:

Please login or register to see this code.

For me the scope of v1, v2, v3 is the same. Can someone explain a practical difference between those three definitions?
Thank you in advance.

Posted (edited)
7 hours ago, Łukasz997 said:

1. Working on a quiet large quickapp (>1000 lines of code, HC3) I've make a stupid mistake: some functions has at his end setTimeout command. This functions are used to update interface, registering some operations and so on. At the other hand - I invoke some of the functions to update interface "on demand". As a result more and more timers was set by setTimeout, and a "timer leak" occurs... Nothing bad happened, I correct this mistake.

My question is: how many timers can be set? Is there a limit of them? And is it possible to retrieve in QA information about already "ticking" timers (how many, time remaining, code to execute etc).

In this post I started a million setTimeout - it took 41s for the last the finish.

There is no standard way to see how many timers you have. You can redefine setTmeout and clearTimeout to keep track of timers.

Ex.

Please login or register to see this code.

 

 

 

7 hours ago, Łukasz997 said:

 

2. As I know in general Fibaro is asynchronous. But maybe some built-in function is and other is not?...

if on example we have the following:

Please login or register to see this code.

and in the next few lines we test the value 'a'. After few millisecond, there's no guarantee that hub.getValue() answers until tested. Is such test unreliable? And to what value 'a' is initialised?

getValue is synhronous.

 

7 hours ago, Łukasz997 said:

If correct how to protect from such situations - when I need to do something with received value at once?

Of course getValue is only an example. It can be also hub.call(....) and then test some property and many more.

In general only setTimeout, setInterval and the various callbacks from net.* functions are asynchronous.

When you press a button and your QuickApp:button() function is called or when someone call your QA with fibaro.call(YourQAid,...) it is an "asynchronous" callback into your code/QA.

We can argue then if fibaro.call is asynchronous as it doesn't wait for the call to complete before continuing (except sometimes checking if it is a valid call) - but that is part of a bigger discussion (ex.

Please login or register to see this link.

)

I'm saying "asynchronous" as the Lua model is cooperative multitasking (

Please login or register to see this link.

).

 

 

7 hours ago, Łukasz997 said:

3. Is there any differences between calling hub.function() and fibaro.function()? For me it working the same way...

No, 

Please login or register to see this code.

 

 

7 hours ago, Łukasz997 said:

4. What is the difference with scope of the variable in QA:

Please login or register to see this code.

For me the scope of v1, v2, v3 is the same. Can someone explain a practical difference between those three definitions?
Thank you in advance.

As the example stands, local v1 is declared as a local variable in the QA fil, on top-level.

self.v2 gives and error, as self is not defined on top-level. 'self' is only defined inside metods (object:method(...))

self.v3 creates a new key-value pair (v3 = 3) in the QuickApp object. You can think about self as a Lua table where you can lookup keys where some are bound to values and other to methods...

It's much faster to access a local variable (v1) than it is to access a key in a Lua table (v3)

Edited by jgab
Protected timer fun with pcall...
  • Like 1
  • Thanks 1
  • Topic Author
  • Posted

    Well, I appreciate your explanation, I've learned some again.

    *

    One cogitation: the time someone spent doing something for you, you can refund with money. But engagement, availability and will to share of knowledge cannot be simply paid - a gratitude for all this you do for us, Jan.

  • Topic Author
  • Posted
    W dniu 17.04.2024 o 10:55, jgab napisał:

    You can think about self as a Lua table where you can lookup keys where some are bound to values and other to methods...

    It's much faster to access a local variable (v1) than it is to access a key in a Lua table (v3)

    Inspire by above I made small test:

    Please login or register to see this code.

    And the results (on the HC3):
     

    Please login or register to see this code.

    14x slower. Worth remembered. Tested the same with VSC and emulator on 9 years old mac gives result of ~15 sec, similar for both type of variables.

    • Like 1

    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...