customerInfoService = $customerInfoService; } public function telemetryData($assetId) { // Fetch devices with asset and telemetry keys + device profile $devices = Device::with(['asset', 'timeseriesKeys.deviceProfile']) ->where('asset_id', $assetId) ->get(); if ($devices->isEmpty()) { return response()->json(['error' => 'No devices found for the asset'], 404); } $telemetryData = []; // Set start and end timestamps $startTs = now()->subHours(1)->timestamp * 1000; $endTs = now()->timestamp * 1000; // Fetch telemetry data from ThingsBoard $thingsBoardData = $this->customerInfoService->getTelemetryData($devices, $startTs, $endTs); foreach ($devices as $device) { $deviceToken = $device->token; // Use token // Find telemetry data by token $telemetry = collect($thingsBoardData) ->firstWhere('device_token', $deviceToken); $telemetryData[] = [ 'device_name' => $device->name, 'telemetry' => $telemetry['telemetry'] ?? [], ]; } return response()->json([ 'telemetry' => $telemetryData ]); } }