2018-02-05 23:25:32 +01:00
|
|
|
""" Sonnetteweb — Enfin une sonnette à l'Arcoloc
|
|
|
|
|
|
|
|
Sonnez via le réseau local. C'est cool, hein ?
|
|
|
|
"""
|
|
|
|
|
|
|
|
from datetime import datetime, timedelta
|
2018-02-06 01:42:11 +01:00
|
|
|
import subprocess
|
2018-02-05 23:25:32 +01:00
|
|
|
from flask import Flask, render_template, request
|
|
|
|
|
|
|
|
app = Flask('sonnetteweb')
|
|
|
|
|
|
|
|
|
|
|
|
class Ringer:
|
|
|
|
_instance = None
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.last_rung = datetime.fromtimestamp(0)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get():
|
|
|
|
if Ringer._instance is None:
|
|
|
|
Ringer._instance = Ringer()
|
|
|
|
return Ringer._instance
|
|
|
|
|
|
|
|
def ring(self):
|
|
|
|
if datetime.now() - self.last_rung < timedelta(seconds=5):
|
|
|
|
return False
|
|
|
|
self.last_rung = datetime.now()
|
|
|
|
|
2018-02-06 01:42:11 +01:00
|
|
|
print('Dring!')
|
|
|
|
|
|
|
|
subprocess.run(['bash',
|
|
|
|
'-c',
|
|
|
|
'aplay ringtone.wav &'])
|
2018-02-05 23:25:32 +01:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/', methods=['GET', 'POST'])
|
|
|
|
def view_home():
|
|
|
|
context = {}
|
|
|
|
if request.method == 'POST':
|
|
|
|
if request.form['ding'] == 'dong':
|
|
|
|
if Ringer.get().ring():
|
|
|
|
context['has_rung'] = True
|
|
|
|
else:
|
|
|
|
context['too_much_dring'] = True
|
|
|
|
|
|
|
|
return render_template('homepage.html', **context)
|