sneha #23
@@ -0,0 +1,160 @@
|
||||
<?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 = [];
|
||||
|
||||
// ✅ 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -48,8 +48,15 @@ class Asset extends Model
|
||||
});
|
||||
}
|
||||
|
||||
public function customer()
|
||||
{
|
||||
return $this->belongsTo(Customer::class, 'customer_xid', 'id');
|
||||
}
|
||||
|
||||
public function devices()
|
||||
{
|
||||
return $this->hasMany(Device::class, 'asset_id', 'id');
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
50
app/Services/CustomerInfoService.php
Normal file
50
app/Services/CustomerInfoService.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use App\Services\AdminService;
|
||||
|
||||
class CustomerInfoService
|
||||
{
|
||||
protected $adminService;
|
||||
|
||||
public function __construct(AdminService $adminService)
|
||||
{
|
||||
$this->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 [];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -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']);
|
||||
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user