customerInfoService = $customerInfoService; } // public function customerDeviceInfo(Request $request) // { // try { // $tokenPayload = getTokenFromHeader($request); // if (isset($tokenPayload['error'])) { // return response()->json($tokenPayload, 401); // } // $userId = $tokenPayload['sub']; // // Get user with assets and devices // $user = User::with(['assets' => function($query) { // $query->with(['devices:id,name,asset_id']); // }]) // ->where('id', $userId) // ->first(); // if (!$user) { // return response()->json(['error' => 'User not found'], 404); // } // try { // // Get devices data from ThingsBoard service // $devicesArray = $this->customerInfoService->getThingsBoardDevices($user->customer_id); // if (!is_array($devicesArray)) { // throw new \Exception("Invalid devices data received from service"); // } // $thingsBoardDevices = collect($devicesArray); // // Process devices // $deviceDetails = $user->assets->flatMap(function ($asset) use ($thingsBoardDevices) { // return $thingsBoardDevices->whereIn('id.id', $asset->devices->pluck('id')->toArray()) // ->map(function ($tbDevice) { // return [ // 'device_id' => $tbDevice['id']['id'], // 'active' => $tbDevice['active'] ?? false // ]; // }); // }); // // Calculate counts // $good = $deviceDetails->where('active', true)->count(); // $moderate = $deviceDetails->where('active', false)->count(); // $total_count = $good + $moderate; // // Successful response // return response()->json([ // 'good' => $good, // 'moderate' => $moderate, // 'total_count' => $total_count // ], 200); // } catch (\Exception $serviceException) { // Log::error("ThingsBoard service error: " . $serviceException->getMessage()); // return response()->json([ // 'error' => 'Failed to fetch device information', // 'details' => $serviceException->getMessage() // ], 503); // } // } catch (\Exception $e) { // Log::error("Customer device info error: " . $e->getMessage()); // return response()->json([ // 'error' => 'An unexpected error occurred', // 'details' => $e->getMessage() // ], 500); // } // } // public function customerDeviceInfo(Request $request) // { // try { // $token = readHeaderToken(); // if (!$token) { // return response()->json([ // 'success' => false, // 'error' => 'Authorization token required' // ], 401); // } // $user = User::with(['assets.devices:id,name,asset_id']) // ->find($token['sub']); // if (!$user) { // return response()->json([ // 'error' => 'user_not_found', // 'message' => 'User not found' // ], 404); // } // $devicesArray = $this->customerInfoService->getThingsBoardDevices($user->customer_id); // if (isset($devicesArray['error'])) { // throw new \Exception($devicesArray['message'] ?? 'Failed to fetch devices'); // } // if (!is_array($devicesArray)) { // throw new \Exception("Invalid devices data format received from service"); // } // $userDeviceIds = $user->assets->flatMap->devices->pluck('id')->toArray(); // $activeCount = collect($devicesArray) // ->whereIn('id.id', $userDeviceIds) // ->where('active', true) // ->count(); // $deviceIdCount = count($userDeviceIds); // return response()->json([ // 'data' => [ // 'active_count' => $activeCount, // 'total_count' => $deviceIdCount, // ], // 'success' => true // ], 200); // } catch (\Exception $e) { // Log::error("Device info error: " . $e->getMessage()); // $statusCode = ($e->getMessage() === "Invalid devices data format received from service") ? 503 : 500; // return response()->json([ // 'error' => 'server_error', // 'message' => 'An error occurred', // 'details' => $e->getMessage() // ], $statusCode); // } // } public function customerDeviceInfo(Request $request) { try { $token = readHeaderToken($request); if (!$token) { return response()->json([ 'success' => false, 'error' => 'authorization_required', 'message' => 'Authorization token required' ], 401); } // Get user with devices $user = User::with(['assets.devices:id,name,asset_id']) ->find($token['sub']); if (!$user) { return response()->json([ 'success' => false, 'error' => 'user_not_found', 'message' => 'User not found' ], 404); } // Get user's device IDs $userDeviceIds = $user->assets->flatMap->devices->pluck('id')->toArray(); if (empty($userDeviceIds)) { return response()->json([ 'data' => [ 'active_count' => 0, 'total_count' => 0, 'alarms_count' => 0 ], 'success' => true ], 200); } // Get devices and alarms from service $serviceResponse = $this->customerInfoService->getCustomerDevicesAndAlarms( $user->customer_id, $userDeviceIds ); // Handle service errors if (isset($serviceResponse['error'])) { throw new \Exception($serviceResponse['message'] ?? 'Service request failed'); } // Process devices $devices = $serviceResponse['devices'] ?? []; $activeCount = collect($devices) ->whereIn('id.id', $userDeviceIds) ->where('active', true) ->count(); // Process alarms $alarms = $serviceResponse['alarms'] ?? []; $recentAlarms = $this->filterRecentAlarms($alarms); return response()->json([ 'data' => [ 'active_count' => $activeCount, 'total_count' => count($userDeviceIds), 'alarms_count' => count($recentAlarms), ], 'success' => true ], 200); } catch (\Exception $e) { Log::error("Device info error: " . $e->getMessage()); return response()->json([ 'success' => false, 'error' => 'server_error', 'message' => 'An error occurred while fetching device information', 'details' => $e->getMessage() ], 500); } } private function filterRecentAlarms(array $alarms): array { $twentyFourHoursAgo = (time() - 86400) * 1000; return array_values(array_filter($alarms, function($alarm) use ($twentyFourHoursAgo) { return ($alarm['createdTime'] ?? 0) >= $twentyFourHoursAgo; })); } }