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

[Question] VD run loop in parallel with main loop


Question

Posted

Greetings!

I try to build a VD that I like to rotate the label value from a global Variable (Table) while the other part of code on main scene is executed.

I tried to build a function with a while..do on a button but it seems that stuck on the loop.

Is there any way to run a parallel loop ? Any help please?

5 answers to this question

Recommended Posts

  • 0
Posted

As I know the LUA does not provide parallel programming...

 

But... there is a way. But if you create it in main loop than every time the main loop runs the new parallel loop will be created... Or you need some parameter that tells if there is already a loop running. 

I recommend to do this in a scene, not in VD.

And the code is:

 

Please login or register to see this code.

Actually it creates a new parallel process every time you run function with the setTimeout().

I believe there is a better way to achieve this, but this is the first one I managed to think of. 

 

You need to create condition to set "needNextRun" and "needToCreateLoop" variables... be careful, because you can make it create lots of processes that will kill your CPU. You have to find a way to monitor a number of running loops of this kind, and kill the unnecessary ones.

 

If anything goes wrong, be ready to restart HC2 and then quickly remove the wrong code.

 

I've got such a loop in my code, but in scene with restriction to run only one instance.

  • 0
  • Inquirer
  • Posted

    Thanks @pogo for answer.

    I am aware of setTimeout on scenes. AFAIK setTimeout do not work on VDs.

    Also I try to build a solid VD that does not have any dependencies from scenes, so an extra scene is not included.  

     

    the only idea till now is to use 2 buttons that will trigger each other and update the label, but it looks overkill just for a simple VD.

     

    • 0
    Posted

    @FSE VD working within Fibaro main thread.

     

    init();

    while true do

     runUserVDBody()';  // you live here

     sleep(5000);

    end

     

    • 0
  • Inquirer
  • Posted (edited)
    8 hours ago, 10der said:

    @FSE VD working within Fibaro main thread.

     

    init();

    while true do

     runUserVDBody()';  // you live here

     sleep(5000);

    end

     

    Thank you @10der for answer. It is working very nice on main loop but when I place it inside a button, it stuck on loop, even I press button again. I like to go on init again when button is pressed. 

    I guess a global variable if is true will do the while loop else will go to init

    Edited by FSE
    • 0
  • Inquirer
  • Posted (edited)

    Please login or register to see this attachment.

     build a very basic VD that it do not use main loop to run a loop. Instead loop line in button rotation. the magic part is that you can control if you like to run the loop by a second button.

    For testing you can change uId number with a valid user id. it change the variable "atHome" of user so take care if you use this API call to another scene/VD.

    if you like to run loop on a button without any control, it more simple as it is not required the check of a variable on while loop. So in this case you may have as much loops as your buttons! Take care that it may increase load on your HC2.

     

    Please login or register to see this attachment.

    Edited by FSE

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