|
| 1 | +""" |
| 2 | +This is from an interesting blog: |
| 3 | +https://www.raspberrypi.org/forums/viewtopic.php?t=47466 |
| 4 | +""" |
| 5 | +import os |
| 6 | +import sys |
| 7 | +import argparse |
| 8 | +from pathlib import Path |
| 9 | +from itertools import cycle |
| 10 | +from collections import deque |
| 11 | +from subprocess import Popen, run, PIPE, STDOUT |
| 12 | + |
| 13 | +PROGRESS = cycle(['-', '\\', '|', '/']) |
| 14 | +UNLOCK_STATE = 'UNLOCK' |
| 15 | +LOCK_STATE = 'LOCK' |
| 16 | +SENSOR_SCRIPT_PATH = Path(__file__).parent.joinpath('bluetooth_distance_sensor.sh') |
| 17 | +DO_NOT_CARE_SENSOR_MESSAGES = [ |
| 18 | + 'Read RSSI failed: Input/output error', |
| 19 | + 'Attempting connection...', |
| 20 | + 'GONE!', |
| 21 | + 'HERE!', |
| 22 | +] |
| 23 | +LOCKER_LOCK_COMMAND = os.getenv( |
| 24 | + 'LOCKER_LOCK_COMMAND', |
| 25 | + 'gnome-screensaver-command --lock') |
| 26 | +LOCKER_UNLOCK_COMMAND = os.getenv( |
| 27 | + 'LOCKER_UNLOCK_COMMAND', |
| 28 | + 'gnome-screensaver-command --deactivate') |
| 29 | +LOCKER_STATUS_COMMAND = os.getenv( |
| 30 | + 'LOCKER_STATUS_COMMAND', |
| 31 | + 'gnome-screensaver-command --query') |
| 32 | +LOCKER_UNLOCK_SCREEN_SAVE_PREFIX = os.getenv( |
| 33 | + 'LOCKER_UNLOCK_SCREEN_SAVE_PREFIX', |
| 34 | + 'The screensaver is inactive') |
| 35 | + |
| 36 | + |
| 37 | +def main(): |
| 38 | + parser = argparse.ArgumentParser() |
| 39 | + parser.add_argument( |
| 40 | + '-a', '--address', |
| 41 | + required=True, |
| 42 | + help='Your phone or another bluetooth device MAC Address') |
| 43 | + options = parser.parse_args() |
| 44 | + return bluetooth_distance_locker(options.address) |
| 45 | + |
| 46 | + |
| 47 | +def bluetooth_distance_locker(device_mac_address): |
| 48 | + commend = [str(SENSOR_SCRIPT_PATH), device_mac_address] |
| 49 | + queue = deque(maxlen=5) |
| 50 | + state = UNLOCK_STATE |
| 51 | + bluetooth_active = False |
| 52 | + |
| 53 | + def parse_line(): |
| 54 | + nonlocal line, bluetooth_active |
| 55 | + line = line.strip() |
| 56 | + if line in DO_NOT_CARE_SENSOR_MESSAGES: |
| 57 | + return |
| 58 | + if line == 'Not connected.': |
| 59 | + if bluetooth_active: |
| 60 | + queue.appendleft(-255) |
| 61 | + return |
| 62 | + bluetooth_active = True |
| 63 | + value = int(line.replace('Device connected. RSSI: ', '')) |
| 64 | + if value < -1: |
| 65 | + queue.appendleft(value) |
| 66 | + return |
| 67 | + if queue: |
| 68 | + queue.pop() |
| 69 | + return |
| 70 | + |
| 71 | + def print_state(): |
| 72 | + if index: |
| 73 | + # move cursor one line up, delete till end of line |
| 74 | + sys.stdout.write('\033[1A\033[K' * 4) |
| 75 | + sys.stdout.write( |
| 76 | + f'{next(PROGRESS)}\n' |
| 77 | + f'Bluetooth Distance Sensor output:>{line}\n' |
| 78 | + f'Current Queue size:>{queue_size}\n' |
| 79 | + f'Status:>{state}\n' |
| 80 | + ) |
| 81 | + |
| 82 | + def check_screen_save_status(): |
| 83 | + screen_saver_status_output = run( |
| 84 | + LOCKER_STATUS_COMMAND.split(), |
| 85 | + universal_newlines=True, |
| 86 | + stdout=PIPE).stdout |
| 87 | + if screen_saver_status_output.startswith(LOCKER_UNLOCK_SCREEN_SAVE_PREFIX): |
| 88 | + return UNLOCK_STATE |
| 89 | + return LOCK_STATE |
| 90 | + |
| 91 | + with Popen(commend, stderr=STDOUT, stdout=PIPE, universal_newlines=True) as process: |
| 92 | + for index, line in enumerate(iter(process.stdout.readline, '')): |
| 93 | + parse_line() |
| 94 | + queue_size = len(queue) |
| 95 | + state = check_screen_save_status() |
| 96 | + if state == UNLOCK_STATE and queue_size == queue.maxlen: |
| 97 | + state = LOCK_STATE |
| 98 | + run(LOCKER_LOCK_COMMAND.split()) |
| 99 | + elif state == LOCK_STATE and queue_size < 4: |
| 100 | + state = UNLOCK_STATE |
| 101 | + run(LOCKER_UNLOCK_COMMAND.split()) |
| 102 | + print_state() |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == '__main__': |
| 106 | + sys.exit(main()) |
0 commit comments