Adjusted clock commands for clock backend
This commit is contained in:
		@@ -11,7 +11,7 @@ Just like AIO-coolers, this little program aims to tackle many problems at once.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### Chatbot
 | 
			
		||||
Periodically calls the telegram api and reacts to sent commands. Also handles basic calls to the hardware: it allows you to control certain aspects of the clock.
 | 
			
		||||
Periodically calls the telegram api and reacts to sent commands. Also handles basic calls to the hardware: it allows you to control certain aspects of the leds (TODO: implement this correctly)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### Clock
 | 
			
		||||
@@ -30,5 +30,3 @@ This program makes use of git submodules, namely `sql_as_rest_api`. This implies
 | 
			
		||||
* Enter the repo
 | 
			
		||||
* Type `git submodule init` which creates a `.gitmodules` file
 | 
			
		||||
* Type `git submodule update` which fetches the newest version of these submodules
 | 
			
		||||
 | 
			
		||||
TODO Describe dev process
 | 
			
		||||
@@ -9,10 +9,10 @@ MESSAGE, WAKE, ALARM, IMAGE, ART = range(3,8)
 | 
			
		||||
 | 
			
		||||
class Clock(BotFunc):
 | 
			
		||||
    """pass on commands to clock-module"""
 | 
			
		||||
    def __init__(self, prst, clock_module, art_api):
 | 
			
		||||
        super().__init__(prst)
 | 
			
		||||
    def __init__(self, db_utils, clock_module, art_api):
 | 
			
		||||
        super().__init__(db_utils)
 | 
			
		||||
        self.clock = clock_module
 | 
			
		||||
        self.api_art = art_api
 | 
			
		||||
        self.art_api = art_api
 | 
			
		||||
 | 
			
		||||
    def create_handler(self):
 | 
			
		||||
        handler = ConversationHandler(
 | 
			
		||||
@@ -100,46 +100,36 @@ class Clock(BotFunc):
 | 
			
		||||
    def exec_wake_light(self, update: Update, context: CallbackContext) -> None:
 | 
			
		||||
        duration = update.message.text
 | 
			
		||||
 | 
			
		||||
        def output(duration):
 | 
			
		||||
            self.clock.set_brightness(value=1)
 | 
			
		||||
            start_color = numpy.array([153, 0, 51])
 | 
			
		||||
            end_color = numpy.array([255, 255, 0])
 | 
			
		||||
            col_show = numpy.zeros((*self.clock.shape, 3))
 | 
			
		||||
            col_show[:,:,...] = start_color
 | 
			
		||||
        matrices = []
 | 
			
		||||
        start_color = numpy.array([153, 0, 51])
 | 
			
		||||
        end_color = numpy.array([255, 255, 0])
 | 
			
		||||
        col_show = numpy.zeros((*self.clock.MOP.shape, 3))
 | 
			
		||||
        col_show[:,:,...] = start_color
 | 
			
		||||
 | 
			
		||||
            gradient = end_color - start_color
 | 
			
		||||
            # 20 steps should be fine => sleep_time = duration / 20
 | 
			
		||||
            for i in range(20):
 | 
			
		||||
                ct = i/20 * gradient
 | 
			
		||||
                col_show[:,:,...] = [int(x) for x in ct+start_color]
 | 
			
		||||
                self.clock.IO.put(col_show)
 | 
			
		||||
                time.sleep(int(duration) / 20)
 | 
			
		||||
        gradient = end_color - start_color
 | 
			
		||||
        # steps are shown at a frequency of ?? frames / second =>
 | 
			
		||||
        for i in range(duration * 2): # / 0.5
 | 
			
		||||
            ct = i/20 * gradient
 | 
			
		||||
            col_show[:,:,...] = [int(x) for x in ct+start_color]
 | 
			
		||||
            matrices.append(col_show)
 | 
			
		||||
 | 
			
		||||
        self.clock.run(output,(duration,))
 | 
			
		||||
        self.clock.out.queue.append({"matrices" : matrices})
 | 
			
		||||
        return ConversationHandler.END
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def exec_alarm_blink(self, update: Update, context: CallbackContext) -> None:
 | 
			
		||||
        duration = self.additional_argument
 | 
			
		||||
        frequency = update.message.text
 | 
			
		||||
 | 
			
		||||
        def output(duration, frequency):
 | 
			
		||||
            self.clock.set_brightness(value=1)
 | 
			
		||||
            duration =  int(duration)
 | 
			
		||||
            frequency = int(frequency)
 | 
			
		||||
            n = duration * frequency / 2
 | 
			
		||||
            empty = numpy.zeros((*self.clock.shape,3))
 | 
			
		||||
            red = empty.copy()
 | 
			
		||||
            red[...,0] = 255
 | 
			
		||||
            for i in range(int(n)):
 | 
			
		||||
                self.clock.IO.put(red)
 | 
			
		||||
                time.sleep(1/frequency)
 | 
			
		||||
                self.clock.IO.put(empty)
 | 
			
		||||
                time.sleep(1/frequency)
 | 
			
		||||
        matrices = []
 | 
			
		||||
        duration =  int(duration * 2)
 | 
			
		||||
        empty = numpy.zeros((*self.clock.MOP.shape,3))
 | 
			
		||||
        red = numpy.ones_like(empty) * 255
 | 
			
		||||
 | 
			
		||||
        if not(duration == 0 or frequency == 0):
 | 
			
		||||
            update.message.reply_text("Now blinking")
 | 
			
		||||
            self.clock.run(output,(duration, frequency))
 | 
			
		||||
        for _ in range(int(duration / 2)):
 | 
			
		||||
            matrices.append(red)
 | 
			
		||||
            matrices.append(empty)
 | 
			
		||||
 | 
			
		||||
        self.clock.out.queue.append({"matrices": matrices})
 | 
			
		||||
        return ConversationHandler.END
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
@@ -166,11 +156,9 @@ class Clock(BotFunc):
 | 
			
		||||
        t = img.resize((width, height),box=(0,0,width*scale,height*scale))
 | 
			
		||||
        a = numpy.asarray(t)
 | 
			
		||||
        
 | 
			
		||||
        def output(image, duration):
 | 
			
		||||
            self.clock.IO.put(image)
 | 
			
		||||
            time.sleep(int(duration) * 60)
 | 
			
		||||
        matrices = [a for _ in range(2*60*duration)]
 | 
			
		||||
 | 
			
		||||
        self.clock.run(output,(a, duration))
 | 
			
		||||
        self.clock.out.queue.append({"matrices": matrices})
 | 
			
		||||
        return ConversationHandler.END
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -187,7 +175,7 @@ class Clock(BotFunc):
 | 
			
		||||
        
 | 
			
		||||
        def output(number, duration):
 | 
			
		||||
            for i in range(number):
 | 
			
		||||
                img = self.api_art.get_random_art() # returns an PIL.Image object
 | 
			
		||||
                img = self.art_api.get_random_art() # returns an PIL.Image object
 | 
			
		||||
                im_height = img.height
 | 
			
		||||
                im_width = img.width
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										33
									
								
								bot/main.py
									
									
									
									
									
								
							
							
						
						
									
										33
									
								
								bot/main.py
									
									
									
									
									
								
							@@ -16,12 +16,12 @@ class ChatBot():
 | 
			
		||||
                -> prst:dict - persistence (overloaded dict that writes to json file)
 | 
			
		||||
                -> logger - logging object to unify log messages
 | 
			
		||||
        """
 | 
			
		||||
        # added by the launcher, we have self.modules (dict) and persistence and db
 | 
			
		||||
        # added by the launcher, we have self.modules (dict) and persistence and db_utils
 | 
			
		||||
 | 
			
		||||
        self.name = name
 | 
			
		||||
        self.version = version
 | 
			
		||||
        
 | 
			
		||||
        # Import submodules
 | 
			
		||||
        # Import apis
 | 
			
		||||
        self.api_weather = api.weather.WeatherFetch(api.keys.weather_api)
 | 
			
		||||
        self.api_reddit = api.reddit.RedditFetch(api.keys.reddit_api)
 | 
			
		||||
        self.api_search = api.search.WebSearch()
 | 
			
		||||
@@ -30,37 +30,38 @@ class ChatBot():
 | 
			
		||||
 | 
			
		||||
        self.telegram = Updater(api.keys.telegram_api, use_context=True)
 | 
			
		||||
        self.dispatcher = self.telegram.dispatcher
 | 
			
		||||
        self.commands = commands
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        
 | 
			
		||||
    def add_commands(self):
 | 
			
		||||
        # Mark modules as available
 | 
			
		||||
        db = self.db_utils
 | 
			
		||||
        self.help_module = self.commands.help.Help(db)
 | 
			
		||||
        # self.help_module = commands.help.Help(db)
 | 
			
		||||
        self.sub_modules = {
 | 
			
		||||
            "weather": self.commands.weather.Weather(self.api_weather, db),
 | 
			
		||||
            "help" : self.help_module,
 | 
			
		||||
            "status" : self.commands.status.Status(self.name, self.version, db),
 | 
			
		||||
            "zvv" : self.commands.zvv.Zvv(db),
 | 
			
		||||
            "list" : self.commands.lists.Lists(db),
 | 
			
		||||
            "weather": commands.weather.Weather(self.api_weather, db),
 | 
			
		||||
            "help" : commands.help.Help(db),
 | 
			
		||||
            "status" : commands.status.Status(self.name, self.version, db),
 | 
			
		||||
            "zvv" : commands.zvv.Zvv(db),
 | 
			
		||||
            "list" : commands.lists.Lists(db),
 | 
			
		||||
            # "alias" : commands.alias.Alias(self.dispatcher, db),
 | 
			
		||||
            "joke" : self.commands.reddit.Joke(self.api_reddit, db),
 | 
			
		||||
            "meme" : self.commands.reddit.Meme(self.api_reddit, db),
 | 
			
		||||
            # "news" : self.commands.reddit.News(self.api_reddit, db),
 | 
			
		||||
            "search" : self.commands.search.Search(self.api_search, db),
 | 
			
		||||
            "joke" : commands.reddit.Joke(self.api_reddit, db),
 | 
			
		||||
            "meme" : commands.reddit.Meme(self.api_reddit, db),
 | 
			
		||||
            "search" : commands.search.Search(self.api_search, db),
 | 
			
		||||
            "clock" : commands.clock.Clock(self.api_art, self.modules["clock"], db),
 | 
			
		||||
            # ...
 | 
			
		||||
            "plaintext" : self.commands.plaintext.Plain(db) # for handling non-command messages that should simply contribute to statistics
 | 
			
		||||
            "plaintext" : commands.plaintext.Plain(db)
 | 
			
		||||
            # for handling non-command messages that should simply contribute to statistics
 | 
			
		||||
            }
 | 
			
		||||
        # must be a class that has a method create_handler
 | 
			
		||||
    
 | 
			
		||||
        for k in self.sub_modules:
 | 
			
		||||
            self.dispatcher.add_handler(self.sub_modules[k].create_handler())
 | 
			
		||||
 | 
			
		||||
        self.help_module.add_commands(self.sub_modules)
 | 
			
		||||
        # self.help_module.add_commands(self.sub_modules)
 | 
			
		||||
        self.sub_modules["help"].add_commands(self.sub_modules)
 | 
			
		||||
 | 
			
		||||
    	
 | 
			
		||||
    def start(self):
 | 
			
		||||
        self.sub_modules = {"clock" : self.commands.clock.Clock(self.db_utils, self.modules["clock"], self.api_art)}
 | 
			
		||||
        self.add_commands()
 | 
			
		||||
        self.telegram.start_polling(
 | 
			
		||||
            poll_interval=0.2,
 | 
			
		||||
 
 | 
			
		||||
@@ -4,9 +4,10 @@ from broadcast import b_in
 | 
			
		||||
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
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										12
									
								
								server.py
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								server.py
									
									
									
									
									
								
							@@ -2,14 +2,12 @@
 | 
			
		||||
from bot import main
 | 
			
		||||
from clock import c_back
 | 
			
		||||
from broadcast import b_out 
 | 
			
		||||
# from dashboard import d_out
 | 
			
		||||
 | 
			
		||||
import launcher
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
import logging
 | 
			
		||||
import platform
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if platform.uname().node == "ArchSpectre":
 | 
			
		||||
    # development
 | 
			
		||||
    logging.basicConfig(
 | 
			
		||||
@@ -28,19 +26,13 @@ class BroadcastLauncher(launcher.Launcher):
 | 
			
		||||
    """Launcher for all server-side modules. The hard-computations"""
 | 
			
		||||
    def __init__(self):
 | 
			
		||||
        
 | 
			
		||||
        self.bot_module = main.ChatBot(name="Norbit", version="4.1a") # ??? 
 | 
			
		||||
        self.bot_module = main.ChatBot(name="Norbit", version="4.1a") 
 | 
			
		||||
        self.clock_backend_module = c_back.ClockBackend() # threaded through threading.Timer
 | 
			
		||||
        self.broadcast_module = b_out.BroadcastUpdates(port="1111") # Thread
 | 
			
		||||
        # self.dashboard_module = d_out.DashBoard(port="80") # ??? threaded as Thread
 | 
			
		||||
 | 
			
		||||
        # "sensors" : self.sensors,
 | 
			
		||||
        # "bot" : self.bot_module,
 | 
			
		||||
        # "clock" : self.clock_module,
 | 
			
		||||
        # "dashboard" : self.dashboard_module,
 | 
			
		||||
        super().__init__(
 | 
			
		||||
            bot = self.bot_module,
 | 
			
		||||
            clock = self.clock_backend_module,
 | 
			
		||||
            # dashboard = self.dashboard_module,
 | 
			
		||||
            broadcast = self.broadcast_module
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user