sayali #36
@@ -13,7 +13,6 @@ use Illuminate\Support\Facades\DB as FacadesDB;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
|
||||
class TelemetryController extends Controller
|
||||
{
|
||||
protected $customerInfoService;
|
||||
@@ -94,4 +93,196 @@ class TelemetryController extends Controller
|
||||
|
||||
return response()->json(['telemetry' => $response]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function telemetryDataDevice(Request $request)
|
||||
{
|
||||
|
||||
$validator = Validator::make($request->all(), [
|
||||
|
||||
'device_id' => 'required|string',
|
||||
]);
|
||||
if ($validator->fails()) {
|
||||
return jsonResponseWithErrorMessage($validator->errors()->first(), 400);
|
||||
}
|
||||
$deviceId = $request->input('device_id');
|
||||
|
||||
|
||||
|
||||
$devices = Device::with('deviceProfile')
|
||||
->where('id', $deviceId)
|
||||
->get();
|
||||
|
||||
|
||||
|
||||
if ($devices->isEmpty()) {
|
||||
return response()->json(['error' => 'No devices found'], 404);
|
||||
}
|
||||
|
||||
$startTs = now()->subHours(1)->timestamp * 1000;
|
||||
$endTs = now()->timestamp * 1000;
|
||||
|
||||
$response = [];
|
||||
|
||||
foreach ($devices as $device) {
|
||||
$telemetry = [];
|
||||
|
||||
$keyNames = TimeseriesKeyMaster::where('device_profile_xid', $device->device_profile_id)
|
||||
->pluck('key_name','display_name')
|
||||
->toArray();
|
||||
|
||||
// ✅ Fetch telemetry data
|
||||
$telemetryResponse = $this->customerInfoService->getTelemetryDataDevice($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,
|
||||
'display_name' => $keyName,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($telemetry)) {
|
||||
$response[] = [
|
||||
'device_id' => (string) $device->id,
|
||||
'device_name' => $device->name,
|
||||
'device_profile_name' => (string) $device->deviceProfile->name,
|
||||
'device_profile_id' => (string) $device->device_profile_id,
|
||||
'telemetry' => $telemetry,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return response()->json(['telemetry' => $response]);
|
||||
}
|
||||
|
||||
public function telemetryDataDeviceDiagnostic(Request $request, $deviceId)
|
||||
{
|
||||
$devices = Device::with('deviceProfile')
|
||||
->where('id', $deviceId)
|
||||
->get();
|
||||
|
||||
if ($devices->isEmpty()) {
|
||||
return response()->json(['error' => 'No devices found'], 404);
|
||||
}
|
||||
|
||||
$startTs = $request->has('start_date') ? strtotime($request->start_date) * 1000 : null;
|
||||
$endTs = $request->has('end_date') ? strtotime($request->end_date) * 1000 : null;
|
||||
|
||||
$response = [];
|
||||
|
||||
foreach ($devices as $device) {
|
||||
$telemetry = [];
|
||||
|
||||
$keyNames = TimeseriesKeyMaster::where('device_profile_xid', $device->device_profile_id)
|
||||
->pluck('key_name', 'display_name')
|
||||
->toArray();
|
||||
|
||||
$telemetryResponse = $this->customerInfoService->getTelemetryDataDeviceDiagonostic($device, $keyNames, $startTs, $endTs);
|
||||
|
||||
foreach ($keyNames as $keyName) {
|
||||
if (isset($telemetryResponse[$keyName])) {
|
||||
foreach ($telemetryResponse[$keyName] as $item) {
|
||||
$timestamp = $item['ts'] ?? null;
|
||||
|
||||
// ✅ Filter telemetry by timestamp range
|
||||
if ($timestamp && $timestamp >= $startTs && $timestamp <= $endTs) {
|
||||
$telemetry[] = [
|
||||
'key_name' => $keyName,
|
||||
'timestamp' => $timestamp,
|
||||
'start_date' => $startTs,
|
||||
'end_date' => $endTs,
|
||||
'value' => $item['value'] ?? null,
|
||||
'display_name' => $keyName,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($telemetry)) {
|
||||
$response[] = [
|
||||
'device_id' => (string) $device->id,
|
||||
'device_name' => $device->name,
|
||||
'device_profile_name' => (string) $device->deviceProfile->name,
|
||||
'device_profile_id' => (string) $device->device_profile_id,
|
||||
'telemetry' => $telemetry,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return response()->json(['telemetry' => $response]);
|
||||
}
|
||||
|
||||
// public function telemetryDataDeviceDiagnostic(Request $request, $deviceId)
|
||||
// {
|
||||
// // Fetch devices
|
||||
// $devices = Device::with('deviceProfile')
|
||||
// ->where('id', $deviceId)
|
||||
// ->get();
|
||||
|
||||
// if ($devices->isEmpty()) {
|
||||
// return response()->json(['error' => 'No devices found'], 404);
|
||||
// }
|
||||
|
||||
// // Get start and end timestamps from request parameters
|
||||
// $startTs = $request->has('start_date') ? strtotime($request->start_date) * 1000 : null;
|
||||
// $endTs = $request->has('end_date') ? strtotime($request->end_date) * 1000 : null;
|
||||
|
||||
// if (!$startTs || !$endTs) {
|
||||
// return response()->json(['error' => 'Start date and end date are required'], 400);
|
||||
// }
|
||||
|
||||
// $response = [];
|
||||
|
||||
// foreach ($devices as $device) {
|
||||
// $telemetry = [];
|
||||
|
||||
// $keyNames = TimeseriesKeyMaster::where('device_profile_xid', $device->device_profile_id)
|
||||
// ->pluck('key_name', 'display_name')
|
||||
// ->toArray();
|
||||
|
||||
// $telemetryResponse = $this->customerInfoService->getTelemetryDataDeviceDiagonostic($device, $keyNames, $startTs, $endTs);
|
||||
|
||||
// foreach ($keyNames as $displayName => $keyName) {
|
||||
// if (isset($telemetryResponse[$keyName])) {
|
||||
// foreach ($telemetryResponse[$keyName] as $item) {
|
||||
// $itemTs = $item['timestamp'] ?? null;
|
||||
|
||||
// // ✅ Filter only telemetry within the date range
|
||||
// if ($itemTs >= $startTs && $itemTs <= $endTs) {
|
||||
// $telemetry[] = [
|
||||
// 'key_name' => $keyName,
|
||||
// 'value' => $item['value'] ?? null,
|
||||
// 'display_name' => $displayName,
|
||||
// 'timestamp' => $itemTs,
|
||||
// 'start_date' => $startTs,
|
||||
// 'end_date' => $endTs
|
||||
// ];
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// if (!empty($telemetry)) {
|
||||
// $response[] = [
|
||||
// 'device_id' => (string) $device->id,
|
||||
// 'device_name' => $device->name,
|
||||
// 'device_profile_name' => (string) $device->deviceProfile->name,
|
||||
// 'device_profile_id' => (string) $device->device_profile_id,
|
||||
// 'telemetry' => $telemetry,
|
||||
// ];
|
||||
// }
|
||||
// }
|
||||
|
||||
// return response()->json(['telemetry' => $response]);
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
@@ -109,6 +109,7 @@ class CustomerInfoService
|
||||
|
||||
public function getTelemetryDataDevice($device, $keyNames, $startTs, $endTs)
|
||||
{
|
||||
|
||||
$token = $this->adminService->getToken();
|
||||
|
||||
if (!$token) {
|
||||
|
||||
@@ -17,8 +17,8 @@ Route::get('/customerapi', function () {
|
||||
Route::post('user-login', [AuthController::class, 'login']);
|
||||
Route::get('/customer-device-info/{customerId}',[CustomerDeviceInfoController::class,'customerDeviceInfo']);
|
||||
Route::post('/telemetry-data-asset',[TelemetryController::class,'telemetryDataAsset']);
|
||||
Route::get('/telemetry-data-device/{deviceId}',[TelemetryController::class,'telemetryDataDevice']);
|
||||
Route::post('/telemetry-data-device-diagnostic/{deviceId}',[TelemetryController::class,'telemePtryDataDeviceDiagnostic']);
|
||||
Route::post('/telemetry-data-device',[TelemetryController::class,'telemetryDataDevice']);
|
||||
Route::post('/telemetry-data-device-diagnostic',[TelemetryController::class,'telemePtryDataDeviceDiagnostic']);
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user