291 lines
13 KiB
PHP
291 lines
13 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\APIS\CustomerApi;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Asset;
|
|
use App\Models\Device;
|
|
use App\Models\User;
|
|
use App\Models\UserAssetLink;
|
|
use App\Services\AdminService;
|
|
use Illuminate\Http\Request;
|
|
use Tymon\JWTAuth\Facades\JWTAuth;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Container\Attributes\Auth;
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Exception;
|
|
|
|
|
|
class UserAssetLinkController extends Controller
|
|
{
|
|
private $adminService;
|
|
|
|
public function __construct(AdminService $adminService)
|
|
{
|
|
$this->adminService = $adminService;
|
|
}
|
|
|
|
// public function index()
|
|
// {
|
|
// try {
|
|
// $token = readHeaderToken();
|
|
|
|
// // 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();
|
|
|
|
// if (!$user) {
|
|
// return response()->json(['error' => 'User not found'], 404);
|
|
// }
|
|
|
|
// $bearerToken = $this->adminService->getToken();
|
|
// $apiBaseUrl = env('THINGSBOARD_URL', 'http://65.0.131.117:8080');
|
|
|
|
|
|
// // Log::info('Devices: ' . json_encode($user->assets->flatMap->devices));
|
|
// foreach ($user->assets->flatMap->devices as $device) {
|
|
// $device->health_status = null;
|
|
// $device->online = null;
|
|
|
|
// // 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
|
|
// ]);
|
|
|
|
// if (!$deviceResponse->successful()) {
|
|
// Log::error("Failed to fetch device info for Customer ID: {$device->customer_id}, Error: " . $deviceResponse->body());
|
|
// continue;
|
|
// }
|
|
|
|
// $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;
|
|
// }
|
|
|
|
// $telemetryData = $telemetryResponse->json();
|
|
// // Log::info('Telemetry data: ' . json_encode($telemetryData));
|
|
|
|
// $engineName = $telemetryData['Engine_Name'][0]['value'] ?? null;
|
|
|
|
// // Extract values from telemetry data
|
|
// $mechanicalHealthValue1 = isset($telemetryData['MechanicalHealth_value'][0]['value'])
|
|
// ? (float) $telemetryData['MechanicalHealth_value'][0]['value']
|
|
// : null;
|
|
|
|
// $engineEfficiencyValue1 = isset($telemetryData['EngineEfficiency_value'][0]['value'])
|
|
// ? (float) $telemetryData['EngineEfficiency_value'][0]['value']
|
|
// : null;
|
|
|
|
// $engineEfficiencyValue4 = isset($telemetryData['EngineEfficiency_valueInHealth'][0]['value'])
|
|
// ? (float) $telemetryData['EngineEfficiency_valueInHealth'][0]['value']
|
|
// : null;
|
|
|
|
// $powerLossValue1 = isset($telemetryData['PowerLoss_value'][0]['value'])
|
|
// ? (float) $telemetryData['PowerLoss_value'][0]['value']
|
|
// : null;
|
|
|
|
// // Default health status
|
|
// $healthStatusColor = '#0EC23E'; // Green
|
|
|
|
// if ($engineName === "Torque") {
|
|
// $healthStatusColor = '#0EC23E'; // Green
|
|
// } elseif (
|
|
// ($mechanicalHealthValue1 > 0 && $mechanicalHealthValue1 < 31) ||
|
|
// ($engineEfficiencyValue1 > 0 && $engineEfficiencyValue1 < 31) ||
|
|
// ($engineEfficiencyValue4 > 0 && $engineEfficiencyValue4 < 31) ||
|
|
// ($powerLossValue1 > 0 && $powerLossValue1 < 31)
|
|
// ) {
|
|
// $healthStatusColor = '#EF7F30'; // Red
|
|
// } elseif (
|
|
// ($mechanicalHealthValue1 > 30 && $mechanicalHealthValue1 < 71) ||
|
|
// ($engineEfficiencyValue1 > 30 && $engineEfficiencyValue1 < 71) ||
|
|
// ($engineEfficiencyValue4 > 30 && $engineEfficiencyValue4 < 71) ||
|
|
// ($powerLossValue1 > 30 && $powerLossValue1 < 71)
|
|
// ) {
|
|
// $healthStatusColor = '#FFC164'; // Yellow
|
|
// }
|
|
|
|
// $device->health_status = "<div style='height: 10px; width: 10px; border-radius: 50%; display: inline-block; margin-left: 10px; background-color: $healthStatusColor'></div>";
|
|
// }
|
|
|
|
|
|
// return response()->json($user);
|
|
// } catch (Exception $e) {
|
|
// Log::error('Error fetching telemetry data: ' . $e->getMessage());
|
|
// return response()->json(['error' => 'Failed to fetch data'], 500);
|
|
// }
|
|
// }
|
|
|
|
|
|
|
|
|
|
public function index()
|
|
{
|
|
try {
|
|
$token = readHeaderToken();
|
|
|
|
$user = User::with(['assets.devices'])
|
|
->withCount([
|
|
'assets as active_devices_count' => function ($query) {
|
|
$query->whereHas('devices', fn($q) => $q->where('active', 1));
|
|
},
|
|
'assets as inactive_devices_count' => function ($query) {
|
|
$query->whereHas('devices', fn($q) => $q->where('active', 0));
|
|
},
|
|
])
|
|
->where('id', $token['sub'])
|
|
->first();
|
|
|
|
if (!$user) return response()->json(['error' => 'User not found'], 404);
|
|
|
|
$bearerToken = $this->adminService->getToken();
|
|
$apiBaseUrl = env('THINGSBOARD_URL', 'http://65.0.131.117:8080');
|
|
$thirtyDaysAgo = now()->subDays(30)->timestamp * 1000;
|
|
$currentTime = now()->timestamp * 1000;
|
|
|
|
$deviceParameters = [ /* ...same as above... */];
|
|
|
|
$user->assets = collect($user->assets)->map(function ($asset) use ($bearerToken, $apiBaseUrl, $thirtyDaysAgo, $currentTime, $deviceParameters) {
|
|
$healthyDevices = [];
|
|
|
|
$asset->devices = collect($asset->devices)->map(function ($device) use ($bearerToken, $apiBaseUrl, $thirtyDaysAgo, $currentTime, $deviceParameters, &$healthyDevices) {
|
|
$device->online = null;
|
|
$device->celebration_icon = false;
|
|
$device->celebration_message = null;
|
|
$device->health_status = 'green';
|
|
|
|
$deviceResponse = Http::withHeaders([
|
|
'accept' => 'application/json',
|
|
'Authorization' => 'Bearer ' . $bearerToken,
|
|
])->get("$apiBaseUrl/api/customer/{$device->customer_id}/deviceInfos", [
|
|
'pageSize' => 100,
|
|
'page' => 0,
|
|
]);
|
|
|
|
if ($deviceResponse->failed()) return $device;
|
|
|
|
$deviceData = collect($deviceResponse->json()['data'] ?? []);
|
|
$matchingDevice = $deviceData->firstWhere('id.id', $device->id);
|
|
$device->online = $matchingDevice['active'] ?? false;
|
|
|
|
$telemetryResponse = Http::withHeaders([
|
|
'accept' => 'application/json',
|
|
'Authorization' => 'Bearer ' . $bearerToken,
|
|
])->get("$apiBaseUrl/api/plugins/telemetry/DEVICE/{$device->id}/values/timeseries", [
|
|
'startTs' => $thirtyDaysAgo,
|
|
'endTs' => $currentTime,
|
|
'useStrictDataTypes' => 'false',
|
|
]);
|
|
|
|
if ($telemetryResponse->failed()) return $device;
|
|
|
|
$telemetryData = $telemetryResponse->json();
|
|
$deviceType = trim($device->type) ?? 'Unknown';
|
|
$parameters = $deviceParameters[$deviceType] ?? [];
|
|
|
|
$averages = collect($parameters)->mapWithKeys(function ($param) use ($telemetryData) {
|
|
return [
|
|
$param => collect($telemetryData[$param] ?? [])
|
|
->pluck('value')->map(fn($v) => (float)$v)->avg()
|
|
];
|
|
});
|
|
|
|
// Set alert messages
|
|
$deviceModel = Device::with([
|
|
'timeseriesKeys' => fn($q) => $q->where(function ($q2) {
|
|
$q2->where('display_on_dashboard', true)->orWhere('display_on_popup', true);
|
|
})->with(['timeseriesAlert'])
|
|
])->find($device->id);
|
|
|
|
$device->alert = collect();
|
|
|
|
if ($deviceModel) {
|
|
$device->alert = collect($deviceModel->timeseriesKeys)->flatMap(function ($key) use ($telemetryData) {
|
|
$keyName = $key->key_name;
|
|
$dataPoint = $telemetryData[$keyName][0] ?? null;
|
|
|
|
if (!$dataPoint || !isset($dataPoint['value'])) return [];
|
|
|
|
$value = (float) $dataPoint['value'];
|
|
|
|
return collect($key->timeseriesAlert)->filter(function ($alert) use ($value) {
|
|
return ($alert->min_value !== null && $value < $alert->min_value) ||
|
|
($alert->max_value !== null && $value > $alert->max_value);
|
|
})->pluck('alert_msg');
|
|
})->values();
|
|
}
|
|
|
|
// Set health status
|
|
collect($deviceParameters)->each(function ($params) use (&$device, $telemetryData) {
|
|
collect($params)->each(function ($parameter) use (&$device, $telemetryData) {
|
|
$value = isset($telemetryData[$parameter][0]['value']) ? (float) $telemetryData[$parameter][0]['value'] : null;
|
|
if ($value !== null) {
|
|
if ($value > 0 && $value < 31) {
|
|
$device->health_status = 'red';
|
|
} elseif ($value > 30 && $value < 71 && $device->health_status !== 'red') {
|
|
$device->health_status = 'yellow';
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
if ($averages->filter(fn($avg) => $avg > 70)->isNotEmpty()) {
|
|
$device->celebration_icon = true;
|
|
$device->celebration_message = "Your device '{$device->name}' has maintained good health for the past 30 days.";
|
|
$healthyDevices[] = $device->name;
|
|
}
|
|
|
|
return $device;
|
|
});
|
|
|
|
if (!empty($healthyDevices)) {
|
|
$asset->celebration_icon = true;
|
|
$asset->celebration_message = "Your asset '{$asset->name}' has maintained good health for the past 30 days, along with the following devices: " . implode(", ", $healthyDevices) . ".";
|
|
} else {
|
|
$asset->celebration_icon = false;
|
|
$asset->celebration_message = null;
|
|
}
|
|
|
|
return $asset;
|
|
});
|
|
|
|
return response()->json($user);
|
|
} catch (Exception $e) {
|
|
Log::error('Error fetching telemetry data: ' . $e->getMessage());
|
|
return response()->json(['error' => 'Failed to fetch data'], 500);
|
|
}
|
|
}
|
|
}
|