Last active
March 27, 2019 15:40
-
-
Save vicenterocha/0344b7091936294715906780f56ee99b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import bluetooth | |
import sys | |
import os | |
import time | |
import subprocess | |
import re | |
# s10 bluetooth address | |
addr='CC:21:19:CF:F9:F2' | |
# bluetooth port | |
port=3 | |
def send_message(message): | |
""" | |
sends system notification | |
""" | |
subprocess.Popen(['notify-send', message]) | |
return | |
def connection_check(): | |
""" | |
checks if s10 is connected. Returns True if connected, else False | |
""" | |
ps = subprocess.Popen(['hcitool', 'con'], stdout=subprocess.PIPE) | |
s10 = ps.communicate()[0].decode('utf-8') | |
if "CC:21:19:CF:F9:F2" in s10: | |
return True | |
else: | |
return False | |
def proximity_check(): | |
""" | |
checks if s10 is nearby, returns rssi value | |
""" | |
ps = subprocess.Popen(['hcitool', 'rssi', addr], stdout=subprocess.PIPE) | |
proximity = ps.communicate()[0].decode('utf-8') | |
proximity = [int(d) for d in re.findall(r'-?\d+', proximity)] | |
return proximity[0] | |
#while True: | |
# blindly tries to connect everytime (TODO smooth) | |
try: | |
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM) | |
s.connect((addr,port)) | |
except bluetooth.btcommon.BluetoothError as err: | |
pass | |
if connection_check(): | |
print(' ') | |
if proximity_check() < 0: | |
print(' ') | |
send_message('Too far away. Locking..') | |
time.sleep(5) | |
ps = subprocess.Popen(['pgrep slock'], stdout=subprocess.PIPE) | |
if ps is None: | |
ps = subprocess.Popen(['slock'], stdout=subprocess.PIPE) | |
else: | |
send_message('Not connected.. Locking device..') | |
print(' ') | |
time.sleep(5) | |
ps = subprocess.Popen(['pgrep slock'], stdout=subprocess.PIPE) | |
if ps is None: | |
ps = subprocess.Popen(['slock'], stdout=subprocess.PIPE) | |
#time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment