Files
backend_vib360_laravel/app/Http/Controllers/APIS/CustomerApi/CustomerDeviceInfoController.php

160 lines
4.8 KiB
PHP
Raw Normal View History

2025-03-21 12:11:20 +05:30
<?php
namespace App\Http\Controllers\APIS\CustomerApi;
use App\Http\Controllers\Controller;
use App\Models\Customer;
use Illuminate\Http\Request;
use App\Services\CustomerInfoService;
use Illuminate\Support\Facades\Log;
class CustomerDeviceInfoController extends Controller
{
protected $customerInfoService;
public function __construct(CustomerInfoService $customerInfoService)
{
$this->customerInfoService = $customerInfoService;
}
// public function customerDeviceInfo($customerId)
// {
// try {
// $localActiveCount = 0;
// $customer = Customer::with('users.assets.devices')
// ->findOrFail($customerId);
// foreach ($customer->users as $user) {
// foreach ($user->assets as $asset) {
// $localActiveCount += $asset->devices->where('active', true)->count();
// }
// }
// $thingsBoardActiveCount = $this->customerInfoService->getThingsBoardActiveDevicesCount($customerId);
// if ($thingsBoardActiveCount === null) {
// Log::warning("Failed to fetch ThingsBoard devices, setting count to 0.");
// $thingsBoardActiveCount = 0;
// }
// return response()->json([
// 'customer_id' => $customerId,
// 'local_active_devices_count' => $localActiveCount,
// 'thingsboard_active_devices_count' => $thingsBoardActiveCount,
// 'total_active_devices_count' => $localActiveCount + $thingsBoardActiveCount
// ]);
// } catch (\Exception $e) {
// Log::error("Error fetching active devices: " . $e->getMessage());
// return response()->json([
// 'error' => 'Failed to fetch active devices'
// ], 500);
// }
// }
public function customerDeviceInfo($customerId)
{
try {
$data = [];
$customer = Customer::with('users.assets.devices')->findOrFail($customerId);
foreach ($customer->users as $user) {
foreach ($user->assets as $asset) {
$localGood = $asset->devices->where('active', true)->count();
$localModerate = $asset->devices->where('active', false)->count();
$data[] = [
'customer_id' => $customerId,
'user_id' => $user->id,
'asset_id' => $asset->id,
'good' => $localGood,
'moderate' => $localModerate
];
}
}
// ✅ Fetch Correct Customer ID from ThingsBoard
$correctCustomerId = $this->customerInfoService->getThingsBoardDevices('784f394c-42b6-435a-983c-b7beff2784f9');
if (!$correctCustomerId) {
Log::warning("No valid customer ID found for Device ID.");
return response()->json([
'status' => 404,
'error' => 'Customer ID not found for the device',
'data' => $data
], 404);
}
// ✅ Fetch ThingsBoard Devices
$thingsBoardDevices = $this->customerInfoService->getThingsBoardDevices($correctCustomerId);
if (empty($thingsBoardDevices)) {
Log::warning("No devices found for Customer ID: $correctCustomerId");
return response()->json([
'status' => 200,
'customer_id' => $customerId,
'data' => $data
], 200);
}
// ✅ Aggregate ThingsBoard Data
foreach ($customer->users as $user) {
foreach ($user->assets as $asset) {
$tbGood = 0;
$tbModerate = 0;
foreach ($thingsBoardDevices as $device) {
$isActive = $device['active'] ?? false;
if ($isActive) {
$tbGood++;
} else {
$tbModerate++;
}
}
$data[] = [
'customer_id' => $customerId,
'user_id' => $user->id,
'asset_id' => $asset->id,
'good' => $tbGood,
'moderate' => $tbModerate
];
}
}
// ✅ Return Final Response
return response()->json([
'status' => 200,
'customer_id' => $customerId,
'data' => $data
], 200);
} catch (\Exception $e) {
Log::error("Error fetching customer device info: " . $e->getMessage());
return response()->json([
'status' => 500,
'error' => 'Failed to fetch customer device info'
], 500);
}
}
}