diff --git a/app/Http/Controllers/APIS/CustomerApi/TelemetryController.php b/app/Http/Controllers/APIS/CustomerApi/TelemetryController.php index 42d02af..fe52ba0 100644 --- a/app/Http/Controllers/APIS/CustomerApi/TelemetryController.php +++ b/app/Http/Controllers/APIS/CustomerApi/TelemetryController.php @@ -986,43 +986,71 @@ class TelemetryController extends Controller } } - public function getActiveDevicesList(){ - try { - $token = readHeaderToken(); - $devices = UserAssetLink::with('asset.devices') - ->where(['user_id' => $token['sub'], 'active' => 1]) - ->get() - ->pluck('asset.devices') - ->flatten() - ->unique('id'); - $deviceIds = $devices->pluck('id')->toArray(); - $activeDevices = $this->customerInfoService->getActiveDevicesList($deviceIds); - $deviceCount = $this->customerInfoService->getDevicesCount($deviceIds); + public function getActiveDevicesList() { + try { + $token = readHeaderToken(); + $userId = $token['sub']; - $activeDeviceIds = collect($activeDevices['activeDevices'])->pluck('entityId')->pluck('id')->toArray(); + $assetDeviceLinks = UserAssetLink::with('asset.devices') + ->where(['user_id' => $userId, 'active' => 1]) + ->get(); - $getDevices = Device::with('asset')->whereIn('id', $activeDeviceIds)->get(['id', 'name', 'label', 'asset_id', 'customer_id', 'type', 'created_at']); + // Maintain order as per userAssetsNew + $orderedDeviceIds = []; - $liveDevices = $getDevices->map(function($device){ - - $assetName = Asset::where('id', $device->asset_id)->first()->name; - - return [ - 'deviceId' => $device->id, - 'assetName' => $assetName, - 'deviceName' => $device->label, - 'deviceType' => $device->type, - 'deviceCustomer' => $device->customer->title, - 'deviceCreatedAt' => $device->created_at ?? 'N/A' - ]; - }); - - - return response()->json(['success' => true, 'totalDevices' => $deviceCount['totalDevices'] ?? 0, 'activeDevices' => $deviceCount['activeDevices'] ?? 0, 'data' => $liveDevices]); - } catch(Exception $e){ - return response()->json(['success' => false, 'message' => $e->getMessage()], 500); + foreach ($assetDeviceLinks as $link) { + foreach ($link->asset->devices as $device) { + $orderedDeviceIds[] = $device->id; + } } + + // Remove duplicates while keeping order + $orderedDeviceIds = array_values(array_unique($orderedDeviceIds)); + + // Fetch only active devices + $activeDevices = $this->customerInfoService->getActiveDevicesList($orderedDeviceIds); + $activeDeviceIds = collect($activeDevices['activeDevices'])->pluck('entityId')->pluck('id')->toArray(); + + // Intersect ordered list with active IDs while preserving order + $orderedActiveDeviceIds = array_values(array_filter($orderedDeviceIds, function ($id) use ($activeDeviceIds) { + return in_array($id, $activeDeviceIds); + })); + + // Fetch device details + $getDevices = Device::with(['asset', 'customer']) + ->whereIn('id', $orderedActiveDeviceIds) + ->get(['id', 'name', 'label', 'asset_id', 'customer_id', 'type', 'created_at']); + + // Maintain order + $deviceMap = $getDevices->keyBy('id'); + $liveDevices = collect($orderedActiveDeviceIds)->map(function ($id) use ($deviceMap) { + $device = $deviceMap[$id] ?? null; + if (!$device) return null; + + return [ + 'deviceId' => $device->id, + 'assetName' => $device->asset->name ?? 'N/A', + 'deviceName' => $device->label, + 'deviceType' => $device->type, + 'deviceCustomer' => $device->customer->title ?? 'N/A', + 'deviceCreatedAt' => $device->created_at ?? 'N/A' + ]; + })->filter(); // remove nulls + + // Count + $deviceCount = $this->customerInfoService->getDevicesCount($orderedDeviceIds); + + return response()->json([ + 'success' => true, + 'totalDevices' => $deviceCount['totalDevices'] ?? 0, + 'activeDevices' => $deviceCount['activeDevices'] ?? 0, + 'data' => $liveDevices + ]); + } catch (Exception $e) { + return response()->json(['success' => false, 'message' => $e->getMessage()], 500); } +} + public function getDeviceIndicators($assetId){