2017-05-21 14:29:17 -04:00
|
|
|
import logging
|
2017-05-25 02:40:36 -04:00
|
|
|
from subprocess import check_output, CalledProcessError
|
2016-12-30 02:51:56 -05:00
|
|
|
from flask import Flask, render_template, Response, Blueprint, redirect, url_for
|
|
|
|
from flask_wtf import FlaskForm
|
2017-05-21 14:29:17 -04:00
|
|
|
from wtforms.fields import StringField, SubmitField
|
2016-12-30 02:51:56 -05:00
|
|
|
from wtforms.validators import InputRequired
|
|
|
|
|
2017-05-25 02:40:36 -04:00
|
|
|
from auxilary import async
|
|
|
|
|
2017-05-21 14:29:17 -04:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
# gag the flask logger unless it has something useful to say
|
|
|
|
werkzeug = logging.getLogger('werkzeug')
|
|
|
|
werkzeug.setLevel(logging.ERROR)
|
|
|
|
|
2016-12-30 02:51:56 -05:00
|
|
|
class TTSForm(FlaskForm):
|
|
|
|
tts = StringField(validators=[InputRequired()])
|
|
|
|
submitTTS = SubmitField('Speak')
|
|
|
|
|
|
|
|
# TODO: fix random connection fails (might be an nginx thing)
|
|
|
|
|
2017-05-21 14:29:17 -04:00
|
|
|
@async(daemon=True)
|
2017-05-25 02:40:36 -04:00
|
|
|
def _runApp(a):
|
|
|
|
logger.info('Starting web interface')
|
|
|
|
a.run(debug=False, threaded=True)
|
|
|
|
|
2017-05-21 14:29:17 -04:00
|
|
|
def initWebInterface(stateMachine):
|
|
|
|
siteRoot = Blueprint('siteRoot', __name__, static_folder='static', static_url_path='')
|
2016-12-30 02:51:56 -05:00
|
|
|
|
2017-05-21 14:29:17 -04:00
|
|
|
@siteRoot.route('/', methods=['GET', 'POST'])
|
|
|
|
@siteRoot.route('/index', methods=['GET', 'POST'])
|
|
|
|
def index():
|
|
|
|
ttsForm = TTSForm()
|
2016-12-30 02:51:56 -05:00
|
|
|
|
2017-05-21 14:29:17 -04:00
|
|
|
if ttsForm.validate_on_submit() and ttsForm.submitTTS.data:
|
|
|
|
stateMachine.soundLib.speak(ttsForm.tts.data)
|
|
|
|
return redirect(url_for('siteRoot.index'))
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
'index.html',
|
|
|
|
ttsForm=ttsForm,
|
|
|
|
state=stateMachine.currentState
|
|
|
|
)
|
|
|
|
|
2017-05-25 02:40:36 -04:00
|
|
|
try:
|
|
|
|
check_output(['pidof', 'janus'])
|
|
|
|
except CalledProcessError:
|
2017-05-30 02:11:15 -04:00
|
|
|
logger.error('Janus not running. Aborting')
|
2017-05-25 02:40:36 -04:00
|
|
|
raise SystemExit
|
|
|
|
|
2017-05-21 14:29:17 -04:00
|
|
|
app = Flask(__name__)
|
|
|
|
app.secret_key = '3276d68dac56985bea352325125641ff'
|
|
|
|
app.register_blueprint(siteRoot, url_prefix='/pyledriver')
|
2017-05-25 02:40:36 -04:00
|
|
|
|
|
|
|
_runApp(app)
|