DmitrMakeev commited on
Commit
3f2b61a
1 Parent(s): 8da0ed2

Update app.py

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