Files
backend_vib360_laravel/app/Services/CustomerInfoService.php
2025-03-26 12:18:45 +05:30

228 lines
6.8 KiB
PHP

<?php
namespace App\Services;
use App\Models\TimeseriesKeyMaster;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use App\Services\AdminService;
use Exception;
use Illuminate\Support\Facades\Request;
class CustomerInfoService
{
protected $adminService;
public function __construct(AdminService $adminService)
{
$this->adminService = $adminService;
}
public function getThingsBoardDevices($customerId)
{
try {
$token = $this->adminService->getToken();
if (!$token) {
Log::error("Failed to authenticate with ThingsBoard.");
return [];
}
$response = Http::withHeaders([
'Authorization' => "Bearer $token",
'Accept' => 'application/json',
])->get("http://65.0.131.117:8080/api/customer/{$customerId}/deviceInfos?pageSize=100&page=0");
if (!$response->successful()) {
Log::error("Failed to fetch ThingsBoard devices: " . $response->body());
return [];
}
return $response->json()['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);
$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;
}
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;
}
}