90 lines
2.6 KiB
Python
90 lines
2.6 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"
|
|
# > read the above characteristic 0xFD-01-XX-04 where XX is speed
|
|
# > bytearray(b'\xfd\x01\x00\x01')
|
|
|
|
class Headwind:
|
|
fanClient = None
|
|
flaskApp = None
|
|
def __init__(self, flaskApp, address):
|
|
self.flaskApp = flaskApp
|
|
self.fanClient = BleakClient(address)
|
|
|
|
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:
|
|
async with self.fanClient as client:
|
|
await client.write_gatt_char(CHARACTERISTIC, SLEEP)
|
|
return True
|
|
except Exception as e:
|
|
return False
|
|
|
|
async def writeOn(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 writeSpeedMode(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 writeHrMode(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 writeSpeed(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 writeOff(self):
|
|
try:
|
|
async with self.fanClient as client:
|
|
await client.write_gatt_char(CHARACTERISTIC, OFF)
|
|
return True
|
|
except Exception as e:
|
|
return False |