Skip to content

Instantly share code, notes, and snippets.

@eldridgea

eldridgea/sm.py

Created Sep 3, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.