File size: 915 Bytes
dc808c4
 
 
 
 
 
eaae512
 
 
 
dc808c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eaae512
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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)