-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_client.py
More file actions
executable file
·405 lines (310 loc) · 13.9 KB
/
api_client.py
File metadata and controls
executable file
·405 lines (310 loc) · 13.9 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/usr/bin/env python3
"""
HLV-RAPS REST API Client Example
Demonstrates how to query the HLV-RAPS Flight Middleware REST API
for observability and telemetry data.
Usage:
python api_client.py [host] [port]
Example:
python api_client.py 192.168.1.100 8080
"""
import sys
import json
import time
from typing import Dict, Any, Optional
import urllib.request
import urllib.error
class RapsApiClient:
"""Client for HLV-RAPS REST API"""
def __init__(self, host: str = "localhost", port: int = 8080):
"""Initialize API client
Args:
host: API server hostname or IP address
port: API server port
"""
self.base_url = f"http://{host}:{port}"
def _get(self, endpoint: str) -> Dict[str, Any]:
"""Make GET request to API endpoint
Args:
endpoint: API endpoint path (e.g., '/health')
Returns:
Parsed JSON response
Raises:
urllib.error.URLError: If request fails
"""
url = f"{self.base_url}{endpoint}"
try:
with urllib.request.urlopen(url, timeout=5) as response:
data = response.read()
return json.loads(data)
except urllib.error.HTTPError as e:
error_body = e.read().decode('utf-8')
try:
error_data = json.loads(error_body)
error_msg = error_data.get('error', str(e))
except json.JSONDecodeError:
error_msg = str(e)
raise Exception(f"HTTP {e.code}: {error_msg}")
def health(self) -> Dict[str, Any]:
"""Get health status
Returns:
Health check response
"""
return self._get("/health")
def get_state(self) -> Dict[str, Any]:
"""Get current state (Ψ and Φ)
Returns:
Current physical and informational state
"""
return self._get("/api/state")
def get_pdt(self) -> Dict[str, Any]:
"""Get PDT predictions
Returns:
Predictive Digital Twin predictions with confidence/uncertainty
"""
return self._get("/api/pdt")
def get_dsm(self) -> Dict[str, Any]:
"""Get DSM safety status
Returns:
Deterministic Safety Monitor status and gates
"""
return self._get("/api/dsm")
def get_supervisor(self) -> Dict[str, Any]:
"""Get supervisor state
Returns:
Redundant supervisor state (A/B, failover, mismatch)
"""
return self._get("/api/supervisor")
def get_rollback(self) -> Dict[str, Any]:
"""Get rollback status
Returns:
Rollback status and last rollback event
"""
return self._get("/api/rollback")
def get_itl(self) -> Dict[str, Any]:
"""Get ITL telemetry entries
Returns:
Latest N telemetry entries from Immutable Telemetry Ledger
"""
return self._get("/api/itl")
def print_separator(title: str = ""):
"""Print a section separator"""
if title:
print(f"\n{'='*60}")
print(f" {title}")
print(f"{'='*60}")
else:
print(f"{'='*60}")
def print_health(client: RapsApiClient):
"""Print health check information"""
print_separator("Health Check")
try:
health = client.health()
print(f"Status: {health.get('status', 'unknown')}")
print(f"Service: {health.get('service', 'unknown')}")
print(f"API Version: {health.get('api_version', 'unknown')}")
print(f"Observability Only: {health.get('observability_only', False)}")
except Exception as e:
print(f"Error: {e}")
def print_state(client: RapsApiClient):
"""Print current state information"""
print_separator("Current State (Ψ and Φ)")
try:
state = client.get_state()
if not state.get('valid', False):
print("⚠ Warning: State snapshot not valid")
print(f"Timestamp: {state.get('timestamp_ms', 0)} ms")
# Physical state
physics = state.get('physics_state', {})
print("\nPhysical State (Ψ):")
pos = physics.get('position_m', [0, 0, 0])
vel = physics.get('velocity_m_s', [0, 0, 0])
print(f" Position (m): [{pos[0]:12.2f}, {pos[1]:12.2f}, {pos[2]:12.2f}]")
print(f" Velocity (m/s): [{vel[0]:12.2f}, {vel[1]:12.2f}, {vel[2]:12.2f}]")
print(f" Mass (kg): {physics.get('mass_kg', 0):12.2f}")
# Spacetime state (if available)
if 'spacetime_state' in state:
st = state['spacetime_state']
print("\nInformational State (Φ):")
print(f" Warp Field: {st.get('warp_field_strength', 0):.6f}")
print(f" Gravito Flux Bias: {st.get('gravito_flux_bias', 0):.6f}")
print(f" Curvature Magnitude: {st.get('spacetime_curvature_magnitude', 0):.9e}")
print(f" Time Dilation: {st.get('time_dilation_factor', 1):.9f}")
print(f" Stability Index: {st.get('spacetime_stability_index', 0):.6f}")
print(f" Control Authority: {st.get('control_authority_remaining', 0):.6f}")
print(f" Antimatter Remaining: {st.get('remaining_antimatter_kg', 0):.2f} kg")
print(f" Emergency Mode: {st.get('emergency_mode_active', False)}")
except Exception as e:
print(f"Error: {e}")
def print_pdt(client: RapsApiClient):
"""Print PDT prediction information"""
print_separator("PDT Predictions")
try:
pdt = client.get_pdt()
if not pdt.get('valid', False):
print("⚠ Warning: PDT snapshot not valid")
status = pdt.get('status', 'UNKNOWN')
confidence = pdt.get('confidence', 0)
uncertainty = pdt.get('uncertainty', 0)
print(f"Timestamp: {pdt.get('timestamp_ms', 0)} ms")
print(f"Status: {status}")
print(f"Confidence: {confidence:.4f} ({confidence*100:.2f}%)")
print(f"Uncertainty: {uncertainty:.4f} ({uncertainty*100:.2f}%)")
# Status indicator
if status == "NOMINAL":
print("✓ Prediction nominal")
elif status == "PREDICTED_ESE":
print("⚠ WARNING: Predicted Excursion Safety Event (ESE)")
# Predicted end state
end_state = pdt.get('predicted_end_state', {})
if end_state:
print("\nPredicted End State:")
pos = end_state.get('position_m', [0, 0, 0])
vel = end_state.get('velocity_m_s', [0, 0, 0])
print(f" Position (m): [{pos[0]:12.2f}, {pos[1]:12.2f}, {pos[2]:12.2f}]")
print(f" Velocity (m/s): [{vel[0]:12.2f}, {vel[1]:12.2f}, {vel[2]:12.2f}]")
print(f" Mass (kg): {end_state.get('mass_kg', 0):12.2f}")
except Exception as e:
print(f"Error: {e}")
def print_dsm(client: RapsApiClient):
"""Print DSM safety status"""
print_separator("DSM Safety Status")
try:
dsm = client.get_dsm()
if not dsm.get('valid', False):
print("⚠ Warning: DSM snapshot not valid")
action = dsm.get('current_action', 'UNKNOWN')
safing = dsm.get('safing_sequence_active', False)
print(f"Timestamp: {dsm.get('timestamp_ms', 0)} ms")
print(f"Current Action: {action}")
print(f"Safing Active: {safing}")
# Action indicator
if action == "NONE":
print("✓ No safety action required")
elif action == "ROLLBACK":
print("⚠ WARNING: ROLLBACK action triggered")
elif action == "FULL_SHUTDOWN":
print("🛑 CRITICAL: FULL SHUTDOWN triggered")
print("\nSafety Metrics:")
print(f" Curvature (R_max): {dsm.get('last_estimated_curvature', 0):.9e}")
print(f" Time Dilation: {dsm.get('measured_time_dilation', 1):.9f}")
print(f" Oscillatory Prefactor: {dsm.get('measured_oscillatory_prefactor', 0):.6f}")
print(f" TCC Coupling (J): {dsm.get('measured_tcc_coupling', 0):.2f}")
print(f" Resonance Amplitude: {dsm.get('current_resonance_amplitude', 0):.6f}")
print(f" Main Control Healthy: {dsm.get('main_control_healthy', False)}")
except Exception as e:
print(f"Error: {e}")
def print_supervisor(client: RapsApiClient):
"""Print supervisor state"""
print_separator("Supervisor State")
try:
supervisor = client.get_supervisor()
if not supervisor.get('valid', False):
print("⚠ Warning: Supervisor snapshot not valid")
channel = supervisor.get('active_channel', 'UNKNOWN')
mismatch = supervisor.get('has_prediction_mismatch', False)
print(f"Timestamp: {supervisor.get('timestamp_ms', 0)} ms")
print(f"Active Channel: {channel}")
print(f"Prediction Mismatch: {mismatch}")
print(f"Last Sync: {supervisor.get('last_sync_timestamp_ms', 0)} ms")
print(f"Last Confidence: {supervisor.get('last_prediction_confidence', 0):.4f}")
print(f"Last Uncertainty: {supervisor.get('last_prediction_uncertainty', 0):.4f}")
if mismatch:
print("\n⚠ WARNING: A/B prediction mismatch detected")
except Exception as e:
print(f"Error: {e}")
def print_rollback(client: RapsApiClient):
"""Print rollback status"""
print_separator("Rollback Status")
try:
rollback = client.get_rollback()
if not rollback.get('valid', False):
print("⚠ Warning: Rollback snapshot not valid")
has_plan = rollback.get('has_rollback_plan', False)
count = rollback.get('rollback_count', 0)
print(f"Timestamp: {rollback.get('timestamp_ms', 0)} ms")
print(f"Has Rollback Plan: {has_plan}")
print(f"Rollback Count: {count}")
if has_plan and 'last_rollback_plan' in rollback:
plan = rollback['last_rollback_plan']
print("\nLast Rollback Plan:")
print(f" Policy ID: {plan.get('policy_id', 'unknown')}")
print(f" Thrust (kN): {plan.get('thrust_magnitude_kN', 0):.2f}")
print(f" Gimbal Theta: {plan.get('gimbal_theta_rad', 0):.6f} rad")
print(f" Gimbal Phi: {plan.get('gimbal_phi_rad', 0):.6f} rad")
except Exception as e:
print(f"Error: {e}")
def print_itl(client: RapsApiClient):
"""Print ITL telemetry entries"""
print_separator("ITL Telemetry")
try:
itl = client.get_itl()
if not itl.get('valid', False):
print("⚠ Warning: ITL snapshot not valid")
count = itl.get('count', 0)
print(f"Timestamp: {itl.get('timestamp_ms', 0)} ms")
print(f"Entry Count: {count}")
entries = itl.get('entries', [])
if entries:
print("\nRecent Entries:")
for i, entry in enumerate(entries[:10]): # Show last 10
print(f"\n [{i+1}] Type {entry.get('type', 0)} @ {entry.get('timestamp_ms', 0)} ms")
print(f" {entry.get('summary', 'No summary')}")
except Exception as e:
print(f"Error: {e}")
def monitor_loop(client: RapsApiClient, interval: float = 2.0):
"""Continuous monitoring loop
Args:
client: API client instance
interval: Update interval in seconds
"""
print_separator("Monitoring Mode")
print(f"Updating every {interval} seconds. Press Ctrl+C to stop.\n")
try:
while True:
# Clear screen (works on most terminals)
print("\033[2J\033[H", end='')
print(f"HLV-RAPS Telemetry Monitor - {time.strftime('%Y-%m-%d %H:%M:%S')}")
# Quick status
try:
pdt = client.get_pdt()
dsm = client.get_dsm()
supervisor = client.get_supervisor()
print(f"\nPDT: {pdt.get('status', 'UNKNOWN')} "
f"(confidence: {pdt.get('confidence', 0):.2f})")
print(f"DSM: {dsm.get('current_action', 'UNKNOWN')}")
print(f"Supervisor: Channel {supervisor.get('active_channel', '?')}")
if pdt.get('status') == 'PREDICTED_ESE':
print("\n⚠ WARNING: ESE predicted!")
if dsm.get('current_action') != 'NONE':
print(f"\n⚠ WARNING: Safety action: {dsm.get('current_action')}")
if supervisor.get('has_prediction_mismatch'):
print("\n⚠ WARNING: Prediction mismatch!")
except Exception as e:
print(f"\nError: {e}")
time.sleep(interval)
except KeyboardInterrupt:
print("\n\nMonitoring stopped.")
def main():
"""Main entry point"""
# Parse command line arguments
host = sys.argv[1] if len(sys.argv) > 1 else "localhost"
port = int(sys.argv[2]) if len(sys.argv) > 2 else 8080
print(f"Connecting to HLV-RAPS API at {host}:{port}")
# Create client
client = RapsApiClient(host, port)
# Print all telemetry
print_health(client)
print_state(client)
print_pdt(client)
print_dsm(client)
print_supervisor(client)
print_rollback(client)
print_itl(client)
print_separator()
print("\nFor continuous monitoring, uncomment the monitor_loop() call in main()")
print("Example: monitor_loop(client, interval=2.0)")
# Uncomment to enable continuous monitoring:
# monitor_loop(client, interval=2.0)
if __name__ == "__main__":
main()