working speed increase and decrease endpoints

This commit is contained in:
Tomasz Frątczak 2023-11-14 18:22:27 +01:00
parent eb4a70a7b1
commit 1c8980f3a5
2 changed files with 40 additions and 42 deletions

View File

@ -75,50 +75,10 @@ class Headwind:
try: try:
async with self.fanClient as client: async with self.fanClient as client:
await client.write_gatt_char(CHARACTERISTIC, value) await client.write_gatt_char(CHARACTERISTIC, value)
# print("flag")
return True return True
except Exception as e: except Exception as e:
return False return False
else:
return False
async def writeIncreaseSpeed(self):
try:
async with self.fanClient as client:
currentSpeed = await client.read_gatt_char(CHARACTERISTIC)[2]
newSpeed = 0
if currentSpeed < 25:
newSpeed = 25
elif currentSpeed < 50:
newSpeed = 50
elif currentSpeed < 75:
newSpeed = 75
elif currentSpeed < 100:
newSpeed = 100
value = [0x2, newSpeed]
await client.write_gatt_char(CHARACTERISTIC, value)
return True
except Exception as e:
return False
async def writeDecreaseSpeed(self):
try:
async with self.fanClient as client:
currentSpeed = await client.read_gatt_char(CHARACTERISTIC)[2]
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:
return True
value = [0x2, newSpeed]
await client.write_gatt_char(CHARACTERISTIC, value)
return True
except Exception as e:
return False return False
async def writeOff(self): async def writeOff(self):

View File

@ -22,6 +22,44 @@ async def setSpeed(speed):
return f"Failed to set headwind speed to {speed}", 503 return f"Failed to set headwind speed to {speed}", 503
return f"Setting headwind speed to {speed}", 200 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"]) @app.route("/speed", methods=["GET"])
async def getSpeed(): async def getSpeed():
speed = await fan.readSpeed() speed = await fan.readSpeed()