add read mode

This commit is contained in:
Tomasz Frątczak 2023-11-10 23:30:00 +01:00
parent 8ca102de26
commit a156c46015
3 changed files with 65 additions and 29 deletions

50
api.py
View File

@ -5,42 +5,48 @@ from headwind import Headwind as Headwind
app = Flask(__name__) app = Flask(__name__)
app.config.from_prefixed_env() app.config.from_prefixed_env()
@app.route("/on") @app.route("/on", methods=["POST"])
async def on(): async def on():
fan = Headwind(app, app.config["ADDRESS"]) fan = Headwind(app, app.config["ADDRESS"])
fan_status = await fan.on() fan_status = await fan.writeOn()
if not fan_status: if not fan_status:
return "<p>Failed to turn headwind on</p>", 503 return "Failed to turn headwind on", 503
return "<p>Turning headwind on</p>", 200 return "Turning headwind on", 200
@app.route("/sleep") @app.route("/sleep", methods=["POST"])
async def sleep(): async def sleep():
fan = Headwind(app, app.config["ADDRESS"]) fan = Headwind(app, app.config["ADDRESS"])
fan_status = await fan.sleep() fan_status = await fan.writeSleep()
if not fan_status: if not fan_status:
return "<p>Failed to put headwind to sleep</p>", 503 return "Failed to put headwind to sleep", 503
return "<p>Putting headwind to sleep</p>", 200 return "Putting headwind to sleep", 200
@app.route("/speed/<int:speed>") @app.route("/speed/<int:speed>", methods=["POST"])
async def speed(speed): async def speed(speed):
fan = Headwind(app, app.config["ADDRESS"]) fan = Headwind(app, app.config["ADDRESS"])
fan_status = await fan.manualSpeed(speed) fan_status = await fan.writeSpeed(speed)
if not fan_status: if not fan_status:
return f"<p>Failed to set headwind speed to {speed}</p>", 503 return f"Failed to set headwind speed to {speed}", 503
return f"<p>Setting headwind speed to {speed}</p>", 200 return f"Setting headwind speed to {speed}", 200
@app.route("/hr") @app.route("/speed", methods=["GET"])
async def hr(): async def getSpeed():
fan = Headwind(app, app.config["ADDRESS"]) fan = Headwind(app, app.config["ADDRESS"])
fan_status = await fan.hr() speed = await fan.readSpeed()
if not fan_status: return f"{speed}", 200
return "<p>Failed to set headwind to HR mode</p>", 503
return "<p>Setting headwind to HR mode</p>", 200
@app.route("/off") @app.route("/hr", methods=["POST"])
async def off(): async def writeHr():
fan = Headwind(app, app.config["ADDRESS"])
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 writeOff():
fan = Headwind(app, app.config["ADDRESS"]) fan = Headwind(app, app.config["ADDRESS"])
fan_status = await fan.off() fan_status = await fan.off()
if not fan_status: if not fan_status:
return "<p>Failed to turn headwind off</p>", 503 return "Failed to turn headwind off", 503
return "<p>Turning headwind off</p>", 200 return "Turning headwind off", 200

View File

