-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiphone_monitor.py
More file actions
137 lines (100 loc) · 3.99 KB
/
iphone_monitor.py
File metadata and controls
137 lines (100 loc) · 3.99 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
import subprocess
import time
import threading
from typing import Optional
try:
from pymobiledevice3.usbmux import list_devices
from pymobiledevice3.lockdown import create_using_usbmux
from pymobiledevice3.services.diagnostics import DiagnosticsService
except ImportError:
print("Required package not found. Install with:")
print(" pip install pymobiledevice3")
exit(1)
def get_iphone_info() -> Optional[dict]:
devices = list_devices()
if not devices:
return None
try:
lockdown = create_using_usbmux()
device_info = {
"model": lockdown.display_name,
"product_type": lockdown.product_type,
"ios_version": lockdown.product_version,
}
try:
with DiagnosticsService(lockdown) as diag:
battery_info = diag.get_battery()
if "CurrentCapacity" in battery_info:
device_info["battery_percent"] = battery_info["CurrentCapacity"]
elif "BatteryCurrentCapacity" in battery_info:
device_info["battery_percent"] = battery_info["BatteryCurrentCapacity"]
if "Temperature" in battery_info:
temp_raw = battery_info["Temperature"]
device_info["temperature_c"] = round((temp_raw / 100) - 273.15, 1)
device_info["is_charging"] = battery_info.get("IsCharging", False)
except Exception as e:
device_info["diagnostics_error"] = str(e)
return device_info
except Exception as e:
return {"error": str(e)}
def show_popup(title: str, message: str):
script = f'''
display dialog "{message}" with title "{title}" buttons {{"OK"}} default button "OK"
'''
subprocess.run(["osascript", "-e", script], capture_output=True)
def format_device_info(info: dict) -> str:
lines = []
if "error" in info:
return f"Error: {info['error']}"
model = info.get("model", "Unknown")
product_type = info.get("product_type", "")
ios_version = info.get("ios_version", "Unknown")
lines.append(f"Model: {model} ({product_type})")
lines.append(f"iOS Version: {ios_version}")
if "battery_percent" in info:
charging = " (Charging)" if info.get("is_charging") else ""
lines.append(f"Battery: {info['battery_percent']}%{charging}")
if "temperature_c" in info:
temp_c = info["temperature_c"]
temp_f = round((temp_c * 9/5) + 32, 1)
if temp_c < 0:
status = " (Too Cold!)"
elif temp_c > 35:
status = " (Too Hot!)"
else:
status = " (Normal)"
lines.append(f"Temperature: {temp_c}°C / {temp_f}°F{status}")
if "diagnostics_error" in info:
lines.append(f"\\nNote: Some data unavailable - {info['diagnostics_error']}")
return "\\n".join(lines)
def monitor_iphone_connection():
print("iPhone Monitor started. Waiting for device connection...")
print("Press Ctrl+C to stop.\n")
was_connected = False
while True:
try:
devices = list_devices()
is_connected = len(devices) > 0
if is_connected and not was_connected:
print("iPhone detected! Gathering information...")
time.sleep(1)
info = get_iphone_info()
if info:
message = format_device_info(info)
print(f"\n{message}\n")
show_popup("iPhone Connected", message)
else:
print("Could not retrieve device information.")
elif not is_connected and was_connected:
print("iPhone disconnected.\n")
was_connected = is_connected
time.sleep(2)
except KeyboardInterrupt:
print("\nMonitor stopped.")
break
except Exception as e:
print(f"Error: {e}")
time.sleep(2)
if __name__ == "__main__":
monitor_iphone_connection()