Projekt Litsimaja - Lapikute tagatoa seintele programmeeritavad ARGB ribad
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

83 lines
2.2 KiB

#!/usr/bin/env python3
import logging
import sys
import lib.ProgramLoading as Pl
from lib.Litsimaja import Litsimaja
from flask import Flask, request, Response, render_template, json
from flask_accept import accept
# 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('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'])
@accept('text/html')
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'])
@accept('application/json')
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'))