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>
<script type="text/javascript" src="{{ url_for('siteRoot.static', filename='janus.js') }}"></script> {% if janusRunning %}
<script type="text/javascript" src="{{ url_for('siteRoot.static', filename='streamingtest.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>
{% endif %}
</head> </head>
<body> <body>
@ -39,33 +41,34 @@
</div> </div>
</form> </form>
</ul> </ul>
<ul class="nav navbar-nav navbar-right"> {% if janusRunning %}
<li class="dropdown"> <ul class="nav navbar-nav navbar-right">
<a id="streamset" class="dropdown-toggle" data-toggle="dropdown"> <li class="dropdown">
Pick Stream<span class="caret"></span> <a id="streamset" class="dropdown-toggle" data-toggle="dropdown">
</a> Pick Stream<span class="caret"></span>
<ul id="streamslist" class="dropdown-menu" role="menu"></ul> </a>
</li> <ul id="streamslist" class="dropdown-menu" role="menu"></ul>
<button class="btn btn-default navbar-btn" autocomplete="off" id="watch">Start</button> </li>
</ul> <button class="btn btn-default navbar-btn" autocomplete="off" id="watch">Start</button>
</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 id="stream"></div>
<div class="panel-heading"> <p class="hide" id="status">
<h3 class="panel-title">Stream <span class="label label-info hide" id="status"></h3> {% 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">
<div id="stream"></div> {{ janusRestart.hidden_tag() }}
<p class="hide" id="status"> {{ janusRestart.submitRestart(class_="btn btn-default") }}
<!-- </form>
</div> {% endif %}
--> </div>
</div> </div>
</div>
</body> </body>
</html> </html>

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
@ -16,9 +16,21 @@ werkzeug.setLevel(logging.ERROR)
class TTSForm(FlaskForm): 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'