Spaces:
Sleeping
Sleeping
| from flask import Flask, request, jsonify | |
| app = Flask(__name__) | |
| latest_data = {} | |
| history = [] | |
| def root(): | |
| return jsonify({"message": "Sensor API is running!", "routes": ["/sensor/ingest", "/sensor/latest", "/sensor/history"]}) | |
| def ingest(): | |
| global latest_data, history | |
| data = request.json | |
| if not data: | |
| return jsonify({"error": "No JSON received"}), 400 | |
| latest_data = data | |
| history.append(data) | |
| if len(history) > 500: | |
| history.pop(0) | |
| return jsonify({"status": "ok"}) | |
| def get_latest(): | |
| if not latest_data: | |
| return jsonify({"error": "No data yet"}), 404 | |
| return jsonify(latest_data) | |
| def get_history(): | |
| return jsonify(history) | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=7860) | |