X-Git-Url: https://git.slaskete.net/einar-bin/blobdiff_plain/d903416fd54b2472e5cf1df37ac85f377beec9cb..354034f16f52d2a224ab932a2ac6bca89a327315:/autolock.py diff --git a/autolock.py b/autolock.py index 0fddaad..b02642b 100644 --- a/autolock.py +++ b/autolock.py @@ -1,12 +1,14 @@ +#!/usr/bin/env python # Automatically lock the screen when bluetooth beacon moves out of range -# Requires GNOME Shell -from beacontools import BeaconScanner, IBeaconFilter, IBeaconAdvertisement import time +from beacontools import BeaconScanner, IBeaconFilter, IBeaconAdvertisement import signal import dbus +from gi.repository import Gio -global isHere, screenSaverSetActive, screenSaverGetActive +global isHere, beaconTimeout +global screenSaverSetActive, screenSaverGetActive, sessionIdleTime def handler(signum, frame): scanner.stop() @@ -17,12 +19,16 @@ def callback(bt_addr, rssi, packet, additional_info): 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: +mutter = sessionBus.get_object('org.gnome.Mutter.IdleMonitor', '/org/gnome/Mutter/IdleMonitor/Core') +mutterIface = dbus.Interface(mutter, 'org.gnome.Mutter.IdleMonitor') +sessionIdleTime = mutterIface.get_dbus_method("GetIdletime") + # scan for all iBeacon advertisements regardless from which beacon #scanner = BeaconScanner(callback, # packet_filter=IBeaconAdvertisement @@ -30,19 +36,25 @@ screenSaverGetActive = screenSaverIface.get_dbus_method("GetActive") signal.signal(signal.SIGINT, handler) +beaconTimeout=0 + while True: isHere = False scanner = BeaconScanner(callback, - device_filter=IBeaconFilter(uuid="INSERT YOUR UUID HERE") + device_filter=IBeaconFilter(uuid="INSERT BEACON UUID HERE") ) scanner.start() - time.sleep(45) + time.sleep(10) scanner.stop() + idleTime = sessionIdleTime() / 1000 if isHere: - print("Beacon is here") + #print("Beacon observed") + beaconTimeout = 0 else: - print("Beacon is gone") - if not screenSaverGetActive(): - print("Locking screen") - screenSaverSetActive(True) + beaconTimeout += 10 + #print("Beacon not observed,", idleTime, "seconds since last activity,", beaconTimeout, "seconds since beacon seen") + if idleTime >= 60 and beaconTimeout >= 60: + if not screenSaverGetActive(): + print("No activity and not seen for at least 60 seconds, locking screen") + screenSaverSetActive(True)