Automatisk lÄsing av skjerm
[einar-bin] / autolock.py
1 # Automatically lock the screen when bluetooth beacon moves out of range
2 # Requires GNOME Shell
3
4 from beacontools import BeaconScanner, IBeaconFilter, IBeaconAdvertisement
5 import time
6 import signal
7 import dbus
8
9 global isHere, screenSaverSetActive, screenSaverGetActive
10
11 def handler(signum, frame):
12 scanner.stop()
13 exit(0)
14
15 def callback(bt_addr, rssi, packet, additional_info):
16 global isHere
17 isHere = True
18
19 sessionBus = dbus.SessionBus()
20 screenSaver = sessionBus.get_object("org.gnome.ScreenSaver", "/org/gnome/ScreenSaver")
21 screenSaverIface = dbus.Interface(screenSaver, 'org.gnome.ScreenSaver')
22 screenSaverSetActive = screenSaverIface.get_dbus_method("SetActive")
23 screenSaverGetActive = screenSaverIface.get_dbus_method("GetActive")
24
25 # To find beacon:
26 # scan for all iBeacon advertisements regardless from which beacon
27 #scanner = BeaconScanner(callback,
28 # packet_filter=IBeaconAdvertisement
29 #)
30
31 signal.signal(signal.SIGINT, handler)
32
33 while True:
34 isHere = False
35 scanner = BeaconScanner(callback,
36 device_filter=IBeaconFilter(uuid="INSERT YOUR UUID HERE")
37 )
38 scanner.start()
39 time.sleep(45)
40 scanner.stop()
41
42 if isHere:
43 print("Beacon is here")
44 else:
45 print("Beacon is gone")
46 if not screenSaverGetActive():
47 print("Locking screen")
48 screenSaverSetActive(True)