no longer require janus to run, and add safety net to restart at web interface

This commit is contained in:
petrucci4prez 2017-06-23 01:50:07 -04:00
parent 75b823b369
commit dab054099c
2 changed files with 51 additions and 33 deletions

View File

@ -14,8 +14,10 @@
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootbox.js/4.1.0/bootbox.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/spin.js/2.3.2/spin.min.js"></script>
{% if janusRunning %}
<script type="text/javascript" src="{{ url_for('siteRoot.static', filename='janus.js') }}"></script>
<script type="text/javascript" src="{{ url_for('siteRoot.static', filename='streamingtest.js') }}"></script>
{% endif %}
</head>
<body>
@ -39,6 +41,7 @@
</div>
</form>
</ul>
{% if janusRunning %}
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a id="streamset" class="dropdown-toggle" data-toggle="dropdown">
@ -48,22 +51,22 @@
</li>
<button class="btn btn-default navbar-btn" autocomplete="off" id="watch">Start</button>
</ul>
{% endif %}
</div>
</div>
</div>
<div class="container-fluid body-content">
<div class="col-md-6">
<!--
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Stream <span class="label label-info hide" id="status"></h3>
</div>
-->
{% if janusRunning %}
<div id="stream"></div>
<p class="hide" id="status">
<!--
</div>
-->
{% else %}
<p>Janus not running. Streaming not available.</p>
<form action="{{ url_for('siteRoot.index') }}" method="post" name="janus_running" class="navbar-form" role="search">
{{ janusRestart.hidden_tag() }}
{{ janusRestart.submitRestart(class_="btn btn-default") }}
</form>
{% endif %}
</div>
</div>
</body>

View File

@ -1,5 +1,5 @@
import logging
from subprocess import check_output, CalledProcessError
from subprocess import check_output, CalledProcessError, run, PIPE
from flask import Flask, render_template, Response, Blueprint, redirect, url_for
from flask_wtf import FlaskForm
from wtforms.fields import StringField, SubmitField
@ -17,8 +17,20 @@ class TTSForm(FlaskForm):
tts = StringField(validators=[InputRequired()])
submitTTS = SubmitField('Speak')
class JanusRestart(FlaskForm):
submitRestart = SubmitField('Restart Janus')
# TODO: fix random connection fails (might be an nginx thing)
def janusRunning():
try:
check_output(['pidof', 'janus'])
except CalledProcessError:
logger.warning('Janus not running')
return False
else:
return True
@async(daemon=True)
def startWebInterface(stateMachine):
siteRoot = Blueprint('siteRoot', __name__, static_folder='static', static_url_path='')
@ -27,22 +39,25 @@ def startWebInterface(stateMachine):
@siteRoot.route('/index', methods=['GET', 'POST'])
def index():
ttsForm = TTSForm()
janusRestart = JanusRestart()
if ttsForm.validate_on_submit() and ttsForm.submitTTS.data:
stateMachine.soundLib.speak(ttsForm.tts.data)
return redirect(url_for('siteRoot.index'))
elif janusRestart.validate_on_submit():
logger.info('Restarting Janus')
run(['systemctl', 'restart', 'janus'], stdout=PIPE, stderr=PIPE)
return redirect(url_for('siteRoot.index'))
return render_template(
'index.html',
ttsForm=ttsForm,
state=stateMachine.currentState
state=stateMachine.currentState,
janusRunning=janusRunning(),
janusRestart=janusRestart
)
try:
check_output(['pidof', 'janus'])
except CalledProcessError:
logger.error('Janus not running. Aborting')
raise SystemExit
janusRunning()
app = Flask(__name__)
app.secret_key = '3276d68dac56985bea352325125641ff'