bluewind/api.py

52 lines
1.6 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
from flask import Flask
from headwind import Headwind as Headwind
app = Flask(__name__)
app.config.from_prefixed_env()
2023-11-10 22:30:00 +00:00
@app.route("/on", methods=["POST"])
async def on():
fan = Headwind(app, app.config["ADDRESS"])
2023-11-10 22:30:00 +00:00
fan_status = await fan.writeOn()
if not fan_status:
2023-11-10 22:30:00 +00:00
return "Failed to turn headwind on", 503
return "Turning headwind on", 200
2023-11-10 22:30:00 +00:00
@app.route("/sleep", methods=["POST"])
async def sleep():
fan = Headwind(app, app.config["ADDRESS"])
2023-11-10 22:30:00 +00:00
fan_status = await fan.writeSleep()
if not fan_status:
2023-11-10 22:30:00 +00:00
return "Failed to put headwind to sleep", 503
return "Putting headwind to sleep", 200
2023-11-10 22:30:00 +00:00
@app.route("/speed/<int:speed>", methods=["POST"])
async def speed(speed):
fan = Headwind(app, app.config["ADDRESS"])
2023-11-10 22:30:00 +00:00
fan_status = await fan.writeSpeed(speed)
if not fan_status:
2023-11-10 22:30:00 +00:00
return f"Failed to set headwind speed to {speed}", 503
return f"Setting headwind speed to {speed}", 200
2023-11-10 22:30:00 +00:00
@app.route("/speed", methods=["GET"])
async def getSpeed():
fan = Headwind(app, app.config["ADDRESS"])
2023-11-10 22:30:00 +00:00
speed = await fan.readSpeed()
return f"{speed}", 200
@app.route("/hr", methods=["POST"])
async def writeHr():
fan = Headwind(app, app.config["ADDRESS"])
fan_status = await fan.writeHr()
if not fan_status:
2023-11-10 22:30:00 +00:00
return "Failed to set headwind to HR mode", 503
return "Setting headwind to HR mode", 200
2023-11-10 22:30:00 +00:00
@app.route("/off", methods=["POST"])
async def writeOff():
fan = Headwind(app, app.config["ADDRESS"])
fan_status = await fan.off()
if not fan_status:
2023-11-10 22:30:00 +00:00
return "Failed to turn headwind off", 503
return "Turning headwind off", 200