|
2 | 2 |
|
3 | 3 | from django.db import transaction |
4 | 4 | from django.utils import timezone |
| 5 | +from django.conf import settings |
| 6 | +from django.db import connections |
5 | 7 | from rest_framework.permissions import AllowAny |
6 | 8 | from rest_framework.response import Response |
7 | 9 | from rest_framework.views import APIView |
8 | 10 |
|
9 | 11 | from .agent_auth import authenticate_agent |
10 | | -from .models import Device, DeviceCommand, DeviceCommandLog |
| 12 | +from .models import Device, DeviceCommand, DeviceCommandLog, TelemetryRecord |
11 | 13 | from .realtime import publish_command_log, publish_command_status, publish_device_update, publish_network_update |
12 | 14 | from .automation_engine import run_jobs_for_event |
13 | 15 |
|
@@ -65,13 +67,42 @@ def get(self, request): |
65 | 67 | auth = authenticate_agent(request) |
66 | 68 | device = auth.device |
67 | 69 |
|
| 70 | + debug_enabled = bool(settings.DEBUG) and (str(request.query_params.get('debug') or '').strip() == '1') |
| 71 | + |
68 | 72 | cmd = ( |
69 | 73 | DeviceCommand.objects.filter(device=device, status=DeviceCommand.Status.QUEUED) |
70 | 74 | .order_by('queued_at', 'id') |
71 | 75 | .first() |
72 | 76 | ) |
73 | 77 |
|
74 | 78 | if not cmd: |
| 79 | + if debug_enabled: |
| 80 | + try: |
| 81 | + db_name = connections['default'].settings_dict.get('NAME') |
| 82 | + except Exception: |
| 83 | + db_name = None |
| 84 | + |
| 85 | + try: |
| 86 | + total_for_device = DeviceCommand.objects.filter(device=device).count() |
| 87 | + queued_for_device = DeviceCommand.objects.filter( |
| 88 | + device=device, status=DeviceCommand.Status.QUEUED |
| 89 | + ).count() |
| 90 | + except Exception: |
| 91 | + total_for_device = None |
| 92 | + queued_for_device = None |
| 93 | + |
| 94 | + return Response( |
| 95 | + { |
| 96 | + 'command': None, |
| 97 | + 'debug': { |
| 98 | + 'device_id': device.id, |
| 99 | + 'db_name': db_name, |
| 100 | + 'total_commands_for_device': total_for_device, |
| 101 | + 'queued_commands_for_device': queued_for_device, |
| 102 | + }, |
| 103 | + } |
| 104 | + ) |
| 105 | + |
75 | 106 | return Response({'command': None}) |
76 | 107 |
|
77 | 108 | cmd.status = DeviceCommand.Status.DELIVERED |
@@ -217,3 +248,73 @@ def post(self, request, command_id: int): |
217 | 248 | publish_network_update({'reason': 'command_finished', 'device_id': cmd.device_id, 'command_id': cmd.id}) |
218 | 249 |
|
219 | 250 | return Response({'detail': 'finished', 'id': cmd.id, 'status': cmd.status}) |
| 251 | + |
| 252 | + |
| 253 | +class AgentTelemetryIngestView(APIView): |
| 254 | + """Allow an edge agent to ingest telemetry using device token auth. |
| 255 | +
|
| 256 | + Payload: |
| 257 | + - timestamp: optional ISO string |
| 258 | + - metrics: dict |
| 259 | + - raw_text: optional string |
| 260 | + """ |
| 261 | + |
| 262 | + permission_classes = [AllowAny] |
| 263 | + |
| 264 | + def post(self, request): |
| 265 | + auth = authenticate_agent(request) |
| 266 | + device = auth.device |
| 267 | + |
| 268 | + ts_raw = request.data.get('timestamp') |
| 269 | + metrics = request.data.get('metrics') or {} |
| 270 | + raw_text = request.data.get('raw_text') or '' |
| 271 | + |
| 272 | + # Parse timestamp if provided; otherwise use now. |
| 273 | + timestamp = timezone.now() |
| 274 | + if ts_raw: |
| 275 | + try: |
| 276 | + # Django parses ISO 8601 with fromisoformat for basic cases. |
| 277 | + timestamp = timezone.datetime.fromisoformat(str(ts_raw).replace('Z', '+00:00')) |
| 278 | + if timezone.is_naive(timestamp): |
| 279 | + timestamp = timezone.make_aware(timestamp, timezone=timezone.utc) |
| 280 | + except Exception: |
| 281 | + timestamp = timezone.now() |
| 282 | + |
| 283 | + rec = TelemetryRecord.objects.create( |
| 284 | + device=device, |
| 285 | + timestamp=timestamp, |
| 286 | + metrics=metrics if isinstance(metrics, dict) else {}, |
| 287 | + raw_text=str(raw_text)[:20000], |
| 288 | + created_by=None, |
| 289 | + ) |
| 290 | + |
| 291 | + # Mark device online. |
| 292 | + Device.objects.filter(id=device.id).update(last_seen_at=timestamp, status=Device.Status.ONLINE) |
| 293 | + |
| 294 | + # Fire any telemetry-driven automations. |
| 295 | + try: |
| 296 | + run_jobs_for_event(event_type='telemetry', telemetry=rec, actor=None) |
| 297 | + except Exception: |
| 298 | + pass |
| 299 | + |
| 300 | + # Publish to WS dashboards. |
| 301 | + try: |
| 302 | + from .realtime import publish_telemetry_record |
| 303 | + |
| 304 | + publish_telemetry_record( |
| 305 | + rec.device_id, |
| 306 | + { |
| 307 | + 'id': rec.id, |
| 308 | + 'device': rec.device_id, |
| 309 | + 'device_name': getattr(device, 'name', None), |
| 310 | + 'timestamp': rec.timestamp.isoformat() if rec.timestamp else None, |
| 311 | + 'metrics': rec.metrics, |
| 312 | + 'raw_text': rec.raw_text, |
| 313 | + 'created_at': rec.created_at.isoformat() if rec.created_at else None, |
| 314 | + }, |
| 315 | + ) |
| 316 | + except Exception: |
| 317 | + pass |
| 318 | + |
| 319 | + publish_network_update({'reason': 'agent_telemetry', 'device_id': device.id, 'telemetry_id': rec.id}) |
| 320 | + return Response({'detail': 'ok', 'device_id': device.id, 'telemetry_id': rec.id}) |
0 commit comments