works flawlessly, hehe

This commit is contained in:
Remy Moll 2020-10-19 12:27:59 +02:00
parent 341b2b6ece
commit 6c4dbacdef
7 changed files with 68 additions and 36 deletions

@ -19,6 +19,9 @@ class TelegramIO():
def update_commands(self,commands):
self.commands = commands
######################################################################## ########################################################################
"""Helper-Functions""" """Helper-Functions"""
@ -79,7 +82,7 @@ class TelegramIO():
/[command] [argument 1] [argument 2] ... /[command] [argument 1] [argument 2] ...
""" """
full = command.split(" ") full = command.split(" ")
command = self.fuzzy_match_command(full[0]) # ["EXACT",full[0]] # command = self.fuzzy_match_command(full[0])
if len(command) != 1: if len(command) != 1:
if command[0] == "EXACT": if command[0] == "EXACT":
self.persistence.increment("commands_executed") self.persistence.increment("commands_executed")

@ -11,7 +11,7 @@ import emoji
class ChatBot(): class ChatBot():
"""""" """"""
def __init__(self, name, version, hw_commands): def __init__(self, name, version):
"""Inits the Bot with a few conf. vars """Inits the Bot with a few conf. vars
Args: -> name:str - Name of the bot Args: -> name:str - Name of the bot
-> api_key:str - t.me api-key -> api_key:str - t.me api-key
@ -27,7 +27,7 @@ class ChatBot():
self.start_time = datetime.datetime.now() self.start_time = datetime.datetime.now()
self.persistence.increment("reboots") self.persistence.increment("reboots")
# Available commands # Available commands. Must be manually updated!
self.commands = { self.commands = {
"help" : self.bot_show_help, "help" : self.bot_show_help,
"status" : self.bot_print_status, "status" : self.bot_print_status,
@ -86,9 +86,12 @@ class ChatBot():
"9" : ":keycap_digit_nine:", "9" : ":keycap_digit_nine:",
} }
self.all_commands = {**self.commands, **hw_commands} self.telegram = telegram.TelegramIO(self.persistence, self.commands)
self.telegram = telegram.TelegramIO(self.persistence, self.all_commands) def add_commands(self, commands):
"""adds new commands to an existing list"""
self.commands = {**self.commands, **commands}
self.telegram.update_commands(self.commands)
def react_command(self, command, params): def react_command(self, command, params):
@ -211,10 +214,10 @@ class ChatBot():
"""Shows a list of all commands and their description""" """Shows a list of all commands and their description"""
send_text = "BeebBop, this is " + self.name + " (V." + self.version + ")\n" send_text = "BeebBop, this is " + self.name + " (V." + self.version + ")\n"
send_text += "Here is what I can do up to now: \n" send_text += "Here is what I can do up to now: \n"
entries = sorted(list(self.all_commands.keys())) entries = sorted(list(self.commands.keys()))
for entry in entries: for entry in entries:
send_text += "<b>" + entry + "</b> - " send_text += "<b>" + entry + "</b> - "
send_text += "<code>" + self.all_commands[entry].__doc__ + "</code>\n\n" send_text += "<code>" + self.commands[entry].__doc__ + "</code>\n\n"
return send_text return send_text

@ -1,26 +1,25 @@
import time import time
import datetime import datetime
import bot.main
import clock.main
class ModuleWrapper(): class ModuleWrapper():
"""Wrapper for the BOT-functionality""" """Wrapper for the BOT-functionality"""
def __init__(self): def __init__(self, bot_module, clock_module):
"""""" """"""
print("Initializing bot-functionality") print("Initializing bot-functionality")
####################################################################### #######################################################################
self.clock = clock.main.ClockFace() self.bot = bot_module
self.clock = clock_module
# available hw-commands. Must be updated manually!
self.hw_commands = { self.hw_commands = {
"blink" : self.clock.alarm_blink, "blink" : self.clock.alarm_blink,
"wakeup" : self.clock.wake_light, "wakeup" : self.clock.wake_light,
"showmessage" : self.clock.show_message, "showmessage" : self.clock.show_message,
} }
self.bot.add_commands(self.hw_commands)
self.bot = bot.main.ChatBot("ChatterBot", "2.0", self.hw_commands)
self.message_loop() self.message_loop()
@ -34,13 +33,15 @@ class ModuleWrapper():
command, params = self.bot.telegram.handle_result(result) command, params = self.bot.telegram.handle_result(result)
if command != "nothing": if command != "nothing":
if command in self.hw_commands: if command in self.hw_commands:
self.react_command(command,params) self.react_command(command,params) #hw-level
else: else:
self.bot.react_command(command,params) self.bot.react_command(command,params) #sw-level
time.sleep(5) time.sleep(5)
def react_command(self, command, params): def react_command(self, command, params):
"""""" """"""
# Oh yeah, that needs to be changed # Oh yeah, that needs to be changed
# so params is a list, and so, to pass the commands, we need to unpack it: # so params is a list, and so, to pass the commands, we need to unpack it:
# should work fine
self.hw_commands[command](*params) self.hw_commands[command](*params)

