From 6c4dbacdef1631ea9b6ad6ed0dcc2523836a3f5f Mon Sep 17 00:00:00 2001 From: Remy Moll Date: Mon, 19 Oct 2020 12:27:59 +0200 Subject: [PATCH] works flawlessly, hehe --- bot/api/telegram.py | 5 ++++- bot/main.py | 15 +++++++++------ bot_wrapper.py | 19 ++++++++++--------- clock/api/hat/sim.py | 11 +++++++---- clock/main.py | 17 +++++++++++++---- clock_wrapper.py | 17 ++++++++--------- launcher.py | 20 +++++++++++++++++--- 7 files changed, 68 insertions(+), 36 deletions(-) diff --git a/bot/api/telegram.py b/bot/api/telegram.py index a9d2e8f..fc49763 100644 --- a/bot/api/telegram.py +++ b/bot/api/telegram.py @@ -19,6 +19,9 @@ class TelegramIO(): + def update_commands(self,commands): + self.commands = commands + ######################################################################## """Helper-Functions""" @@ -79,7 +82,7 @@ class TelegramIO(): /[command] [argument 1] [argument 2] ... """ 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 command[0] == "EXACT": self.persistence.increment("commands_executed") diff --git a/bot/main.py b/bot/main.py index 3b70e00..3850bc0 100644 --- a/bot/main.py +++ b/bot/main.py @@ -11,7 +11,7 @@ import emoji class ChatBot(): """""" - def __init__(self, name, version, hw_commands): + def __init__(self, name, version): """Inits the Bot with a few conf. vars Args: -> name:str - Name of the bot -> api_key:str - t.me api-key @@ -27,7 +27,7 @@ class ChatBot(): self.start_time = datetime.datetime.now() self.persistence.increment("reboots") - # Available commands + # Available commands. Must be manually updated! self.commands = { "help" : self.bot_show_help, "status" : self.bot_print_status, @@ -86,9 +86,12 @@ class ChatBot(): "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): @@ -211,10 +214,10 @@ class ChatBot(): """Shows a list of all commands and their description""" send_text = "BeebBop, this is " + self.name + " (V." + self.version + ")\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: send_text += "" + entry + " - " - send_text += "" + self.all_commands[entry].__doc__ + "\n\n" + send_text += "" + self.commands[entry].__doc__ + "\n\n" return send_text diff --git a/bot_wrapper.py b/bot_wrapper.py index f80b137..f1e3100 100644 --- a/bot_wrapper.py +++ b/bot_wrapper.py @@ -1,26 +1,25 @@ import time import datetime -import bot.main -import clock.main class ModuleWrapper(): """Wrapper for the BOT-functionality""" - def __init__(self): + def __init__(self, bot_module, clock_module): """""" 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 = { "blink" : self.clock.alarm_blink, "wakeup" : self.clock.wake_light, "showmessage" : self.clock.show_message, } - - self.bot = bot.main.ChatBot("ChatterBot", "2.0", self.hw_commands) - + self.bot.add_commands(self.hw_commands) self.message_loop() @@ -34,13 +33,15 @@ class ModuleWrapper(): command, params = self.bot.telegram.handle_result(result) if command != "nothing": if command in self.hw_commands: - self.react_command(command,params) + self.react_command(command,params) #hw-level else: - self.bot.react_command(command,params) + self.bot.react_command(command,params) #sw-level time.sleep(5) + def react_command(self, command, params): """""" # Oh yeah, that needs to be changed # so params is a list, and so, to pass the commands, we need to unpack it: + # should work fine self.hw_commands[command](*params) diff --git a/clock/api/hat/sim.py b/clock/api/hat/sim.py index 036d218..69e3e90 100644 --- a/clock/api/hat/sim.py +++ b/clock/api/hat/sim.py @@ -19,6 +19,7 @@ class UnicornHat(object): self.window_width = self.width * self.pixel_size self.window_height = self.height * self.pixel_size + self.brightness = 1 # Init pygame and off we go pygame.init() pygame.display.set_caption("Unicorn HAT simulator") @@ -27,7 +28,7 @@ class UnicornHat(object): 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): @@ -52,7 +53,9 @@ class UnicornHat(object): #w_y = int((self.height - 1 - y) * p + p / 2) w_y = int(i * p + p / 2) 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.filled_circle(self.screen, w_x, w_y, r, color) @@ -69,8 +72,8 @@ class UnicornHat(object): return (self.width, self.height) - def set_brightness(self, *args): - pass + def set_brightness(self, brightness): + self.brightness = brightness def rotation(self, r): diff --git a/clock/main.py b/clock/main.py index dd91b43..48bf11f 100644 --- a/clock/main.py +++ b/clock/main.py @@ -30,8 +30,10 @@ class ClockFace(object): def run(self, command, kw=()): """Checks for running threads and executes the ones in queue""" def enhanced_run(command, kw): + """""" self.output_thread = "Running " + str(command) command(*kw) + self.set_brightness() self.output_thread = "" if len(self.output_queue) != 0: n = self.output_queue.pop(0) @@ -50,7 +52,6 @@ class ClockFace(object): ### basic clock commands def set_face(self, weather): """""" - self.set_brightness() self.weather = 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)) - def set_brightness(self, overwrite=[]): + def set_brightness(self, overwrite=[],value=-1): """Checks, what brightness rules to apply""" + + if value != -1: + self.IO.output.set_brightness(value) + return + + if len(overwrite) != 0: 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)): brightness = 0.8 else: - brightness = 0.1 + brightness = 0.05 self.IO.output.set_brightness(brightness) @@ -80,6 +87,7 @@ class ClockFace(object): def wake_light(self, duration=600): """Simulates a sunris, takes one optional parameter: the duration""" def output(duration): + self.set_brightness(value=0.1) start_color = numpy.array([153, 0, 51]) end_color = numpy.array([255, 255, 0]) empty = numpy.zeros((16,32)) @@ -91,7 +99,7 @@ class ClockFace(object): ct = i/20 * gradient col = [int(x) for x in ct+start_color] self.IO.set_matrix(ones,colors=[col]) - time.sleep(duration / 20) + time.sleep(int(duration) / 20) self.run(output,(duration,)) @@ -100,6 +108,7 @@ class ClockFace(object): def alarm_blink(self, duration, frequency): """Blinks the whole screen (red-black). Duration in seconds, frequency in Hertz""" def output(duration, frequency): + self.set_brightness(value=1) duration = int(duration) frequency = int(frequency) n = duration * frequency / 2 diff --git a/clock_wrapper.py b/clock_wrapper.py index 2cb6355..dd51ed2 100644 --- a/clock_wrapper.py +++ b/clock_wrapper.py @@ -2,16 +2,14 @@ import time import datetime from threading import Thread -import clock.main -import bot.main class ModuleWrapper(): """Wrapper for the CLOCK-functionality""" - def __init__(self): + def __init__(self, bot_module, clock_module): """""" print("Initializing clock-functionality") - self.clock = clock.main.ClockFace() - self.bot = bot.main.ChatBot("Clockbot","1.1",{}) + self.clock = clock_module + self.bot = bot_module self.time_thread = Thread(target=self.mainloop) self.time_thread.start() self.weather = "" @@ -48,10 +46,11 @@ class ModuleWrapper(): if d.total_seconds() >= 3*3600: prev_weather_time = datetime.datetime.now() weather = self.bot.bot_show_weather(["zurich"]) - offset = weather.find("") + 6 - weather = weather[offset:] - weather = weather[:weather.find(":")] - self.weather = weather + l1 = weather[:weather.find("\n")] + l1 = l1.replace("Today: ","") + l1 = l1.replace (":","") + self.weather = l1 prev_time = datetime.datetime.now().strftime("%H:%M") + self.clock.set_face(self.categories[self.weather]) diff --git a/launcher.py b/launcher.py index af3df7b..ff13254 100644 --- a/launcher.py +++ b/launcher.py @@ -1,28 +1,42 @@ +# functionality +import bot.main +import clock.main +# wrapper import clock_wrapper import bot_wrapper + +# misc. from threading import Thread + + class Launcher(): """Launches all other submodules""" def __init__(self): + """""" + self.bot_module = bot.main.ChatBot("ChatterBot", "2.0") + self.clock_module = clock.main.ClockFace() + self.threads = [] self.threads.append(Thread(target=self.chatbot)) self.threads.append(Thread(target=self.clock)) - for i in self.threads: i.start() def clock(self): print("Launching clock-functionality") - self.clock = clock_wrapper.ModuleWrapper() + self.clock = clock_wrapper.ModuleWrapper(self.bot_module, self.clock_module) + def chatbot(self): print("Launching bot-functionality") - self.bot = bot_wrapper.ModuleWrapper() + self.bot = bot_wrapper.ModuleWrapper(self.bot_module, self.clock_module) +######################################################################## +## Aand liftoff! Launcher()