jackstone Posted November 28, 2020 Share Posted November 28, 2020 This question is aimed at C4 driver developers. I know there is not really mutual help between us but I'm throwing the question anyway... I'm looking for a simple way to schedule stuff within a Lua script, without using C4 scheduling agent and without using the C4 API that only add scheduled event but can't add task to run.. I know there is a way, some of you guys have done it, maybe someone will have the kindness to share how he have done it... Or maybe C4 staff on this forum can help on that? TIA Quote Link to comment Share on other sites More sharing options...
alanchow Posted November 28, 2020 Share Posted November 28, 2020 17 hours ago, jackstone said: This question is aimed at C4 driver developers. I know there is not really mutual help between us but I'm throwing the question anyway... I'm looking for a simple way to schedule stuff within a Lua script, without using C4 scheduling agent and without using the C4 API that only add scheduled event but can't add task to run.. I know there is a way, some of you guys have done it, maybe someone will have the kindness to share how he have done it... Or maybe C4 staff on this forum can help on that? TIA Method 1 - Utilise os.time() and a initial timer set up a reoccurring timer to the period you want (this lines it up to the exact second. You will need to do a bit of math eg every minute/five/half hour/hour) and utilise a table of a schedules (time stamps) to determine when you want to fire a schedule) Method 2 - similar to above but rather than using timestamps utilise os.date to figure out the day of the week, month, year if you want to do reoccurring based on any of those factors. Hope that helps. Quote Link to comment Share on other sites More sharing options...
jackstone Posted November 29, 2020 Author Share Posted November 29, 2020 Thanks Alan, the method I've figured out is to do a recurring timer that read os.time in epoch format, than do some math to see if the current time match a schedule each time the looping timer expire. With some tolerance since the time input comes from an interval and not real time. I'm not really sure if that is close to what you've explained? Quote Link to comment Share on other sites More sharing options...
alanchow Posted November 29, 2020 Share Posted November 29, 2020 17 hours ago, jackstone said: Thanks Alan, the method I've figured out is to do a recurring timer that read os.time in epoch format, than do some math to see if the current time match a schedule each time the looping timer expire. With some tolerance since the time input comes from an interval and not real time. I'm not really sure if that is close to what you've explained? Yep similar concept. My method just aligns it to the minute rather than randomly whenever the timer starts. Quote Link to comment Share on other sites More sharing options...
RyanE Posted November 29, 2020 Share Posted November 29, 2020 Just be aware that you should of course do your time / date polling as infrequently as possible. In other words, don't be stupid and set a 1 second (or worse, sub-second) timer to check os.time or os.date repeatedly... RyanE Quote Link to comment Share on other sites More sharing options...
jackstone Posted November 29, 2020 Author Share Posted November 29, 2020 3 hours ago, alanchow said: Yep similar concept. My method just aligns it to the minute rather than randomly whenever the timer starts. I see... so you're pulling os.time only once and then trust your recurring timer to keep track of time? Quote Link to comment Share on other sites More sharing options...
Neo1738 Posted November 30, 2020 Share Posted November 30, 2020 4 hours ago, jackstone said: I see... so you're pulling os.time only once and then trust your recurring timer to keep track of time? I think he means don't bog down the cpu by constantly polling for times set it every hr or so to verify time. Quote Link to comment Share on other sites More sharing options...
jackstone Posted November 30, 2020 Author Share Posted November 30, 2020 5 minutes ago, Neo1738 said: I think he means don't bog down the cpu by constantly polling for times set it every hr or so to verify time. Yeah I understand that part. Quote Link to comment Share on other sites More sharing options...
jackstone Posted November 30, 2020 Author Share Posted November 30, 2020 I think I got it, at least for my case. I only have 8 daily tasks to schedule within the driver, so instead of polling os.time periodically, I just have to get the current time once a day, then start 8 timers each 24h interval, each timer will be set to the number of seconds before triggering based on the actual epoch time. 8 timers should be more efficient than a recurring one plus a loop of IF-THEN to check if an event is set to trigger... Quote Link to comment Share on other sites More sharing options...
alanchow Posted November 30, 2020 Share Posted November 30, 2020 6 minutes ago, jackstone said: I think I got it, at least for my case. I only have 8 daily tasks to schedule within the driver, so instead of polling os.time periodically, I just have to get the current time once a day, then start 8 timers each 24h interval, each timer will be set to the number of seconds before triggering based on the actual epoch time. 8 timers should be more efficient than a recurring one plus a loop of IF-THEN to check if an event is set to trigger... If it’s 8 timers only you should do the math to align the timer up to whenever you have set it once off first off. After that 24 hour repeating timers will do. Quote Link to comment Share on other sites More sharing options...
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.