Extracted loop timing and added refresh interval option.

This commit is contained in:
Maximilian Giller 2019-03-13 20:33:20 +01:00
parent c79f0b8888
commit c9c6ce4207
3 changed files with 100 additions and 35 deletions

View file

@ -10,6 +10,7 @@ Copyright by aceisace
from datetime import datetime from datetime import datetime
from time import sleep from time import sleep
from Assets import datetime_locals from Assets import datetime_locals
from LoopTimer import LoopTimer
import locale import locale
from DebugConsole import DebugConsole from DebugConsole import DebugConsole
from settings import * from settings import *
@ -43,20 +44,16 @@ available_panels = {
"month-overview" : MonthOvPanel "month-overview" : MonthOvPanel
} }
loop_timer = LoopTimer(update_interval, run_on_hour=True)
"""Main loop starts from here""" """Main loop starts from here"""
def main (): def main ():
while True: while True:
loop_timer.begin_loop()
start_time = loop_timer.get_current()[0]
time = datetime.now() if start_time.hour in calibrate_hours and loop_timer.is_new_hour_loop():
hour = int(time.strftime("%H")) debug.print_line("Calibrating outputs")
month = int(time.now().strftime('%m'))
year = int(time.now().strftime('%Y'))
for i in range(1):
debug.print_line('_________Starting new loop___________')
debug.print_line('Date: '+ time.strftime('%a %d %b %y') + ', time: ' + time.strftime('%H:%M') + '\n')
if hour in calibrate_hours:
for output in output_adapters: for output in output_adapters:
output.calibrate() output.calibrate()
@ -77,14 +74,19 @@ def main ():
rss = RssParserPosts.RssParserPosts(rss_feeds) rss = RssParserPosts.RssParserPosts(rss_feeds)
design.add_rssfeed(rss) design.add_rssfeed(rss)
for output in output_adapters: debug.print_line("Starting to render")
for i, output in enumerate(output_adapters):
output.render(design) output.render(design)
debug.print_line(str(i + 1) + " of " + str(len(output_adapters)) + " rendered")
debug.print_line("=> Finished rendering" + "\n") debug.print_line("=> Finished rendering" + "\n")
for i in range(1): loop_timer.end_loop()
nexthour = ((60 - int(time.strftime("%M"))) * 60) - (int(time.strftime("%S"))) sleep_time = loop_timer.time_until_next()
sleep(nexthour)
debug.print_line("This loop took " + str(loop_timer.get_last_duration()) + " to execute.")
debug.print_line("Sleeping " + str(sleep_time) + " until next loop.")
sleep(sleep_time.total_seconds())
if __name__ == '__main__': if __name__ == '__main__':
main() main()

62
Calendar/LoopTimer.py Normal file
View file

@ -0,0 +1,62 @@
from datetime import datetime, timedelta
class LoopTimer(object):
"""Manages loop times and sleeps until
next loop."""
def __init__(self, loop_interval, run_on_hour=False):
self.interval = loop_interval
self.on_hour = run_on_hour
self.loop_history = []
def begin_loop(self):
begin_time = datetime.now()
print('\n_________Starting new loop___________')
print('Date: ' + begin_time.strftime('%a %d %b %y') + ', time: ' + begin_time.strftime('%H:%M') + '\n')
self.__add_beginning__(begin_time)
def __add_beginning__(self, time):
self.loop_history.append((time,))
def __add_ending__(self, time):
current = self.get_current()
self.loop_history[-1] = (current[0], time)
def end_loop(self):
end_time = datetime.now()
self.__add_ending__(end_time)
def get_current(self):
return self.loop_history[-1]
def time_until_next(self):
interval_duration = timedelta(minutes=self.interval)
loop_duration = self.get_last_duration()
sleep_time = interval_duration - loop_duration
if self.on_hour:
time_until_hour = self.get_time_to_next_hour()
if time_until_hour < sleep_time:
return time_until_hour
return sleep_time
def get_last_duration(self):
if len(self.loop_history) == 0:
return
begin, end = self.loop_history[-1]
return end - begin
def get_time_to_next_hour(self):
cur = datetime.now()
rounded = datetime(cur.year, cur.month, cur.day, cur.hour)
next_hour_time = rounded + timedelta(hours=1)
return next_hour_time - datetime.now()
def is_new_hour_loop(self):
if len(self.loop_history) < 2:
return False
previous_loop = self.loop_history[-2]
current_loop = self.get_current()
if previous_loop[0].hour != current_loop[0].hour:
return True
else:
return False

View file

@ -19,6 +19,7 @@ display_colours = "bwr"
language = "en" language = "en"
units = "metric" units = "metric"
hours = "24" hours = "24"
update_interval = "60"
"""DESIGN""" """DESIGN"""