Automatisk låsing av skjerm
authorEinar Jørgen Haraldseid <einar.haraldseid@ntnu.no>
Wed, 8 Feb 2023 08:53:56 +0000 (09:53 +0100)
committerEinar Jørgen Haraldseid <einar.haraldseid@ntnu.no>
Wed, 8 Feb 2023 08:53:56 +0000 (09:53 +0100)
autolock.py [new file with mode: 0644]

diff --git a/autolock.py b/autolock.py
new file mode 100644 (file)
index 0000000..0fddaad
--- /dev/null
@@ -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)