bluewind/bluewind/views.py

80 lines
2.3 KiB
Python
Executable File

#!/usr/bin/env python3
from bluewind import app, fan
@app.route("/on", methods=["POST"])
async def setOn():
fan_status = await fan.writeOn()
if not fan_status:
return "Failed to turn headwind on", 503
return "Turning headwind on", 200
@app.route("/sleep", methods=["POST"])
async def setSleep():
fan_status = await fan.writeSleep()
if not fan_status:
return "Failed to put headwind to sleep", 503
return "Putting headwind to sleep", 200
@app.route("/speed/<int:speed>", methods=["POST"])
async def setSpeed(speed):
fan_status = await fan.writeSpeed(speed)
if not fan_status:
return f"Failed to set headwind speed to {speed}", 503
return f"Setting headwind speed to {speed}", 200
@app.route("/speed/increase", methods=["POST"])
async def setIncreaseSpeed():
print("increase speed")
currentSpeed = await fan.readSpeed()
newSpeed = 0
if currentSpeed < 25:
newSpeed = 25
elif currentSpeed < 50:
newSpeed = 50
elif currentSpeed < 75:
newSpeed = 75
elif currentSpeed < 100:
newSpeed = 100
fan_status = await fan.writeSpeed(newSpeed)
if not fan_status:
return f"Failed to increase speed by 25%", 503
return f"Increased speed by 25%", 200
@app.route("/speed/decrease", methods=["POST"])
async def setDecreaseSpeed():
print("decrease speed")
currentSpeed = await fan.readSpeed()
newSpeed = 0
if currentSpeed == 100:
newSpeed = 75
elif currentSpeed >= 75:
newSpeed = 50
elif currentSpeed >= 50:
newSpeed = 25
elif currentSpeed > 25:
newSpeed = 25
elif currentSpeed <= 25:
newSpeed = 1
fan_status = await fan.writeSpeed(newSpeed)
if not fan_status:
return f"Failed to decrease speed by 25%", 503
return f"Decreased speed by 25%", 200
@app.route("/speed", methods=["GET"])
async def getSpeed():
speed = await fan.readSpeed()
return f"{speed}", 200
@app.route("/hr", methods=["POST"])
async def setHr():
fan_status = await fan.writeHr()
if not fan_status:
return "Failed to set headwind to HR mode", 503
return "Setting headwind to HR mode", 200
@app.route("/off", methods=["POST"])
async def setOff():
fan_status = await fan.off()
if not fan_status:
return "Failed to turn headwind off", 503
return "Turning headwind off", 200