2025-03-11 17:15:41 +05:30
|
|
|
<?php
|
|
|
|
|
|
2025-03-11 17:52:55 +05:30
|
|
|
namespace App\Http\Controllers\APIS\CustomerApi;
|
2025-03-11 17:15:41 +05:30
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2025-03-24 13:31:06 +05:30
|
|
|
use App\Models\Asset;
|
2025-03-11 17:52:55 +05:30
|
|
|
use App\Models\User;
|
2025-03-11 17:15:41 +05:30
|
|
|
use App\Models\UserAssetLink;
|
2025-03-25 11:40:43 +05:30
|
|
|
use App\Services\AdminService;
|
2025-03-11 17:15:41 +05:30
|
|
|
use Illuminate\Http\Request;
|
2025-03-11 19:44:37 +05:30
|
|
|
use Tymon\JWTAuth\Facades\JWTAuth;
|
2025-03-25 11:40:43 +05:30
|
|
|
use Illuminate\Support\Facades\Http;
|
2025-03-12 19:19:13 +05:30
|
|
|
use Illuminate\Container\Attributes\Auth;
|
|
|
|
|
use Illuminate\Database\QueryException;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2025-03-25 11:40:43 +05:30
|
|
|
use Exception;
|
|
|
|
|
|
|
|
|
|
|
2025-03-11 17:15:41 +05:30
|
|
|
class UserAssetLinkController extends Controller
|
|
|
|
|
{
|
2025-03-25 11:40:43 +05:30
|
|
|
private $adminService;
|
|
|
|
|
|
|
|
|
|
public function __construct(AdminService $adminService)
|
|
|
|
|
{
|
|
|
|
|
$this->adminService = $adminService;
|
|
|
|
|
}
|
2025-03-12 19:19:13 +05:30
|
|
|
|
2025-03-11 17:15:41 +05:30
|
|
|
public function index()
|
|
|
|
|
{
|
2025-03-25 11:40:43 +05:30
|
|
|
try {
|
|
|
|
|
$token = readHeaderToken();
|
2025-03-18 15:45:21 +05:30
|
|
|
|
2025-03-26 16:53:02 +05:30
|
|
|
// $token = readHeaderToken();
|
|
|
|
|
|
|
|
|
|
// $userAssetLinks = UserAssetLink::with(['user', 'asset.devices'])
|
|
|
|
|
// ->withCount([
|
|
|
|
|
// 'assets as active_devices_count' => function ($query) {
|
|
|
|
|
// $query->whereHas('devices', function ($q) {
|
|
|
|
|
// $q->where('active', 1);
|
|
|
|
|
// });
|
|
|
|
|
// },
|
|
|
|
|
// 'assets as inactive_devices_count' => function ($query) {
|
|
|
|
|
// $query->whereHas('devices', function ($q) {
|
|
|
|
|
// $q->where('active', 0);
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// ])
|
|
|
|
|
// ->where('id', $token['sub'])
|
|
|
|
|
// ->first();
|
|
|
|
|
|
|
|
|
|
// return response()->json($userAssetLinks);
|
|
|
|
|
|
|
|
|
|
$token = readHeaderToken();
|
2025-03-24 13:31:06 +05:30
|
|
|
|
2025-03-25 11:40:43 +05:30
|
|
|
// Fetch user with assets and device counts
|
|
|
|
|
$user = User::with(['assets.devices'])
|
|
|
|
|
->withCount([
|
|
|
|
|
'assets as active_devices_count' => function ($query) {
|
|
|
|
|
$query->whereHas('devices', function ($q) {
|
|
|
|
|
$q->where('active', 1);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
'assets as inactive_devices_count' => function ($query) {
|
|
|
|
|
$query->whereHas('devices', function ($q) {
|
|
|
|
|
$q->where('active', 0);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
])
|
|
|
|
|
->where('id', $token['sub'])
|
|
|
|
|
->first();
|
2025-03-18 15:45:21 +05:30
|
|
|
|
2025-03-25 11:40:43 +05:30
|
|
|
if (!$user) {
|
|
|
|
|
return response()->json(['error' => 'User not found'], 404);
|
|
|
|
|
}
|
2025-03-18 15:45:21 +05:30
|
|
|
|
2025-03-25 11:40:43 +05:30
|
|
|
$bearerToken = $this->adminService->getToken();
|
|
|
|
|
$apiBaseUrl = env('THINGSBOARD_URL', 'http://65.0.131.117:8080');
|
2025-03-18 15:45:21 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-03-26 16:53:02 +05:30
|
|
|
foreach ($user->assets->flatMap->devices as $device) {
|
|
|
|
|
$device->health_status = null;
|
|
|
|
|
$device->online = null;
|
2025-03-25 11:40:43 +05:30
|
|
|
|
2025-03-26 16:53:02 +05:30
|
|
|
// Fetch device details from API using customer_id
|
|
|
|
|
$deviceResponse = Http::withHeaders([
|
|
|
|
|
'accept' => 'application/json',
|
|
|
|
|
'Authorization' => 'Bearer ' . $bearerToken,
|
|
|
|
|
])->get("$apiBaseUrl/api/customer/{$device->customer_id}/deviceInfos", [
|
|
|
|
|
'pageSize' => 100,
|
|
|
|
|
'page' => 0
|
|
|
|
|
]);
|
2025-03-25 11:40:43 +05:30
|
|
|
|
2025-03-26 16:53:02 +05:30
|
|
|
if (!$deviceResponse->successful()) {
|
|
|
|
|
Log::error("Failed to fetch device info for Customer ID: {$device->customer_id}, Error: " . $deviceResponse->body());
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2025-03-25 11:40:43 +05:30
|
|
|
|
2025-03-26 16:53:02 +05:30
|
|
|
$deviceData = collect($deviceResponse->json()['data'] ?? []);
|
|
|
|
|
$matchingDevice = $deviceData->firstWhere('id.id', $device->id);
|
|
|
|
|
|
|
|
|
|
// Explicitly set online status for all devices
|
|
|
|
|
$device->online = $matchingDevice['active'] ?? null;
|
|
|
|
|
|
|
|
|
|
// Fetch telemetry data for the device
|
|
|
|
|
$telemetryResponse = Http::withHeaders([
|
|
|
|
|
'accept' => 'application/json',
|
|
|
|
|
'Authorization' => 'Bearer ' . $bearerToken,
|
|
|
|
|
])->get("$apiBaseUrl/api/plugins/telemetry/DEVICE/{$device->id}/values/timeseries", [
|
|
|
|
|
'useStrictDataTypes' => 'false'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if (!$telemetryResponse->successful()) {
|
|
|
|
|
Log::error("Failed to fetch telemetry for Device ID: {$device->id}, Error: " . $telemetryResponse->body());
|
|
|
|
|
continue;
|
2025-03-25 11:40:43 +05:30
|
|
|
}
|
2025-03-26 16:53:02 +05:30
|
|
|
|
|
|
|
|
$telemetryData = $telemetryResponse->json();
|
|
|
|
|
$device->health_status = !empty($telemetryData['MechanicalHealth_valueInPercent'])
|
|
|
|
|
? (float) $telemetryData['MechanicalHealth_valueInPercent'][0]['value']
|
|
|
|
|
: null;
|
2025-03-25 11:40:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response()->json($user);
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
Log::error('Error fetching telemetry data: ' . $e->getMessage());
|
|
|
|
|
return response()->json(['error' => 'Failed to fetch data'], 500);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-11 19:05:25 +05:30
|
|
|
}
|