From 71536952180e1a0f5fab8a1494fb4873eddd9b6f Mon Sep 17 00:00:00 2001 From: kshitige Date: Fri, 21 Mar 2025 12:11:20 +0530 Subject: [PATCH] customerdeviceInfo --- .../CustomerDeviceInfoController.php | 160 ++++++++++++++++++ app/Models/Asset.php | 6 + app/Models/Customer.php | 25 ++- app/Services/CustomerInfoService.php | 50 ++++++ routes/customer_api.php | 4 +- 5 files changed, 243 insertions(+), 2 deletions(-) create mode 100644 app/Http/Controllers/APIS/CustomerApi/CustomerDeviceInfoController.php create mode 100644 app/Services/CustomerInfoService.php diff --git a/app/Http/Controllers/APIS/CustomerApi/CustomerDeviceInfoController.php b/app/Http/Controllers/APIS/CustomerApi/CustomerDeviceInfoController.php new file mode 100644 index 0000000..4d9f01d --- /dev/null +++ b/app/Http/Controllers/APIS/CustomerApi/CustomerDeviceInfoController.php @@ -0,0 +1,160 @@ +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 = []; + + // ✅ Check Local Database + $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); + } +} + + + + + + + + + + + +} diff --git a/app/Models/Asset.php b/app/Models/Asset.php index 3ab270e..3e07357 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -35,9 +35,15 @@ class Asset extends Model 'additional_info' => 'array', ]; + public function customer() + { + return $this->belongsTo(Customer::class, 'customer_xid', 'id'); + } + public function devices() { return $this->hasMany(Device::class, 'asset_id', 'id'); } + } \ No newline at end of file diff --git a/app/Models/Customer.php b/app/Models/Customer.php index 8f386e0..95fd9e4 100644 --- a/app/Models/Customer.php +++ b/app/Models/Customer.php @@ -9,7 +9,7 @@ class Customer extends Model { use HasFactory; - protected $table = 'customers'; + protected $table = 'customers'; protected $fillable = [ 'id', @@ -37,4 +37,27 @@ class Customer extends Model 'external_id' => 'string', 'additional_info' => 'array', ]; + + + public function assets() +{ + return $this->hasMany(Asset::class, 'customer_id', 'id'); +} + +public function devices() +{ + return $this->hasManyThrough( + Device::class, // Final model + Asset::class, // Intermediate model + 'customer_id', // FK on Asset table + 'asset_id', // FK on Device table + 'id', // Local key on Customer table + 'id' // Local key on Asset table + ); +} + +public function users() + { + return $this->hasMany(User::class, 'customer_id', 'id'); + } } \ No newline at end of file diff --git a/app/Services/CustomerInfoService.php b/app/Services/CustomerInfoService.php new file mode 100644 index 0000000..205dc20 --- /dev/null +++ b/app/Services/CustomerInfoService.php @@ -0,0 +1,50 @@ +adminService = $adminService; + } + + public function getThingsBoardDevices($customerId) + { + try { + $token = $this->adminService->getToken(); + + if (!$token) { + Log::error("Failed to authenticate with ThingsBoard."); + return []; + } + + $response = Http::withHeaders([ + 'Authorization' => "Bearer $token", + 'Accept' => 'application/json', + ])->get("http://65.0.131.117:8080/api/customer/{$customerId}/deviceInfos?pageSize=100&page=0"); + + if (!$response->successful()) { + Log::error("Failed to fetch ThingsBoard devices: " . $response->body()); + return []; + } + + return $response->json()['data'] ?? []; + + } catch (\Exception $e) { + Log::error("Error fetching ThingsBoard devices: " . $e->getMessage()); + return []; + } + } + + + + + +} diff --git a/routes/customer_api.php b/routes/customer_api.php index 70553e8..0afe816 100644 --- a/routes/customer_api.php +++ b/routes/customer_api.php @@ -6,16 +6,18 @@ use App\Http\Controllers\APIS\CustomerApi\UserAssetLinkController; use Tymon\JWTAuth\Facades\JWTAuth; use App\Http\Controllers\APIS\CustomerApi\AuthController; +use App\Http\Controllers\APIS\CustomerApi\CustomerDeviceInfoController; + Route::get('/customerapi', function () { return ('Welcome to admin api routes.'); }); Route::post('user-login', [AuthController::class, 'login']); +Route::get('/customer-device-info/{customerId}',[CustomerDeviceInfoController::class,'customerDeviceInfo']); // Route::post('/user-login', [AuthController::class, 'login']); Route::middleware(['customerApiBasicAuth'])->group(function () { Route::get('/user-assets', [UserAssetLinkController::class, 'index']); }); -