pass starTs and EndTs
This commit is contained in:
@@ -28,14 +28,20 @@ class TelemetryController extends Controller
|
||||
public function telemetryDataAsset(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
|
||||
'asset_id' => 'required|string',
|
||||
'startTs' => 'nullable|string',
|
||||
'endTs' => 'nullable|string',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return jsonResponseWithErrorMessage($validator->errors()->first(), 400);
|
||||
}
|
||||
|
||||
$assetId = $request->input('asset_id');
|
||||
$startTs = $request->input('startTs') ?: null;
|
||||
$endTs = $request->input('endTs') ?: null;
|
||||
|
||||
// Fetch devices associated with the asset
|
||||
$devices = Device::with('deviceProfile')
|
||||
->where('asset_id', $assetId)
|
||||
->get();
|
||||
@@ -44,29 +50,30 @@ class TelemetryController extends Controller
|
||||
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 along with additional columns from timeseries_key_master
|
||||
|
||||
// Fetch key names and additional columns from TimeseriesKeyMaster
|
||||
$keysData = TimeseriesKeyMaster::where('device_profile_xid', $device->device_profile_id)
|
||||
->where(function ($query) {
|
||||
$query->where('display_on_dashboard', true)
|
||||
->orWhere('display_on_popup', true);
|
||||
})
|
||||
->get(['key_name', 'display_name', 'display_on_dashboard', 'display_on_popup']);
|
||||
|
||||
$keyNames = $keysData->pluck('key_name')->toArray();
|
||||
// dd($keyNames);
|
||||
// Log::info('keyNames count', ['count' => count($keyNames)]);
|
||||
|
||||
// Log key names for debugging
|
||||
// Log::info('Key Names for Device', ['device_id' => $device->id, 'key_names' => $keyNames]);
|
||||
|
||||
// Fetch telemetry data
|
||||
$telemetryResponse = $this->customerInfoService->getTelemetryData($device, $keyNames, $startTs, $endTs);
|
||||
// dd($telemetryResponse);
|
||||
|
||||
foreach ($keysData as $keyData) {
|
||||
$keyName = $keyData->key_name;
|
||||
|
||||
|
||||
if (isset($telemetryResponse[$keyName])) {
|
||||
foreach ($telemetryResponse[$keyName] as $item) {
|
||||
$telemetry[] = [
|
||||
@@ -75,28 +82,24 @@ class TelemetryController extends Controller
|
||||
'display_on_dashboard' => $keyData->display_on_dashboard,
|
||||
'display_on_popup' => $keyData->display_on_popup,
|
||||
'timestamp' => $item['ts'] ?? null,
|
||||
'value' => $item['value'] ?? 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,
|
||||
];
|
||||
}
|
||||
$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]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function telemetryDataDevice(Request $request)
|
||||
{
|
||||
|
||||
@@ -130,7 +133,7 @@ class TelemetryController extends Controller
|
||||
$telemetry = [];
|
||||
|
||||
$keyNames = TimeseriesKeyMaster::where('device_profile_xid', $device->device_profile_id)
|
||||
->pluck('key_name','display_name')
|
||||
->pluck('key_name', 'display_name')
|
||||
->toArray();
|
||||
|
||||
// ✅ Fetch telemetry data
|
||||
@@ -222,68 +225,68 @@ class TelemetryController extends Controller
|
||||
return response()->json(['telemetry' => $response]);
|
||||
}
|
||||
|
||||
// public function telemetryDataDeviceDiagnostic(Request $request, $deviceId)
|
||||
// {
|
||||
// // Fetch devices
|
||||
// $devices = Device::with('deviceProfile')
|
||||
// ->where('id', $deviceId)
|
||||
// ->get();
|
||||
// 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);
|
||||
// }
|
||||
// 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;
|
||||
// // 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);
|
||||
// }
|
||||
// if (!$startTs || !$endTs) {
|
||||
// return response()->json(['error' => 'Start date and end date are required'], 400);
|
||||
// }
|
||||
|
||||
// $response = [];
|
||||
// $response = [];
|
||||
|
||||
// foreach ($devices as $device) {
|
||||
// $telemetry = [];
|
||||
// foreach ($devices as $device) {
|
||||
// $telemetry = [];
|
||||
|
||||
// $keyNames = TimeseriesKeyMaster::where('device_profile_xid', $device->device_profile_id)
|
||||
// ->pluck('key_name', 'display_name')
|
||||
// ->toArray();
|
||||
// $keyNames = TimeseriesKeyMaster::where('device_profile_xid', $device->device_profile_id)
|
||||
// ->pluck('key_name', 'display_name')
|
||||
// ->toArray();
|
||||
|
||||
// $telemetryResponse = $this->customerInfoService->getTelemetryDataDeviceDiagonostic($device, $keyNames, $startTs, $endTs);
|
||||
// $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;
|
||||
// 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
|
||||
// ];
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// // ✅ 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,
|
||||
// ];
|
||||
// }
|
||||
// }
|
||||
// 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]);
|
||||
// }
|
||||
// return response()->json(['telemetry' => $response]);
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
@@ -18,38 +18,38 @@ class CustomerInfoService
|
||||
$this->adminService = $adminService;
|
||||
}
|
||||
|
||||
public function getThingsBoardDevices($customerId)
|
||||
{
|
||||
try {
|
||||
$token = $this->adminService->getToken();
|
||||
public function getThingsBoardDevices($customerId)
|
||||
{
|
||||
try {
|
||||
$token = $this->adminService->getToken();
|
||||
|
||||
if (!$token) {
|
||||
Log::error("Failed to authenticate with ThingsBoard.");
|
||||
if (!$token) {
|
||||
Log::error("Failed to authenticate with ThingsBoard.");
|
||||
return [];
|
||||
}
|
||||
|
||||
// Sending the request to ThingsBoard API
|
||||
$response = Http::withHeaders([
|
||||
'Authorization' => "Bearer $token",
|
||||
'Accept' => 'application/json',
|
||||
])->get("http://65.0.131.117:8080/api/customer/{$customerId}/deviceInfos?pageSize=100&page=0");
|
||||
|
||||
// Check if the response is successful
|
||||
if (!$response->successful()) {
|
||||
Log::error("Failed to fetch ThingsBoard devices: " . $response->body());
|
||||
return [];
|
||||
}
|
||||
|
||||
// Convert the JSON response to an array by calling json()
|
||||
$data = $response->json(); // This will convert the response into an array
|
||||
|
||||
// Return only the 'data' key from the response or an empty array if it's not set
|
||||
return $data['data'] ?? [];
|
||||
} catch (\Exception $e) {
|
||||
Log::error("Error fetching ThingsBoard devices: " . $e->getMessage());
|
||||
return [];
|
||||
}
|
||||
|
||||
// Sending the request to ThingsBoard API
|
||||
$response = Http::withHeaders([
|
||||
'Authorization' => "Bearer $token",
|
||||
'Accept' => 'application/json',
|
||||
])->get("http://65.0.131.117:8080/api/customer/{$customerId}/deviceInfos?pageSize=100&page=0");
|
||||
|
||||
// Check if the response is successful
|
||||
if (!$response->successful()) {
|
||||
Log::error("Failed to fetch ThingsBoard devices: " . $response->body());
|
||||
return [];
|
||||
}
|
||||
|
||||
// Convert the JSON response to an array by calling json()
|
||||
$data = $response->json(); // This will convert the response into an array
|
||||
|
||||
// Return only the 'data' key from the response or an empty array if it's not set
|
||||
return $data['data'] ?? [];
|
||||
} catch (\Exception $e) {
|
||||
Log::error("Error fetching ThingsBoard devices: " . $e->getMessage());
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -70,30 +70,30 @@ public function getThingsBoardDevices($customerId)
|
||||
|
||||
$keys = implode(',', $keyNames);
|
||||
|
||||
$queryParams = [
|
||||
'keys' => $keys,
|
||||
];
|
||||
|
||||
if (!empty($startTs)) {
|
||||
$queryParams['startTs'] = $startTs;
|
||||
}
|
||||
|
||||
if (!empty($endTs)) {
|
||||
$queryParams['interval'] = $endTs;
|
||||
}
|
||||
|
||||
// Make the HTTP request
|
||||
$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
|
||||
]);
|
||||
|
||||
|
||||
'Accept' => 'application/json',
|
||||
])->get("$baseUrl/api/plugins/telemetry/DEVICE/{$deviceId}/values/timeseries", $queryParams);
|
||||
|
||||
// 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()
|
||||
'response' => $response->body(),
|
||||
]);
|
||||
|
||||
return ['error' => "Failed to fetch telemetry. HTTP Code: " . $response->status()];
|
||||
@@ -101,11 +101,11 @@ public function getThingsBoardDevices($customerId)
|
||||
|
||||
// Decode the telemetry response
|
||||
$telemetry = $response->json();
|
||||
Log::info("telemetry", $telemetry);
|
||||
// Log::info("Telemetry Data", $telemetry);
|
||||
|
||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||
Log::error("Failed to decode telemetry response for device: {$device->name}", [
|
||||
'response' => $response->body()
|
||||
'response' => $response->body(),
|
||||
]);
|
||||
|
||||
return ['error' => 'Invalid telemetry response format'];
|
||||
@@ -114,8 +114,6 @@ public function getThingsBoardDevices($customerId)
|
||||
return $telemetry;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function getTelemetryDataDevice($device, $keyNames, $startTs, $endTs)
|
||||
{
|
||||
|
||||
@@ -136,8 +134,8 @@ public function getThingsBoardDevices($customerId)
|
||||
$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", [
|
||||
// ])->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,
|
||||
@@ -193,8 +191,8 @@ public function getThingsBoardDevices($customerId)
|
||||
$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", [
|
||||
// ])->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',
|
||||
@@ -230,7 +228,4 @@ public function getThingsBoardDevices($customerId)
|
||||
|
||||
return $telemetry;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user