TelemetryData
This commit is contained in:
@@ -5,8 +5,11 @@ 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
|
||||
@@ -18,10 +21,14 @@ class TelemetryController extends Controller
|
||||
$this->customerInfoService = $customerInfoService;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public function telemetryData($assetId)
|
||||
{
|
||||
// Fetch devices with asset and telemetry keys + device profile
|
||||
$devices = Device::with(['asset', 'timeseriesKeys.deviceProfile'])
|
||||
// ✅ Fetch devices with profiles
|
||||
$devices = Device::with('deviceProfile')
|
||||
->where('asset_id', $assetId)
|
||||
->get();
|
||||
|
||||
@@ -29,31 +36,54 @@ class TelemetryController extends Controller
|
||||
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);
|
||||
$response = [];
|
||||
|
||||
foreach ($devices as $device) {
|
||||
$deviceToken = $device->token; // Use token
|
||||
$telemetry = [];
|
||||
|
||||
// Find telemetry data by token
|
||||
$telemetry = collect($thingsBoardData)
|
||||
->firstWhere('device_token', $deviceToken);
|
||||
// ✅ 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();
|
||||
|
||||
$telemetryData[] = [
|
||||
'device_name' => $device->name,
|
||||
'telemetry' => $telemetry['telemetry'] ?? [],
|
||||
];
|
||||
// ✅ 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' => $telemetryData
|
||||
]);
|
||||
return response()->json(['telemetry' => $response]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user