adjust clock module

This commit is contained in:
Remy Moll 2022-01-01 16:20:44 +01:00
parent f468332843
commit 1c5092065b
20 changed files with 22 additions and 437 deletions

View File

@ -20,12 +20,9 @@ class WeatherFetch():
data = {"lat" : location[0], "lon" : location[1], "exclude" : "minutely,hourly", "appid" : self.key, "units" : "metric"} data = {"lat" : location[0], "lon" : location[1], "exclude" : "minutely,hourly", "appid" : self.key, "units" : "metric"}
self.calls += 1 self.calls += 1
logger.info("Just fetched weather. ({}th time)".format(self.calls)) logger.info("Just fetched weather. ({}th time)".format(self.calls))
# today = datetime.datetime.today().weekday()
# days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
try: try:
weather = requests.get(self.url,params=data).json() 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"] now = weather["current"]
ret_weather = [] ret_weather = []
ret_weather.append({ ret_weather.append({
@ -41,47 +38,9 @@ class WeatherFetch():
except: except:
ret_weather = [] ret_weather = []
# now = weather["current"]
# message = "<b>Now:</b> " + 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 += "<b>" + "Today" + ":</b> " + categories[day["weather"][0]["main"]] + "\n"
# else:
# message += "<b>" + days[(today + i + 1) % 7] + ":</b> " + 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_weather = ret_weather
self.last_fetch = datetime.datetime.now() self.last_fetch = datetime.datetime.now()
else: else:
ret_weather = self.last_weather ret_weather = self.last_weather
return ret_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

View File

@ -1,10 +1,8 @@
# functionality # functionality
from clock import c_in, c_out from clock import c_in, c_out
from broadcast import b_in from broadcast import b_in
import launcher import launcher
import logging import logging
import os import os

View File

@ -1,6 +1,4 @@
import datetime
import time import time
from threading import Thread, Timer
from . import hardware, helpers from . import hardware, helpers
@ -16,9 +14,10 @@ class SensorReadout:
"luminosity" : hardware.sensors.BrightnessModule(), "luminosity" : hardware.sensors.BrightnessModule(),
# more to come? # more to come?
} }
# self db_utils set externally
def start(self): def start(self):
helpers.timer.RepeatedTimer(300, self.spread_measure) helpers.timer.RepeatedTimer(120, self.spread_measure)
def spread_measure(self): def spread_measure(self):
measurements = dict((el,[]) for el in self.sensor_modules.keys()) measurements = dict((el,[]) for el in self.sensor_modules.keys())
@ -33,24 +32,7 @@ class SensorReadout:
results = {} results = {}
for e in measurements.keys(): for e in measurements.keys():
lst = measurements[e] 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()

View File

@ -4,8 +4,8 @@ logger = logging.getLogger(__name__)
class TempSim: class TempSim:
"""Simulates a temperature for running on windows""" """Simulates a temperature for running on windows"""
temperature = 23 # return a celsius value temperature = 23.23 # return a celsius value
humidity = 30 humidity = 30.4
class LightSim: class LightSim:

Binary file not shown.

View File

@ -1 +0,0 @@
# Placeholder

View File

@ -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

View File

@ -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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 331 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 310 KiB

View File

@ -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;
}

View File

@ -1,22 +1,22 @@
import logging import logging
from persistence import p_io, p_out from persistence import local_io, database
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class Launcher: class Launcher:
"""base launcher that launches other submodules""" """Template for launching collections of modules"""
def __init__(self, **modules): def __init__(self, **modules):
"""""" """"""
self.persistence = p_io.PersistentDict("persistence/prst.json") self.persistence = local_io.PersistentDict("persistence/prst.json")
self.db = p_out.DataBaseConnector() self.db_utils = database.DatabaseUtils
self.modules = modules
logger.info(self.__class__.__name__ + " initialized") logger.info(self.__class__.__name__ + " initialized")
self.modules = modules
if len(self.persistence) == 0: if len(self.persistence) == 0:
self.init_persistence() self.init_persistence()
self.persistence["global"]["reboots"] += 1 self.persistence["global"]["reboots"] += 1
@ -26,12 +26,11 @@ class Launcher:
def launch_modules(self): def launch_modules(self):
for module in self.modules.values(): 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.modules = self.modules
module.persistence = self.persistence module.persistence = self.persistence
module.db = self.db # pooled ie multithreaded module.db_utils = self.db_utils()
module.start() module.start()
@ -66,8 +65,4 @@ class Launcher:
self.persistence[m_name] = data self.persistence[m_name] = data
########################################################################
## Aand liftoff!
# Launcher()

View File

@ -82,5 +82,7 @@ class DatabaseUtils:
def list_delete(self, list_name): 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()

View File

@ -27,9 +27,9 @@ class Metric(DBModel):
class SensorMetric(Metric): class SensorMetric(Metric):
# this is a continuous metric # this is a continuous metric
temperature = IntegerField() temperature = FloatField()
humidity = IntegerField() humidity = FloatField()
luminosity = IntegerField() luminosity = FloatField()
class ChatMetric(Metric): class ChatMetric(Metric):

Binary file not shown.

View File

@ -8,9 +8,9 @@ import launcher
import logging import logging
import os import platform
if os.name == "nt": if platform.uname().node == "ArchSpectre":
# development # development
logging.basicConfig( logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO