diff --git a/bot/api/weather.py b/bot/api/weather.py index d5e92fc..d379cea 100644 --- a/bot/api/weather.py +++ b/bot/api/weather.py @@ -20,12 +20,9 @@ class WeatherFetch(): data = {"lat" : location[0], "lon" : location[1], "exclude" : "minutely,hourly", "appid" : self.key, "units" : "metric"} self.calls += 1 logger.info("Just fetched weather. ({}th time)".format(self.calls)) - # 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({ @@ -41,47 +38,9 @@ class WeatherFetch(): 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 - - # def get_weather_by_city(self, city): - # loc = get_coords_from_city(self, city) - # weather = self.show_weather(loc) - # return weather - - - # def get_coords_from_city(self, city): - # url = "https://devru-latitude-longitude-find-v1.p.rapidapi.com/latlon.php" - # data = {"location": city} - # headers = { - # "x-rapidapi-key" : "d4e0ab7ab3mshd5dde5a282649e0p11fd98jsnc93afd98e3aa", - # "x-rapidapi-host" : "devru-latitude-longitude-find-v1.p.rapidapi.com", - # } - - # #try: - # resp = requests.request("GET", url, headers=headers, params=data) - # result = resp.text - # #except: - # # result = "???" - # return result - diff --git a/client.py b/client.py index a32d2dc..a711a8f 100644 --- a/client.py +++ b/client.py @@ -1,10 +1,8 @@ # functionality from clock import c_in, c_out from broadcast import b_in - import launcher - import logging import os diff --git a/clock/c_in.py b/clock/c_in.py index 4122fd9..17ba573 100644 --- a/clock/c_in.py +++ b/clock/c_in.py @@ -1,6 +1,4 @@ -import datetime import time -from threading import Thread, Timer from . import hardware, helpers @@ -16,9 +14,10 @@ class SensorReadout: "luminosity" : hardware.sensors.BrightnessModule(), # more to come? } + # self db_utils set externally def start(self): - helpers.timer.RepeatedTimer(300, self.spread_measure) + helpers.timer.RepeatedTimer(120, self.spread_measure) def spread_measure(self): measurements = dict((el,[]) for el in self.sensor_modules.keys()) @@ -33,24 +32,7 @@ class SensorReadout: results = {} for e in measurements.keys(): lst = measurements[e] - results[e] = int(sum(lst) / len(lst)) + results[e] = sum(lst) / len(lst) - self.save_results(**results) + self.db_utils.sensor_log(**results) - - # def save_results(self, results): - # current_minute = int(datetime.datetime.now().timestamp() // 60) - - # self.persistence["clock"]["sensors"]["time"] += [current_minute] - - # for name in results.keys(): - # keep_value = sum(results[name]) / len(results[name]) - # self.persistence["clock"]["sensors"][name] += [keep_value] - - - def save_results(self, **results): - data = self.db.sensors( - time=datetime.datetime.now(), - **results, - ) - data.save() diff --git a/clock/hardware/sensors.py b/clock/hardware/sensors.py index 3489606..587cbfa 100644 --- a/clock/hardware/sensors.py +++ b/clock/hardware/sensors.py @@ -4,8 +4,8 @@ logger = logging.getLogger(__name__) class TempSim: """Simulates a temperature for running on windows""" - temperature = 23 # return a celsius value - humidity = 30 + temperature = 23.23 # return a celsius value + humidity = 30.4 class LightSim: diff --git a/dashboard/Microsoft Edge.lnk b/dashboard/Microsoft Edge.lnk deleted file mode 100644 index 09a8c0c..0000000 Binary files a/dashboard/Microsoft Edge.lnk and /dev/null differ diff --git a/dashboard/__init__.py b/dashboard/__init__.py deleted file mode 100644 index 9a5984d..0000000 --- a/dashboard/__init__.py +++ /dev/null @@ -1 +0,0 @@ -# Placeholder diff --git a/dashboard/cards/__init__.py b/dashboard/cards/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/dashboard/cards/weather.py b/dashboard/cards/weather.py deleted file mode 100644 index e69de29..0000000 diff --git a/dashboard/cards/xkcd.py b/dashboard/cards/xkcd.py deleted file mode 100644 index e69de29..0000000 diff --git a/dashboard/d_out.py b/dashboard/d_out.py deleted file mode 100644 index e1f2fcc..0000000 --- a/dashboard/d_out.py +++ /dev/null @@ -1,299 +0,0 @@ -import dash -import dash_bootstrap_components as dbc -import dash_html_components as html -import dash_core_components as dcc -import plotly.graph_objects as go -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 -from threading import Thread - - -from . import helpers - -class DashBoard(): - """""" - # added by the launcher, we have self.modules (dict) - - def __init__(self, port): - ## pre-sets - - self.inter_margin = "1em" - self.host_ip = "0.0.0.0" - self.port = port - - ex_css = [dbc.themes.BOOTSTRAP] - self.app = dash.Dash(__name__, external_stylesheets=ex_css) - self.app.layout = html.Div([ - 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 - ) - ]#,style={'background-image':'url("static/background.jpg")'} - ) - - @self.app.callback(Output('layout-update','children'), Input('interval-component','n_intervals')) - def update_layout(n): - self.set_stats() - kids = [ - self.card_header(), - dbc.CardColumns([ - # self.card_weather(), - *self.cards_lists(), - self.card_bot_stats(), - self.card_news(), - self.card_xkcd(), - self.card_sensor_stats(), - ]) - ] - return kids - - - def start(self): - flaskThread = Thread(target=app.run_server, kwargs={"host": self.host_ip, "port": self.port}).start() - #self.app.run_server()#, 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 = [html.A(t, href="#", className="list-group-item bg-dark list-group-item-action text-light") 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): - if not self.stat_graph: - self.set_stats() - - card = dbc.Card( - [ - dbc.CardBody([ - html.H4("Chat-Metriken", className="card-title"), - dcc.Graph(figure=self.stat_graph, config={'displayModeBar': False}) - ]), - ], - color="dark", - inverse=True, - ) - return card - - def card_sensor_stats(self): - fig = go.Figure() - sensors = self.persistence["clock"]["sensors"] - time = sensors["time"] - time = [t - time[0] for t in time] # rescale - for sensor in sensors.keys(): - if sensor != "time": - fig.add_trace(go.Scatter(x=time, y=sensors[sensor], mode="lines", text=sensor, line=dict(width=4))) - - fig.layout.update( - # xaxis = { - # 'showgrid': False, # thin lines in the background - # 'zeroline': False, # thick line at x=0 - # 'visible': False, # numbers below - # }, # the same for yaxis - # yaxis = { - # 'showgrid': False, # thin lines in the background - # 'zeroline': False, # thick line at x=0 - # 'visible': False, # numbers below - # }, # the same for yaxis - - showlegend=False, - # margin=dict(l=0, r=0, t=0, b=0), - # paper_bgcolor='rgba(0,0,0,0)', - # plot_bgcolor='rgba(0,0,0,0)', - ) - - card = dbc.Card( - [ - dbc.CardBody([ - html.H4("Sensor-Metriken", className="card-title"), - dcc.Graph(figure=fig, config={'displayModeBar': False}) - ]), - ], - color="dark", - inverse=True, - ) - return card - - - def card_weather(self): - def weather_item(name, overview, temps): - if len(temps) == 2: - temp = "🌡(❄): " + str(temps[0]) + "° ➡ 🌡(🔥): " + str(temps[1]) + "°" - else: - temp = "🌡: " + str(temps[0]) + "°" - temp_line = html.P(temp, className="mb-1") - - it = html.A([ - html.Div([ - html.H5(name, className="mb-1"), - html.Span(categories[overview], className="badge badge-primary badge-pill") - ], - className="d-flex w-100 justify-content-between"), - temp_line, - ], - href="#", className="list-group-item bg-dark list-group-item-action text-light" - ) - - return it - - days = ["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"] - categories = {"Clouds": "☁", "Rain": "🌧", "Thunderstorm": "🌩", "Drizzle": ":droplet:", "Snow": "❄", "Clear": "☀", "Mist": "🌫", "Smoke": "Smoke", "Haze": "Haze", "Dust": "Dust", "Fog": "Fog", "Sand": "Sand", "Dust": "Dust", "Ash": "Ash", "Squall": "Squall", "Tornado": "Tornado",} - today = datetime.datetime.today().weekday() - - body = [] - - try: - bot = self.modules["bot"] - content = bot.api_weather.show_weather([47.3769, 8.5417]) # still zürich - - wt = content.pop(0) - body.append(weather_item("Jetzt", wt["short"], wt["temps"])) - - for i, day in enumerate(content): - tmp = [] - if i == 0: - day_name = "Heute" - else: - day_name = days[(today + i) % 7] - - body.append(weather_item(day_name, day["short"], day["temps"])) - body = dbc.ListGroup(body, flush=True, style={"color":"black"}) - - - except: - body.append(html.H6("Konnte nicht geladen werden")) - - card = dbc.Card( - [dbc.CardBody([ - html.H4("Wetter", className="card-title"), - body])], - 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 = dbc.Card([ - dbc.CardBody([ - html.H4("Could not load XKCD", className="card-title"), - ]) - ], - color="dark", - inverse=True, - ) - return card - - - ######### helper: - def set_stats(self): - xs, ys = helpers.clean_axis(self.persistence["bot"]["send_activity"]["hour"], self.persistence["bot"]["send_activity"]["count"]) - xr, yr = helpers.clean_axis(self.persistence["bot"]["receive_activity"]["hour"], self.persistence["bot"]["receive_activity"]["count"]) - xe, ye = helpers.clean_axis(self.persistence["bot"]["execute_activity"]["hour"], self.persistence["bot"]["execute_activity"]["count"]) - - fig = go.Figure() - fig.add_trace(go.Scatter(x=xr, y=yr, mode="lines", text="Gelesen", line=dict(width=4))) - fig.add_trace(go.Scatter(x=xs, y=ys, mode="lines", text="Gesendet", line=dict(width=4))) - fig.add_trace(go.Scatter(x=xe, y=ye, mode="lines", text="Ausgeführt", line=dict(width=4))) - - fig.update_xaxes(showgrid=False) - fig.update_yaxes(showgrid=False) - fig.layout.update( - xaxis = { - 'showgrid': False, # thin lines in the background - 'zeroline': False, # thick line at x=0 - 'visible': False, # numbers below - }, # the same for yaxis - yaxis = { - 'showgrid': False, # thin lines in the background - 'zeroline': False, # thick line at x=0 - 'visible': False, # numbers below - }, # the same for yaxis - - showlegend=False, - margin=dict(l=0, r=0, t=0, b=0), - paper_bgcolor='rgba(0,0,0,0)', - plot_bgcolor='rgba(0,0,0,0)', - ) - - self.stat_graph = fig diff --git a/dashboard/helpers.py b/dashboard/helpers.py deleted file mode 100644 index e32b9f3..0000000 --- a/dashboard/helpers.py +++ /dev/null @@ -1,18 +0,0 @@ - -def clean_axis(x,y): - """x is the time the point in y was taken""" - try: - xn = range(x[0], x[-1]+1) - yn = [] - count = 0 - for x_i in xn: - if x_i in x: - yn.append(y[count]) - count += 1 - else: - yn.append(0) - xn = [i - int(x[0]) for i in xn] - except: - xn = [] - yn = [] - return xn, yn \ No newline at end of file diff --git a/dashboard/static/background.jpg b/dashboard/static/background.jpg deleted file mode 100644 index 4b02663..0000000 Binary files a/dashboard/static/background.jpg and /dev/null differ diff --git a/dashboard/static/header.jpg b/dashboard/static/header.jpg deleted file mode 100644 index d51cc5a..0000000 Binary files a/dashboard/static/header.jpg and /dev/null differ diff --git a/dashboard/static/test.css b/dashboard/static/test.css deleted file mode 100644 index ee614d6..0000000 --- a/dashboard/static/test.css +++ /dev/null @@ -1,33 +0,0 @@ -div.header { - position: fixed; - width: 100%; - height: var(--heading-height); -} - -/* Create angled background with 'before' pseudo-element */ -header::before { - content: ""; - display: block; - position: absolute; - left: 0; - bottom: 6em; - width: 100%; - height: calc(var(--heading-height) + 10em); - z-index: -1; - transform: skewY(-3.5deg); - background: - linear-gradient(rgba(0,0,0,.6), rgba(0,0,0,.6)), - url(https://images.unsplash.com/photo-1495464101292-552d0b52fe41?auto=format&fit=crop&w=1350&q=80) no-repeat center, - linear-gradient(#4e4376, #2b5876); - background-size: cover; - border-bottom: .2em solid #fff; -} - -h1 { - font-size: calc(2.8em + 2.6vw); - font-weight: 500; - letter-spacing: .01em; - padding: 6rem 0 0 4.5rem; - text-shadow: .022em .022em .022em #111; - color: #fff; -} diff --git a/launcher.py b/launcher.py index 6790e60..59e1fdb 100644 --- a/launcher.py +++ b/launcher.py @@ -1,22 +1,22 @@ import logging -from persistence import p_io, p_out +from persistence import local_io, database logger = logging.getLogger(__name__) class Launcher: - """base launcher that launches other submodules""" + """Template for launching collections of modules""" def __init__(self, **modules): """""" - self.persistence = p_io.PersistentDict("persistence/prst.json") - self.db = p_out.DataBaseConnector() + self.persistence = local_io.PersistentDict("persistence/prst.json") + self.db_utils = database.DatabaseUtils + self.modules = modules logger.info(self.__class__.__name__ + " initialized") - self.modules = modules if len(self.persistence) == 0: self.init_persistence() self.persistence["global"]["reboots"] += 1 @@ -26,12 +26,11 @@ class Launcher: def launch_modules(self): - for module in self.modules.values(): - logger.info("Starting module "+ module.__class__.__name__) + logger.info("Starting module {}".format(module.__class__.__name__)) module.modules = self.modules module.persistence = self.persistence - module.db = self.db # pooled ie multithreaded + module.db_utils = self.db_utils() module.start() @@ -66,8 +65,4 @@ class Launcher: self.persistence[m_name] = data - - -######################################################################## -## Aand liftoff! -# Launcher() + \ No newline at end of file diff --git a/persistence/database.py b/persistence/database.py index 7449dd3..5998db9 100644 --- a/persistence/database.py +++ b/persistence/database.py @@ -82,5 +82,7 @@ class DatabaseUtils: def list_delete(self, list_name): - models.List.delete().where(self.db.lists.name == self.current_name).execute() + models.List.delete().where(self.db.lists.name == list_name).execute() + def sensor_log(self, **kwargs): + models.SensorMetric(**kwargs).save() \ No newline at end of file diff --git a/persistence/p_io.py b/persistence/local_io.py similarity index 100% rename from persistence/p_io.py rename to persistence/local_io.py diff --git a/persistence/models.py b/persistence/models.py index d03fac9..5954b23 100644 --- a/persistence/models.py +++ b/persistence/models.py @@ -27,9 +27,9 @@ class Metric(DBModel): class SensorMetric(Metric): # this is a continuous metric - temperature = IntegerField() - humidity = IntegerField() - luminosity = IntegerField() + temperature = FloatField() + humidity = FloatField() + luminosity = FloatField() class ChatMetric(Metric): diff --git a/requirements.txt b/requirements.txt index 66fa993..8360100 100644 Binary files a/requirements.txt and b/requirements.txt differ diff --git a/server.py b/server.py index 32a87a8..38e0757 100644 --- a/server.py +++ b/server.py @@ -8,9 +8,9 @@ import launcher import logging -import os +import platform -if os.name == "nt": +if platform.uname().node == "ArchSpectre": # development logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO