adminService = $adminService; } public function getThingsBoardDevices($customerId) { try { $token = $this->adminService->getToken(); if (!$token) { Log::error("Failed to authenticate with ThingsBoard."); return []; } // Sending the request to ThingsBoard API $response = Http::withHeaders([ 'Authorization' => "Bearer $token", 'Accept' => 'application/json', ])->get("http://65.0.131.117:8080/api/customer/{$customerId}/deviceInfos?pageSize=100&page=0"); // Check if the response is successful if (!$response->successful()) { Log::error("Failed to fetch ThingsBoard devices: " . $response->body()); return []; } // Convert the JSON response to an array by calling json() $data = $response->json(); // This will convert the response into an array // Return only the 'data' key from the response or an empty array if it's not set return $data['data'] ?? []; } catch (\Exception $e) { Log::error("Error fetching ThingsBoard devices: " . $e->getMessage()); return []; } } public function getTelemetryData($device, $keyNames, $startTs, $endTs) { $token = $this->adminService->getToken(); if (!$token) { Log::error('Failed to fetch ThingsBoard token'); return ['error' => 'Failed to fetch ThingsBoard token']; } $baseUrl = env('THINGSBOARD_URL'); $deviceId = $device->id; $keys = implode(',', $keyNames); $queryParams = [ 'keys' => $keys, ]; if (!empty($startTs)) { $queryParams['startTs'] = $startTs; } if (!empty($endTs)) { $queryParams['interval'] = $endTs; } // Make the HTTP request $response = Http::withHeaders([ 'Authorization' => "Bearer $token", 'Accept' => 'application/json', ])->get("$baseUrl/api/plugins/telemetry/DEVICE/{$deviceId}/values/timeseries", $queryParams); // Check if the response was successful if (!$response->successful()) { Log::error("Failed to fetch telemetry for device: {$device->name} (ID: {$deviceId})", [ 'http_code' => $response->status(), 'url' => $response->effectiveUri(), 'response' => $response->body(), ]); return ['error' => "Failed to fetch telemetry. HTTP Code: " . $response->status()]; } // Decode the telemetry response $telemetry = $response->json(); // Log::info("Telemetry Data", $telemetry); if (json_last_error() !== JSON_ERROR_NONE) { Log::error("Failed to decode telemetry response for device: {$device->name}", [ 'response' => $response->body(), ]); return ['error' => 'Invalid telemetry response format']; } return $telemetry; } public function getTelemetryDataDevice($device, $keyNames, $startTs, $endTs) { $token = $this->adminService->getToken(); if (!$token) { Log::error('Failed to fetch ThingsBoard token'); return ['error' => 'Failed to fetch ThingsBoard token']; } $baseUrl = env('THINGSBOARD_URL'); $deviceId = $device->id; $keys = implode(',', $keyNames); $response = Http::withHeaders([ 'Authorization' => "Bearer $token", 'Accept' => 'application/json' // ])->get("$baseUrl/api/plugins/telemetry/DEVICE/58bf81a0-0619-11f0-a9dc-45dd276e4cd5/values/timeseries", [ ])->get("$baseUrl/api/plugins/telemetry/DEVICE/{$deviceId}/values/timeseries", [ 'keys' => $keys, 'startTs' => $startTs, 'interval' => $endTs, 'limit' => 100, 'useStrictDataTypes' => false ]); // Check if the response was successful if (!$response->successful()) { Log::error("Failed to fetch telemetry for device: {$device->name} (ID: {$deviceId})", [ 'http_code' => $response->status(), 'url' => $response->effectiveUri(), 'response' => $response->body() ]); return ['error' => "Failed to fetch telemetry. HTTP Code: " . $response->status()]; } // Decode the telemetry response $telemetry = $response->json(); if (json_last_error() !== JSON_ERROR_NONE) { Log::error("Failed to decode telemetry response for device: {$device->name}", [ 'response' => $response->body() ]); return ['error' => 'Invalid telemetry response format']; } return $telemetry; } public function getTelemetryDataDeviceDiagonostic($device, $keyNames, $startTs, $endTs) { $token = $this->adminService->getToken(); if (!$token) { Log::error('Failed to fetch ThingsBoard token'); return ['error' => 'Failed to fetch ThingsBoard token']; } $baseUrl = env('THINGSBOARD_URL'); $deviceId = $device->id; $keys = implode(',', $keyNames); $response = Http::withHeaders([ 'Authorization' => "Bearer $token", 'Accept' => 'application/json' // ])->get("$baseUrl/api/plugins/telemetry/DEVICE/58bf81a0-0619-11f0-a9dc-45dd276e4cd5/values/timeseries", [ ])->get("$baseUrl/api/plugins/telemetry/DEVICE/{$deviceId}/values/timeseries", [ 'keys' => $keys, // 'keys' => 'MechanicalHealth_valueInPercent', 'startTs' => $startTs, 'interval' => $endTs, 'limit' => 100, 'useStrictDataTypes' => false ]); // Check if the response was successful if (!$response->successful()) { Log::error("Failed to fetch telemetry for device: {$device->name} (ID: {$deviceId})", [ 'http_code' => $response->status(), 'url' => $response->effectiveUri(), 'response' => $response->body() ]); return ['error' => "Failed to fetch telemetry. HTTP Code: " . $response->status()]; } // Decode the telemetry response $telemetry = $response->json(); if (json_last_error() !== JSON_ERROR_NONE) { Log::error("Failed to decode telemetry response for device: {$device->name}", [ 'response' => $response->body() ]); return ['error' => 'Invalid telemetry response format']; } return $telemetry; } }