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


  • 0

please help understanding lua-function-process-order-clash


h.fuckar@aon.at

Question

Hi

I have a problem to understand why my lua-code not working as i expected.

See first this (properly working) test-lua-scene and the debug-result:

Please login or register to see this code.

Please login or register to see this attachment.

As expected first was the function "funcDevices" performed.

Second the function "funcSections".

And then at last the function "funcRooms".

 

And now for my scene, which does not work as expected (code and result):

Please login or register to see this code.

Please login or register to see this attachment.

See the order of statements in the program. I would expected, first the function "funcDevices" was performed. Then, the second function "funcRooms" is performed. And last the function "funcSections".

[DEBUG] 12:20:50: before call function funcDevices--------------------

In principle, the program is working properly. But the wrong order  in run the functions de facto can not be explained to me:

 

Order of call's: D R S = direction de facto: S R D

Order of call's: R S D = direction de facto: S R D

Order of call's: R D S = direction de facto: R S D

Order of call's: S R D = direction de facto: S R D

 

Here's someone who can tell me why my desired order is not complied with by the Fibaro-LUA-system?
What should I do to make it work the way I want it?

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

I think it may be due to multithreading. I do not know if you can influence the process in lua or set semaphores. Try to put a sleep or wait between the function calls to allow the threads to run out before calling the next function.  

Link to comment
Share on other sites

  • 0
  • Inquirer
  • Thanks for your answer!

    Yes, multithreading would be a possibility.

    Pause between the calls I've tried. While it changed the order but it's still an unsafe solution. Because it probably also depends on the amount of data as long as a thread is running.

    I have now solved the problem as follows. The finish is only called when all threads are processed (DRSdone == 111):

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0

    It is only one command that is causing this:

    hhtp:request

    It uses a callback. So it will launch a http request, and when that's finished, it will call the code after success. You don't know when that is...

    There is only one other command, if I'm not mistaken, that has this kind of effect, it's setTimeout.

    Can I recommend my own thread?

    Please login or register to see this link.

    It will be more easy to understand the "callback" nature because the examples are really simple.

    I couldn't resist reducing your script to the essence, so it was easier for me to understand and maybe also a bit clearer to other users:

     

    Please login or register to see this code.

    If you run it a few times, you get different (as expected) results. But... why is google always last, although that's the first I ask (call)?

    Please login or register to see this image.

    /uploads/emoticons/default_icon_smile.gif" />

     

    EDIT: google is last because the call to the external site takes longer to handle than the call to internal web server (handling the API requests).

    Edited by petergebruers
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Hallo petergebruers you are welcome!

    Thanks, I'll try to understand.

    I will put my tonight under the pillow. Maybe it will help me.

    I am with my solution but also quite happy, as long as it is running correctly

    Please login or register to see this image.

    /emoticons/default_icon_wink.gif" alt=";-)" />

    Harald

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Hi, now i have read the article of @petergebruers and it was very interesting.

    I conclude that it is better to use "setTimeout" because it uses less resources than "fibaro:sleep"

    But with my problem so both will not help.

    My problem was that I can not know the order in which the three functions are processed.

    The problem I have solved by my program. The result is however characterized an unnecessary overhead on program-lines.

    But why it should be good, that the sequence is so different that I still do not understand.

    The three routines are obviously already processed not parallel but in series.

    And here operate with waiting times, is an uncertain Thing.

    Since it does matter the amount of data that are processed in each Routine.

    Waiting time may be too short but here too Long.

    But also stupid for too long, because processing-time is so precious.

     

    So, this is what an old COBOL-Programmer is thinking

    Please login or register to see this image.

    /emoticons/default_icon_wink.gif" alt=";-)" />

    Good night to all!

    Link to comment
    Share on other sites

    • 0

    You say "processed not parallel but in series.". That is only partially true. Yes, the http requests are SCHEDULED in series. The http request statement itself is not waiting for the response! Your program continues to run, to schedule the other requests. The functions after "success =" only run when the response has arrived. In my demo program, I am asking 4 URL's *nearly at the same time*. Due to multitasking, the responses to the internal server are not in a fixed order. So why is google always last? Because the other three connect to the internal web server of the HC2 and its response is very fast, and the external google web server is always slower.

    You asked to fix your code, so here it is. Have a look at the few lines I added (lines with "ADDED" or CHANGED"). It runs in fixed order.

     

    Please login or register to see this code.

    • Like 1
    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
    Answer this question...

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