mudpi / mudpi-core

Configurable automation library for linux SBC boards including raspberry pi
https://mudpi.app
275 stars 40 forks source link

Improve time scheduling #20

Closed SecT0uch closed 3 years ago

SecT0uch commented 3 years ago

Hey,

I'm thinking about a way to improve the Time Trigger scheduling.

My initial concern was to be able to detect if we missed a schedule and correct the relay position. Exemple:

I know this is possible use pycron's has_been() function.

Shouldn't we change the Time Trigger declaration or create a new trigger type daily_schedule? It would take 2 options, let's say schedule_on and schedule_off.

In the init we would add something like:

from datetime import date
from datetime import datetime

today = date.today()
midnight = datetime.combine(today, datetime.min.time())

if pycron.has_been(schedule_on, midnight) and not pycron.has_been(schedule_off, midnight):   # if in schedule
   <Turn the relay On>

Do you have any better idea on how to implement this?

I can contribute to implement that if needed.

SecT0uch commented 3 years ago

What if I set 2 schedules linked to 2 events as such:

Would it be a problem to trigger the same event every minute ? I see there are checks in turn_on() and turn_off() functions, but what about Redis and logger spamming ?

To avoid logger spamming, we could change this LOG_LEVEL to debug and add a info logs to turn_on() and turn_off() (after this check) I can PR for that :)

BTW, is the Redis db fully in memory or does it also write on the disk ? (Limiting I/O to preserve SD cards)

olixr commented 3 years ago

These are wonderful suggestions. Check out the new v0.10.0 that was in the works. https://github.com/mudpi/mudpi-core/commit/809f998aeec62af7ddef827326065177e5bf7c50

I think some of the issue you had with triggers will be resolved. However I do have plans for more trigger options following the update. One of them is very similar to the suggestion you make. Let me know if the new changes get the behavor you are looking for. If not perhaps the triggers I have planned are what you are thinking of then. I want to provide the option to use cron strings and also just time based (timers) to give a variety of choices.

Let me know what you think of the updates and if this resolves the issues.

SecT0uch commented 3 years ago

My board is currently used in production, but can wait to put my hands on the new version. I will let you know as soon as I'm able to test :)