-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
102 lines (87 loc) · 2.57 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from pythonosc.osc_server import AsyncIOOSCUDPServer
from pythonosc.dispatcher import Dispatcher
from aiohttp import web
import asyncio
import os
import signal
import functools
from sys import exit
script_path = os.path.abspath(os.path.dirname(__file__))
index_path = os.path.join(script_path, 'public', 'index.html')
public_path = os.path.join(script_path, 'public')
oscip = "0.0.0.0"
oscp = 3333
PRINT_DUR = 0.25
WS_DUR = 0
webhost = os.getenv("HOST", "127.0.0.1")
webport = int(os.getenv("PORT", 8080))
ws = None
to_send = None
to_print = None
async def add_to_print(cur):
global to_print
await to_print.put(cur)
async def add_to_send(cur):
global to_send
await to_send.put(cur)
def hard_exit(signame, loop):
loop.stop()
exit(0)
def osc_to_ws(addr, *args):
loop = asyncio.get_event_loop()
loop.create_task(add_to_print(f"{addr}: {args}"))
if ws != None:
#print(f"{addr}: {args}")
retstr = addr
for x in args:
retstr += f" {x}"
loop.create_task(add_to_send(retstr))
else:
loop.create_task(add_to_print("no ws"))
dispatcher = Dispatcher()
dispatcher.map("/*", osc_to_ws)
async def ws_handler(req):
global ws
global to_send
ws = web.WebSocketResponse()
await ws.prepare(req)
while True:
cur = await to_send.get()
await ws.send_str(cur)
await asyncio.sleep(WS_DUR)
async def send_loop():
global ws
global to_print
while True:
while not to_print.empty():
cur = await to_print.get()
print(cur)
await asyncio.sleep(PRINT_DUR)
async def root_router(req):
return web.FileResponse(index_path)
async def web_server():
app = web.Application()
app.add_routes([web.get('/ws', ws_handler), web.get('/', root_router), web.static('/', public_path)])
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, webhost, webport)
await site.start()
while True:
await asyncio.sleep(3600)
async def osc_server(loop):
server = AsyncIOOSCUDPServer((oscip, oscp), dispatcher, loop)
tport, protocol = await server.create_serve_endpoint()
await send_loop()
tport.close()
async def main():
global to_send
global to_print
to_send = asyncio.Queue()
to_print = asyncio.Queue()
loop = asyncio.get_event_loop()
for signame in {'SIGINT', 'SIGTERM'}:
loop.add_signal_handler(
getattr(signal, signame),
functools.partial(hard_exit, signame, loop))
await asyncio.gather(osc_server(loop), web_server())
asyncio.run(main())