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/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> <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='janus.js') }}"></script>
<script type="text/javascript" src="{{ url_for('siteRoot.static', filename='streamingtest.js') }}"></script> <script type="text/javascript" src="{{ url_for('siteRoot.static', filename='streamingtest.js') }}"></script>
{% endif %}
</head> </head>
<body> <body>
@ -39,6 +41,7 @@
</div> </div>
</form> </form>
</ul> </ul>
{% if janusRunning %}
<ul class="nav navbar-nav navbar-right"> <ul class="nav navbar-nav navbar-right">
<li class="dropdown"> <li class="dropdown">
<a id="streamset" class="dropdown-toggle" data-toggle="dropdown"> <a id="streamset" class="dropdown-toggle" data-toggle="dropdown">
@ -48,22 +51,22 @@
</li> </li>
<button class="btn btn-default navbar-btn" autocomplete="off" id="watch">Start</button> <button class="btn btn-default navbar-btn" autocomplete="off" id="watch">Start</button>
</ul> </ul>
{% endif %}
</div> </div>
</div> </div>
</div> </div>
<div class="container-fluid body-content"> <div class="container-fluid body-content">
<div class="col-md-6"> <div class="col-md-6">
<!-- {% if janusRunning %}
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Stream <span class="label label-info hide" id="status"></h3>
</div>
-->
<div id="stream"></div> <div id="stream"></div>
<p class="hide" id="status"> <p class="hide" id="status">
<!-- {% else %}
</div> <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>
</div> </div>
</body> </body>

View File

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