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 time import sleep
from Assets import datetime_locals
from LoopTimer import LoopTimer
import locale
from DebugConsole import DebugConsole
from settings import *
@ -43,48 +44,49 @@ available_panels = {
"month-overview" : MonthOvPanel
}
loop_timer = LoopTimer(update_interval, run_on_hour=True)
"""Main loop starts from here"""
def main ():
while True:
loop_timer.begin_loop()
start_time = loop_timer.get_current()[0]
time = datetime.now()
hour = int(time.strftime("%H"))
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:
output.calibrate()
if choosen_design in available_panels.keys():
design = available_panels[choosen_design]((epd.width, epd.height))
else:
raise ImportError("choosen_design must be valid (" + choosen_design + ")")
debug.print_line("Fetching weather information from open weather map")
owm = OwmForecasts.OwmForecasts(location, api_key, paid_api=owm_paid_subscription)
design.add_weather(owm)
debug.print_line('Fetching events from your calendar')
events_cal = IcalEvents.IcalEvents(ical_urls, highlighted_ical_urls)
design.add_calendar(events_cal)
debug.print_line('Fetching posts from your rss-feeds')
rss = RssParserPosts.RssParserPosts(rss_feeds)
design.add_rssfeed(rss)
if start_time.hour in calibrate_hours and loop_timer.is_new_hour_loop():
debug.print_line("Calibrating outputs")
for output in output_adapters:
output.render(design)
output.calibrate()
debug.print_line("=> Finished rendering" + "\n")
if choosen_design in available_panels.keys():
design = available_panels[choosen_design]((epd.width, epd.height))
else:
raise ImportError("choosen_design must be valid (" + choosen_design + ")")
for i in range(1):
nexthour = ((60 - int(time.strftime("%M"))) * 60) - (int(time.strftime("%S")))
sleep(nexthour)
debug.print_line("Fetching weather information from open weather map")
owm = OwmForecasts.OwmForecasts(location, api_key, paid_api=owm_paid_subscription)
design.add_weather(owm)
debug.print_line('Fetching events from your calendar')
events_cal = IcalEvents.IcalEvents(ical_urls, highlighted_ical_urls)
design.add_calendar(events_cal)
debug.print_line('Fetching posts from your rss-feeds')
rss = RssParserPosts.RssParserPosts(rss_feeds)
design.add_rssfeed(rss)
debug.print_line("Starting to render")
for i, output in enumerate(output_adapters):
output.render(design)
debug.print_line(str(i + 1) + " of " + str(len(output_adapters)) + " rendered")
debug.print_line("=> Finished rendering" + "\n")
loop_timer.end_loop()
sleep_time = loop_timer.time_until_next()
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__':
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"
units = "metric"
hours = "24"
update_interval = "60"
"""DESIGN"""