cleanups, working winrt beacon

This commit is contained in:
F. Duncanh
2025-11-02 01:07:28 -04:00
parent 671fd9a715
commit d92836e387

View File

@@ -4,14 +4,12 @@
# a standalone python-3.6 or later winrt-based AirPlay Service-Discovery Bluetooth LE beacon for UxPlay
# (c) F. Duncanh, October 2025
import gi
try:
from gi.repository import GLib
except ImportError:
print(f"ImportError: failed to import GLib")
# Import WinRT APIs
try:
@@ -40,7 +38,7 @@ import asyncio
publisher = None
def on_status_changed(sender, args):
print(f"Publisher status: {args.status}")
print(f"Publisher status change to: {args.status.name}")
def create_airplay_service_discovery_advertisement_publisher(ipv4_str, port):
@@ -69,17 +67,16 @@ async def publish_advertisement():
print(f"Publisher started successfully")
except Exception as e:
print(f"Failed to start Publihser: {e}")
ptint(f"Publisher Status: {publisher.status}")
print(f"Failed to start Publisher: {e}")
print(f"Publisher Status: {publisher.status.name}")
def setup_beacon(ipv4_str, port, advmin, advmax, index):
def setup_beacon(ipv4_str, port):
#index will be ignored
print(f"setup_beacon port {ipv4_str}:{port} Note: min, max advertising interval and index are not used)")
print(f"setup_beacon port {ipv4_str}:{port}")
create_airplay_service_discovery_advertisement_publisher(ipv4_str, port)
def beacon_on():
print(f"beacon_on")
global publisher
try:
asyncio.run( publish_advertisement())
@@ -90,9 +87,9 @@ def beacon_on():
def beacon_off():
print(f"beacon off")
global publisher
publisher.stop()
print(f"Current Publisher Status: {publisher.status.name}")
publisher = None
#==generic code (non-winrt) below here =============
@@ -110,19 +107,13 @@ beacon_is_pending_on = False
beacon_is_pending_off = False
port = int(0)
advmin = int(100)
advmax = int(100)
ipv4_str = "ipv4_address"
index = int(0)
def start_beacon():
global beacon_is_running
global port
global ipv4_str
global advmin
global advmax
global index
setup_beacon(ipv4_str, port, advmin, advmax, index)
setup_beacon(ipv4_str, port)
beacon_is_running = beacon_on()
def stop_beacon():
@@ -188,45 +179,13 @@ def on_timeout(file_path):
return True
def process_input(value):
try:
my_integer = int(value)
return my_integer
except ValueError:
print(f'Error: could not convert "{value}" to integer: {my_integer}')
return None
#check AdvInterval
def check_adv_intrvl(min, max):
if not (100 <= min):
raise ValueError('AdvMin was smaller than 100 msecs')
if not (max >= min):
raise ValueError('AdvMax was smaller than AdvMin')
if not (max <= 10240):
raise ValueError('AdvMax was larger than 10240 msecs')
def main(file_path, ipv4_str_in, advmin_in, advmax_in, index_in):
def main(file_path, ipv4_str_in):
global ipv4_str
global advmin
global advmax
global index
ipv4_str = ipv4_str_in
advmin = advmin_in
advmax = advmax_in
index = index_in
try:
while True:
try:
check_adv_intrvl(advmin, advmax)
except ValueError as e:
print(f'Error: {e}')
raise SystemExit(1)
GLib.timeout_add_seconds(5, on_timeout, file_path)
GLib.timeout_add_seconds(2, on_timeout, file_path)
GLib.timeout_add_seconds(1, check_pending)
mainloop = GLib.MainLoop()
mainloop.run()
@@ -237,15 +196,13 @@ def main(file_path, ipv4_str_in, advmin_in, advmax_in, index_in):
if __name__ == '__main__':
if not sys.version_info >= (3,6):
print("uxplay-beacon.py requires Python 3.6 or higher")
# Create an ArgumentParser object
parser = argparse.ArgumentParser(
description='A program that runs an AirPlay service discovery BLE beacon.',
epilog='Example: python beacon.py --ipv4 "192.168.1.100" --path "/home/user/ble" --AdvMin 100 --AdvMax 100"'
description='A program (for MS Windows systems only) that runs an AirPlay service discovery BLE beacon.',
epilog='Example: python beacon.py --ipv4 "192.168.1.100" --path "/home/user/ble"'
)
home_dir = os.environ.get("HOME")
@@ -271,33 +228,10 @@ if __name__ == '__main__':
help='ipv4 address of AirPlay server (default: use gethostbyname).'
)
parser.add_argument(
'--AdvMin',
type=str,
default="0",
help='The minimum Advertising Interval (>= 100) units=msec, default 100)'
)
parser.add_argument(
'--AdvMax',
type=str,
default="0",
help='The maximum Advertising Interval (>= AdvMin, <= 10240) units=msec, default 100)'
)
parser.add_argument(
'--index',
type=str,
default="0",
help='use index >= 0 to distinguish multiple AirPlay Service Discovery beacons, default 0)'
)
# Parse the command-line argunts
args = parser.parse_args()
ipv4_str = None
path = None
advmin = int(100)
advmax = int(100)
index = int(0)
if args.file:
print(f'Using config file: {args.file}')
@@ -316,24 +250,6 @@ if __name__ == '__main__':
path = value
elif key == "--ipv4":
ipv4_str = value
elif key == "--AdvMin":
if value.isdigit():
advmin = int(value)
else:
print(f'Invalid config file input (--AdvMin) {value} in {args.file}')
raise SystemExit(1)
elif key == "--AdvMax":
if value.isdigit():
advmax = int(value)
else:
print(f'Invalid config file input (--AdvMax) {value} in {args.file}')
raise SystemExit(1)
elif key == "--index":
if value.isdigit():
index = int(value)
else:
print(f'Invalid config file input (--index) {value} in {args.file}')
raise SystemExit(1)
else:
print(f'Unknown key "{key}" in config file {args.file}')
raise SystemExit(1)
@@ -344,30 +260,7 @@ if __name__ == '__main__':
else:
ipv4_str = args.ipv4
if args.AdvMin != "0":
if args.AdvMin.isdigit():
advmin = int(args.AdvMin)
else:
print(f'Invalid input (AdvMin) {args.AdvMin}')
raise SystemExit(1)
if args.AdvMax != "0":
if args.AdvMax.isdigit():
advmax = int(args.AdvMax)
else:
print(f'Invalid input (AdvMin) {args.AdvMin}')
raise SystemExit(1)
if args.index != "0":
if args.index.isdigit():
index = int(args.index)
else:
print(f'Invalid input (AdvMin) {args.AdvMin}')
raise SystemExit(1)
if index < 0:
raise ValueError('index was negative (forbidden)')
print(f'AirPlay Service-Discovery Bluetooth LE beacon: using BLE file {args.path}, advmin:advmax {advmin}:{advmax} index:{index}')
print(f'AirPlay Service-Discovery Bluetooth LE beacon: using BLE file {args.path}')
print(f'(Press Ctrl+C to exit)')
main(args.path, ipv4_str, advmin, advmax, index)
main(args.path, ipv4_str)