File size: 1,729 Bytes
90654e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Literal, TypedDict

EscalationLevel = Literal["ignore", "log_only", "notify_user", "alarm"]

class EscalationDecision(TypedDict):
    level: EscalationLevel
    reason: str

def decide_escalation(event_type: str, is_night: bool, human_confirmed: bool) -> EscalationDecision:
    """Decide how to escalate a simple security event."""
    et = event_type.lower()

    if et == "door_open":
        if is_night and not human_confirmed:
            return {"level": "alarm", "reason": "Door opened at night with no human confirmed."}
        return {"level": "notify_user", "reason": "Door opened, but appears normal."}

    if et in {"window_open", "device_disconnected"}:
        if is_night:
            return {"level": "notify_user", "reason": "Event at night; user should review."}
        return {"level": "log_only", "reason": "Event during daytime; log for later review."}

    if et in {"motion_detected", "noise_high"}:
        if is_night and not human_confirmed:
            return {"level": "notify_user", "reason": "Unexpected motion/noise at night."}
        if not human_confirmed:
            return {"level": "log_only", "reason": "No human detected; log event."}
        return {"level": "ignore", "reason": "Human presence detected; likely normal."}

    return {"level": "log_only", "reason": "Unknown event type; logging by default."}


if __name__ == "__main__":
    tests = [
        ("door_open", True, False),
        ("door_open", False, True),
        ("motion_detected", True, False),
        ("noise_high", False, True),
        ("device_disconnected", True, False),
    ]
    for et, night, human in tests:
        print(et, night, human, "->", decide_escalation(et, night, human))