From 069c83e796526622efa338e648c70579cc4ff8bd Mon Sep 17 00:00:00 2001 From: Remy Moll Date: Sun, 3 Jan 2021 00:33:24 +0100 Subject: [PATCH] Many fixes to the dashboard. --- bot/api/weather.py | 77 ++++++++---- bot/framework.py | 1 + bot/main.py | 19 ++- clock/api/converter.py | 14 +-- clock/api/led.py | 2 +- dashboard/main.py | 272 +++++++++++++++++++++++++---------------- launcher.py | 6 +- wrapper.py | 59 +++------ 8 files changed, 270 insertions(+), 180 deletions(-) diff --git a/bot/api/weather.py b/bot/api/weather.py index c547dce..4570177 100644 --- a/bot/api/weather.py +++ b/bot/api/weather.py @@ -2,29 +2,60 @@ import requests import bot.api.keys import datetime -def show_weather(location): - url = "https://api.openweathermap.org/data/2.5/onecall?" - data = {"lat" : location[0], "lon" : location[1], "exclude" : "minutely,hourly", "appid" : bot.api.keys.weather_api, "units" : "metric"} - today = datetime.datetime.today().weekday() - days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] +class WeatherFetch(): + def __init__(self): + self.last_fetch = datetime.datetime.fromtimestamp(0) + self.last_weather = "" - try: - weather = requests.get(url,params=data).json() - print("WEATHER API CALLED") - categories = {"Clouds": ":cloud:", "Rain": ":cloud_with_rain:", "Thunderstorm": "thunder_cloud_rain", "Drizzle": ":droplet:", "Snow": ":cloud_snow:", "Clear": ":sun:", "Mist": "Mist", "Smoke": "Smoke", "Haze": "Haze", "Dust": "Dust", "Fog": "Fog", "Sand": "Sand", "Dust": "Dust", "Ash": "Ash", "Squall": "Squall", "Tornado": "Tornado",} + self.url = "https://api.openweathermap.org/data/2.5/onecall?" + self.key = bot.api.keys.weather_api - now = weather["current"] - message = "Now: " + categories[now["weather"][0]["main"]] + "\n" - message += ":thermometer: " + str(int(now["temp"])) + "°\n\n" + def show_weather(self, location): + delta = datetime.datetime.now() - self.last_fetch + if delta.total_seconds()/60 > 60 or "\n" not in self.last_weather: # 1 hour passed: - weather_days = weather["daily"] - - for i, day in enumerate(weather_days): - if i == 0: - message += "" + "Today" + ": " + categories[day["weather"][0]["main"]] + "\n" - else: - message += "" + days[(today + i + 1) % 7] + ": " + categories[day["weather"][0]["main"]] + "\n" - message += ":thermometer: :fast_down_button: " + str(int(day["temp"]["min"])) + "° , :thermometer: :fast_up_button: " + str(int(day["temp"]["max"])) + "°\n\n" - except: - return "Query failed, it's my fault, I'm sorry :sad:" - return message + + data = {"lat" : location[0], "lon" : location[1], "exclude" : "minutely,hourly", "appid" : self.key, "units" : "metric"} + # today = datetime.datetime.today().weekday() + # days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] + + try: + weather = requests.get(self.url,params=data).json() + # categories = {"Clouds": ":cloud:", "Rain": ":cloud_with_rain:", "Thunderstorm": "thunder_cloud_rain", "Drizzle": ":droplet:", "Snow": ":cloud_snow:", "Clear": ":sun:", "Mist": "Mist", "Smoke": "Smoke", "Haze": "Haze", "Dust": "Dust", "Fog": "Fog", "Sand": "Sand", "Dust": "Dust", "Ash": "Ash", "Squall": "Squall", "Tornado": "Tornado",} + now = weather["current"] + ret_weather = [] + ret_weather.append({ + "short" : now["weather"][0]["main"], + "temps" : [int(now["temp"])] + }) + weather_days = weather["daily"] + for i, day in enumerate(weather_days): + ret_weather.append({ + "short" : day["weather"][0]["main"], + "temps" : [int(day["temp"]["min"]),int(day["temp"]["max"])] + }) + except: + ret_weather = [] + + + # now = weather["current"] + # message = "Now: " + categories[now["weather"][0]["main"]] + "\n" + # message += ":thermometer: " + str(int(now["temp"])) + "°\n\n" + + # weather_days = weather["daily"] + + # for i, day in enumerate(weather_days): + # if i == 0: + # message += "" + "Today" + ": " + categories[day["weather"][0]["main"]] + "\n" + # else: + # message += "" + days[(today + i + 1) % 7] + ": " + categories[day["weather"][0]["main"]] + "\n" + # message += ":thermometer: :fast_down_button: " + str(int(day["temp"]["min"])) + "° , :thermometer: :fast_up_button: " + str(int(day["temp"]["max"])) + "°\n\n" + # except: + # message = "Query failed, it's my fault, I'm sorry :sad:" + + self.last_weather = ret_weather + self.last_fetch = datetime.datetime.now() + else: + ret_weather = self.last_weather + + return ret_weather diff --git a/bot/framework.py b/bot/framework.py index 17fed77..9c37b3d 100644 --- a/bot/framework.py +++ b/bot/framework.py @@ -60,6 +60,7 @@ class BotFramework(): } self.telegram = telegram.TelegramIO(self.persistence) + self.weather = weather.WeatherFetch() def react_chats(self): """Checks unanswered messages and answers them""" diff --git a/bot/main.py b/bot/main.py index c290d34..dc558e7 100644 --- a/bot/main.py +++ b/bot/main.py @@ -1,6 +1,5 @@ -from bot.api import telegram, google, weather, reddit - import datetime +from bot.api import telegram, google, weather, reddit import requests import time @@ -90,8 +89,22 @@ class ChatBot(FW.BotFramework): city = locations[loc.lower().replace("ü","u")] else: return "Couldn't find city, it might be added later though." + + categories = {"Clouds": ":cloud:", "Rain": ":cloud_with_rain:", "Thunderstorm": "thunder_cloud_rain", "Drizzle": ":droplet:", "Snow": ":cloud_snow:", "Clear": ":sun:", "Mist": "Mist", "Smoke": "Smoke", "Haze": "Haze", "Dust": "Dust", "Fog": "Fog", "Sand": "Sand", "Dust": "Dust", "Ash": "Ash", "Squall": "Squall", "Tornado": "Tornado",} + days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] + today = datetime.datetime.today().weekday() + weather = self.weather.show_weather(city) + + now = weather.pop(0) + message = "Now: " + categories[now["short"]] + "\n" + message += ":thermometer: " + str(now["temps"][0]) + "°\n\n" - message = weather.show_weather(city) + for i, day in enumerate(weather): + if i == 0: + message += "" + "Today" + ": " + categories[day["short"]] + "\n" + else: + message += "" + days[(today + i + 1) % 7] + ": " + categories[day["short"]] + "\n" + message += ":thermometer: :fast_down_button: " + str(day["temps"][0]) + "° , :thermometer: :fast_up_button: " + str(day["temps"][1]) + "°\n\n" return message diff --git a/clock/api/converter.py b/clock/api/converter.py index c25f93e..4ba5381 100644 --- a/clock/api/converter.py +++ b/clock/api/converter.py @@ -86,15 +86,13 @@ def date_converter(): return pixels - - weather_categories = { - "cloud": "cloud", - "cloud_with_rain": "rain and cloud", - "thunder_cloud_rain": "thunder and cloud", - "droplet": "rain and cloud", - "cloud_snow": "snow and cloud", - "sun": "sun", + "Clouds": "cloud", + "Rain": "rain and cloud", + "Thunderstorm": "thunder and cloud", + "Drizzle": "rain and cloud", + "Snow": "snow and cloud", + "Clear": "sun", "Mist": "fog and clouds", "Smoke": "Smoke", "Haze": "Haze", diff --git a/clock/api/led.py b/clock/api/led.py index 7a61cde..40d4187 100644 --- a/clock/api/led.py +++ b/clock/api/led.py @@ -92,7 +92,7 @@ class OutputHandler(): if weather["show"] == "weather": face2_3d = converter.weather_converter(weather["weather"]) else: - face2 = converter.time_converter(top=weather["low"], bottom=weather["high"]) + face2 = converter.time_converter(top=str(weather["low"]), bottom=str(weather["high"])) face2 = np.concatenate((face2[:8,...],2*face2[8:,...])) face2_3d = self.matrix_add_depth(face2,[[0, 102, 255],[255, 102, 0]]) diff --git a/dashboard/main.py b/dashboard/main.py index 3bf68dd..c8953d9 100644 --- a/dashboard/main.py +++ b/dashboard/main.py @@ -2,125 +2,193 @@ import dash import dash_bootstrap_components as dbc import dash_html_components as html import dash_core_components as dcc +from dash.dependencies import Input, Output + import locale locale.setlocale(locale.LC_TIME, "de_DE.utf8") #from dash.dependencies import Input, Output import datetime +import time +import xmltodict +import requests +import emoji class DashBoard(): """""" - def __init__(self, host_ip): + def __init__(self, host_ip, prst): ## pre-sets - self.today = datetime.date.today().strftime("%A, der %d. %B %Y") + self.inter_margin = "1em" - + self.persistence = prst + self.host_ip = host_ip ex_css = [dbc.themes.BOOTSTRAP] self.app = dash.Dash(__name__, external_stylesheets=ex_css) - - self.set_layout() - self.app.run_server(host=host_ip) - - - def set_layout(self): - - card_placeholder = dbc.Card( - [ - #dbc.CardImg(src="/assets/ball_of_sun.jpg", top=True, bottom=False, - # title="Image by Kevin Dinkel", alt='Learn Dash Bootstrap Card Component'), - dbc.CardBody( - [ - html.H4("Learn Dash with Charming Data", className="card-title"), - html.H6("Lesson 1:", className="card-subtitle"), - html.P( - "Choose the year you would like to see on the bubble chart. Test this is a very long text that should hopefully have a line break.", - className="card-text", - ), - html.P( - "Choose the year you would like to see on the bubble chart.", - className="card-text", - ), - html.P( - "Choose the year you would like to see on the bubble chart.", - className="card-text", - ), - html.P( - "Choose the year you would like to see on the bubble chart.", - className="card-text", - ), - - dbc.Button("Still not live I guess?", color="primary"), - dbc.CardLink("GirlsWhoCode", href="https://girlswhocode.com/", target="_blank"), - ] - ), - ], - color="dark", # https://bootswatch.com/default/ for more card colors - inverse=True, # change color of text (black or white) - outline=False, # True = remove the block colors from the background and header - ) - - card_weather = dbc.Card( - - ) - - card_bot_stats = dbc.Card( - - ) - - - card_header = dbc.Card( - [ dbc.CardImg(src="static/header.jpg", top=True, bottom=False, - title="Header", alt='Header image'), - dbc.CardBody([html.H4(self.today, className="card-title")]), - ], - color="dark", - style = {"width" : "100%", "margin-bottom":self.inter_margin}, - inverse=True, - ) - - card_shopping_list = dbc.Card( - [ - dbc.CardBody([ - html.H4("Shopping list:", className="card-title"), - # html.P("What was India's life expectancy in 1952?", className="card-text"), - dbc.ListGroup( - [ - dbc.ListGroupItem("test"), - dbc.ListGroupItem("B. 37 years"), - dbc.ListGroupItem("C. 49 years"), - ], flush=False) - ]), - ], - color="dark", - style = {"width" : "100%", "margin-bottom":self.inter_margin}, - inverse=True, - ) - - self.app.layout = html.Div([ - html.Div( - className = "content", - style={"padding":self.inter_margin}, - children = [ - # dbc.Row([dbc.Col(card_main, width=3), - # dbc.Col(card_question, width=3)], justify="around"), # justify="start", "center", "end", "between", "around" - card_header, - dbc.CardColumns([ - card_shopping_list, - card_placeholder, - card_placeholder, - card_placeholder, - card_placeholder, - card_placeholder, - ]), - - ]) + html.Div(id = 'layout-update', className = "content", style={"padding":self.inter_margin},), + dcc.Interval( + id='interval-component', + interval=3600*1000, # in milliseconds + n_intervals=0 + ) ]) + @self.app.callback(Output('layout-update','children'), Input('interval-component','n_intervals')) + def update_layout(n): + print("REFRESH") + kids = [ + self.card_header(), + dbc.CardColumns([ + *self.cards_lists(), + self.card_news(), + self.card_xkcd(), + self.card_weather() + + + ]) + ] + return kids + #[card_header, dbc.CardColumns([card_shopping_list,card_placeholder,card_placeholder,card_placeholder,card_placeholder,card_placeholder])] + + + def launch_dashboard(self): + self.app.run_server(host=self.host_ip)#, debug=True) + + + def card_header(self): + today = datetime.date.today().strftime("%A, der %d. %B %Y") + card = dbc.Card( + [ dbc.CardImg(src="static/header.jpg", top=True, bottom=False, + title="Header", alt='Header image'), + dbc.CardBody([html.H3(today, className="card-title")]), + ], + color="dark", + style = {"width" : "100%", "margin-bottom":self.inter_margin}, + inverse=True, + ) + return card + + + def cards_lists(self): + ret = [] + for l in self.persistence["global"]["lists"].keys(): + l_content = self.persistence["global"]["lists"][l] + html_content = [dbc.ListGroupItem(t) for t in l_content] + card = dbc.Card( + [ + dbc.CardBody([ + html.H4("Liste '" + l + "':", className="card-title"), + dbc.ListGroup(html_content, flush=True, style={"color":"black"}) + ]), + ], + color="dark", + inverse=True, + ) + ret.append(card) + return ret + + + # def card_bot_stats(self): + # return card + + def card_weather(self): + try: + body = [html.H4("Wetter", className="card-title")] + + content = self.bot.weather.show_weather([47.3769, 8.5417]) # still zürich + + wt = content.pop(0) + body.append(html.Span(children=[ + html.H6("Jetzt: " + wt["short"]), + html.P(emoji.emojize(":thermometer: ") + str(wt["temps"][0]) + "°") + ])) + + days = ["Montag", "Dienstag", "Miitwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"] + today = datetime.datetime.today().weekday() + + for i, day in enumerate(content): + tmp = [] + if i == 0: + tmp.append(html.H6("Heute: "+ day["short"])) + else: + tmp.append(html.H6(days[(today + i + 1) % 7] + ": " + day["short"])) + tmp.append(html.P(emoji.emojize(":thermometer: :fast_down_button: " + str(day["temps"][0]) + "° , :thermometer: :fast_up_button: " + str(day["temps"][1]) + "°"))) + + body.append(html.Span(children=tmp)) + + + card = dbc.Card( + [dbc.CardBody(body)], + color="dark", + inverse=True, + ) + except: + card = card = dbc.Card([ + dbc.CardBody([ + html.H4("Could not load WEATHER", className="card-title"), + ]) + ], + color="dark", + inverse=True, + ) + return card + + + def card_news(self): + try: + card = dbc.Card([ + dbc.CardBody([html.Iframe(src="https://nzz.ch", style={"border":"none", "min-height":"30em", "width":"100%"})]) + ], + color="dark", + inverse=True, + ) + except: + card = card = dbc.Card([ + dbc.CardBody([ + html.H4("Could not load NEWS", className="card-title"), + ]) + ], + color="dark", + inverse=True, + ) + return card + + + def card_xkcd(self): + try: + xml = requests.get("https://xkcd.com/atom.xml").content + feed = xmltodict.parse(xml) + title = feed["feed"]["entry"][0]["title"] + img = feed["feed"]["entry"][0]["summary"]["#text"] + i1 = img.find('"') +1 + i2 = img.find('"', i1+1) + i3 = img.find('"', i2+1) + 1 + i4 = img.find('"', i3+1) + img_src = img[i1:i2] + img_alt = img[i3:i4] + card = dbc.Card([ + dbc.CardBody([ + html.H4(title, className="card-title"), + html.Img(src=img_src, style={"width":"100%"}), + html.P(img_alt) + ]) + ], + color="dark", + inverse=True, + ) + except: + card = card = dbc.Card([ + dbc.CardBody([ + html.H4("Could not load XKCD", className="card-title"), + ]) + ], + color="dark", + inverse=True, + ) + return card if __name__ == "__main__": - test = DashBoard(host_ip="0.0.0.0") - + test = DashBoard(host_ip="0.0.0.0") \ No newline at end of file diff --git a/launcher.py b/launcher.py index d6ec187..cc33541 100644 --- a/launcher.py +++ b/launcher.py @@ -18,13 +18,15 @@ class Launcher(): if len(self.persistence) == 0: self.init_persistence() self.persistence["global"]["reboots"] += 1 + self.clock_module = clock.main.ClockFace(prst=self.persistence) self.bot_module = bot.main.ChatBot(name="ChatterBot", version="2.1", prst=self.persistence, hw_commands=self.clock_module.commands) - + self.dashboard_module = dashboard.main.DashBoard(host_ip="0.0.0.0", prst=self.persistence) + self.threads = [] self.threads.append(Thread(target=self.chatbot)) self.threads.append(Thread(target=self.clock)) - + self.threads.append(Thread(target=self.dashboard)) for i in self.threads: i.start() diff --git a/wrapper.py b/wrapper.py index fcdc268..b4e44b7 100644 --- a/wrapper.py +++ b/wrapper.py @@ -30,6 +30,7 @@ class ClockWrapper(Wrapper): """""" super().__init__(own_module, *other_modules) self.weather = {"weather":"", "high":"", "low":"", "show":"temps"} + self.weather_raw = {} self.mainloop(15) @@ -37,42 +38,35 @@ class ClockWrapper(Wrapper): 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) + self.prev_time = "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 self.prev_time != datetime.datetime.now().strftime("%H%M"): - 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("")+5:weather.find("\n")].replace (":","") - # current weather situation (icon): we pick the first line, remove the start string, remove :: indicating an emoji + if int(self.prev_time) % 5 == 0: + weather = self.others[0].weather.show_weather([47.3769, 8.5417]) # zürich - 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 + if weather != self.weather_raw and len(weather) != 0: + td = weather[1] + + low = td["temps"][0] + high = td["temps"][1] + self.weather["weather"] = td["short"] self.weather["high"] = high self.weather["low"] = low - else: + elif len(weather) == 0: self.weather["weather"] = "error" self.weather["high"] = "error" self.weather["low"] = "error" + # if weather == self.weather.raw do nothing - 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.prev_time = datetime.datetime.now().strftime("%H%M") self.own.set_face(self.weather) @@ -105,28 +99,11 @@ class BotWrapper(Wrapper): - -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) + # self.mainloop(1 * 3600) # 1 hour refresh-cycle + # cannot get called through mainloop, will use the included callback-functionality of Dash + own_module.bot = other_modules[0] + own_module.launch_dashboard() \ No newline at end of file