diff --git a/app/Http/Controllers/APIS/CustomerApi/UserAssetLinkController.php b/app/Http/Controllers/APIS/CustomerApi/UserAssetLinkController.php index c79b905..6d6c4a6 100644 --- a/app/Http/Controllers/APIS/CustomerApi/UserAssetLinkController.php +++ b/app/Http/Controllers/APIS/CustomerApi/UserAssetLinkController.php @@ -4,6 +4,7 @@ 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; @@ -281,10 +282,6 @@ class UserAssetLinkController extends Controller $currentMonth = $currentDate->month; $currentYear = $currentDate->year; - // $deviceParameters = [ - // 'Engine Health' => ['MechanicalHealth_valueInPercent', 'EngineEfficiency_valueInPercent', 'PowerLoss_value'], - // // Add more device types and their parameters as needed - // ]; $deviceParameters = [ 'Engine Health' => ['MechanicalHealth_valueInPercent', 'EngineEfficiency_valueInPercent', 'PowerLoss_value'], 'Bearing' => ['MBearing_valueInPercent', 'MElectromag_valueInPercent', 'MStressStability_valueInPercent'], @@ -303,7 +300,6 @@ class UserAssetLinkController extends Controller 'Friction' => ['8KMixed_valueInPercent', 'BearingGlobal_valueInPercent', 'GlobalMixed_valueInPercent', 'GlobalKurto_valueInPercent', '2KMixed_valueInPercent', '4KMixed_valueInPercent'], ]; - foreach ($user->assets as $asset) { $asset->celebration_message = null; $asset->celebration_icon = false; @@ -314,10 +310,13 @@ class UserAssetLinkController extends Controller $device->celebration_message = null; $device->online = null; $device->celebration_message = null; - $device->celebration_icon = false; // Default to false - $device->health_status = 'green'; // Default - //online status + $device->celebration_icon = false; // Default to false + $device->health_status = 'green'; + + + + // Fetch online status $deviceResponse = Http::withHeaders([ 'accept' => 'application/json', 'Authorization' => 'Bearer ' . $bearerToken, @@ -333,20 +332,14 @@ class UserAssetLinkController extends Controller $deviceData = collect($deviceResponse->json()['data'] ?? []); $matchingDevice = $deviceData->firstWhere('id.id', $device->id); - - // Explicitly set online status for all devices $device->online = $matchingDevice['active'] ?? false; - $deviceType = trim($device->type) ?? 'Unknown'; $parameters = $deviceParameters[$deviceType] ?? []; - $allParametersHealthy = true; foreach ($parameters as $param) { $allDaysHealthy = true; - - // Loop through each day of the current month $daysInMonth = now()->daysInMonth; for ($day = 1; $day <= $daysInMonth; $day++) { @@ -383,6 +376,57 @@ class UserAssetLinkController extends Controller } } + // === Fetch telemetry once for alerts === + $telemetryResponse = Http::withHeaders([ + 'accept' => 'application/json', + 'Authorization' => 'Bearer ' . $bearerToken, + ])->get("$apiBaseUrl/api/plugins/telemetry/DEVICE/{$device->id}/values/timeseries"); + + $telemetryData = $telemetryResponse->successful() ? $telemetryResponse->json() : []; + + // === Alert Logic === + $alerts = []; + $deviceModel = Device::with([ + 'timeseriesKeys' => function ($query) { + $query->where(function ($q) { + $q->where('display_on_dashboard', true) + ->orWhere('display_on_popup', true); + }) + ->select('id', 'device_profile_xid', 'key_name', 'display_name') + ->with(['timeseriesAlert' => function ($query) { + $query->select('id', 'timeseries_key_master_xid', 'min_value', 'max_value', 'alert_msg'); + }]); + } + ])->find($device->id); + + if ($deviceModel) { + foreach ($deviceModel->timeseriesKeys as $key) { + $keyName = $key->key_name; + $dataPoint = $telemetryData[$keyName][0] ?? null; + + if (!$dataPoint || !isset($dataPoint['value'])) { + continue; + } + + $currentValue = (float) $dataPoint['value']; + + foreach ($key->timeseriesAlert as $alert) { + $minValue = $alert->min_value !== null ? (float) $alert->min_value : null; + $maxValue = $alert->max_value !== null ? (float) $alert->max_value : null; + + $isBelowMin = $minValue !== null && $currentValue < $minValue; + $isAboveMax = $maxValue !== null && $currentValue > $maxValue; + + if ($isBelowMin || $isAboveMax) { + $alerts[] = $alert->alert_msg; + } + } + } + } + + $device->alert = $alerts; + + // === Celebration logic === if ($allParametersHealthy && !empty($parameters)) { $device->celebration_icon = true; $device->celebration_message = "Your device '{$device->name}' maintained > 70 average for every parameter on all days of this month."; @@ -402,4 +446,5 @@ class UserAssetLinkController extends Controller return response()->json(['error' => 'Failed to fetch data'], 500); } } + }