-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbasic.py
More file actions
51 lines (41 loc) · 1.91 KB
/
Copy pathbasic.py
File metadata and controls
51 lines (41 loc) · 1.91 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
import time
# Thresholds for drastic changes
HR_THRESHOLD = 30 # Sudden change in heart rate (bpm)
STEP_THRESHOLD = 500 # Sudden drop in steps
SPO2_THRESHOLD = 5 # Sudden drop in SpO2 percentage
# Store previous values
previous_data = {"heart_rate": None, "steps": None, "spo2": None, "timestamp": None}
def detect_drastic_change(current_heart_rate, current_steps, current_spo2):
global previous_data
alert_message = ""
if previous_data["timestamp"]:
time_diff = time.time() - previous_data["timestamp"]
# Check drastic heart rate change
if previous_data["heart_rate"] is not None:
hr_diff = abs(current_heart_rate - previous_data["heart_rate"])
if hr_diff >= HR_THRESHOLD:
alert_message += f"⚠️ Drastic heart rate change detected: {hr_diff} bpm.\n"
# Check drastic step count change
if previous_data["steps"] is not None:
step_diff = abs(current_steps - previous_data["steps"])
if step_diff >= STEP_THRESHOLD:
alert_message += f"⚠️ Sudden step count drop: {step_diff} steps.\n"
# Check drastic SpO2 change
if previous_data["spo2"] is not None:
spo2_diff = abs(current_spo2 - previous_data["spo2"])
if spo2_diff >= SPO2_THRESHOLD:
alert_message += f"⚠️ Sudden SpO2 drop: {spo2_diff}%.\n"
# Update previous data
previous_data = {
"heart_rate": current_heart_rate,
"steps": current_steps,
"spo2": current_spo2,
"timestamp": time.time()
}
if alert_message:
return alert_message # This can be sent via Twilio or displayed in the app
return "✅ No drastic changes detected."
# Example usage
print(detect_drastic_change(100, 5000, 98)) # First call (no alert)
time.sleep(2) # Simulating time delay
print(detect_drastic_change(140, 4500, 92)) # Second call (should trigger alert)