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
Question
afendikov 35
Hi all,
Want to share - it may be useful for your HC3 to balance CPU load.
I have several QA working every minute ( power control, data export, etc.. ) and I started to give every QA their own time space during "one minute" time.
when QA starts, it wait till right second, which I define in secondToExecute - before start.
for example - export to Influx start on 30th second within one minute.
and main loop also calculate how much seconds taken by all routines and correct "sleep" time to run again on 30th second
As I know, Fibaro & LUA can't provide milliseconds timing, so - it can be some un-sync in time, because milliseconds give some extra timing.
But this is not dramatical situation - solution can be - restart HC3 every 1 week to restart all QA's and sync them again to their timings
Code example below:
function QuickApp:loop(refresh)
local timeNow = os.time()
-- any code to run
loopSeconds = os.time() - timeNow
self:trace('loop seconds = '..loopSeconds)
fibaro.setTimeout((refresh-loopSeconds) * 1000, function()
self:loop(refresh)
end)
end
function QuickApp:syncTime(timeout)
local secondToExecute = 30
self:trace ("now is "..os.date("%H:%M:%S").." | trying to sync to "..secondToExecute.." sec" )
timer = secondToExecute - ( os.time() - 1591477200 ) % 60
if timer == 0 then
self:loop(timeout)
elseif timer < 0 then
timer = 60 + timer
end
self:trace ("need to sleep "..timer.." sec for sync" )
fibaro.setTimeout(timer * 1000, function()
self:loop(timeout)
end)
end
function QuickApp:onInit()
self:debug("Starting... QuickApp Init")
self.loop1timeout = 60
self:syncTime(self.loop1timeout)
end
Link to comment
Share on other sites
3 answers to this question
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.