2020-11-15 17:40:18 +00:00
|
|
|
import asyncio
|
2020-12-20 01:10:19 +00:00
|
|
|
import multiprocessing
|
2020-12-20 18:52:27 +00:00
|
|
|
import queue
|
2021-02-14 13:23:51 +00:00
|
|
|
from typing import Dict, List, Optional
|
2020-11-15 17:40:18 +00:00
|
|
|
import websockets
|
|
|
|
import json
|
|
|
|
|
|
|
|
baps_clients = set()
|
2021-02-14 13:23:51 +00:00
|
|
|
channel_to_q: List[multiprocessing.Queue]
|
2020-12-20 18:52:27 +00:00
|
|
|
webstudio_to_q: List[multiprocessing.Queue]
|
2021-02-14 13:23:51 +00:00
|
|
|
server_name: str
|
2020-11-15 17:40:18 +00:00
|
|
|
|
|
|
|
|
2020-12-20 01:10:19 +00:00
|
|
|
|
2020-11-15 17:40:18 +00:00
|
|
|
async def websocket_handler(websocket, path):
|
|
|
|
baps_clients.add(websocket)
|
2020-11-15 17:48:05 +00:00
|
|
|
await websocket.send(json.dumps({"message": "Hello", "serverName": server_name}))
|
2020-11-15 17:40:18 +00:00
|
|
|
print("New Client: {}".format(websocket))
|
2020-12-20 18:52:27 +00:00
|
|
|
for channel in channel_to_q:
|
|
|
|
channel.put("STATUS")
|
2020-11-15 17:40:18 +00:00
|
|
|
|
2020-12-20 01:10:19 +00:00
|
|
|
async def handle_from_webstudio():
|
|
|
|
try:
|
|
|
|
async for message in websocket:
|
|
|
|
data = json.loads(message)
|
|
|
|
channel = int(data["channel"])
|
2021-02-14 00:29:47 +00:00
|
|
|
print(data)
|
2020-12-20 01:10:19 +00:00
|
|
|
if "command" in data.keys():
|
|
|
|
if data["command"] == "PLAY":
|
|
|
|
channel_to_q[channel].put("PLAY")
|
|
|
|
elif data["command"] == "PAUSE":
|
|
|
|
channel_to_q[channel].put("PAUSE")
|
|
|
|
elif data["command"] == "UNPAUSE":
|
|
|
|
channel_to_q[channel].put("UNPAUSE")
|
|
|
|
elif data["command"] == "STOP":
|
|
|
|
channel_to_q[channel].put("STOP")
|
|
|
|
elif data["command"] == "SEEK":
|
|
|
|
channel_to_q[channel].put("SEEK:" + str(data["time"]))
|
|
|
|
elif data["command"] == "LOAD":
|
|
|
|
channel_to_q[channel].put("LOAD:" + str(data["weight"]))
|
|
|
|
elif data["command"] == "ADD":
|
|
|
|
if "managedId" in data["newItem"].keys() and isinstance(data["newItem"]["managedId"], str):
|
|
|
|
if data["newItem"]["managedId"].startswith("managed"):
|
|
|
|
managed_id = int(data["newItem"]["managedId"].split(":")[1])
|
|
|
|
else:
|
|
|
|
managed_id = int(data["newItem"]["managedId"])
|
|
|
|
else:
|
|
|
|
managed_id = None
|
|
|
|
new_item: Dict[str, any] = {
|
|
|
|
"channelWeight": int(data["newItem"]["weight"]),
|
|
|
|
"filename": None,
|
|
|
|
"title": data["newItem"]["title"],
|
|
|
|
"artist": data["newItem"]["artist"] if "artist" in data["newItem"].keys() else None,
|
|
|
|
"timeslotItemId": int(data["newItem"]["timeslotItemId"]) if "timeslotItemId" in data["newItem"].keys() and data["newItem"]["timeslotItemId"] != None else None,
|
|
|
|
"trackId": int(data["newItem"]["trackId"]) if "trackId" in data["newItem"].keys() and data["newItem"]["trackId"] != None else None,
|
2020-12-20 18:52:27 +00:00
|
|
|
"recordId": int(data["newItem"]["trackId"]) if "trackId" in data["newItem"].keys() and data["newItem"]["trackId"] != None else None,
|
2020-12-20 01:10:19 +00:00
|
|
|
"managedId": managed_id
|
|
|
|
}
|
|
|
|
channel_to_q[channel].put("ADD:" + json.dumps(new_item))
|
|
|
|
elif data["command"] == "REMOVE":
|
|
|
|
channel_to_q[channel].put("REMOVE:" + str(data["weight"]))
|
|
|
|
|
|
|
|
await asyncio.wait([conn.send(message) for conn in baps_clients])
|
|
|
|
|
|
|
|
except websockets.exceptions.ConnectionClosedError as e:
|
|
|
|
print("RIP {}, {}".format(websocket, e))
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
print("Exception", e)
|
|
|
|
|
|
|
|
finally:
|
|
|
|
baps_clients.remove(websocket)
|
|
|
|
|
|
|
|
async def handle_to_webstudio():
|
|
|
|
while True:
|
2020-12-20 18:52:27 +00:00
|
|
|
for channel in range(len(webstudio_to_q)):
|
2020-12-20 01:10:19 +00:00
|
|
|
try:
|
2020-12-20 18:52:27 +00:00
|
|
|
message = webstudio_to_q[channel].get_nowait()
|
2021-02-14 00:29:47 +00:00
|
|
|
command = message.split(":")[0]
|
|
|
|
print("Websocket Out:", command)
|
|
|
|
if command == "STATUS":
|
|
|
|
try:
|
|
|
|
message = message.split("OKAY:")[1]
|
|
|
|
message = json.loads(message)
|
|
|
|
except:
|
|
|
|
continue
|
|
|
|
elif command == "POS":
|
|
|
|
message = message.split(":")[1]
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
|
2020-12-20 01:10:19 +00:00
|
|
|
data = json.dumps({
|
2021-02-14 00:29:47 +00:00
|
|
|
"command": command,
|
2020-12-20 18:52:27 +00:00
|
|
|
"data": message,
|
|
|
|
"channel": channel
|
2020-12-20 01:10:19 +00:00
|
|
|
})
|
|
|
|
await asyncio.wait([conn.send(data) for conn in baps_clients])
|
2020-12-20 18:52:27 +00:00
|
|
|
except queue.Empty:
|
2020-12-20 01:10:19 +00:00
|
|
|
pass
|
2021-02-14 13:23:51 +00:00
|
|
|
except Exception as e:
|
|
|
|
raise e
|
2020-12-20 01:10:19 +00:00
|
|
|
await asyncio.sleep(0.01)
|
|
|
|
|
|
|
|
from_webstudio = asyncio.create_task(handle_from_webstudio())
|
2020-12-20 18:52:27 +00:00
|
|
|
to_webstudio = asyncio.create_task(handle_to_webstudio())
|
2020-11-15 17:40:18 +00:00
|
|
|
|
2020-12-20 01:10:19 +00:00
|
|
|
try:
|
2020-12-20 18:52:27 +00:00
|
|
|
await asyncio.gather(from_webstudio, to_webstudio)
|
2020-11-15 17:40:18 +00:00
|
|
|
finally:
|
2020-12-20 01:10:19 +00:00
|
|
|
from_webstudio.cancel()
|
2020-12-20 18:52:27 +00:00
|
|
|
to_webstudio.cancel()
|
2020-11-15 17:40:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WebsocketServer:
|
|
|
|
|
2020-12-20 01:10:19 +00:00
|
|
|
def __init__(self, in_q, out_q, state):
|
2020-11-15 17:40:18 +00:00
|
|
|
global channel_to_q
|
2020-12-20 18:52:27 +00:00
|
|
|
global webstudio_to_q
|
2020-11-15 17:40:18 +00:00
|
|
|
channel_to_q = in_q
|
2020-12-20 18:52:27 +00:00
|
|
|
webstudio_to_q = out_q
|
2020-11-15 17:40:18 +00:00
|
|
|
|
2020-11-15 17:48:05 +00:00
|
|
|
global server_name
|
|
|
|
server_name = state.state["server_name"]
|
|
|
|
|
2020-11-15 17:40:18 +00:00
|
|
|
websocket_server = websockets.serve(websocket_handler, state.state["host"], state.state["ws_port"])
|
|
|
|
|
|
|
|
asyncio.get_event_loop().run_until_complete(websocket_server)
|
|
|
|
asyncio.get_event_loop().run_forever()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
print("Don't do this")
|