From a9dcae38fcab11dbc6202177fc84b99a3c5b245a Mon Sep 17 00:00:00 2001 From: johannes walcher <johannes.walcher@stusta.de> Date: Thu, 3 May 2018 15:55:21 +0200 Subject: [PATCH] added hackerman: hackerspace manager --- hackerman/__init__.py | 0 hackerman/__main__.py | 20 +++++++++++++++++++ hackerman/hackerman.py | 41 ++++++++++++++++++++++++++++++++++++++ hackerman/test.py | 45 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 hackerman/__init__.py create mode 100644 hackerman/__main__.py create mode 100644 hackerman/hackerman.py create mode 100644 hackerman/test.py diff --git a/hackerman/__init__.py b/hackerman/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hackerman/__main__.py b/hackerman/__main__.py new file mode 100644 index 0000000..d8dc1e1 --- /dev/null +++ b/hackerman/__main__.py @@ -0,0 +1,20 @@ +import asyncio + +from hackerman.hackerman import Hackerman + +def main(): + """ + Actually start the shit + """ + loop = asyncio.get_event_loop() + hackerman = Hackerman(loop=loop) + loop.set_debug(True) + try: + loop.run_forever() + except KeyboardInterrupt: + pass + + loop.run_until_complete(hackerman.teardown()) + +if __name__ == "__main__": + main() diff --git a/hackerman/hackerman.py b/hackerman/hackerman.py new file mode 100644 index 0000000..17f3c9f --- /dev/null +++ b/hackerman/hackerman.py @@ -0,0 +1,41 @@ +import asyncio + +from hauptbahnhof import Hauptbahnhof + +class Hackerman: + """ + Scan the local hackerspace network for new and unknown devices to send back a result + """ + + def __init__(self, loop=None): + if not loop: + loop = asyncio.get_event_loop() + + self.loop = loop + self.hbf = Hauptbahnhof(loop) + self.hbf.subscribe('/haspa/status', self.command_status) + + async def teardown(self): + await self.hbf.teardown() + + async def command_status(self, client, message, _): + """ + React to a status change of the hackerspace - switch the lights, ... + """ + try: + if 'haspa' in message: + if message['haspa'] == 'open': + await self.hbf.publish('/haspa/power', { + 'table':1023, + 'fan':1023, + 'ledstrip':400, + }) + elif message['haspa'] == 'closed': + await self.hbf.publish('/haspa/power', { + 'table':0, + 'fan':0, + 'ledstrip':0, + 'alarm':0, + }) + except KeyError: + raise # because - fuck you sender! i will die now, silently. diff --git a/hackerman/test.py b/hackerman/test.py new file mode 100644 index 0000000..878807b --- /dev/null +++ b/hackerman/test.py @@ -0,0 +1,45 @@ +import asyncio +from hauptbahnhof import Hauptbahnhof + +from hackerman.hackerman import Hackerman + + +messages = asyncio.Queue() +async def on_message(client, message, _): + print("Got message: %s"%message) + await messages.put(message) + + +async def test(loop): + testbf = Hauptbahnhof(loop=loop) + testbf.subscribe("/haspa/power", on_message) + + await asyncio.sleep(2) + + await testbf.publish("/haspa/status", {'haspa':'open'}) # without blacklist + + # Now everythin should be set up + msg = await asyncio.wait_for(messages.get(), 10) # wait max 10 secs + + assert(msg['table'] == 1023) + assert(msg['ledstrip'] == 400) + + try: + await testbf.teardown() + except asyncio.CancelledError: + pass + +def main(): + loop = asyncio.get_event_loop() + lib = Hackerman(loop=loop) + + result = loop.run_until_complete(test(loop)) + loop.run_until_complete(lib.teardown()) + + if result: + exit(0) + else: + exit(1) + +if __name__=="__main__": + main() -- GitLab