-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdetails.py
69 lines (61 loc) · 1.94 KB
/
details.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
import os
from typing import Union
from pathlib import Path
from collections import deque
from subprocess import run, Popen, STDOUT, PIPE
UNLOCK_STATE = 'UNLOCK'
LOCK_STATE = 'LOCK'
SENSOR_SCRIPT_PATH = Path('bluetooth_distance_sensor.sh')
DO_NOT_CARE_SENSOR_MESSAGES = [
'Read RSSI failed: Input/output error',
'Attempting connection...',
'GONE!',
'HERE!',
]
LOCKER_LOCK_COMMAND = os.getenv(
'LOCKER_LOCK_COMMAND',
'gnome-screensaver-command --lock')
LOCKER_UNLOCK_COMMAND = os.getenv(
'LOCKER_UNLOCK_COMMAND',
'gnome-screensaver-command --deactivate')
LOCKER_STATUS_COMMAND = os.getenv(
'LOCKER_STATUS_COMMAND',
'gnome-screensaver-command --query')
LOCKER_UNLOCK_SCREEN_SAVE_PREFIX = os.getenv(
'LOCKER_UNLOCK_SCREEN_SAVE_PREFIX',
'The screensaver is inactive')
def bluetooth_distance_locker(device_mac_address):
commend = [str(SENSOR_SCRIPT_PATH), device_mac_address]
queue = deque(maxlen=5)
state = UNLOCK_STATE
bluetooth_active = False
def parse_line():
"""
Parse the child process line
* check if relevant
* modify the queue
"""
...
def print_state():
"""
Print nice status to stdout
"""
...
def check_screen_save_status() -> Union[UNLOCK_STATE, LOCK_STATE]:
"""
Check status of the screen save
"""
...
with Popen(commend,
stderr=STDOUT, stdout=PIPE,
bufsize=1, universal_newlines=True) as process:
for line in iter(process.stdout.readline, ''):
parse_line()
state = check_screen_save_status()
if state == UNLOCK_STATE and len(queue) == queue.maxlen:
state = LOCK_STATE
run(LOCKER_LOCK_COMMAND.split())
elif state == LOCK_STATE and len(queue) < 4:
state = UNLOCK_STATE
run(LOCKER_UNLOCK_COMMAND.split())
print_state()