macropad/main.py
2022-11-29 21:22:30 +01:00

83 lines
2.0 KiB
Python

from flask import Flask, render_template, request
from os import system
app = Flask(__name__)
#define actuators GPIOs
ledRed = 13
ledYlw = 19
ledGrn = 26
#initialize GPIO status variables
ledRedSts = 0
ledYlwSts = 0
ledGrnSts = 0
def check_app_states(apps):
for app in apps:
if 'check_cmd' in app:
exitcode = system(app['check_cmd'])
app['default'] = app['default'] if exitcode == 0 else False
@app.route("/")
def index():
if request.args.get('cmd') is not None:
exitcode = system(request.args.get('cmd'))
if exitcode == 0:
return "Command was run", 200
else:
return "Command failed (exit code {0})".format(exitcode), 500
templateData = {
'is_local': True if request.remote_addr == "127.0.0.1" else False,
'apps': [
{
'name': 'clock',
'cmd': 'espeak "$text"'
},
{
'name': 'toggle',
'text_on': 'ON',
'text_off': 'OFF',
'cmd_on': 'espeak "on"',
'cmd_off': 'espeak "off"',
'cooldown': 3000
},
{
'name': 'toggle',
'text_true': '🔊',
'text_false': '🔇',
'default': False,
'cmd_true': 'mpg123 ~/tng_viewscreen_on.mp3',
'cmd_false': 'mpg123 ~/tng_viewscreen_off.mp3',
'cooldown': 1000,
'show_cooldown': False
},
{
'name': 'toggle',
'text_true': '🔊',
'text_false': '🔇',
'default': False,
'cmd_true': 'touch ~/testfile.tmp',
'cmd_false': 'rm ~/testfile.tmp',
'check_cmd': 'test -f ~/testfile.tmp',
'cooldown': 1000,
'show_cooldown': False
}
]
}
check_app_states(templateData)
return render_template('index.html', **templateData)
@app.route("/configure")
def configure():
return render_template('configure.html')
@app.route("/<deviceName>/<action>")
def action(deviceName, action):
templateData = {}
return render_template('index.html', **templateData)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8081, debug=True)