From: Einar Jørgen Haraldseid Date: Wed, 8 Feb 2023 08:53:56 +0000 (+0100) Subject: Automatisk låsing av skjerm X-Git-Url: https://git.slaskete.net/einar-bin/commitdiff_plain/d903416fd54b2472e5cf1df37ac85f377beec9cb?ds=sidebyside;hp=64f4eb1eaa044c3a67dd151114f0f2ded460f0af Automatisk låsing av skjerm --- diff --git a/autolock.py b/autolock.py new file mode 100644 index 0000000..0fddaad --- /dev/null +++ b/autolock.py @@ -0,0 +1,48 @@ +# Automatically lock the screen when bluetooth beacon moves out of range +# Requires GNOME Shell + +from beacontools import BeaconScanner, IBeaconFilter, IBeaconAdvertisement +import time +import signal +import dbus + +global isHere, screenSaverSetActive, screenSaverGetActive + +def handler(signum, frame): + scanner.stop() + exit(0) + +def callback(bt_addr, rssi, packet, additional_info): + global isHere + isHere = True + +sessionBus = dbus.SessionBus() +screenSaver = sessionBus.get_object("org.gnome.ScreenSaver", "/org/gnome/ScreenSaver") +screenSaverIface = dbus.Interface(screenSaver, 'org.gnome.ScreenSaver') +screenSaverSetActive = screenSaverIface.get_dbus_method("SetActive") +screenSaverGetActive = screenSaverIface.get_dbus_method("GetActive") + +# To find beacon: +# scan for all iBeacon advertisements regardless from which beacon +#scanner = BeaconScanner(callback, +# packet_filter=IBeaconAdvertisement +#) + +signal.signal(signal.SIGINT, handler) + +while True: + isHere = False + scanner = BeaconScanner(callback, + device_filter=IBeaconFilter(uuid="INSERT YOUR UUID HERE") + ) + scanner.start() + time.sleep(45) + scanner.stop() + + if isHere: + print("Beacon is here") + else: + print("Beacon is gone") + if not screenSaverGetActive(): + print("Locking screen") + screenSaverSetActive(True)