-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
180 lines (127 loc) · 4.9 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import pathlib
import urllib
import zoneinfo
from pprint import pprint
import gtts.lang
from flask import Flask, render_template, request, redirect, url_for, send_from_directory, make_response
import os
import subprocess
import RPi.GPIO as GPIO
import time
from gtts import gTTS
from datetime import datetime
app = Flask(__name__)
# Webserver Port (HTTP)
http_port = 8000
# App-Name (HTML)
app_name = "37C3 Funkzentrale"
# App-Name (PWA / Progressive Web App)
app_pwa_name = "37C3 DMR-Funkzentrale"
app_pwa_short_name = "37C3 Funk"
# App-Logo in '/static' Sub-Folder
app_logo = "logo.png"
# Push-to-Talk GPIO (BCM / Broadcom notation, https://pinout.xyz/)
gpio_pin = 17
# Audio-Files Sub-Folder
audio_folder = 'audio_files'
# GPIO INIT
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(gpio_pin, GPIO.OUT)
GPIO.output(gpio_pin, GPIO.LOW)
@app.route('/')
def index():
# audio_files = [f for f in os.listdir(audio_folder) if f.endswith(('.mp3', '.wav', 'm4a', 'aac'))]
audio_files = generate_tree(audio_folder)
tts_langs = gtts.lang.tts_langs()
return render_template('index.html.j2', audio_files=audio_files, tts_langs=tts_langs, app_name=app_name,
app_logo=app_logo)
@app.route('/cancel', methods=['POST'])
def cancel_playback():
subprocess.run(['killall', '-9', 'mplayer'])
return redirect("/")
@app.route('/play/<path:filename>')
def play_audio(filename):
selected_audio = filename
print(selected_audio)
print(os.path.join(audio_folder, selected_audio))
GPIO.output(gpio_pin, GPIO.HIGH)
time.sleep(1.0)
subprocess.run(['open', os.path.join(audio_folder, selected_audio)])
#subprocess.run(['mplayer', '-ao', 'alsa', os.path.join(audio_folder, selected_audio)])
GPIO.output(gpio_pin, GPIO.LOW)
return redirect('/')
@app.route('/tts', methods=['POST'])
def play_tts():
tts_text = request.form['tts']
tts_lang = request.form['lang']
print(tts_lang)
say(tts_text, tts_lang)
return redirect('/')
@app.route('/time', methods=['POST'])
def time_announce():
tz = zoneinfo.ZoneInfo("Europe/Berlin")
tts_text = f"Es folgt eine Servicemeldung: Es ist nun {datetime.now(tz).strftime('%H:%M')} und {datetime.now(tz).strftime('%S')} Sekunden!"
say(tts_text, 'de')
return redirect("/")
@app.route('/upload', methods=['POST'])
def upload_file():
uploaded_file = request.files['file']
if uploaded_file.filename != '':
allowed_extensions = {'mp3', 'wav', 'm4a', 'aac'}
file_extension = uploaded_file.filename.rsplit('.', 1)[-1].lower()
if file_extension in allowed_extensions:
uploaded_file.save(os.path.join(audio_folder, uploaded_file.filename))
return redirect('/')
@app.route('/delete/<filename>')
def delete_file(filename):
file_path = os.path.join(audio_folder, filename)
if os.path.exists(file_path):
os.remove(file_path)
return redirect('/')
@app.route('/rename/<old_filename>/<new_filename>')
def rename_file(old_filename, new_filename):
old_path = os.path.join(audio_folder, old_filename)
new_path = os.path.join(audio_folder, new_filename)
if os.path.exists(old_path):
os.rename(old_path, new_path)
return redirect('/')
@app.route('/audio_files/<path:filename>')
def serve_audio(filename):
print(filename)
return send_from_directory(audio_folder, filename)
@app.route('/files')
def file_browser():
audio_files = [f for f in os.listdir(audio_folder)]
return render_template('file_browser.html.j2', audio_files=audio_files)
@app.route('/manifest.json')
def manifest():
return render_template('manifest.json.j2', app_pwa_name=app_pwa_name, app_pwa_short_name=app_pwa_short_name)
@app.route('/service-worker.js')
def service_worker():
return send_from_directory('static', 'service-worker.js', mimetype='application/javascript')
# Helper Funcs
def say(tts_text, tts_lang):
tts = gTTS(tts_text, lang=tts_lang)
tts.save(os.path.join('/tmp', 'tts.mp3'))
GPIO.output(gpio_pin, GPIO.HIGH)
time.sleep(0.2)
subprocess.run(['mplayer', '-ao', 'alsa', os.path.join('/tmp', 'tts.mp3')])
os.remove(os.path.join('/tmp', 'tts.mp3'))
GPIO.output(gpio_pin, GPIO.LOW)
def generate_tree(root_dir):
tree = {'name': os.path.basename(root_dir), 'type': '0folder', 'children': []}
try:
for entry in os.listdir(root_dir):
full_path = os.path.join(root_dir, entry)
if os.path.isdir(full_path):
tree['children'].append(generate_tree(full_path))
else:
corrected_path = pathlib.Path(*pathlib.Path(full_path).parts[1:])
tree['children'].append({'name': entry, 'type': '1file', 'full_path': corrected_path})
except OSError:
pass
tree['children'] = sorted(tree['children'], key=lambda x: (x['type'], x['name']))
return tree
if __name__ == '__main__':
app.run(host='0.0.0.0', port=http_port, debug=True)