@ -19,6 +19,7 @@ class UnicornHat(object):
self.window_width = self.width * self.pixel_size self.window_width = self.width * self.pixel_size
self.window_height = self.height * self.pixel_size self.window_height = self.height * self.pixel_size
self.brightness = 1
# Init pygame and off we go # Init pygame and off we go
pygame.init() pygame.init()
pygame.display.set_caption("Unicorn HAT simulator") pygame.display.set_caption("Unicorn HAT simulator")
@ -27,7 +28,7 @@ class UnicornHat(object):
def set_pixel(self, x, y, r, g, b): def set_pixel(self, x, y, r, g, b):
self.pixels[x][y] = int(r), int(g), int(b) self.pixels[x][y] = r, g, b
def set_matrix(self, matrix): def set_matrix(self, matrix):
@ -52,7 +53,9 @@ class UnicornHat(object):
#w_y = int((self.height - 1 - y) * p + p / 2) #w_y = int((self.height - 1 - y) * p + p / 2)
w_y = int(i * p + p / 2) w_y = int(i * p + p / 2)
r = int(p / 4) r = int(p / 4)
color = self.pixels[i,j,:] color = self.pixels[i,j,:]*self.brightness
color = color.astype("int")
pygame.gfxdraw.aacircle(self.screen, w_x, w_y, r, color) pygame.gfxdraw.aacircle(self.screen, w_x, w_y, r, color)
pygame.gfxdraw.filled_circle(self.screen, w_x, w_y, r, color) pygame.gfxdraw.filled_circle(self.screen, w_x, w_y, r, color)
@ -69,8 +72,8 @@ class UnicornHat(object):
return (self.width, self.height) return (self.width, self.height)
def set_brightness(self, *args): def set_brightness(self, brightness):
pass self.brightness = brightness
def rotation(self, r): def rotation(self, r):

@ -30,8 +30,10 @@ class ClockFace(object):
def run(self, command, kw=()): def run(self, command, kw=()):
"""Checks for running threads and executes the ones in queue""" """Checks for running threads and executes the ones in queue"""
def enhanced_run(command, kw): def enhanced_run(command, kw):
""""""
self.output_thread = "Running " + str(command) self.output_thread = "Running " + str(command)
command(*kw) command(*kw)
self.set_brightness()
self.output_thread = "" self.output_thread = ""
if len(self.output_queue) != 0: if len(self.output_queue) != 0:
n = self.output_queue.pop(0) n = self.output_queue.pop(0)
@ -50,7 +52,6 @@ class ClockFace(object):
### basic clock commands ### basic clock commands
def set_face(self, weather): def set_face(self, weather):
"""""" """"""
self.set_brightness()
self.weather = weather self.weather = weather
self.run(self.IO.clock_face,(weather,)) self.run(self.IO.clock_face,(weather,))
@ -60,8 +61,14 @@ class ClockFace(object):
self.run(self.IO.text_scroll,(text, self.tspeed, color)) self.run(self.IO.text_scroll,(text, self.tspeed, color))
def set_brightness(self, overwrite=[]): def set_brightness(self, overwrite=[],value=-1):
"""Checks, what brightness rules to apply""" """Checks, what brightness rules to apply"""
if value != -1:
self.IO.output.set_brightness(value)
return
if len(overwrite) != 0: if len(overwrite) != 0:
self.brightness_overwrite = overwrite self.brightness_overwrite = overwrite
@ -70,7 +77,7 @@ class ClockFace(object):
if (is_WE and (now > 1000 and now < 2200)) or ((not is_WE) and (now > 800 and now < 2130)): if (is_WE and (now > 1000 and now < 2200)) or ((not is_WE) and (now > 800 and now < 2130)):
brightness = 0.8 brightness = 0.8
else: else:
brightness = 0.1 brightness = 0.05
self.IO.output.set_brightness(brightness) self.IO.output.set_brightness(brightness)
@ -80,6 +87,7 @@ class ClockFace(object):
def wake_light(self, duration=600): def wake_light(self, duration=600):
"""Simulates a sunris, takes one optional parameter: the duration""" """Simulates a sunris, takes one optional parameter: the duration"""
def output(duration): def output(duration):
self.set_brightness(value=0.1)
start_color = numpy.array([153, 0, 51]) start_color = numpy.array([153, 0, 51])
end_color = numpy.array([255, 255, 0]) end_color = numpy.array([255, 255, 0])
empty = numpy.zeros((16,32)) empty = numpy.zeros((16,32))
@ -91,7 +99,7 @@ class ClockFace(object):
ct = i/20 * gradient ct = i/20 * gradient
col = [int(x) for x in ct+start_color] col = [int(x) for x in ct+start_color]
self.IO.set_matrix(ones,colors=[col]) self.IO.set_matrix(ones,colors=[col])
time.sleep(duration / 20) time.sleep(int(duration) / 20)
self.run(output,(duration,)) self.run(output,(duration,))
@ -100,6 +108,7 @@ class ClockFace(object):
def alarm_blink(self, duration, frequency): def alarm_blink(self, duration, frequency):
"""Blinks the whole screen (red-black). Duration in seconds, frequency in Hertz""" """Blinks the whole screen (red-black). Duration in seconds, frequency in Hertz"""
def output(duration, frequency): def output(duration, frequency):
self.set_brightness(value=1)
duration = int(duration) duration = int(duration)
frequency = int(frequency) frequency = int(frequency)
n = duration * frequency / 2 n = duration * frequency / 2

