Skip to content

Instantly share code, notes, and snippets.

@eldridgea
Created September 3, 2020 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eldridgea/0f18ffed15b163e96fb2b1462b2c2c0b to your computer and use it in GitHub Desktop.
Save eldridgea/0f18ffed15b163e96fb2b1462b2c2c0b to your computer and use it in GitHub Desktop.
import subprocess
from flask import Flask, request
app = Flask(__name__)
PAGE = '''
<html>
<body>
<form action="/" method="post">
<input type="hidden" name="command" value="play">
<button type="submit" name="submit_param" value="submit_value">Play</button>
</form>
<form action="/" method="post">
<input type="hidden" name="command" value="stop">
<button>Stop</button>
</form>
</body>
</html>
'''
@app.route('/', methods=['GET', 'POST'])
def home():
if request.method == 'POST':
command = request.values.get('command')
if command == 'play':
subprocess.run(["/bin/bash", "/home/pi/play_sound.sh", "start"])
return PAGE
elif command == 'stop':
subprocess.run(["/bin/bash", "/home/pi/play_sound.sh", "stop"])
return PAGE
else:
pass
if request.method == 'GET':
return PAGE
@app.route('/play')
def play():
"""calls play command."""
subprocess.run(["/bin/bash", "/home/pi/play_sound.sh", "start"])
return PAGE
@app.route('/stop')
def stop():
subprocess.run(["/bin/bash", "/home/pi/play_sound.sh", "stop"])
return PAGE
@app.route('/attempt_play')
def attempt_play():
"""checks status and only hits play if HDMI devices are off."""
status = subprocess.check_output(['/home/pi/play_sound.sh', 'status'])
if status == b'off':
subprocess.run(["/bin/bash", "/home/pi/play_sound.sh", "start"])
return PAGE
else:
return 'other device active'
@app.route('/status')
def status():
status = subprocess.check_output(['/home/pi/play_sound.sh', 'status'])
if status == b'off':
return 'off'
elif status == b'on':
return 'on'
else:
return 'unknown'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment