-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
71 lines (62 loc) · 1.95 KB
/
utils.py
File metadata and controls
71 lines (62 loc) · 1.95 KB
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
import random
import subprocess
from time import sleep
import sys
import os
import logging
import psutil
def add_submodule_to_path():
d = os.path.dirname(os.path.abspath(__file__))
d = os.path.join(d, "PythonSake")
if not os.path.isdir(d):
raise FileNotFoundError(f"you are missing the submodule checkout! please fix")
sys.path.append(d)
logging.debug(f"{d} was added to path")
return
def gen_mobile_name():
while True:
num = random.randint(100000, 999999)
if num % 2 == 1:
return f"Mobile {num}"
def exec(cmd:str) -> None:
logging.debug(f"executing: {cmd}")
subprocess.run(cmd, shell=True)
return
def forget_pump_devices() -> None:
"""
Forget all paired Bluetooth devices whose name starts with "Pump".
Uses bluetoothctl CLI.
"""
input("We will forget all paired pumps! Do you want to continue? Press enter...")
try:
# Get list of paired devices
result = subprocess.run(
['bluetoothctl', 'devices', 'Paired'],
capture_output=True,
text=True,
check=True
)
lines = result.stdout.splitlines()
for line in lines:
# Format: Device XX:XX:XX:XX:XX:XX DeviceName
parts = line.split(maxsplit=2)
if len(parts) < 3:
continue
mac, name = parts[1], parts[2]
if name.startswith("Pump"):
logging.debug(f"Removing pairing for {name} ({mac})")
subprocess.run(['bluetoothctl', 'remove', mac], check=False)
except subprocess.CalledProcessError as e:
logging.error(f"Error running bluetoothctl: {e}")
return
def is_bluetooth_active() -> bool:
"""
If we dont check it, the script will hang somewhere.
"""
for p in psutil.process_iter():
try:
if p.name() == "bluetoothd":
return True
except psutil.Error:
pass
return False