diff --git a/app/Http/Controllers/APIS/CustomerApi/TelemetryController.php b/app/Http/Controllers/APIS/CustomerApi/TelemetryController.php index fcaf259..2ce3d11 100644 --- a/app/Http/Controllers/APIS/CustomerApi/TelemetryController.php +++ b/app/Http/Controllers/APIS/CustomerApi/TelemetryController.php @@ -367,6 +367,9 @@ class TelemetryController extends Controller public function telemetryDataDevice(Request $request) { try { + $token = readHeaderToken(); + $customerId = User::where('id', $token['sub'])->value('customer_id'); + Log::info('Telemetry request received', ['request_data' => $request->all()]); $token = readHeaderToken(); @@ -410,6 +413,7 @@ class TelemetryController extends Controller } ]) ->where('id', $deviceId) + ->where('customer_id', $customerId) ->firstOrFail(); $displayKeys = $deviceWithTelemetry->timeseriesKeys->pluck('key_name')->toArray(); diff --git a/app/Http/Controllers/APIS/CustomerApi/UserAssetLinkController.php b/app/Http/Controllers/APIS/CustomerApi/UserAssetLinkController.php index 67eb051..cede88a 100644 --- a/app/Http/Controllers/APIS/CustomerApi/UserAssetLinkController.php +++ b/app/Http/Controllers/APIS/CustomerApi/UserAssetLinkController.php @@ -148,7 +148,7 @@ class UserAssetLinkController extends Controller // } - + public function index() { @@ -209,8 +209,31 @@ class UserAssetLinkController extends Controller $healthyDevices = []; foreach ($asset->devices as $device) { + $device->online = null; $device->celebration_message = null; $device->celebration_icon = false; // Default to false + $device->health_status = 'green'; // Default + + //online status + $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'] ?? false; + $telemetryResponse = Http::withHeaders([ 'accept' => 'application/json', @@ -236,6 +259,20 @@ class UserAssetLinkController extends Controller ->pluck('value')->map(fn($v) => (float)$v)->avg(); } + // Set health status based on telemetry values + foreach ($deviceParameters as $category => $parameters) { + foreach ($parameters as $parameter) { + if (isset($telemetryData[$parameter][0]['value'])) { + $value = (float) $telemetryData[$parameter][0]['value']; + if ($value > 0 && $value < 31) { + $device->health_status = 'red'; + } elseif ($value > 30 && $value < 71) { + $device->health_status = 'yellow'; + } + } + } + } + if (collect($averages)->filter(fn($avg) => $avg > 70)->isNotEmpty()) { $device->celebration_icon = true; // Set to true $device->celebration_message = "Your device '{$device->name}' has maintained good health for the past 30 days.";