AMI-system / ami_setup

AMI configuration files
MIT License
1 stars 2 forks source link

Overlapping sunrise and sunset schedules #1

Closed gls21 closed 1 year ago

gls21 commented 1 year ago

1st job will end when 2nd job begins – make this clear to user We don’t want to have 2 cronjobs for certain hours

gls21 commented 1 year ago

After the sunrise and sunset start and end times are calculated in the determine_times_birdpi.py script, I've added a check that see whether there is any overlap between the sunrise and sunset schedules.

Couldn't just do it based on times e.g. 19:00-00:30 and 23:30-05:00 are overlapping and 23:30 is bigger than 00:30 so this case would worked if you just checked whether 23:30 was bigger than 00:30. Is there overlap? - yes. Is sunrise_start (23:30) > sunset_end (00:30)? - yes. BUT if it was 19:00-00:30 and 00:15-05:00, then these schedules still overlap but 00:15 isn't bigger than 00:30 so wouldn't catch this by testing is 00:15 was bigger than 00:30. Is there overlap? - yes. Is sunrise_start (00:15) > sunset_end (00:30)? - no.

SO have to consider the day as well as the time. This got fiddly because when you get sunset time e.g. 19:25 on the 13th, your schedule can become something like 18:25 on the 13th to 00:25 on the 14th. Then if sunrise is 6:36 on the 13th, your schedule can become something like 23:36 on the 12th to 7:36 on the 13th. And so these schedules (18:25 13th - 00:25 14th and 23:36 12th - 7:36 on 13th) don't overlap. They only overlap if you make sure that the sunrise schedule happens the day after the sunset schedule. So you have to add a day to the sunrise schedule. So now the resulting schedules (18:25 13th - 00:25 14th and 23:36 13th - 7:36 on 14th) do overlap.

The above just considers when the schedules overlap in the night. But they can also overlap in the day. Adding a day to the sunrise schedule wasn't necessary to check whether the schedules overlap in the day because in this case they will happen on the same day. E.g. 17:25 13th - 20:25 13th and 6:36 13th - 18:36 13th do overlap.

If either of the checks found that the schedules did overlap, then the end time of the 1st schedule is cut to 1 minute before the start of the second schedule.

Tested the code with no overlap, overlap in the night, overlap in the day, and overlap in both day and night. Working as expected.

Not sure how we make it clear to user? As can't comment in .json config file.