/
autolock.py
144 lines (112 loc) · 4.42 KB
/
autolock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# -*- coding: utf-8 -*-
"""
Event server simple client
Waits for I-Beacons and sends messages
Temporary stores messages in file
MIT License
Copyright (c) 2017 Roman Mindlin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import blescan
import sys
import math
import const
import bluetooth._bluetooth as bluez
from subprocess import call
import processors
import logger
from time import sleep
import gobject
import dbus
from dbus.mainloop.glib import DBusGMainLoop
import threading
logger = logger.get_logger(__name__)
def getrange(txPower, rssi):
"""https://stackoverflow.com/questions/20416218/understanding-ibeacon-distancing"""
if txPower == 0:
txPower = 1
ratio = float(rssi) / txPower
if (ratio < 1.0):
return round(math.pow(ratio, 10))
else:
distance = (0.89976) * math.pow(ratio, 7.7095) + 0.111
return round(distance)
def filter_cb(bus, message):
"""https://unix.stackexchange.com/questions/212347/how-to-monitor-the-screen-lock-unlock-in-the-ubuntu-14-04"""
global locked
if message.get_member() != "EventEmitted":
return
args = message.get_args_list()
if args[0] == "desktop-lock":
locked = True
elif args[0] == "desktop-unlock":
locked = False
locked = False
DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
bus.add_match_string("type='signal',interface='com.ubuntu.Upstart0_6'")
bus.add_message_filter(filter_cb)
mainloop = gobject.MainLoop()
def start(*args, **kwargs):
"""Main loop"""
processor = processors.Kalman()
gobject_thread = threading.Thread(target=mainloop.run)
gobject_thread.daemon = True
gobject_thread.start()
dev_id = 0
try:
sock = bluez.hci_open_dev(dev_id)
blescan.hci_le_set_scan_parameters(sock)
blescan.hci_enable_le_scan(sock)
logger.info("ble thread started")
except Exception as e:
logger.error('Error accessing bluetooth device', exc_info=True)
sys.exit(1)
try:
global locked
counter = 0
while True:
returnedList = blescan.parse_events(sock, 10)
for beacon in returnedList:
uuid, major, minor = beacon.split(',')[1:4]
if major in const.ALLOWED_MAJOR and minor in const.ALLOWED_MINOR:
beacon_id = beacon[:-8]
if beacon_id[-2:] == ',,': # double comma at end of string
beacon_id = beacon_id[:-1]
txpower = int(beacon.split(',')[4])
rssi = int(beacon.split(',')[5])
rssi = -99 if rssi < -99 else rssi # rssi peak fix
rssi_filtered = processor.filter(beacon_id, rssi)
if rssi_filtered is None:
continue
beacon_dist = getrange(txpower, rssi_filtered)
if not locked:
if beacon_dist >= const.MAX_RANGE:
counter += 1
else:
counter = counter-1 if counter > 1 else 0
if counter == 4:
logger.info('screen is locked')
call(const.LOCK_SCREEN, shell=True)
locked = True
counter = 0
sleep(.1)
except KeyboardInterrupt:
logger.warning("Ctrl-C pressed")
sys.exit()
if __name__ == "__main__":
sys.exit(start(sys.argv))