@ -2,16 +2,14 @@ import time
import datetime import datetime
from threading import Thread from threading import Thread
import clock.main
import bot.main
class ModuleWrapper(): class ModuleWrapper():
"""Wrapper for the CLOCK-functionality""" """Wrapper for the CLOCK-functionality"""
def __init__(self): def __init__(self, bot_module, clock_module):
"""""" """"""
print("Initializing clock-functionality") print("Initializing clock-functionality")
self.clock = clock.main.ClockFace() self.clock = clock_module
self.bot = bot.main.ChatBot("Clockbot","1.1",{}) self.bot = bot_module
self.time_thread = Thread(target=self.mainloop) self.time_thread = Thread(target=self.mainloop)
self.time_thread.start() self.time_thread.start()
self.weather = "" self.weather = ""
@ -48,10 +46,11 @@ class ModuleWrapper():
if d.total_seconds() >= 3*3600: if d.total_seconds() >= 3*3600:
prev_weather_time = datetime.datetime.now() prev_weather_time = datetime.datetime.now()
weather = self.bot.bot_show_weather(["zurich"]) weather = self.bot.bot_show_weather(["zurich"])
offset = weather.find("</b>") + 6 l1 = weather[:weather.find("\n")]
weather = weather[offset:] l1 = l1.replace("<b>Today:</b> ","")
weather = weather[:weather.find(":")] l1 = l1.replace (":","")
self.weather = weather self.weather = l1
prev_time = datetime.datetime.now().strftime("%H:%M") prev_time = datetime.datetime.now().strftime("%H:%M")
self.clock.set_face(self.categories[self.weather]) self.clock.set_face(self.categories[self.weather])

@ -1,28 +1,42 @@
# functionality
import bot.main
import clock.main
# wrapper
import clock_wrapper import clock_wrapper
import bot_wrapper import bot_wrapper
# misc.
from threading import Thread from threading import Thread
class Launcher(): class Launcher():
"""Launches all other submodules""" """Launches all other submodules"""
def __init__(self): def __init__(self):
""""""
self.bot_module = bot.main.ChatBot("ChatterBot", "2.0")
self.clock_module = clock.main.ClockFace()
self.threads = [] self.threads = []
self.threads.append(Thread(target=self.chatbot)) self.threads.append(Thread(target=self.chatbot))
self.threads.append(Thread(target=self.clock)) self.threads.append(Thread(target=self.clock))
for i in self.threads: for i in self.threads:
i.start() i.start()
def clock(self): def clock(self):
print("Launching clock-functionality") print("Launching clock-functionality")
self.clock = clock_wrapper.ModuleWrapper() self.clock = clock_wrapper.ModuleWrapper(self.bot_module, self.clock_module)
def chatbot(self): def chatbot(self):
print("Launching bot-functionality") print("Launching bot-functionality")
self.bot = bot_wrapper.ModuleWrapper() self.bot = bot_wrapper.ModuleWrapper(self.bot_module, self.clock_module)
########################################################################
## Aand liftoff!
Launcher() Launcher()