better stats-managing, clock functionality.

This commit is contained in:
Remy Moll 2021-02-21 21:50:45 +01:00
parent 3ec1744a15
commit b2007f2111
7 changed files with 115 additions and 63 deletions

View File

@ -1,5 +1,4 @@
import requests
# import api.keys
import datetime
class WeatherFetch():
@ -59,3 +58,25 @@ class WeatherFetch():
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

View File

@ -1,6 +1,8 @@
from .template import *
import time
import numpy
from PIL import Image
import io
CHOOSE, ADDARG = range(2)
MESSAGE, WAKE, ALARM, IMAGE, ART = range(3,8)
@ -59,7 +61,7 @@ class Clock(BotFunc):
query.answer()
query.edit_message_text("Ok. How long should it blink? (In seconds)")
self.next_state = {"ALARM" : "What frequency (Hertz)"}
self.next_state = {ALARM : "What frequency (Hertz)"}
return ADDARG
def show_message(self, update: Update, context: CallbackContext) -> None:
@ -74,7 +76,7 @@ class Clock(BotFunc):
query.answer()
query.edit_message_text("How long (in minutes) should the image be displayed?")
self.next_state = {"IMAGE" : "Please send me the photo to display."}
self.next_state = {IMAGE : "Please send me the photo to display."}
return ADDARG
def art_gallery(self, update: Update, context: CallbackContext) -> None:
@ -98,7 +100,7 @@ class Clock(BotFunc):
duration = update.message.text
def output(duration):
self.clock.set_brightness(value=0.1)
self.clock.set_brightness(value=1)
start_color = numpy.array([153, 0, 51])
end_color = numpy.array([255, 255, 0])
empty = numpy.zeros((16,32))
@ -137,19 +139,37 @@ class Clock(BotFunc):
if not(duration == 0 or frequency == 0):
update.message.reply_text("Now blinking")
self.clock.run(output,(duration, frequency))
print("DOOONE")
return ConversationHandler.END
def exec_show_image(self, update: Update, context: CallbackContext) -> None:
duration = self.additional_argument
img = update.message.photo
i = update.message.photo
img = update.message.photo[0]
bot = img.bot
id = img.file_id
file = bot.getFile(id).download_as_bytearray()
width = self.clock.IO.width
height = self.clock.IO.height
img = Image.open(io.BytesIO(file))
im_height = img.height
im_width = img.width
scalex = im_width // width
scaley = im_height // height
scale = min(scalex, scaley)
t = img.resize((width, height),box=(0,0,width*scale,height*scale))
a = numpy.asarray(t)
def output(image, duration):
self.clock.IO.set_matrix_rgb([100,0,0])
self.clock.IO.set_matrix(image)
time.sleep(int(duration) * 60)
self.clock.run(output,("image", duration))
self.clock.run(output,(a, duration))
return ConversationHandler.END

View File

@ -26,7 +26,7 @@ class Help(BotFunc):
# ]
},
fallbacks=[CommandHandler('help', self.entry_point)],
# conversation_timeout=5,
conversation_timeout=15,
)
return conv_handler
@ -92,10 +92,11 @@ class Help(BotFunc):
query.edit_message_text(
text= message,
#reply_markup = reply_markup,
reply_markup = reply_markup,
parse_mode = ParseMode.MARKDOWN_V2
)
return ConversationHandler.END #EXECUTE
return EXECUTE
def execute_now(self, update: Update, context: CallbackContext) -> None:
query = update.callback_query
@ -105,9 +106,10 @@ class Help(BotFunc):
for func in funcs:
if name == func.entry_points[0].command[0]:
break
callback = func.entry_points[0].callback
func.callback(update, context)
return FIRST
callback = func.entry_points[0].handle_update
callback(update, context.dispatcher, check_result=True, context=context)
return ConversationHandler.END
def timeout(self, update: Update, context: CallbackContext) -> None:
"""For dying conversation. Currently unused."""

View File

@ -40,7 +40,7 @@ class Search(BotFunc):
# formating
self.results = results
first = results[0]
message = first["text"] + "\n(" + first["url"] + ")\n"
message = first["text"] + "\n(" + first["url"] + ")\n\n"
update.message.reply_text(text = message, reply_markup=reply_markup)
return MORE
@ -52,7 +52,7 @@ class Search(BotFunc):
message = ""
for r in self.results:
message += r["text"] + "\n(" + r["url"] + ")\n"
message += r["text"] + "\n(" + r["url"] + ")\n\n"
query.edit_message_text(message)
return ConversationHandler.END

View File

@ -31,10 +31,10 @@ class OutputHandler():
# reshape to the main size: (eg 32x16) (always aligns the given matrix on top left.)
if len(matrix.shape) != 3:
# add depth (rgb values)
r3 = self.matrix_add_depth(matrix,colors)
self.set_matrix_rgb(r3,quadrant)
matrix = self.matrix_add_depth(matrix,colors)
self.set_matrix_rgb(matrix,quadrant)
def matrix_add_depth(self, matrix, colors = []):

View File

@ -40,6 +40,7 @@ class DashBoard():
@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([
@ -91,55 +92,14 @@ class DashBoard():
def card_bot_stats(self):
def cleanse_graph(category):
x = self.persistence["bot"][category]["hour"]
y = self.persistence["bot"][category]["count"]
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]
return xn, yn
xs, ys = cleanse_graph("send_activity")
xr, yr = cleanse_graph("receive_activity")
xe, ye = cleanse_graph("execute_activity")
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)',
)
if not self.stat_graph:
self.set_stats()
card = dbc.Card(
[
dbc.CardBody([
html.H4("Statistiken", className="card-title"),
dcc.Graph(figure=fig,config={'displayModeBar': False})
dcc.Graph(figure=self.stat_graph, config={'displayModeBar': False})
]),
],
color="dark",
@ -147,6 +107,7 @@ class DashBoard():
)
return card
def card_weather(self):
def weather_item(name, overview, temps):
if len(temps) == 2:
@ -258,3 +219,51 @@ class DashBoard():
)
return card
######### helper:
def set_stats(self):
def cleanse_graph(category):
x = self.persistence["bot"][category]["hour"]
y = self.persistence["bot"][category]["count"]
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]
return xn, yn
xs, ys = cleanse_graph("send_activity")
xr, yr = cleanse_graph("receive_activity")
xe, ye = cleanse_graph("execute_activity")
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

BIN
image.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB