From 8d5474c0b77579ff454d28529cfc2dad5ace615e Mon Sep 17 00:00:00 2001 From: Matthew Grove Date: Thu, 4 Apr 2019 14:36:20 +0100 Subject: [PATCH] Add configuration files for easy setup --- btpl.py | 44 ++++++++++++++++---------------- config | 73 +++++++++++++++++++++++++++++++++++++++++++++++++---- reconfigure | 55 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 145 insertions(+), 27 deletions(-) mode change 100644 => 100755 config create mode 100755 reconfigure diff --git a/btpl.py b/btpl.py index ba26b31..ad672aa 100644 --- a/btpl.py +++ b/btpl.py @@ -6,6 +6,25 @@ import time import sys import os +# time between searches for device, in seconds +scanPeriod = 2.5 +# command to run when the device leaves desired range +leftRange = "gnome-screensaver-command -l" +# command to run when the device enters desired range +enteredRange = "gnome-screensaver-command -d" +# maximum number of times the device can be out of range before the PC is locked +maxMissed = 3 +# range at which PC is locked (0 - rangeLimit = RSSI) +rangeLimit = 7 +# get Bluetooth address of target device +BTAddress = sys.argv[1] +# initiate variables for use when processing RSSIs +status = "gone" +awayCounter = maxMissed +screenLocked = False +BTInRange = True +firstRun = True + # function for retrieving the RSSI of the desired bluetooth device def getRSSI(): # get output from terminal command that retrieves device name @@ -27,31 +46,12 @@ if len(sys.argv) < 2: print("Usage: btpl.py ") sys.exit(1) -# time between searches for device, in seconds -scanPeriod = 2.5 -# command to run when the device leaves desired range -leftRange = "gnome-screensaver-command -l" -# command to run when the device enters desired range -enteredRange = "gnome-screensaver-command -d" -# maximum number of times the device can be out of range before the PC is locked -maxMissed = 3 -# range at which PC is locked (0 - rangeLimit = RSSI) -rangeLimit = 7 -# get Bluetooth address of target device -BTAddress = sys.argv[1] -# initiate variables for use when processing RSSIs -status = "gone" -awayCounter = maxMissed -screenLocked = False -BTInRange = True -firstRun = True - # tell user the program is running print("Identifying device...") try: # display device name, with the first part bold & green - print("\033[1;32m[OK]\033[0;37m Device found:", bluetooth.lookup_name(BTAddress,timeout=5)) + print("\033[1;32m[OK]\033[0m Device found:", bluetooth.lookup_name(BTAddress,timeout=5)) # run forever while True: @@ -91,7 +91,7 @@ try: os.system(leftRange) # print current Bluetooth data, with the first part bold & green - print("\033[1;32m[OK]\033[0;37m RSSI:", rssi, "|", "Status:", status, "|", "Away counter:", awayCounter, "|", "Device in range:", BTInRange, "|", "Screen locked:", screenLocked, "|", time.strftime('%H:%M:%S')) + print("\033[1;32m[OK]\033[0m RSSI:", rssi, "|", "Status:", status, "|", "Away counter:", awayCounter, "|", "Device in range:", BTInRange, "|", "Screen locked:", screenLocked, "|", time.strftime('%H:%M:%S')) # wait for specified time before checking device status again time.sleep(scanPeriod) @@ -99,4 +99,4 @@ try: # usually happens when bluetooth is disabled except: # display error message, with the first part bold & red - print("\033[1;31m[ERROR]\033[0;37m Bluetooth on PC is not active") + print("\033[1;31m[ERROR]\033[0m Bluetooth on PC is not active") diff --git a/config b/config old mode 100644 new mode 100755 index ba13116..02c341a --- a/config +++ b/config @@ -1,11 +1,74 @@ #!/bin/bash -mkdir ~/.bluetooth-proximity-locking > /dev/null -chmod +x btpl.sh -mkdir bin -mv btpl.sh bin/btpl +# exit if not enough arguments provided +if [ $# \< 6 ] +then + echo -e "\033[1;31m[ERROR]\033[0m Not enough arguments" + exit 1 +fi +# exit if invalid Bluetooth address provided +if ! [ `echo "$1" | egrep "^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"` ] +then + echo -e "\033[1;31m[ERROR]\033[0m Invalid Bluetooth address" + exit 1 +fi + +# set user-specific config options, if arguments aren't "x" +sed -i "s/\[bluetooth address\]/$1/g" btpl.sh +echo -e "\033[1;32m[OK]\033[0m Bluetooth address set" +if [ "$2" != "x" ] +then + sed -i "s/scanPeriod = 2.5/scanPeriod = $2/g" btpl.py + echo -e "\033[1;32m[OK]\033[0m Scan period set" +else + echo -e "\033[1;32m[OK]\033[0m Scan period left as default" +fi +if [ "$3" != "x" ] +then + sed -i "s/leftRange = \"gnome-screensaver-command -l\"/leftRange = $3/g" btpl.py + echo -e "\033[1;32m[OK]\033[0m Left range command set" +else + echo -e "\033[1;32m[OK]\033[0m Left range command left as default" +fi +if [ "$4" != "x" ] +then + sed -i "s/enteredRange = \"gnome-screensaver-command -d\"/enteredRange = $4/g" btpl.py + echo -e "\033[1;32m[OK]\033[0m Entered range command set" +else + echo -e "\033[1;32m[OK]\033[0m Entered range command left as default" +fi +if [ "$5" != "x" ] +then + sed -i "s/maxMissed = 3/maxMissed = $5/g" btpl.py + echo -e "\033[1;32m[OK]\033[0m Maximum missed set" +else + echo -e "\033[1;32m[OK]\033[0m Maximum missed left as default" +fi +if [ "$6" != "x" ] +then + sed -i "s/rangeLimit = 7/rangeLimit = $6/g" btpl.py + echo -e "\033[1;32m[OK]\033[0m Range limit set" +else + echo -e "\033[1;32m[OK]\033[0m Range limit left as default" +fi + +# set permissions for files that need to be executable +chmod +x btpl.sh reconfigure +# reorganise files +mkdir ~/.bluetooth-proximity-locking > /dev/null 2>&1 +mkdir bin > /dev/null 2>&1 +mv btpl.sh bin/btpl > /dev/null 2>&1 +# add command 'btpl' to PATH if ! grep -Fq 'export PATH=$PATH":$HOME/.bluetooth-proximity-locking/bin"' ~/.profile then echo 'export PATH=$PATH":$HOME/.bluetooth-proximity-locking/bin"' >> ~/.profile + echo -e "\033[1;32m[OK]\033[0m Command 'btpl' added to PATH successfully" fi -mv * ~/.bluetooth-proximity-locking/ > /dev/null +# move files to a hidden folder in the user's home folder +mv * ~/.bluetooth-proximity-locking/ > /dev/null 2>&1 +echo -e "\033[1;32m[OK]\033[0m Files reorganised successfully" +# refresh PATH source ~/.profile +echo -e "\033[1;32m[OK]\033[0m PATH refreshed successfully" + +# display final success message +echo -e "\033[1;32m[OK]\033[0m Bluetooth Proximity Locking configured successfully" diff --git a/reconfigure b/reconfigure new file mode 100755 index 0000000..52fd238 --- /dev/null +++ b/reconfigure @@ -0,0 +1,55 @@ +#!/bin/bash +# exit if not enough arguments provided +if [ $# \< 6 ] +then + echo -e "\033[1;31m[ERROR]\033[0m Not enough arguments" + exit 1 +fi +# exit if invalid Bluetooth address provided +if ! [ `echo "$1" | egrep "^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"` ] +then + echo -e "\033[1;31m[ERROR]\033[0m Invalid Bluetooth address" + exit 1 +fi + +# set user-specific config options, if arguments aren't "x" +sed -i "s/\[bluetooth address\]/$1/g" ~/.bluetooth-proximity-locking/btpl.sh +echo -e "\033[1;32m[OK]\033[0m Bluetooth address set" +if [ "$2" != "x" ] +then + sed -i "s/scanPeriod = 2.5/scanPeriod = $2/g" ~/.bluetooth-proximity-locking/btpl.py + echo -e "\033[1;32m[OK]\033[0m Scan period set" +else + echo -e "\033[1;32m[OK]\033[0m Scan period left as default" +fi +if [ "$3" != "x" ] +then + sed -i "s/leftRange = \"gnome-screensaver-command -l\"/leftRange = $3/g" ~/.bluetooth-proximity-locking/btpl.py + echo -e "\033[1;32m[OK]\033[0m Left range command set" +else + echo -e "\033[1;32m[OK]\033[0m Left range command left as default" +fi +if [ "$4" != "x" ] +then + sed -i "s/enteredRange = \"gnome-screensaver-command -d\"/enteredRange = $4/g" ~/.bluetooth-proximity-locking/btpl.py + echo -e "\033[1;32m[OK]\033[0m Entered range command set" +else + echo -e "\033[1;32m[OK]\033[0m Entered range command left as default" +fi +if [ "$5" != "x" ] +then + sed -i "s/maxMissed = 3/maxMissed = $5/g" ~/.bluetooth-proximity-locking/btpl.py + echo -e "\033[1;32m[OK]\033[0m Maximum missed set" +else + echo -e "\033[1;32m[OK]\033[0m Maximum missed left as default" +fi +if [ "$6" != "x" ] +then + sed -i "s/rangeLimit = 7/rangeLimit = $6/g" ~/.bluetooth-proximity-locking/btpl.py + echo -e "\033[1;32m[OK]\033[0m Range limit set" +else + echo -e "\033[1;32m[OK]\033[0m Range limit left as default" +fi + +# display final success message +echo -e "\033[1;32m[OK]\033[0m Bluetooth Proximity Locking configured successfully"