Created
May 5, 2020 08:33
-
-
Save MitI-7/9314f5cabd2b80303984855a6ceebf8c to your computer and use it in GitHub Desktop.
switch bot on windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import binascii | |
from bleak import BleakClient, BleakScanner | |
COMMUNICATION_SERVICE_UUID = "cba20d00-224d-11e6-9fb8-0002a5d5c51b" | |
RX_CHARACTERISTIC_UUID = "cba20002-224d-11e6-9fb8-0002a5d5c51b" | |
TX_CHARACTERISTIC_UUID = "cba20003-224d-11e6-9fb8-0002a5d5c51b" | |
PRESS = "PRESS" | |
ON = "ON" | |
OFF = "OFF" | |
DOWN = "DOWN" | |
UP = "UP" | |
PREFIX_NOPASSWORD = "5701" | |
PREFIX_PASSWORD = "5711" | |
PREFIX_INFO = "5712" | |
SUFFIX_PRESS = "00" | |
SUFFIX_ON = "01" | |
SUFFIX_OFF = "02" | |
SUFFIX_DOWN = "03" | |
SUFFIX_UP = "04" | |
async def scan(): | |
devices = await BleakScanner.discover() | |
for d in devices: | |
print(d.address, d.name, d.metadata) | |
async def send_data(data: bytes, address, loop, timeout): | |
async with BleakClient(address, loop=loop, timeout=timeout) as client: | |
is_connected = await client.is_connected() | |
if is_connected: | |
await client.write_gatt_char(RX_CHARACTERISTIC_UUID, data) | |
else: | |
print("not connected") | |
def make_data(key: str, password=None) -> bytes: | |
d = {PRESS: SUFFIX_PRESS, | |
ON: SUFFIX_ON, | |
OFF: SUFFIX_OFF, | |
DOWN: SUFFIX_DOWN, | |
UP: SUFFIX_UP} | |
assert key in d.keys() | |
prefix = PREFIX_NOPASSWORD | |
if password: | |
prefix = PREFIX_PASSWORD + format(binascii.crc32(password.encode("ascii")) & 0xffffffff, "x") | |
suffix = d[key] | |
return binascii.a2b_hex(prefix + suffix) | |
def main(): | |
address = "" | |
password = None | |
# loop = asyncio.get_event_loop() | |
# loop.run_until_complete(scan()) | |
data = make_data(key=PRESS, password=password) | |
loop.run_until_complete(send_data(data=data, address=address, loop=loop, timeout=10)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment