From aa1e5255f27a3b12b27679dc2c933383a46a52c5 Mon Sep 17 00:00:00 2001
From: johannes walcher <johannes.walcher@stusta.de>
Date: Thu, 26 Jul 2018 23:05:49 +0200
Subject: [PATCH] Optimized party mode

---
 babel/babel.py         |  20 ++++---
 hackerman/hackerman.py | 129 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 139 insertions(+), 10 deletions(-)

diff --git a/babel/babel.py b/babel/babel.py
index 6f6d7f3..b09ac60 100644
--- a/babel/babel.py
+++ b/babel/babel.py
@@ -18,14 +18,13 @@ class Babel:
         self.ledstrip_states = [[0, 0, 0, 0], [0, 0, 0, 0]]
         self.espids = ['a9495a00', 'c14e5a00']
 
-        # TODO create this mapping:
         # mapps from (color, id) ==> (self.ledstrip indexes)
         self.idxpair = {
             # The first one has cold and warm swapped...
-            ('c', 1):(0, 1),
-            ('w', 1):(0, 0),
-            ('c', 2):(0, 3),
-            ('w', 2):(0, 2),
+            ('w', 2):(0, 0),
+            ('c', 2):(0, 1),
+            ('w', 1):(0, 2),
+            ('c', 1):(0, 3),
             # This one has cold and warm right
             ('c', 3):(1, 0),
             ('w', 3):(1, 1),
@@ -72,18 +71,23 @@ class Babel:
                             self.ledstrip_states[idx[0]][idx[1]] = int(value)
 
                 elif len(tmp) == 3 and tmp[1] in ('c', 'w') and abs(int(tmp[2])) <= 4:
-                    idx = self.idxpair[(tmp[1],abs(int(tmp[2])))]
+                    idx = self.idxpair[(tmp[1], abs(int(tmp[2])))]
                     group_changed |= self.ledstrip_states[idx[0]][idx[1]] != int(value)
                     self.ledstrip_states[idx[0]][idx[1]] = int(value)
 
         self.hbf.log.info("Done mapping: ")
         self.hbf.log.info(self.ledstrip_states)
         if group_changed:
-            for idx, e in enumerate(self.espids):
-                msg[e] = self.ledstrip_states[idx]
+            for idx, ledidx in enumerate(self.espids):
+                msg[ledidx] = self.ledstrip_states[idx]
         await self.hbf.publish('/haspa/led', msg)
+        print("MSG: ", msg)
 
     async def command_requestinfo(self, client, msg, _):
+        """
+        Request details about configured led mappings
+        """
+        del client, msg
         await self.hbf.publish('/haspa/power/info', {
             'documentation':'too lazy to implement'
         })
diff --git a/hackerman/hackerman.py b/hackerman/hackerman.py
index 1747da0..f3a67e5 100644
--- a/hackerman/hackerman.py
+++ b/hackerman/hackerman.py
@@ -1,4 +1,6 @@
 import asyncio
+import requests
+import random
 
 from hauptbahnhof import Hauptbahnhof
 
@@ -14,6 +16,7 @@ class Hackerman:
         self.loop = loop
         self.hbf = Hauptbahnhof(loop)
         self.hbf.subscribe('/haspa/status', self.command_status)
+        self.hbf.subscribe('/haspa/action', self.command_action)
 
     async def teardown(self):
         await self.hbf.teardown()
@@ -30,13 +33,135 @@ class Hackerman:
                         'table':1023,
                         'fan':1023,
                         'ledstrip':400,
-                        })
+                    })
+                    await self.hbf.publish('/haspa/music/control', {
+                        'play': True
+                    })
                 elif message['haspa'] in ['close', 'zu', 'closed']:
                     await self.hbf.publish('/haspa/power', {
                         'table':0,
                         'fan':0,
                         'ledstrip':0,
                         'alarm':0,
-                        })
+                    })
+                    await self.hbf.publish('/haspa/music/control', {
+                        'play': False
+                    })
         except KeyError:
             raise # because - fuck you sender! i will die now, silently.
