102 lines
2.2 KiB
Python
102 lines
2.2 KiB
Python
from flask import Flask, jsonify, render_template, request
|
|
from esp32_sim import esp32
|
|
from pico_sim import PicoSimulator
|
|
|
|
app = Flask(__name__)
|
|
|
|
pico = PicoSimulator(esp32)
|
|
|
|
|
|
@app.route("/")
|
|
def index():
|
|
return render_template("index.html")
|
|
|
|
|
|
@app.route("/status")
|
|
def status():
|
|
return jsonify(pico.get_status())
|
|
|
|
|
|
@app.route("/battery")
|
|
def battery():
|
|
return jsonify(pico.get_status().get("battery", {}))
|
|
|
|
|
|
@app.route("/temps")
|
|
def temps():
|
|
return jsonify(pico.get_status().get("temps", {}))
|
|
|
|
|
|
@app.route("/relays")
|
|
def relays():
|
|
return jsonify(pico.get_status().get("relays", {}))
|
|
|
|
|
|
@app.route("/relay/<name>", methods=["POST"])
|
|
def set_relay(name):
|
|
data = request.get_json(force=True)
|
|
response = pico.set_relay(name, data.get("state", False))
|
|
|
|
if not response.get("success"):
|
|
return jsonify(response), 503
|
|
|
|
return jsonify(response)
|
|
|
|
|
|
@app.route("/network")
|
|
def network():
|
|
return jsonify(pico.get_status().get("network", {}))
|
|
|
|
|
|
@app.route("/network/wifi", methods=["POST"])
|
|
def enable_wifi():
|
|
data = request.get_json(force=True)
|
|
minutes = int(data.get("minutes", 10))
|
|
response = pico.enable_wifi(minutes)
|
|
|
|
if not response.get("success"):
|
|
return jsonify(response), 503
|
|
|
|
return jsonify(response)
|
|
|
|
|
|
@app.route("/vehicle/ignition", methods=["POST"])
|
|
def toggle_ignition():
|
|
response = pico.toggle_ignition()
|
|
|
|
if not response.get("success"):
|
|
return jsonify(response), 503
|
|
|
|
return jsonify(response)
|
|
|
|
|
|
@app.route("/sensor/<name>/fault", methods=["POST"])
|
|
def toggle_sensor_fault(name):
|
|
response = pico.toggle_sensor_fault(name)
|
|
|
|
if not response.get("success"):
|
|
return jsonify(response), 503
|
|
|
|
return jsonify(response)
|
|
|
|
|
|
@app.route("/comms")
|
|
def comms():
|
|
return jsonify(pico.get_comms())
|
|
|
|
|
|
@app.route("/comms/rs485/disconnect", methods=["POST"])
|
|
def disconnect_rs485():
|
|
pico.disconnect_rs485()
|
|
return jsonify({"success": True, "rs485_connected": False})
|
|
|
|
|
|
@app.route("/comms/rs485/restore", methods=["POST"])
|
|
def restore_rs485():
|
|
pico.restore_rs485()
|
|
return jsonify({"success": True, "rs485_connected": True})
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app.run(host="0.0.0.0", port=5000, debug=True)
|