DmitrMakeev commited on
Commit
109bade
·
verified ·
1 Parent(s): af56c3a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -26
app.py CHANGED
@@ -1,41 +1,71 @@
1
- from flask import Flask, jsonify
2
  from flask_socketio import SocketIO, send, emit
3
  import os
4
 
5
-
 
6
 
7
  app = Flask(__name__)
 
8
  socketio = SocketIO(app)
9
 
10
- # Store the latest sensor data
11
- sensor_data = {}
 
12
 
13
- @app.route('/')
14
- def index():
15
- return "WebSocket Server is running."
16
 
17
- @socketio.on('connect')
18
- def handle_connect():
19
- print('Client connected')
 
 
 
 
 
 
 
20
 
21
- @socketio.on('disconnect')
22
- def handle_disconnect():
23
- print('Client disconnected')
24
-
25
- @socketio.on('message')
26
- def handle_message(data):
27
- global sensor_data
28
- print('Received message:', data)
29
- sensor_data = data
30
- emit('response', {'message': 'Data received'})
31
 
32
- @app.route('/data')
33
- def get_data():
34
- global sensor_data
35
- return jsonify(sensor_data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
 
 
 
 
37
 
 
 
 
 
38
 
39
  if __name__ == '__main__':
40
- socketio.run(app, host='0.0.0.0', port=7860)
41
-
 
1
+ from flask import Flask, request, send_from_directory, render_template_string
2
  from flask_socketio import SocketIO, send, emit
3
  import os
4
 
5
+ UPLOAD_FOLDER = 'static'
6
+ IMAGE_FILENAME = 'latest_image.jpg'
7
 
8
  app = Flask(__name__)
9
+ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
10
  socketio = SocketIO(app)
11
 
12
+ # Создание директории, если она не существует
13
+ if not os.path.exists(UPLOAD_FOLDER):
14
+ os.makedirs(UPLOAD_FOLDER)
15
 
16
+ @app.route('/online', methods=['GET'])
17
+ def online():
18
+ return render_template('online.html')
19
 
20
+ @app.route('/upload', methods=['POST'])
21
+ def upload_file():
22
+ if 'photo' not in request.files:
23
+ return "No file part", 400
24
+ file = request.files['photo']
25
+ if file.filename == '':
26
+ return "No selected file", 400
27
+ save_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
28
+ file.save(save_path)
29
+ return f"File uploaded successfully and saved to {save_path}", 200
30
 
31
+ @app.route('/image', methods=['GET'])
32
+ def get_image():
33
+ return send_from_directory(UPLOAD_FOLDER, IMAGE_FILENAME)
 
 
 
 
 
 
 
34
 
35
+ @app.route('/')
36
+ def index():
37
+ html = '''
38
+ <!DOCTYPE html>
39
+ <html lang="en">
40
+ <head>
41
+ <meta charset="UTF-8">
42
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
43
+ <title>Camera Image</title>
44
+ </head>
45
+ <body>
46
+ <h1>Latest Image</h1>
47
+ <img id="cameraImage" src="/image" alt="Image" style="width:100%;">
48
+ <script>
49
+ setInterval(function(){
50
+ var image = document.getElementById("cameraImage");
51
+ image.src = "/image?" + new Date().getTime();
52
+ }, 10000); // обновление каждые 10 секунд
53
+ </script>
54
+ </body>
55
+ </html>
56
+ '''
57
+ return render_template_string(html)
58
 
59
+ @socketio.on('message')
60
+ def handle_message(msg):
61
+ print('Message: ' + msg)
62
+ send(msg, broadcast=True)
63
 
64
+ @socketio.on('json')
65
+ def handle_json(json):
66
+ print('JSON: ' + str(json))
67
+ send(json, json=True, broadcast=True)
68
 
69
  if __name__ == '__main__':
70
+ socketio.run(app, host='0.0.0.0', port=7860, allow_unsafe_werkzeug=True)
71
+