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


HC3 QuickApps coding - tips and tricks


jgab

Recommended Posts

  • Topic Author
  • 1 minute ago, cag014 said:

    But all that still doesn't explain why takes 5 seconds to execute API or fibaro.call inside QucikApp.

     

    Well, it depends on how they implemented to "cooperation" between incoming calls/events to an QuickApp. I believe that they hang waiting for the receiving QA the ack the call, but because the receiving part is the same QA that is busy waiting for the ack it hangs - and they have some timeout mechanism that saves them? I can only guess - but it's atypical source of issues when you accidentally call yourself...

     

    Link to comment
    Share on other sites

    1 minute ago, jgab said:

    Well, it depends on how they implemented to "cooperation" between incoming calls/events to an QuickApp. I believe that they hang waiting for the receiving QA the ack the call, but because the receiving part is the same QA that is busy waiting for the ack it hangs - and they have some timeout mechanism that saves them? I can only guess - but it's atypical source of issues when you accidentally call yourself...

    We seem to think the same... Calling a "function" in Lua never leaves the interpreter, but doing "api call" goes through a http client, a http server and talks to that your same process but via a long winded road... And api call is blocking.

    Link to comment
    Share on other sites

    What's this event?

    Unhandled event:{"type":"RoomModifiedEvent","data":{"id":219}} -- please report

    This occurs each room.

    room 1 to the end.

    Link to comment
    Share on other sites

  • Topic Author
  • 9 minutes ago, petergebruers said:

    A bit off topic but I think we've all been on this planet for a while and I guess some of our ways of thinking are "learned" while some simply reflect how the human brain works.

     

    When I try to explain issues to my wife, I often tell her we are used to "following recipes", like preparing "béchamel sauce".

     

    Ever tried to write a recipe in "recursive style"? Or "functional style"? Maybe there is some "callback style" in recipes eg  "after 10 minutes, take the pan and stir" =: setTimout with callback :D

     

    Please login or register to see this link.

     

    No matter how hard I try (and I've written small programs in Erlang so I did try very hard :D ) to get fluent in different paradigms - I always go back to "imperative". For multitasking, I am inclined to believe "Communicating sequential processes" is a viable option, like used in Erlang and Go... but I am not a professional programmer so no experience with practical implications at all.

     

    I saw that and I would recommend other users to have a look at it. It indeed captures part of what I said...

     

    Yeah, and sometimes recursion works, but if you are not careful it really blows up your stack because it does not really convert to a loop - instead it really causes each function call to nest another... and another... and... Until the interpreter shuts down your process.

     

    Thank you for confirming that.

     

    Personally, I think it is good thing, that QuickApp:onInit() blocks every other part of the FQA. Because you would probably set things up before everything else runs.

     

    Suppose you use "X" in a button, and by some silly coincidence the user clicks when you are halfway through your QuickApp:onInit() and X has not been set up yet?

     

    Indeed it does not, but it reminds me of the 10 second "hangs" we sometimes have on HC2 when a scene gets killed.

     

    I have no access to the "guts" of a HC3 so this is big speculation, but maybe and "api call to yourself" causes the HC3 to reload/redo something, cause a time-out and a retry "behind the scenes"?

     

    Maybe it is possible to discuss that with Fibaro (through their beta bug tracker I mean)... So "api call" to another device (any typ) is OK but "api call" to yourself (your FQA) = 5 second delay.

     

    ...and I do love recursions :-)

    setTimeout & friends are nice as it is almost like recursion but not - no stack that grows - and you have to carry your own stack if you want to collect some result.

    However, Lua is tail-recursive so it won't eat stack if the recursive call is the last in a execution branch in the function. (and that goes for A calling B calling A etc) - so it's usually easy to avoid stack blow-ups.

     

    5 minutes ago, rangee said:

    What's this event?

    Unhandled event:{"type":"RoomModifiedEvent","data":{"id":219}} -- please report

    This occurs each room.

    room 1 to the end.

    Yes, it's when you modify rooms - you are running ER4 or some of the example code I posted?

    So, you can ignore it - I will add it to the next release so it won't be so noisy about it.

    It was a way for me to be aware of new types of events... I haven't seen them all yet ;-) 

    Thanks for reporting anyway.

    Edited by jgab
    Link to comment
    Share on other sites

    17 minutes ago, petergebruers said:

    Maybe it is possible to discuss that with Fibaro (through their beta bug tracker I mean)... So "api call" to another device (any typ) is OK but "api call" to yourself (your FQA) = 5 second delay.

    Have reported about that ages ago.... no response

     

    Link to comment
    Share on other sites

    1 hour ago, jgab said:

    Yes, but you do that loop with setTimeout or setInterval.

     

    please be carefull with setInterval, set a variable with the timer id and use clearTimeout function to kill it ;)

     

    3 hours ago, cag014 said:

    Is anyone has knowledge how to press button inside QuickApp (beside using API, which takes 5 seconds)?

     

    I asked the question, here is the response of the SW team: 

    Quote

    It’s taking so long because there is 5 s timeout on communication with QuickApp process. QuickApp process is unable to handle this because it’s still processing fibaro.call.

     

    Link to comment
    Share on other sites

    @Krikroff thanks, so this answer settles it and confirms our suspicions "t’s taking so long because there is 5 s timeout on communication with QuickApp process. QuickApp process is unable to handle this because it’s still processing fibaro.call". So this style of programming is a no-go because you are essentially deadlocking yourself. The whole FQA has to be treated as "cooperative multitasking".

    Link to comment
    Share on other sites

  • Topic Author
  • 6 minutes ago, Krikroff said:

     

    please be carefull with setInterval, set a variable with the timer id and use clearTimeout function to kill it ;)

    What's wrong with clearInterval(<ref>) ?

    ...anyway, all my setInterval's run forever.... :-)

     

    Quote

    I asked the question, here is the response of the SW team: 

    Yes, and they can solve it in two ways. Either discovering that they call themselves and short-cut it (trivial inside fibaro.call), or implement some kind of message-box to use as a synchronisation mechanism behind the scene.

    Edited by jgab
    Link to comment
    Share on other sites

    5 minutes ago, Krikroff said:

    I asked the question, here is the response of the SW team: 

    Quote

    It’s taking so long because there is 5 s timeout on communication with QuickApp process. QuickApp process is unable to handle this because it’s still processing fi

    Thanks.   (So in QuickApp fibaro.call commands are useless.)

    Link to comment
    Share on other sites

    1 minute ago, cag014 said:

    Thanks.   (So in QuickApp fibaro.call commands are useless.)

    Not useless in general, it is safe to use them if the target is another device (or room or whatever api you want to call). It is not safe to use them "on yourself"

    Link to comment
    Share on other sites

    7 minutes ago, jgab said:

    Yes, it's when you modify rooms - you are running ER4 or some of the example code I posted?

    So, you can ignore it - I will add it to the next release so it won't be so noisy about it.

    It was a way for me to be aware of new types of events... I haven't seen them all yet ;-) 

    Thanks for reporting anyway.

     

    I'm only using setUpUtilities function.

     

    I found out when RoomModifiedEvent happen.

    This happnes when value of the room temperature sensor changed.

    And this affect to all room.(bcz I added only one temperature QD.)

    Link to comment
    Share on other sites

  • Topic Author
  • 2 minutes ago, cag014 said:

    Thanks.   (So in QuickApp fibaro.call commands are useless.)

    Only to inside the QuickApp to itself. Between QuickApp they are pretty fast.

     

    1 minute ago, petergebruers said:

    Not useless in general, it is safe to use them if the target is another device (or room or whatever api you want to call). It is not safe to use them "on yourself"

     

    Between QuickApps they are pretty fast

     

    Link to comment
    Share on other sites

    5 minutes ago, cag014 said:

    Thanks.   (So in QuickApp fibaro.call commands are useless.)

     

    Using fibar.call should be used only to call actions from different devices or scenes and never to call actions on themselves.

    Link to comment
    Share on other sites

  • Topic Author
  • 1 minute ago, rangee said:

     

    I'm only using setUpUtilities function.

     

    I found out when RoomModifiedEvent happen.

    This happnes when value of the room temperature sensor changed.

    And this affect to all room.(bcz I added only one temperature QD.)

    Thanks, I didn't think of that but now rooms can have associated temperature sensors - So when the temperature changes you get a RoomModified event? Funny :-)

     

    Link to comment
    Share on other sites

    6 minutes ago, jgab said:

    Between QuickApps they are pretty fast

    When I read your original post I rounded that to 15 calls per second but I had 2 questions, how much CPU does it use and does it scale. There are 4 cores so possibly running that test from/to multiple FQA gets even better numbers.

    Link to comment
    Share on other sites

    8 minutes ago, jgab said:

    Thanks, I didn't think of that but now rooms can have associated temperature sensors - So when the temperature changes you get a RoomModified event? Funny :-)

     

     

    Yes. Mayby humidity sensor and light also affect to RoomModified. (maybe..)

    Because there are 3 options when editing room.

    Main temperature sensor, Main humidity sensor, Main light sensor.
    Link to comment
    Share on other sites

  • Topic Author
  • 1 minute ago, petergebruers said:

    When I read your original post I rounded that to 15 calls per second but I had 2 questions, how much CPU does it use and does it scale. There are 4 cores so possibly running that test from/to multiple FQA gets even better numbers.

    Yes, in theory. I don't know if they give us all the cores or keep some for themselves ;-) ?

    Do they "nice" the QA/scene processes?

    Link to comment
    Share on other sites

    • jgab changed the title to SDK for remote and offline HC3 development.
    1 hour ago, jgab said:

    But setTimeout & friends is not about recursion. The function calling setTimeout(f,x) exits before the function f is called.

    Not recursive in the true sense like the classical Fibonnaci example

     

    function fibonacci(n)
        if n<3 then
            return 1
        else
            return fibonacci(n-1) + fibonacci(n-2)
        end
    end
     

    But it  the sense that your timer or http handler ends by calling http or timer again.

     

    I am pretty certain you can shoot yourself in the foot with net.HTTPClient() - someone contacted me about this but I cannot find back a good example.

     

    The issue is with the lifetime of the objects you pass around.

     

    It ends with a Lue interpreter crash because it runs out of handles or memory.

     

    As a rule of thumb, I tell people they should have only one of these in there code, if they have more than one then they are wasting resources:

     

    local http = net.HTTPClient()

     

    That is probably a white lie but quite a few users seem to think you need a net.HTTPClient object per request but that is not the case, one object can handle all your requests.

     

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

  • Topic Author
  • 7 minutes ago, petergebruers said:

    I am pretty certain you can shoot yourself in the foot with net.HTTPClient() - someone contacted me about this but I cannot find back a good example.

    The issue is with the lifetime of the objects you pass around.

    It ends with a Lue interpreter crash because it runs out of handles or memory.

    As a rule of thumb, I tell people they should have only one of these in there code, if they have more than one then they are wasting resources:

    local http = net.HTTPClient()

    That is probably a white lie but quite a few users seem to think you need a net.HTTPClient object per request but that is not the case, one object can handle all your requests.

    I haven't seen that behaviour. It but instead of looping back from the call-back handler one can do a setTimeout(cont,0) to exit the call-back handler and continue in a new context - no strings attached. Which is usually what happens the way I structure my code.

    If they have a bad call-back mechanism (from C?) I guess it can cause issues.

    In fibaroapiHC3 I have a library that do http requests synchronous - so I simulate the asynchronous behaviour by calling setTimeout with s small random delay - just to make it realistic :-)

    Yes, one object usually is enough - not sure about passing options to net.HTTPClient(options). Are there any option that  can't as easily be provided in the request? 

    Link to comment
    Share on other sites

    edited -- gray wizard killed me

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