berserk3142 commited on
Commit
837f06d
·
1 Parent(s): fded4b6

Deploy: Drone Security Analyst - AI drone surveillance with YOLOv8 + GPT-4o-mini + LangChain

Browse files
.dockerignore ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .env
2
+ __pycache__/
3
+ *.pyc
4
+ .pytest_cache/
5
+ data/
6
+ *.db
7
+ chroma_db/
8
+ .venv/
9
+ venv/
10
+ *.pt
11
+ *.onnx
12
+ .git/
13
+ .gitignore
.env.example ADDED
@@ -0,0 +1 @@
 
 
1
+ GEMINI_API_KEY=your_gemini_api_key_here
.gitignore ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .env
2
+ __pycache__/
3
+ *.pyc
4
+ *.pyo
5
+ .pytest_cache/
6
+ data/
7
+ *.db
8
+ chroma_db/
9
+ .venv/
10
+ venv/
11
+ dist/
12
+ build/
13
+ *.egg-info/
14
+ *.pt
15
+ *.onnx
16
+ .gemini/
Dockerfile ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+
3
+ # Set working directory
4
+ WORKDIR /app
5
+
6
+ # Install system dependencies for OpenCV
7
+ RUN apt-get update && apt-get install -y --no-install-recommends \
8
+ libgl1-mesa-glx \
9
+ libglib2.0-0 \
10
+ libsm6 \
11
+ libxext6 \
12
+ libxrender1 \
13
+ && rm -rf /var/lib/apt/lists/*
14
+
15
+ # Copy requirements first for Docker layer caching
16
+ COPY requirements.txt .
17
+
18
+ # Install Python dependencies
19
+ RUN pip install --no-cache-dir --upgrade pip && \
20
+ pip install --no-cache-dir -r requirements.txt && \
21
+ pip install --no-cache-dir python-multipart
22
+
23
+ # Copy entire project
24
+ COPY . .
25
+
26
+ # Create data directory
27
+ RUN mkdir -p /app/data
28
+
29
+ # Hugging Face Spaces uses port 7860
30
+ ENV PORT=7860
31
+ EXPOSE 7860
32
+
33
+ # Start the FastAPI server
34
+ CMD ["uvicorn", "server:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -1,10 +1,24 @@
1
  ---
2
  title: Drone Security Analyst
3
- emoji: 👁
4
- colorFrom: indigo
5
- colorTo: gray
6
  sdk: docker
7
- pinned: false
 
 
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  title: Drone Security Analyst
3
+ emoji: 🛡️
4
+ colorFrom: green
5
+ colorTo: blue
6
  sdk: docker
7
+ app_port: 7860
8
+ pinned: true
9
+ license: mit
10
+ short_description: AI-powered drone surveillance with YOLOv8 + GPT-4o-mini
11
  ---
12
 
13
+ # 🛡️ Drone Security Analyst Agent
14
+
15
+ AI-powered autonomous drone surveillance system with real-time threat detection.
16
+
17
+ **Features:**
18
+ - 30 simulated drone frames covering a full 24-hour patrol
19
+ - YOLOv8 + DeepSORT object detection on uploaded videos
20
+ - GPT-4o-mini vision analysis for contextual understanding
21
+ - LangChain ReAct agent with 4 custom tools
22
+ - Dual indexing: SQLite (structured) + ChromaDB (semantic search)
23
+ - 8 configurable alert rules with 4 severity levels
24
+ - Military-grade web dashboard (Aegis Drone Command)
agent/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ # Agent package
agent/prompts.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Agent Prompts — System prompts and templates for the security analyst agent.
3
+ """
4
+
5
+ SECURITY_AGENT_SYSTEM_PROMPT = """You are an elite AI Security Analyst monitoring live drone surveillance footage for SecureTech Industrial Complex.
6
+
7
+ ## Your Role
8
+ You process frame-by-frame analysis results from a Vision Language Model (VLM) and make intelligent security decisions. You maintain awareness of previous frames to identify patterns, repeated visitors, and escalating threats.
9
+
10
+ ## Your Responsibilities
11
+ For EVERY frame analysis you receive, you must:
12
+ 1. **LOG** the event — decide what details to record using the log_event tool
13
+ 2. **EVALUATE ALERTS** — check if any security rules are triggered using the check_alert_rules tool
14
+ 3. **IDENTIFY PATTERNS** — reference your memory of previous frames to spot repeat vehicles, loitering individuals, or escalating behavior
15
+ 4. **RECOMMEND ACTIONS** — provide actionable security recommendations
16
+
17
+ ## Decision Framework
18
+ - **LOW risk**: Log and continue patrol. Examples: employee arrivals, scheduled deliveries, wildlife.
19
+ - **MEDIUM risk**: Log with a note. Monitor on next pass. Examples: unauthorized parking, unfamiliar vehicles during business hours, doors left ajar.
20
+ - **HIGH risk**: Log + Alert. Recommend security dispatch. Examples: persons after hours, unknown vehicles at night, suspicious behavior.
21
+ - **CRITICAL risk**: Log + Alert + IMMEDIATE action. Examples: perimeter breach attempts, unauthorized access to server room, active theft.
22
+
23
+ ## Pattern Recognition Guidelines
24
+ - If you see the same vehicle described more than once, flag it as "repeat visitor"
25
+ - If a person appears in consecutive frames near a restricted area, flag as "loitering"
26
+ - If activity escalates (e.g., person near fence → person climbing fence), note the escalation
27
+ - Compare current frame to your memory of previous frames
28
+
29
+ ## Output Format
30
+ For each frame, provide a concise response with:
31
+ 1. What you observed (1 sentence)
32
+ 2. Actions taken (logged, alert fired, etc.)
33
+ 3. Any pattern connections to previous frames
34
+ 4. Risk assessment and recommendation
35
+
36
+ Keep responses professional, concise, and actionable — like a real security analyst's radio communication."""
37
+
38
+
39
+ SUMMARY_PROMPT = """Based on all the security events processed during this monitoring session, generate a comprehensive security summary report.
40
+
41
+ Include:
42
+ 1. **Session Overview**: Total frames processed, time range covered, locations patrolled
43
+ 2. **Key Events**: Most significant events in chronological order
44
+ 3. **Alert Summary**: All alerts fired, grouped by severity
45
+ 4. **Pattern Analysis**: Recurring vehicles, repeat visitors, escalating threats
46
+ 5. **Risk Assessment**: Overall security posture for the monitored period
47
+ 6. **Recommendations**: Suggested actions for security team
48
+
49
+ Format the summary as a clear, professional security briefing."""
agent/security_agent.py ADDED
@@ -0,0 +1,211 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Security Agent — LangGraph-based AI agent for drone security analysis.
3
+
4
+ Orchestrates frame analysis using custom tools, maintains conversation memory
5
+ for cross-frame pattern recognition, and produces intelligent security assessments.
6
+ """
7
+
8
+ from langchain_openai import ChatOpenAI
9
+ from langgraph.prebuilt import create_react_agent
10
+
11
+ from config import OPENAI_API_KEY, OPENAI_MODEL
12
+ from agent.prompts import SECURITY_AGENT_SYSTEM_PROMPT
13
+ from agent.tools import log_event, check_alert_rules, query_past_events, generate_summary, init_tools
14
+ from indexing.sqlite_store import SQLiteStore
15
+ from indexing.chroma_store import ChromaStore
16
+ from alerts.rules_engine import AlertManager
17
+
18
+
19
+ class SecurityAgent:
20
+ """LangGraph-based security analyst agent with memory and custom tools."""
21
+
22
+ def __init__(self, sqlite_store: SQLiteStore = None,
23
+ chroma_store: ChromaStore = None,
24
+ alert_manager: AlertManager = None):
25
+ """
26
+ Initialize the security agent.
27
+
28
+ Args:
29
+ sqlite_store: SQLiteStore instance for structured storage
30
+ chroma_store: ChromaStore instance for semantic search
31
+ alert_manager: AlertManager instance for alert evaluation
32
+ """
33
+ # Initialize stores
34
+ self.sqlite_store = sqlite_store or SQLiteStore()
35
+ self.chroma_store = chroma_store or ChromaStore()
36
+ self.alert_manager = alert_manager or AlertManager()
37
+
38
+ # Initialize tool dependencies
39
+ init_tools(self.sqlite_store, self.chroma_store, self.alert_manager)
40
+
41
+ # Define tools
42
+ self.tools = [log_event, check_alert_rules, query_past_events, generate_summary]
43
+
44
+ # Initialize LLM
45
+ self.llm = ChatOpenAI(
46
+ model=OPENAI_MODEL,
47
+ api_key=OPENAI_API_KEY,
48
+ temperature=0.1,
49
+ )
50
+
51
+ # Create LangGraph react agent
52
+ self.agent = create_react_agent(
53
+ self.llm,
54
+ self.tools,
55
+ prompt=SECURITY_AGENT_SYSTEM_PROMPT,
56
+ )
57
+
58
+ # Store message history for cross-frame context
59
+ self.message_history = []
60
+
61
+ def process_frame(self, frame: dict, analysis: dict, telemetry: dict) -> dict:
62
+ """
63
+ Process a single frame through the agent.
64
+
65
+ Args:
66
+ frame: Raw frame dict (frame_id, time, location, description)
67
+ analysis: VLM analysis result dict
68
+ telemetry: Telemetry data dict
69
+
70
+ Returns:
71
+ Dict with agent_response, alerts, and actions taken
72
+ """
73
+ # Build the input prompt for the agent
74
+ input_text = self._build_frame_input(frame, analysis, telemetry)
75
+
76
+ try:
77
+ # Add the new message to history
78
+ messages = self.message_history + [{"role": "user", "content": input_text}]
79
+
80
+ result = self.agent.invoke(
81
+ {"messages": messages},
82
+ config={"recursion_limit": 15},
83
+ )
84
+
85
+ # Extract the final AI message
86
+ output_messages = result.get("messages", [])
87
+ agent_response = "No response generated"
88
+ if output_messages:
89
+ # Get the last AI message
90
+ for msg in reversed(output_messages):
91
+ if hasattr(msg, 'content') and msg.content and hasattr(msg, 'type') and msg.type == 'ai':
92
+ agent_response = msg.content
93
+ break
94
+
95
+ # Update message history with the user message and final AI response
96
+ # Keep history manageable (last 10 exchanges)
97
+ self.message_history.append({"role": "user", "content": input_text})
98
+ self.message_history.append({"role": "assistant", "content": agent_response})
99
+ if len(self.message_history) > 20:
100
+ self.message_history = self.message_history[-20:]
101
+
102
+ # Extract alerts from the response
103
+ alerts = self.alert_manager.fired_alerts[-5:] # Last 5 alerts
104
+
105
+ return {
106
+ "agent_response": agent_response,
107
+ "alerts": alerts,
108
+ "steps": len([m for m in output_messages if hasattr(m, 'type') and m.type == 'tool']),
109
+ "success": True,
110
+ }
111
+
112
+ except Exception as e:
113
+ error_msg = f"Agent error processing frame {frame.get('frame_id', '?')}: {str(e)}"
114
+ # Still try to log the event directly
115
+ try:
116
+ self._direct_log(frame, analysis)
117
+ except Exception:
118
+ pass
119
+
120
+ return {
121
+ "agent_response": error_msg,
122
+ "alerts": [],
123
+ "steps": 0,
124
+ "success": False,
125
+ }
126
+
127
+ def _build_frame_input(self, frame: dict, analysis: dict, telemetry: dict) -> str:
128
+ """Build a comprehensive input string for the agent."""
129
+ import json
130
+
131
+ # Merge frame and analysis info for tool calls
132
+ tool_data = {
133
+ "frame_id": frame.get("frame_id", 0),
134
+ "timestamp": frame.get("time", ""),
135
+ "time": frame.get("time", ""),
136
+ "location": frame.get("location", ""),
137
+ "objects": analysis.get("objects", []),
138
+ "event_type": analysis.get("event_type", ""),
139
+ "risk_level": analysis.get("risk_level", ""),
140
+ "is_suspicious": analysis.get("is_suspicious", False),
141
+ "description": analysis.get("description", ""),
142
+ "raw_description": frame.get("description", ""),
143
+ "recommended_action": analysis.get("recommended_action", ""),
144
+ }
145
+ tool_json = json.dumps(tool_data)
146
+
147
+ return f"""NEW FRAME FOR ANALYSIS:
148
+
149
+ Frame ID: {frame.get('frame_id')}
150
+ Time: {frame.get('time')}
151
+ Location: {frame.get('location')}
152
+ Drone Altitude: {telemetry.get('altitude_m', 'N/A')}m | Battery: {telemetry.get('battery_pct', 'N/A')}%
153
+
154
+ VLM Analysis Result:
155
+ - Objects Detected: {', '.join(analysis.get('objects', ['None']))}
156
+ - Event Type: {analysis.get('event_type', 'unknown')}
157
+ - Risk Level: {analysis.get('risk_level', 'unknown')}
158
+ - Suspicious: {analysis.get('is_suspicious', False)}
159
+ - Assessment: {analysis.get('description', 'N/A')}
160
+ - Recommended Action: {analysis.get('recommended_action', 'N/A')}
161
+
162
+ Raw Frame Description: {frame.get('description', 'N/A')}
163
+
164
+ INSTRUCTIONS:
165
+ 1. Use the log_event tool to log this event. Pass this JSON: {tool_json}
166
+ 2. Use the check_alert_rules tool to evaluate alerts. Pass the same JSON.
167
+ 3. Based on your memory of previous frames, note any patterns (repeat vehicles, escalating threats, etc.)
168
+ 4. Provide your security assessment."""
169
+
170
+ def _direct_log(self, frame: dict, analysis: dict):
171
+ """Fallback: directly log to stores without going through the agent."""
172
+ self.sqlite_store.insert_frame(
173
+ frame_id=frame.get("frame_id", 0),
174
+ timestamp=frame.get("time", ""),
175
+ location=frame.get("location", ""),
176
+ objects_detected=analysis.get("objects", []),
177
+ event_type=analysis.get("event_type", ""),
178
+ risk_level=analysis.get("risk_level", ""),
179
+ is_suspicious=analysis.get("is_suspicious", False),
180
+ raw_description=frame.get("description", ""),
181
+ analysis_summary=analysis.get("description", ""),
182
+ )
183
+ self.chroma_store.add_frame(
184
+ frame_id=frame.get("frame_id", 0),
185
+ timestamp=frame.get("time", ""),
186
+ location=frame.get("location", ""),
187
+ description=frame.get("description", ""),
188
+ event_type=analysis.get("event_type", ""),
189
+ risk_level=analysis.get("risk_level", ""),
190
+ )
191
+
192
+ def get_summary(self) -> str:
193
+ """Ask the agent to generate a session summary."""
194
+ try:
195
+ result = self.agent.invoke({
196
+ "messages": [
197
+ {"role": "user", "content": "Generate a comprehensive security summary for this monitoring session using the generate_summary tool."}
198
+ ]
199
+ })
200
+ output_messages = result.get("messages", [])
201
+ for msg in reversed(output_messages):
202
+ if hasattr(msg, 'content') and msg.content and hasattr(msg, 'type') and msg.type == 'ai':
203
+ return msg.content
204
+ return "Summary generation failed."
205
+ except Exception as e:
206
+ return f"Error generating summary: {e}"
207
+
208
+ def reset(self):
209
+ """Reset the agent's memory and stores for a new session."""
210
+ self.message_history.clear()
211
+ self.alert_manager.reset()
agent/tools.py ADDED
@@ -0,0 +1,225 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Agent Tools — Custom LangChain tools for the security analyst agent.
3
+
4
+ Provides four tools:
5
+ 1. log_event — Write events to SQLite + ChromaDB
6
+ 2. check_alert_rules — Evaluate alert conditions
7
+ 3. query_past_events — Search historical frames
8
+ 4. generate_summary — Produce session summary
9
+ """
10
+
11
+ import json
12
+ from langchain.tools import tool
13
+ from indexing.sqlite_store import SQLiteStore
14
+ from indexing.chroma_store import ChromaStore
15
+ from alerts.rules_engine import AlertManager
16
+
17
+ # Global instances (initialized when agent is created)
18
+ _sqlite_store: SQLiteStore = None
19
+ _chroma_store: ChromaStore = None
20
+ _alert_manager: AlertManager = None
21
+
22
+
23
+ def init_tools(sqlite_store: SQLiteStore, chroma_store: ChromaStore,
24
+ alert_manager: AlertManager):
25
+ """Initialize the tool dependencies."""
26
+ global _sqlite_store, _chroma_store, _alert_manager
27
+ _sqlite_store = sqlite_store
28
+ _chroma_store = chroma_store
29
+ _alert_manager = alert_manager
30
+
31
+
32
+ @tool
33
+ def log_event(event_json: str) -> str:
34
+ """Log a security event to the database. Input must be a JSON string with keys:
35
+ frame_id (int), timestamp (str), location (str), objects (list of strings),
36
+ event_type (str), risk_level (str), is_suspicious (bool), description (str),
37
+ raw_description (str, optional).
38
+ Returns a confirmation message."""
39
+ try:
40
+ event = json.loads(event_json)
41
+
42
+ frame_id = event.get("frame_id", 0)
43
+ timestamp = event.get("timestamp", "")
44
+ location = event.get("location", "")
45
+ objects = event.get("objects", [])
46
+ event_type = event.get("event_type", "normal_operations")
47
+ risk_level = event.get("risk_level", "low")
48
+ is_suspicious = event.get("is_suspicious", False)
49
+ description = event.get("description", "")
50
+ raw_description = event.get("raw_description", "")
51
+
52
+ # Log to SQLite
53
+ _sqlite_store.insert_frame(
54
+ frame_id=frame_id,
55
+ timestamp=timestamp,
56
+ location=location,
57
+ objects_detected=objects,
58
+ event_type=event_type,
59
+ risk_level=risk_level,
60
+ is_suspicious=is_suspicious,
61
+ raw_description=raw_description,
62
+ analysis_summary=description,
63
+ )
64
+
65
+ # Log to ChromaDB for semantic search
66
+ _chroma_store.add_frame(
67
+ frame_id=frame_id,
68
+ timestamp=timestamp,
69
+ location=location,
70
+ description=f"{raw_description} {description}",
71
+ event_type=event_type,
72
+ risk_level=risk_level,
73
+ objects_detected=", ".join(objects) if isinstance(objects, list) else str(objects),
74
+ )
75
+
76
+ # Track vehicles
77
+ for obj in objects:
78
+ obj_lower = obj.lower()
79
+ if any(v in obj_lower for v in ["car", "truck", "vehicle", "van", "sedan", "suv",
80
+ "camry", "civic", "bmw", "ford", "chevrolet",
81
+ "silverado", "f150"]):
82
+ count = _sqlite_store.log_vehicle(obj, frame_id, timestamp, location)
83
+ if count > 1:
84
+ return (f"Event logged: Frame {frame_id} at {timestamp} ({location}). "
85
+ f"[!] VEHICLE RE-ENTRY: {obj} seen {count} times today.")
86
+
87
+ return f"Event logged: Frame {frame_id} at {timestamp} ({location}). {event_type} - {risk_level} risk."
88
+
89
+ except json.JSONDecodeError:
90
+ return f"Error: Invalid JSON input. Please provide a valid JSON string."
91
+ except Exception as e:
92
+ return f"Error logging event: {str(e)}"
93
+
94
+
95
+ @tool
96
+ def check_alert_rules(analysis_json: str) -> str:
97
+ """Check if a frame analysis triggers any alert rules. Input must be a JSON string
98
+ with keys: frame_id (int), time (str), location (str), objects (list), event_type (str),
99
+ risk_level (str), is_suspicious (bool), description (str), raw_description (str).
100
+ Returns triggered alerts or 'No alerts triggered'."""
101
+ try:
102
+ analysis = json.loads(analysis_json)
103
+
104
+ # Get vehicle context
105
+ context = {}
106
+ for obj in analysis.get("objects", []):
107
+ obj_lower = obj.lower()
108
+ if any(v in obj_lower for v in ["car", "truck", "vehicle", "van", "sedan", "suv",
109
+ "camry", "civic", "bmw", "ford", "chevrolet"]):
110
+ count = _sqlite_store.get_vehicle_count(obj)
111
+ if count > 0:
112
+ context["vehicle_count"] = count
113
+ context["vehicle_name"] = obj
114
+
115
+ # Evaluate rules
116
+ triggered = _alert_manager.evaluate_rules(analysis, context)
117
+
118
+ if triggered:
119
+ # Log alerts to SQLite
120
+ for alert in triggered:
121
+ _sqlite_store.insert_alert(
122
+ frame_id=analysis.get("frame_id", 0),
123
+ timestamp=analysis.get("time", ""),
124
+ rule_name=alert["rule_name"],
125
+ severity=alert["severity"],
126
+ message=alert["message"],
127
+ location=alert.get("location", ""),
128
+ )
129
+
130
+ alert_msgs = [f"[{a['severity']}] {a['message']}" for a in triggered]
131
+ return "ALERTS TRIGGERED:\n" + "\n".join(alert_msgs)
132
+ else:
133
+ return "No alerts triggered. All clear."
134
+
135
+ except json.JSONDecodeError:
136
+ return "Error: Invalid JSON input."
137
+ except Exception as e:
138
+ return f"Error checking alerts: {str(e)}"
139
+
140
+
141
+ @tool
142
+ def query_past_events(query: str) -> str:
143
+ """Search past events in the database. Input should be a natural language query
144
+ like 'truck', 'suspicious activity', 'perimeter fence', or a specific object type.
145
+ Uses both keyword (SQLite) and semantic (ChromaDB) search.
146
+ Returns matching events."""
147
+ try:
148
+ results = []
149
+
150
+ # SQLite keyword search
151
+ sqlite_results = _sqlite_store.query_by_object(query)
152
+ if sqlite_results:
153
+ results.append(f"=== SQLite Results ({len(sqlite_results)} matches) ===")
154
+ for r in sqlite_results[:5]:
155
+ results.append(
156
+ f" Frame {r['frame_id']} [{r['timestamp']}] at {r['location']}: "
157
+ f"{r.get('analysis_summary', r.get('raw_description', 'N/A'))[:100]}"
158
+ )
159
+
160
+ # ChromaDB semantic search
161
+ chroma_results = _chroma_store.semantic_search(query, top_k=5)
162
+ if chroma_results:
163
+ results.append(f"\n=== Semantic Search Results ({len(chroma_results)} matches) ===")
164
+ for r in chroma_results[:5]:
165
+ results.append(
166
+ f" Frame {r['frame_id']} [{r.get('timestamp', 'N/A')}] at "
167
+ f"{r.get('location', 'N/A')}: similarity={1 - r.get('distance', 0):.2f}"
168
+ )
169
+
170
+ if results:
171
+ return "\n".join(results)
172
+ else:
173
+ return f"No events found matching '{query}'."
174
+
175
+ except Exception as e:
176
+ return f"Error querying events: {str(e)}"
177
+
178
+
179
+ @tool
180
+ def generate_summary() -> str:
181
+ """Generate a summary report of all events in the current monitoring session.
182
+ No input required — pass an empty string or 'generate'.
183
+ Returns a formatted session summary."""
184
+ try:
185
+ stats = _sqlite_store.get_session_stats()
186
+ alerts = _sqlite_store.get_all_alerts()
187
+ all_frames = _sqlite_store.get_all_frames()
188
+
189
+ summary_lines = [
190
+ "=======================================",
191
+ " SESSION SECURITY SUMMARY ",
192
+ "=======================================",
193
+ f"",
194
+ f"Total Frames Processed: {stats['total_frames']}",
195
+ f"Suspicious Frames: {stats['suspicious_frames']}",
196
+ f"Total Alerts: {stats['total_alerts']}",
197
+ ]
198
+
199
+ if stats["alerts_by_severity"]:
200
+ summary_lines.append("\nAlerts by Severity:")
201
+ for sev, count in sorted(stats["alerts_by_severity"].items()):
202
+ summary_lines.append(f" [{sev}]: {count}")
203
+
204
+ if alerts:
205
+ summary_lines.append("\nAlert Details:")
206
+ for alert in alerts:
207
+ summary_lines.append(
208
+ f" [{alert['timestamp']}] [{alert['severity']}] {alert['message']}"
209
+ )
210
+
211
+ # Suspicious frame details
212
+ suspicious = [f for f in all_frames if f.get("is_suspicious")]
213
+ if suspicious:
214
+ summary_lines.append(f"\nSuspicious Activity Log ({len(suspicious)} events):")
215
+ for frame in suspicious:
216
+ summary_lines.append(
217
+ f" Frame {frame['frame_id']} [{frame['timestamp']}] at {frame['location']}: "
218
+ f"{frame.get('analysis_summary', 'N/A')[:80]}"
219
+ )
220
+
221
+ summary_lines.append("\n=======================================")
222
+ return "\n".join(summary_lines)
223
+
224
+ except Exception as e:
225
+ return f"Error generating summary: {str(e)}"
alerts/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ # Alerts package
alerts/rules_engine.py ADDED
@@ -0,0 +1,260 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Alert Rules Engine — Evaluates security conditions and triggers alerts.
3
+
4
+ Defines a set of configurable rules that are evaluated against each frame's
5
+ analysis results. Fires alerts when conditions are met.
6
+ """
7
+
8
+ from config import (
9
+ SEVERITY_LOW, SEVERITY_MEDIUM, SEVERITY_HIGH, SEVERITY_CRITICAL,
10
+ KNOWN_VEHICLES, BUSINESS_HOURS_START, BUSINESS_HOURS_END
11
+ )
12
+
13
+
14
+ # --- Alert Rule Definitions ---
15
+ ALERT_RULES = [
16
+ {
17
+ "name": "after_hours_person",
18
+ "description": "Person detected outside business hours",
19
+ "severity": SEVERITY_HIGH,
20
+ "message": "[WARNING] Person detected after hours at {location} - {details}",
21
+ },
22
+ {
23
+ "name": "perimeter_breach",
24
+ "description": "Person near or attempting to breach perimeter fence",
25
+ "severity": SEVERITY_CRITICAL,
26
+ "message": "[CRITICAL] PERIMETER BREACH at {location} - {details}",
27
+ },
28
+ {
29
+ "name": "unauthorized_access",
30
+ "description": "Unauthorized access attempt to restricted area",
31
+ "severity": SEVERITY_CRITICAL,
32
+ "message": "[CRITICAL] UNAUTHORIZED ACCESS ATTEMPT at {location} - {details}",
33
+ },
34
+ {
35
+ "name": "unknown_vehicle_night",
36
+ "description": "Unrecognized vehicle detected after hours",
37
+ "severity": SEVERITY_HIGH,
38
+ "message": "[WARNING] Unknown vehicle at {location} after hours - {details}",
39
+ },
40
+ {
41
+ "name": "vehicle_reentry",
42
+ "description": "Same vehicle seen multiple times (possible surveillance)",
43
+ "severity": SEVERITY_MEDIUM,
44
+ "message": "[ALERT] Vehicle re-entry detected: {details} - seen {count} times at {location}",
45
+ },
46
+ {
47
+ "name": "suspicious_behavior",
48
+ "description": "Suspicious activity detected (loitering, photographing, circling)",
49
+ "severity": SEVERITY_HIGH,
50
+ "message": "[WARNING] Suspicious behavior at {location} - {details}",
51
+ },
52
+ {
53
+ "name": "door_anomaly",
54
+ "description": "Door open or ajar when it should be closed",
55
+ "severity": SEVERITY_MEDIUM,
56
+ "message": "[ALERT] Door anomaly at {location} - {details}",
57
+ },
58
+ {
59
+ "name": "unauthorized_parking",
60
+ "description": "Vehicle parked in unauthorized zone",
61
+ "severity": SEVERITY_LOW,
62
+ "message": "[INFO] Unauthorized parking at {location} - {details}",
63
+ },
64
+ ]
65
+
66
+
67
+ class AlertManager:
68
+ """Manages alert evaluation and tracking to prevent duplicates."""
69
+
70
+ def __init__(self):
71
+ self.fired_alerts: list[dict] = []
72
+ self._alert_history: set = set() # (rule_name, location) to prevent duplicates
73
+
74
+ def evaluate_rules(self, frame_analysis: dict, context: dict = None) -> list[dict]:
75
+ """
76
+ Evaluate all alert rules against a frame analysis result.
77
+
78
+ Args:
79
+ frame_analysis: Dict with keys: objects, event_type, risk_level,
80
+ location, is_suspicious, description
81
+ context: Additional context (vehicle counts, time info, etc.)
82
+
83
+ Returns:
84
+ List of triggered alert dicts
85
+ """
86
+ triggered = []
87
+ context = context or {}
88
+
89
+ time_str = frame_analysis.get("time", "12:00")
90
+ hour = int(time_str.split(":")[0])
91
+ location = frame_analysis.get("location", "Unknown")
92
+ objects = frame_analysis.get("objects", [])
93
+ event_type = frame_analysis.get("event_type", "")
94
+ risk_level = frame_analysis.get("risk_level", "low")
95
+ is_suspicious = frame_analysis.get("is_suspicious", False)
96
+ description = frame_analysis.get("description", "")
97
+ raw_description = frame_analysis.get("raw_description", "")
98
+
99
+ is_after_hours = hour >= BUSINESS_HOURS_END or hour < BUSINESS_HOURS_START
100
+
101
+ # Combine descriptions for matching
102
+ full_text = f"{description} {raw_description} {event_type}".lower()
103
+
104
+ # --- Rule: After-hours person ---
105
+ has_person = any("person" in obj.lower() or "individual" in obj.lower()
106
+ or "man" in obj.lower() or "woman" in obj.lower()
107
+ for obj in objects) or "person" in full_text
108
+ if is_after_hours and has_person:
109
+ alert = self._create_alert(
110
+ "after_hours_person", location,
111
+ details=f"Person detected at {time_str}",
112
+ frame_analysis=frame_analysis
113
+ )
114
+ if alert:
115
+ triggered.append(alert)
116
+
117
+ # --- Rule: Perimeter breach ---
118
+ is_perimeter = "perimeter" in location.lower() or "fence" in location.lower()
119
+ has_breach_indicators = any(w in full_text for w in [
120
+ "climb", "breach", "cut", "hole", "testing", "attempting",
121
+ "break", "intrusion", "trespass"
122
+ ])
123
+ if is_perimeter and has_person and (has_breach_indicators or is_suspicious):
124
+ alert = self._create_alert(
125
+ "perimeter_breach", location,
126
+ details=description or "Perimeter breach attempt detected",
127
+ frame_analysis=frame_analysis
128
+ )
129
+ if alert:
130
+ triggered.append(alert)
131
+
132
+ # --- Rule: Unauthorized access ---
133
+ is_restricted = any(area in location.lower() for area in [
134
+ "server room", "warehouse", "loading dock"
135
+ ])
136
+ has_unauthorized = any(w in full_text for w in [
137
+ "unauthorized", "no badge", "ski mask", "break in",
138
+ "forced entry", "no identification", "mask"
139
+ ])
140
+ if is_restricted and has_unauthorized:
141
+ alert = self._create_alert(
142
+ "unauthorized_access", location,
143
+ details=description or "Unauthorized access attempt",
144
+ frame_analysis=frame_analysis
145
+ )
146
+ if alert:
147
+ triggered.append(alert)
148
+
149
+ # --- Rule: Unknown vehicle at night ---
150
+ has_vehicle = any("vehicle" in obj.lower() or "car" in obj.lower()
151
+ or "truck" in obj.lower() or "suv" in obj.lower()
152
+ or "sedan" in obj.lower() or "van" in obj.lower()
153
+ for obj in objects)
154
+ is_known = any(kv.lower() in full_text for kv in KNOWN_VEHICLES)
155
+ if is_after_hours and has_vehicle and not is_known:
156
+ alert = self._create_alert(
157
+ "unknown_vehicle_night", location,
158
+ details=f"Unidentified vehicle at {time_str}",
159
+ frame_analysis=frame_analysis
160
+ )
161
+ if alert:
162
+ triggered.append(alert)
163
+
164
+ # --- Rule: Vehicle re-entry ---
165
+ vehicle_count = context.get("vehicle_count", 0)
166
+ vehicle_name = context.get("vehicle_name", "")
167
+ if vehicle_count > 1:
168
+ alert = self._create_alert(
169
+ "vehicle_reentry", location,
170
+ details=vehicle_name,
171
+ count=vehicle_count,
172
+ frame_analysis=frame_analysis
173
+ )
174
+ if alert:
175
+ triggered.append(alert)
176
+
177
+ # --- Rule: Suspicious behavior ---
178
+ has_suspicious = any(w in full_text for w in [
179
+ "suspicious", "loitering", "circling", "photographing",
180
+ "photo", "surveill", "casing", "watching"
181
+ ])
182
+ if has_suspicious:
183
+ alert = self._create_alert(
184
+ "suspicious_behavior", location,
185
+ details=description or "Suspicious activity observed",
186
+ frame_analysis=frame_analysis
187
+ )
188
+ if alert:
189
+ triggered.append(alert)
190
+
191
+ # --- Rule: Door anomaly ---
192
+ has_door_issue = any(w in full_text for w in [
193
+ "door ajar", "door open", "door unlocked", "partially open"
194
+ ])
195
+ if has_door_issue:
196
+ alert = self._create_alert(
197
+ "door_anomaly", location,
198
+ details="Door found open/ajar outside normal hours",
199
+ frame_analysis=frame_analysis
200
+ )
201
+ if alert:
202
+ triggered.append(alert)
203
+
204
+ # --- Rule: Unauthorized parking ---
205
+ has_parking_violation = "unauthorized" in full_text and ("park" in full_text or "zone" in full_text)
206
+ if has_parking_violation:
207
+ alert = self._create_alert(
208
+ "unauthorized_parking", location,
209
+ details=description or "Vehicle in unauthorized zone",
210
+ frame_analysis=frame_analysis
211
+ )
212
+ if alert:
213
+ triggered.append(alert)
214
+
215
+ return triggered
216
+
217
+ def _create_alert(self, rule_name: str, location: str,
218
+ details: str = "", count: int = 0,
219
+ frame_analysis: dict = None) -> dict | None:
220
+ """Create an alert if it hasn't been fired before for this location."""
221
+ # Find the rule definition
222
+ rule = next((r for r in ALERT_RULES if r["name"] == rule_name), None)
223
+ if not rule:
224
+ return None
225
+
226
+ # Format the message
227
+ message = rule["message"].format(
228
+ location=location,
229
+ details=details,
230
+ count=count,
231
+ )
232
+
233
+ alert = {
234
+ "rule_name": rule_name,
235
+ "severity": rule["severity"],
236
+ "message": message,
237
+ "location": location,
238
+ "time": frame_analysis.get("time", "") if frame_analysis else "",
239
+ "frame_id": frame_analysis.get("frame_id", 0) if frame_analysis else 0,
240
+ }
241
+
242
+ self.fired_alerts.append(alert)
243
+ return alert
244
+
245
+ def get_alert_summary(self) -> dict:
246
+ """Get summary of all fired alerts."""
247
+ summary = {
248
+ "total": len(self.fired_alerts),
249
+ "by_severity": {},
250
+ "alerts": self.fired_alerts,
251
+ }
252
+ for alert in self.fired_alerts:
253
+ sev = alert["severity"]
254
+ summary["by_severity"][sev] = summary["by_severity"].get(sev, 0) + 1
255
+ return summary
256
+
257
+ def reset(self):
258
+ """Reset all alerts — for new sessions."""
259
+ self.fired_alerts.clear()
260
+ self._alert_history.clear()
analysis/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ # Analysis package
analysis/vlm_analyzer.py ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ VLM Frame Analyzer - Analyzes drone surveillance frames using OpenAI GPT-4o-mini.
3
+
4
+ Acts as the AI "vision" component, taking frame descriptions + telemetry
5
+ and producing structured security analysis with object detection,
6
+ event classification, and risk assessment.
7
+ """
8
+
9
+ import json
10
+ from openai import OpenAI
11
+ from config import OPENAI_API_KEY, OPENAI_MODEL
12
+
13
+
14
+ # Configure OpenAI client
15
+ client = OpenAI(api_key=OPENAI_API_KEY)
16
+
17
+
18
+ ANALYSIS_SYSTEM_PROMPT = """You are an expert security analyst monitoring drone surveillance footage for a secure industrial complex.
19
+
20
+ For each frame description provided, analyze the scene and produce a structured security assessment.
21
+
22
+ Your analysis MUST be a valid JSON object with exactly these fields:
23
+ {
24
+ "objects": ["list of detected objects/people/vehicles"],
25
+ "event_type": "one of: vehicle_entry, vehicle_exit, person_detected, delivery, employee_activity, maintenance, wildlife, security_patrol, suspicious_activity, perimeter_breach, unauthorized_access, parking_violation, door_anomaly, normal_operations, theft_attempt",
26
+ "risk_level": "one of: low, medium, high, critical",
27
+ "is_suspicious": true/false,
28
+ "description": "Brief security assessment of the scene (1-2 sentences)",
29
+ "recommended_action": "What action should be taken (e.g., 'Log and continue', 'Dispatch security', 'Trigger alarm')"
30
+ }
31
+
32
+ Assessment guidelines:
33
+ - Business hours are 06:00-22:00. Activity outside these hours gets elevated scrutiny.
34
+ - Known employee vehicles: White Toyota Camry, Silver Honda Civic, Black BMW X5, Blue Ford F150, Red Chevrolet Silverado
35
+ - Known delivery services: FedEx, UPS
36
+ - Any person without a badge near the perimeter is suspicious.
37
+ - Vehicles without employee stickers warrant attention.
38
+ - Repeated appearances of the same vehicle may indicate surveillance.
39
+ - Ski masks, face coverings (non-medical), and tools near restricted areas are critical threats.
40
+ - Open doors after hours are always reported.
41
+
42
+ CRITICAL: Return ONLY the JSON object. No markdown, no code blocks, no explanation."""
43
+
44
+
45
+ def analyze_frame(frame_description: str, telemetry: dict) -> dict:
46
+ """
47
+ Analyze a single frame using OpenAI GPT-4o-mini.
48
+
49
+ Args:
50
+ frame_description: Text description of what the drone camera sees
51
+ telemetry: Dict with drone telemetry (time, location, altitude, etc.)
52
+
53
+ Returns:
54
+ Structured analysis dict with objects, event_type, risk_level, etc.
55
+ """
56
+ # Build the prompt with context
57
+ prompt = f"""Analyze this drone surveillance frame:
58
+
59
+ TELEMETRY DATA:
60
+ - Time: {telemetry.get('time', 'unknown')}
61
+ - Location: {telemetry.get('location', 'unknown')}
62
+ - Drone ID: {telemetry.get('drone_id', 'unknown')}
63
+ - Altitude: {telemetry.get('altitude_m', 'unknown')}m
64
+ - Property: {telemetry.get('property', 'unknown')}
65
+
66
+ FRAME DESCRIPTION:
67
+ {frame_description}
68
+
69
+ Provide your security analysis as a JSON object."""
70
+
71
+ try:
72
+ response = client.chat.completions.create(
73
+ model=OPENAI_MODEL,
74
+ messages=[
75
+ {"role": "system", "content": ANALYSIS_SYSTEM_PROMPT},
76
+ {"role": "user", "content": prompt},
77
+ ],
78
+ temperature=0.1,
79
+ max_tokens=500,
80
+ )
81
+
82
+ response_text = response.choices[0].message.content.strip()
83
+
84
+ # Clean response — remove markdown code blocks if present
85
+ if response_text.startswith("```"):
86
+ lines = response_text.split("\n")
87
+ response_text = "\n".join(lines[1:-1])
88
+ if response_text.startswith("json"):
89
+ response_text = response_text[4:].strip()
90
+
91
+ analysis = json.loads(response_text)
92
+
93
+ # Ensure all required fields exist
94
+ analysis.setdefault("objects", [])
95
+ analysis.setdefault("event_type", "normal_operations")
96
+ analysis.setdefault("risk_level", "low")
97
+ analysis.setdefault("is_suspicious", False)
98
+ analysis.setdefault("description", "No analysis available")
99
+ analysis.setdefault("recommended_action", "Log and continue")
100
+
101
+ return analysis
102
+
103
+ except json.JSONDecodeError:
104
+ # Fallback: try to extract JSON from the response
105
+ return _fallback_parse(response_text, frame_description, telemetry)
106
+ except Exception as e:
107
+ print(f" [VLM Error] {e}")
108
+ return _create_fallback_analysis(frame_description, telemetry)
109
+
110
+
111
+ def _fallback_parse(response_text: str, frame_description: str, telemetry: dict) -> dict:
112
+ """Try harder to extract JSON from a messy response."""
113
+ import re
114
+ json_match = re.search(r'\{[^{}]*\}', response_text, re.DOTALL)
115
+ if json_match:
116
+ try:
117
+ return json.loads(json_match.group())
118
+ except json.JSONDecodeError:
119
+ pass
120
+ return _create_fallback_analysis(frame_description, telemetry)
121
+
122
+
123
+ def _create_fallback_analysis(frame_description: str, telemetry: dict) -> dict:
124
+ """Create a basic analysis when the LLM fails."""
125
+ desc_lower = frame_description.lower()
126
+
127
+ objects = []
128
+ if any(v in desc_lower for v in ["car", "truck", "vehicle", "van", "sedan", "suv"]):
129
+ objects.append("Vehicle")
130
+ if any(p in desc_lower for p in ["person", "people", "man", "woman", "individual", "guard"]):
131
+ objects.append("Person")
132
+
133
+ is_suspicious = any(w in desc_lower for w in [
134
+ "suspicious", "dark clothing", "no badge", "ski mask",
135
+ "unauthorized", "breach", "climbing"
136
+ ])
137
+
138
+ return {
139
+ "objects": objects,
140
+ "event_type": "suspicious_activity" if is_suspicious else "normal_operations",
141
+ "risk_level": "high" if is_suspicious else "low",
142
+ "is_suspicious": is_suspicious,
143
+ "description": f"Fallback analysis: {frame_description[:100]}",
144
+ "recommended_action": "Dispatch security" if is_suspicious else "Log and continue",
145
+ }
app.py ADDED
@@ -0,0 +1,511 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Drone Security Analyst Agent — Streamlit Dashboard
3
+ Full-featured UI with Live Feed simulation, Video Analysis, Alerts, Query, and Summary tabs.
4
+ """
5
+
6
+ import streamlit as st
7
+ import sys
8
+ import os
9
+ import json
10
+ import time
11
+ import cv2
12
+ import numpy as np
13
+ from PIL import Image
14
+ from datetime import datetime
15
+
16
+ os.environ["PYTHONIOENCODING"] = "utf-8"
17
+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
18
+
19
+ from config import PROPERTY_NAME, DRONE_ID, ensure_data_dir
20
+ from simulators.frames import get_simulated_frames
21
+ from simulators.telemetry import TelemetrySimulator
22
+ from analysis.vlm_analyzer import analyze_frame
23
+ from agent.security_agent import SecurityAgent
24
+ from indexing.sqlite_store import SQLiteStore
25
+ from indexing.chroma_store import ChromaStore
26
+ from alerts.rules_engine import AlertManager
27
+
28
+ # --- Page Config ---
29
+ st.set_page_config(
30
+ page_title="Drone Security Analyst Agent",
31
+ page_icon="🛡️",
32
+ layout="wide",
33
+ initial_sidebar_state="expanded",
34
+ )
35
+
36
+ # --- Custom CSS ---
37
+ st.markdown("""
38
+ <style>
39
+ @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap');
40
+
41
+ html, body, [class*="css"] { font-family: 'Inter', sans-serif; }
42
+
43
+ .main .block-container { padding-top: 1.5rem; max-width: 1400px; }
44
+
45
+ div[data-testid="stMetric"] {
46
+ background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
47
+ border: 1px solid #0f3460;
48
+ border-radius: 12px;
49
+ padding: 16px 20px;
50
+ box-shadow: 0 4px 15px rgba(0,0,0,0.3);
51
+ }
52
+ div[data-testid="stMetric"] label { color: #8892b0 !important; font-size: 0.85rem !important; }
53
+ div[data-testid="stMetric"] [data-testid="stMetricValue"] { color: #ccd6f6 !important; font-size: 1.8rem !important; font-weight: 700 !important; }
54
+
55
+ .alert-critical { background: linear-gradient(135deg, #3d0000, #5c1010); border-left: 4px solid #ff4444; padding: 12px 16px; border-radius: 8px; margin: 6px 0; color: #ffcccc; }
56
+ .alert-high { background: linear-gradient(135deg, #3d1c00, #5c3010); border-left: 4px solid #ff8c00; padding: 12px 16px; border-radius: 8px; margin: 6px 0; color: #ffe0b2; }
57
+ .alert-medium { background: linear-gradient(135deg, #3d3d00, #5c5c10); border-left: 4px solid #ffd700; padding: 12px 16px; border-radius: 8px; margin: 6px 0; color: #fff9c4; }
58
+ .alert-low { background: linear-gradient(135deg, #003d1c, #105c30); border-left: 4px solid #00c853; padding: 12px 16px; border-radius: 8px; margin: 6px 0; color: #c8e6c9; }
59
+
60
+ .frame-card { background: linear-gradient(135deg, #0d1b2a, #1b2838); border: 1px solid #233554; border-radius: 10px; padding: 14px 18px; margin: 8px 0; }
61
+ .frame-header { color: #64ffda; font-weight: 600; font-size: 0.95rem; }
62
+ .risk-badge { display: inline-block; padding: 2px 10px; border-radius: 20px; font-size: 0.75rem; font-weight: 700; }
63
+ .risk-low { background: #00c85333; color: #00c853; border: 1px solid #00c853; }
64
+ .risk-medium { background: #ffd70033; color: #ffd700; border: 1px solid #ffd700; }
65
+ .risk-high { background: #ff8c0033; color: #ff8c00; border: 1px solid #ff8c00; }
66
+ .risk-critical { background: #ff444433; color: #ff4444; border: 1px solid #ff4444; }
67
+
68
+ .header-banner { background: linear-gradient(135deg, #0a192f 0%, #112240 50%, #1a365d 100%); border: 1px solid #233554; border-radius: 16px; padding: 24px 32px; margin-bottom: 20px; text-align: center; }
69
+ .header-title { color: #64ffda; font-size: 1.8rem; font-weight: 800; letter-spacing: 2px; margin: 0; }
70
+ .header-sub { color: #8892b0; font-size: 0.9rem; margin-top: 6px; }
71
+
72
+ .stTabs [data-baseweb="tab-list"] { gap: 8px; }
73
+ .stTabs [data-baseweb="tab"] { background-color: #112240; border-radius: 8px 8px 0 0; color: #8892b0; font-weight: 600; padding: 10px 24px; }
74
+ .stTabs [aria-selected="true"] { background-color: #1a365d !important; color: #64ffda !important; border-bottom: 3px solid #64ffda; }
75
+ </style>
76
+ """, unsafe_allow_html=True)
77
+
78
+ # --- Header ---
79
+ st.markdown(f"""
80
+ <div class="header-banner">
81
+ <p class="header-title">🛡️ DRONE SECURITY ANALYST AGENT</p>
82
+ <p class="header-sub">Property: {PROPERTY_NAME} &nbsp;|&nbsp; Drone: {DRONE_ID} &nbsp;|&nbsp; Mode: Autonomous Patrol &nbsp;|&nbsp; AI: GPT-4o-mini</p>
83
+ </div>
84
+ """, unsafe_allow_html=True)
85
+
86
+ # --- Session State Init ---
87
+ if "sim_running" not in st.session_state:
88
+ st.session_state.sim_running = False
89
+ if "sim_results" not in st.session_state:
90
+ st.session_state.sim_results = []
91
+ if "sim_alerts" not in st.session_state:
92
+ st.session_state.sim_alerts = []
93
+ if "frames_done" not in st.session_state:
94
+ st.session_state.frames_done = 0
95
+ if "objects_count" not in st.session_state:
96
+ st.session_state.objects_count = 0
97
+ if "summary_text" not in st.session_state:
98
+ st.session_state.summary_text = ""
99
+
100
+ # --- Helpers ---
101
+ def get_risk_badge(risk):
102
+ risk = risk.upper()
103
+ cls = {"LOW": "risk-low", "MEDIUM": "risk-medium", "HIGH": "risk-high", "CRITICAL": "risk-critical"}.get(risk, "risk-low")
104
+ return f'<span class="risk-badge {cls}">{risk}</span>'
105
+
106
+ def get_alert_class(severity):
107
+ return {"CRITICAL": "alert-critical", "HIGH": "alert-high", "MEDIUM": "alert-medium", "LOW": "alert-low"}.get(severity, "alert-low")
108
+
109
+ # --- Sidebar ---
110
+ with st.sidebar:
111
+ st.markdown("### ⚙️ Controls")
112
+ st.markdown(f"**Time:** {datetime.now().strftime('%H:%M:%S')}")
113
+ st.markdown(f"**Frames Processed:** {st.session_state.frames_done}")
114
+ st.markdown(f"**Alerts Fired:** {len(st.session_state.sim_alerts)}")
115
+ st.divider()
116
+ st.markdown("### 📊 Risk Legend")
117
+ st.markdown("🟢 **LOW** — Normal activity")
118
+ st.markdown("🟡 **MEDIUM** — Monitor on next pass")
119
+ st.markdown("🟠 **HIGH** — Dispatch security")
120
+ st.markdown("🔴 **CRITICAL** — Immediate action")
121
+ st.divider()
122
+ st.markdown("### 🔧 Architecture")
123
+ st.markdown("- **VLM**: GPT-4o-mini")
124
+ st.markdown("- **Agent**: LangGraph ReAct")
125
+ st.markdown("- **Storage**: SQLite + ChromaDB")
126
+ st.markdown("- **Detection**: YOLOv8 + DeepSORT")
127
+
128
+ # --- Tabs ---
129
+ tab1, tab2, tab3, tab4, tab5 = st.tabs(["🎬 Live Feed", "📹 Video Analysis", "🚨 Alerts", "🔍 Query Frames", "📋 Daily Summary"])
130
+
131
+ # ===================== TAB 1: LIVE FEED =====================
132
+ with tab1:
133
+ c1, c2, c3, c4 = st.columns(4)
134
+ c1.metric("Frames Processed", st.session_state.frames_done)
135
+ c2.metric("Objects Detected", st.session_state.objects_count)
136
+ c3.metric("Alerts Fired", len(st.session_state.sim_alerts))
137
+ critical_count = sum(1 for a in st.session_state.sim_alerts if a.get("severity") == "CRITICAL")
138
+ c4.metric("Critical Alerts", critical_count)
139
+
140
+ st.markdown("---")
141
+
142
+ col_btn, col_status = st.columns([1, 3])
143
+ with col_btn:
144
+ run_btn = st.button("▶️ Run Simulation", type="primary", use_container_width=True, key="run_sim")
145
+ with col_status:
146
+ status_area = st.empty()
147
+
148
+ log_area = st.container()
149
+
150
+ if run_btn and not st.session_state.sim_running:
151
+ st.session_state.sim_running = True
152
+ st.session_state.sim_results = []
153
+ st.session_state.sim_alerts = []
154
+ st.session_state.frames_done = 0
155
+ st.session_state.objects_count = 0
156
+
157
+ ensure_data_dir()
158
+ sqlite_store = SQLiteStore()
159
+ chroma_store = ChromaStore()
160
+ alert_manager = AlertManager()
161
+ telemetry_sim = TelemetrySimulator()
162
+ sqlite_store.clear_all()
163
+ try:
164
+ chroma_store.clear_all()
165
+ except Exception:
166
+ pass
167
+
168
+ status_area.info("⏳ Initializing AI Security Agent...")
169
+ agent = SecurityAgent(sqlite_store, chroma_store, alert_manager)
170
+ status_area.success("✅ Agent initialized. Processing frames...")
171
+
172
+ frames = get_simulated_frames()
173
+ progress = st.progress(0, text="Processing frames...")
174
+
175
+ for i, frame in enumerate(frames):
176
+ progress.progress((i + 1) / len(frames), text=f"Processing Frame {i+1}/{len(frames)} — {frame['location']}")
177
+
178
+ telemetry = telemetry_sim.get_telemetry(frame["time"], frame["location"])
179
+ analysis = analyze_frame(frame["description"], telemetry)
180
+ agent_result = agent.process_frame(frame, analysis, telemetry)
181
+
182
+ frame_alerts = [a for a in alert_manager.fired_alerts if a.get("frame_id") == frame["frame_id"]]
183
+ st.session_state.sim_alerts.extend(frame_alerts)
184
+
185
+ risk = analysis.get("risk_level", "low").upper()
186
+ objects = analysis.get("objects", [])
187
+ st.session_state.objects_count += len(objects)
188
+ st.session_state.frames_done = i + 1
189
+
190
+ result_entry = {
191
+ "frame_id": frame["frame_id"],
192
+ "time": frame["time"],
193
+ "location": frame["location"],
194
+ "objects": objects,
195
+ "event_type": analysis.get("event_type", "unknown"),
196
+ "risk_level": risk,
197
+ "description": analysis.get("description", ""),
198
+ "is_suspicious": analysis.get("is_suspicious", False),
199
+ "alerts": frame_alerts,
200
+ "agent_response": agent_result.get("agent_response", ""),
201
+ }
202
+ st.session_state.sim_results.append(result_entry)
203
+
204
+ with log_area:
205
+ risk_icon = {"LOW": "🟢", "MEDIUM": "🟡", "HIGH": "🟠", "CRITICAL": "🔴"}.get(risk, "⚪")
206
+ obj_str = ", ".join(objects) if objects else "—"
207
+
208
+ st.markdown(f"""
209
+ <div class="frame-card">
210
+ <div class="frame-header">{risk_icon} [{frame['time']}] Frame {frame['frame_id']} — {frame['location']} {get_risk_badge(risk)}</div>
211
+ <div style="color:#8892b0;margin-top:6px;font-size:0.85rem;">
212
+ <b>Event:</b> {analysis.get('event_type','—')} &nbsp;|&nbsp; <b>Objects:</b> {obj_str}
213
+ </div>
214
+ <div style="color:#ccd6f6;margin-top:4px;font-size:0.85rem;">{analysis.get('description','')}</div>
215
+ </div>
216
+ """, unsafe_allow_html=True)
217
+
218
+ for alert in frame_alerts:
219
+ acls = get_alert_class(alert["severity"])
220
+ st.markdown(f'<div class="{acls}">🚨 <b>[{alert["severity"]}]</b> {alert["message"]}</div>', unsafe_allow_html=True)
221
+
222
+ if i < len(frames) - 1:
223
+ time.sleep(2)
224
+
225
+ # Generate summary
226
+ progress.progress(1.0, text="Generating AI summary...")
227
+ try:
228
+ st.session_state.summary_text = agent.get_summary()
229
+ except Exception as e:
230
+ st.session_state.summary_text = f"Summary generation error: {e}"
231
+
232
+ sqlite_store.close()
233
+ st.session_state.sim_running = False
234
+ progress.empty()
235
+ status_area.success(f"✅ Simulation complete! {len(frames)} frames processed, {len(st.session_state.sim_alerts)} alerts fired.")
236
+
237
+ elif st.session_state.sim_results:
238
+ for entry in st.session_state.sim_results:
239
+ risk = entry["risk_level"]
240
+ risk_icon = {"LOW": "🟢", "MEDIUM": "🟡", "HIGH": "🟠", "CRITICAL": "🔴"}.get(risk, "⚪")
241
+ obj_str = ", ".join(entry["objects"]) if entry["objects"] else "—"
242
+
243
+ with log_area:
244
+ st.markdown(f"""
245
+ <div class="frame-card">
246
+ <div class="frame-header">{risk_icon} [{entry['time']}] Frame {entry['frame_id']} — {entry['location']} {get_risk_badge(risk)}</div>
247
+ <div style="color:#8892b0;margin-top:6px;font-size:0.85rem;"><b>Event:</b> {entry['event_type']} &nbsp;|&nbsp; <b>Objects:</b> {obj_str}</div>
248
+ <div style="color:#ccd6f6;margin-top:4px;font-size:0.85rem;">{entry['description']}</div>
249
+ </div>
250
+ """, unsafe_allow_html=True)
251
+ for alert in entry.get("alerts", []):
252
+ acls = get_alert_class(alert["severity"])
253
+ st.markdown(f'<div class="{acls}">🚨 <b>[{alert["severity"]}]</b> {alert["message"]}</div>', unsafe_allow_html=True)
254
+
255
+ # ===================== TAB 2: VIDEO ANALYSIS =====================
256
+ with tab2:
257
+ st.markdown("### 📹 Real-Time Video Object Detection")
258
+ st.markdown("Upload drone surveillance footage for **YOLOv8 + DeepSORT** detection and tracking.")
259
+
260
+ uploaded = st.file_uploader("Upload drone video", type=["mp4", "avi", "mov", "mkv"], key="video_upload")
261
+
262
+ col_conf, col_skip = st.columns(2)
263
+ with col_conf:
264
+ confidence = st.slider("Detection Confidence", 0.2, 0.9, 0.4, 0.05)
265
+ with col_skip:
266
+ frame_skip = st.slider("Process every Nth frame", 1, 10, 3, help="Skip frames for faster processing")
267
+
268
+ if uploaded:
269
+ temp_path = os.path.join(os.path.dirname(__file__), "data", "temp_video.mp4")
270
+ ensure_data_dir()
271
+ with open(temp_path, "wb") as f:
272
+ f.write(uploaded.read())
273
+
274
+ from detector import VideoDetector, get_video_info
275
+ info = get_video_info(temp_path)
276
+ st.markdown(f"**Video:** {info.get('width','?')}x{info.get('height','?')} @ {info.get('fps','?')} FPS — {info.get('total_frames','?')} frames ({info.get('duration_sec','?')}s)")
277
+
278
+ if st.button("🚀 Start Detection", type="primary", key="start_detection"):
279
+ detector = VideoDetector(confidence=confidence)
280
+ cap = cv2.VideoCapture(temp_path)
281
+
282
+ col_vid, col_log = st.columns([2, 1])
283
+ video_placeholder = col_vid.empty()
284
+ log_placeholder = col_log.empty()
285
+ metrics_placeholder = st.empty()
286
+ prog = st.progress(0)
287
+
288
+ detection_log = []
289
+ total = info.get("total_frames", 1)
290
+ frame_idx = 0
291
+
292
+ while cap.isOpened():
293
+ ret, frame = cap.read()
294
+ if not ret:
295
+ break
296
+
297
+ frame_idx += 1
298
+ if frame_idx % frame_skip != 0:
299
+ continue
300
+
301
+ result = detector.process_frame(frame)
302
+ annotated = result["annotated_frame"]
303
+
304
+ rgb = cv2.cvtColor(annotated, cv2.COLOR_BGR2RGB)
305
+ video_placeholder.image(rgb, channels="RGB", use_container_width=True)
306
+
307
+ for track in result["active_tracks"]:
308
+ detection_log.insert(0, f"[Frame {frame_idx}] {track['label']} #{track['track_id']}")
309
+
310
+ log_placeholder.text("\n".join(detection_log[:25]))
311
+
312
+ prog.progress(min(frame_idx / total, 1.0), text=f"Frame {frame_idx}/{total}")
313
+
314
+ with metrics_placeholder.container():
315
+ mc1, mc2, mc3 = st.columns(3)
316
+ mc1.metric("Frame", frame_idx)
317
+ mc2.metric("Active Objects", result["total_objects"])
318
+ mc3.metric("Unique Tracks", result["unique_track_count"])
319
+
320
+ cap.release()
321
+ prog.progress(1.0, text="✅ Video analysis complete!")
322
+ st.success(f"Processed {frame_idx} frames. Detected {detector.total_detections} objects across {len(detector.unique_tracks)} unique tracks.")
323
+
324
+ try:
325
+ os.remove(temp_path)
326
+ except Exception:
327
+ pass
328
+ else:
329
+ st.info("👆 Upload a drone surveillance video (.mp4, .avi, .mov) to start real-time detection with bounding boxes and tracking IDs.")
330
+
331
+ # ===================== TAB 3: ALERTS =====================
332
+ with tab3:
333
+ st.markdown("### 🚨 Alert Dashboard")
334
+
335
+ if st.session_state.sim_alerts:
336
+ sev_counts = {}
337
+ for a in st.session_state.sim_alerts:
338
+ s = a.get("severity", "LOW")
339
+ sev_counts[s] = sev_counts.get(s, 0) + 1
340
+
341
+ ac1, ac2, ac3, ac4 = st.columns(4)
342
+ ac1.metric("🔴 Critical", sev_counts.get("CRITICAL", 0))
343
+ ac2.metric("🟠 High", sev_counts.get("HIGH", 0))
344
+ ac3.metric("🟡 Medium", sev_counts.get("MEDIUM", 0))
345
+ ac4.metric("🟢 Low", sev_counts.get("LOW", 0))
346
+
347
+ st.markdown("---")
348
+
349
+ sev_filter = st.multiselect("Filter by Severity", ["CRITICAL", "HIGH", "MEDIUM", "LOW"], default=["CRITICAL", "HIGH", "MEDIUM", "LOW"])
350
+
351
+ for alert in st.session_state.sim_alerts:
352
+ if alert.get("severity") not in sev_filter:
353
+ continue
354
+ acls = get_alert_class(alert["severity"])
355
+ loc = alert.get("location", "—")
356
+ t = alert.get("time", "—")
357
+ st.markdown(f"""
358
+ <div class="{acls}">
359
+ <b>🚨 [{alert['severity']}]</b> &nbsp; <span style="opacity:0.7">Frame {alert.get('frame_id','?')} | {t} | {loc}</span><br>
360
+ {alert['message']}
361
+ </div>
362
+ """, unsafe_allow_html=True)
363
+ else:
364
+ st.info("No alerts yet. Run the simulation in the **Live Feed** tab first.")
365
+
366
+ # ===================== TAB 4: QUERY FRAMES =====================
367
+ with tab4:
368
+ st.markdown("### 🔍 Query Security Events")
369
+
370
+ ensure_data_dir()
371
+ try:
372
+ query_store = SQLiteStore()
373
+ has_data = len(query_store.get_all_frames()) > 0
374
+ except Exception:
375
+ has_data = False
376
+ query_store = None
377
+
378
+ if has_data and query_store:
379
+ qcol1, qcol2 = st.columns([2, 1])
380
+ with qcol1:
381
+ search_query = st.text_input("🔎 Search by object, description, or keyword", placeholder="e.g. truck, person, suspicious")
382
+ with qcol2:
383
+ time_filter = st.selectbox("⏰ Time Filter", ["Any Time", "Business Hours (06:00-22:00)", "After Hours (22:00-06:00)", "Morning (06:00-12:00)", "Afternoon (12:00-18:00)", "Night (18:00-06:00)"])
384
+
385
+ fcol1, fcol2, fcol3 = st.columns(3)
386
+ with fcol1:
387
+ show_suspicious = st.checkbox("🔴 Suspicious only")
388
+ with fcol2:
389
+ location_filter = st.text_input("📍 Location filter", placeholder="e.g. Main Gate, Perimeter")
390
+ with fcol3:
391
+ risk_filter = st.multiselect("Risk Level", ["low", "medium", "high", "critical"], default=["low", "medium", "high", "critical"])
392
+
393
+ if st.button("🔍 Search", type="primary", key="search_btn"):
394
+ results = []
395
+
396
+ if search_query:
397
+ results = query_store.query_by_object(search_query)
398
+ elif show_suspicious:
399
+ results = query_store.query_suspicious_frames()
400
+ elif location_filter:
401
+ results = query_store.query_by_location(location_filter)
402
+ else:
403
+ results = query_store.get_all_frames()
404
+
405
+ # Apply time filter
406
+ if time_filter != "Any Time":
407
+ time_ranges = {
408
+ "Business Hours (06:00-22:00)": ("06:00", "22:00"),
409
+ "After Hours (22:00-06:00)": ("22:00", "06:00"),
410
+ "Morning (06:00-12:00)": ("06:00", "12:00"),
411
+ "Afternoon (12:00-18:00)": ("12:00", "18:00"),
412
+ "Night (18:00-06:00)": ("18:00", "06:00"),
413
+ }
414
+ if time_filter in time_ranges:
415
+ s, e = time_ranges[time_filter]
416
+ time_results = query_store.query_by_time_range(s, e)
417
+ time_ids = {r["frame_id"] for r in time_results}
418
+ results = [r for r in results if r["frame_id"] in time_ids]
419
+
420
+ # Apply risk filter
421
+ results = [r for r in results if r.get("risk_level", "low") in risk_filter]
422
+
423
+ if results:
424
+ st.markdown(f"**Found {len(results)} results**")
425
+ import pandas as pd
426
+ df = pd.DataFrame([{
427
+ "Frame": r["frame_id"],
428
+ "Time": r["timestamp"],
429
+ "Location": r["location"],
430
+ "Event": r.get("event_type", "—"),
431
+ "Risk": r.get("risk_level", "—").upper(),
432
+ "Objects": r.get("objects_detected", "—"),
433
+ "Description": (r.get("analysis_summary") or r.get("raw_description", ""))[:80],
434
+ } for r in results])
435
+ st.dataframe(df, use_container_width=True, hide_index=True)
436
+ else:
437
+ st.warning("No results found for the given filters.")
438
+
439
+ # Semantic search section
440
+ st.markdown("---")
441
+ st.markdown("#### 🧠 Semantic Search (ChromaDB)")
442
+ sem_query = st.text_input("Natural language query", placeholder="e.g. suspicious nighttime activity near the fence")
443
+ if st.button("🧠 Semantic Search", key="sem_search"):
444
+ try:
445
+ chroma = ChromaStore()
446
+ sem_results = chroma.semantic_search(sem_query, top_k=10)
447
+ if sem_results:
448
+ import pandas as pd
449
+ df = pd.DataFrame([{
450
+ "Frame": r.get("frame_id", "?"),
451
+ "Time": r.get("timestamp", "—"),
452
+ "Location": r.get("location", "—"),
453
+ "Relevance": f"{(1 - r.get('distance', 0)):.1%}",
454
+ "Description": r.get("raw_description", r.get("document", ""))[:80],
455
+ } for r in sem_results])
456
+ st.dataframe(df, use_container_width=True, hide_index=True)
457
+ else:
458
+ st.warning("No semantic matches found.")
459
+ except Exception as e:
460
+ st.error(f"Semantic search error: {e}")
461
+
462
+ query_store.close()
463
+ else:
464
+ st.info("No data available. Run the simulation in the **Live Feed** tab first to populate the database.")
465
+ if query_store:
466
+ query_store.close()
467
+
468
+ # ===================== TAB 5: DAILY SUMMARY =====================
469
+ with tab5:
470
+ st.markdown("### 📋 AI Security Analyst Summary")
471
+
472
+ if st.session_state.summary_text:
473
+ st.markdown(f"""
474
+ <div style="background:linear-gradient(135deg,#0d1b2a,#1b2838);border:1px solid #233554;border-radius:12px;padding:24px 28px;color:#ccd6f6;line-height:1.7;font-size:0.92rem;">
475
+ {st.session_state.summary_text.replace(chr(10), '<br>')}
476
+ </div>
477
+ """, unsafe_allow_html=True)
478
+ elif st.session_state.frames_done > 0:
479
+ if st.button("📋 Generate Summary", type="primary"):
480
+ with st.spinner("Generating AI summary..."):
481
+ try:
482
+ sqlite_store = SQLiteStore()
483
+ chroma_store = ChromaStore()
484
+ alert_manager = AlertManager()
485
+ agent = SecurityAgent(sqlite_store, chroma_store, alert_manager)
486
+ st.session_state.summary_text = agent.get_summary()
487
+ sqlite_store.close()
488
+ st.rerun()
489
+ except Exception as e:
490
+ st.error(f"Error: {e}")
491
+ else:
492
+ st.info("Run the simulation first to generate an AI security summary.")
493
+
494
+ # Session stats from DB
495
+ st.markdown("---")
496
+ st.markdown("#### 📊 Database Statistics")
497
+ try:
498
+ s = SQLiteStore()
499
+ stats = s.get_session_stats()
500
+ sc1, sc2, sc3 = st.columns(3)
501
+ sc1.metric("Total Frames in DB", stats["total_frames"])
502
+ sc2.metric("Suspicious Events", stats["suspicious_frames"])
503
+ sc3.metric("Total Alerts in DB", stats["total_alerts"])
504
+
505
+ if stats["alerts_by_severity"]:
506
+ import pandas as pd
507
+ sev_df = pd.DataFrame([{"Severity": k, "Count": v} for k, v in sorted(stats["alerts_by_severity"].items())])
508
+ st.bar_chart(sev_df.set_index("Severity"))
509
+ s.close()
510
+ except Exception:
511
+ st.caption("No database stats available yet.")
config.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Central configuration for the Drone Security Analyst Agent.
3
+ Loads environment variables and defines project-wide constants.
4
+ """
5
+
6
+ import os
7
+ import sys
8
+
9
+ # Fix ChromaDB SQLite version issue on Render/Linux servers
10
+ # ChromaDB requires SQLite >= 3.35.0, but some servers have older versions
11
+ try:
12
+ import pysqlite3
13
+ sys.modules["sqlite3"] = pysqlite3
14
+ except ImportError:
15
+ pass # pysqlite3 not installed (local dev) — use built-in sqlite3
16
+
17
+ from dotenv import load_dotenv
18
+
19
+ load_dotenv()
20
+
21
+ # --- API Configuration ---
22
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
23
+ OPENAI_MODEL = "gpt-4o-mini"
24
+
25
+ # --- Database Configuration ---
26
+ SQLITE_DB_PATH = os.path.join(os.path.dirname(__file__), "data", "security_events.db")
27
+ CHROMA_DB_PATH = os.path.join(os.path.dirname(__file__), "data", "chroma_db")
28
+ CHROMA_COLLECTION_NAME = "drone_frames"
29
+
30
+ # --- Drone Configuration ---
31
+ DRONE_ID = "DRN-01"
32
+ PROPERTY_NAME = "SecureTech Industrial Complex"
33
+
34
+ # --- Alert Severity Levels ---
35
+ SEVERITY_LOW = "LOW"
36
+ SEVERITY_MEDIUM = "MEDIUM"
37
+ SEVERITY_HIGH = "HIGH"
38
+ SEVERITY_CRITICAL = "CRITICAL"
39
+
40
+ # --- Locations ---
41
+ LOCATIONS = [
42
+ "Main Gate",
43
+ "Perimeter Fence North",
44
+ "Perimeter Fence South",
45
+ "Perimeter Fence East",
46
+ "Perimeter Fence West",
47
+ "Parking Lot A",
48
+ "Parking Lot B",
49
+ "Warehouse Entrance",
50
+ "Loading Dock",
51
+ "Admin Building",
52
+ "Server Room Exterior",
53
+ "Emergency Exit Rear",
54
+ ]
55
+
56
+ # --- Known Vehicles (for alert rules) ---
57
+ KNOWN_VEHICLES = [
58
+ "White Toyota Camry",
59
+ "Silver Honda Civic",
60
+ "Black BMW X5",
61
+ "Blue Ford F150",
62
+ "Red Chevrolet Silverado",
63
+ "FedEx Delivery Van",
64
+ "UPS Delivery Truck",
65
+ ]
66
+
67
+ # --- Time Classification ---
68
+ BUSINESS_HOURS_START = 6 # 06:00
69
+ BUSINESS_HOURS_END = 22 # 22:00
70
+
71
+ def ensure_data_dir():
72
+ """Create the data directory if it doesn't exist."""
73
+ data_dir = os.path.join(os.path.dirname(__file__), "data")
74
+ os.makedirs(data_dir, exist_ok=True)
75
+ return data_dir
detector.py ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Video Detector — YOLOv8 + DeepSORT real-time object detection & tracking.
3
+
4
+ Processes drone surveillance video frame-by-frame, detecting and tracking
5
+ objects (people, vehicles, etc.) with persistent IDs across frames.
6
+ """
7
+
8
+ import cv2
9
+ import numpy as np
10
+ from ultralytics import YOLO
11
+ from deep_sort_realtime.deepsort_tracker import DeepSort
12
+
13
+
14
+ # Security-relevant COCO classes
15
+ SECURITY_CLASSES = {
16
+ 0: "person",
17
+ 1: "bicycle",
18
+ 2: "car",
19
+ 3: "motorcycle",
20
+ 5: "bus",
21
+ 7: "truck",
22
+ 14: "bird",
23
+ 15: "cat",
24
+ 16: "dog",
25
+ 24: "backpack",
26
+ 25: "umbrella",
27
+ 26: "handbag",
28
+ 27: "tie",
29
+ 28: "suitcase",
30
+ 63: "laptop",
31
+ 67: "cell phone",
32
+ }
33
+
34
+ # Risk colors for bounding boxes (BGR for OpenCV)
35
+ RISK_COLORS = {
36
+ "person": (0, 140, 255), # Orange
37
+ "car": (255, 180, 0), # Cyan-blue
38
+ "truck": (0, 200, 255), # Yellow-orange
39
+ "motorcycle": (200, 100, 0), # Teal
40
+ "bus": (150, 50, 0), # Dark teal
41
+ "bicycle": (180, 180, 0), # Cyan
42
+ "backpack": (0, 0, 255), # Red
43
+ "suitcase": (0, 0, 200), # Dark red
44
+ "default": (0, 255, 0), # Green
45
+ }
46
+
47
+
48
+ class VideoDetector:
49
+ """YOLOv8 + DeepSORT object detection and tracking for drone footage."""
50
+
51
+ def __init__(self, model_name: str = "yolov8n.pt", confidence: float = 0.4):
52
+ """
53
+ Initialize the detector.
54
+
55
+ Args:
56
+ model_name: YOLOv8 model name (auto-downloads on first use)
57
+ confidence: Minimum detection confidence threshold
58
+ """
59
+ self.model = YOLO(model_name)
60
+ self.tracker = DeepSort(
61
+ max_age=30,
62
+ n_init=3,
63
+ max_iou_distance=0.7,
64
+ )
65
+ self.confidence = confidence
66
+ self.frame_count = 0
67
+ self.total_detections = 0
68
+ self.unique_tracks = set()
69
+
70
+ def process_frame(self, frame: np.ndarray) -> dict:
71
+ """
72
+ Process a single video frame through YOLO + DeepSORT.
73
+
74
+ Args:
75
+ frame: OpenCV BGR frame (numpy array)
76
+
77
+ Returns:
78
+ Dict with annotated_frame, detections, tracks, and metadata
79
+ """
80
+ self.frame_count += 1
81
+
82
+ # Run YOLO detection
83
+ results = self.model(frame, verbose=False)[0]
84
+ detections = []
85
+ raw_detections = []
86
+
87
+ for box in results.boxes:
88
+ x1, y1, x2, y2 = map(int, box.xyxy[0])
89
+ conf = float(box.conf[0])
90
+ cls = int(box.cls[0])
91
+ label = self.model.names.get(cls, f"class_{cls}")
92
+
93
+ if conf >= self.confidence and cls in SECURITY_CLASSES:
94
+ # DeepSORT expects [x, y, w, h]
95
+ detections.append(([x1, y1, x2 - x1, y2 - y1], conf, label))
96
+ raw_detections.append({
97
+ "label": label,
98
+ "confidence": round(conf, 2),
99
+ "bbox": [x1, y1, x2, y2],
100
+ })
101
+
102
+ self.total_detections += len(detections)
103
+
104
+ # Update DeepSORT tracker
105
+ tracks = self.tracker.update_tracks(detections, frame=frame)
106
+
107
+ # Draw annotations on frame
108
+ annotated = frame.copy()
109
+ active_tracks = []
110
+
111
+ for track in tracks:
112
+ if not track.is_confirmed():
113
+ continue
114
+
115
+ tid = track.track_id
116
+ label = track.get_det_class() or "unknown"
117
+ x1, y1, x2, y2 = map(int, track.to_ltrb())
118
+
119
+ self.unique_tracks.add(tid)
120
+
121
+ # Get color based on object type
122
+ color = RISK_COLORS.get(label, RISK_COLORS["default"])
123
+
124
+ # Draw bounding box
125
+ cv2.rectangle(annotated, (x1, y1), (x2, y2), color, 2)
126
+
127
+ # Draw label background
128
+ text = f"{label} #{tid}"
129
+ (tw, th), _ = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 2)
130
+ cv2.rectangle(annotated, (x1, y1 - th - 10), (x1 + tw + 4, y1), color, -1)
131
+ cv2.putText(annotated, text, (x1 + 2, y1 - 5),
132
+ cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
133
+
134
+ active_tracks.append({
135
+ "track_id": tid,
136
+ "label": label,
137
+ "bbox": [x1, y1, x2, y2],
138
+ })
139
+
140
+ # Draw frame info overlay
141
+ info_text = f"Frame: {self.frame_count} | Objects: {len(active_tracks)} | Tracks: {len(self.unique_tracks)}"
142
+ cv2.putText(annotated, info_text, (10, 30),
143
+ cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
144
+
145
+ return {
146
+ "annotated_frame": annotated,
147
+ "frame_id": self.frame_count,
148
+ "raw_detections": raw_detections,
149
+ "active_tracks": active_tracks,
150
+ "total_objects": len(active_tracks),
151
+ "unique_track_count": len(self.unique_tracks),
152
+ }
153
+
154
+ def get_detection_summary(self, tracks: list) -> str:
155
+ """Generate a text summary of current detections for the LLM agent."""
156
+ if not tracks:
157
+ return "No objects detected in current frame."
158
+
159
+ objects = {}
160
+ for t in tracks:
161
+ label = t["label"]
162
+ objects[label] = objects.get(label, 0) + 1
163
+
164
+ parts = [f"{count} {label}{'s' if count > 1 else ''}" for label, count in objects.items()]
165
+ return f"Detected: {', '.join(parts)}"
166
+
167
+ def reset(self):
168
+ """Reset tracker state for a new video."""
169
+ self.tracker = DeepSort(max_age=30, n_init=3, max_iou_distance=0.7)
170
+ self.frame_count = 0
171
+ self.total_detections = 0
172
+ self.unique_tracks = set()
173
+
174
+
175
+ def get_video_info(video_path: str) -> dict:
176
+ """Get basic video metadata."""
177
+ cap = cv2.VideoCapture(video_path)
178
+ if not cap.isOpened():
179
+ return {"error": "Cannot open video file"}
180
+
181
+ info = {
182
+ "width": int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
183
+ "height": int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)),
184
+ "fps": cap.get(cv2.CAP_PROP_FPS),
185
+ "total_frames": int(cap.get(cv2.CAP_PROP_FRAME_COUNT)),
186
+ "duration_sec": int(cap.get(cv2.CAP_PROP_FRAME_COUNT) / max(cap.get(cv2.CAP_PROP_FPS), 1)),
187
+ }
188
+ cap.release()
189
+ return info
docs/.gitkeep ADDED
@@ -0,0 +1 @@
 
 
1
+ # Docs directory
docs/architecture.md ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Architecture Document: Drone Security Analyst Agent
2
+
3
+ ## System Architecture
4
+
5
+ ```
6
+ ┌─────────────────────────────────────────────────────────────────┐
7
+ │ DRONE SECURITY ANALYST AGENT │
8
+ ├─────────────────────────────────────────────────────────────────┤
9
+ │ │
10
+ │ ┌──────────────┐ ┌──────────────┐ │
11
+ │ │ Telemetry │ │ Frame │ │
12
+ │ │ Simulator │ │ Simulator │ │
13
+ │ │ (telemetry.py│ │ (frames.py) │ │
14
+ │ └──────┬───────┘ └──────┬───────┘ │
15
+ │ │ │ │
16
+ │ └────────┬───────────┘ │
17
+ │ ▼ │
18
+ │ ┌──────────────────────────┐ │
19
+ │ │ VLM Frame Analyzer │ ◄── Google Gemini 2.0 Flash │
20
+ │ │ (vlm_analyzer.py) │ │
21
+ │ └──────────┬───────────────┘ │
22
+ │ ▼ │
23
+ │ ┌──────────────────────────┐ │
24
+ │ │ LangChain Agent │ ◄── ConversationBufferMemory │
25
+ │ │ (security_agent.py) │ │
26
+ │ │ ┌────────────────────┐ │ │
27
+ │ │ │ Tools: │ │ │
28
+ │ │ │ • log_event │ │ │
29
+ │ │ │ • check_alert_rules│ │ │
30
+ │ │ │ • query_past_events│ │ │
31
+ │ │ │ • generate_summary │ │ │
32
+ │ │ └────────────────────┘ │ │
33
+ │ └──────┬──────────┬────────┘ │
34
+ │ │ │ │
35
+ │ ┌────▼────┐ ┌───▼──────────┐ ┌──────────────┐ │
36
+ │ │ SQLite │ │ ChromaDB │ │ Alert Rules │ │
37
+ │ │ Store │ │ Store │ │ Engine │ │
38
+ │ │(struct) │ │ (semantic) │ │(rules_engine)│ │
39
+ │ └─────────┘ └──────────────┘ └──────────────┘ │
40
+ │ │
41
+ │ ┌──────────────────────────┐ │
42
+ │ │ Query CLI (query.py) │ │
43
+ │ └──────────────────────────┘ │
44
+ └─────────────────────────────────────────────────────────────────┘
45
+ ```
46
+
47
+ ## Component Details
48
+
49
+ ### 1. Simulators
50
+ - **Telemetry Simulator**: Generates GPS, altitude, battery, wind data per frame
51
+ - **Frame Simulator**: 30 diverse text descriptions covering 24hr patrol cycle
52
+
53
+ ### 2. VLM Frame Analyzer
54
+ - Uses Google Gemini 2.0 Flash for frame analysis
55
+ - Security-focused system prompt extracts objects, event types, risk levels
56
+ - Structured JSON output with fallback parsing
57
+
58
+ ### 3. LangChain Agent
59
+ - AgentExecutor with tool-calling capability
60
+ - ConversationBufferMemory for cross-frame pattern recognition
61
+ - 4 custom tools for logging, alerts, queries, and summaries
62
+
63
+ ### 4. Dual Indexing (Cross-Domain Innovation)
64
+ - **SQLite**: Structured queries by object, time, location
65
+ - **ChromaDB**: Semantic vector search for natural language queries
66
+ - Both populated simultaneously for comprehensive coverage
67
+
68
+ ### 5. Alert Rules Engine
69
+ - 8 configurable rules with severity levels
70
+ - Evaluates each frame against all rules
71
+ - Tracks alert history to inform agent decisions
72
+
73
+ ## Design Decisions
74
+
75
+ | Decision | Choice | Rationale |
76
+ |----------|--------|-----------|
77
+ | LLM | Gemini 2.0 Flash | Fast, cost-effective, good JSON output |
78
+ | Agent Framework | LangChain | Industry standard, tool-calling support |
79
+ | Structured DB | SQLite | Built-in Python support, zero setup |
80
+ | Vector DB | ChromaDB | Easy setup, built-in embeddings |
81
+ | Console UX | Rich library | Professional colored output for demos |
82
+ | Memory | ConversationBuffer | Full context for pattern recognition |
docs/feature_spec.md ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Feature Specification: Drone Security Analyst Agent
2
+
3
+ ## Value Proposition
4
+
5
+ The Drone Security Analyst Agent automates 24/7 property monitoring for industrial complexes, reducing reliance on human security guards while generating a searchable, AI-analyzed event history. It combines drone surveillance simulation with AI-powered frame analysis, intelligent alerting, and semantic search capabilities.
6
+
7
+ ---
8
+
9
+ ## Requirement 1: Object Detection & Event Logging
10
+
11
+ **Description**: Detect and log objects (vehicles, people, animals) per frame with timestamp, location, and security classification.
12
+
13
+ **Acceptance Criteria**:
14
+ - Each frame is analyzed by a Vision Language Model (VLM) to identify objects
15
+ - Detected objects are classified by type: vehicle, person, animal, equipment
16
+ - Each event is stored with: frame_id, timestamp, location, objects_detected, event_type, risk_level
17
+ - Events are stored in both structured (SQLite) and semantic (ChromaDB) databases
18
+ - Minimum 30 frames processed per monitoring session
19
+
20
+ ---
21
+
22
+ ## Requirement 2: Real-Time Alert Generation
23
+
24
+ **Description**: Trigger real-time alerts when predefined security rules are violated.
25
+
26
+ **Alert Rules**:
27
+ | Rule | Condition | Severity |
28
+ |------|-----------|----------|
29
+ | After-hours person | Person detected between 22:00–06:00 | HIGH |
30
+ | Perimeter breach | Person near/on perimeter fence + suspicious behavior | CRITICAL |
31
+ | Unauthorized access | Person without badge at restricted area | CRITICAL |
32
+ | Unknown vehicle at night | Unrecognized vehicle after 22:00 | HIGH |
33
+ | Vehicle re-entry | Same vehicle seen 2+ times | MEDIUM |
34
+ | Suspicious behavior | Loitering, photographing, circling | HIGH |
35
+ | Door anomaly | Door open/ajar outside normal hours | MEDIUM |
36
+ | Unauthorized parking | Vehicle in restricted zone | LOW |
37
+
38
+ **Acceptance Criteria**:
39
+ - All 8 rule types are evaluated per frame
40
+ - Alerts include severity level, timestamp, location, and descriptive message
41
+ - Alert history is persisted and queryable
42
+ - No false alerts for normal business-hour activity
43
+
44
+ ---
45
+
46
+ ## Requirement 3: Queryable Event History
47
+
48
+ **Description**: Allow querying of past events by object type, time window, location, or natural language.
49
+
50
+ **Query Types**:
51
+ - `--object "truck"` → keyword search in SQLite
52
+ - `--time "after 22:00"` → time-range filter in SQLite
53
+ - `--search "suspicious nighttime activity"` → semantic search in ChromaDB
54
+ - `--alerts` → all triggered alerts
55
+ - `--suspicious` → all flagged events
56
+ - `--location "Main Gate"` → location-based filter
57
+
58
+ **Acceptance Criteria**:
59
+ - All query types return accurate, formatted results
60
+ - Semantic search finds relevant frames even without exact keyword matches
61
+ - Results displayed in formatted tables via CLI
indexing/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ # Indexing package
indexing/chroma_store.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ChromaDB Store — Semantic vector storage for drone surveillance frames.
3
+
4
+ Enables natural language queries like "show me suspicious nighttime activity"
5
+ by embedding frame descriptions and performing similarity search.
6
+ """
7
+
8
+ import chromadb
9
+ from chromadb.config import Settings
10
+ from config import CHROMA_DB_PATH, CHROMA_COLLECTION_NAME, ensure_data_dir
11
+
12
+
13
+ class ChromaStore:
14
+ """ChromaDB-based semantic search for security frame descriptions."""
15
+
16
+ def __init__(self, db_path: str = None, collection_name: str = None):
17
+ self.db_path = db_path or CHROMA_DB_PATH
18
+ self.collection_name = collection_name or CHROMA_COLLECTION_NAME
19
+ ensure_data_dir()
20
+
21
+ # Initialize ChromaDB with persistent storage
22
+ self.client = chromadb.PersistentClient(path=self.db_path)
23
+
24
+ # Get or create collection — uses ChromaDB's default embedding function
25
+ self.collection = self.client.get_or_create_collection(
26
+ name=self.collection_name,
27
+ metadata={"description": "Drone surveillance frame descriptions for semantic search"}
28
+ )
29
+
30
+ def add_frame(self, frame_id: int, timestamp: str, location: str,
31
+ description: str, event_type: str = "",
32
+ risk_level: str = "", objects_detected: str = "") -> bool:
33
+ """
34
+ Add a frame description to the vector store.
35
+
36
+ Args:
37
+ frame_id: Unique frame identifier
38
+ timestamp: Time in HH:MM format
39
+ location: Patrol location name
40
+ description: Rich text description of the frame
41
+ event_type: Classified event type
42
+ risk_level: Risk classification
43
+ objects_detected: Comma-separated list of detected objects
44
+
45
+ Returns:
46
+ True if added successfully
47
+ """
48
+ try:
49
+ # Build a rich document for embedding
50
+ document = (
51
+ f"Time: {timestamp}. Location: {location}. "
52
+ f"{description} "
53
+ f"Event type: {event_type}. Risk level: {risk_level}. "
54
+ f"Objects: {objects_detected}."
55
+ )
56
+
57
+ self.collection.upsert(
58
+ ids=[str(frame_id)],
59
+ documents=[document],
60
+ metadatas=[{
61
+ "frame_id": frame_id,
62
+ "timestamp": timestamp,
63
+ "location": location,
64
+ "event_type": event_type,
65
+ "risk_level": risk_level,
66
+ "objects_detected": objects_detected,
67
+ "raw_description": description,
68
+ }]
69
+ )
70
+ return True
71
+ except Exception as e:
72
+ print(f"ChromaDB add error: {e}")
73
+ return False
74
+
75
+ def semantic_search(self, query: str, top_k: int = 5) -> list[dict]:
76
+ """
77
+ Perform semantic search across all stored frames.
78
+
79
+ Args:
80
+ query: Natural language query (e.g., "suspicious nighttime activity")
81
+ top_k: Number of results to return
82
+
83
+ Returns:
84
+ List of matching frame dicts with similarity scores
85
+ """
86
+ try:
87
+ results = self.collection.query(
88
+ query_texts=[query],
89
+ n_results=min(top_k, self.collection.count()) if self.collection.count() > 0 else 1,
90
+ )
91
+
92
+ formatted = []
93
+ if results and results["ids"] and results["ids"][0]:
94
+ for i, frame_id in enumerate(results["ids"][0]):
95
+ entry = {
96
+ "frame_id": int(frame_id),
97
+ "document": results["documents"][0][i] if results["documents"] else "",
98
+ "distance": results["distances"][0][i] if results["distances"] else 0,
99
+ }
100
+ if results["metadatas"] and results["metadatas"][0]:
101
+ entry.update(results["metadatas"][0][i])
102
+ formatted.append(entry)
103
+
104
+ return formatted
105
+ except Exception as e:
106
+ print(f"ChromaDB search error: {e}")
107
+ return []
108
+
109
+ def get_similar_frames(self, frame_id: int, top_k: int = 3) -> list[dict]:
110
+ """Find frames similar to a given frame."""
111
+ try:
112
+ # Get the frame's document
113
+ result = self.collection.get(ids=[str(frame_id)])
114
+ if not result or not result["documents"]:
115
+ return []
116
+
117
+ doc = result["documents"][0]
118
+ # Search for similar, excluding self
119
+ results = self.semantic_search(doc, top_k=top_k + 1)
120
+ return [r for r in results if r["frame_id"] != frame_id][:top_k]
121
+ except Exception as e:
122
+ print(f"ChromaDB similarity error: {e}")
123
+ return []
124
+
125
+ def get_frame_count(self) -> int:
126
+ """Get the total number of indexed frames."""
127
+ return self.collection.count()
128
+
129
+ def clear_all(self):
130
+ """Delete and recreate the collection — for testing."""
131
+ self.client.delete_collection(self.collection_name)
132
+ self.collection = self.client.get_or_create_collection(
133
+ name=self.collection_name,
134
+ metadata={"description": "Drone surveillance frame descriptions for semantic search"}
135
+ )
136
+
137
+ def __enter__(self):
138
+ return self
139
+
140
+ def __exit__(self, exc_type, exc_val, exc_tb):
141
+ pass # ChromaDB PersistentClient handles cleanup
indexing/sqlite_store.py ADDED
@@ -0,0 +1,250 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ SQLite Store — Structured storage for drone security events and alerts.
3
+
4
+ Provides relational database storage with fast querying by object type,
5
+ time range, location, and event type.
6
+ """
7
+
8
+ import sqlite3
9
+ import json
10
+ import os
11
+ from config import SQLITE_DB_PATH, ensure_data_dir
12
+
13
+
14
+ class SQLiteStore:
15
+ """SQLite-based structured storage for security events and alerts."""
16
+
17
+ def __init__(self, db_path: str = None):
18
+ self.db_path = db_path or SQLITE_DB_PATH
19
+ ensure_data_dir()
20
+ self.conn = sqlite3.connect(self.db_path, check_same_thread=False)
21
+ self.conn.row_factory = sqlite3.Row
22
+ self._create_tables()
23
+
24
+ def _create_tables(self):
25
+ """Create the frames and alerts tables if they don't exist."""
26
+ cursor = self.conn.cursor()
27
+
28
+ cursor.execute("""
29
+ CREATE TABLE IF NOT EXISTS frames (
30
+ frame_id INTEGER PRIMARY KEY,
31
+ timestamp TEXT NOT NULL,
32
+ location TEXT NOT NULL,
33
+ objects_detected TEXT,
34
+ event_type TEXT,
35
+ risk_level TEXT,
36
+ is_suspicious INTEGER DEFAULT 0,
37
+ raw_description TEXT,
38
+ analysis_summary TEXT,
39
+ agent_response TEXT,
40
+ created_at DATETIME DEFAULT CURRENT_TIMESTAMP
41
+ )
42
+ """)
43
+
44
+ cursor.execute("""
45
+ CREATE TABLE IF NOT EXISTS alerts (
46
+ alert_id INTEGER PRIMARY KEY AUTOINCREMENT,
47
+ frame_id INTEGER,
48
+ timestamp TEXT NOT NULL,
49
+ rule_name TEXT,
50
+ severity TEXT NOT NULL,
51
+ message TEXT NOT NULL,
52
+ location TEXT,
53
+ created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
54
+ FOREIGN KEY (frame_id) REFERENCES frames(frame_id)
55
+ )
56
+ """)
57
+
58
+ cursor.execute("""
59
+ CREATE TABLE IF NOT EXISTS vehicle_log (
60
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
61
+ vehicle_description TEXT NOT NULL,
62
+ frame_id INTEGER,
63
+ timestamp TEXT NOT NULL,
64
+ location TEXT,
65
+ entry_count INTEGER DEFAULT 1,
66
+ FOREIGN KEY (frame_id) REFERENCES frames(frame_id)
67
+ )
68
+ """)
69
+
70
+ self.conn.commit()
71
+
72
+ def insert_frame(self, frame_id: int, timestamp: str, location: str,
73
+ objects_detected: list, event_type: str, risk_level: str,
74
+ is_suspicious: bool, raw_description: str,
75
+ analysis_summary: str = "", agent_response: str = "") -> bool:
76
+ """
77
+ Insert a processed frame into the database.
78
+
79
+ Returns:
80
+ True if inserted, False if frame_id already exists
81
+ """
82
+ try:
83
+ cursor = self.conn.cursor()
84
+ cursor.execute("""
85
+ INSERT OR REPLACE INTO frames
86
+ (frame_id, timestamp, location, objects_detected, event_type,
87
+ risk_level, is_suspicious, raw_description, analysis_summary, agent_response)
88
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
89
+ """, (
90
+ frame_id, timestamp, location,
91
+ json.dumps(objects_detected) if isinstance(objects_detected, list) else objects_detected,
92
+ event_type, risk_level,
93
+ 1 if is_suspicious else 0,
94
+ raw_description, analysis_summary, agent_response
95
+ ))
96
+ self.conn.commit()
97
+ return True
98
+ except sqlite3.Error as e:
99
+ print(f"SQLite insert error: {e}")
100
+ return False
101
+
102
+ def insert_alert(self, frame_id: int, timestamp: str, rule_name: str,
103
+ severity: str, message: str, location: str = "") -> int:
104
+ """
105
+ Insert an alert and return its ID.
106
+ """
107
+ cursor = self.conn.cursor()
108
+ cursor.execute("""
109
+ INSERT INTO alerts (frame_id, timestamp, rule_name, severity, message, location)
110
+ VALUES (?, ?, ?, ?, ?, ?)
111
+ """, (frame_id, timestamp, rule_name, severity, message, location))
112
+ self.conn.commit()
113
+ return cursor.lastrowid
114
+
115
+ def log_vehicle(self, vehicle_description: str, frame_id: int,
116
+ timestamp: str, location: str) -> int:
117
+ """Log a vehicle sighting and return the entry count for this vehicle."""
118
+ cursor = self.conn.cursor()
119
+
120
+ # Count previous sightings
121
+ cursor.execute(
122
+ "SELECT COUNT(*) FROM vehicle_log WHERE vehicle_description = ?",
123
+ (vehicle_description,)
124
+ )
125
+ count = cursor.fetchone()[0] + 1
126
+
127
+ cursor.execute("""
128
+ INSERT INTO vehicle_log (vehicle_description, frame_id, timestamp, location, entry_count)
129
+ VALUES (?, ?, ?, ?, ?)
130
+ """, (vehicle_description, frame_id, timestamp, location, count))
131
+ self.conn.commit()
132
+ return count
133
+
134
+ def query_by_object(self, object_type: str) -> list[dict]:
135
+ """Search frames where objects_detected contains the given object type."""
136
+ cursor = self.conn.cursor()
137
+ cursor.execute(
138
+ "SELECT * FROM frames WHERE objects_detected LIKE ?",
139
+ (f"%{object_type}%",)
140
+ )
141
+ return [dict(row) for row in cursor.fetchall()]
142
+
143
+ def query_by_time_range(self, start_time: str, end_time: str) -> list[dict]:
144
+ """Get frames within a time range (HH:MM format)."""
145
+ cursor = self.conn.cursor()
146
+
147
+ start_minutes = self._time_to_minutes(start_time)
148
+ end_minutes = self._time_to_minutes(end_time)
149
+
150
+ # Get all frames and filter in Python for overnight ranges
151
+ cursor.execute("SELECT * FROM frames")
152
+ all_frames = [dict(row) for row in cursor.fetchall()]
153
+
154
+ if end_minutes < start_minutes:
155
+ # Overnight range (e.g., 22:00 to 02:00)
156
+ return [f for f in all_frames
157
+ if self._time_to_minutes(f["timestamp"]) >= start_minutes
158
+ or self._time_to_minutes(f["timestamp"]) <= end_minutes]
159
+ else:
160
+ return [f for f in all_frames
161
+ if start_minutes <= self._time_to_minutes(f["timestamp"]) <= end_minutes]
162
+
163
+ def query_by_location(self, location: str) -> list[dict]:
164
+ """Search frames by location."""
165
+ cursor = self.conn.cursor()
166
+ cursor.execute(
167
+ "SELECT * FROM frames WHERE location LIKE ?",
168
+ (f"%{location}%",)
169
+ )
170
+ return [dict(row) for row in cursor.fetchall()]
171
+
172
+ def query_suspicious_frames(self) -> list[dict]:
173
+ """Get all frames flagged as suspicious."""
174
+ cursor = self.conn.cursor()
175
+ cursor.execute("SELECT * FROM frames WHERE is_suspicious = 1")
176
+ return [dict(row) for row in cursor.fetchall()]
177
+
178
+ def get_all_alerts(self) -> list[dict]:
179
+ """Get all triggered alerts."""
180
+ cursor = self.conn.cursor()
181
+ cursor.execute("SELECT * FROM alerts ORDER BY created_at")
182
+ return [dict(row) for row in cursor.fetchall()]
183
+
184
+ def get_alerts_by_severity(self, severity: str) -> list[dict]:
185
+ """Get alerts filtered by severity level."""
186
+ cursor = self.conn.cursor()
187
+ cursor.execute("SELECT * FROM alerts WHERE severity = ?", (severity,))
188
+ return [dict(row) for row in cursor.fetchall()]
189
+
190
+ def get_vehicle_count(self, vehicle_description: str) -> int:
191
+ """Get the number of times a vehicle has been seen."""
192
+ cursor = self.conn.cursor()
193
+ cursor.execute(
194
+ "SELECT COUNT(*) FROM vehicle_log WHERE vehicle_description = ?",
195
+ (vehicle_description,)
196
+ )
197
+ return cursor.fetchone()[0]
198
+
199
+ def get_all_frames(self) -> list[dict]:
200
+ """Get all stored frames."""
201
+ cursor = self.conn.cursor()
202
+ cursor.execute("SELECT * FROM frames ORDER BY frame_id")
203
+ return [dict(row) for row in cursor.fetchall()]
204
+
205
+ def get_session_stats(self) -> dict:
206
+ """Get summary statistics for the current session."""
207
+ cursor = self.conn.cursor()
208
+
209
+ cursor.execute("SELECT COUNT(*) FROM frames")
210
+ total_frames = cursor.fetchone()[0]
211
+
212
+ cursor.execute("SELECT COUNT(*) FROM frames WHERE is_suspicious = 1")
213
+ suspicious = cursor.fetchone()[0]
214
+
215
+ cursor.execute("SELECT COUNT(*) FROM alerts")
216
+ total_alerts = cursor.fetchone()[0]
217
+
218
+ cursor.execute("SELECT severity, COUNT(*) FROM alerts GROUP BY severity")
219
+ alerts_by_severity = {row[0]: row[1] for row in cursor.fetchall()}
220
+
221
+ return {
222
+ "total_frames": total_frames,
223
+ "suspicious_frames": suspicious,
224
+ "total_alerts": total_alerts,
225
+ "alerts_by_severity": alerts_by_severity,
226
+ }
227
+
228
+ def clear_all(self):
229
+ """Clear all data — used for testing and fresh sessions."""
230
+ cursor = self.conn.cursor()
231
+ cursor.execute("DELETE FROM alerts")
232
+ cursor.execute("DELETE FROM vehicle_log")
233
+ cursor.execute("DELETE FROM frames")
234
+ self.conn.commit()
235
+
236
+ def close(self):
237
+ """Close the database connection."""
238
+ self.conn.close()
239
+
240
+ @staticmethod
241
+ def _time_to_minutes(time_str: str) -> int:
242
+ """Convert HH:MM to total minutes."""
243
+ parts = time_str.split(":")
244
+ return int(parts[0]) * 60 + int(parts[1])
245
+
246
+ def __enter__(self):
247
+ return self
248
+
249
+ def __exit__(self, exc_type, exc_val, exc_tb):
250
+ self.close()
main.py ADDED
@@ -0,0 +1,256 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Drone Security Analyst Agent — Main Pipeline
3
+
4
+ Processes all simulated drone surveillance frames through:
5
+ 1. Telemetry generation
6
+ 2. VLM frame analysis (Gemini)
7
+ 3. LangChain agent reasoning (log, alert, pattern detection)
8
+ 4. Dual indexing (SQLite + ChromaDB)
9
+
10
+ Produces a professional security monitoring console output.
11
+ """
12
+
13
+ import sys
14
+ import time
15
+ import os
16
+
17
+ # Fix Windows encoding
18
+ os.environ["PYTHONIOENCODING"] = "utf-8"
19
+
20
+ # Add project root to path
21
+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
22
+
23
+ from rich.console import Console
24
+ from rich.panel import Panel
25
+ from rich.table import Table
26
+ from rich.text import Text
27
+ from rich.progress import Progress, SpinnerColumn, TextColumn
28
+ from rich import box
29
+
30
+ from config import PROPERTY_NAME, DRONE_ID, ensure_data_dir
31
+ from simulators.frames import get_simulated_frames
32
+ from simulators.telemetry import TelemetrySimulator
33
+ from analysis.vlm_analyzer import analyze_frame
34
+ from agent.security_agent import SecurityAgent
35
+ from indexing.sqlite_store import SQLiteStore
36
+ from indexing.chroma_store import ChromaStore
37
+ from alerts.rules_engine import AlertManager
38
+
39
+ console = Console(force_terminal=True)
40
+
41
+
42
+ def print_header():
43
+ """Print the monitoring session header."""
44
+ header = Text()
45
+ header.append("DRONE SECURITY ANALYST AGENT", style="bold white")
46
+ header.append("\n")
47
+ header.append("\n")
48
+ header.append(f"Property: {PROPERTY_NAME}", style="cyan")
49
+ header.append(f" | Drone: {DRONE_ID}", style="cyan")
50
+ header.append(f" | Mode: Autonomous Patrol", style="cyan")
51
+
52
+ console.print(Panel(
53
+ header,
54
+ title="[bold green]>> MONITORING SESSION STARTED[/bold green]",
55
+ border_style="green",
56
+ box=box.DOUBLE_EDGE,
57
+ padding=(1, 2),
58
+ ))
59
+ console.print()
60
+
61
+
62
+ def print_frame_result(frame: dict, analysis: dict, agent_result: dict, alerts: list):
63
+ """Print the result of processing a single frame."""
64
+ time_str = frame["time"]
65
+ location = frame["location"]
66
+ risk = analysis.get("risk_level", "low").upper()
67
+
68
+ # Color based on risk
69
+ risk_colors = {
70
+ "LOW": "green",
71
+ "MEDIUM": "yellow",
72
+ "HIGH": "red",
73
+ "CRITICAL": "bold red",
74
+ }
75
+ risk_color = risk_colors.get(risk, "white")
76
+
77
+ # Status icon
78
+ if risk in ("CRITICAL", "HIGH"):
79
+ icon = "[!!]"
80
+ elif risk == "MEDIUM":
81
+ icon = "[!]"
82
+ else:
83
+ icon = "[+]"
84
+
85
+ # Main log line
86
+ objects_str = ", ".join(analysis.get("objects", ["-"]))
87
+ event_type = analysis.get("event_type", "unknown")
88
+ description = analysis.get("description", "")
89
+
90
+ console.print(
91
+ f" [{risk_color}][{time_str}][/{risk_color}] "
92
+ f"{icon} "
93
+ f"[bold]{event_type.upper()}[/bold] at [cyan]{location}[/cyan] - "
94
+ f"{objects_str}"
95
+ )
96
+
97
+ if description:
98
+ console.print(f" > {description}", style="dim")
99
+
100
+ # Print alerts
101
+ for alert in alerts:
102
+ sev = alert.get("severity", "")
103
+ msg = alert.get("message", "")
104
+ sev_color = risk_colors.get(sev, "white")
105
+ console.print(f" [bold {sev_color}]ALERT [{sev}]: {msg}[/bold {sev_color}]")
106
+
107
+ console.print()
108
+
109
+
110
+ def print_session_summary(sqlite_store: SQLiteStore, alert_manager: AlertManager):
111
+ """Print the end-of-session summary."""
112
+ stats = sqlite_store.get_session_stats()
113
+ all_alerts = sqlite_store.get_all_alerts()
114
+
115
+ console.print()
116
+ console.print(Panel(
117
+ "[bold white]SESSION COMPLETE[/bold white]",
118
+ title="[bold cyan]>> MONITORING SESSION ENDED[/bold cyan]",
119
+ border_style="cyan",
120
+ box=box.DOUBLE_EDGE,
121
+ ))
122
+
123
+ # Stats table
124
+ stats_table = Table(
125
+ title="Session Statistics",
126
+ box=box.ROUNDED,
127
+ show_header=True,
128
+ header_style="bold cyan",
129
+ )
130
+ stats_table.add_column("Metric", style="bold")
131
+ stats_table.add_column("Value", justify="right")
132
+
133
+ stats_table.add_row("Total Frames Processed", str(stats["total_frames"]))
134
+ stats_table.add_row("Suspicious Events", f"[red]{stats['suspicious_frames']}[/red]")
135
+ stats_table.add_row("Total Alerts Fired", f"[yellow]{stats['total_alerts']}[/yellow]")
136
+
137
+ for sev, count in sorted(stats.get("alerts_by_severity", {}).items()):
138
+ color = {"CRITICAL": "bold red", "HIGH": "red", "MEDIUM": "yellow", "LOW": "green"}.get(sev, "white")
139
+ stats_table.add_row(f" > {sev} Alerts", f"[{color}]{count}[/{color}]")
140
+
141
+ console.print(stats_table)
142
+
143
+ # Alert details
144
+ if all_alerts:
145
+ console.print()
146
+ alert_table = Table(
147
+ title="Alert Log",
148
+ box=box.ROUNDED,
149
+ show_header=True,
150
+ header_style="bold red",
151
+ )
152
+ alert_table.add_column("Time", style="cyan")
153
+ alert_table.add_column("Severity", justify="center")
154
+ alert_table.add_column("Location")
155
+ alert_table.add_column("Message", max_width=60)
156
+
157
+ for alert in all_alerts:
158
+ sev = alert["severity"]
159
+ color = {"CRITICAL": "bold red", "HIGH": "red", "MEDIUM": "yellow", "LOW": "green"}.get(sev, "white")
160
+ alert_table.add_row(
161
+ alert["timestamp"],
162
+ f"[{color}]{sev}[/{color}]",
163
+ alert.get("location", "-"),
164
+ alert["message"][:60],
165
+ )
166
+
167
+ console.print(alert_table)
168
+
169
+
170
+ def main():
171
+ """Run the main monitoring pipeline."""
172
+ ensure_data_dir()
173
+
174
+ # Initialize components
175
+ sqlite_store = SQLiteStore()
176
+ chroma_store = ChromaStore()
177
+ alert_manager = AlertManager()
178
+ telemetry_sim = TelemetrySimulator()
179
+
180
+ # Clear previous session data
181
+ sqlite_store.clear_all()
182
+ try:
183
+ chroma_store.clear_all()
184
+ except Exception:
185
+ pass
186
+
187
+ # Initialize the LangChain agent
188
+ console.print("[dim]Initializing AI Security Agent...[/dim]")
189
+ agent = SecurityAgent(sqlite_store, chroma_store, alert_manager)
190
+ console.print("[green]>> Agent initialized successfully[/green]\n")
191
+
192
+ # Get simulated frames
193
+ frames = get_simulated_frames()
194
+
195
+ # Print header
196
+ print_header()
197
+
198
+ total_alerts = []
199
+
200
+ # Process each frame
201
+ for i, frame in enumerate(frames):
202
+ frame_label = f"Frame {frame['frame_id']}/{len(frames)}"
203
+
204
+ console.print(
205
+ f" [dim]--- {frame_label} ---[/dim]",
206
+ style="dim",
207
+ )
208
+
209
+ # Step 1: Generate telemetry
210
+ telemetry = telemetry_sim.get_telemetry(frame["time"], frame["location"])
211
+
212
+ # Step 2: VLM frame analysis
213
+ console.print(f" [dim] Analyzing frame with VLM...[/dim]")
214
+ analysis = analyze_frame(frame["description"], telemetry)
215
+
216
+ # Step 3: Agent processing
217
+ console.print(f" [dim] Agent processing...[/dim]")
218
+ agent_result = agent.process_frame(frame, analysis, telemetry)
219
+
220
+ # Collect alerts for this frame
221
+ frame_alerts = [a for a in alert_manager.fired_alerts
222
+ if a.get("frame_id") == frame["frame_id"]]
223
+ total_alerts.extend(frame_alerts)
224
+
225
+ # Step 4: Print results
226
+ print_frame_result(frame, analysis, agent_result, frame_alerts)
227
+
228
+ # Delay between frames to respect API rate limits (free tier: 15 RPM)
229
+ if i < len(frames) - 1:
230
+ time.sleep(4)
231
+
232
+ # Print session summary
233
+ print_session_summary(sqlite_store, alert_manager)
234
+
235
+ # Generate agent summary
236
+ console.print("\n[dim]Generating AI session summary...[/dim]")
237
+ try:
238
+ summary = agent.get_summary()
239
+ console.print(Panel(
240
+ summary,
241
+ title="[bold cyan]AI Security Analyst Summary[/bold cyan]",
242
+ border_style="cyan",
243
+ box=box.ROUNDED,
244
+ padding=(1, 2),
245
+ ))
246
+ except Exception as e:
247
+ console.print(f"[yellow]Summary generation skipped: {e}[/yellow]")
248
+
249
+ # Cleanup
250
+ sqlite_store.close()
251
+ console.print("\n[green]>> Session data saved to SQLite + ChromaDB[/green]")
252
+ console.print("[green]>> Run 'python query.py --help' to query events[/green]")
253
+
254
+
255
+ if __name__ == "__main__":
256
+ main()
query.py ADDED
@@ -0,0 +1,362 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Query CLI — Command-line interface for querying drone security events.
3
+
4
+ Supports:
5
+ --object "truck" : Search by object type (SQLite)
6
+ --time "after 22:00" : Search by time window (SQLite)
7
+ --search "suspicious ..." : Semantic search (ChromaDB)
8
+ --alerts : Show all triggered alerts
9
+ --summary : Show session statistics
10
+ --location "Main Gate" : Search by location
11
+ --suspicious : Show all suspicious events
12
+ """
13
+
14
+ import sys
15
+ import os
16
+ import argparse
17
+
18
+ os.environ["PYTHONIOENCODING"] = "utf-8"
19
+
20
+ # Add project root to path
21
+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
22
+
23
+ from rich.console import Console
24
+ from rich.table import Table
25
+ from rich.panel import Panel
26
+ from rich import box
27
+
28
+ from indexing.sqlite_store import SQLiteStore
29
+ from indexing.chroma_store import ChromaStore
30
+ from config import ensure_data_dir
31
+
32
+ console = Console(force_terminal=True)
33
+
34
+
35
+ def show_object_results(store: SQLiteStore, object_type: str):
36
+ """Search frames by object type."""
37
+ results = store.query_by_object(object_type)
38
+
39
+ if not results:
40
+ console.print(f"[yellow]No frames found containing '{object_type}'[/yellow]")
41
+ return
42
+
43
+ table = Table(
44
+ title=f"Frames containing '{object_type}' ({len(results)} results)",
45
+ box=box.ROUNDED,
46
+ show_header=True,
47
+ header_style="bold cyan",
48
+ )
49
+ table.add_column("Frame", justify="center", style="bold")
50
+ table.add_column("Time", style="cyan")
51
+ table.add_column("Location")
52
+ table.add_column("Event Type")
53
+ table.add_column("Risk", justify="center")
54
+ table.add_column("Description", max_width=50)
55
+
56
+ for r in results:
57
+ risk = r.get("risk_level", "low").upper()
58
+ risk_color = {"CRITICAL": "bold red", "HIGH": "red", "MEDIUM": "yellow", "LOW": "green"}.get(risk, "white")
59
+ table.add_row(
60
+ str(r["frame_id"]),
61
+ r["timestamp"],
62
+ r["location"],
63
+ r.get("event_type", "-"),
64
+ f"[{risk_color}]{risk}[/{risk_color}]",
65
+ (r.get("analysis_summary") or r.get("raw_description", "-"))[:50],
66
+ )
67
+
68
+ console.print(table)
69
+
70
+
71
+ def show_time_results(store: SQLiteStore, time_query: str):
72
+ """Search frames by time range."""
73
+ # Parse time query
74
+ time_query = time_query.lower().strip()
75
+
76
+ if "after" in time_query:
77
+ # Extract time after "after"
78
+ time_part = time_query.replace("after", "").strip()
79
+ start_time = time_part
80
+ end_time = "23:59"
81
+ elif "before" in time_query:
82
+ time_part = time_query.replace("before", "").strip()
83
+ start_time = "00:00"
84
+ end_time = time_part
85
+ elif "-" in time_query or "to" in time_query:
86
+ parts = time_query.replace("to", "-").split("-")
87
+ start_time = parts[0].strip()
88
+ end_time = parts[1].strip()
89
+ else:
90
+ # Assume it's a specific time, show +/- 30 min
91
+ start_time = time_query
92
+ end_time = time_query
93
+ console.print(f"[dim]Showing events at {time_query}[/dim]")
94
+
95
+ results = store.query_by_time_range(start_time, end_time)
96
+
97
+ if not results:
98
+ console.print(f"[yellow]No frames found in time range {start_time} - {end_time}[/yellow]")
99
+ return
100
+
101
+ table = Table(
102
+ title=f"Frames from {start_time} to {end_time} ({len(results)} results)",
103
+ box=box.ROUNDED,
104
+ show_header=True,
105
+ header_style="bold cyan",
106
+ )
107
+ table.add_column("Frame", justify="center", style="bold")
108
+ table.add_column("Time", style="cyan")
109
+ table.add_column("Location")
110
+ table.add_column("Event Type")
111
+ table.add_column("Risk", justify="center")
112
+ table.add_column("Description", max_width=50)
113
+
114
+ for r in results:
115
+ risk = r.get("risk_level", "low").upper()
116
+ risk_color = {"CRITICAL": "bold red", "HIGH": "red", "MEDIUM": "yellow", "LOW": "green"}.get(risk, "white")
117
+ table.add_row(
118
+ str(r["frame_id"]),
119
+ r["timestamp"],
120
+ r["location"],
121
+ r.get("event_type", "-"),
122
+ f"[{risk_color}]{risk}[/{risk_color}]",
123
+ (r.get("analysis_summary") or r.get("raw_description", "-"))[:50],
124
+ )
125
+
126
+ console.print(table)
127
+
128
+
129
+ def show_semantic_search(chroma_store: ChromaStore, query: str):
130
+ """Semantic search using ChromaDB."""
131
+ results = chroma_store.semantic_search(query, top_k=10)
132
+
133
+ if not results:
134
+ console.print(f"[yellow]No semantic matches found for '{query}'[/yellow]")
135
+ return
136
+
137
+ table = Table(
138
+ title=f"Semantic Search: '{query}' ({len(results)} results)",
139
+ box=box.ROUNDED,
140
+ show_header=True,
141
+ header_style="bold magenta",
142
+ )
143
+ table.add_column("Frame", justify="center", style="bold")
144
+ table.add_column("Time", style="cyan")
145
+ table.add_column("Location")
146
+ table.add_column("Relevance", justify="center")
147
+ table.add_column("Description", max_width=60)
148
+
149
+ for r in results:
150
+ similarity = 1 - r.get("distance", 0)
151
+ sim_color = "green" if similarity > 0.7 else "yellow" if similarity > 0.4 else "red"
152
+ table.add_row(
153
+ str(r.get("frame_id", "?")),
154
+ r.get("timestamp", "-"),
155
+ r.get("location", "-"),
156
+ f"[{sim_color}]{similarity:.1%}[/{sim_color}]",
157
+ r.get("raw_description", r.get("document", "-"))[:60],
158
+ )
159
+
160
+ console.print(table)
161
+
162
+
163
+ def show_alerts(store: SQLiteStore):
164
+ """Show all triggered alerts."""
165
+ alerts = store.get_all_alerts()
166
+
167
+ if not alerts:
168
+ console.print("[green]No alerts were triggered during this session.[/green]")
169
+ return
170
+
171
+ table = Table(
172
+ title=f"Alert Log ({len(alerts)} total alerts)",
173
+ box=box.ROUNDED,
174
+ show_header=True,
175
+ header_style="bold red",
176
+ )
177
+ table.add_column("ID", justify="center", style="bold")
178
+ table.add_column("Frame", justify="center")
179
+ table.add_column("Time", style="cyan")
180
+ table.add_column("Severity", justify="center")
181
+ table.add_column("Rule")
182
+ table.add_column("Location")
183
+ table.add_column("Message", max_width=50)
184
+
185
+ for alert in alerts:
186
+ sev = alert["severity"]
187
+ color = {"CRITICAL": "bold red", "HIGH": "red", "MEDIUM": "yellow", "LOW": "green"}.get(sev, "white")
188
+ table.add_row(
189
+ str(alert["alert_id"]),
190
+ str(alert.get("frame_id", "-")),
191
+ alert["timestamp"],
192
+ f"[{color}]{sev}[/{color}]",
193
+ alert.get("rule_name", "-"),
194
+ alert.get("location", "-"),
195
+ alert["message"][:50],
196
+ )
197
+
198
+ console.print(table)
199
+
200
+
201
+ def show_summary(store: SQLiteStore):
202
+ """Show session statistics."""
203
+ stats = store.get_session_stats()
204
+
205
+ console.print(Panel(
206
+ f"""[bold]Session Statistics[/bold]
207
+
208
+ Total Frames: {stats['total_frames']}
209
+ Suspicious Events: [red]{stats['suspicious_frames']}[/red]
210
+ Total Alerts: [yellow]{stats['total_alerts']}[/yellow]
211
+
212
+ [bold]Alerts by Severity:[/bold]
213
+ {chr(10).join(f" [{sev}]: {count}" for sev, count in sorted(stats.get('alerts_by_severity', {}).items()))}""",
214
+ title="[bold cyan]Session Summary[/bold cyan]",
215
+ border_style="cyan",
216
+ box=box.ROUNDED,
217
+ ))
218
+
219
+
220
+ def show_suspicious(store: SQLiteStore):
221
+ """Show all suspicious events."""
222
+ results = store.query_suspicious_frames()
223
+
224
+ if not results:
225
+ console.print("[green]No suspicious events recorded.[/green]")
226
+ return
227
+
228
+ table = Table(
229
+ title=f"Suspicious Events ({len(results)} total)",
230
+ box=box.ROUNDED,
231
+ show_header=True,
232
+ header_style="bold red",
233
+ )
234
+ table.add_column("Frame", justify="center", style="bold")
235
+ table.add_column("Time", style="cyan")
236
+ table.add_column("Location")
237
+ table.add_column("Event Type")
238
+ table.add_column("Risk", justify="center")
239
+ table.add_column("Description", max_width=50)
240
+
241
+ for r in results:
242
+ risk = r.get("risk_level", "").upper()
243
+ risk_color = {"CRITICAL": "bold red", "HIGH": "red", "MEDIUM": "yellow", "LOW": "green"}.get(risk, "white")
244
+ table.add_row(
245
+ str(r["frame_id"]),
246
+ r["timestamp"],
247
+ r["location"],
248
+ r.get("event_type", "-"),
249
+ f"[{risk_color}]{risk}[/{risk_color}]",
250
+ (r.get("analysis_summary") or r.get("raw_description", "-"))[:50],
251
+ )
252
+
253
+ console.print(table)
254
+
255
+
256
+ def show_location_results(store: SQLiteStore, location: str):
257
+ """Search frames by location."""
258
+ results = store.query_by_location(location)
259
+
260
+ if not results:
261
+ console.print(f"[yellow]No frames found at '{location}'[/yellow]")
262
+ return
263
+
264
+ table = Table(
265
+ title=f"Events at '{location}' ({len(results)} results)",
266
+ box=box.ROUNDED,
267
+ show_header=True,
268
+ header_style="bold cyan",
269
+ )
270
+ table.add_column("Frame", justify="center", style="bold")
271
+ table.add_column("Time", style="cyan")
272
+ table.add_column("Location")
273
+ table.add_column("Event Type")
274
+ table.add_column("Risk", justify="center")
275
+ table.add_column("Description", max_width=50)
276
+
277
+ for r in results:
278
+ risk = r.get("risk_level", "").upper()
279
+ risk_color = {"CRITICAL": "bold red", "HIGH": "red", "MEDIUM": "yellow", "LOW": "green"}.get(risk, "white")
280
+ table.add_row(
281
+ str(r["frame_id"]),
282
+ r["timestamp"],
283
+ r["location"],
284
+ r.get("event_type", "-"),
285
+ f"[{risk_color}]{risk}[/{risk_color}]",
286
+ (r.get("analysis_summary") or r.get("raw_description", "-"))[:50],
287
+ )
288
+
289
+ console.print(table)
290
+
291
+
292
+ def main():
293
+ """Main CLI entry point."""
294
+ parser = argparse.ArgumentParser(
295
+ description="Drone Security Event Query Tool",
296
+ formatter_class=argparse.RawDescriptionHelpFormatter,
297
+ epilog="""
298
+ Examples:
299
+ python query.py --object "truck"
300
+ python query.py --time "after 22:00"
301
+ python query.py --search "suspicious nighttime activity"
302
+ python query.py --alerts
303
+ python query.py --summary
304
+ python query.py --suspicious
305
+ python query.py --location "Main Gate"
306
+ """
307
+ )
308
+
309
+ parser.add_argument("--object", "-o", type=str, help="Search by object type (e.g., 'truck', 'person')")
310
+ parser.add_argument("--time", "-t", type=str, help="Search by time (e.g., 'after 22:00', '06:00-12:00')")
311
+ parser.add_argument("--search", "-s", type=str, help="Semantic search query (ChromaDB)")
312
+ parser.add_argument("--alerts", "-a", action="store_true", help="Show all triggered alerts")
313
+ parser.add_argument("--summary", action="store_true", help="Show session summary statistics")
314
+ parser.add_argument("--suspicious", action="store_true", help="Show all suspicious events")
315
+ parser.add_argument("--location", "-l", type=str, help="Search by location")
316
+
317
+ args = parser.parse_args()
318
+
319
+ # Check if any argument was provided
320
+ if not any([args.object, args.time, args.search, args.alerts, args.summary,
321
+ args.suspicious, args.location]):
322
+ parser.print_help()
323
+ return
324
+
325
+ ensure_data_dir()
326
+
327
+ console.print(Panel(
328
+ "[bold]Drone Security Event Query System[/bold]",
329
+ border_style="cyan",
330
+ box=box.DOUBLE_EDGE,
331
+ ))
332
+
333
+ # Initialize stores
334
+ sqlite_store = SQLiteStore()
335
+
336
+ if args.object:
337
+ show_object_results(sqlite_store, args.object)
338
+
339
+ if args.time:
340
+ show_time_results(sqlite_store, args.time)
341
+
342
+ if args.search:
343
+ chroma_store = ChromaStore()
344
+ show_semantic_search(chroma_store, args.search)
345
+
346
+ if args.alerts:
347
+ show_alerts(sqlite_store)
348
+
349
+ if args.summary:
350
+ show_summary(sqlite_store)
351
+
352
+ if args.suspicious:
353
+ show_suspicious(sqlite_store)
354
+
355
+ if args.location:
356
+ show_location_results(sqlite_store, args.location)
357
+
358
+ sqlite_store.close()
359
+
360
+
361
+ if __name__ == "__main__":
362
+ main()
requirements.txt ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ openai>=1.0.0
2
+ langchain>=0.2.0
3
+ langchain-openai>=0.1.0
4
+ langgraph>=0.2.0
5
+ chromadb>=0.4.0
6
+ pysqlite3-binary>=0.5.0
7
+ sentence-transformers>=2.0.0
8
+ pillow>=10.0.0
9
+ python-dotenv>=1.0.0
10
+ pyyaml>=6.0.0
11
+ rich>=13.0.0
12
+ ultralytics>=8.0.0
13
+ opencv-python-headless>=4.8.0
14
+ deep-sort-realtime>=1.3.0
15
+ fastapi>=0.104.0
16
+ uvicorn[standard]>=0.24.0
17
+ sse-starlette>=1.6.0
18
+ python-multipart>=0.0.6
server.py ADDED
@@ -0,0 +1,471 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Aegis Drone Command — FastAPI Backend Server
3
+ Serves the military-industrial UI and provides REST API endpoints.
4
+ """
5
+ import os, sys, json, time, asyncio, threading, tempfile, base64
6
+ from datetime import datetime
7
+ import cv2
8
+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
9
+ os.environ["PYTHONIOENCODING"] = "utf-8"
10
+
11
+ from fastapi import FastAPI, UploadFile, File, Query, Form
12
+ from fastapi.staticfiles import StaticFiles
13
+ from fastapi.responses import FileResponse, JSONResponse
14
+ from sse_starlette.sse import EventSourceResponse
15
+
16
+ from config import ensure_data_dir, OPENAI_API_KEY, OPENAI_MODEL
17
+ from simulators.frames import get_simulated_frames
18
+ from simulators.telemetry import TelemetrySimulator
19
+ from analysis.vlm_analyzer import analyze_frame
20
+ from agent.security_agent import SecurityAgent
21
+ from indexing.sqlite_store import SQLiteStore
22
+ from indexing.chroma_store import ChromaStore
23
+ from alerts.rules_engine import AlertManager
24
+
25
+ app = FastAPI(title="Aegis Drone Command")
26
+
27
+ # --- State ---
28
+ sim_state = {
29
+ "running": False,
30
+ "results": [],
31
+ "alerts": [],
32
+ "frames_done": 0,
33
+ "objects_count": 0,
34
+ "summary": "",
35
+ "total_frames": 30,
36
+ }
37
+
38
+ # --- Static files ---
39
+ app.mount("/static", StaticFiles(directory="static"), name="static")
40
+
41
+ @app.get("/")
42
+ async def root():
43
+ return FileResponse("static/index.html")
44
+
45
+ # --- API ---
46
+ @app.get("/api/status")
47
+ async def get_status():
48
+ return sim_state
49
+
50
+ @app.get("/api/simulate")
51
+ async def simulate():
52
+ """SSE endpoint — streams frame results in real-time."""
53
+ if sim_state["running"]:
54
+ return JSONResponse({"error": "Simulation already running"}, 409)
55
+
56
+ async def event_generator():
57
+ sim_state["running"] = True
58
+ sim_state["results"] = []
59
+ sim_state["alerts"] = []
60
+ sim_state["frames_done"] = 0
61
+ sim_state["objects_count"] = 0
62
+ sim_state["summary"] = ""
63
+
64
+ ensure_data_dir()
65
+ sqlite_store = SQLiteStore()
66
+ chroma_store = ChromaStore()
67
+ alert_manager = AlertManager()
68
+ telemetry_sim = TelemetrySimulator()
69
+ sqlite_store.clear_all()
70
+ try: chroma_store.clear_all()
71
+ except: pass
72
+
73
+ yield {"event": "status", "data": json.dumps({"message": "Agent initializing..."})}
74
+ agent = SecurityAgent(sqlite_store, chroma_store, alert_manager)
75
+ yield {"event": "status", "data": json.dumps({"message": "Agent ready. Starting patrol..."})}
76
+
77
+ frames = get_simulated_frames()
78
+ sim_state["total_frames"] = len(frames)
79
+
80
+ for i, frame in enumerate(frames):
81
+ telemetry = telemetry_sim.get_telemetry(frame["time"], frame["location"])
82
+
83
+ # Run blocking work in thread
84
+ loop = asyncio.get_event_loop()
85
+ analysis = await loop.run_in_executor(None, analyze_frame, frame["description"], telemetry)
86
+ agent_result = await loop.run_in_executor(None, agent.process_frame, frame, analysis, telemetry)
87
+
88
+ prev_alert_count = len(sim_state["alerts"])
89
+ frame_alerts = [a for a in alert_manager.fired_alerts if a not in sim_state["alerts"]]
90
+ sim_state["alerts"].extend(frame_alerts)
91
+
92
+ objects = analysis.get("objects", [])
93
+ sim_state["objects_count"] += len(objects)
94
+ sim_state["frames_done"] = i + 1
95
+
96
+ entry = {
97
+ "frame_id": frame["frame_id"],
98
+ "time": frame["time"],
99
+ "location": frame["location"],
100
+ "objects": objects,
101
+ "event_type": analysis.get("event_type", "unknown"),
102
+ "risk_level": analysis.get("risk_level", "low").upper(),
103
+ "description": analysis.get("description", ""),
104
+ "is_suspicious": analysis.get("is_suspicious", False),
105
+ "recommended_action": analysis.get("recommended_action", ""),
106
+ "agent_response": agent_result.get("agent_response", ""),
107
+ "alerts": frame_alerts,
108
+ }
109
+ sim_state["results"].append(entry)
110
+ yield {"event": "frame", "data": json.dumps(entry, default=str)}
111
+ await asyncio.sleep(0.5)
112
+
113
+ # Generate summary
114
+ yield {"event": "status", "data": json.dumps({"message": "Generating AI summary..."})}
115
+ try:
116
+ summary = await loop.run_in_executor(None, agent.get_summary)
117
+ sim_state["summary"] = summary
118
+ except Exception as e:
119
+ sim_state["summary"] = f"Summary error: {e}"
120
+
121
+ sqlite_store.close()
122
+ sim_state["running"] = False
123
+ yield {"event": "complete", "data": json.dumps({
124
+ "summary": sim_state["summary"],
125
+ "total_frames": sim_state["frames_done"],
126
+ "total_alerts": len(sim_state["alerts"]),
127
+ "total_objects": sim_state["objects_count"],
128
+ })}
129
+
130
+ return EventSourceResponse(event_generator())
131
+
132
+ @app.get("/api/alerts")
133
+ async def get_alerts(severity: str = None):
134
+ alerts = sim_state["alerts"]
135
+ if severity:
136
+ alerts = [a for a in alerts if a.get("severity","").upper() == severity.upper()]
137
+ return {"alerts": alerts, "total": len(alerts)}
138
+
139
+ @app.get("/api/frames")
140
+ async def get_frames():
141
+ return {"frames": sim_state["results"], "total": len(sim_state["results"])}
142
+
143
+ @app.get("/api/frames/search")
144
+ async def search_frames(q: str = "", time_filter: str = "any", risk: str = ""):
145
+ try:
146
+ store = SQLiteStore()
147
+ if q:
148
+ results = store.query_by_object(q)
149
+ else:
150
+ results = store.get_all_frames()
151
+ if risk:
152
+ results = [r for r in results if r.get("risk_level","").upper() == risk.upper()]
153
+ if time_filter == "after_hours":
154
+ results = [r for r in results if _is_after_hours(r.get("timestamp",""))]
155
+ elif time_filter == "business":
156
+ results = [r for r in results if not _is_after_hours(r.get("timestamp",""))]
157
+ store.close()
158
+ return {"results": results, "total": len(results)}
159
+ except Exception as e:
160
+ return {"results": [], "total": 0, "error": str(e)}
161
+
162
+ @app.get("/api/semantic")
163
+ async def semantic_search(q: str = ""):
164
+ try:
165
+ chroma = ChromaStore()
166
+ results = chroma.semantic_search(q, top_k=10)
167
+ return {"results": results, "total": len(results)}
168
+ except Exception as e:
169
+ return {"results": [], "total": 0, "error": str(e)}
170
+
171
+ @app.get("/api/summary")
172
+ async def get_summary():
173
+ return {"summary": sim_state["summary"]}
174
+
175
+ @app.post("/api/generate_summary")
176
+ async def generate_summary_now():
177
+ """Generate a fresh AI summary from stored data."""
178
+ if sim_state["running"]:
179
+ return JSONResponse({"error": "Simulation is running, please wait"}, 409)
180
+
181
+ results = sim_state["results"]
182
+ if not results:
183
+ return {"summary": "No data available. Run simulation from Live Feed first to collect frame data."}
184
+
185
+ try:
186
+ # Build context from in-memory results
187
+ frame_summaries = []
188
+ for r in results:
189
+ risk = r.get("risk_level", "LOW")
190
+ objs = ", ".join(r.get("objects", []))
191
+ desc = r.get("description", "")
192
+ frame_summaries.append(
193
+ f"Frame {r.get('frame_id','?')} at {r.get('time','')} [{r.get('location','')}]: "
194
+ f"Risk={risk}, Objects=[{objs}], Description: {desc}"
195
+ )
196
+
197
+ alert_count = len(sim_state["alerts"])
198
+ total_frames = sim_state["frames_done"]
199
+ total_objects = sim_state["objects_count"]
200
+
201
+ context = (
202
+ f"Monitoring Session Report:\n"
203
+ f"Total Frames Analyzed: {total_frames}\n"
204
+ f"Total Objects Detected: {total_objects}\n"
205
+ f"Total Alerts Fired: {alert_count}\n\n"
206
+ f"Frame Details:\n" + "\n".join(frame_summaries)
207
+ )
208
+
209
+ sqlite_store = SQLiteStore()
210
+ chroma_store = ChromaStore()
211
+ alert_manager = AlertManager()
212
+ agent = SecurityAgent(sqlite_store, chroma_store, alert_manager)
213
+
214
+ # Ask agent to summarize with context
215
+ loop = asyncio.get_event_loop()
216
+ def _gen():
217
+ try:
218
+ result = agent.agent.invoke({
219
+ "messages": [
220
+ {"role": "user", "content": f"Based on the following surveillance monitoring data, generate a comprehensive security summary:\n\n{context}\n\nProvide a professional security briefing with key findings, risk assessment, and recommendations."}
221
+ ]
222
+ })
223
+ output_messages = result.get("messages", [])
224
+ for msg in reversed(output_messages):
225
+ if hasattr(msg, 'content') and msg.content and hasattr(msg, 'type') and msg.type == 'ai':
226
+ return msg.content
227
+ return "Summary generated but no content returned."
228
+ except Exception as e:
229
+ return f"Error: {e}"
230
+
231
+ summary = await loop.run_in_executor(None, _gen)
232
+ sim_state["summary"] = summary
233
+ sqlite_store.close()
234
+ return {"summary": summary}
235
+ except Exception as e:
236
+ return {"summary": f"Error generating summary: {str(e)}"}
237
+
238
+ # --- Video Analysis ---
239
+ from openai import OpenAI as _OpenAI
240
+ _vision_client = _OpenAI(api_key=OPENAI_API_KEY)
241
+
242
+ # Lazy-loaded YOLO detector for video analysis
243
+ _yolo_detector = None
244
+
245
+ def _get_yolo_detector():
246
+ """Lazily initialize YOLO detector to avoid slow startup."""
247
+ global _yolo_detector
248
+ if _yolo_detector is None:
249
+ from detector import VideoDetector
250
+ _yolo_detector = VideoDetector(confidence=0.3)
251
+ else:
252
+ _yolo_detector.reset()
253
+ return _yolo_detector
254
+
255
+ def _extract_frames(video_path, interval_sec=2, max_frames=20):
256
+ """Extract frames from video at regular intervals with high quality."""
257
+ cap = cv2.VideoCapture(video_path)
258
+ fps = cap.get(cv2.CAP_PROP_FPS) or 30
259
+ total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
260
+ duration = total / fps
261
+
262
+ # Dynamically adjust interval to cover more of the video
263
+ if duration > 0:
264
+ # Aim for ~max_frames evenly spaced across the video
265
+ ideal_interval = max(duration / max_frames, 1.0)
266
+ interval_sec = min(interval_sec, ideal_interval)
267
+
268
+ step = max(int(fps * interval_sec), 1)
269
+ frames = []
270
+ idx = 0
271
+ while cap.isOpened() and len(frames) < max_frames:
272
+ cap.set(cv2.CAP_PROP_POS_FRAMES, idx)
273
+ ret, frame = cap.read()
274
+ if not ret:
275
+ break
276
+ # Higher JPEG quality for better object identification
277
+ _, buf = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 90])
278
+ b64 = base64.b64encode(buf).decode('utf-8')
279
+ timestamp_sec = idx / fps
280
+ frames.append({
281
+ "index": len(frames),
282
+ "timestamp": f"{int(timestamp_sec//60):02d}:{int(timestamp_sec%60):02d}",
283
+ "base64": b64,
284
+ "raw_frame": frame, # Keep raw frame for YOLO detection
285
+ })
286
+ idx += step
287
+ cap.release()
288
+ return frames, round(duration, 1)
289
+
290
+ def _detect_objects_yolo(frame_bgr):
291
+ """Run YOLO object detection on a raw OpenCV frame."""
292
+ try:
293
+ detector = _get_yolo_detector()
294
+ result = detector.process_frame(frame_bgr)
295
+ objects = []
296
+ seen = set()
297
+ for track in result.get("active_tracks", []):
298
+ label = track.get("label", "unknown")
299
+ if label not in seen:
300
+ objects.append(label)
301
+ seen.add(label)
302
+ for det in result.get("raw_detections", []):
303
+ label = det.get("label", "unknown")
304
+ if label not in seen:
305
+ objects.append(label)
306
+ seen.add(label)
307
+ return objects, result.get("raw_detections", [])
308
+ except Exception as e:
309
+ print(f" [YOLO Error] {e}")
310
+ return [], []
311
+
312
+ def _describe_frame_with_vision(b64_image, yolo_objects=None):
313
+ """Send frame to OpenAI Vision API for detailed description."""
314
+ yolo_hint = ""
315
+ if yolo_objects:
316
+ yolo_hint = f"\n\nNote: An object detector has already identified these objects in the frame: {', '.join(yolo_objects)}. Use this to cross-reference your analysis."
317
+ try:
318
+ resp = _vision_client.chat.completions.create(
319
+ model="gpt-4o-mini",
320
+ messages=[
321
+ {"role": "system", "content": (
322
+ "You are an expert security surveillance analyst specializing in drone footage analysis. "
323
+ "Your job is to provide extremely detailed, accurate descriptions of what you see in each frame.\n\n"
324
+ "IMPORTANT — You MUST identify and list:\n"
325
+ "1. ALL people visible: count them, describe their clothing, positions, and actions\n"
326
+ "2. ALL vehicles: type (car, truck, van, motorcycle, bicycle), color, make/model if visible, license plates if readable\n"
327
+ "3. ALL animals: species, count, behavior\n"
328
+ "4. ALL objects of interest: bags, packages, tools, weapons, barriers, equipment\n"
329
+ "5. Environmental details: lighting conditions (day/night), weather, visibility\n"
330
+ "6. Activities: what are people/vehicles doing? entering, leaving, loading, standing, running?\n"
331
+ "7. Security concerns: anything unusual, out of place, or suspicious\n\n"
332
+ "Be SPECIFIC with numbers (e.g., '3 people' not 'several people'). "
333
+ "Be SPECIFIC with positions (e.g., 'near the entrance gate' not 'in the area'). "
334
+ "Describe in 4-6 detailed sentences."
335
+ )},
336
+ {"role": "user", "content": [
337
+ {"type": "text", "text": f"Analyze this drone surveillance frame in detail. Identify every person, vehicle, animal, and object visible.{yolo_hint}"},
338
+ {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64_image}", "detail": "high"}}
339
+ ]}
340
+ ],
341
+ max_tokens=500,
342
+ temperature=0.1,
343
+ )
344
+ return resp.choices[0].message.content.strip()
345
+ except Exception as e:
346
+ return f"Frame captured. Automated description unavailable: {e}"
347
+
348
+ @app.post("/api/analyze_video")
349
+ async def analyze_video(file: UploadFile = File(...)):
350
+ """Upload a video file, extract frames, analyze each with AI, stream results."""
351
+ # Save uploaded file to temp
352
+ ensure_data_dir()
353
+ tmp_path = os.path.join(os.path.dirname(__file__), "data", f"_upload_{int(time.time())}.mp4")
354
+ try:
355
+ content = await file.read()
356
+ with open(tmp_path, "wb") as f:
357
+ f.write(content)
358
+ except Exception as e:
359
+ return JSONResponse({"error": f"Upload failed: {e}"}, 500)
360
+
361
+ async def event_gen():
362
+ try:
363
+ yield {"event": "status", "data": json.dumps({"message": "Extracting frames from video..."})}
364
+
365
+ loop = asyncio.get_event_loop()
366
+ frames, duration = await loop.run_in_executor(None, _extract_frames, tmp_path)
367
+
368
+ yield {"event": "status", "data": json.dumps({
369
+ "message": f"Extracted {len(frames)} frames from {duration}s video. Starting AI analysis...",
370
+ "total_frames": len(frames), "duration": duration
371
+ })}
372
+
373
+ locations = ["Main Gate", "Perimeter Fence North", "Parking Lot A", "Warehouse Entrance", "Loading Dock"]
374
+ telemetry_sim = TelemetrySimulator()
375
+
376
+ for i, frame_data in enumerate(frames):
377
+ yield {"event": "status", "data": json.dumps({"message": f"Analyzing frame {i+1}/{len(frames)} — Running object detection..."})}
378
+
379
+ # Step 1: Run YOLO object detection on the raw frame
380
+ raw_frame = frame_data.get("raw_frame")
381
+ yolo_objects = []
382
+ yolo_detections = []
383
+ if raw_frame is not None:
384
+ yolo_objects, yolo_detections = await loop.run_in_executor(
385
+ None, _detect_objects_yolo, raw_frame
386
+ )
387
+
388
+ yield {"event": "status", "data": json.dumps({"message": f"Analyzing frame {i+1}/{len(frames)} — Running vision AI..."})}
389
+
390
+ # Step 2: Get AI description with YOLO hints for cross-referencing
391
+ description = await loop.run_in_executor(
392
+ None, _describe_frame_with_vision, frame_data["base64"], yolo_objects
393
+ )
394
+
395
+ # Build telemetry
396
+ telemetry = {
397
+ "time": frame_data["timestamp"],
398
+ "location": locations[i % len(locations)],
399
+ "drone_id": "UNIT-07",
400
+ "altitude_m": 120,
401
+ "property": "SecureTech Industrial Complex",
402
+ }
403
+
404
+ # Step 3: Run through existing analysis pipeline
405
+ analysis = await loop.run_in_executor(None, analyze_frame, description, telemetry)
406
+
407
+ # Step 4: Merge objects — combine YOLO detections with VLM-identified objects
408
+ vlm_objects = analysis.get("objects", [])
409
+ merged_objects = list(vlm_objects) # Start with VLM objects
410
+ vlm_lower = {o.lower() for o in vlm_objects}
411
+ for yobj in yolo_objects:
412
+ if yobj.lower() not in vlm_lower:
413
+ merged_objects.append(yobj)
414
+
415
+ # Add detection counts from YOLO for more accuracy
416
+ det_counts = {}
417
+ for det in yolo_detections:
418
+ label = det.get("label", "unknown")
419
+ det_counts[label] = det_counts.get(label, 0) + 1
420
+
421
+ entry = {
422
+ "frame_id": i + 1,
423
+ "time": frame_data["timestamp"],
424
+ "location": telemetry["location"],
425
+ "objects": merged_objects,
426
+ "yolo_detections": [{"label": d["label"], "confidence": d["confidence"]} for d in yolo_detections],
427
+ "detection_counts": det_counts,
428
+ "event_type": analysis.get("event_type", "unknown"),
429
+ "risk_level": analysis.get("risk_level", "low").upper(),
430
+ "description": analysis.get("description", description[:200]),
431
+ "is_suspicious": analysis.get("is_suspicious", False),
432
+ "recommended_action": analysis.get("recommended_action", ""),
433
+ "frame_description": description,
434
+ }
435
+
436
+ yield {"event": "frame", "data": json.dumps(entry, default=str)}
437
+ await asyncio.sleep(0.3)
438
+
439
+ yield {"event": "complete", "data": json.dumps({
440
+ "message": "Video analysis complete.",
441
+ "total_frames": len(frames),
442
+ })}
443
+ except Exception as e:
444
+ yield {"event": "error", "data": json.dumps({"message": str(e)})}
445
+ finally:
446
+ try: os.remove(tmp_path)
447
+ except: pass
448
+
449
+ return EventSourceResponse(event_gen())
450
+
451
+ @app.get("/api/stats")
452
+ async def get_stats():
453
+ try:
454
+ s = SQLiteStore()
455
+ stats = s.get_session_stats()
456
+ s.close()
457
+ return stats
458
+ except:
459
+ return {"total_frames": 0, "suspicious_frames": 0, "total_alerts": 0, "alerts_by_severity": {}}
460
+
461
+ def _is_after_hours(timestamp: str) -> bool:
462
+ try:
463
+ h = int(timestamp.split(":")[0])
464
+ return h >= 22 or h < 6
465
+ except:
466
+ return False
467
+
468
+ if __name__ == "__main__":
469
+ import uvicorn
470
+ port = int(os.environ.get("PORT", 8000))
471
+ uvicorn.run(app, host="0.0.0.0", port=port)
simulators/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ # Simulators package
simulators/frames.py ADDED
@@ -0,0 +1,245 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Video Frame Simulator — Generates 30 diverse simulated drone surveillance frames.
3
+
4
+ Each frame represents a single snapshot from a drone camera, described as text.
5
+ Covers day/night scenarios, vehicles, people, suspicious/normal activity, and edge cases.
6
+ """
7
+
8
+
9
+ def get_simulated_frames() -> list[dict]:
10
+ """
11
+ Returns 30 diverse simulated video frames covering a full 24-hour patrol cycle.
12
+
13
+ Each frame is a dict with:
14
+ - frame_id (int): Sequential frame identifier
15
+ - time (str): Timestamp in HH:MM format
16
+ - location (str): Where the drone is patrolling
17
+ - description (str): Rich text description of what the camera sees
18
+
19
+ Returns:
20
+ List of 30 frame dictionaries
21
+ """
22
+ frames = [
23
+ # === EARLY MORNING (06:00 - 08:00) — Shift start, employee arrivals ===
24
+ {
25
+ "frame_id": 1,
26
+ "time": "06:00",
27
+ "location": "Main Gate",
28
+ "description": "Security guard opening the main gate. No vehicles or pedestrians in view. Morning light, clear visibility. Gate barrier is being raised."
29
+ },
30
+ {
31
+ "frame_id": 2,
32
+ "time": "06:15",
33
+ "location": "Main Gate",
34
+ "description": "White Toyota Camry approaching the main gate from the east road. Single occupant visible — appears to be an employee with a badge. Vehicle slowing down for entry."
35
+ },
36
+ {
37
+ "frame_id": 3,
38
+ "time": "06:30",
39
+ "location": "Parking Lot A",
40
+ "description": "White Toyota Camry parking in spot A-12. Driver exiting vehicle, wearing company uniform, walking toward Admin Building. Parking lot is mostly empty."
41
+ },
42
+ {
43
+ "frame_id": 4,
44
+ "time": "06:45",
45
+ "location": "Perimeter Fence North",
46
+ "description": "North perimeter fence intact. A stray cat walking along the fence line. No signs of intrusion or damage. Morning dew on the grass. Fence sensors appear normal."
47
+ },
48
+ {
49
+ "frame_id": 5,
50
+ "time": "07:00",
51
+ "location": "Main Gate",
52
+ "description": "Silver Honda Civic and Black BMW X5 arriving in convoy through main gate. Both vehicles have employee parking stickers visible. Normal morning traffic."
53
+ },
54
+
55
+ # === MID MORNING (08:00 - 11:00) — Business hours, deliveries ===
56
+ {
57
+ "frame_id": 6,
58
+ "time": "08:30",
59
+ "location": "Loading Dock",
60
+ "description": "FedEx Delivery Van backing into Loading Dock Bay 2. Driver in FedEx uniform stepping out with a handheld scanner. Two warehouse workers approaching to receive packages."
61
+ },
62
+ {
63
+ "frame_id": 7,
64
+ "time": "09:00",
65
+ "location": "Warehouse Entrance",
66
+ "description": "Three employees in safety vests entering the warehouse through the main entrance. Forklift visible inside. Normal warehouse operations in progress."
67
+ },
68
+ {
69
+ "frame_id": 8,
70
+ "time": "09:30",
71
+ "location": "Parking Lot B",
72
+ "description": "Blue Ford F150 pickup truck entering Parking Lot B. No employee sticker visible. Truck has out-of-state license plates. Driver remains seated in vehicle for several minutes."
73
+ },
74
+ {
75
+ "frame_id": 9,
76
+ "time": "10:00",
77
+ "location": "Admin Building",
78
+ "description": "Group of 4 people in business attire entering the Admin Building front entrance. Appears to be a scheduled meeting — visitor badges visible on lanyards. Receptionist greeting them at the door."
79
+ },
80
+ {
81
+ "frame_id": 10,
82
+ "time": "10:30",
83
+ "location": "Server Room Exterior",
84
+ "description": "Maintenance technician in company coveralls using a keycard to access the Server Room exterior door. Tool bag in hand. Security badge clearly visible."
85
+ },
86
+
87
+ # === MIDDAY (11:00 - 14:00) — Lunch, routine activity ===
88
+ {
89
+ "frame_id": 11,
90
+ "time": "11:30",
91
+ "location": "Parking Lot A",
92
+ "description": "Blue Ford F150 from Parking Lot B now seen driving slowly through Parking Lot A. Same out-of-state plates. Vehicle circling the lot without parking. Suspicious behavior — possible surveillance."
93
+ },
94
+ {
95
+ "frame_id": 12,
96
+ "time": "12:00",
97
+ "location": "Admin Building",
98
+ "description": "Multiple employees exiting Admin Building for lunch break. Approximately 8-10 people walking toward Parking Lot A. Normal midday activity."
99
+ },
100
+ {
101
+ "frame_id": 13,
102
+ "time": "12:30",
103
+ "location": "Main Gate",
104
+ "description": "UPS Delivery Truck entering through main gate. UPS branding clearly visible. Driver showing delivery manifest to the security guard at the checkpoint."
105
+ },
106
+ {
107
+ "frame_id": 14,
108
+ "time": "13:00",
109
+ "location": "Loading Dock",
110
+ "description": "UPS Delivery Truck at Loading Dock Bay 1. Three large pallets being unloaded. Warehouse supervisor checking delivery against order sheet. Normal delivery operation."
111
+ },
112
+
113
+ # === AFTERNOON (14:00 - 18:00) — Continued operations, end of business ===
114
+ {
115
+ "frame_id": 15,
116
+ "time": "14:30",
117
+ "location": "Perimeter Fence East",
118
+ "description": "Person in dark hoodie standing near the east perimeter fence, looking through the chain-link. Not an employee — no badge visible. Person appears to be taking photos of the warehouse with a phone."
119
+ },
120
+ {
121
+ "frame_id": 16,
122
+ "time": "14:45",
123
+ "location": "Perimeter Fence East",
124
+ "description": "Same person in dark hoodie now walking along the east perimeter fence toward the south corner. Still taking photos. No vehicle visible nearby — person appears to have arrived on foot."
125
+ },
126
+ {
127
+ "frame_id": 17,
128
+ "time": "15:00",
129
+ "location": "Main Gate",
130
+ "description": "Blue Ford F150 exiting through main gate at moderate speed. Same out-of-state plates from earlier. Driver not making eye contact with security guard. Vehicle heading east."
131
+ },
132
+ {
133
+ "frame_id": 18,
134
+ "time": "16:30",
135
+ "location": "Parking Lot A",
136
+ "description": "Red Chevrolet Silverado parked in an unauthorized zone near the emergency exit. Engine running, no occupant visible inside. Hazard lights are on."
137
+ },
138
+ {
139
+ "frame_id": 19,
140
+ "time": "17:00",
141
+ "location": "Main Gate",
142
+ "description": "End of business traffic. Silver Honda Civic, Black BMW X5, and two other vehicles exiting through the main gate. Normal employee departure pattern. Security guard waving them through."
143
+ },
144
+ {
145
+ "frame_id": 20,
146
+ "time": "17:30",
147
+ "location": "Emergency Exit Rear",
148
+ "description": "Rear emergency exit door is ajar. No person visible. Door should be closed and locked at all times. Interior lights flickering — possible maintenance issue."
149
+ },
150
+
151
+ # === EVENING (18:00 - 22:00) — Reduced activity, transition to night ===
152
+ {
153
+ "frame_id": 21,
154
+ "time": "18:30",
155
+ "location": "Parking Lot A",
156
+ "description": "Parking lot nearly empty. Only 3 vehicles remaining — White Toyota Camry in A-12, Red Chevrolet Silverado still in unauthorized zone (engine now off), and an unrecognized gray sedan in A-5."
157
+ },
158
+ {
159
+ "frame_id": 22,
160
+ "time": "19:00",
161
+ "location": "Warehouse Entrance",
162
+ "description": "Night shift security guard beginning patrol at warehouse entrance. Flashlight beam visible. Guard checking door locks and scanning badge at checkpoint. Normal shift change procedure."
163
+ },
164
+ {
165
+ "frame_id": 23,
166
+ "time": "20:00",
167
+ "location": "Perimeter Fence South",
168
+ "description": "Two deer near the south perimeter fence, grazing on the lawn. Motion sensors triggered but no human activity. Wildlife false alarm — common occurrence per historical data."
169
+ },
170
+ {
171
+ "frame_id": 24,
172
+ "time": "21:30",
173
+ "location": "Main Gate",
174
+ "description": "White Toyota Camry exiting main gate. Late departure — employee working overtime. Security guard logs the exit. Gate area well-lit, no other activity."
175
+ },
176
+
177
+ # === NIGHT (22:00 - 02:00) — High security, suspicious events ===
178
+ {
179
+ "frame_id": 25,
180
+ "time": "22:15",
181
+ "location": "Perimeter Fence North",
182
+ "description": "Person in dark clothing crouching near the north perimeter fence. Appears to be testing the fence for weaknesses. No badge or identification visible. Night vision shows a backpack on the ground beside them."
183
+ },
184
+ {
185
+ "frame_id": 26,
186
+ "time": "22:30",
187
+ "location": "Perimeter Fence North",
188
+ "description": "Same person from Frame 25 now attempting to climb the north perimeter fence. Hands on the chain-link, one foot on the lower rail. Backpack slung over shoulder. Clear perimeter breach attempt."
189
+ },
190
+ {
191
+ "frame_id": 27,
192
+ "time": "23:00",
193
+ "location": "Main Gate",
194
+ "description": "Unidentified dark-colored SUV idling near the main gate with headlights off. Windows are tinted — occupants not visible. Vehicle not in the known vehicles database. License plate partially obscured by mud."
195
+ },
196
+ {
197
+ "frame_id": 28,
198
+ "time": "23:30",
199
+ "location": "Server Room Exterior",
200
+ "description": "Person in a ski mask approaching the Server Room exterior door. Attempting to use a device on the keycard reader. Unauthorized access attempt. No security badge visible."
201
+ },
202
+ {
203
+ "frame_id": 29,
204
+ "time": "00:15",
205
+ "location": "Loading Dock",
206
+ "description": "Loading dock bay 3 door partially open from inside. No scheduled deliveries at this hour. Shadow of a person visible inside the dock area. Dock lights are off — area should be fully illuminated per security protocol."
207
+ },
208
+ {
209
+ "frame_id": 30,
210
+ "time": "01:00",
211
+ "location": "Parking Lot B",
212
+ "description": "Two individuals near the gray sedan in Parking Lot B (same vehicle from earlier in A-5, now relocated). Trunk is open, items being loaded. Both wearing dark clothing with face coverings. Highly suspicious — potential theft in progress."
213
+ },
214
+ ]
215
+
216
+ return frames
217
+
218
+
219
+ def get_frame_by_id(frame_id: int) -> dict | None:
220
+ """Get a specific frame by its ID."""
221
+ frames = get_simulated_frames()
222
+ for frame in frames:
223
+ if frame["frame_id"] == frame_id:
224
+ return frame
225
+ return None
226
+
227
+
228
+ def get_frames_by_time_range(start_time: str, end_time: str) -> list[dict]:
229
+ """Get frames within a time range (HH:MM format)."""
230
+ frames = get_simulated_frames()
231
+ start_minutes = _time_to_minutes(start_time)
232
+ end_minutes = _time_to_minutes(end_time)
233
+
234
+ # Handle overnight ranges (e.g., 22:00 to 02:00)
235
+ if end_minutes < start_minutes:
236
+ return [f for f in frames if _time_to_minutes(f["time"]) >= start_minutes
237
+ or _time_to_minutes(f["time"]) <= end_minutes]
238
+ else:
239
+ return [f for f in frames if start_minutes <= _time_to_minutes(f["time"]) <= end_minutes]
240
+
241
+
242
+ def _time_to_minutes(time_str: str) -> int:
243
+ """Convert HH:MM to total minutes for comparison."""
244
+ parts = time_str.split(":")
245
+ return int(parts[0]) * 60 + int(parts[1])
simulators/telemetry.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Telemetry Simulator — Generates fake drone telemetry data.
3
+
4
+ Produces structured telemetry readings for each frame timestamp,
5
+ simulating GPS position, altitude, battery, and drone status.
6
+ """
7
+
8
+ import random
9
+ from config import DRONE_ID, LOCATIONS, PROPERTY_NAME
10
+
11
+
12
+ class TelemetrySimulator:
13
+ """Simulates drone telemetry data for security patrol missions."""
14
+
15
+ def __init__(self, drone_id: str = DRONE_ID):
16
+ self.drone_id = drone_id
17
+ self.battery = 100
18
+ self.mission_start = True
19
+
20
+ def get_telemetry(self, time_str: str, location: str) -> dict:
21
+ """
22
+ Generate telemetry data for a given time and location.
23
+
24
+ Args:
25
+ time_str: Time in HH:MM format (e.g., "06:00")
26
+ location: Name of the patrol location
27
+
28
+ Returns:
29
+ dict with telemetry readings
30
+ """
31
+ # Battery drains ~2-4% per frame
32
+ if self.mission_start:
33
+ self.battery = random.randint(95, 100)
34
+ self.mission_start = False
35
+ else:
36
+ self.battery = max(10, self.battery - random.randint(1, 3))
37
+
38
+ # Altitude varies by location type
39
+ altitude = self._get_altitude_for_location(location)
40
+
41
+ # Wind speed simulation
42
+ hour = int(time_str.split(":")[0])
43
+ wind_speed = round(random.uniform(2.0, 15.0), 1)
44
+ if hour >= 22 or hour < 6:
45
+ wind_speed = round(random.uniform(1.0, 8.0), 1) # calmer at night
46
+
47
+ return {
48
+ "drone_id": self.drone_id,
49
+ "time": time_str,
50
+ "location": location,
51
+ "property": PROPERTY_NAME,
52
+ "altitude_m": altitude,
53
+ "battery_pct": self.battery,
54
+ "wind_speed_kmh": wind_speed,
55
+ "gps_signal": "strong" if random.random() > 0.1 else "moderate",
56
+ "status": "patrolling",
57
+ }
58
+
59
+ def _get_altitude_for_location(self, location: str) -> int:
60
+ """Determine drone altitude based on patrol location."""
61
+ if "Gate" in location:
62
+ return random.randint(10, 15)
63
+ elif "Perimeter" in location:
64
+ return random.randint(15, 25)
65
+ elif "Parking" in location:
66
+ return random.randint(12, 18)
67
+ elif "Warehouse" in location or "Loading" in location:
68
+ return random.randint(8, 14)
69
+ elif "Server" in location:
70
+ return random.randint(10, 15)
71
+ else:
72
+ return random.randint(12, 20)
73
+
74
+ def reset(self):
75
+ """Reset the simulator for a new mission."""
76
+ self.battery = 100
77
+ self.mission_start = True
78
+
79
+
80
+ def get_telemetry_for_frame(frame: dict, simulator: TelemetrySimulator = None) -> dict:
81
+ """
82
+ Convenience function to generate telemetry for a given frame.
83
+
84
+ Args:
85
+ frame: Frame dict with 'time' and 'location' keys
86
+ simulator: Optional TelemetrySimulator instance
87
+
88
+ Returns:
89
+ Telemetry dict
90
+ """
91
+ if simulator is None:
92
+ simulator = TelemetrySimulator()
93
+ return simulator.get_telemetry(frame["time"], frame["location"])
static/alerts.html ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html class="dark" lang="en"><head>
3
+ <meta charset="utf-8"/><meta content="width=device-width, initial-scale=1.0" name="viewport"/>
4
+ <title>Aegis Drone Command - Alerts</title>
5
+ <script src="https://cdn.tailwindcss.com?plugins=forms,container-queries"></script>
6
+ <link href="https://fonts.googleapis.com/css2?family=Anton&family=Arimo:wght@400;700&family=JetBrains+Mono:wght@400;500;700&display=swap" rel="stylesheet"/>
7
+ <link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:wght,FILL@100..700,0..1&display=swap" rel="stylesheet"/>
8
+ <script>tailwind.config={darkMode:"class",theme:{extend:{colors:{"surface-tint":"#c6c6cb","on-surface-variant":"#c6c6cb","surface-dim":"#101418","surface-container-highest":"#32353a","surface-container-high":"#272a2f","surface-container-lowest":"#0b0e13","surface-container":"#1d2024","on-error-container":"#ffdad6","on-surface":"#e0e2e8",primary:"#c6c6cb","secondary-fixed":"#72ff70","on-tertiary-container":"#ff5d53","on-primary-container":"#939499",surface:"#101418",tertiary:"#ffb4ab",error:"#ffb4ab",outline:"#909095","tertiary-container":"#620005","error-container":"#93000a",background:"#101418","primary-container":"#2b2d31","outline-variant":"#45474a","primary-fixed-dim":"#c6c6cb","tertiary-fixed-dim":"#ffb4ab","on-background":"#e0e2e8","surface-container-low":"#191c20","on-secondary-container":"#007117","surface-bright":"#36393e","secondary-fixed-dim":"#00e639","on-error":"#690005","surface-variant":"#32353a"},borderRadius:{DEFAULT:"0.125rem",lg:"0.25rem",xl:"0.5rem",full:"0.75rem"},spacing:{"gutter-md":"1.5rem","panel-padding":"2rem","component-gap":"1rem"},fontFamily:{"body-md":["Arimo"],"display-code":["JetBrains Mono"],"headline-lg":["Anton"],"label-caps":["JetBrains Mono"],"headline-md":["Anton"]},fontSize:{"body-md":["16px",{lineHeight:"1.5",fontWeight:"400"}],"display-code":["20px",{lineHeight:"1.2",letterSpacing:"-0.02em",fontWeight:"500"}],"headline-lg":["48px",{lineHeight:"1.1",letterSpacing:"0.05em",fontWeight:"400"}],"label-caps":["14px",{lineHeight:"1.2",fontWeight:"700"}],"headline-md":["32px",{lineHeight:"1.2",fontWeight:"400"}]}}}};</script>
9
+ <style>.glass-highlight{background:linear-gradient(135deg,rgba(255,255,255,.05) 0%,rgba(255,255,255,0) 50%)}</style>
10
+ </head>
11
+ <body class="bg-background text-on-surface font-body-md h-screen w-screen overflow-hidden flex flex-col">
12
+ <script src="/static/shell.js"></script>
13
+
14
+ <!-- MAIN -->
15
+ <main class="ml-60 mt-16 p-8 overflow-y-auto bg-surface relative h-[calc(100vh-4rem)]">
16
+ <div class="max-w-6xl mx-auto">
17
+ <div class="bg-surface-container rounded-lg border-4 border-surface-variant shadow-[0_15px_35px_rgba(0,0,0,0.8),inset_0_2px_0_rgba(255,255,255,0.05)] p-panel-padding relative">
18
+ <!-- Corner screws -->
19
+ <div class="absolute top-2 left-2 w-4 h-4 rounded-full bg-surface-dim border border-surface shadow-[inset_0_2px_4px_rgba(0,0,0,0.8)] flex items-center justify-center"><div class="w-2 h-[1px] bg-surface-variant rotate-45"></div></div>
20
+ <div class="absolute top-2 right-2 w-4 h-4 rounded-full bg-surface-dim border border-surface shadow-[inset_0_2px_4px_rgba(0,0,0,0.8)] flex items-center justify-center"><div class="w-2 h-[1px] bg-surface-variant -rotate-12"></div></div>
21
+ <div class="absolute bottom-2 left-2 w-4 h-4 rounded-full bg-surface-dim border border-surface shadow-[inset_0_2px_4px_rgba(0,0,0,0.8)] flex items-center justify-center"><div class="w-2 h-[1px] bg-surface-variant rotate-90"></div></div>
22
+ <div class="absolute bottom-2 right-2 w-4 h-4 rounded-full bg-surface-dim border border-surface shadow-[inset_0_2px_4px_rgba(0,0,0,0.8)] flex items-center justify-center"><div class="w-2 h-[1px] bg-surface-variant rotate-180"></div></div>
23
+
24
+ <div class="border-b-2 border-surface-dim pb-4 mb-8 flex justify-between items-end">
25
+ <div><h1 class="font-headline-lg text-headline-lg text-on-surface uppercase tracking-wider">Threat Matrix</h1>
26
+ <p class="font-display-code text-[14px] text-outline uppercase tracking-widest mt-1">System Status: <span id="sysStatus" class="text-secondary-fixed-dim">NOMINAL</span></p></div>
27
+ <div id="clock" class="font-display-code text-[24px] text-primary-fixed-dim"></div>
28
+ </div>
29
+
30
+ <!-- Severity counters -->
31
+ <div class="grid grid-cols-4 gap-6 mb-10">
32
+ <div class="bg-surface-container-lowest border-2 border-surface-dim rounded shadow-[inset_0_8px_20px_rgba(0,0,0,0.9)] p-6 relative overflow-hidden flex flex-col items-center justify-center glass-highlight">
33
+ <div id="cnt-critical" class="font-display-code text-[72px] leading-none text-error drop-shadow-[0_0_15px_rgba(255,180,171,0.6)] font-bold">0</div>
34
+ <div class="font-label-caps text-label-caps text-on-surface-variant mt-4 tracking-[0.2em]">CRITICAL</div>
35
+ <div class="absolute bottom-2 right-2 w-2 h-2 rounded-full bg-error shadow-[0_0_5px_rgba(255,180,171,0.8)] animate-pulse"></div></div>
36
+ <div class="bg-surface-container-lowest border-2 border-surface-dim rounded shadow-[inset_0_8px_20px_rgba(0,0,0,0.9)] p-6 relative overflow-hidden flex flex-col items-center justify-center glass-highlight">
37
+ <div id="cnt-high" class="font-display-code text-[72px] leading-none text-tertiary-fixed-dim font-bold">0</div>
38
+ <div class="font-label-caps text-label-caps text-on-surface-variant mt-4 tracking-[0.2em]">HIGH</div></div>
39
+ <div class="bg-surface-container-lowest border-2 border-surface-dim rounded shadow-[inset_0_8px_20px_rgba(0,0,0,0.9)] p-6 relative overflow-hidden flex flex-col items-center justify-center glass-highlight">
40
+ <div id="cnt-medium" class="font-display-code text-[72px] leading-none text-primary font-bold">0</div>
41
+ <div class="font-label-caps text-label-caps text-on-surface-variant mt-4 tracking-[0.2em]">MEDIUM</div></div>
42
+ <div class="bg-surface-container-lowest border-2 border-surface-dim rounded shadow-[inset_0_8px_20px_rgba(0,0,0,0.9)] p-6 relative overflow-hidden flex flex-col items-center justify-center glass-highlight">
43
+ <div id="cnt-low" class="font-display-code text-[72px] leading-none text-surface-tint font-bold">0</div>
44
+ <div class="font-label-caps text-label-caps text-on-surface-variant mt-4 tracking-[0.2em]">LOW</div></div>
45
+ </div>
46
+
47
+ <!-- Filter bar -->
48
+ <div class="bg-surface-container-high rounded p-4 mb-8 border border-surface shadow-[inset_0_4px_10px_rgba(0,0,0,0.4)] flex items-center justify-between">
49
+ <div class="font-label-caps text-[12px] text-outline uppercase tracking-widest flex items-center gap-2"><span class="material-symbols-outlined text-[16px]">tune</span> Filter Engaged</div>
50
+ <div class="flex space-x-4" id="filterBtns">
51
+ <button onclick="filterAlerts('all')" class="filter-btn active relative px-6 py-3 bg-surface-container-lowest rounded border border-surface-dim shadow-[inset_0_3px_6px_rgba(0,0,0,0.9)] font-label-caps text-label-caps text-on-surface">ALL</button>
52
+ <button onclick="filterAlerts('CRITICAL')" class="filter-btn relative px-6 py-3 bg-surface-variant rounded border-t border-white/10 shadow-[0_5px_0_#101418] font-label-caps text-label-caps text-error hover:bg-surface-bright transition-all">CRITICAL</button>
53
+ <button onclick="filterAlerts('HIGH')" class="filter-btn relative px-6 py-3 bg-surface-variant rounded border-t border-white/10 shadow-[0_5px_0_#101418] font-label-caps text-label-caps text-on-surface hover:bg-surface-bright transition-all">HIGH</button>
54
+ <button onclick="filterAlerts('MEDIUM')" class="filter-btn relative px-6 py-3 bg-surface-variant rounded border-t border-white/10 shadow-[0_5px_0_#101418] font-label-caps text-label-caps text-on-surface hover:bg-surface-bright transition-all">MEDIUM</button>
55
+ <button onclick="filterAlerts('LOW')" class="filter-btn relative px-6 py-3 bg-surface-variant rounded border-t border-white/10 shadow-[0_5px_0_#101418] font-label-caps text-label-caps text-on-surface hover:bg-surface-bright transition-all">LOW</button>
56
+ </div>
57
+ </div>
58
+
59
+ <!-- Alert cards -->
60
+ <div id="alertList" class="space-y-4">
61
+ <div class="text-center font-display-code text-outline py-12">Run simulation from Live Feed to generate alerts</div>
62
+ </div>
63
+ </div></div>
64
+ </main>
65
+
66
+ <script>
67
+ // Clock
68
+ setInterval(()=>{document.getElementById('clock').textContent=new Date().toLocaleTimeString('en-US',{hour12:false,timeZone:'UTC'})+' UTC'},1000);
69
+
70
+ let allAlerts=[];
71
+ let allFrames=[];
72
+
73
+ function renderAlert(frame, idx) {
74
+ const risk = (frame.risk_level||'LOW').toUpperCase();
75
+ const borderColor = risk==='CRITICAL'?'error':risk==='HIGH'?'tertiary-fixed-dim':risk==='MEDIUM'?'primary':'surface-tint';
76
+ const code = `${risk.substring(0,3)}-${String(idx).padStart(3,'0')}`;
77
+ return `<div class="alert-card relative bg-surface-container-highest rounded border border-surface-dim shadow-[0_8px_20px_rgba(0,0,0,0.6),inset_0_1px_0_rgba(255,255,255,0.05)] p-5 pl-8 overflow-hidden group hover:bg-surface-bright transition-colors cursor-pointer" data-risk="${risk}">
78
+ <div class="absolute left-0 top-0 bottom-0 w-3 bg-${borderColor} shadow-[0_0_20px_rgba(255,180,171,0.5)]"></div>
79
+ <div class="absolute top-2 left-5 w-2 h-2 rounded-full bg-surface-dim border border-surface shadow-[inset_0_1px_2px_rgba(0,0,0,0.8)]"></div>
80
+ <div class="absolute top-2 right-2 w-2 h-2 rounded-full bg-surface-dim border border-surface shadow-[inset_0_1px_2px_rgba(0,0,0,0.8)]"></div>
81
+ <div class="flex justify-between items-start">
82
+ <div class="flex flex-col gap-1">
83
+ <div class="flex items-center gap-3">
84
+ <span class="font-display-code text-[16px] text-${borderColor}">${code}</span>
85
+ <span class="font-label-caps text-[10px] text-surface-tint bg-surface-dim px-2 py-0.5 rounded border border-surface-variant">${frame.location||'UNKNOWN'}</span>
86
+ <span class="font-label-caps text-[10px] text-on-surface bg-surface-variant px-2 py-0.5 rounded">${risk}</span>
87
+ </div>
88
+ <h3 class="font-headline-md text-[20px] text-on-surface mt-1">${frame.event_type||'Security Event'}</h3>
89
+ <p class="font-body-md text-on-surface-variant text-[14px]">${frame.description||''}</p>
90
+ </div>
91
+ <div class="flex flex-col items-end gap-2">
92
+ <span class="font-display-code text-[14px] text-outline">${frame.time||''}</span>
93
+ <button onclick="this.textContent='ACKNOWLEDGED';this.disabled=true;this.classList.add('opacity-50')" class="bg-surface-variant hover:bg-surface-bright border-t border-white/10 shadow-[0_2px_0_#101418] px-4 py-1.5 rounded font-label-caps text-[12px] text-on-surface transition-all active:translate-y-[2px] active:shadow-none">ACKNOWLEDGE</button>
94
+ </div>
95
+ </div></div>`;
96
+ }
97
+
98
+ function filterAlerts(level) {
99
+ document.querySelectorAll('.filter-btn').forEach(b=>{
100
+ b.classList.remove('bg-surface-container-lowest','shadow-[inset_0_3px_6px_rgba(0,0,0,0.9)]');
101
+ b.classList.add('bg-surface-variant','shadow-[0_5px_0_#101418]');
102
+ });
103
+ event.target.classList.add('bg-surface-container-lowest','shadow-[inset_0_3px_6px_rgba(0,0,0,0.9)]');
104
+ event.target.classList.remove('bg-surface-variant','shadow-[0_5px_0_#101418]');
105
+ document.querySelectorAll('.alert-card').forEach(c=>{
106
+ c.style.display = (level==='all'||c.dataset.risk===level)?'block':'none';
107
+ });
108
+ }
109
+
110
+ function loadAlerts() {
111
+ fetch('/api/status').then(r=>r.json()).then(data=>{
112
+ allFrames = data.results||[];
113
+ allAlerts = data.alerts||[];
114
+ const suspicious = allFrames.filter(f=>f.is_suspicious||f.risk_level==='CRITICAL'||f.risk_level==='HIGH');
115
+ let crit=0,high=0,med=0,low=0;
116
+ allFrames.forEach(f=>{
117
+ const r=(f.risk_level||'LOW').toUpperCase();
118
+ if(r==='CRITICAL')crit++;else if(r==='HIGH')high++;else if(r==='MEDIUM')med++;else low++;
119
+ });
120
+ document.getElementById('cnt-critical').textContent=String(crit).padStart(2,'0');
121
+ document.getElementById('cnt-high').textContent=String(high).padStart(2,'0');
122
+ document.getElementById('cnt-medium').textContent=String(med).padStart(2,'0');
123
+ document.getElementById('cnt-low').textContent=String(low).padStart(2,'0');
124
+ if(crit>0){document.getElementById('sysStatus').textContent='ELEVATED';document.getElementById('sysStatus').classList.add('text-error','animate-pulse');document.getElementById('sysStatus').classList.remove('text-secondary-fixed-dim');}
125
+ const list=document.getElementById('alertList');
126
+ if(allFrames.length===0){list.innerHTML='<div class="text-center font-display-code text-outline py-12">Run simulation from Live Feed to generate alerts</div>';return;}
127
+ const sorted=[...allFrames].sort((a,b)=>{const o={CRITICAL:0,HIGH:1,MEDIUM:2,LOW:3};return (o[a.risk_level]||3)-(o[b.risk_level]||3);});
128
+ list.innerHTML=sorted.map((f,i)=>renderAlert(f,i)).join('');
129
+ });
130
+ }
131
+ loadAlerts();
132
+ setInterval(loadAlerts,5000);
133
+ </script>
134
+ </body></html>
static/index.html ADDED
@@ -0,0 +1,207 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html class="dark" lang="en"><head>
3
+ <meta charset="utf-8"/>
4
+ <meta content="width=device-width, initial-scale=1.0" name="viewport"/>
5
+ <title>Aegis Drone Command - Live Feed</title>
6
+ <script src="https://cdn.tailwindcss.com?plugins=forms,container-queries"></script>
7
+ <link href="https://fonts.googleapis.com/css2?family=Anton&family=Arimo:wght@400;700&family=JetBrains+Mono:wght@400;500;700&display=swap" rel="stylesheet"/>
8
+ <link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:wght,FILL@100..700,0..1&display=swap" rel="stylesheet"/>
9
+ <script>
10
+ tailwind.config = {darkMode:"class",theme:{extend:{colors:{"surface-tint":"#c6c6cb","on-surface-variant":"#c6c6cb","surface-dim":"#101418","surface-container-highest":"#32353a","surface-container-high":"#272a2f","surface-container-lowest":"#0b0e13","surface-container":"#1d2024","on-error-container":"#ffdad6","on-surface":"#e0e2e8",primary:"#c6c6cb","secondary-fixed":"#72ff70","on-tertiary-container":"#ff5d53","on-primary-container":"#939499",surface:"#101418",tertiary:"#ffb4ab",error:"#ffb4ab",outline:"#909095","tertiary-container":"#620005","primary-fixed":"#e2e2e7","error-container":"#93000a","secondary-container":"#13ff43",background:"#101418","primary-container":"#2b2d31","outline-variant":"#45474a","primary-fixed-dim":"#c6c6cb","tertiary-fixed-dim":"#ffb4ab","on-background":"#e0e2e8","surface-container-low":"#191c20","on-secondary-container":"#007117","surface-bright":"#36393e","secondary-fixed-dim":"#00e639","on-error":"#690005","surface-variant":"#32353a"},borderRadius:{DEFAULT:"0.125rem",lg:"0.25rem",xl:"0.5rem",full:"0.75rem"},spacing:{"bezel-width":"4px","gutter-md":"1.5rem","panel-padding":"2rem","component-gap":"1rem"},fontFamily:{"body-md":["Arimo"],"display-code":["JetBrains Mono"],"headline-lg":["Anton"],"label-caps":["JetBrains Mono"],"headline-md":["Anton"]},fontSize:{"body-md":["16px",{lineHeight:"1.5",fontWeight:"400"}],"display-code":["20px",{lineHeight:"1.2",letterSpacing:"-0.02em",fontWeight:"500"}],"headline-lg":["48px",{lineHeight:"1.1",letterSpacing:"0.05em",fontWeight:"400"}],"label-caps":["14px",{lineHeight:"1.2",fontWeight:"700"}],"headline-md":["32px",{lineHeight:"1.2",fontWeight:"400"}]}}}};
11
+ </script>
12
+ <style>
13
+ .material-symbols-outlined{font-variation-settings:'FILL' 0,'wght' 400,'GRAD' 0,'opsz' 24}
14
+ .material-symbols-outlined.fill{font-variation-settings:'FILL' 1}
15
+ .scanlines{background:linear-gradient(to bottom,rgba(255,255,255,0),rgba(255,255,255,0) 50%,rgba(0,0,0,.3) 50%,rgba(0,0,0,.3));background-size:100% 4px}
16
+ @keyframes flicker{0%{opacity:.95}5%{opacity:.85}10%{opacity:.95}15%{opacity:1}100%{opacity:1}}
17
+ .crt-flicker{animation:flicker .15s infinite alternate}
18
+ .scrollbar-hide::-webkit-scrollbar{display:none}.scrollbar-hide{-ms-overflow-style:none;scrollbar-width:none}
19
+ ::-webkit-scrollbar{width:10px;background:#101418}::-webkit-scrollbar-thumb{background:#32353a;border:1px solid #45474a;border-radius:2px}
20
+ </style>
21
+ </head>
22
+ <body class="bg-surface-dim text-on-surface h-screen overflow-hidden font-body-md" style="background-image:repeating-linear-gradient(90deg,rgba(255,255,255,.01) 0px,rgba(255,255,255,.01) 1px,transparent 1px,transparent 10px),repeating-linear-gradient(0deg,rgba(255,255,255,.01) 0px,rgba(255,255,255,.01) 1px,transparent 1px,transparent 10px)">
23
+
24
+ <script src="/static/shell.js"></script>
25
+
26
+ <!-- MAIN -->
27
+ <main class="ml-60 mt-16 p-panel-padding h-[calc(100vh-4rem)] overflow-y-auto flex flex-col gap-6">
28
+ <div class="max-w-7xl mx-auto w-full flex flex-col gap-8">
29
+
30
+ <!-- LCD Metrics -->
31
+ <section class="bg-surface-container rounded-xl p-4 shadow-[0_15px_30px_rgba(0,0,0,0.8)] border-t border-l border-white/5 border-b-4 border-r-4 border-black/50">
32
+ <div class="flex items-center gap-2 mb-4 px-2">
33
+ <div class="w-2 h-2 rounded-full bg-secondary-fixed-dim shadow-[0_0_8px_#00e639]"></div>
34
+ <h2 class="font-label-caps text-outline uppercase tracking-[0.2em] text-[12px]">Real-Time Metrics Grid</h2>
35
+ </div>
36
+ <div class="grid grid-cols-2 lg:grid-cols-4 gap-4">
37
+ <div class="relative bg-surface-container-lowest border-t-4 border-l-4 border-surface-dim border-b border-r border-surface-bright rounded p-4 overflow-hidden h-36 flex flex-col justify-between">
38
+ <div class="absolute inset-0 bg-gradient-to-br from-white/5 to-transparent pointer-events-none z-20"></div>
39
+ <div class="absolute inset-0 scanlines pointer-events-none z-10 opacity-60"></div>
40
+ <span class="font-label-caps text-on-surface-variant uppercase tracking-widest z-20 relative text-[11px]">Frames Processed</span>
41
+ <div id="lcd-frames" class="font-display-code text-[42px] leading-none text-secondary-fixed-dim font-medium tracking-tighter drop-shadow-[0_0_12px_rgba(0,230,57,0.7)] z-20 relative crt-flicker">0</div>
42
+ </div>
43
+ <div class="relative bg-surface-container-lowest border-t-4 border-l-4 border-surface-dim border-b border-r border-surface-bright rounded p-4 overflow-hidden h-36 flex flex-col justify-between">
44
+ <div class="absolute inset-0 bg-gradient-to-br from-white/5 to-transparent pointer-events-none z-20"></div>
45
+ <div class="absolute inset-0 scanlines pointer-events-none z-10 opacity-60"></div>
46
+ <span class="font-label-caps text-on-surface-variant uppercase tracking-widest z-20 relative text-[11px]">Objects Detected</span>
47
+ <div id="lcd-objects" class="font-display-code text-[42px] leading-none text-tertiary-fixed-dim font-medium tracking-tighter drop-shadow-[0_0_12px_rgba(255,180,171,0.5)] z-20 relative crt-flicker">0</div>
48
+ </div>
49
+ <div class="relative bg-surface-container-lowest border-t-4 border-l-4 border-surface-dim border-b border-r border-surface-bright rounded p-4 overflow-hidden h-36 flex flex-col justify-between">
50
+ <div class="absolute inset-0 bg-gradient-to-br from-white/5 to-transparent pointer-events-none z-20"></div>
51
+ <div class="absolute inset-0 scanlines pointer-events-none z-10 opacity-60"></div>
52
+ <span class="font-label-caps text-on-surface-variant uppercase tracking-widest z-20 relative text-[11px]">Alerts Fired</span>
53
+ <div id="lcd-alerts" class="font-display-code text-[42px] leading-none text-primary-fixed-dim font-medium tracking-tighter drop-shadow-[0_0_10px_rgba(198,198,203,0.5)] z-20 relative crt-flicker">0</div>
54
+ </div>
55
+ <div class="relative bg-[#1a0505] border-t-4 border-l-4 border-surface-dim border-b border-r border-surface-bright rounded p-4 overflow-hidden h-36 flex flex-col justify-between ring-1 ring-error/30">
56
+ <div class="absolute inset-0 scanlines pointer-events-none z-10 opacity-80"></div>
57
+ <div class="absolute top-0 right-0 p-2 z-20 opacity-80 animate-pulse"><span class="material-symbols-outlined text-error text-sm">warning</span></div>
58
+ <span class="font-label-caps text-error uppercase tracking-widest z-20 relative text-[11px] font-bold">Critical Alerts</span>
59
+ <div id="lcd-critical" class="font-display-code text-[56px] leading-none text-error font-bold tracking-tighter drop-shadow-[0_0_15px_rgba(255,180,171,0.9)] z-20 relative crt-flicker">0</div>
60
+ </div>
61
+ </div>
62
+ </section>
63
+
64
+ <!-- Action + Log -->
65
+ <div class="grid grid-cols-1 lg:grid-cols-3 gap-8" style="min-height:420px">
66
+ <!-- Run Simulation Panel -->
67
+ <section class="lg:col-span-1 bg-surface-container rounded-xl p-6 shadow-[0_15px_30px_rgba(0,0,0,0.8)] border-t border-l border-white/5 border-b-4 border-r-4 border-black/50 flex flex-col relative">
68
+ <div class="absolute top-3 left-3 w-3 h-3 rounded-full bg-surface-dim shadow-[inset_0_1px_2px_rgba(0,0,0,0.8)]"></div>
69
+ <div class="absolute top-3 right-3 w-3 h-3 rounded-full bg-surface-dim shadow-[inset_0_1px_2px_rgba(0,0,0,0.8)]"></div>
70
+ <div class="absolute bottom-3 left-3 w-3 h-3 rounded-full bg-surface-dim shadow-[inset_0_1px_2px_rgba(0,0,0,0.8)]"></div>
71
+ <div class="absolute bottom-3 right-3 w-3 h-3 rounded-full bg-surface-dim shadow-[inset_0_1px_2px_rgba(0,0,0,0.8)]"></div>
72
+ <div class="mb-auto text-center mt-4">
73
+ <h3 class="font-headline-md text-on-surface uppercase tracking-widest text-[22px]">Override Matrix</h3>
74
+ <p class="font-label-caps text-outline text-[11px] uppercase mt-2">Auth Required: Lvl 4</p>
75
+ <p id="sim-status" class="font-display-code text-[14px] text-secondary-fixed-dim mt-4 hidden"></p>
76
+ </div>
77
+ <div class="p-6 bg-surface-container-highest rounded-xl border border-surface-variant shadow-[inset_0_10px_20px_rgba(0,0,0,0.8)] mx-auto w-full max-w-[240px] aspect-square flex items-center justify-center relative mt-6">
78
+ <div class="absolute -top-2 -left-2 w-8 h-8 border-t-4 border-l-4 border-surface-container-high rounded-tl-lg"></div>
79
+ <div class="absolute -top-2 -right-2 w-8 h-8 border-t-4 border-r-4 border-surface-container-high rounded-tr-lg"></div>
80
+ <button id="runSimBtn" onclick="runSimulation()" class="relative w-full h-full bg-on-tertiary-container hover:bg-[#ff6d63] active:bg-[#d94a42] transition-all rounded-lg shadow-[0_12px_0_#620005,0_20px_25px_rgba(0,0,0,0.7)] active:shadow-[0_0px_0_#620005,inset_0_8px_15px_rgba(0,0,0,0.6)] active:translate-y-[12px] flex flex-col items-center justify-center gap-2 group">
81
+ <div class="absolute inset-x-2 top-2 h-2 bg-white/20 rounded-t-sm group-active:hidden"></div>
82
+ <span class="material-symbols-outlined text-[48px] text-surface drop-shadow-[0_2px_2px_rgba(255,255,255,0.5)]">power_settings_new</span>
83
+ <span class="font-headline-md text-surface tracking-[0.1em] text-[18px] uppercase leading-tight text-center">Run<br/>Simulation</span>
84
+ </button>
85
+ </div>
86
+ </section>
87
+
88
+ <!-- Terminal Log -->
89
+ <section class="lg:col-span-2 bg-surface-container rounded-xl p-4 shadow-[0_15px_30px_rgba(0,0,0,0.8)] border-t border-l border-white/5 border-b-4 border-r-4 border-black/50 flex flex-col">
90
+ <div class="flex justify-between items-center mb-3 px-2">
91
+ <h3 class="font-label-caps text-outline uppercase tracking-[0.2em] text-[12px]">Event Log Stream</h3>
92
+ <div class="flex gap-2 items-center">
93
+ <div id="rec-dot" class="w-2 h-2 rounded-full bg-surface-dim"></div>
94
+ <span class="font-display-code text-outline text-[12px]">REC</span>
95
+ </div>
96
+ </div>
97
+ <div class="flex-1 bg-[#020406] rounded-md border-8 border-surface-container-highest shadow-[inset_0_10px_20px_rgba(0,0,0,1)] relative overflow-hidden flex flex-col p-4">
98
+ <div class="absolute inset-0 bg-[radial-gradient(ellipse_at_center,transparent_0%,rgba(0,0,0,0.6)_100%)] pointer-events-none z-30"></div>
99
+ <div class="absolute inset-0 bg-gradient-to-b from-white/5 to-transparent h-1/3 pointer-events-none z-30"></div>
100
+ <div class="absolute inset-0 scanlines pointer-events-none z-20 opacity-40"></div>
101
+ <div id="logContainer" class="flex-1 overflow-y-auto z-10 flex flex-col gap-2 pr-2 scrollbar-hide">
102
+ <div class="flex items-start gap-3 font-display-code text-[13px]">
103
+ <div class="mt-1 w-3 h-3 shrink-0 rounded-full bg-surface-variant border border-surface relative"><div class="absolute inset-0 rounded-full bg-secondary-fixed-dim shadow-[0_0_8px_#00e639] scale-75"></div></div>
104
+ <span class="text-surface-tint shrink-0">[BOOT]</span>
105
+ <span class="text-secondary-fixed-dim font-bold shrink-0">SYS_OK:</span>
106
+ <span class="text-on-surface-variant">Aegis Drone Command initialized. Awaiting operator input.</span>
107
+ </div>
108
+ <div id="cursor-blink" class="mt-2 w-3 h-5 bg-secondary-fixed-dim animate-pulse ml-8 shadow-[0_0_8px_#00e639]"></div>
109
+ </div>
110
+ </div>
111
+ </section>
112
+ </div>
113
+ </div>
114
+ </main>
115
+
116
+ <script>
117
+ let simRunning = false;
118
+ function addLog(time, level, code, msg) {
119
+ const c = document.getElementById('logContainer');
120
+ const blink = document.getElementById('cursor-blink');
121
+ if(blink) blink.remove();
122
+ const colors = {ok:'secondary-fixed-dim',warn:'tertiary',error:'error',critical:'error'};
123
+ const color = colors[level]||'secondary-fixed-dim';
124
+ const ledShadow = level==='critical'||level==='error' ? '#ffb4ab' : level==='warn' ? '#ffb4ab' : '#00e639';
125
+ const isCrit = level==='critical'||level==='error';
126
+ const wrapper = document.createElement('div');
127
+ wrapper.className = `flex items-start gap-3 font-display-code text-[13px] ${isCrit?'bg-error/10 p-1 -ml-1 rounded border border-error/20':''}`;
128
+ wrapper.innerHTML = `
129
+ <div class="mt-1 w-3 h-3 shrink-0 rounded-full bg-surface-variant border border-surface relative"><div class="absolute inset-0 rounded-full bg-${color} shadow-[0_0_8px_${ledShadow}] scale-75 ${isCrit?'animate-pulse':''}"></div></div>
130
+ <span class="text-surface-tint shrink-0">[${time}]</span>
131
+ <span class="text-${color} font-bold shrink-0 drop-shadow-[0_0_5px_rgba(0,230,57,0.5)]">${code}</span>
132
+ <span class="text-on-surface-variant ${isCrit?'font-bold text-on-surface':''}">${msg}</span>`;
133
+ c.appendChild(wrapper);
134
+ const newBlink = document.createElement('div');
135
+ newBlink.id = 'cursor-blink';
136
+ newBlink.className = 'mt-2 w-3 h-5 bg-secondary-fixed-dim animate-pulse ml-8 shadow-[0_0_8px_#00e639]';
137
+ c.appendChild(newBlink);
138
+ c.scrollTop = c.scrollHeight;
139
+ }
140
+
141
+ async function runSimulation() {
142
+ if(simRunning) return;
143
+ simRunning = true;
144
+ const btn = document.getElementById('runSimBtn');
145
+ btn.disabled = true;
146
+ btn.style.opacity = '0.5';
147
+ document.getElementById('rec-dot').classList.add('bg-error','shadow-[0_0_8px_#ffb4ab]','animate-pulse');
148
+ document.getElementById('rec-dot').classList.remove('bg-surface-dim');
149
+ const status = document.getElementById('sim-status');
150
+ status.classList.remove('hidden');
151
+ status.textContent = 'CONNECTING...';
152
+ let framesCount=0, objectsCount=0, alertsCount=0, critCount=0;
153
+ try {
154
+ const es = new EventSource('/api/simulate');
155
+ es.addEventListener('status', e => {
156
+ const d = JSON.parse(e.data);
157
+ status.textContent = d.message;
158
+ addLog(new Date().toLocaleTimeString(),'ok','SYS_MSG:',d.message);
159
+ });
160
+ es.addEventListener('frame', e => {
161
+ const f = JSON.parse(e.data);
162
+ framesCount++;
163
+ objectsCount += (f.objects||[]).length;
164
+ document.getElementById('lcd-frames').textContent = framesCount;
165
+ document.getElementById('lcd-objects').textContent = objectsCount;
166
+ const risk = (f.risk_level||'LOW').toUpperCase();
167
+ const lvl = risk==='CRITICAL'?'critical':risk==='HIGH'?'error':risk==='MEDIUM'?'warn':'ok';
168
+ if(risk==='CRITICAL') { critCount++; document.getElementById('lcd-critical').textContent = String(critCount).padStart(2,'0'); }
169
+ if(f.alerts && f.alerts.length>0) { alertsCount+=f.alerts.length; document.getElementById('lcd-alerts').textContent = alertsCount; }
170
+ const objs = (f.objects||[]).join(', ')||'None';
171
+ addLog(f.time, lvl, `FRM_${f.frame_id}:`, `[${f.location}] Risk:${risk} | Objects: ${objs}`);
172
+ if(f.is_suspicious) addLog(f.time,'critical','ALERT!!',f.description||'Suspicious activity detected');
173
+ status.textContent = `Processing frame ${framesCount}/30...`;
174
+ });
175
+ es.addEventListener('complete', e => {
176
+ const d = JSON.parse(e.data);
177
+ addLog(new Date().toLocaleTimeString(),'ok','COMPLETE:',`Simulation done. ${d.total_frames} frames, ${d.total_alerts} alerts.`);
178
+ status.textContent = 'SIMULATION COMPLETE';
179
+ es.close();
180
+ simRunning = false;
181
+ btn.disabled = false;
182
+ btn.style.opacity = '1';
183
+ document.getElementById('rec-dot').classList.remove('animate-pulse');
184
+ });
185
+ es.onerror = () => {
186
+ addLog(new Date().toLocaleTimeString(),'error','ERR:','Connection lost');
187
+ es.close();
188
+ simRunning = false;
189
+ btn.disabled = false;
190
+ btn.style.opacity = '1';
191
+ };
192
+ } catch(err) {
193
+ addLog(new Date().toLocaleTimeString(),'error','ERR:',err.message);
194
+ simRunning = false;
195
+ btn.disabled = false;
196
+ btn.style.opacity = '1';
197
+ }
198
+ }
199
+
200
+ // Load existing state on page load
201
+ fetch('/api/status').then(r=>r.json()).then(d=>{
202
+ document.getElementById('lcd-frames').textContent = d.frames_done||0;
203
+ document.getElementById('lcd-objects').textContent = d.objects_count||0;
204
+ document.getElementById('lcd-alerts').textContent = d.alerts?d.alerts.length:0;
205
+ });
206
+ </script>
207
+ </body></html>
static/query.html ADDED
@@ -0,0 +1,211 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html class="dark" lang="en"><head>
3
+ <meta charset="utf-8"/><meta content="width=device-width, initial-scale=1.0" name="viewport"/>
4
+ <title>Aegis Drone Command - Query Frames</title>
5
+ <script src="https://cdn.tailwindcss.com?plugins=forms,container-queries"></script>
6
+ <link href="https://fonts.googleapis.com/css2?family=Anton&family=Arimo:wght@400;700&family=JetBrains+Mono:wght@400;500;700&display=swap" rel="stylesheet"/>
7
+ <link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:wght,FILL@100..700,0..1&display=swap" rel="stylesheet"/>
8
+ <script>tailwind.config={darkMode:"class",theme:{extend:{colors:{"surface-tint":"#c6c6cb","on-surface-variant":"#c6c6cb","surface-dim":"#101418","surface-container-highest":"#32353a","surface-container-high":"#272a2f","surface-container-lowest":"#0b0e13","surface-container":"#1d2024","on-error-container":"#ffdad6","on-surface":"#e0e2e8",primary:"#c6c6cb","secondary-fixed":"#72ff70","on-tertiary-container":"#ff5d53","on-primary-container":"#939499",surface:"#101418",tertiary:"#ffb4ab",error:"#ffb4ab",outline:"#909095","error-container":"#93000a",background:"#101418","primary-container":"#2b2d31","outline-variant":"#45474a","primary-fixed-dim":"#c6c6cb","tertiary-fixed-dim":"#ffb4ab","on-background":"#e0e2e8","surface-container-low":"#191c20","on-secondary-container":"#007117","surface-bright":"#36393e","secondary-fixed-dim":"#00e639","on-error":"#690005","surface-variant":"#32353a"},borderRadius:{DEFAULT:"0.125rem",lg:"0.25rem",xl:"0.5rem",full:"0.75rem"},spacing:{"gutter-md":"1.5rem","panel-padding":"2rem","component-gap":"1rem"},fontFamily:{"body-md":["Arimo"],"display-code":["JetBrains Mono"],"headline-lg":["Anton"],"label-caps":["JetBrains Mono"],"headline-md":["Anton"]},fontSize:{"body-md":["16px",{lineHeight:"1.5",fontWeight:"400"}],"display-code":["20px",{lineHeight:"1.2",letterSpacing:"-0.02em",fontWeight:"500"}],"headline-lg":["48px",{lineHeight:"1.1",letterSpacing:"0.05em",fontWeight:"400"}],"label-caps":["14px",{lineHeight:"1.2",fontWeight:"700"}],"headline-md":["32px",{lineHeight:"1.2",fontWeight:"400"}]}}}};</script>
9
+ <style>::-webkit-scrollbar{width:10px;background:#101418}::-webkit-scrollbar-thumb{background:#32353a;border:1px solid #45474a;border-radius:2px}</style>
10
+ </head>
11
+ <body class="bg-background text-on-surface font-body-md overflow-hidden h-screen">
12
+ <script src="/static/shell.js"></script>
13
+
14
+ <main class="ml-60 mt-16 h-[calc(100vh-4rem)] p-panel-padding overflow-y-auto bg-background relative z-0">
15
+ <header class="mb-6 flex justify-between items-end border-b-2 border-surface-dim pb-4">
16
+ <div><h1 class="font-headline-lg text-[28px] text-primary uppercase tracking-wide">Tactical Frame Interrogation</h1>
17
+ <p class="font-display-code text-outline mt-1 text-[13px]">SYS.OP.MODE // DEEP_SCAN_ACTIVE</p></div>
18
+ <div class="flex items-center gap-2 bg-surface-container-lowest border border-surface-dim p-2 rounded shadow-[inset_0_2px_4px_rgba(0,0,0,0.5)]">
19
+ <div class="w-2 h-2 rounded-full bg-secondary-fixed-dim shadow-[0_0_8px_#00e639] animate-pulse"></div>
20
+ <span class="font-label-caps text-[11px] text-secondary-fixed-dim">DATA LINK STABLE</span>
21
+ </div>
22
+ </header>
23
+ <div class="grid lg:grid-cols-12 gap-6">
24
+ <!-- Left Panel -->
25
+ <div class="lg:col-span-4 flex flex-col gap-5">
26
+ <!-- Query Input -->
27
+ <section class="bg-surface-container border border-surface-dim rounded-lg p-5 relative shadow-[0_8px_16px_rgba(0,0,0,0.6),inset_0_2px_0_rgba(255,255,255,0.05)]">
28
+ <div class="absolute w-2 h-2 rounded-full bg-surface-dim shadow-[inset_0_1px_1px_rgba(255,255,255,0.2)] top-3 left-3"></div>
29
+ <div class="absolute w-2 h-2 rounded-full bg-surface-dim shadow-[inset_0_1px_1px_rgba(255,255,255,0.2)] top-3 right-3"></div>
30
+ <h3 class="font-label-caps text-[12px] text-outline mb-3 flex items-center gap-2 border-b border-surface-dim pb-2"><span class="material-symbols-outlined text-[16px]">search</span> PARAMETER INPUT</h3>
31
+ <div class="space-y-4">
32
+ <div class="flex flex-col gap-1.5">
33
+ <label class="font-label-caps text-[11px] text-primary-fixed-dim">Entity Designation</label>
34
+ <div class="relative"><input id="searchInput" class="w-full bg-surface-container-lowest text-secondary-fixed-dim font-display-code p-2.5 border-t-2 border-l-2 border-black border-b border-r border-white/10 rounded outline-none focus:shadow-[inset_0_0_15px_rgba(0,230,57,0.15)] text-[14px]" type="text" placeholder="e.g. truck, person"/><span class="absolute right-3 top-2.5 material-symbols-outlined text-outline text-[18px]">qr_code_scanner</span></div>
35
+ </div>
36
+ <div class="grid grid-cols-2 gap-3">
37
+ <div class="flex flex-col gap-1.5">
38
+ <label class="font-label-caps text-[11px] text-primary-fixed-dim">Risk Level</label>
39
+ <div class="relative bg-surface-container-lowest border-t-2 border-l-2 border-black border-b border-r border-white/10 rounded">
40
+ <select id="riskFilter" class="w-full bg-transparent text-on-surface font-display-code p-2.5 outline-none appearance-none cursor-pointer text-[13px]"><option value="">ALL LEVELS</option><option value="CRITICAL">CRITICAL</option><option value="HIGH">HIGH</option><option value="MEDIUM">MEDIUM</option><option value="LOW">LOW</option></select>
41
+ <span class="absolute right-2 top-2.5 material-symbols-outlined text-outline pointer-events-none text-[16px]">arrow_drop_down</span></div>
42
+ </div>
43
+ <div class="flex flex-col gap-1.5">
44
+ <label class="font-label-caps text-[11px] text-primary-fixed-dim">Timeframe</label>
45
+ <div class="relative bg-surface-container-lowest border-t-2 border-l-2 border-black border-b border-r border-white/10 rounded">
46
+ <select id="timeFilter" class="w-full bg-transparent text-on-surface font-display-code p-2.5 outline-none appearance-none cursor-pointer text-[13px]"><option value="any">ALL TIME</option><option value="after_hours">AFTER HRS</option><option value="business">BUSINESS</option></select>
47
+ <span class="absolute right-2 top-2.5 material-symbols-outlined text-outline pointer-events-none text-[16px]">arrow_drop_down</span></div>
48
+ </div>
49
+ </div>
50
+ </div>
51
+ </section>
52
+ <!-- Threat Filter -->
53
+ <section class="bg-surface-container border border-surface-dim rounded-lg p-5 relative shadow-[0_8px_16px_rgba(0,0,0,0.6),inset_0_2px_0_rgba(255,255,255,0.05)]">
54
+ <h3 class="font-label-caps text-[12px] text-outline mb-3 flex items-center gap-2 border-b border-surface-dim pb-2"><span class="material-symbols-outlined text-[16px]">policy</span> THREAT FILTER</h3>
55
+ <div class="flex flex-col gap-3 bg-surface-dim p-3 rounded border border-surface shadow-[inset_0_4px_8px_rgba(0,0,0,0.8)]">
56
+ <button onclick="setFilter('CRITICAL')" data-level="CRITICAL" class="threat-filter-btn relative w-full bg-surface-container-high text-on-surface font-label-caps text-[12px] px-3 py-2.5 border-t border-l border-white/10 border-b-2 border-r-2 border-black shadow-[0_3px_0_#0b0e13] active:translate-y-[3px] active:shadow-none transition-all flex items-center justify-between">
57
+ <div class="flex items-center gap-2"><div class="filter-led w-3 h-3 rounded-full bg-on-tertiary-container shadow-[0_0_8px_#ff5d53] border border-black/50" style="opacity:0.5"></div><span class="filter-label text-outline">CRITICAL</span></div>
58
+ <span class="filter-status font-display-code text-outline text-[12px]">OFF</span></button>
59
+ <button onclick="setFilter('HIGH')" data-level="HIGH" class="threat-filter-btn relative w-full bg-surface-container-high font-label-caps text-[12px] px-3 py-2.5 border-t border-l border-white/10 border-b-2 border-r-2 border-black shadow-[0_3px_0_#0b0e13] active:translate-y-[3px] active:shadow-none transition-all flex items-center justify-between">
60
+ <div class="flex items-center gap-2"><div class="filter-led w-3 h-3 rounded-full bg-error shadow-[0_0_6px_#ffb4ab] border border-black/50" style="opacity:0.5"></div><span class="filter-label text-outline">ELEVATED</span></div>
61
+ <span class="filter-status font-display-code text-outline text-[12px]">OFF</span></button>
62
+ <button onclick="setFilter('LOW')" data-level="LOW" class="threat-filter-btn relative w-full bg-surface-container-high font-label-caps text-[12px] px-3 py-2.5 border-t border-l border-white/10 border-b-2 border-r-2 border-black shadow-[0_3px_0_#0b0e13] active:translate-y-[3px] active:shadow-none transition-all flex items-center justify-between">
63
+ <div class="flex items-center gap-2"><div class="filter-led w-3 h-3 rounded-full bg-secondary-fixed-dim shadow-[0_0_6px_#00e639] border border-black/50" style="opacity:0.5"></div><span class="filter-label text-outline">NOMINAL</span></div>
64
+ <span class="filter-status font-display-code text-outline text-[12px]">OFF</span></button>
65
+ </div>
66
+ </section>
67
+ <!-- Semantic Search -->
68
+ <section class="bg-surface-container border border-surface-dim rounded-lg p-5 relative shadow-[0_8px_16px_rgba(0,0,0,0.6),inset_0_2px_0_rgba(255,255,255,0.05)]">
69
+ <div class="bg-surface-dim p-3 rounded border-2 border-surface-container-highest shadow-[inset_0_4px_12px_rgba(0,0,0,0.9)] flex flex-col items-center justify-center min-h-[100px] relative overflow-hidden">
70
+ <div class="absolute inset-0 pointer-events-none opacity-20" style="background-image:repeating-linear-gradient(0deg,transparent,transparent 2px,#000 2px,#000 4px)"></div>
71
+ <span class="material-symbols-outlined text-secondary-fixed-dim text-[32px] mb-1 drop-shadow-[0_0_10px_rgba(0,230,57,0.5)] animate-pulse">neurology</span>
72
+ <p class="font-display-code text-secondary-fixed-dim text-center text-[12px] leading-tight">NEURAL SEMANTIC<br/>MATCHING ENGAGED</p>
73
+ </div>
74
+ <input id="semanticInput" class="w-full mt-3 bg-surface-container-lowest text-secondary-fixed-dim font-display-code p-2.5 border-t-2 border-l-2 border-black border-b border-r border-white/10 rounded outline-none text-[13px]" type="text" placeholder="Describe what you're looking for..."/>
75
+ <button onclick="executeQuery()" class="w-full mt-3 bg-primary-container text-on-surface font-headline-md py-3 rounded border-b-4 border-r-4 border-black shadow-[0_4px_0_#0b0e13] active:translate-y-[4px] active:shadow-none transition-all flex items-center justify-center gap-2 text-[16px]">
76
+ <span class="material-symbols-outlined text-[20px]" style="font-variation-settings:'FILL' 1">manage_search</span>EXECUTE QUERY</button>
77
+ </section>
78
+ </div>
79
+ <!-- Right Panel: CRT Viewport -->
80
+ <div class="lg:col-span-8 flex flex-col">
81
+ <div class="bg-surface-container-lowest border-4 border-surface rounded-xl p-2 relative shadow-[0_10px_30px_rgba(0,0,0,0.8)] flex flex-col" style="min-height:600px">
82
+ <div class="flex justify-between items-center bg-surface-dim px-3 py-1.5 border-b border-surface-container z-10">
83
+ <span class="font-display-code text-secondary-fixed-dim text-[12px]">VDP-OUT // CH-01</span>
84
+ <div class="flex gap-2 items-center"><span class="w-2 h-2 rounded-full bg-secondary-fixed-dim animate-pulse"></span><span class="font-display-code text-outline text-[12px]" id="resultCount">0 MATCHES</span></div>
85
+ </div>
86
+ <div class="relative flex-1 bg-[#05080a] overflow-hidden">
87
+ <div class="absolute inset-0 pointer-events-none z-10 opacity-40" style="background-image:repeating-linear-gradient(0deg,transparent,transparent 1px,#000 1px,#000 2px)"></div>
88
+ <div class="absolute inset-0 pointer-events-none z-10 shadow-[inset_0_0_80px_rgba(0,0,0,1)]"></div>
89
+ <div id="resultsGrid" class="absolute inset-0 overflow-y-auto p-4 z-0 space-y-3">
90
+ <div class="text-center font-display-code text-outline py-12 text-[14px]">Enter search parameters and execute query</div>
91
+ </div>
92
+ </div>
93
+ <div class="bg-surface border-t border-surface-container h-5 w-full rounded-b flex items-center justify-center gap-8">
94
+ <div class="w-12 h-1 bg-surface-dim rounded"></div><div class="w-12 h-1 bg-surface-dim rounded"></div>
95
+ </div>
96
+ </div>
97
+ </div>
98
+ </div>
99
+ </main>
100
+ <script>
101
+ document.getElementById('searchInput').addEventListener('keydown',e=>{if(e.key==='Enter')executeQuery()});
102
+ document.getElementById('semanticInput').addEventListener('keydown',e=>{if(e.key==='Enter')executeQuery()});
103
+
104
+ let activeFilter='';
105
+ const filterBtns = document.querySelectorAll('[onclick^="setFilter"]');
106
+
107
+ function setFilter(level){
108
+ activeFilter = activeFilter===level ? '' : level;
109
+ document.getElementById('riskFilter').value = activeFilter;
110
+ // Update filter button visuals
111
+ document.querySelectorAll('.threat-filter-btn').forEach(btn => {
112
+ const btnLevel = btn.getAttribute('data-level');
113
+ const led = btn.querySelector('.filter-led');
114
+ const label = btn.querySelector('.filter-label');
115
+ const status = btn.querySelector('.filter-status');
116
+ if(btnLevel === activeFilter) {
117
+ led.style.opacity = '1';
118
+ label.className = 'filter-label text-on-surface';
119
+ status.textContent = 'ON';
120
+ status.className = 'filter-status font-display-code text-secondary-fixed-dim text-[12px]';
121
+ } else {
122
+ led.style.opacity = '0.5';
123
+ label.className = 'filter-label text-outline';
124
+ status.textContent = 'OFF';
125
+ status.className = 'filter-status font-display-code text-outline text-[12px]';
126
+ }
127
+ });
128
+ executeQuery();
129
+ }
130
+
131
+ function executeQuery(){
132
+ const paramQ = document.getElementById('searchInput').value.trim();
133
+ const semanticQ = document.getElementById('semanticInput').value.trim();
134
+ const risk = document.getElementById('riskFilter').value;
135
+ const time = document.getElementById('timeFilter').value;
136
+
137
+ document.getElementById('resultsGrid').innerHTML='<div class="text-center font-display-code text-secondary-fixed-dim py-12 text-[14px] animate-pulse">SCANNING DATABASE...</div>';
138
+ document.getElementById('resultCount').textContent='SEARCHING...';
139
+
140
+ // Decide which API to use
141
+ if(semanticQ && !paramQ) {
142
+ // Semantic search via ChromaDB
143
+ fetch('/api/semantic?q='+encodeURIComponent(semanticQ))
144
+ .then(r=>r.json())
145
+ .then(d=>{
146
+ let results = d.results||[];
147
+ // Apply client-side risk filter to semantic results
148
+ if(risk) results = results.filter(r=>(r.risk_level||'').toUpperCase()===risk);
149
+ renderResults(results, 'SEMANTIC');
150
+ })
151
+ .catch(()=>{
152
+ // Fallback: try in-memory
153
+ fetchInMemory(semanticQ, risk, time);
154
+ });
155
+ } else {
156
+ // Parameter search via SQLite + in-memory fallback
157
+ fetch(`/api/frames/search?q=${encodeURIComponent(paramQ)}&risk=${encodeURIComponent(risk)}&time_filter=${encodeURIComponent(time)}`)
158
+ .then(r=>r.json())
159
+ .then(d=>{
160
+ let results = d.results||[];
161
+ if(results.length > 0) {
162
+ renderResults(results, 'DB');
163
+ } else {
164
+ // Fallback to in-memory results
165
+ fetchInMemory(paramQ, risk, time);
166
+ }
167
+ })
168
+ .catch(()=>{
169
+ fetchInMemory(paramQ, risk, time);
170
+ });
171
+ }
172
+ }
173
+
174
+ function fetchInMemory(q, risk, time){
175
+ fetch('/api/status').then(r=>r.json()).then(data=>{
176
+ let results=data.results||[];
177
+ if(q) results=results.filter(f=>(f.objects||[]).some(o=>o.toLowerCase().includes(q.toLowerCase()))||(f.description||'').toLowerCase().includes(q.toLowerCase()));
178
+ if(risk) results=results.filter(f=>(f.risk_level||'').toUpperCase()===risk);
179
+ if(time==='after_hours') results=results.filter(f=>{const h=parseInt((f.time||'12').split(':')[0]);return h>=22||h<6});
180
+ else if(time==='business') results=results.filter(f=>{const h=parseInt((f.time||'12').split(':')[0]);return h>=6&&h<22});
181
+ renderResults(results, 'MEMORY');
182
+ });
183
+ }
184
+
185
+ function renderResults(results, source){
186
+ document.getElementById('resultCount').textContent=results.length+' MATCHES';
187
+ if(!results.length){
188
+ document.getElementById('resultsGrid').innerHTML=`<div class="text-center font-display-code text-outline py-12 text-[14px]">
189
+ <span class="material-symbols-outlined text-[32px] block mb-2 text-outline/50">search_off</span>
190
+ No matches found.<br/><span class="text-[12px] text-surface-variant mt-1 block">Run simulation from Live Feed first, then query.</span></div>`;
191
+ return;
192
+ }
193
+ const rc={CRITICAL:'error',HIGH:'tertiary-fixed-dim',MEDIUM:'primary',LOW:'surface-tint'};
194
+ const srcBadge = `<span class="font-label-caps text-[9px] text-secondary-fixed-dim bg-surface-container-lowest px-1 py-0.5 rounded border border-secondary-fixed-dim/20">${source}</span>`;
195
+ document.getElementById('resultsGrid').innerHTML=results.map(f=>{
196
+ const r=(f.risk_level||'LOW').toUpperCase();
197
+ const fid = f.frame_id || f.id || '?';
198
+ const objs = f.objects || [];
199
+ return `<div class="bg-surface-dim border-l-4 border-${rc[r]||'surface-tint'} p-3 hover:bg-surface-container transition-colors cursor-pointer">
200
+ <div class="flex justify-between items-start mb-1"><div class="flex items-center gap-2">
201
+ <span class="font-display-code text-[13px] text-${rc[r]}">FRM-${fid}</span>
202
+ <span class="font-label-caps text-[10px] text-surface-tint bg-surface-container px-1.5 py-0.5 rounded border border-surface-variant">${f.location||f.sector||''}</span>
203
+ <span class="font-label-caps text-[10px] text-on-surface bg-surface-variant px-1.5 py-0.5 rounded">${r}</span>
204
+ ${srcBadge}
205
+ </div><span class="font-display-code text-[11px] text-outline">${f.time||f.timestamp||''}</span></div>
206
+ <p class="font-display-code text-on-surface-variant text-[12px]">${f.description||''}</p>
207
+ <div class="mt-1.5 flex gap-1.5 flex-wrap">${objs.map(o=>`<span class="font-label-caps text-[10px] bg-surface-container-highest text-secondary-fixed-dim px-1.5 py-0.5 rounded border border-secondary-fixed-dim/30">${o}</span>`).join('')}</div></div>`;
208
+ }).join('');
209
+ }
210
+ </script>
211
+ </body></html>
static/shell.js ADDED
@@ -0,0 +1,309 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Aegis Drone Command — Shared Shell
2
+ const PAGES = [
3
+ { label: 'Live Feed', href: '/' },
4
+ { label: 'Video Analysis', href: '/static/video.html' },
5
+ { label: 'Alerts', href: '/static/alerts.html' },
6
+ { label: 'Query Frames', href: '/static/query.html' },
7
+ { label: 'Daily Summary', href: '/static/summary.html' },
8
+ ];
9
+ const SIDEBAR_LINKS = [
10
+ { icon: 'precision_manufacturing', label: 'Systems', id: 'systems' },
11
+ { icon: 'airplanemode_active', label: 'Drones', id: 'drones' },
12
+ { icon: 'settings_input_component', label: 'Telemetry', id: 'telemetry', active: true },
13
+ { icon: 'view_in_ar', label: 'Payload', id: 'payload' },
14
+ { icon: 'admin_panel_settings', label: 'Security', id: 'security' },
15
+ ];
16
+
17
+ // Panel content for each sidebar item
18
+ const PANEL_DATA = {
19
+ systems: {
20
+ title: 'SYSTEM STATUS',
21
+ icon: 'precision_manufacturing',
22
+ rows: [
23
+ { label: 'Core Engine', val: 'ONLINE', color: 'secondary-fixed-dim' },
24
+ { label: 'GPU Cluster', val: 'ACTIVE', color: 'secondary-fixed-dim' },
25
+ { label: 'Neural Net v4.2', val: 'LOADED', color: 'secondary-fixed-dim' },
26
+ { label: 'Memory Usage', val: '67%', color: 'primary' },
27
+ { label: 'CPU Load', val: '42%', color: 'primary' },
28
+ { label: 'Uptime', val: '14d 07h 33m', color: 'on-surface' },
29
+ { label: 'Last Reboot', val: '2026-04-28', color: 'outline' },
30
+ { label: 'Firmware', val: 'v8.1.4-mil', color: 'outline' },
31
+ ]
32
+ },
33
+ drones: {
34
+ title: 'DRONE FLEET',
35
+ icon: 'airplanemode_active',
36
+ rows: [
37
+ { label: 'Unit-01 (Alpha)', val: 'PATROL', color: 'secondary-fixed-dim' },
38
+ { label: 'Unit-02 (Alpha)', val: 'PATROL', color: 'secondary-fixed-dim' },
39
+ { label: 'Unit-03 (Bravo)', val: 'STANDBY', color: 'primary' },
40
+ { label: 'Unit-04 (Delta)', val: 'ENGAGED', color: 'error' },
41
+ { label: 'Unit-07 (Delta)', val: 'ACTIVE', color: 'secondary-fixed-dim' },
42
+ { label: 'Unit-09 (Charlie)', val: 'CHARGING', color: 'tertiary' },
43
+ { label: 'Unit-12 (Bravo)', val: 'MAINTENANCE', color: 'tertiary' },
44
+ { label: 'Unit-15 (Alpha)', val: 'PATROL', color: 'secondary-fixed-dim' },
45
+ ]
46
+ },
47
+ telemetry: {
48
+ title: 'TELEMETRY FEED',
49
+ icon: 'settings_input_component',
50
+ rows: [
51
+ { label: 'Altitude', val: '120m AGL', color: 'secondary-fixed-dim' },
52
+ { label: 'Ground Speed', val: '14.2 m/s', color: 'on-surface' },
53
+ { label: 'Heading', val: '247° WSW', color: 'on-surface' },
54
+ { label: 'Battery', val: '78%', color: 'secondary-fixed-dim' },
55
+ { label: 'Signal Strength', val: '-42 dBm', color: 'secondary-fixed-dim' },
56
+ { label: 'GPS Satellites', val: '14 locked', color: 'on-surface' },
57
+ { label: 'Wind Speed', val: '8.3 kts NE', color: 'primary' },
58
+ { label: 'Temperature', val: '22.4°C', color: 'outline' },
59
+ ]
60
+ },
61
+ payload: {
62
+ title: 'PAYLOAD STATUS',
63
+ icon: 'view_in_ar',
64
+ rows: [
65
+ { label: 'Camera (RGB)', val: 'ACTIVE', color: 'secondary-fixed-dim' },
66
+ { label: 'Camera (IR)', val: 'STANDBY', color: 'primary' },
67
+ { label: 'LIDAR Module', val: 'SCANNING', color: 'secondary-fixed-dim' },
68
+ { label: 'Spotlight', val: 'OFF', color: 'outline' },
69
+ { label: 'Speaker Array', val: 'ARMED', color: 'tertiary' },
70
+ { label: 'Gas Sensor', val: 'NOMINAL', color: 'secondary-fixed-dim' },
71
+ { label: 'Storage Used', val: '34.2 GB', color: 'on-surface' },
72
+ { label: 'Transmission', val: 'ENCRYPTED', color: 'secondary-fixed-dim' },
73
+ ]
74
+ },
75
+ security: {
76
+ title: 'SECURITY CONFIG',
77
+ icon: 'admin_panel_settings',
78
+ rows: [
79
+ { label: 'Auth Level', val: 'LEVEL 4', color: 'secondary-fixed-dim' },
80
+ { label: 'Encryption', val: 'AES-256', color: 'secondary-fixed-dim' },
81
+ { label: 'Firewall', val: 'ACTIVE', color: 'secondary-fixed-dim' },
82
+ { label: 'IDS Status', val: 'MONITORING', color: 'secondary-fixed-dim' },
83
+ { label: 'Last Breach', val: 'NONE', color: 'outline' },
84
+ { label: 'Perimeter', val: 'SECURED', color: 'secondary-fixed-dim' },
85
+ { label: 'Access Tokens', val: '3 active', color: 'on-surface' },
86
+ { label: 'Audit Log', val: '1,247 entries', color: 'outline' },
87
+ ]
88
+ },
89
+ logs: {
90
+ title: 'SYSTEM LOGS',
91
+ icon: 'list_alt',
92
+ rows: [
93
+ { label: '20:14:22', val: 'SYS_BOOT complete', color: 'secondary-fixed-dim' },
94
+ { label: '20:14:25', val: 'Network link established', color: 'secondary-fixed-dim' },
95
+ { label: '20:15:01', val: 'Auth token refreshed', color: 'on-surface' },
96
+ { label: '20:15:44', val: 'Drone fleet sync OK', color: 'secondary-fixed-dim' },
97
+ { label: '20:16:12', val: 'Patrol route loaded', color: 'on-surface' },
98
+ { label: '20:18:03', val: 'Sensor calibration done', color: 'on-surface' },
99
+ { label: '20:19:30', val: 'Operator login: Unit 07', color: 'primary' },
100
+ { label: '20:20:00', val: 'System nominal', color: 'secondary-fixed-dim' },
101
+ ]
102
+ },
103
+ terminal: {
104
+ title: 'TERMINAL',
105
+ icon: 'terminal',
106
+ rows: [
107
+ { label: '$', val: 'aegis status --all', color: 'secondary-fixed-dim' },
108
+ { label: '>', val: 'All subsystems operational', color: 'on-surface' },
109
+ { label: '$', val: 'drone list --active', color: 'secondary-fixed-dim' },
110
+ { label: '>', val: '7 units online, 1 maintenance', color: 'on-surface' },
111
+ { label: '$', val: 'alert count --today', color: 'secondary-fixed-dim' },
112
+ { label: '>', val: '3 alerts (0 critical)', color: 'on-surface' },
113
+ { label: '$', val: 'uptime', color: 'secondary-fixed-dim' },
114
+ { label: '>', val: '14 days, 7 hours, 33 minutes', color: 'on-surface' },
115
+ ]
116
+ },
117
+ settings: {
118
+ title: 'SETTINGS',
119
+ icon: 'settings',
120
+ rows: [
121
+ { label: 'Theme', val: 'DARK MODE', color: 'on-surface' },
122
+ { label: 'Language', val: 'ENGLISH', color: 'on-surface' },
123
+ { label: 'Notifications', val: 'ENABLED', color: 'secondary-fixed-dim' },
124
+ { label: 'Sound Alerts', val: 'ON', color: 'secondary-fixed-dim' },
125
+ { label: 'Auto-Refresh', val: '5 SEC', color: 'on-surface' },
126
+ { label: 'Map Overlay', val: 'TACTICAL', color: 'on-surface' },
127
+ { label: 'Data Retention', val: '90 DAYS', color: 'outline' },
128
+ { label: 'API Version', val: 'v2.4.1', color: 'outline' },
129
+ ]
130
+ },
131
+ };
132
+
133
+ let activePanelId = null;
134
+
135
+ function isActive(href) {
136
+ const p = window.location.pathname;
137
+ if (href === '/') return p === '/' || p === '/static/index.html';
138
+ return p === href;
139
+ }
140
+
141
+ function openPanel(id) {
142
+ const wasOpen = activePanelId === id;
143
+ closePanel();
144
+ if (wasOpen) return;
145
+ activePanelId = id;
146
+ updateSidebarHighlight(id);
147
+ const data = PANEL_DATA[id];
148
+ if (!data) return;
149
+ const overlay = document.createElement('div');
150
+ overlay.id = 'aegis-panel-overlay';
151
+ overlay.className = 'fixed inset-0 bg-black/40 z-[55]';
152
+ overlay.onclick = () => closePanel();
153
+ document.body.appendChild(overlay);
154
+
155
+ const panel = document.createElement('div');
156
+ panel.id = 'aegis-panel';
157
+ panel.className = 'fixed top-16 left-60 w-80 bg-surface-container-high border-2 border-surface-dim shadow-[0_10px_40px_rgba(0,0,0,0.9)] z-[60] flex flex-col overflow-hidden';
158
+ panel.style.maxHeight = 'calc(100vh - 5rem)';
159
+ panel.style.animation = 'panelSlide .2s ease-out';
160
+
161
+ let rowsHtml = data.rows.map(r =>
162
+ `<div class="flex justify-between items-center py-2 px-4 border-b border-surface-dim/50 hover:bg-surface-container transition-colors">
163
+ <span class="font-display-code text-[12px] text-on-surface-variant">${r.label}</span>
164
+ <span class="font-label-caps text-[11px] text-${r.color}">${r.val}</span>
165
+ </div>`
166
+ ).join('');
167
+
168
+ panel.innerHTML = `
169
+ <div class="bg-surface-container-highest px-4 py-3 border-b-2 border-surface-dim flex justify-between items-center">
170
+ <div class="flex items-center gap-2">
171
+ <span class="material-symbols-outlined text-secondary-fixed-dim text-[18px]" style="font-variation-settings:'FILL' 1">${data.icon}</span>
172
+ <span class="font-label-caps text-[12px] text-on-surface uppercase tracking-widest">${data.title}</span>
173
+ </div>
174
+ <button onclick="closePanel()" class="text-outline hover:text-on-surface p-1 rounded hover:bg-surface-bright transition-colors">
175
+ <span class="material-symbols-outlined text-[16px]">close</span>
176
+ </button>
177
+ </div>
178
+ <div class="flex-1 overflow-y-auto">${rowsHtml}</div>
179
+ <div class="bg-surface-dim px-4 py-2 border-t border-surface-container flex items-center justify-between">
180
+ <div class="flex items-center gap-1.5">
181
+ <div class="w-2 h-2 rounded-full bg-secondary-fixed-dim shadow-[0_0_6px_#00e639] animate-pulse"></div>
182
+ <span class="font-display-code text-[10px] text-outline">LIVE DATA</span>
183
+ </div>
184
+ <span class="font-display-code text-[10px] text-outline">${new Date().toLocaleTimeString('en-US',{hour12:false})} UTC</span>
185
+ </div>`;
186
+ document.body.appendChild(panel);
187
+ }
188
+
189
+ function closePanel() {
190
+ const p = document.getElementById('aegis-panel');
191
+ const o = document.getElementById('aegis-panel-overlay');
192
+ if (p) p.remove();
193
+ if (o) o.remove();
194
+ activePanelId = null;
195
+ updateSidebarHighlight(null);
196
+ }
197
+
198
+ function updateSidebarHighlight(activeId) {
199
+ const links = document.querySelectorAll('#aegis-sidebar nav a[data-panel]');
200
+ links.forEach(a => {
201
+ const id = a.getAttribute('data-panel');
202
+ if (id === activeId) {
203
+ a.className = 'flex items-center gap-3 px-3 py-2.5 rounded font-label-caps text-[12px] bg-surface-container-highest text-secondary-fixed shadow-[inset_0_2px_4px_rgba(0,0,0,0.8)] border-l-4 border-secondary-fixed uppercase';
204
+ a.querySelector('.material-symbols-outlined').style.fontVariationSettings = "'FILL' 1";
205
+ } else {
206
+ a.className = 'flex items-center gap-3 px-3 py-2.5 rounded font-label-caps text-[12px] text-outline hover:text-on-surface-variant hover:bg-surface-container-high transition-all active:scale-95 uppercase';
207
+ a.querySelector('.material-symbols-outlined').style.fontVariationSettings = "'FILL' 0";
208
+ }
209
+ });
210
+ }
211
+
212
+ function emergencyStop(el) {
213
+ const btn = el ? el.closest('button') : (window.event ? window.event.target.closest('button') : null);
214
+ if (!btn || btn.dataset.stopping) return;
215
+ btn.dataset.stopping = '1';
216
+ btn.classList.add('translate-y-[3px]');
217
+ btn.innerHTML = `<span class="material-symbols-outlined text-[16px] animate-spin" style="font-variation-settings:'FILL' 1">sync</span>STOPPING ALL...`;
218
+ setTimeout(() => {
219
+ btn.innerHTML = `<span class="material-symbols-outlined text-[16px]" style="font-variation-settings:'FILL' 1">check_circle</span>ALL HALTED`;
220
+ btn.classList.remove('bg-error'); btn.classList.add('bg-secondary-fixed-dim','text-black');
221
+ setTimeout(() => {
222
+ btn.classList.remove('translate-y-[3px]','bg-secondary-fixed-dim','text-black');
223
+ btn.classList.add('bg-error');
224
+ btn.innerHTML = `<span class="material-symbols-outlined text-[16px]" style="font-variation-settings:'FILL' 1">warning</span>EMERGENCY STOP`;
225
+ delete btn.dataset.stopping;
226
+ }, 2000);
227
+ }, 1500);
228
+ }
229
+
230
+ function buildTopNav() {
231
+ const nav = document.createElement('header');
232
+ nav.id = 'aegis-topnav';
233
+ nav.className = 'bg-surface-container-highest fixed w-full top-0 border-b-4 border-surface-dim shadow-[0_4px_10px_rgba(0,0,0,0.8)] flex justify-between items-center px-6 h-16 z-50';
234
+ const brand = document.createElement('div');
235
+ brand.className = 'flex items-center gap-3 shrink-0';
236
+ brand.innerHTML = `<span class="material-symbols-outlined text-secondary-fixed-dim text-[22px]" style="font-variation-settings:'FILL' 1">security</span>
237
+ <span class="font-headline-lg text-on-surface uppercase tracking-widest text-[20px]">Aegis Drone Command</span>`;
238
+ const tabs = document.createElement('nav');
239
+ tabs.className = 'hidden md:flex items-center h-full gap-1 ml-8';
240
+ PAGES.forEach(p => {
241
+ const a = document.createElement('a');
242
+ a.href = p.href;
243
+ a.textContent = p.label;
244
+ a.className = isActive(p.href)
245
+ ? 'h-full flex items-center px-4 text-secondary-fixed-dim border-b-2 border-secondary-fixed-dim font-bold shadow-[0_0_12px_rgba(0,230,57,0.3)] bg-surface-container/50 font-label-caps text-[13px] uppercase tracking-wider'
246
+ : 'h-full flex items-center px-4 text-on-surface-variant hover:bg-surface-bright hover:text-on-surface transition-colors font-label-caps text-[13px] uppercase tracking-wider';
247
+ tabs.appendChild(a);
248
+ });
249
+ const right = document.createElement('div');
250
+ right.className = 'flex items-center gap-2 shrink-0';
251
+ right.innerHTML = `<button onclick="openPanel('settings')" class="p-2 hover:bg-surface-bright rounded transition-colors text-primary"><span class="material-symbols-outlined text-[20px]">settings</span></button>
252
+ <button onclick="emergencyStop.call(this)" class="p-2 hover:bg-surface-bright rounded transition-colors text-error"><span class="material-symbols-outlined text-[20px]">power_settings_new</span></button>`;
253
+ nav.appendChild(brand); nav.appendChild(tabs); nav.appendChild(right);
254
+ return nav;
255
+ }
256
+
257
+ function buildSidebar() {
258
+ const aside = document.createElement('aside');
259
+ aside.id = 'aegis-sidebar';
260
+ aside.className = 'bg-surface-container-low fixed left-0 h-full w-60 border-r-4 border-surface-dim shadow-[10px_0_20px_rgba(0,0,0,0.5)] flex flex-col pt-16 pb-4 z-40';
261
+ const profile = document.createElement('div');
262
+ profile.className = 'px-5 py-5 border-b border-surface-variant flex items-center gap-3';
263
+ profile.innerHTML = `<div class="w-10 h-10 rounded bg-surface-container-highest border border-surface-variant flex items-center justify-center shadow-[inset_0_2px_4px_rgba(0,0,0,0.5)]">
264
+ <span class="material-symbols-outlined text-outline text-[20px]">person</span></div>
265
+ <div><div class="font-headline-md text-[16px] text-on-surface leading-none uppercase">Unit 07</div>
266
+ <div class="font-label-caps text-[10px] text-on-surface-variant mt-1">Sector Delta</div></div>`;
267
+ aside.appendChild(profile);
268
+ const navCont = document.createElement('nav');
269
+ navCont.className = 'flex-1 py-4 flex flex-col gap-1 px-3 overflow-y-auto';
270
+ SIDEBAR_LINKS.forEach(l => {
271
+ const a = document.createElement('a');
272
+ a.href = '#'; a.setAttribute('data-panel', l.id);
273
+ a.onclick = (e) => { e.preventDefault(); openPanel(l.id); };
274
+ a.className = 'flex items-center gap-3 px-3 py-2.5 rounded font-label-caps text-[12px] text-outline hover:text-on-surface-variant hover:bg-surface-container-high transition-all active:scale-95 uppercase';
275
+ a.innerHTML = `<span class="material-symbols-outlined text-[18px]">${l.icon}</span>${l.label}`;
276
+ navCont.appendChild(a);
277
+ });
278
+ aside.appendChild(navCont);
279
+ const footer = document.createElement('div');
280
+ footer.className = 'mt-auto px-3 flex flex-col gap-2';
281
+ footer.innerHTML = `<button onclick="emergencyStop(this)" class="w-full py-2.5 bg-error text-on-error font-label-caps text-[12px] uppercase rounded shadow-[0_3px_0_#93000a] active:shadow-none active:translate-y-[3px] border border-red-400/30 flex items-center justify-center gap-2 transition-all">
282
+ <span class="material-symbols-outlined text-[16px]" style="font-variation-settings:'FILL' 1">warning</span>EMERGENCY STOP</button>
283
+ <div class="h-px bg-surface-container-highest my-1"></div>
284
+ <div class="flex gap-1">
285
+ <a href="#" onclick="event.preventDefault();openPanel('logs')" class="flex items-center gap-2 px-3 py-2 rounded font-label-caps text-[11px] text-outline hover:text-on-surface-variant hover:bg-surface-container-high flex-1"><span class="material-symbols-outlined text-[16px]">list_alt</span>Logs</a>
286
+ <a href="#" onclick="event.preventDefault();openPanel('terminal')" class="flex items-center gap-2 px-3 py-2 rounded font-label-caps text-[11px] text-outline hover:text-on-surface-variant hover:bg-surface-container-high flex-1"><span class="material-symbols-outlined text-[16px]">terminal</span>Terminal</a>
287
+ </div>`;
288
+ aside.appendChild(footer);
289
+ return aside;
290
+ }
291
+
292
+ // CSS animation
293
+ function injectStyles() {
294
+ if (document.getElementById('aegis-shell-styles')) return;
295
+ const s = document.createElement('style');
296
+ s.id = 'aegis-shell-styles';
297
+ s.textContent = `@keyframes panelSlide{from{opacity:0;transform:translateY(-10px)}to{opacity:1;transform:translateY(0)}}`;
298
+ document.head.appendChild(s);
299
+ }
300
+
301
+ function injectShell() {
302
+ document.querySelectorAll('#aegis-topnav, #aegis-sidebar, #aegis-panel, #aegis-panel-overlay').forEach(e => e.remove());
303
+ injectStyles();
304
+ document.body.prepend(buildSidebar());
305
+ document.body.prepend(buildTopNav());
306
+ }
307
+
308
+ if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', injectShell);
309
+ else injectShell();
static/summary.html ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html class="dark" lang="en"><head>
3
+ <meta charset="utf-8"/><meta content="width=device-width, initial-scale=1.0" name="viewport"/>
4
+ <title>Aegis Drone Command - Daily Summary</title>
5
+ <script src="https://cdn.tailwindcss.com?plugins=forms,container-queries"></script>
6
+ <link href="https://fonts.googleapis.com/css2?family=Anton&family=Arimo:wght@400;700&family=JetBrains+Mono:wght@400;500;700&display=swap" rel="stylesheet"/>
7
+ <link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:wght,FILL@100..700,0..1&display=swap" rel="stylesheet"/>
8
+ <script>tailwind.config={darkMode:"class",theme:{extend:{colors:{"surface-tint":"#c6c6cb","on-surface-variant":"#c6c6cb","surface-dim":"#101418","surface-container-highest":"#32353a","surface-container-high":"#272a2f","surface-container-lowest":"#0b0e13","surface-container":"#1d2024","on-error-container":"#ffdad6","on-surface":"#e0e2e8",primary:"#c6c6cb","secondary-fixed":"#72ff70","on-tertiary-container":"#ff5d53","on-primary-container":"#939499",surface:"#101418",tertiary:"#ffb4ab",error:"#ffb4ab",outline:"#909095","error-container":"#93000a",background:"#101418","primary-container":"#2b2d31","outline-variant":"#45474a","primary-fixed-dim":"#c6c6cb","tertiary-fixed-dim":"#ffb4ab","on-background":"#e0e2e8","surface-container-low":"#191c20","on-secondary-container":"#007117","surface-bright":"#36393e","secondary-fixed-dim":"#00e639","on-error":"#690005","surface-variant":"#32353a"},borderRadius:{DEFAULT:"0.125rem",lg:"0.25rem",xl:"0.5rem",full:"0.75rem"},spacing:{"gutter-md":"1.5rem","panel-padding":"2rem","component-gap":"1rem"},fontFamily:{"body-md":["Arimo"],"display-code":["JetBrains Mono"],"headline-lg":["Anton"],"label-caps":["JetBrains Mono"],"headline-md":["Anton"]},fontSize:{"body-md":["16px",{lineHeight:"1.5",fontWeight:"400"}],"display-code":["20px",{lineHeight:"1.2",letterSpacing:"-0.02em",fontWeight:"500"}],"headline-lg":["48px",{lineHeight:"1.1",letterSpacing:"0.05em",fontWeight:"400"}],"label-caps":["14px",{lineHeight:"1.2",fontWeight:"700"}],"headline-md":["32px",{lineHeight:"1.2",fontWeight:"400"}]}}}};</script>
9
+ <style>
10
+ .scanlines{background:linear-gradient(to bottom,rgba(255,255,255,0),rgba(255,255,255,0) 50%,rgba(0,0,0,.2) 50%,rgba(0,0,0,.2));background-size:100% 4px}
11
+ .metal-texture{background-image:radial-gradient(circle,#1d2024 1px,transparent 1px);background-size:4px 4px}
12
+ </style>
13
+ </head>
14
+ <body class="bg-background text-on-background font-body-md min-h-screen overflow-hidden">
15
+ <script src="/static/shell.js"></script>
16
+
17
+ <main class="ml-60 mt-16 flex-1 p-panel-padding bg-surface metal-texture overflow-y-auto relative shadow-[inset_0_10px_30px_rgba(0,0,0,0.8)] h-[calc(100vh-4rem)]">
18
+ <div class="max-w-5xl mx-auto bg-surface-container border-4 border-surface-dim rounded-lg p-5 shadow-[0_10px_30px_rgba(0,0,0,0.9)] relative">
19
+ <!-- Corner Screws -->
20
+ <div class="absolute top-2 left-2 w-2.5 h-2.5 rounded-full bg-outline shadow-[inset_1px_1px_2px_rgba(255,255,255,0.4),inset_-1px_-1px_2px_rgba(0,0,0,0.8)] flex items-center justify-center"><div class="w-1.5 h-0.5 bg-surface-dim rotate-45"></div></div>
21
+ <div class="absolute top-2 right-2 w-2.5 h-2.5 rounded-full bg-outline shadow-[inset_1px_1px_2px_rgba(255,255,255,0.4),inset_-1px_-1px_2px_rgba(0,0,0,0.8)]"></div>
22
+ <div class="absolute bottom-2 left-2 w-2.5 h-2.5 rounded-full bg-outline shadow-[inset_1px_1px_2px_rgba(255,255,255,0.4),inset_-1px_-1px_2px_rgba(0,0,0,0.8)]"></div>
23
+ <div class="absolute bottom-2 right-2 w-2.5 h-2.5 rounded-full bg-outline shadow-[inset_1px_1px_2px_rgba(255,255,255,0.4),inset_-1px_-1px_2px_rgba(0,0,0,0.8)]"></div>
24
+ <!-- Screen -->
25
+ <div class="bg-surface-container-lowest border-2 border-surface-dim rounded shadow-[inset_0_10px_20px_rgba(0,0,0,1)] relative overflow-hidden flex flex-col min-h-[600px]">
26
+ <div class="absolute inset-0 scanlines pointer-events-none z-10 opacity-40"></div>
27
+ <div class="absolute inset-0 bg-gradient-to-br from-white/5 to-transparent pointer-events-none z-20"></div>
28
+ <div class="p-6 relative z-30 flex-1 flex flex-col">
29
+ <!-- Screen Header -->
30
+ <div class="border-b-2 border-outline-variant pb-3 mb-5 flex justify-between items-end">
31
+ <div><h1 class="font-headline-lg text-[28px] text-on-surface uppercase tracking-wider">Daily Operations Summary</h1>
32
+ <p id="logDate" class="font-display-code text-secondary-fixed-dim text-[13px] mt-1">LOG DATE: -- | AUTHORIZATION: LEVEL 4</p></div>
33
+ <div class="text-right"><span class="font-label-caps text-[11px] text-on-surface-variant block mb-1">System Status</span>
34
+ <div class="flex items-center justify-end gap-2 text-secondary-fixed font-display-code">
35
+ <div class="w-3 h-3 rounded-full bg-secondary-fixed shadow-[0_0_10px_#72ff70] animate-pulse"></div><span class="text-[16px]">NOMINAL</span></div></div>
36
+ </div>
37
+ <!-- Bento Grid -->
38
+ <div class="grid grid-cols-12 gap-5 flex-1">
39
+ <!-- Left Col -->
40
+ <div class="col-span-5 flex flex-col gap-5">
41
+ <div class="bg-surface-container border border-outline-variant p-4 shadow-inner">
42
+ <h2 class="font-label-caps text-[12px] text-primary uppercase border-b border-surface-dim pb-2 mb-3 flex items-center gap-2"><span class="material-symbols-outlined text-[16px]">bar_chart</span>Fleet Overview</h2>
43
+ <div class="space-y-3">
44
+ <div class="flex justify-between items-end border-b border-surface-dim/50 pb-2"><span class="font-display-code text-on-surface-variant text-[13px]">Frames Processed</span><span id="statFrames" class="font-headline-md text-on-surface text-[20px]">--</span></div>
45
+ <div class="flex justify-between items-end border-b border-surface-dim/50 pb-2"><span class="font-display-code text-on-surface-variant text-[13px]">Objects Detected</span><span id="statObjects" class="font-headline-md text-on-surface text-[20px]">--</span></div>
46
+ <div class="flex justify-between items-end"><span class="font-display-code text-on-surface-variant text-[13px]">Alerts Fired</span><span id="statAlerts" class="font-headline-md text-error text-[20px]">--</span></div>
47
+ </div>
48
+ </div>
49
+ <!-- Generate button -->
50
+ <div class="bg-surface-container border border-outline-variant p-4 shadow-inner">
51
+ <button onclick="generateSummary()" id="genBtn" class="w-full bg-primary-container text-on-surface font-headline-md py-3 rounded border-b-4 border-r-4 border-black shadow-[0_4px_0_#0b0e13] active:translate-y-[4px] active:shadow-none transition-all flex items-center justify-center gap-2 text-[16px]">
52
+ <span class="material-symbols-outlined text-[20px]" style="font-variation-settings:'FILL' 1">auto_awesome</span>GENERATE AI SUMMARY</button>
53
+ </div>
54
+ <!-- Map -->
55
+ <div class="flex-1 bg-surface border-2 border-surface-dim relative overflow-hidden shadow-inner min-h-[160px]">
56
+ <div class="absolute inset-0 border-[6px] border-surface-container-high z-10 pointer-events-none opacity-80">
57
+ <div class="absolute top-1.5 left-1.5 w-3 h-3 border-t-2 border-l-2 border-secondary-fixed"></div>
58
+ <div class="absolute top-1.5 right-1.5 w-3 h-3 border-t-2 border-r-2 border-secondary-fixed"></div>
59
+ <div class="absolute bottom-1.5 left-1.5 w-3 h-3 border-b-2 border-l-2 border-secondary-fixed"></div>
60
+ <div class="absolute bottom-1.5 right-1.5 w-3 h-3 border-b-2 border-r-2 border-secondary-fixed"></div>
61
+ </div>
62
+ <div class="absolute top-0 left-0 bg-surface-container-high px-2 py-0.5 font-display-code text-[10px] text-primary z-20">SECTOR DELTA MAP</div>
63
+ <div class="w-full h-full bg-gradient-to-br from-[#0a1a0a] to-[#050a05] flex items-center justify-center"><span class="font-display-code text-secondary-fixed-dim/30 text-[12px]">TACTICAL OVERLAY</span></div>
64
+ <div class="absolute top-1/3 left-1/4 w-2 h-2 rounded-full bg-secondary-fixed shadow-[0_0_8px_#72ff70] animate-ping z-20"></div>
65
+ <div class="absolute bottom-1/3 right-1/4 w-2 h-2 rounded-full bg-error shadow-[0_0_8px_#ffb4ab] z-20"></div>
66
+ </div>
67
+ </div>
68
+ <!-- Right Col: Incident Log -->
69
+ <div class="col-span-7 bg-surface-container border border-outline-variant flex flex-col shadow-inner">
70
+ <div class="bg-surface-container-high p-2.5 border-b border-surface-dim flex justify-between items-center">
71
+ <h2 class="font-headline-md text-[16px] text-on-surface uppercase flex items-center gap-2 tracking-wide"><span class="material-symbols-outlined text-[18px]">receipt_long</span>Incident Log</h2>
72
+ <span class="font-display-code text-[11px] bg-surface-dim text-secondary-fixed px-2 py-0.5 border border-secondary-fixed/30 rounded-sm">AUTO-SCROLL: ON</span>
73
+ </div>
74
+ <div id="incidentLog" class="flex-1 p-3 overflow-y-auto space-y-2">
75
+ <div class="text-center font-display-code text-surface-variant pt-4 text-[13px]">--- AWAITING DATA ---</div>
76
+ </div>
77
+ </div>
78
+ </div>
79
+ <!-- AI Summary -->
80
+ <div id="summaryBlock" class="mt-5 bg-surface-container border border-outline-variant p-5 shadow-inner hidden">
81
+ <h2 class="font-headline-md text-on-surface uppercase text-[18px] mb-3 flex items-center gap-2"><span class="material-symbols-outlined text-[20px]">smart_toy</span>AI-Generated Briefing</h2>
82
+ <div id="summaryText" class="font-display-code text-on-surface-variant text-[13px] whitespace-pre-wrap leading-relaxed"></div>
83
+ </div>
84
+ </div>
85
+ </div>
86
+ <div class="absolute bottom-1 right-6 bg-surface-dim border border-outline-variant px-2 py-0.5 flex items-center gap-1.5">
87
+ <span class="material-symbols-outlined text-[12px] text-on-surface-variant">precision_manufacturing</span>
88
+ <span class="font-label-caps text-[9px] text-on-surface-variant tracking-widest">AEGIS DEFENSE SYSTEMS // MIL-SPEC</span>
89
+ </div>
90
+ </div>
91
+ </main>
92
+ <script>
93
+ document.getElementById('logDate').textContent=`LOG DATE: ${new Date().toISOString().split('T')[0]} | AUTHORIZATION: LEVEL 4`;
94
+ function loadData(){
95
+ fetch('/api/status').then(r=>r.json()).then(d=>{
96
+ document.getElementById('statFrames').textContent=d.frames_done||'--';
97
+ document.getElementById('statObjects').textContent=d.objects_count||'--';
98
+ document.getElementById('statAlerts').textContent=(d.alerts||[]).length||'--';
99
+ const results=d.results||[];
100
+ if(results.length>0){
101
+ const log=document.getElementById('incidentLog');log.innerHTML='';
102
+ results.forEach(f=>{
103
+ const risk=(f.risk_level||'LOW').toUpperCase();
104
+ const bc=risk==='CRITICAL'?'error':risk==='HIGH'?'tertiary':risk==='MEDIUM'?'primary':'secondary-fixed';
105
+ const code=risk==='CRITICAL'?'ERR_ALERT':risk==='HIGH'?'WARN_HIGH':'SYS_ROUTINE';
106
+ log.innerHTML+=`<div class="flex gap-3 p-2.5 bg-surface border-l-4 border-${bc} relative overflow-hidden">
107
+ <div class="pt-0.5"><div class="w-3 h-3 rounded-full bg-surface-dim border border-outline-variant flex items-center justify-center shadow-inner">
108
+ <div class="w-2 h-2 rounded-full bg-${bc} shadow-[0_0_8px_currentColor]"></div></div></div>
109
+ <div class="flex-1 font-display-code"><div class="flex justify-between items-baseline mb-0.5">
110
+ <span class="text-${bc} text-[13px]">${code}</span>
111
+ <span class="text-on-surface-variant text-[11px]">${f.time||''} ZULU</span></div>
112
+ <p class="text-on-surface text-[12px] uppercase">${f.description||f.event_type||'Event processed'}</p></div></div>`;
113
+ });
114
+ log.innerHTML+='<div class="text-center font-display-code text-surface-variant pt-3 border-t border-surface-dim/50 text-[12px]">--- END OF LOG ---</div>';
115
+ }
116
+ if(d.summary){document.getElementById('summaryBlock').classList.remove('hidden');document.getElementById('summaryText').textContent=d.summary;}
117
+ });
118
+ }
119
+ function generateSummary(){
120
+ const btn=document.getElementById('genBtn');btn.disabled=true;
121
+ btn.innerHTML='<span class="material-symbols-outlined text-[20px] animate-spin">progress_activity</span>GENERATING...';
122
+ fetch('/api/generate_summary',{method:'POST'})
123
+ .then(r=>r.json())
124
+ .then(d=>{
125
+ document.getElementById('summaryBlock').classList.remove('hidden');
126
+ document.getElementById('summaryText').textContent=d.summary||'No summary available. Run simulation first from Live Feed.';
127
+ btn.disabled=false;btn.innerHTML='<span class="material-symbols-outlined text-[20px]" style="font-variation-settings:\'FILL\' 1">auto_awesome</span>GENERATE AI SUMMARY';
128
+ })
129
+ .catch(err=>{
130
+ document.getElementById('summaryBlock').classList.remove('hidden');
131
+ document.getElementById('summaryText').textContent='Error: '+err.message+'. Make sure simulation has been run first.';
132
+ btn.disabled=false;btn.innerHTML='<span class="material-symbols-outlined text-[20px]" style="font-variation-settings:\'FILL\' 1">auto_awesome</span>GENERATE AI SUMMARY';
133
+ });
134
+ }
135
+ loadData();setInterval(loadData,5000);
136
+ </script>
137
+ </body></html>
static/video.html ADDED
@@ -0,0 +1,255 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html class="dark" lang="en"><head>
3
+ <meta charset="utf-8"/><meta content="width=device-width, initial-scale=1.0" name="viewport"/>
4
+ <title>Aegis Drone Command - Video Analysis</title>
5
+ <script src="https://cdn.tailwindcss.com?plugins=forms,container-queries"></script>
6
+ <link href="https://fonts.googleapis.com/css2?family=Anton&family=Arimo:wght@400;700&family=JetBrains+Mono:wght@400;500;700&display=swap" rel="stylesheet"/>
7
+ <link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:wght,FILL@100..700,0..1&display=swap" rel="stylesheet"/>
8
+ <script>tailwind.config={darkMode:"class",theme:{extend:{colors:{"surface-tint":"#c6c6cb","on-surface-variant":"#c6c6cb","surface-dim":"#101418","surface-container-highest":"#32353a","surface-container-high":"#272a2f","surface-container-lowest":"#0b0e13","surface-container":"#1d2024","on-error-container":"#ffdad6","on-surface":"#e0e2e8",primary:"#c6c6cb","secondary-fixed":"#72ff70","on-tertiary-container":"#ff5d53","on-primary-container":"#939499",surface:"#101418",tertiary:"#ffb4ab",error:"#ffb4ab",outline:"#909095","error-container":"#93000a",background:"#101418","primary-container":"#2b2d31","outline-variant":"#45474a","primary-fixed-dim":"#c6c6cb","tertiary-fixed-dim":"#ffb4ab","on-background":"#e0e2e8","surface-container-low":"#191c20","on-secondary-container":"#007117","surface-bright":"#36393e","secondary-fixed-dim":"#00e639","on-error":"#690005","surface-variant":"#32353a"},borderRadius:{DEFAULT:"0.125rem",lg:"0.25rem",xl:"0.5rem",full:"0.75rem"},spacing:{"gutter-md":"1.5rem","panel-padding":"2rem","component-gap":"1rem"},fontFamily:{"body-md":["Arimo"],"display-code":["JetBrains Mono"],"headline-lg":["Anton"],"label-caps":["JetBrains Mono"],"headline-md":["Anton"]},fontSize:{"body-md":["16px",{lineHeight:"1.5",fontWeight:"400"}],"display-code":["20px",{lineHeight:"1.2",letterSpacing:"-0.02em",fontWeight:"500"}],"headline-lg":["48px",{lineHeight:"1.1",letterSpacing:"0.05em",fontWeight:"400"}],"label-caps":["14px",{lineHeight:"1.2",fontWeight:"700"}],"headline-md":["32px",{lineHeight:"1.2",fontWeight:"400"}]}}}};</script>
9
+ <style>.scanlines{background:linear-gradient(to bottom,rgba(255,255,255,0),rgba(255,255,255,0) 50%,rgba(0,0,0,.2) 50%,rgba(0,0,0,.2));background-size:100% 4px}
10
+ .screw{width:7px;height:7px;border-radius:50%;background:#45474a;box-shadow:inset 1px 1px 2px rgba(0,0,0,.8),inset -1px -1px 2px rgba(255,255,255,.2);position:absolute}
11
+ .screw::after{content:'';width:5px;height:1px;background:#101418;transform:rotate(45deg);display:block;position:absolute;top:3px;left:1px}</style>
12
+ </head>
13
+ <body class="bg-background text-on-background font-body-md min-h-screen overflow-x-hidden">
14
+ <script src="/static/shell.js"></script>
15
+
16
+ <main class="ml-60 mt-16 p-panel-padding overflow-y-auto h-[calc(100vh-4rem)]">
17
+ <div class="max-w-[1400px] mx-auto grid grid-cols-1 lg:grid-cols-12 gap-6">
18
+ <!-- Monitor -->
19
+ <section class="lg:col-span-8 flex flex-col gap-3">
20
+ <div class="font-label-caps text-[11px] text-outline tracking-widest uppercase flex items-center gap-2">
21
+ <span class="w-2 h-2 rounded-full bg-secondary-fixed-dim animate-pulse shadow-[0_0_6px_#00e639]"></span>Unit 07 : Sensor Input</div>
22
+ <div class="relative bg-surface-container-highest p-3 rounded-xl border-2 border-surface-dim shadow-[0_10px_30px_rgba(0,0,0,0.8)]">
23
+ <div class="screw top-2 left-2"></div><div class="screw top-2 right-2"></div><div class="screw bottom-2 left-2"></div><div class="screw bottom-2 right-2"></div>
24
+ <div id="screenArea" class="bg-surface-dim p-3 rounded-lg border-2 border-surface shadow-[inset_0_4px_10px_rgba(0,0,0,1)] relative overflow-hidden h-[420px] flex flex-col items-center justify-center cursor-pointer" ondrop="handleDrop(event)" ondragover="event.preventDefault()" onclick="document.getElementById('videoInput').click()">
25
+ <div class="absolute inset-0 bg-surface-container-lowest"></div>
26
+ <div class="absolute inset-0 scanlines pointer-events-none opacity-50"></div>
27
+ <div id="uploadZone" class="z-10 flex flex-col items-center gap-4 text-center">
28
+ <div class="w-16 h-16 rounded-full border-2 border-dashed border-outline-variant flex items-center justify-center bg-surface-container/50 hover:border-secondary-fixed-dim transition-all">
29
+ <span class="material-symbols-outlined text-[28px] text-outline-variant">upload_file</span></div>
30
+ <div><p class="font-display-code text-on-surface text-[14px] mb-1">DRAG & DROP VIDEO SOURCE</p>
31
+ <p class="font-label-caps text-[11px] text-outline">Supported: .MP4, .MKV, .AVI</p></div>
32
+ </div>
33
+ <video id="videoPlayer" class="absolute inset-0 w-full h-full object-contain z-10 hidden" controls></video>
34
+ <div class="absolute bottom-3 left-3 font-display-code text-[11px] text-secondary-fixed-dim opacity-70 z-20">REC [WAITING]<br/>CH-01</div>
35
+ </div>
36
+ <input type="file" id="videoInput" accept="video/*" class="hidden" onchange="loadVideo(this)"/>
37
+ </div>
38
+ </section>
39
+
40
+ <!-- Controls -->
41
+ <section class="lg:col-span-4 flex flex-col gap-3">
42
+ <div class="font-label-caps text-[11px] text-outline tracking-widest uppercase">Calibration Parameters</div>
43
+ <div class="bg-surface-container-high rounded-xl border-2 border-surface-dim shadow-[0_8px_20px_rgba(0,0,0,0.6)] p-5 relative flex flex-col gap-4 flex-1">
44
+ <div class="screw top-2 left-2"></div><div class="screw top-2 right-2"></div>
45
+ <!-- Confidence -->
46
+ <div class="flex items-center justify-between bg-surface-container p-3 rounded border border-surface-dim shadow-inner">
47
+ <span class="font-label-caps text-[11px] text-outline">DETECTION CONFIDENCE</span>
48
+ <div class="flex items-center gap-2">
49
+ <input type="range" min="10" max="100" value="85" id="confSlider" class="w-20 accent-green-500 h-1" oninput="document.getElementById('confVal').textContent=this.value+'%'"/>
50
+ <span id="confVal" class="font-display-code text-[13px] text-secondary-fixed-dim w-10 text-right">85%</span>
51
+ </div>
52
+ </div>
53
+ <!-- Toggle thermal -->
54
+ <div class="flex items-center justify-between bg-surface-container p-3 rounded border border-surface-dim shadow-inner">
55
+ <div class="flex items-center gap-2">
56
+ <div id="thermalLed" class="w-2.5 h-2.5 rounded-full bg-secondary-fixed shadow-[0_0_6px_#72ff70] border border-[#002203]"></div>
57
+ <span class="font-display-code text-[13px] text-on-surface">THERMAL OVERLAY</span>
58
+ </div>
59
+ <button onclick="toggleThermal()" class="w-10 h-6 bg-surface-dim rounded border border-surface shadow-inner relative cursor-pointer">
60
+ <div id="thermalKnob" class="absolute top-0.5 left-1/2 -translate-x-1/2 w-6 h-3 bg-gradient-to-b from-surface-tint to-surface-variant rounded shadow-[0_1px_3px_rgba(0,0,0,0.8)] transition-all"></div>
61
+ </button>
62
+ </div>
63
+ <!-- Toggle tracking -->
64
+ <div class="flex items-center justify-between bg-surface-container p-3 rounded border border-surface-dim shadow-inner">
65
+ <div class="flex items-center gap-2">
66
+ <div id="trackLed" class="w-2.5 h-2.5 rounded-full bg-surface-dim shadow-inner border border-surface"></div>
67
+ <span class="font-display-code text-[13px] text-outline-variant">OBJECT TRACKING</span>
68
+ </div>
69
+ <button onclick="toggleTracking()" class="w-10 h-6 bg-surface-dim rounded border border-surface shadow-inner relative cursor-pointer">
70
+ <div id="trackKnob" class="absolute bottom-0.5 left-1/2 -translate-x-1/2 w-6 h-3 bg-gradient-to-b from-surface-variant to-surface rounded shadow-[0_-1px_3px_rgba(0,0,0,0.8)] transition-all"></div>
71
+ </button>
72
+ </div>
73
+ <!-- Action -->
74
+ <div class="mt-auto pt-3">
75
+ <button id="analysisBtn" onclick="initAnalysis()" class="w-full relative group h-16 bg-surface-dim rounded-lg border-2 border-surface-dim shadow-[inset_0_4px_10px_rgba(0,0,0,0.8)] p-1.5">
76
+ <div class="w-full h-full bg-gradient-to-b from-surface-variant to-surface rounded flex items-center justify-center border border-[#45474a] shadow-[0_4px_0_#101418] group-active:translate-y-1 group-active:shadow-none transition-all duration-75">
77
+ <span class="font-headline-md text-[16px] tracking-wider text-on-surface group-active:text-secondary-fixed-dim transition-colors uppercase">Initialize Analysis</span>
78
+ </div></button>
79
+ </div>
80
+ </div>
81
+ </section>
82
+
83
+ <!-- Log table -->
84
+ <section class="lg:col-span-12">
85
+ <div class="font-headline-md text-on-surface-variant mb-3 flex items-center gap-3 text-[18px]">OPERATIONAL LOG<div class="h-[2px] flex-grow bg-gradient-to-r from-surface-dim to-transparent"></div></div>
86
+ <div class="bg-surface-container-lowest border border-surface-dim p-1 font-display-code text-[12px] shadow-[inset_0_0_20px_rgba(0,0,0,0.8)]">
87
+ <div class="grid grid-cols-12 gap-3 text-outline-variant p-2.5 border-b border-surface-dim text-[11px] bg-surface-container">
88
+ <div class="col-span-2">TIMESTAMP</div><div class="col-span-3">SOURCE ID</div><div class="col-span-2">STATUS</div><div class="col-span-4">DETECTION SUMMARY</div><div class="col-span-1 text-right">ACT</div>
89
+ </div>
90
+ <div id="logEntries" class="flex flex-col">
91
+ <div class="grid grid-cols-12 gap-3 p-2.5 border-b border-surface/50 text-on-surface hover:bg-surface transition-colors">
92
+ <div class="col-span-2 text-outline">--:--:--.---</div><div class="col-span-3 text-outline">AWAITING_INPUT</div>
93
+ <div class="col-span-2 flex items-center gap-1.5 text-outline"><span class="w-1.5 h-1.5 bg-outline rounded-full"></span>STANDBY</div>
94
+ <div class="col-span-4 text-outline opacity-60">Upload video to begin</div>
95
+ <div class="col-span-1 text-right"><span class="material-symbols-outlined text-[16px] text-outline">visibility</span></div>
96
+ </div>
97
+ </div>
98
+ </div>
99
+ </section>
100
+ </div>
101
+ </main>
102
+
103
+ <script>
104
+ let thermalOn=true, trackingOn=false, uploadedFile=null, analyzing=false;
105
+
106
+ function toggleThermal(){
107
+ thermalOn=!thermalOn;
108
+ document.getElementById('thermalLed').className = thermalOn
109
+ ? 'w-2.5 h-2.5 rounded-full bg-secondary-fixed shadow-[0_0_6px_#72ff70] border border-[#002203]'
110
+ : 'w-2.5 h-2.5 rounded-full bg-surface-dim shadow-inner border border-surface';
111
+ }
112
+
113
+ function toggleTracking(){
114
+ trackingOn=!trackingOn;
115
+ document.getElementById('trackLed').className = trackingOn
116
+ ? 'w-2.5 h-2.5 rounded-full bg-secondary-fixed shadow-[0_0_6px_#72ff70] border border-[#002203]'
117
+ : 'w-2.5 h-2.5 rounded-full bg-surface-dim shadow-inner border border-surface';
118
+ }
119
+
120
+ function loadVideo(input){
121
+ if(input.files && input.files[0]){
122
+ uploadedFile = input.files[0];
123
+ const url = URL.createObjectURL(uploadedFile);
124
+ document.getElementById('videoPlayer').src = url;
125
+ document.getElementById('videoPlayer').classList.remove('hidden');
126
+ document.getElementById('uploadZone').classList.add('hidden');
127
+ addLogEntry(new Date().toLocaleTimeString(), 'LOCAL_FILE', 'LOADED',
128
+ 'Video: ' + uploadedFile.name + ' (' + (uploadedFile.size/1024/1024).toFixed(1) + ' MB)', 'secondary-fixed');
129
+ }
130
+ }
131
+
132
+ function handleDrop(e){
133
+ e.preventDefault();
134
+ const f = e.dataTransfer.files;
135
+ if(f.length){
136
+ document.getElementById('videoInput').files = f;
137
+ loadVideo(document.getElementById('videoInput'));
138
+ }
139
+ }
140
+
141
+ function initAnalysis(){
142
+ if(analyzing) return;
143
+ if(!uploadedFile){
144
+ addLogEntry(new Date().toLocaleTimeString(), 'AEGIS_CORE', 'ERROR', 'No video loaded. Upload a video first.', 'error');
145
+ return;
146
+ }
147
+
148
+ analyzing = true;
149
+ const btnText = document.querySelector('#analysisBtn span');
150
+ if(btnText) btnText.textContent = 'ANALYZING...';
151
+
152
+ addLogEntry(new Date().toLocaleTimeString(), 'AEGIS_CORE', 'INIT', 'Uploading video for AI analysis...', 'secondary-fixed-dim');
153
+
154
+ // Upload via fetch, then read SSE from the response
155
+ const formData = new FormData();
156
+ formData.append('file', uploadedFile);
157
+
158
+ fetch('/api/analyze_video', { method: 'POST', body: formData })
159
+ .then(response => {
160
+ if(!response.ok) throw new Error('Upload failed: ' + response.status);
161
+
162
+ const reader = response.body.getReader();
163
+ const decoder = new TextDecoder();
164
+ let buffer = '';
165
+ let frameCount = 0;
166
+
167
+ function processChunk(){
168
+ reader.read().then(({done, value}) => {
169
+ if(done){
170
+ analyzing = false;
171
+ if(btnText) btnText.textContent = 'INITIALIZE ANALYSIS';
172
+ addLogEntry(new Date().toLocaleTimeString(), 'AEGIS_CORE', 'COMPLETE',
173
+ `Analysis finished. ${frameCount} frames processed.`, 'secondary-fixed');
174
+ return;
175
+ }
176
+
177
+ buffer += decoder.decode(value, {stream: true});
178
+ const lines = buffer.split('\n');
179
+ buffer = lines.pop(); // keep incomplete line
180
+
181
+ let eventType = '', eventData = '';
182
+ for(const line of lines){
183
+ if(line.startsWith('event: ')) eventType = line.slice(7).trim();
184
+ else if(line.startsWith('data: ')){
185
+ eventData = line.slice(6).trim();
186
+ try {
187
+ const d = JSON.parse(eventData);
188
+ if(eventType === 'status'){
189
+ addLogEntry(new Date().toLocaleTimeString(), 'AEGIS_CORE', 'STATUS', d.message, 'primary');
190
+ } else if(eventType === 'frame'){
191
+ frameCount++;
192
+ const risk = (d.risk_level||'LOW').toUpperCase();
193
+ const color = risk==='CRITICAL' ? 'error' : risk==='HIGH' ? 'tertiary' : risk==='MEDIUM' ? 'primary' : 'secondary-fixed-dim';
194
+ addLogEntry(
195
+ d.time || new Date().toLocaleTimeString(),
196
+ `FRM-${String(d.frame_id).padStart(3,'0')}`,
197
+ risk,
198
+ `[${d.location}] ${d.description}`,
199
+ color
200
+ );
201
+ // Show merged objects (YOLO + VLM)
202
+ if(d.objects && d.objects.length > 0){
203
+ addLogEntry('', '', 'OBJECTS', d.objects.join(', '), 'secondary-fixed-dim');
204
+ }
205
+ // Show YOLO detection counts with confidence
206
+ if(d.yolo_detections && d.yolo_detections.length > 0){
207
+ const detSummary = d.yolo_detections.map(det =>
208
+ `${det.label} (${Math.round(det.confidence*100)}%)`
209
+ ).join(', ');
210
+ addLogEntry('', '', 'YOLO', `Detected: ${detSummary}`, 'secondary-fixed');
211
+ }
212
+ if(d.detection_counts && Object.keys(d.detection_counts).length > 0){
213
+ const countSummary = Object.entries(d.detection_counts).map(([k,v]) =>
214
+ `${v}x ${k}`
215
+ ).join(', ');
216
+ addLogEntry('', '', 'COUNT', countSummary, 'primary');
217
+ }
218
+ if(d.recommended_action){
219
+ addLogEntry('', '', 'ACTION', d.recommended_action, risk==='CRITICAL'||risk==='HIGH' ? 'error' : 'outline');
220
+ }
221
+ } else if(eventType === 'complete'){
222
+ addLogEntry(new Date().toLocaleTimeString(), 'AEGIS_CORE', 'DONE', d.message, 'secondary-fixed');
223
+ } else if(eventType === 'error'){
224
+ addLogEntry(new Date().toLocaleTimeString(), 'AEGIS_CORE', 'ERROR', d.message, 'error');
225
+ }
226
+ } catch(e){}
227
+ eventType = '';
228
+ }
229
+ }
230
+ processChunk();
231
+ });
232
+ }
233
+ processChunk();
234
+ })
235
+ .catch(err => {
236
+ analyzing = false;
237
+ if(btnText) btnText.textContent = 'INITIALIZE ANALYSIS';
238
+ addLogEntry(new Date().toLocaleTimeString(), 'AEGIS_CORE', 'ERROR', err.message, 'error');
239
+ });
240
+ }
241
+
242
+ function addLogEntry(time, src, status, summary, color){
243
+ const row = document.createElement('div');
244
+ row.className = 'grid grid-cols-12 gap-3 p-2.5 border-b border-surface/50 text-on-surface hover:bg-surface transition-colors text-[12px]';
245
+ row.innerHTML = `<div class="col-span-2">${time}</div>
246
+ <div class="col-span-3">${src}</div>
247
+ <div class="col-span-2 flex items-center gap-1.5">
248
+ <span class="w-1.5 h-1.5 bg-${color||'secondary-fixed'} rounded-full shadow-[0_0_4px_currentColor]"></span>${status}</div>
249
+ <div class="col-span-4 truncate opacity-80">${summary}</div>
250
+ <div class="col-span-1 text-right">
251
+ <span class="material-symbols-outlined text-[16px] cursor-pointer hover:text-on-surface">visibility</span></div>`;
252
+ document.getElementById('logEntries').prepend(row);
253
+ }
254
+ </script>
255
+ </body></html>
tests/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ # Tests package
tests/test_agent.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Tests for the LangChain agent integration.
3
+ Note: These tests mock the LLM to avoid API calls during testing.
4
+ """
5
+
6
+ import sys
7
+ import os
8
+ import tempfile
9
+ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
10
+
11
+ from indexing.sqlite_store import SQLiteStore
12
+ from indexing.chroma_store import ChromaStore
13
+ from alerts.rules_engine import AlertManager
14
+ from agent.tools import init_tools, log_event, check_alert_rules, query_past_events, generate_summary
15
+ import json
16
+
17
+
18
+ class TestAgentTools:
19
+ """Tests for the agent's custom tools (without LLM calls)."""
20
+
21
+ def setup_method(self):
22
+ self.test_dir = tempfile.mkdtemp()
23
+ self.sqlite = SQLiteStore(db_path=os.path.join(self.test_dir, "test.db"))
24
+ self.chroma = ChromaStore(db_path=os.path.join(self.test_dir, "chroma"), collection_name="test")
25
+ self.alerts = AlertManager()
26
+ init_tools(self.sqlite, self.chroma, self.alerts)
27
+
28
+ def teardown_method(self):
29
+ self.sqlite.close()
30
+ try:
31
+ self.chroma.clear_all()
32
+ except Exception:
33
+ pass
34
+
35
+ def test_log_event_tool(self):
36
+ """Test the log_event tool writes to both stores."""
37
+ event = {
38
+ "frame_id": 1, "timestamp": "06:15", "location": "Main Gate",
39
+ "objects": ["White Toyota Camry"], "event_type": "vehicle_entry",
40
+ "risk_level": "low", "is_suspicious": False,
41
+ "description": "Employee arrival", "raw_description": "Camry at gate",
42
+ }
43
+ result = log_event.invoke(json.dumps(event))
44
+ assert "Event logged" in result
45
+ assert self.sqlite.get_all_frames()[0]["frame_id"] == 1
46
+
47
+ def test_check_alert_rules_tool(self):
48
+ """Test the check_alert_rules tool detects threats."""
49
+ analysis = {
50
+ "frame_id": 25, "time": "22:15", "location": "Perimeter Fence North",
51
+ "objects": ["Person"], "event_type": "perimeter_breach",
52
+ "risk_level": "critical", "is_suspicious": True,
53
+ "description": "Person climbing fence", "raw_description": "Person climbing fence",
54
+ }
55
+ result = check_alert_rules.invoke(json.dumps(analysis))
56
+ assert "ALERTS TRIGGERED" in result
57
+
58
+ def test_query_past_events_tool(self):
59
+ """Test querying after logging events."""
60
+ event = {
61
+ "frame_id": 8, "timestamp": "09:30", "location": "Parking Lot B",
62
+ "objects": ["Blue Ford F150"], "event_type": "vehicle_entry",
63
+ "risk_level": "medium", "is_suspicious": False,
64
+ "description": "Truck entering lot", "raw_description": "F150 truck",
65
+ }
66
+ log_event.invoke(json.dumps(event))
67
+ result = query_past_events.invoke("Ford F150")
68
+ assert "Frame 8" in result
69
+
70
+ def test_generate_summary_tool(self):
71
+ """Test summary generation."""
72
+ event = {
73
+ "frame_id": 1, "timestamp": "06:00", "location": "Gate",
74
+ "objects": ["Car"], "event_type": "entry", "risk_level": "low",
75
+ "is_suspicious": False, "description": "Normal", "raw_description": "Car",
76
+ }
77
+ log_event.invoke(json.dumps(event))
78
+ result = generate_summary.invoke("")
79
+ assert "Total Frames" in result
80
+
81
+ def test_agent_context_via_vehicle_tracking(self):
82
+ """Test that the same vehicle across 3 frames is tracked."""
83
+ for i, (fid, time, loc) in enumerate([(8, "09:30", "Lot B"), (11, "11:30", "Lot A"), (17, "15:00", "Gate")]):
84
+ event = {
85
+ "frame_id": fid, "timestamp": time, "location": loc,
86
+ "objects": ["Blue Ford F150"], "event_type": "vehicle_entry",
87
+ "risk_level": "medium", "is_suspicious": i > 0,
88
+ "description": f"F150 seen at {loc}", "raw_description": f"F150 at {loc}",
89
+ }
90
+ result = log_event.invoke(json.dumps(event))
91
+
92
+ # After 3 sightings, vehicle re-entry should be noted
93
+ count = self.sqlite.get_vehicle_count("Blue Ford F150")
94
+ assert count == 3
tests/test_alerts.py ADDED
@@ -0,0 +1,207 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Tests for the alert rules engine.
3
+ """
4
+
5
+ import sys
6
+ import os
7
+ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
8
+
9
+ from alerts.rules_engine import AlertManager, ALERT_RULES
10
+
11
+
12
+ class TestAlertManager:
13
+ """Tests for the AlertManager and alert rules."""
14
+
15
+ def setup_method(self):
16
+ """Reset alert manager before each test."""
17
+ self.manager = AlertManager()
18
+
19
+ def test_alert_fires_at_midnight(self):
20
+ """A person detected after midnight should trigger an after-hours alert."""
21
+ analysis = {
22
+ "frame_id": 99,
23
+ "time": "00:15",
24
+ "location": "Loading Dock",
25
+ "objects": ["Person"],
26
+ "event_type": "person_detected",
27
+ "risk_level": "high",
28
+ "is_suspicious": True,
29
+ "description": "Person in dark clothing at loading dock",
30
+ "raw_description": "Person in dark clothing near loading dock at night",
31
+ }
32
+
33
+ alerts = self.manager.evaluate_rules(analysis)
34
+
35
+ # Should fire the after_hours_person rule
36
+ rule_names = [a["rule_name"] for a in alerts]
37
+ assert "after_hours_person" in rule_names
38
+
39
+ def test_perimeter_breach_alert(self):
40
+ """Person attempting to climb a perimeter fence should trigger CRITICAL alert."""
41
+ analysis = {
42
+ "frame_id": 26,
43
+ "time": "22:30",
44
+ "location": "Perimeter Fence North",
45
+ "objects": ["Person"],
46
+ "event_type": "perimeter_breach",
47
+ "risk_level": "critical",
48
+ "is_suspicious": True,
49
+ "description": "Person attempting to climb the fence",
50
+ "raw_description": "Person climbing the north perimeter fence",
51
+ }
52
+
53
+ alerts = self.manager.evaluate_rules(analysis)
54
+
55
+ # Should fire perimeter_breach
56
+ rule_names = [a["rule_name"] for a in alerts]
57
+ assert "perimeter_breach" in rule_names
58
+
59
+ # Should be CRITICAL severity
60
+ breach_alert = next(a for a in alerts if a["rule_name"] == "perimeter_breach")
61
+ assert breach_alert["severity"] == "CRITICAL"
62
+
63
+ def test_no_false_alert_daytime_employee(self):
64
+ """Normal employee activity during business hours should not trigger after-hours alerts."""
65
+ analysis = {
66
+ "frame_id": 5,
67
+ "time": "09:00",
68
+ "location": "Main Gate",
69
+ "objects": ["Silver Honda Civic"],
70
+ "event_type": "vehicle_entry",
71
+ "risk_level": "low",
72
+ "is_suspicious": False,
73
+ "description": "Employee vehicle entering during business hours",
74
+ "raw_description": "Silver Honda Civic arriving at main gate",
75
+ }
76
+
77
+ alerts = self.manager.evaluate_rules(analysis)
78
+
79
+ # Should NOT fire after_hours or perimeter alerts
80
+ rule_names = [a["rule_name"] for a in alerts]
81
+ assert "after_hours_person" not in rule_names
82
+ assert "perimeter_breach" not in rule_names
83
+
84
+ def test_unknown_vehicle_night(self):
85
+ """Unknown vehicle at night should trigger high-severity alert."""
86
+ analysis = {
87
+ "frame_id": 27,
88
+ "time": "23:00",
89
+ "location": "Main Gate",
90
+ "objects": ["Dark SUV"],
91
+ "event_type": "vehicle_entry",
92
+ "risk_level": "high",
93
+ "is_suspicious": True,
94
+ "description": "Unidentified dark SUV at gate after hours",
95
+ "raw_description": "Dark SUV with tinted windows idling near main gate at night",
96
+ }
97
+
98
+ alerts = self.manager.evaluate_rules(analysis)
99
+ rule_names = [a["rule_name"] for a in alerts]
100
+ assert "unknown_vehicle_night" in rule_names
101
+
102
+ def test_vehicle_reentry_alert(self):
103
+ """Vehicle seen multiple times should trigger re-entry alert."""
104
+ analysis = {
105
+ "frame_id": 11,
106
+ "time": "11:30",
107
+ "location": "Parking Lot A",
108
+ "objects": ["Blue Ford F150"],
109
+ "event_type": "vehicle_entry",
110
+ "risk_level": "medium",
111
+ "is_suspicious": True,
112
+ "description": "Same vehicle circling parking lot",
113
+ "raw_description": "Blue Ford F150 circling lot",
114
+ }
115
+
116
+ context = {"vehicle_count": 3, "vehicle_name": "Blue Ford F150"}
117
+ alerts = self.manager.evaluate_rules(analysis, context)
118
+ rule_names = [a["rule_name"] for a in alerts]
119
+ assert "vehicle_reentry" in rule_names
120
+
121
+ def test_unauthorized_access(self):
122
+ """Person with ski mask at server room should trigger CRITICAL alert."""
123
+ analysis = {
124
+ "frame_id": 28,
125
+ "time": "23:30",
126
+ "location": "Server Room Exterior",
127
+ "objects": ["Person"],
128
+ "event_type": "unauthorized_access",
129
+ "risk_level": "critical",
130
+ "is_suspicious": True,
131
+ "description": "Person in ski mask attempting access",
132
+ "raw_description": "Person in ski mask at server room door with device on keycard reader",
133
+ }
134
+
135
+ alerts = self.manager.evaluate_rules(analysis)
136
+ rule_names = [a["rule_name"] for a in alerts]
137
+ assert "unauthorized_access" in rule_names
138
+
139
+ def test_door_anomaly(self):
140
+ """Open door after hours should trigger alert."""
141
+ analysis = {
142
+ "frame_id": 20,
143
+ "time": "17:30",
144
+ "location": "Emergency Exit Rear",
145
+ "objects": [],
146
+ "event_type": "door_anomaly",
147
+ "risk_level": "medium",
148
+ "is_suspicious": False,
149
+ "description": "Rear emergency exit door ajar",
150
+ "raw_description": "Emergency exit door is ajar, partially open",
151
+ }
152
+
153
+ alerts = self.manager.evaluate_rules(analysis)
154
+ rule_names = [a["rule_name"] for a in alerts]
155
+ assert "door_anomaly" in rule_names
156
+
157
+ def test_suspicious_behavior(self):
158
+ """Person taking photos near perimeter should trigger suspicious behavior alert."""
159
+ analysis = {
160
+ "frame_id": 15,
161
+ "time": "14:30",
162
+ "location": "Perimeter Fence East",
163
+ "objects": ["Person"],
164
+ "event_type": "suspicious_activity",
165
+ "risk_level": "high",
166
+ "is_suspicious": True,
167
+ "description": "Person photographing the warehouse from the perimeter",
168
+ "raw_description": "Person in dark hoodie taking photos of warehouse through fence",
169
+ }
170
+
171
+ alerts = self.manager.evaluate_rules(analysis)
172
+ rule_names = [a["rule_name"] for a in alerts]
173
+ assert "suspicious_behavior" in rule_names
174
+
175
+ def test_alert_summary(self):
176
+ """Test that alert summary correctly counts alerts."""
177
+ # Fire two different alerts
178
+ analysis1 = {
179
+ "frame_id": 25, "time": "22:15", "location": "Perimeter Fence North",
180
+ "objects": ["Person"], "event_type": "perimeter_breach", "risk_level": "critical",
181
+ "is_suspicious": True, "description": "Breach attempt",
182
+ "raw_description": "Person climbing fence",
183
+ }
184
+ analysis2 = {
185
+ "frame_id": 27, "time": "23:00", "location": "Main Gate",
186
+ "objects": ["Dark SUV"], "event_type": "vehicle_entry", "risk_level": "high",
187
+ "is_suspicious": True, "description": "Unknown vehicle at night",
188
+ "raw_description": "Dark SUV idling at gate",
189
+ }
190
+
191
+ self.manager.evaluate_rules(analysis1)
192
+ self.manager.evaluate_rules(analysis2)
193
+
194
+ summary = self.manager.get_alert_summary()
195
+ assert summary["total"] > 0
196
+ assert len(summary["alerts"]) > 0
197
+
198
+ def test_alert_rules_defined(self):
199
+ """Verify all expected alert rules are defined."""
200
+ rule_names = [r["name"] for r in ALERT_RULES]
201
+ expected = [
202
+ "after_hours_person", "perimeter_breach", "unauthorized_access",
203
+ "unknown_vehicle_night", "vehicle_reentry", "suspicious_behavior",
204
+ "door_anomaly", "unauthorized_parking",
205
+ ]
206
+ for name in expected:
207
+ assert name in rule_names, f"Missing rule: {name}"
tests/test_indexing.py ADDED
@@ -0,0 +1,209 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Tests for the indexing system (SQLite + ChromaDB).
3
+ """
4
+
5
+ import sys
6
+ import os
7
+ import tempfile
8
+ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
9
+
10
+ import pytest
11
+ from indexing.sqlite_store import SQLiteStore
12
+ from indexing.chroma_store import ChromaStore
13
+
14
+
15
+ class TestSQLiteStore:
16
+ """Tests for SQLiteStore."""
17
+
18
+ def setup_method(self):
19
+ """Create a fresh test database."""
20
+ self.test_dir = tempfile.mkdtemp()
21
+ self.db_path = os.path.join(self.test_dir, "test_events.db")
22
+ self.store = SQLiteStore(db_path=self.db_path)
23
+
24
+ def teardown_method(self):
25
+ """Cleanup test database."""
26
+ self.store.close()
27
+ if os.path.exists(self.db_path):
28
+ os.remove(self.db_path)
29
+
30
+ def test_insert_and_retrieve_frame(self):
31
+ """Test basic frame insertion and retrieval."""
32
+ self.store.insert_frame(
33
+ frame_id=1,
34
+ timestamp="06:15",
35
+ location="Main Gate",
36
+ objects_detected=["White Toyota Camry"],
37
+ event_type="vehicle_entry",
38
+ risk_level="low",
39
+ is_suspicious=False,
40
+ raw_description="White Toyota Camry entering main gate",
41
+ )
42
+
43
+ frames = self.store.get_all_frames()
44
+ assert len(frames) == 1
45
+ assert frames[0]["frame_id"] == 1
46
+ assert frames[0]["location"] == "Main Gate"
47
+
48
+ def test_vehicle_logged_correctly(self):
49
+ """After processing a truck frame, the DB should contain the truck entry."""
50
+ self.store.insert_frame(
51
+ frame_id=8,
52
+ timestamp="09:30",
53
+ location="Parking Lot B",
54
+ objects_detected=["Blue Ford F150"],
55
+ event_type="vehicle_entry",
56
+ risk_level="medium",
57
+ is_suspicious=False,
58
+ raw_description="Blue Ford F150 pickup truck entering lot B",
59
+ )
60
+
61
+ results = self.store.query_by_object("Ford F150")
62
+ assert len(results) >= 1
63
+ assert results[0]["frame_id"] == 8
64
+
65
+ def test_frame_query_by_object(self):
66
+ """Insert 5 frames, query for 'truck', assert only truck frames returned."""
67
+ test_frames = [
68
+ (1, "06:00", "Main Gate", ["Security Guard"], "security_patrol", "low"),
69
+ (2, "06:15", "Main Gate", ["White Toyota Camry"], "vehicle_entry", "low"),
70
+ (3, "08:30", "Loading Dock", ["FedEx Delivery Truck"], "delivery", "low"),
71
+ (4, "09:00", "Warehouse", ["Employees"], "employee_activity", "low"),
72
+ (5, "13:00", "Loading Dock", ["UPS Delivery Truck"], "delivery", "low"),
73
+ ]
74
+
75
+ for fid, time, loc, objs, etype, risk in test_frames:
76
+ self.store.insert_frame(fid, time, loc, objs, etype, risk, False, "desc")
77
+
78
+ truck_results = self.store.query_by_object("Truck")
79
+ assert len(truck_results) == 2 # FedEx and UPS trucks
80
+ frame_ids = [r["frame_id"] for r in truck_results]
81
+ assert 3 in frame_ids
82
+ assert 5 in frame_ids
83
+
84
+ def test_query_by_time_range(self):
85
+ """Test time range queries."""
86
+ self.store.insert_frame(1, "06:00", "Gate", ["Guard"], "patrol", "low", False, "")
87
+ self.store.insert_frame(2, "12:00", "Gate", ["Car"], "entry", "low", False, "")
88
+ self.store.insert_frame(3, "23:00", "Gate", ["SUV"], "entry", "high", True, "")
89
+
90
+ # Query after 22:00
91
+ results = self.store.query_by_time_range("22:00", "23:59")
92
+ assert len(results) == 1
93
+ assert results[0]["frame_id"] == 3
94
+
95
+ def test_insert_and_retrieve_alert(self):
96
+ """Test alert insertion and retrieval."""
97
+ alert_id = self.store.insert_alert(
98
+ frame_id=25,
99
+ timestamp="22:15",
100
+ rule_name="after_hours_person",
101
+ severity="HIGH",
102
+ message="Person detected after hours",
103
+ location="Perimeter Fence North",
104
+ )
105
+
106
+ alerts = self.store.get_all_alerts()
107
+ assert len(alerts) == 1
108
+ assert alerts[0]["alert_id"] == alert_id
109
+ assert alerts[0]["severity"] == "HIGH"
110
+
111
+ def test_vehicle_count(self):
112
+ """Test vehicle counting across multiple sightings."""
113
+ count1 = self.store.log_vehicle("Blue Ford F150", 8, "09:30", "Parking Lot B")
114
+ assert count1 == 1
115
+
116
+ count2 = self.store.log_vehicle("Blue Ford F150", 11, "11:30", "Parking Lot A")
117
+ assert count2 == 2
118
+
119
+ total = self.store.get_vehicle_count("Blue Ford F150")
120
+ assert total == 2
121
+
122
+ def test_suspicious_frames(self):
123
+ """Test querying suspicious frames."""
124
+ self.store.insert_frame(1, "06:00", "Gate", ["Car"], "entry", "low", False, "")
125
+ self.store.insert_frame(2, "22:15", "Fence", ["Person"], "breach", "critical", True, "")
126
+ self.store.insert_frame(3, "23:00", "Gate", ["SUV"], "entry", "high", True, "")
127
+
128
+ suspicious = self.store.query_suspicious_frames()
129
+ assert len(suspicious) == 2
130
+ frame_ids = [r["frame_id"] for r in suspicious]
131
+ assert 2 in frame_ids
132
+ assert 3 in frame_ids
133
+
134
+ def test_session_stats(self):
135
+ """Test session statistics computation."""
136
+ self.store.insert_frame(1, "06:00", "Gate", ["Car"], "entry", "low", False, "")
137
+ self.store.insert_frame(2, "22:15", "Fence", ["Person"], "breach", "critical", True, "")
138
+ self.store.insert_alert(2, "22:15", "perimeter_breach", "CRITICAL", "Breach!", "Fence")
139
+
140
+ stats = self.store.get_session_stats()
141
+ assert stats["total_frames"] == 2
142
+ assert stats["suspicious_frames"] == 1
143
+ assert stats["total_alerts"] == 1
144
+ assert stats["alerts_by_severity"]["CRITICAL"] == 1
145
+
146
+ def test_clear_all(self):
147
+ """Test clearing all data."""
148
+ self.store.insert_frame(1, "06:00", "Gate", ["Car"], "entry", "low", False, "")
149
+ self.store.insert_alert(1, "06:00", "test", "LOW", "test alert")
150
+ self.store.clear_all()
151
+
152
+ assert len(self.store.get_all_frames()) == 0
153
+ assert len(self.store.get_all_alerts()) == 0
154
+
155
+
156
+ class TestChromaStore:
157
+ """Tests for ChromaStore."""
158
+
159
+ def setup_method(self):
160
+ """Create a fresh test ChromaDB."""
161
+ self.test_dir = tempfile.mkdtemp()
162
+ self.chroma_path = os.path.join(self.test_dir, "test_chroma")
163
+ self.store = ChromaStore(db_path=self.chroma_path, collection_name="test_frames")
164
+
165
+ def teardown_method(self):
166
+ """Cleanup."""
167
+ try:
168
+ self.store.clear_all()
169
+ except Exception:
170
+ pass
171
+
172
+ def test_add_and_count_frames(self):
173
+ """Test adding frames and counting them."""
174
+ self.store.add_frame(1, "06:00", "Main Gate", "Car entering gate", "vehicle_entry", "low")
175
+ self.store.add_frame(2, "22:15", "Fence", "Person near fence at night", "suspicious_activity", "high")
176
+
177
+ assert self.store.get_frame_count() == 2
178
+
179
+ def test_semantic_search(self):
180
+ """Test semantic search returns relevant results."""
181
+ self.store.add_frame(1, "06:00", "Main Gate", "White Toyota Camry employee arriving for work", "vehicle_entry", "low")
182
+ self.store.add_frame(2, "22:15", "Perimeter Fence", "Person in dark clothing near fence at night", "suspicious_activity", "high")
183
+ self.store.add_frame(3, "08:30", "Loading Dock", "FedEx delivery truck unloading packages", "delivery", "low")
184
+
185
+ # Search for nighttime suspicious activity
186
+ results = self.store.semantic_search("suspicious person at night near fence", top_k=3)
187
+ assert len(results) > 0
188
+
189
+ # The fence/night frame should be most relevant
190
+ assert results[0]["frame_id"] == 2
191
+
192
+ def test_search_by_vehicle(self):
193
+ """Test searching for vehicles returns vehicle frames."""
194
+ self.store.add_frame(1, "06:00", "Gate", "Toyota Camry entering the property", "vehicle_entry", "low")
195
+ self.store.add_frame(2, "09:00", "Warehouse", "Workers entering warehouse", "employee_activity", "low")
196
+ self.store.add_frame(3, "13:00", "Dock", "UPS delivery truck at dock", "delivery", "low")
197
+
198
+ results = self.store.semantic_search("delivery truck", top_k=3)
199
+ assert len(results) > 0
200
+ # The UPS truck should be most relevant
201
+ assert results[0]["frame_id"] == 3
202
+
203
+ def test_clear_all(self):
204
+ """Test clearing the collection."""
205
+ self.store.add_frame(1, "06:00", "Gate", "Test frame", "test", "low")
206
+ assert self.store.get_frame_count() == 1
207
+
208
+ self.store.clear_all()
209
+ assert self.store.get_frame_count() == 0
tests/test_telemetry.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Tests for the telemetry simulator.
3
+ """
4
+
5
+ import sys
6
+ import os
7
+ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
8
+
9
+ from simulators.telemetry import TelemetrySimulator, get_telemetry_for_frame
10
+ from config import DRONE_ID
11
+
12
+
13
+ class TestTelemetrySimulator:
14
+ """Tests for TelemetrySimulator."""
15
+
16
+ def test_telemetry_structure(self):
17
+ """Test that telemetry output has all required fields."""
18
+ sim = TelemetrySimulator()
19
+ telemetry = sim.get_telemetry("06:00", "Main Gate")
20
+
21
+ assert "drone_id" in telemetry
22
+ assert "time" in telemetry
23
+ assert "location" in telemetry
24
+ assert "altitude_m" in telemetry
25
+ assert "battery_pct" in telemetry
26
+ assert "wind_speed_kmh" in telemetry
27
+ assert "gps_signal" in telemetry
28
+ assert "status" in telemetry
29
+
30
+ def test_drone_id(self):
31
+ """Test that the drone ID matches configuration."""
32
+ sim = TelemetrySimulator()
33
+ telemetry = sim.get_telemetry("06:00", "Main Gate")
34
+ assert telemetry["drone_id"] == DRONE_ID
35
+
36
+ def test_battery_drain(self):
37
+ """Test that battery decreases over time."""
38
+ sim = TelemetrySimulator()
39
+ t1 = sim.get_telemetry("06:00", "Main Gate")
40
+ t2 = sim.get_telemetry("06:15", "Parking Lot A")
41
+ t3 = sim.get_telemetry("06:30", "Warehouse Entrance")
42
+
43
+ assert t1["battery_pct"] >= t2["battery_pct"]
44
+ assert t2["battery_pct"] >= t3["battery_pct"]
45
+
46
+ def test_altitude_varies_by_location(self):
47
+ """Test that altitude varies based on location type."""
48
+ sim = TelemetrySimulator()
49
+ gate = sim.get_telemetry("06:00", "Main Gate")
50
+ perimeter = sim.get_telemetry("06:15", "Perimeter Fence North")
51
+
52
+ # Gate altitude: 10-15m, Perimeter: 15-25m
53
+ assert 8 <= gate["altitude_m"] <= 20
54
+ assert 10 <= perimeter["altitude_m"] <= 30
55
+
56
+ def test_location_coverage(self):
57
+ """Test telemetry works for all defined locations."""
58
+ sim = TelemetrySimulator()
59
+ locations = [
60
+ "Main Gate", "Perimeter Fence North", "Parking Lot A",
61
+ "Warehouse Entrance", "Loading Dock", "Server Room Exterior",
62
+ ]
63
+ for loc in locations:
64
+ telemetry = sim.get_telemetry("12:00", loc)
65
+ assert telemetry["location"] == loc
66
+
67
+ def test_reset(self):
68
+ """Test that reset restores battery."""
69
+ sim = TelemetrySimulator()
70
+ sim.get_telemetry("06:00", "Main Gate")
71
+ sim.get_telemetry("06:15", "Main Gate")
72
+ sim.get_telemetry("06:30", "Main Gate")
73
+
74
+ sim.reset()
75
+ fresh = sim.get_telemetry("07:00", "Main Gate")
76
+ assert fresh["battery_pct"] >= 95
77
+
78
+ def test_convenience_function(self):
79
+ """Test the get_telemetry_for_frame convenience function."""
80
+ frame = {"time": "09:00", "location": "Loading Dock"}
81
+ telemetry = get_telemetry_for_frame(frame)
82
+
83
+ assert telemetry["time"] == "09:00"
84
+ assert telemetry["location"] == "Loading Dock"
85
+
86
+ def test_wind_speed_range(self):
87
+ """Test wind speed is within reasonable range."""
88
+ sim = TelemetrySimulator()
89
+ for _ in range(20):
90
+ t = sim.get_telemetry("12:00", "Main Gate")
91
+ assert 0 <= t["wind_speed_kmh"] <= 20
92
+
93
+ def test_gps_signal_values(self):
94
+ """Test GPS signal is one of expected values."""
95
+ sim = TelemetrySimulator()
96
+ for _ in range(20):
97
+ t = sim.get_telemetry("12:00", "Main Gate")
98
+ assert t["gps_signal"] in ("strong", "moderate")