-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweb.py
More file actions
279 lines (246 loc) · 8.48 KB
/
web.py
File metadata and controls
279 lines (246 loc) · 8.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
"""
Planetastic: Web Dashboard
Version: 2.1.0
Author: James Rich <james.a.rich@gmail.com>
"""
from aiohttp import web
import logging
from typing import Any
try:
from .db import DatabaseManager
except ImportError:
from db import DatabaseManager
logger = logging.getLogger(__name__)
# --- Dashboard Template ---
DASHBOARD_HTML = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Planetastic Dashboard</title>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;800&display=swap" rel="stylesheet">
<style>
:root {
--bg-color: #0d0f14;
--card-bg: #1a1d24;
--accent-color: #3b82f6;
--text-main: #ffffff;
--text-muted: #94a3b8;
--success-color: #10b981;
--alert-color: #ef4444;
}
body {
font-family: 'Inter', sans-serif;
background-color: var(--bg-color);
color: var(--text-main);
margin: 0;
display: flex;
flex-direction: column;
min-height: 100vh;
}
header {
padding: 2rem;
background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
border-bottom: 1px solid #334155;
display: flex;
justify-content: space-between;
align-items: center;
}
h1 {
margin: 0;
font-weight: 800;
font-size: 1.5rem;
letter-spacing: -0.025em;
display: flex;
align-items: center;
gap: 0.75rem;
}
.container {
padding: 2rem;
max-width: 1200px;
margin: 0 auto;
width: 100%;
box-sizing: border-box;
}
.stats-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 1.5rem;
margin-bottom: 2rem;
}
.stat-card {
background-color: var(--card-bg);
padding: 1.5rem;
border-radius: 1rem;
border: 1px solid #334155;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}
.stat-label {
color: var(--text-muted);
font-size: 0.875rem;
font-weight: 600;
text-transform: uppercase;
}
.stat-value {
font-size: 1.75rem;
font-weight: 800;
margin-top: 0.5rem;
}
.table-container {
background-color: var(--card-bg);
border-radius: 1rem;
border: 1px solid #334155;
overflow: hidden;
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
}
table {
width: 100%;
border-collapse: collapse;
text-align: left;
}
th {
background-color: #242933;
padding: 1rem;
color: var(--text-muted);
font-weight: 600;
font-size: 0.75rem;
text-transform: uppercase;
letter-spacing: 0.05em;
}
td {
padding: 1rem;
border-bottom: 1px solid #334155;
font-size: 0.9375rem;
}
tr:last-child td {
border-bottom: none;
}
tr:hover td {
background-color: #242933;
}
.badge {
padding: 0.25rem 0.5rem;
border-radius: 0.375rem;
font-size: 0.75rem;
font-weight: 600;
}
.badge-alt { background-color: rgba(59, 130, 246, 0.2); color: #60a5fa; }
.badge-speed { background-color: rgba(16, 185, 129, 0.2); color: #34d399; }
.badge-squawk { background-color: rgba(239, 68, 68, 0.2); color: #f87171; }
.callsign {
font-weight: 800;
color: var(--accent-color);
}
@keyframes pulse {
0% { opacity: 1; }
50% { opacity: 0.5; }
100% { opacity: 1; }
}
.live-indicator {
display: inline-block;
width: 8px;
height: 8px;
background-color: var(--success-color);
border-radius: 50%;
margin-right: 0.5rem;
animation: pulse 2s infinite;
}
</style>
</head>
<body>
<header>
<h1>✈️ Planetastic Gateway</h1>
<div><span class="live-indicator"></span> LIVE TRACKER</div>
</header>
<div class="container">
<div class="stats-grid">
<div class="stat-card">
<div class="stat-label">Active Aircraft</div>
<div id="stat-active" class="stat-value">0</div>
</div>
<div class="stat-card">
<div class="stat-label">Messages Seen</div>
<div id="stat-total" class="stat-value">0</div>
</div>
<div class="stat-card">
<div class="stat-label">Current Time</div>
<div id="stat-time" class="stat-value">--:--</div>
</div>
</div>
<div class="table-container">
<table>
<thead>
<tr>
<th>ICAO</th>
<th>Callsign</th>
<th>Altitude</th>
<th>Speed</th>
<th>Track</th>
<th>Squawk</th>
<th>Last Seen</th>
</tr>
</thead>
<tbody id="aircraft-table">
<!-- Rows will be populated by JS -->
</tbody>
</table>
</div>
</div>
<script>
async function updateData() {
try {
const response = await fetch('/api/aircraft');
const data = await response.json();
const tableBody = document.getElementById('aircraft-table');
tableBody.innerHTML = '';
data.forEach(ac => {
const row = document.createElement('tr');
const lastSeen = new Date(ac.last_seen).toLocaleTimeString();
const callsign = ac.callsign ? ac.callsign.trim() : '---';
const sq = ac.squawk || '---';
const sqClass = [7500, 7600, 7700].includes(ac.squawk) ? 'badge badge-squawk' : '';
row.innerHTML = `
<td><code>${ac.hex_ident}</code></td>
<td><span class="callsign">${callsign}</span></td>
<td><span class="badge badge-alt">${ac.altitude || 0} ft</span></td>
<td><span class="badge badge-speed">${ac.ground_speed || 0} kt</span></td>
<td>${ac.track ? ac.track + '°' : '---'}</td>
<td><span class="${sqClass}">${sq}</span></td>
<td style="color: var(--text-muted)">${lastSeen}</td>
`;
tableBody.appendChild(row);
});
document.getElementById('stat-active').innerText = data.length;
document.getElementById('stat-time').innerText = new Date().toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'});
} catch (e) {
console.error("Failed to fetch data", e);
}
}
setInterval(updateData, 2000);
updateData();
</script>
</body>
</html>
"""
class DashboardServer:
def __init__(self, db_manager: DatabaseManager, host: str = '0.0.0.0', port: int = 8080):
self.db_manager = db_manager
self.host = host
self.port = port
self.app = web.Application()
self.app.add_routes([
web.get('/', self.handle_index),
web.get('/api/aircraft', self.handle_api)
])
async def handle_index(self, request: web.Request) -> web.Response:
return web.Response(text=DASHBOARD_HTML, content_type='text/html')
async def handle_api(self, request: web.Request) -> web.Response:
aircraft = await self.db_manager.get_all_aircraft()
return web.json_response(aircraft)
async def start(self) -> None:
runner = web.AppRunner(self.app)
await runner.setup()
site = web.TCPSite(runner, self.host, self.port)
await site.start()
logger.info(f"Dashboard available at http://{self.host}:{self.port}")