Files
backend_vib360_laravel/app/Http/Controllers/APIS/CustomerApi/TelemetryController.php
2025-03-25 11:34:55 +05:30

90 lines
2.5 KiB
PHP

<?php
namespace App\Http\Controllers\APIS\CustomerApi;
use App\Http\Controllers\Controller;
use App\Models\Asset;
use App\Models\Device;
use App\Models\TimeseriesKeyMaster;
use App\Services\CustomerInfoService;
use Illuminate\Container\Attributes\DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB as FacadesDB;
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 profiles
$devices = Device::with('deviceProfile')
->where('asset_id', $assetId)
->get();
if ($devices->isEmpty()) {
return response()->json(['error' => 'No devices found for the asset'], 404);
}
$startTs = now()->subHours(1)->timestamp * 1000;
$endTs = now()->timestamp * 1000;
$response = [];
foreach ($devices as $device) {
$telemetry = [];
// ✅ Fetch key names from timeseries_key_master associated with the device profile
$keyNames = TimeseriesKeyMaster::where('device_profile_xid', $device->device_profile_id)
->pluck('key_name')
->toArray();
// ✅ Fetch telemetry data
$telemetryResponse = $this->customerInfoService->getTelemetryData($device, $keyNames, $startTs, $endTs);
foreach ($keyNames as $keyName) {
if (isset($telemetryResponse[$keyName])) {
foreach ($telemetryResponse[$keyName] as $item) {
$telemetry[] = [
'key_name' => $keyName,
'timestamp' => $item['ts'] ?? null,
'value' => $item['value'] ?? null
];
}
}
}
if (!empty($telemetry)) {
$response[] = [
'device_id' => (string) $device->id,
'device_profile_name' => (string) $device->deviceProfile->name,
'asset_id' => (string) $device->asset_id,
'device_profile_id' => (string) $device->device_profile_id,
'telemetry' => $telemetry,
];
}
}
return response()->json(['telemetry' => $response]);
}
}