Flesh out API
This commit is contained in:
parent
5eed388677
commit
4797322ed4
|
@ -1,4 +1,4 @@
|
|||
from flask import Flask, render_template, request, jsonify, url_for
|
||||
from flask import Flask, render_template, request, jsonify
|
||||
from .controller import Controller
|
||||
|
||||
|
||||
|
@ -14,8 +14,9 @@ controller = Controller()
|
|||
@app.route("/api/connect")
|
||||
def connect():
|
||||
host: str = request.args.get("host", "0.0.0.0")
|
||||
port: int = int(request.args.get("port", 5800))
|
||||
controller.connect(host, port)
|
||||
port: int = int(request.args.get("port", 9800))
|
||||
|
||||
return controller.connect(host, port)
|
||||
|
||||
|
||||
@app.route("/api/soundfonts")
|
||||
|
@ -29,7 +30,29 @@ def instruments():
|
|||
return jsonify(controller.get_instruments(font))
|
||||
|
||||
|
||||
@app.route("/", defaults={"path": ""})
|
||||
@app.route("/api/select")
|
||||
def select():
|
||||
channel: int = int(request.args.get("channel", 0))
|
||||
instrument: int = int(request.args.get("instrument", 0))
|
||||
bank: int = int(request.args.get("bank", 0))
|
||||
program: int = int(request.args.get("program", 0))
|
||||
|
||||
return jsonify(controller.select(channel, instrument, bank, program))
|
||||
|
||||
|
||||
@app.route("/api/gain")
|
||||
def gain():
|
||||
amount: int = int(request.args.get("gain", 3))
|
||||
return jsonify(controller.set_gain(amount))
|
||||
|
||||
|
||||
@app.route("/")
|
||||
@app.route("/<path:path>")
|
||||
def index(path: str):
|
||||
def index(path: str = ""):
|
||||
print(f"PATH: {path}")
|
||||
return render_template("index.html")
|
||||
|
||||
|
||||
@app.errorhandler(404)
|
||||
def not_found(*args, **kwargs):
|
||||
return render_template("index.html")
|
||||
|
|
|
@ -1,24 +1,50 @@
|
|||
import re
|
||||
from telnetlib import Telnet
|
||||
|
||||
|
||||
class Controller(object):
|
||||
timeout = 30
|
||||
TIMEOUT = 30
|
||||
SUCCESS_RESPONSE = {"success": True}
|
||||
FONT_MATCHER = re.compile("(?P<id>[0-9]+)\\s(?P<name>.*)", re.MULTILINE)
|
||||
INSTRUMENT_MATCHER = re.compile(
|
||||
"(?P<bank>[0-9]+)-(?P<program>[0-9]+)\\s(?P<name>.*)", re.MULTILINE
|
||||
)
|
||||
|
||||
def connect(self, host: str = "0.0.0.0", port: int = 5800):
|
||||
self.connection = Telnet(host=host, port=port, timeout=Controller.timeout)
|
||||
self.connection = Telnet(host=host, port=port, timeout=Controller.TIMEOUT)
|
||||
|
||||
return Controller.SUCCESS_RESPONSE
|
||||
|
||||
def execute(self, command: str):
|
||||
self.connection.write(command.encode("ascii") + b"\r\n")
|
||||
output = self.connection.read_until(b"END OF IT", timeout=2)
|
||||
output = self.connection.read_until(match=b"OK", timeout=1)
|
||||
|
||||
return output.decode("ascii")
|
||||
|
||||
def get_soundfonts(self):
|
||||
self.execute("fonts")
|
||||
output = self.execute("fonts")
|
||||
fonts = Controller.FONT_MATCHER.findall(output)
|
||||
|
||||
return {}
|
||||
print(fonts)
|
||||
|
||||
return [{"id": int(f[0]), "name": f[1]} for f in fonts]
|
||||
|
||||
def get_instruments(self, font: int):
|
||||
self.execute(f"inst {font}")
|
||||
output = self.execute(f"inst {font}")
|
||||
instruments = Controller.INSTRUMENT_MATCHER.findall(output)
|
||||
|
||||
return {}
|
||||
print(instruments)
|
||||
|
||||
return [
|
||||
{"bank": int(i[0]), "program": int(i[1]), "name": i[2]} for i in instruments
|
||||
]
|
||||
|
||||
def select(self, channel: int, instrument: int, bank: int, program: int):
|
||||
self.execute(f"select {channel} {instrument} {bank} {program}")
|
||||
|
||||
return Controller.SUCCESS_RESPONSE
|
||||
|
||||
def set_gain(self, amount: int):
|
||||
self.execute(f"gain {amount}")
|
||||
|
||||
return Controller.SUCCESS_RESPONSE
|
||||
|
|
Loading…
Reference in New Issue