From 087ac554ceba60a4a75d9d022909fad1ce504c21 Mon Sep 17 00:00:00 2001 From: nouser2013 <6624032+nouser2013@users.noreply.github.com> Date: Tue, 28 Apr 2020 05:37:01 +0200 Subject: [PATCH] Update lighthouse-v2-manager.py - added MAC address format checking - simplified command line handling - added option to automatically create Desktop Shortcuts for both the python script version and the binary stand-alone version - added more descriptive text output and error messages --- lighthouse-v2-manager.py | 107 ++++++++++++++++++++++++++++++--------- 1 file changed, 84 insertions(+), 23 deletions(-) diff --git a/lighthouse-v2-manager.py b/lighthouse-v2-manager.py index e35d616..9a3145b 100644 --- a/lighthouse-v2-manager.py +++ b/lighthouse-v2-manager.py @@ -1,6 +1,7 @@ import asyncio import sys -from bleak import discover, BleakClient +import re +import os __PWR_SERVICE = "00001523-1212-efde-1523-785feabcd124" __PWR_CHARACTERISTIC = "00001525-1212-efde-1523-785feabcd124" @@ -8,41 +9,56 @@ __PWR_STANDBY = bytearray([0x00]) command = "discover" -# hard code mac addresses here if you want, otherwise specify in command line -# lh_macs = ["AA:BB:CC:DD:EE:FF", "FF:EE:DD:CC:BB:AA"] -lh_macs = [] +lh_macs = [] # hard code mac addresses here if you want, otherwise specify in command line print(" ") print("=== LightHouse V2 Manager ===") print(" ") -if "--help" in sys.argv or "-?" in sys.argv: - print(" discover lighthouses: "+sys.argv[0]) - print(" set state on/standby: "+sys.argv[0]+" [on|off] [MAC1] [MAC2] [MACn]") - print(" ") - sys.exit() -if len(sys.argv)==1: +cmdPath = os.path.abspath(sys.argv[0]) +cmdFile = os.path.basename(cmdPath) +if cmdFile.find(" ")>0: + cmdFile = '"' + cmdFile +'"' +cmdStr = cmdPath +if cmdStr.find(".py")>0: + cmdStr = '"'+ sys.executable +'" "' + cmdPath + '"' + +if len(sys.argv)==1 or sys.argv[1] in ["--create-shortcuts","-cs"]: print(">> MODE: discover suitable V2 lighthouses") print(" ") -elif sys.argv[1] == "on": +elif sys.argv[1] in ["on","off"]: + command = sys.argv[1] + print(">> MODE: switch lighthouses "+ command.upper()) lh_macs.extend(sys.argv[2:]) - command = "on" - print(">> MODE: switch lighthouses on") - for mac in lh_macs: print(" * "+mac) - print(" ") -elif sys.argv[1] == "off": - lh_macs.extend(sys.argv[2:]) - command = "standby" - print(">> MODE: switch lighthouses to standby") - for mac in lh_macs: print(" * "+mac) + for mac in list(lh_macs): + if re.match("[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}", mac): + continue + print(" * Invalid MAC address format: "+mac) + lh_macs.remove(mac) + if len(lh_macs) == 0: + print(" ") + print(">> ERROR: no (valid) base station MAC addresses given.") + sys.exit() + for mac in lh_macs: + print(" * "+mac) print(" ") else: - print(">> ERROR: no suitable command given. Try running with --help or -?") + print(" discover lighthouses V2:") + print(" "+cmdStr+" [--create-shortcuts, -cs]") + print(" ") + print(" power one or more lighthoses V2 ON:") + print(" "+cmdStr +" on [MAC1] [MAC2] [...MACn]") + print(" ") + print(" power one or more lighthoses V2 OFF:") + print(" "+cmdStr +" off [MAC1] [MAC2] [...MACn]") print(" ") sys.exit() -async def run(loop): +from bleak import discover, BleakClient + +async def run(loop, lh_macs): if command == "discover": + lh_macs = [] print (">> Discovering BLE devices...") devices = await discover() for d in devices: @@ -65,11 +81,56 @@ async def run(loop): print(" OK: Characteristic "+ __PWR_CHARACTERISTIC +" found.") print(">> This seems to be a valid V2 Base Station.") print(" ") + lh_macs.append(d.address) deviceOk = True if not deviceOk: print(">> ERROR: Service or Characteristic not found.") print(">> This is likely NOT a suitable Lighthouse V2.") print(" ") + if len(lh_macs)>0: + print(">> At least one compatible V2 lighthouse was found.") + print(" ") + if "--create-shortcuts" in sys.argv or "-cs" in sys.argv: + print(">> Trying to create Desktop Shortcuts...") + import winshell + from win32com.client import Dispatch + desktop = winshell.desktop() + path = os.path.join(desktop, "LHv2-ON.lnk") + shell = Dispatch('WScript.Shell') + shortcut = shell.CreateShortCut(path) + if cmdStr.find(".py")>0: + shortcut.Targetpath = sys.executable + shortcut.Arguments = '"' + cmdPath + '" on '+ " ".join(lh_macs) + else: + shortcut.Targetpath = cmdPath + shortcut.Arguments = "on "+ " ".join(lh_macs) + shortcut.WorkingDirectory = os.path.dirname(cmdPath) + shortcut.IconLocation = os.path.dirname(cmdPath) + "\\lhv2_on.ico" + shortcut.save() + print(" * OK: LHv2-ON.lnk was created successfully.") + path = os.path.join(desktop, "LHv2-OFF.lnk") + shell = Dispatch('WScript.Shell') + shortcut = shell.CreateShortCut(path) + if cmdStr.find(".py")>0: + shortcut.Targetpath = sys.executable + shortcut.Arguments = '"' + cmdPath + '" off '+ " ".join(lh_macs) + else: + shortcut.Targetpath = cmdPath + shortcut.Arguments = "off "+ " ".join(lh_macs) + shortcut.WorkingDirectory = os.path.dirname(cmdPath) + shortcut.IconLocation = os.path.dirname(cmdPath) + "\\lhv2_off.ico" + shortcut.save() + print(" * OK: LHv2-OFF.lnk was created successfully.") + else: + print(">> OK, you need to manually create two links, for example on your desktop:") + print(" ") + print(" To turn your lighthouses ON:") + print(" * Link Target: "+ cmdStr +" on "+ " ".join(lh_macs)) + print(" ") + print(" To turn your lighthouses OFF:") + print(" * Link Target: "+ cmdStr +" off "+ " ".join(lh_macs)) + else: + print(">> Sorry, not suitable V2 Lighthouses found.") else: for mac in lh_macs: print(">> Trying to connect to BLE MAC '"+ mac +"'...") @@ -85,4 +146,4 @@ async def run(loop): print(">> ERROR: "+ str(e)) print(" ") loop = asyncio.get_event_loop() -loop.run_until_complete(run(loop)) +loop.run_until_complete(run(loop, lh_macs))