+
+    async def command_action(self, client, message, _):
+        """ Handle actions like alarm or party """
+        del client
+        if 'action' in message:
+            if message['action'] == 'alarm':
+                await self.hbf.publish('/haspa/power', {'alarm':1023})
+                await asyncio.sleep(2)
+                await self.hbf.publish('/haspa/power', {'alarm':0})
+
+            elif message['action'] == 'party':
+                await self.hbf.publish('/haspa/power', {
+                    'alarm':0,
+                    'table':0,
+                    'ledstrip': 0
+                })
+                delay = 0.05
+
+                sounds = [
+                    ('56', 3.5), # führer
+                    ('97', 4.7), # sonnenschein
+                    ('63', 5),   #epische musik
+                    ('110', 3.7),#dota
+                    ('113', 9),  # skrillex
+                ]
+
+                sound = random.choice(sounds)
+
+
+                if sound[0] == '97':
+                    await asyncio.sleep(1)
+                    for i in range(0, 300):
+                        if i == 100:
+                            requests.get("https://bot.stusta.de/set/" + sound[0])
+
+                        await self.hbf.publish('/haspa/power', {
+                            'ledstrip-w': i * 10/3
+                        })
+                        await asyncio.sleep(0.01)
+                elif sound[0] == '113':
+                    requests.get("https://bot.stusta.de/set/" + sound[0])
+                    for i in range(2):
+                        await asyncio.sleep(1.5)
+                        await self.hbf.publish('/haspa/power', {
+                            'ledstrip-c-1': 1023,
+                            'ledstrip-c-3': 1023,
+                            'alarm':1023
+                        })
+                        await asyncio.sleep(0.01)
+                        await self.hbf.publish('/haspa/power', {
+                            'ledstrip-c-1': 0,
+                            'ledstrip-c-3': 0
+                        })
+
+                        for o in range(40):
+                            await self.hbf.publish('/haspa/power', {
+                                'ledstrip-c-2': 20 * o,
+                                'ledstrip-w-4': 20 * o
+                            })
+                            await asyncio.sleep(0.01)
+                        await self.hbf.publish('/haspa/power', {
+                            'Ledstrip-c-2': 0,
+                            'ledstrip-w-4': 0
+                        })
+
+                        #await asyncio.sleep(1.49)
+                        for o in range(20):
+                            await self.hbf.publish('/haspa/power', {
+                                'ledstrip-c-2': 1023,
+                                'ledstrip-w-4': 0
+                            })
+                            await asyncio.sleep(0.01)
+                            await self.hbf.publish('/haspa/power', {
+                                'ledstrip-c-2': 0,
+                                'ledstrip-w-4': 1023
+                            })
+                            await asyncio.sleep(0.01)
+
+                else:
+                    requests.get("https://bot.stusta.de/set/" + sound[0])
+                    for _ in range(int(sound[1]/(delay * 4))):
+                        await self.hbf.publish('/haspa/power', {
+                            'ledstrip-c-1': 0,
+                            'ledstrip-c-2': 1023
+                        })
+                        print("1->2")
+                        await asyncio.sleep(delay)
+
+                        await self.hbf.publish('/haspa/power', {
+                            'ledstrip-c-2': 0,
+                            'ledstrip-c-3': 1023
+                        })
+                        print("2->3")
+                        await asyncio.sleep(delay)
+
+                        await self.hbf.publish('/haspa/power', {
+                            'ledstrip-c-3': 0,
+                            'ledstrip-c-4': 1023
+                        })
+                        print("3->4")
+                        await asyncio.sleep(delay)
+
+                        await self.hbf.publish('/haspa/power', {
+                            'ledstrip-c-4': 0,
+                            'ledstrip-c-1': 1023
+                        })
+                        print("4->1")
+                        await asyncio.sleep(delay)
+
+                        print("round")
+
+                await self.hbf.publish('/haspa/power', {
+                    'alarm':0,
+                    'table':1023,
+                    'ledstrip': 500
+                })
-- 
GitLab