SENSOR / cloud_api.py
rishab1090's picture
Update cloud_api.py
eaae512 verified
raw
history blame contribute delete
915 Bytes
from flask import Flask, request, jsonify
app = Flask(__name__)
latest_data = {}
history = []
@app.route("/")
def root():
return jsonify({"message": "Sensor API is running!", "routes": ["/sensor/ingest", "/sensor/latest", "/sensor/history"]})
@app.route("/sensor/ingest", methods=["POST"])
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"})
@app.route("/sensor/latest", methods=["GET"])
def get_latest():
if not latest_data:
return jsonify({"error": "No data yet"}), 404
return jsonify(latest_data)
@app.route("/sensor/history", methods=["GET"])
def get_history():
return jsonify(history)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)