forked from pvx/litsimaja
82 lines
2.2 KiB
Python
Executable File
82 lines
2.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import logging
|
|
import pathlib
|
|
import sys
|
|
import lib.ProgramLoading as Pl
|
|
from lib.Litsimaja import Litsimaja
|
|
from flask import Flask, request, Response, render_template, json
|
|
|
|
root_path = pathlib.Path(__file__).parent.absolute()
|
|
# start litsimaja
|
|
lm = Litsimaja()
|
|
|
|
# logging
|
|
logger = logging.getLogger('litsimaja')
|
|
logger.setLevel(logging.DEBUG if lm.conf('IS_DEV') else logging.WARN)
|
|
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
file_handler = logging.FileHandler(str(root_path) + '/litsimaja.log')
|
|
file_handler.setFormatter(formatter)
|
|
logger.addHandler(file_handler)
|
|
stdout_handler = logging.StreamHandler(sys.stdout)
|
|
stdout_handler.setFormatter(formatter)
|
|
logger.addHandler(stdout_handler)
|
|
|
|
app = Flask(__name__, static_url_path='', static_folder='templates')
|
|
Pl.run('siinus', 'Wipes', lm, logger, {'color': [0, 0, 0]})
|
|
Pl.run('peter', 'DiskoPidu', lm, logger, {})
|
|
|
|
|
|
def lm_standard_xhr_response() -> Response:
|
|
return Response(response=json.dumps(lm.build_status_array()), status=200, mimetype='application/json')
|
|
|
|
|
|
@app.route('/', methods=['GET'])
|
|
def respond_root():
|
|
return render_template(
|
|
'index.html',
|
|
programs=Pl.list_all(True),
|
|
regions=lm.get_region_ids(),
|
|
status=lm.build_status_array()
|
|
)
|
|
|
|
|
|
@app.route('/status', methods=['GET'])
|
|
def respond_status():
|
|
return lm_standard_xhr_response()
|
|
|
|
|
|
@app.route('/crash', methods=['GET'])
|
|
def crash():
|
|
lm.clear_loops()
|
|
return lm_standard_xhr_response()
|
|
|
|
|
|
@app.route('/program/<program>', methods=['POST'])
|
|
def run_program(program):
|
|
args = request.get_json(force=True)
|
|
prg = program.split('.')
|
|
Pl.run(prg[0], prg[1], lm, logger, args)
|
|
return lm_standard_xhr_response()
|
|
|
|
|
|
@app.route('/region/<region>', methods=['GET'])
|
|
def switch_region(region):
|
|
lm.switch_region(int(region))
|
|
return lm_standard_xhr_response()
|
|
|
|
|
|
@app.route('/tempo/set/<float:bpm>', methods=['GET'])
|
|
def set_tempo(bpm: float):
|
|
tempo = lm.get_tempo()
|
|
tempo.set_bpm(bpm)
|
|
return lm_standard_xhr_response()
|
|
|
|
|
|
@app.route('/tempo/sync', methods=['GET'])
|
|
def sync_beat():
|
|
lm.get_tempo().sync_beat()
|
|
return lm_standard_xhr_response()
|
|
|
|
|
|
app.run(lm.conf('BIND_ADDR'), lm.conf('BIND_PORT'))
|