From dab054099ccf112f2344a6c0e5458a98a9f667d6 Mon Sep 17 00:00:00 2001 From: petrucci4prez Date: Fri, 23 Jun 2017 01:50:07 -0400 Subject: [PATCH] no longer require janus to run, and add safety net to restart at web interface --- templates/index.html | 53 +++++++++++++++++++++++--------------------- webInterface.py | 31 +++++++++++++++++++------- 2 files changed, 51 insertions(+), 33 deletions(-) diff --git a/templates/index.html b/templates/index.html index b93cef0..f6198cd 100644 --- a/templates/index.html +++ b/templates/index.html @@ -14,8 +14,10 @@ - - + {% if janusRunning %} + + + {% endif %} @@ -39,33 +41,34 @@ - + {% if janusRunning %} + + {% endif %}
-
- -
-

- -

-
+
+ {% if janusRunning %} +
+

+ {% else %} +

Janus not running. Streaming not available.

+ + {% endif %} +
+ diff --git a/webInterface.py b/webInterface.py index 944eef9..cc4bd79 100644 --- a/webInterface.py +++ b/webInterface.py @@ -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 @@ -16,9 +16,21 @@ werkzeug.setLevel(logging.ERROR) 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'