-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflask_server.py
More file actions
92 lines (74 loc) · 3.07 KB
/
flask_server.py
File metadata and controls
92 lines (74 loc) · 3.07 KB
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
import os
import signal
import subprocess
import image_utils
from flask import Flask, jsonify, request, redirect, render_template
app = Flask(__name__)
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
state = {
"process": None
}
def save_and_show_image(file): # test
img = image_utils.load_image_file(file)
image_utils.resize_and_crop_and_save(img, 'test.ppm', (32, 32))
if state["process"]:
os.killpg(os.getpgid(state["process"].pid), signal.SIGTERM) # Send the signal to all the process groups
state["process"] = subprocess.Popen(["sudo", "./rpi-rgb-led-matrix/examples-api-use/demo", "-D", "1", "-m", "1000", "test.ppm"], stdout=subprocess.PIPE,
shell=False, preexec_fn=os.setsid)
return "Showing the img!"
@app.route('/img', methods=['GET', 'POST'])
def upload_image():
# Check if a valid image file was uploaded
print(request.content_type)
print(request.files)
print(request.content_length)
print(request.form)
if request.method == 'POST':
if 'file' not in request.files and 'file' not in request.form:
return redirect(request.url)
if 'file' in request.files:
print(f"The file is {request.files['file'].filename}")
file = request.files['file']
else:
file = request.form['file']
if file.filename == '':
print("Empty file")
return redirect(request.url)
if file and allowed_file(file.filename):
# The image file seems valid!
return save_and_show_image(file)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/irRemote')
def ir_mode():
# subprocess.Popen(["rm","-r","some.file"])
state["process"] = subprocess.Popen(["python", "IR-Remote-Receiver-Python-Module/IR_Mode.py"], stdout=subprocess.PIPE,
shell=False, preexec_fn=os.setsid)
return "irRemote"
@app.route('/photoSensor')
def photoSensor_mode():
state["process"] = subprocess.Popen(["python", "light-sensor.py"], stdout=subprocess.PIPE,
shell=False, preexec_fn=os.setsid)
return "Yes, turn it on!"
@app.route('/demoScroll', methods=['GET', 'POST'])
def play_demo():
if state["process"]:
os.killpg(os.getpgid(state["process"].pid), signal.SIGTERM) # Send the signal to all the process groups
demoNumber = request.args.get('demoNumber')
state["process"] = subprocess.Popen(["sudo", "./rpi-rgb-led-matrix/examples-api-use/demo", "-D", demoNumber], stdout=subprocess.PIPE,
shell=False, preexec_fn=os.setsid)
return demoNumber
@app.route('/kill')
def turn_off():
if state["process"]:
os.killpg(os.getpgid(state["process"].pid), signal.SIGTERM) # Send the signal to all the process groups
return "Killed it!"
@app.route('/killAll')
def kill_everything():
os.system("sudo killall demo")
@app.route('/webApp')
def webApp():
return render_template("webApp.html")
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0')