2020-11-01 02:35:14 +00:00
|
|
|
"""
|
|
|
|
BAPSicle Server
|
|
|
|
Next-gen audio playout server for University Radio York playout,
|
|
|
|
based on WebStudio interface.
|
|
|
|
|
|
|
|
Flask Server
|
|
|
|
|
|
|
|
Authors:
|
|
|
|
Matthew Stratford
|
|
|
|
Michael Grace
|
|
|
|
|
|
|
|
Date:
|
|
|
|
October, November 2020
|
|
|
|
"""
|
2021-03-21 13:05:33 +00:00
|
|
|
from api_handler import APIHandler
|
2021-02-14 23:58:32 +00:00
|
|
|
from controllers.mattchbox_usb import MattchBox
|
2020-10-23 20:10:32 +00:00
|
|
|
import multiprocessing
|
2020-12-20 18:52:27 +00:00
|
|
|
import queue
|
2020-12-20 01:10:19 +00:00
|
|
|
import time
|
2020-10-24 13:44:26 +00:00
|
|
|
import player
|
2021-03-15 21:35:56 +00:00
|
|
|
from flask import Flask, render_template, send_from_directory, request, jsonify, abort
|
2021-04-04 21:34:46 +00:00
|
|
|
from flask_cors import CORS
|
2020-12-19 14:57:37 +00:00
|
|
|
from typing import Any, Optional
|
2020-10-23 20:10:32 +00:00
|
|
|
import json
|
2021-04-04 21:34:46 +00:00
|
|
|
from setproctitle import setproctitle
|
2020-11-01 00:31:58 +00:00
|
|
|
import logging
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2021-04-04 21:34:46 +00:00
|
|
|
from player_handler import PlayerHandler
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2020-11-01 01:36:42 +00:00
|
|
|
from helpers.os_environment import isMacOS
|
2020-11-03 21:24:45 +00:00
|
|
|
from helpers.device_manager import DeviceManager
|
2020-10-25 01:23:24 +00:00
|
|
|
|
2020-11-04 22:38:31 +00:00
|
|
|
if not isMacOS():
|
|
|
|
# Rip, this doesn't like threading on MacOS.
|
|
|
|
import pyttsx3
|
|
|
|
|
2020-11-03 23:25:17 +00:00
|
|
|
import config
|
|
|
|
from typing import Dict, List
|
2020-11-09 00:10:36 +00:00
|
|
|
from helpers.state_manager import StateManager
|
|
|
|
from helpers.logging_manager import LoggingManager
|
2020-11-15 17:40:18 +00:00
|
|
|
from websocket_server import WebsocketServer
|
2020-11-03 23:25:17 +00:00
|
|
|
|
2021-04-04 21:34:46 +00:00
|
|
|
setproctitle("BAPSicleServer.py")
|
2020-10-30 23:14:29 +00:00
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
|
|
|
|
class BAPSicleServer:
|
2020-10-24 20:31:52 +00:00
|
|
|
def __init__(self):
|
2020-10-30 23:14:29 +00:00
|
|
|
|
2021-04-04 21:34:46 +00:00
|
|
|
startServer()
|
2020-10-24 20:31:52 +00:00
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
stopServer()
|
|
|
|
|
2020-10-26 21:25:02 +00:00
|
|
|
def get_flask(self):
|
|
|
|
return app
|
|
|
|
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2021-04-04 21:34:46 +00:00
|
|
|
default_state = {
|
|
|
|
"server_version": 0,
|
|
|
|
"server_name": "URY BAPSicle",
|
|
|
|
"host": "localhost",
|
|
|
|
"port": 13500,
|
|
|
|
"ws_port": 13501,
|
2021-04-05 21:13:53 +00:00
|
|
|
"num_channels": 3,
|
|
|
|
"ser_port": None,
|
|
|
|
"ser_connected": False,
|
2021-04-04 21:34:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
app = Flask(__name__, static_url_path="")
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
|
2021-04-07 20:13:19 +00:00
|
|
|
logger: LoggingManager
|
|
|
|
state: StateManager
|
|
|
|
|
2021-03-21 13:05:33 +00:00
|
|
|
api_from_q: queue.Queue
|
|
|
|
api_to_q: queue.Queue
|
|
|
|
|
2021-03-15 20:20:26 +00:00
|
|
|
channel_to_q: List[queue.Queue] = []
|
|
|
|
channel_from_q: List[queue.Queue] = []
|
|
|
|
ui_to_q: List[queue.Queue] = []
|
|
|
|
websocket_to_q: List[queue.Queue] = []
|
2021-03-22 00:33:14 +00:00
|
|
|
controller_to_q: List[queue.Queue] = []
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2021-04-04 21:34:46 +00:00
|
|
|
channel_p: List[multiprocessing.Process] = []
|
|
|
|
websockets_server: multiprocessing.Process
|
|
|
|
controller_handler: multiprocessing.Process
|
|
|
|
webserver: multiprocessing.Process
|
2020-11-01 01:36:42 +00:00
|
|
|
|
2020-11-09 00:10:36 +00:00
|
|
|
# General Endpoints
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
|
2020-10-24 12:47:48 +00:00
|
|
|
@app.errorhandler(404)
|
2020-12-19 14:57:37 +00:00
|
|
|
def page_not_found(e: Any):
|
2021-04-08 19:53:51 +00:00
|
|
|
data = {"ui_page": "404", "ui_title": "404"}
|
|
|
|
return render_template("404.html", data=data), 404
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/")
|
2020-10-24 02:13:02 +00:00
|
|
|
def ui_index():
|
|
|
|
data = {
|
2021-04-08 19:53:51 +00:00
|
|
|
"ui_page": "index",
|
2020-11-10 18:49:26 +00:00
|
|
|
"ui_title": "",
|
|
|
|
"server_version": config.VERSION,
|
2021-04-08 19:53:51 +00:00
|
|
|
"server_name": state.state["server_name"],
|
2020-10-24 02:13:02 +00:00
|
|
|
}
|
2021-04-08 19:53:51 +00:00
|
|
|
return render_template("index.html", data=data)
|
2020-10-24 02:13:02 +00:00
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
|
|
|
|
@app.route("/config")
|
|
|
|
def ui_config():
|
2020-10-23 21:58:53 +00:00
|
|
|
channel_states = []
|
2020-11-10 19:40:42 +00:00
|
|
|
for i in range(state.state["num_channels"]):
|
2020-10-30 00:33:00 +00:00
|
|
|
channel_states.append(status(i))
|
2020-10-23 21:58:53 +00:00
|
|
|
|
2021-04-04 22:14:30 +00:00
|
|
|
outputs = DeviceManager.getAudioOutputs()
|
2020-10-24 14:04:33 +00:00
|
|
|
|
|
|
|
data = {
|
2021-04-08 19:53:51 +00:00
|
|
|
"channels": channel_states,
|
|
|
|
"outputs": outputs,
|
|
|
|
"ui_page": "config",
|
|
|
|
"ui_title": "Config",
|
2020-10-24 14:04:33 +00:00
|
|
|
}
|
2021-04-08 19:53:51 +00:00
|
|
|
return render_template("config.html", data=data)
|
2020-10-23 21:58:53 +00:00
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
|
|
|
|
@app.route("/status")
|
|
|
|
def ui_status():
|
|
|
|
channel_states = []
|
2020-11-09 00:17:48 +00:00
|
|
|
for i in range(state.state["num_channels"]):
|
2020-10-30 00:33:00 +00:00
|
|
|
channel_states.append(status(i))
|
2020-10-23 21:58:53 +00:00
|
|
|
|
2021-04-08 20:15:15 +00:00
|
|
|
data = {"channels": channel_states,
|
|
|
|
"ui_page": "status", "ui_title": "Status"}
|
2021-04-08 19:53:51 +00:00
|
|
|
return render_template("status.html", data=data)
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
|
2020-11-09 00:17:48 +00:00
|
|
|
@app.route("/status-json")
|
|
|
|
def json_status():
|
|
|
|
channel_states = []
|
|
|
|
for i in range(state.state["num_channels"]):
|
|
|
|
channel_states.append(status(i))
|
2021-04-08 19:53:51 +00:00
|
|
|
return {"server": state.state, "channels": channel_states}
|
2020-11-09 00:17:48 +00:00
|
|
|
|
2020-11-10 19:40:42 +00:00
|
|
|
|
|
|
|
@app.route("/server")
|
|
|
|
def server_config():
|
|
|
|
data = {
|
|
|
|
"ui_page": "server",
|
|
|
|
"ui_title": "Server Config",
|
2021-04-05 21:13:53 +00:00
|
|
|
"state": state.state,
|
2021-04-08 19:53:51 +00:00
|
|
|
"ser_ports": DeviceManager.getSerialPorts(),
|
2020-11-10 19:40:42 +00:00
|
|
|
}
|
|
|
|
return render_template("server.html", data=data)
|
|
|
|
|
|
|
|
|
2021-04-05 21:13:53 +00:00
|
|
|
@app.route("/server/update", methods=["POST"])
|
|
|
|
def update_server():
|
2020-11-10 19:40:42 +00:00
|
|
|
state.update("server_name", request.form["name"])
|
|
|
|
state.update("host", request.form["host"])
|
|
|
|
state.update("port", int(request.form["port"]))
|
|
|
|
state.update("num_channels", int(request.form["channels"]))
|
2020-11-15 17:48:05 +00:00
|
|
|
state.update("ws_port", int(request.form["ws_port"]))
|
2021-04-05 21:13:53 +00:00
|
|
|
state.update("serial_port", request.form["serial_port"])
|
2021-04-08 19:53:51 +00:00
|
|
|
# stopServer()
|
2021-04-05 21:13:53 +00:00
|
|
|
return server_config()
|
2020-11-10 19:40:42 +00:00
|
|
|
|
2021-03-15 21:35:56 +00:00
|
|
|
|
|
|
|
# Get audio for UI to generate waveforms.
|
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
|
2021-03-15 21:35:56 +00:00
|
|
|
@app.route("/audiofile/<type>/<int:id>")
|
|
|
|
def audio_file(type: str, id: int):
|
|
|
|
if type not in ["managed", "track"]:
|
|
|
|
abort(404)
|
2021-04-08 19:53:51 +00:00
|
|
|
return send_from_directory("music-tmp", type + "-" + str(id) + ".mp3")
|
2021-03-15 21:35:56 +00:00
|
|
|
|
|
|
|
|
2020-11-09 00:10:36 +00:00
|
|
|
# Channel Audio Options
|
|
|
|
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
@app.route("/player/<int:channel>/play")
|
2020-12-19 14:57:37 +00:00
|
|
|
def play(channel: int):
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2021-03-22 00:33:14 +00:00
|
|
|
channel_to_q[channel].put("UI:PLAY")
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
return ui_status()
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/player/<int:channel>/pause")
|
2020-12-19 14:57:37 +00:00
|
|
|
def pause(channel: int):
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2021-03-22 00:33:14 +00:00
|
|
|
channel_to_q[channel].put("UI:PAUSE")
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
return ui_status()
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/player/<int:channel>/unpause")
|
2020-12-19 14:57:37 +00:00
|
|
|
def unPause(channel: int):
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2021-03-22 00:33:14 +00:00
|
|
|
channel_to_q[channel].put("UI:UNPAUSE")
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
return ui_status()
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/player/<int:channel>/stop")
|
2020-12-19 14:57:37 +00:00
|
|
|
def stop(channel: int):
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2021-03-22 00:33:14 +00:00
|
|
|
channel_to_q[channel].put("UI:STOP")
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
return ui_status()
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
|
2020-12-19 14:57:37 +00:00
|
|
|
@app.route("/player/<int:channel>/seek/<float:pos>")
|
|
|
|
def seek(channel: int, pos: float):
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2021-03-22 00:33:14 +00:00
|
|
|
channel_to_q[channel].put("UI:SEEK:" + str(pos))
|
2020-10-24 14:04:33 +00:00
|
|
|
|
|
|
|
return ui_status()
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
|
2020-10-23 21:58:53 +00:00
|
|
|
@app.route("/player/<int:channel>/output/<name>")
|
2020-12-19 14:57:37 +00:00
|
|
|
def output(channel: int, name: Optional[str]):
|
2021-03-22 00:33:14 +00:00
|
|
|
channel_to_q[channel].put("UI:OUTPUT:" + str(name))
|
2021-03-22 00:45:46 +00:00
|
|
|
return ui_config()
|
2020-10-23 21:58:53 +00:00
|
|
|
|
2020-11-09 00:10:36 +00:00
|
|
|
|
2020-11-03 01:07:25 +00:00
|
|
|
@app.route("/player/<int:channel>/autoadvance/<int:state>")
|
|
|
|
def autoadvance(channel: int, state: int):
|
2021-03-22 00:33:14 +00:00
|
|
|
channel_to_q[channel].put("UI:AUTOADVANCE:" + str(state))
|
2020-10-24 14:04:33 +00:00
|
|
|
return ui_status()
|
2020-10-23 21:58:53 +00:00
|
|
|
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-11-03 01:07:25 +00:00
|
|
|
@app.route("/player/<int:channel>/repeat/<state>")
|
2020-12-19 14:57:37 +00:00
|
|
|
def repeat(channel: int, state: str):
|
2021-03-22 00:33:14 +00:00
|
|
|
channel_to_q[channel].put("UI:REPEAT:" + state.upper())
|
2020-11-03 01:07:25 +00:00
|
|
|
return ui_status()
|
|
|
|
|
2020-11-09 00:10:36 +00:00
|
|
|
|
2020-11-03 01:07:25 +00:00
|
|
|
@app.route("/player/<int:channel>/playonload/<int:state>")
|
2020-11-04 00:09:42 +00:00
|
|
|
def playonload(channel: int, state: int):
|
2021-03-22 00:33:14 +00:00
|
|
|
channel_to_q[channel].put("UI:PLAYONLOAD:" + str(state))
|
2020-11-03 01:07:25 +00:00
|
|
|
return ui_status()
|
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
|
2020-11-09 00:10:36 +00:00
|
|
|
# Channel Items
|
2020-11-02 23:06:45 +00:00
|
|
|
|
|
|
|
|
2020-11-15 19:34:13 +00:00
|
|
|
@app.route("/player/<int:channel>/load/<int:channel_weight>")
|
|
|
|
def load(channel: int, channel_weight: int):
|
2021-03-22 00:33:14 +00:00
|
|
|
channel_to_q[channel].put("UI:LOAD:" + str(channel_weight))
|
2020-11-01 02:35:14 +00:00
|
|
|
return ui_status()
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-11-09 00:10:36 +00:00
|
|
|
|
2020-10-30 00:33:00 +00:00
|
|
|
@app.route("/player/<int:channel>/unload")
|
2020-12-19 14:57:37 +00:00
|
|
|
def unload(channel: int):
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2021-03-22 00:33:14 +00:00
|
|
|
channel_to_q[channel].put("UI:UNLOAD")
|
2020-10-30 00:33:00 +00:00
|
|
|
|
|
|
|
return ui_status()
|
|
|
|
|
2020-11-09 00:10:36 +00:00
|
|
|
|
2020-11-01 02:35:14 +00:00
|
|
|
@app.route("/player/<int:channel>/add", methods=["POST"])
|
|
|
|
def add_to_plan(channel: int):
|
2020-12-20 01:10:19 +00:00
|
|
|
new_item: Dict[str, Any] = {
|
2020-11-15 19:34:13 +00:00
|
|
|
"channel_weight": int(request.form["channel_weight"]),
|
2020-11-01 02:35:14 +00:00
|
|
|
"filename": request.form["filename"],
|
2021-04-08 19:53:51 +00:00
|
|
|
"title": request.form["title"],
|
|
|
|
"artist": request.form["artist"],
|
2020-11-01 02:35:14 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 00:33:14 +00:00
|
|
|
channel_to_q[channel].put("UI:ADD:" + json.dumps(new_item))
|
2020-11-01 02:35:14 +00:00
|
|
|
|
|
|
|
return new_item
|
|
|
|
|
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
# @app.route("/player/<int:channel>/remove/<int:channel_weight>")
|
2020-11-15 19:34:13 +00:00
|
|
|
def remove_plan(channel: int, channel_weight: int):
|
2021-03-22 00:33:14 +00:00
|
|
|
channel_to_q[channel].put("UI:REMOVE:" + str(channel_weight))
|
2020-11-01 02:35:14 +00:00
|
|
|
|
2020-11-09 00:10:36 +00:00
|
|
|
# TODO Return
|
2020-11-01 02:35:14 +00:00
|
|
|
return True
|
2020-10-30 00:33:00 +00:00
|
|
|
|
2020-11-09 00:10:36 +00:00
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
# @app.route("/player/<int:channel>/clear")
|
2020-11-02 23:06:45 +00:00
|
|
|
def clear_channel_plan(channel: int):
|
2021-03-22 00:33:14 +00:00
|
|
|
channel_to_q[channel].put("UI:CLEAR")
|
2020-11-02 23:06:45 +00:00
|
|
|
|
2020-11-09 00:10:36 +00:00
|
|
|
# TODO Return
|
2020-11-02 23:06:45 +00:00
|
|
|
return True
|
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
|
2020-11-09 00:10:36 +00:00
|
|
|
# General Channel Endpoints
|
|
|
|
|
2020-10-30 00:33:00 +00:00
|
|
|
|
|
|
|
@app.route("/player/<int:channel>/status")
|
2020-12-08 19:41:31 +00:00
|
|
|
def channel_json(channel: int):
|
2021-04-08 20:15:15 +00:00
|
|
|
return jsonify(status(channel))
|
2020-10-30 00:33:00 +00:00
|
|
|
|
2021-03-21 13:05:33 +00:00
|
|
|
|
|
|
|
@app.route("/plan/list")
|
|
|
|
def list_showplans():
|
2021-04-08 19:53:51 +00:00
|
|
|
while not api_from_q.empty():
|
|
|
|
api_from_q.get() # Just waste any previous status responses.
|
2021-03-21 13:05:33 +00:00
|
|
|
|
|
|
|
api_to_q.put("LIST_PLANS")
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
response = api_from_q.get_nowait()
|
|
|
|
if response.startswith("LIST_PLANS:"):
|
2021-04-08 20:15:15 +00:00
|
|
|
response = response[response.index(":") + 1:]
|
2021-03-21 13:05:33 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
except queue.Empty:
|
|
|
|
pass
|
|
|
|
|
2021-04-04 22:14:08 +00:00
|
|
|
time.sleep(0.02)
|
2021-03-21 13:05:33 +00:00
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
|
2021-03-21 13:05:33 +00:00
|
|
|
@app.route("/library/search/<type>")
|
|
|
|
def search_library(type: str):
|
|
|
|
|
|
|
|
if type not in ["managed", "track"]:
|
|
|
|
abort(404)
|
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
while not api_from_q.empty():
|
|
|
|
api_from_q.get() # Just waste any previous status responses.
|
2021-03-21 13:05:33 +00:00
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
params = json.dumps(
|
2021-04-08 20:15:15 +00:00
|
|
|
{"title": request.args.get(
|
|
|
|
"title"), "artist": request.args.get("artist")}
|
2021-04-08 19:53:51 +00:00
|
|
|
)
|
2021-03-21 13:05:33 +00:00
|
|
|
api_to_q.put("SEARCH_TRACK:{}".format(params))
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
response = api_from_q.get_nowait()
|
|
|
|
if response.startswith("SEARCH_TRACK:"):
|
2021-03-22 00:33:14 +00:00
|
|
|
response = response.split(":", 1)[1]
|
2021-03-21 13:05:33 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
except queue.Empty:
|
|
|
|
pass
|
|
|
|
|
2021-04-04 22:14:08 +00:00
|
|
|
time.sleep(0.02)
|
2021-03-21 13:05:33 +00:00
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
|
2021-04-04 14:26:39 +00:00
|
|
|
@app.route("/library/playlists/<type>")
|
|
|
|
def get_playlists(type: str):
|
|
|
|
|
|
|
|
if type not in ["music", "aux"]:
|
|
|
|
abort(401)
|
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
while not api_from_q.empty():
|
|
|
|
api_from_q.get() # Just waste any previous status responses.
|
2021-04-04 14:26:39 +00:00
|
|
|
|
|
|
|
command = "LIST_PLAYLIST_{}".format(type.upper())
|
|
|
|
api_to_q.put(command)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
response = api_from_q.get_nowait()
|
|
|
|
if response.startswith(command):
|
|
|
|
response = response.split(":", 1)[1]
|
|
|
|
return response
|
|
|
|
|
|
|
|
except queue.Empty:
|
|
|
|
pass
|
|
|
|
|
2021-04-04 22:14:08 +00:00
|
|
|
time.sleep(0.02)
|
2021-04-04 14:26:39 +00:00
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
|
2021-04-04 14:26:39 +00:00
|
|
|
@app.route("/library/playlist/<type>/<library_id>")
|
|
|
|
def get_playlist(type: str, library_id: str):
|
|
|
|
|
|
|
|
if type not in ["music", "aux"]:
|
|
|
|
abort(401)
|
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
while not api_from_q.empty():
|
|
|
|
api_from_q.get() # Just waste any previous status responses.
|
2021-04-04 14:26:39 +00:00
|
|
|
|
|
|
|
command = "GET_PLAYLIST_{}:{}".format(type.upper(), library_id)
|
|
|
|
api_to_q.put(command)
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
while True:
|
2021-04-04 14:26:39 +00:00
|
|
|
try:
|
|
|
|
response = api_from_q.get_nowait()
|
|
|
|
if response.startswith(command):
|
2021-04-08 20:15:15 +00:00
|
|
|
response = response[len(command) + 1:]
|
2021-04-04 14:26:39 +00:00
|
|
|
if response == "null":
|
|
|
|
abort(401)
|
|
|
|
return response
|
|
|
|
|
|
|
|
except queue.Empty:
|
|
|
|
pass
|
|
|
|
|
2021-04-04 22:14:08 +00:00
|
|
|
time.sleep(0.02)
|
2021-04-04 14:26:39 +00:00
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
|
2021-02-14 00:29:47 +00:00
|
|
|
@app.route("/plan/load/<int:timeslotid>")
|
|
|
|
def load_showplan(timeslotid: int):
|
|
|
|
|
|
|
|
for channel in channel_to_q:
|
2021-03-22 00:33:14 +00:00
|
|
|
channel.put("UI:GET_PLAN:" + str(timeslotid))
|
2021-02-14 00:29:47 +00:00
|
|
|
|
|
|
|
return ui_status()
|
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
|
2020-12-19 14:57:37 +00:00
|
|
|
def status(channel: int):
|
2021-04-08 19:53:51 +00:00
|
|
|
while not ui_to_q[channel].empty():
|
|
|
|
ui_to_q[channel].get() # Just waste any previous status responses.
|
2021-03-15 20:20:26 +00:00
|
|
|
|
2021-03-22 00:33:14 +00:00
|
|
|
channel_to_q[channel].put("UI:STATUS")
|
|
|
|
retries = 0
|
|
|
|
while retries < 40:
|
2020-12-20 18:52:27 +00:00
|
|
|
try:
|
|
|
|
response = ui_to_q[channel].get_nowait()
|
2021-03-22 00:33:14 +00:00
|
|
|
if response.startswith("UI:STATUS:"):
|
2021-04-08 19:53:51 +00:00
|
|
|
response = response.split(":", 2)[2]
|
2021-03-22 00:33:14 +00:00
|
|
|
# TODO: Handle OKAY / FAIL
|
2021-04-08 20:15:15 +00:00
|
|
|
response = response[response.index(":") + 1:]
|
2020-12-20 18:52:27 +00:00
|
|
|
try:
|
|
|
|
response = json.loads(response)
|
|
|
|
except Exception as e:
|
|
|
|
raise e
|
|
|
|
return response
|
|
|
|
|
|
|
|
except queue.Empty:
|
|
|
|
pass
|
|
|
|
|
2021-03-22 00:33:14 +00:00
|
|
|
retries += 1
|
|
|
|
|
2021-04-04 22:14:08 +00:00
|
|
|
time.sleep(0.02)
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
|
2020-11-01 00:31:58 +00:00
|
|
|
@app.route("/quit")
|
|
|
|
def quit():
|
|
|
|
stopServer()
|
|
|
|
return "Shutting down..."
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/player/all/stop")
|
|
|
|
def all_stop():
|
2020-10-24 14:04:33 +00:00
|
|
|
for channel in channel_to_q:
|
2021-03-22 00:33:14 +00:00
|
|
|
channel.put("UI:STOP")
|
2020-11-02 23:06:45 +00:00
|
|
|
return ui_status()
|
|
|
|
|
2020-10-24 02:13:02 +00:00
|
|
|
|
2020-11-02 23:06:45 +00:00
|
|
|
@app.route("/player/all/clear")
|
|
|
|
def clear_all_channels():
|
|
|
|
for channel in channel_to_q:
|
2021-03-22 00:33:14 +00:00
|
|
|
channel.put("UI:CLEAR")
|
2020-11-02 23:06:45 +00:00
|
|
|
return ui_status()
|
2020-10-24 02:13:02 +00:00
|
|
|
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-11-09 00:42:09 +00:00
|
|
|
@app.route("/logs")
|
|
|
|
def list_logs():
|
|
|
|
data = {
|
|
|
|
"ui_page": "loglist",
|
|
|
|
"ui_title": "Logs",
|
2021-04-08 19:53:51 +00:00
|
|
|
"logs": ["BAPSicleServer"]
|
|
|
|
+ ["channel{}".format(x) for x in range(state.state["num_channels"])],
|
2020-11-09 00:42:09 +00:00
|
|
|
}
|
|
|
|
return render_template("loglist.html", data=data)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/logs/<path:path>")
|
|
|
|
def send_logs(path):
|
2021-04-08 21:05:25 +00:00
|
|
|
log_file = open("logs/{}.log".format(path))
|
2020-11-09 00:42:09 +00:00
|
|
|
data = {
|
2021-04-08 21:05:25 +00:00
|
|
|
"logs": log_file.read().splitlines(),
|
2021-04-08 19:53:51 +00:00
|
|
|
"ui_page": "log",
|
|
|
|
"ui_title": "Logs - {}".format(path),
|
2020-11-09 00:42:09 +00:00
|
|
|
}
|
|
|
|
l.close()
|
2021-04-08 19:53:51 +00:00
|
|
|
return render_template("log.html", data=data)
|
|
|
|
|
2020-11-09 00:42:09 +00:00
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
@app.route("/favicon.ico")
|
2021-03-22 00:45:46 +00:00
|
|
|
def serve_favicon():
|
2021-04-08 19:53:51 +00:00
|
|
|
return send_from_directory("ui-static", "favicon.ico")
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
|
|
|
|
@app.route("/static/<path:path>")
|
2021-03-22 00:45:46 +00:00
|
|
|
def serve_static(path: str):
|
2021-04-08 19:53:51 +00:00
|
|
|
return send_from_directory("ui-static", path)
|
|
|
|
|
2020-10-23 21:58:53 +00:00
|
|
|
|
2021-04-04 21:34:46 +00:00
|
|
|
def startServer():
|
2021-04-07 20:13:19 +00:00
|
|
|
process_title = "startServer"
|
2021-04-04 21:34:46 +00:00
|
|
|
setproctitle(process_title)
|
2021-04-08 19:53:51 +00:00
|
|
|
# multiprocessing.current_process().name = process_title
|
2021-04-07 20:13:19 +00:00
|
|
|
|
|
|
|
global logger
|
|
|
|
global state
|
|
|
|
logger = LoggingManager("BAPSicleServer")
|
|
|
|
|
|
|
|
state = StateManager("BAPSicleServer", logger, default_state)
|
|
|
|
state.update("server_version", config.VERSION)
|
2020-12-20 18:52:27 +00:00
|
|
|
|
2020-11-01 01:36:42 +00:00
|
|
|
if isMacOS():
|
|
|
|
multiprocessing.set_start_method("spawn", True)
|
2020-11-09 00:10:36 +00:00
|
|
|
for channel in range(state.state["num_channels"]):
|
2020-10-24 14:04:33 +00:00
|
|
|
|
|
|
|
channel_to_q.append(multiprocessing.Queue())
|
|
|
|
channel_from_q.append(multiprocessing.Queue())
|
2020-12-20 01:10:19 +00:00
|
|
|
ui_to_q.append(multiprocessing.Queue())
|
2021-03-22 00:33:14 +00:00
|
|
|
websocket_to_q.append(multiprocessing.Queue())
|
|
|
|
controller_to_q.append(multiprocessing.Queue())
|
2020-10-24 14:04:33 +00:00
|
|
|
channel_p.append(
|
|
|
|
multiprocessing.Process(
|
|
|
|
target=player.Player,
|
2021-04-04 21:34:46 +00:00
|
|
|
args=(channel, channel_to_q[-1], channel_from_q[-1])
|
2021-04-08 19:53:51 +00:00
|
|
|
# daemon=True
|
2020-10-24 14:04:33 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
channel_p[channel].start()
|
|
|
|
|
2021-04-04 21:34:46 +00:00
|
|
|
global api_from_q, api_to_q, api_handler, player_handler, websockets_server, controller_handler
|
2021-03-21 13:05:33 +00:00
|
|
|
api_to_q = multiprocessing.Queue()
|
|
|
|
api_from_q = multiprocessing.Queue()
|
2021-04-08 19:53:51 +00:00
|
|
|
api_handler = multiprocessing.Process(
|
|
|
|
target=APIHandler, args=(api_to_q, api_from_q)
|
|
|
|
)
|
2021-03-21 13:05:33 +00:00
|
|
|
api_handler.start()
|
2020-12-20 01:10:19 +00:00
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
player_handler = multiprocessing.Process(
|
|
|
|
target=PlayerHandler,
|
|
|
|
args=(channel_from_q, websocket_to_q, ui_to_q, controller_to_q),
|
|
|
|
)
|
2020-12-20 01:10:19 +00:00
|
|
|
player_handler.start()
|
|
|
|
|
2021-04-05 21:13:53 +00:00
|
|
|
# Note, state here will become a copy in the process.
|
|
|
|
# It will not update, and callbacks will not work :/
|
2021-04-08 19:53:51 +00:00
|
|
|
websockets_server = multiprocessing.Process(
|
|
|
|
target=WebsocketServer, args=(channel_to_q, websocket_to_q, state)
|
|
|
|
)
|
2020-11-15 17:40:18 +00:00
|
|
|
websockets_server.start()
|
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
controller_handler = multiprocessing.Process(
|
|
|
|
target=MattchBox, args=(channel_to_q, controller_to_q, state)
|
|
|
|
)
|
2021-02-14 23:58:32 +00:00
|
|
|
controller_handler.start()
|
|
|
|
|
2021-03-22 00:33:14 +00:00
|
|
|
# TODO Move this to player or installer.
|
|
|
|
if False:
|
|
|
|
if not isMacOS():
|
2020-11-04 22:38:31 +00:00
|
|
|
|
2021-03-22 00:33:14 +00:00
|
|
|
# Temporary RIP.
|
2020-11-04 22:38:31 +00:00
|
|
|
|
2021-03-22 00:33:14 +00:00
|
|
|
# Welcome Speech
|
2020-11-04 22:38:31 +00:00
|
|
|
|
2021-03-22 00:33:14 +00:00
|
|
|
text_to_speach = pyttsx3.init()
|
|
|
|
text_to_speach.save_to_file(
|
|
|
|
"""Thank-you for installing BAPSicle - the play-out server from the broadcasting and presenting suite.
|
|
|
|
By default, this server is accepting connections on port 13500
|
|
|
|
The version of the server service is {}
|
|
|
|
Please refer to the documentation included with this application for further assistance.""".format(
|
|
|
|
config.VERSION
|
|
|
|
),
|
2021-04-08 19:53:51 +00:00
|
|
|
"dev/welcome.mp3",
|
2021-03-22 00:33:14 +00:00
|
|
|
)
|
|
|
|
text_to_speach.runAndWait()
|
2020-11-03 00:32:43 +00:00
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
new_item: Dict[str, Any] = {
|
2021-03-22 00:33:14 +00:00
|
|
|
"channel_weight": 0,
|
|
|
|
"filename": "dev/welcome.mp3",
|
2021-04-08 19:53:51 +00:00
|
|
|
"title": "Welcome to BAPSicle",
|
|
|
|
"artist": "University Radio York",
|
2021-03-22 00:33:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
channel_to_q[0].put("ADD:" + json.dumps(new_item))
|
|
|
|
channel_to_q[0].put("LOAD:0")
|
|
|
|
channel_to_q[0].put("PLAY")
|
2020-11-01 02:35:14 +00:00
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
# Don't use reloader, it causes Nested Processes!
|
2021-04-05 21:13:53 +00:00
|
|
|
def runWebServer():
|
2021-04-07 20:13:19 +00:00
|
|
|
process_title = "WebServer"
|
|
|
|
setproctitle(process_title)
|
2021-04-08 19:53:51 +00:00
|
|
|
CORS(app, supports_credentials=True) # Allow ALL CORS!!!
|
2021-04-05 21:13:53 +00:00
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
log = logging.getLogger("werkzeug")
|
2021-04-05 21:13:53 +00:00
|
|
|
log.disabled = True
|
|
|
|
|
|
|
|
app.logger.disabled = True
|
2021-04-08 19:53:51 +00:00
|
|
|
app.run(
|
|
|
|
host=state.state["host"],
|
|
|
|
port=state.state["port"],
|
|
|
|
debug=True,
|
|
|
|
use_reloader=False,
|
|
|
|
)
|
2021-04-05 21:13:53 +00:00
|
|
|
|
2021-04-04 21:34:46 +00:00
|
|
|
global webserver
|
2021-04-08 19:53:51 +00:00
|
|
|
webserver = multiprocessing.Process(runWebServer())
|
2021-04-04 21:34:46 +00:00
|
|
|
webserver.start()
|
2020-10-24 20:31:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def stopServer():
|
2021-04-05 21:13:53 +00:00
|
|
|
global channel_p, channel_from_q, channel_to_q, websockets_server, webserver, controller_handler
|
|
|
|
print("Stopping Controllers")
|
|
|
|
controller_handler.terminate()
|
|
|
|
controller_handler.join()
|
|
|
|
|
2021-04-04 21:34:46 +00:00
|
|
|
print("Stopping Websockets")
|
|
|
|
websocket_to_q[0].put("WEBSOCKET:QUIT")
|
|
|
|
websockets_server.join()
|
|
|
|
del websockets_server
|
2021-04-05 21:13:53 +00:00
|
|
|
|
2020-10-24 20:31:52 +00:00
|
|
|
print("Stopping server.py")
|
|
|
|
for q in channel_to_q:
|
|
|
|
q.put("QUIT")
|
2021-04-08 21:05:25 +00:00
|
|
|
for channel in channel_p:
|
2020-11-01 00:31:58 +00:00
|
|
|
try:
|
2021-04-08 21:05:25 +00:00
|
|
|
channel.join()
|
2021-04-04 21:34:46 +00:00
|
|
|
except Exception as e:
|
2021-04-08 19:53:51 +00:00
|
|
|
print("*** Ignoring exception:", e)
|
2020-11-01 00:31:58 +00:00
|
|
|
pass
|
2020-11-10 19:40:42 +00:00
|
|
|
finally:
|
2021-04-08 21:05:25 +00:00
|
|
|
del channel
|
2021-04-04 21:34:46 +00:00
|
|
|
del channel_from_q
|
|
|
|
del channel_to_q
|
2020-11-01 01:36:42 +00:00
|
|
|
print("Stopped all players.")
|
2021-04-04 21:34:46 +00:00
|
|
|
|
2021-04-05 21:13:53 +00:00
|
|
|
print("Stopping webserver")
|
2021-04-04 21:34:46 +00:00
|
|
|
global webserver
|
|
|
|
webserver.terminate()
|
|
|
|
webserver.join()
|
|
|
|
|
2021-04-05 21:13:53 +00:00
|
|
|
print("Stopped webserver")
|
2020-10-24 20:31:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-04-05 21:13:53 +00:00
|
|
|
raise Exception("BAPSicle is a service. Please run it like one.")
|