-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
160 lines (124 loc) · 4.49 KB
/
main.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
#Import necessary libraries
import modules.startup
import modules.camera as camera_controls
import modules.motion as motion
from modules.sysLogger import logger
from flask import Flask, render_template, jsonify, send_from_directory
from waitress import serve
import time, os, threading, psutil
"""
Librarys to install:
pip install flask
pip install picamera2
pip install Pillow
pip install waitress
pip install psutil
pip install requests
"""
#Initialize the Flask app
app = Flask(__name__)
filePath = f"{os.getcwd()}/static/data/photos/"
lowResPath = f"{os.getcwd()}/static/data/photos/LR/"
highResPath = f"{os.getcwd()}/static/data/photos/HR/"
logger.info('Starting server')
thread = threading.Thread(target=motion.start)
# Start camera system on boot
thread.start()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/manual/photo')
def take_photo():
try:
camera_controls.take_photo()
except Exception as e:
logger.error(e)
return(jsonify(status=500, data=str(e)))
return(jsonify(status=200, data=None))
@app.route('/display_image/<image_folder>/<image>')
def display_image(image_folder, image):
image_path = "data/photos/HR/" + image_folder + "/" + image
image_loc = image_folder + "/" + image
return render_template('display_image.html', image_path=image_path, image_name=image, image_loc=image_loc)
@app.route('/start', methods=['GET'])
def start_motion():
global thread
if motion.running:
return(jsonify(status=200, data=None))
else:
try:
thread.start()
return(jsonify(status=200, data=None))
except Exception as e:
logger.critical(e)
return(jsonify(status=200, data=str(e)))
@app.route('/end', methods=['GET'])
def stop_motion():
global thread
try:
motion.stop()
except Exception as e:
logger.error(e)
return(jsonify(status=400, data=str(e)))
else:
return(jsonify(status=200, data=None))
finally:
thread.join()
@app.route('/lowResImg', methods=['GET'])
def lowResImg_loc():
storage = []
try:
files_and_directories = os.listdir(lowResPath)
directories = [d for d in files_and_directories if os.path.isdir(os.path.join(lowResPath, d))]
for d in directories:
newFilePath = str(lowResPath) + str(d)
files_and_directories = os.listdir(newFilePath)
files = [f for f in files_and_directories if os.path.isfile(os.path.join(newFilePath, f))]
files_list = []
for f in files:
files_list.append(f)
current = {
"folder": d,
"files": sorted(files_list[1:], key=lambda x: x.split('.')[0], reverse=True)
}
storage.append(current)
except Exception as e:
logger.error(e)
return(jsonify(status=500, data=str(e)))
return jsonify(status=200, data=storage)
@app.route('/data', methods=['GET'])
def data():
freq = psutil.cpu_freq()
disk = psutil.disk_usage('/')
cpuTemp = round(psutil.sensors_temperatures()['cpu_thermal'][0].current, 2)
cpuFreq = round(freq.current, 2)
totalDisk = round(disk.total / (1024*1024*1024), 2) #GB
usedDisk = round(disk.used / (1024*1024*1024), 2) #GB
last = motion.last()
deviceData = [cpuTemp,cpuFreq,totalDisk,usedDisk]
return jsonify(data=[last,deviceData], status=200) # Return data
@app.route('/delete_image/<image_folder>/<image>', methods=['GET'])
def deleteImage(image_folder, image):
# delete the file
try:
os.remove(lowResPath+"/"+image_folder+"/"+image)
os.remove(highResPath+"/"+image_folder+"/"+image)
except Exception as e:
logger.error(e)
return jsonify(data=str(e), status=500)
return jsonify(data=None, status=200)
@app.route('/download/image/<image_folder>/<image>', methods=['GET'])
def downloadImage(image_folder, image):
image_path = os.path.join(highResPath, image_folder, image)
# Check if the file exists
if os.path.isfile(image_path):
# Serve the file for download
return send_from_directory(directory=os.path.join(highResPath, image_folder), filename=image, as_attachment=True)
else:
# If the file does not exist, return a 404 error
return "File not found", 404
if __name__ == "__main__":
try:
serve(app, host="0.0.0.0", port=8080)
except Exception as e:
logger.critical(f"failed to start sever: {e}")