Adding Python script, Dockerfile, site files, and some Git meta

This commit is contained in:
2022-10-26 15:48:31 -04:00
parent 18ea7e7831
commit d54731502e
7 changed files with 121 additions and 0 deletions

6
.dockerignore Normal file
View File

@@ -0,0 +1,6 @@
Dockerfile
.dockerignore
.git
.cache
.venv/
*.pyc

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
.venv

14
Dockerfile Normal file
View File

@@ -0,0 +1,14 @@
FROM python:3-alpine
RUN apk add build-base
RUN addgroup -S flask && addgroup -g 997 -S gpio && adduser -S flask -G flask -G gpio
USER flask
WORKDIR /usr/src/flask_klaxon
COPY . .
RUN pip install --no-cache-dir -r requirements.txt
CMD ["python","klaxon_flask.py"]

52
klaxon_flask.py Executable file
View File

@@ -0,0 +1,52 @@
#!/usr/bin/env python3
from flask import Flask, render_template, request
from paste.translogger import TransLogger
import RPi.GPIO as GPIO
from waitress import serve
RELAIS_1_GPIO = 17
app = Flask(__name__)
def setup():
print('Running setup')
GPIO.setmode(GPIO.BCM)
GPIO.setup(RELAIS_1_GPIO, GPIO.OUT)
GPIO.output(RELAIS_1_GPIO, GPIO.HIGH) # Defaulting to high/off
print('GPIO setup, pin set to off')
@app.route('/')
def home():
return render_template('index.html')
@app.route('/start')
def starthorn():
GPIO.setmode(GPIO.BCM)
GPIO.setup(RELAIS_1_GPIO, GPIO.OUT)
GPIO.output(RELAIS_1_GPIO, GPIO.LOW) # Toggle switch to low/on
return('Sending power to the relay switch, hopefully toggling a horn...')
@app.route('/stop')
def stophorn():
GPIO.setmode(GPIO.BCM)
GPIO.setup(RELAIS_1_GPIO, GPIO.OUT)
GPIO.output(RELAIS_1_GPIO, GPIO.HIGH) # Toggle switch to high/off
GPIO.cleanup()
return('Ceasing power to the relay switch, hopefully silencing a horn...')
def destroy():
return('Cleaning up GPIO')
GPIO.cleanup()
if __name__ == '__main__': # Program entrance
try:
setup()
serve(TransLogger(app, setup_console_handler=False), listen='*:5000')
finally:
destroy()

4
requirements.txt Normal file
View File

@@ -0,0 +1,4 @@
Flask>=2.2.2
Paste>=3.5.2
RPi.GPIO>=0.7.0
waitress>=2.1.2

BIN
static/pumpkin.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

44
templates/index.html Normal file
View File

@@ -0,0 +1,44 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Klaxon horn jump scare button</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css">
<script src="https://kit.fontawesome.com/1449a8617b.js" crossorigin="anonymous"></script>
</head>
<body>
<section class="section hero is-fullheight" style='background-image: url("{{ url_for('static', filename='pumpkin.png' ) }}");'>
<div class="container has-text-centered">
<button class="button is-dark is-large is-fullwidth" id="spoop">
<i class="fa-solid fa-skull m-3"></i> SPOOP <i class="fa-solid fa-bullhorn m-3"></i>
</button>
</div>
</section>
<script type='text/javascript'>
var isTouchDevice = 'ontouchstart' in document.documentElement;
var starturl = '/start'
var stopurl = '/stop'
$( "#spoop" )
.mouseup(function() {
$.getJSON(stopurl, { }, function(data) { }); return false;
})
.mousedown(function() {
$.getJSON(starturl, { }, function(data) { }); return false;
})
$('#spoop').on('touchstart', function(){
if (isTouchDevice) {
$.getJSON(starturl, { }, function(data) { }); return false;
}
});
$('#spoop').on('touchend', function(){
if (isTouchDevice) {
$.getJSON(stopurl, { }, function(data) { }); return false;
}
});
</script>
</body>
</html>