Files
backend_vib360_laravel/app/Http/Controllers/APIS/CustomerApi/TelemetryController.php
2025-03-24 13:31:06 +05:30

60 lines
1.7 KiB
PHP

<?php
namespace App\Http\Controllers\APIS\CustomerApi;
use App\Http\Controllers\Controller;
use App\Models\Asset;
use App\Models\Device;
use App\Services\CustomerInfoService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class TelemetryController extends Controller
{
protected $customerInfoService;
public function __construct(CustomerInfoService $customerInfoService)
{
$this->customerInfoService = $customerInfoService;
}
public function telemetryData($assetId)
{
// Fetch devices with asset and telemetry keys + device profile
$devices = Device::with(['asset', 'timeseriesKeys.deviceProfile'])
->where('asset_id', $assetId)
->get();
if ($devices->isEmpty()) {
return response()->json(['error' => 'No devices found for the asset'], 404);
}
$telemetryData = [];
// Set start and end timestamps
$startTs = now()->subHours(1)->timestamp * 1000;
$endTs = now()->timestamp * 1000;
// Fetch telemetry data from ThingsBoard
$thingsBoardData = $this->customerInfoService->getTelemetryData($devices, $startTs, $endTs);
foreach ($devices as $device) {
$deviceToken = $device->token; // Use token
// Find telemetry data by token
$telemetry = collect($thingsBoardData)
->firstWhere('device_token', $deviceToken);
$telemetryData[] = [
'device_name' => $device->name,
'telemetry' => $telemetry['telemetry'] ?? [],
];
}
return response()->json([
'telemetry' => $telemetryData
]);
}
}