133 lines
4.5 KiB
Python
133 lines
4.5 KiB
Python
import time
|
|
import datetime
|
|
|
|
|
|
class Wrapper():
|
|
"""Wrapper skeleton for the modules (bot, clock dashboard...)"""
|
|
|
|
def __init__(self, own_module, *other_modules):
|
|
self.own = own_module
|
|
self.others = other_modules
|
|
print("Starting " + self.__class__.__name__ + " functionality")
|
|
|
|
|
|
|
|
def mainloop(self, sleep_delta, action):
|
|
"""sleep_delta in seconds sets the sleep period of the loop
|
|
action is a function that is performed every * seconds"""
|
|
print("Launching " + self.__class__.__name__ + " mainloop")
|
|
while True:
|
|
action()
|
|
time.sleep(sleep_delta)
|
|
self.own.persistence.sync()
|
|
|
|
|
|
|
|
|
|
class ClockWrapper(Wrapper):
|
|
"""Wrapper for the CLOCK-functionality"""
|
|
def __init__(self, own_module, *other_modules):
|
|
""""""
|
|
super().__init__(own_module, *other_modules)
|
|
self.weather = {"weather":"", "high":"", "low":"", "show":"temps"}
|
|
self.mainloop(15)
|
|
|
|
|
|
|
|
def mainloop(self, sleep_delta):
|
|
"""Runs the showing of the clock-face periodically (better way?)"""
|
|
|
|
self.prev_time = 0
|
|
self.prev_weather_time = datetime.datetime.fromtimestamp(0)
|
|
|
|
def perform_loop():
|
|
if self.prev_time != datetime.datetime.now().strftime("%H:%M"):
|
|
d = datetime.datetime.now() - self.prev_weather_time
|
|
mins_elapsed = int(d.total_seconds()/60)
|
|
|
|
if mins_elapsed >= 3*60:
|
|
# fetch new weather every 3 hours (hard coded)
|
|
self.prev_weather_time = datetime.datetime.now()
|
|
weather = self.others[0].bot_show_weather("zurich")
|
|
if not (":sad:" in weather):
|
|
l1 = weather[weather.find("</b>")+5:weather.find("\n")].replace (":","")
|
|
# current weather situation (icon): we pick the first line, remove the start string, remove :: indicating an emoji
|
|
|
|
temps_today = weather.splitlines()[4]
|
|
low = temps_today[temps_today.find("button")+8:temps_today.find("°")]
|
|
temps_today = temps_today[temps_today.find("°") + 1:]
|
|
high = temps_today[temps_today.find("button")+8:temps_today.find("°")]
|
|
self.weather["weather"] = l1
|
|
self.weather["high"] = high
|
|
self.weather["low"] = low
|
|
else:
|
|
self.weather["weather"] = "error"
|
|
self.weather["high"] = "error"
|
|
self.weather["low"] = "error"
|
|
|
|
if mins_elapsed % 5 == 0:
|
|
if self.weather["show"] == "weather":
|
|
next = "temps"
|
|
else:
|
|
next = "weather"
|
|
self.weather["show"] = next
|
|
|
|
self.prev_time = datetime.datetime.now().strftime("%H:%M")
|
|
|
|
self.own.set_face(self.weather)
|
|
|
|
super().mainloop(sleep_delta,perform_loop)
|
|
|
|
|
|
|
|
class BotWrapper(Wrapper):
|
|
"""Wrapper for the BOT-functionality"""
|
|
def __init__(self, own_module, *other_modules):
|
|
""""""
|
|
super().__init__(own_module, *other_modules)
|
|
|
|
self.bot = own_module
|
|
self.clock = other_modules[0]
|
|
|
|
self.mainloop(10)
|
|
|
|
|
|
def mainloop(self, sleep_delta):
|
|
"""Calls the telegram entity regularly to check for activity"""
|
|
def perform_loop():
|
|
self.bot.react_chats()
|
|
# num = self.bot.telegram.fetch_updates()
|
|
# for message in range(num):
|
|
# command, params = self.bot.react_command() # returns None if handled internally
|
|
# if command != None:
|
|
# self.clock.external_action(command, params)
|
|
super().mainloop(sleep_delta, perform_loop)
|
|
|
|
|
|
|
|
|
|
from threading import Thread
|
|
|
|
class DashBoardWrapper(Wrapper):
|
|
def __init__(self, own_module, *other_modules):
|
|
"""Wrapper for the dashboard functionality"""
|
|
super().__init__(own_module, other_modules)
|
|
# self.mainloop(2 * 3600) # 2 hours refresh-cycle
|
|
|
|
|
|
def mainloop(self, sleep_delta):
|
|
def perform_loop():
|
|
self.set_weather()
|
|
self.set_shopping_list()
|
|
self.set_bot_logs()
|
|
self.set_joke()
|
|
self.bot.refresh()
|
|
|
|
super().mainloop(sleep_delta, perform_loop)
|
|
|
|
|
|
def set_weather(self):
|
|
weather = self.bot.bot_show_weather("zurich")
|
|
...
|
|
self.own.set_weather(weather)
|