72 lines
2.0 KiB
Python
72 lines
2.0 KiB
Python
|
#!/usr/bin/env python3
|
||
|
|
||
|
from bleak import BleakClient
|
||
|
|
||
|
ON = [0x4, 0x4, 0x2]
|
||
|
OFF = [0x2, 0x0]
|
||
|
SLEEP = [0x4, 0x1]
|
||
|
HR = [0x4, 0x2]
|
||
|
SPD = [0x4, 0x3]
|
||
|
MIN_SPEED = [0x2, 0x1]
|
||
|
HALF_SPEED = [0x2, 0x32]
|
||
|
FULL_SPEED = [0x2, 0x64]
|
||
|
CHARACTERISTIC = "a026e038-0a7d-4ab3-97fa-f1500f9feb8b"
|
||
|
|
||
|
class Headwind:
|
||
|
fanClient = None
|
||
|
flaskApp = None
|
||
|
def __init__(self, flaskApp, address):
|
||
|
self.flaskApp = flaskApp
|
||
|
self.fanClient = BleakClient(address)
|
||
|
|
||
|
async def sleep(self):
|
||
|
try:
|
||
|
async with self.fanClient as client:
|
||
|
await client.write_gatt_char(CHARACTERISTIC, SLEEP)
|
||
|
return True
|
||
|
except Exception as e:
|
||
|
return False
|
||
|
|
||
|
async def on(self):
|
||
|
try:
|
||
|
async with self.fanClient as client:
|
||
|
await client.write_gatt_char(CHARACTERISTIC, ON)
|
||
|
return True
|
||
|
except Exception as e:
|
||
|
return False
|
||
|
|
||
|
async def speedMode(self):
|
||
|
try:
|
||
|
async with self.fanClient as client:
|
||
|
await client.write_gatt_char(CHARACTERISTIC, SPD)
|
||
|
return True
|
||
|
except Exception as e:
|
||
|
return False
|
||
|
|
||
|
async def hrMode(self):
|
||
|
try:
|
||
|
async with self.fanClient as client:
|
||
|
await client.write_gatt_char(CHARACTERISTIC, HR)
|
||
|
return True
|
||
|
except Exception as e:
|
||
|
return False
|
||
|
|
||
|
async def manualSpeed(self, speed):
|
||
|
if speed > 0:
|
||
|
value = [0x2, speed]
|
||
|
try:
|
||
|
async with self.fanClient as client:
|
||
|
await client.write_gatt_char(CHARACTERISTIC, value)
|
||
|
return True
|
||
|
except Exception as e:
|
||
|
return False
|
||
|
else:
|
||
|
return False
|
||
|
|
||
|
async def off(self):
|
||
|
try:
|
||
|
async with self.fanClient as client:
|
||
|
await client.write_gatt_char(CHARACTERISTIC, OFF)
|
||
|
return True
|
||
|
except Exception as e:
|
||
|
return False
|