@ -11,6 +11,8 @@ MIN_SPEED = [0x2, 0x1]
HALF_SPEED = [0x2, 0x32] HALF_SPEED = [0x2, 0x32]
FULL_SPEED = [0x2, 0x64] FULL_SPEED = [0x2, 0x64]
CHARACTERISTIC = "a026e038-0a7d-4ab3-97fa-f1500f9feb8b" CHARACTERISTIC = "a026e038-0a7d-4ab3-97fa-f1500f9feb8b"
# > read the above characteristic 0xFD-01-XX-04 where XX is speed
# > bytearray(b'\xfd\x01\x00\x01')
class Headwind: class Headwind:
fanClient = None fanClient = None
@ -19,7 +21,23 @@ class Headwind:
self.flaskApp = flaskApp self.flaskApp = flaskApp
self.fanClient = BleakClient(address) self.fanClient = BleakClient(address)
async def sleep(self): async def readSpeed(self):
try:
async with self.fanClient as client:
result = await client.read_gatt_char(CHARACTERISTIC)
return result[2]
except Exception:
return 0
async def readMode(self):
try:
async with self.fanClient as client:
result = await client.read_gatt_char(CHARACTERISTIC)
return result[3] # Return state code
except Exception:
return 0
async def writeSleep(self):
try: try:
async with self.fanClient as client: async with self.fanClient as client:
await client.write_gatt_char(CHARACTERISTIC, SLEEP) await client.write_gatt_char(CHARACTERISTIC, SLEEP)
@ -27,7 +45,7 @@ class Headwind:
except Exception as e: except Exception as e:
return False return False
async def on(self): async def writeOn(self):
try: try:
async with self.fanClient as client: async with self.fanClient as client:
await client.write_gatt_char(CHARACTERISTIC, ON) await client.write_gatt_char(CHARACTERISTIC, ON)
@ -35,7 +53,7 @@ class Headwind:
except Exception as e: except Exception as e:
return False return False
async def speedMode(self): async def writeSpeedMode(self):
try: try:
async with self.fanClient as client: async with self.fanClient as client:
await client.write_gatt_char(CHARACTERISTIC, SPD) await client.write_gatt_char(CHARACTERISTIC, SPD)
@ -43,7 +61,7 @@ class Headwind:
except Exception as e: except Exception as e:
return False return False
async def hrMode(self): async def writeHrMode(self):
try: try:
async with self.fanClient as client: async with self.fanClient as client:
await client.write_gatt_char(CHARACTERISTIC, HR) await client.write_gatt_char(CHARACTERISTIC, HR)
@ -51,7 +69,7 @@ class Headwind:
except Exception as e: except Exception as e:
return False return False
async def manualSpeed(self, speed): async def writeSpeed(self, speed):
if speed > 0: if speed > 0:
value = [0x2, speed] value = [0x2, speed]
try: try:
@ -63,7 +81,7 @@ class Headwind:
else: else:
return False return False
async def off(self): async def writeOff(self):
try: try:
async with self.fanClient as client: async with self.fanClient as client:
await client.write_gatt_char(CHARACTERISTIC, OFF) await client.write_gatt_char(CHARACTERISTIC, OFF)

View File

@ -16,3 +16,15 @@ service001e_char001f = "a026e038-0a7d-4ab3-97fa-f1500f9feb8b" # Vendor
service001e_char001f_desc0021 = "00002902-0000-1000-8000-00805f9b34fb" # Client Characteristic Configuration service001e_char001f_desc0021 = "00002902-0000-1000-8000-00805f9b34fb" # Client Characteristic Configuration
handle = 0x0000 handle = 0x0000
address = 'F2:B3:F7:6A:24:48' address = 'F2:B3:F7:6A:24:48'
# bytearray(b'\xfd\x01\x19\x04')
# 127.0.0.1 - - [10/Nov/2023 15:56:50] "GET /get/speed HTTP/1.1" 200 -
# 127.0.0.1 - - [10/Nov/2023 15:57:14] "GET /speed/32 HTTP/1.1" 200 -
# bytearray(b'\xfd\x01 \x04')
# 127.0.0.1 - - [10/Nov/2023 15:57:23] "GET /get/speed HTTP/1.1" 200 -
# 127.0.0.1 - - [10/Nov/2023 15:58:01] "GET /speed/50 HTTP/1.1" 200 -
# bytearray(b'\xfd\x012\x04')
# 127.0.0.1 - - [10/Nov/2023 15:58:14] "GET /get/speed HTTP/1.1" 200 -
# 127.0.0.1 - - [10/Nov/2023 15:58:37] "GET /speed/100 HTTP/1.1" 200 -
# bytearray(b'\xfd\x01d\x04')
# 127.0.0.1 - - [10/Nov/2023 15:58:45] "GET /get/speed HTTP/1.1" 200 -