Update app.py
Browse files
app.py
CHANGED
|
@@ -1,85 +1,34 @@
|
|
| 1 |
-
import
|
| 2 |
-
from
|
| 3 |
|
| 4 |
-
app = Flask(__name__
|
| 5 |
-
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 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 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 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)))
|