sneha #54
@@ -55,7 +55,6 @@ class TelemetryController extends Controller
|
||||
foreach ($devices as $device) {
|
||||
$telemetry = [];
|
||||
|
||||
// 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)
|
||||
@@ -101,10 +100,109 @@ class TelemetryController extends Controller
|
||||
}
|
||||
|
||||
|
||||
// public function telemetryDataDevice(Request $request)
|
||||
// {
|
||||
// try {
|
||||
|
||||
// $token = readHeaderToken();
|
||||
// if (!$token) {
|
||||
// return response()->json([
|
||||
// 'success' => false,
|
||||
// 'error' => 'Authorization token required'
|
||||
// ], 401);
|
||||
// }
|
||||
|
||||
// $validator = Validator::make($request->all(), [
|
||||
// 'device_id' => 'required|string',
|
||||
// 'startTs' => 'nullable|string',
|
||||
// 'endTs' => 'nullable|string',
|
||||
// ]);
|
||||
|
||||
// if ($validator->fails()) {
|
||||
// return response()->json([
|
||||
// 'success' => false,
|
||||
// 'error' => $validator->errors()->first()
|
||||
// ], 400);
|
||||
// }
|
||||
|
||||
// $deviceId = $request->input('device_id');
|
||||
// $startTs = $request->input('startTs') ?: null;
|
||||
// $endTs = $request->input('endTs') ?: null;
|
||||
|
||||
// try {
|
||||
// $deviceWithTelemetry = Device::with([
|
||||
// 'deviceProfile',
|
||||
// 'timeseriesKeys' => function ($query) {
|
||||
// $query->select('key_name', 'display_name', 'device_profile_xid', 'display_on_dashboard','display_on_dashboard');
|
||||
// }
|
||||
// ])
|
||||
// ->where('id', $deviceId)
|
||||
// ->firstOrFail();
|
||||
|
||||
// $telemetryResponse = $this->customerInfoService->getTelemetryDataDevice(
|
||||
// $deviceWithTelemetry,
|
||||
// $deviceWithTelemetry->timeseriesKeys->pluck('key_name')->toArray(),
|
||||
// $startTs,
|
||||
// $endTs,
|
||||
// $token
|
||||
// );
|
||||
|
||||
// if (!is_array($telemetryResponse)) {
|
||||
// throw new \Exception("Invalid telemetry data format received from service");
|
||||
// }
|
||||
|
||||
// $telemetry = collect($telemetryResponse)
|
||||
// ->flatMap(function ($items, $keyName) use ($deviceWithTelemetry) {
|
||||
// $displayName = $deviceWithTelemetry->timeseriesKeys
|
||||
// ->firstWhere('key_name', $keyName)?->display_name ?? $keyName;
|
||||
|
||||
// return collect($items)->map(function ($item) use ($keyName, $displayName) {
|
||||
// return [
|
||||
// 'key_name' => $keyName,
|
||||
// 'timestamp' => $item['ts'] ?? null,
|
||||
// 'value' => $item['value'] ?? null,
|
||||
// 'display_name' => $displayName,
|
||||
// 'display_on_dashboard'
|
||||
// ];
|
||||
// });
|
||||
// })
|
||||
// ->values()
|
||||
// ->all();
|
||||
|
||||
// return response()->json([
|
||||
// 'success' => true,
|
||||
// 'telemetry' => [
|
||||
// 'device_id' => (string) $deviceWithTelemetry->id,
|
||||
// 'device_name' => $deviceWithTelemetry->name,
|
||||
// 'device_profile_name' => $deviceWithTelemetry->deviceProfile->name,
|
||||
// 'device_profile_id' => (string) $deviceWithTelemetry->device_profile_id,
|
||||
// 'telemetry_data' => $telemetry,
|
||||
// ]
|
||||
// ], 200);
|
||||
// } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
|
||||
// return response()->json([
|
||||
// 'success' => false,
|
||||
// 'error' => 'Device not found'
|
||||
// ], 404);
|
||||
// } catch (\Exception $e) {
|
||||
// return response()->json([
|
||||
// 'success' => false,
|
||||
// 'error' => 'Failed to fetch telemetry data',
|
||||
// 'details' => config('app.debug') ? $e->getMessage() : null
|
||||
// ], 503);
|
||||
// }
|
||||
// } catch (\Exception $e) {
|
||||
// return response()->json([
|
||||
// 'success' => false,
|
||||
// 'error' => 'Internal server error',
|
||||
// 'details' => config('app.debug') ? $e->getMessage() : null
|
||||
// ], 500);
|
||||
// }
|
||||
// }
|
||||
|
||||
public function telemetryDataDevice(Request $request)
|
||||
{
|
||||
try {
|
||||
// Read and validate token first
|
||||
$token = readHeaderToken();
|
||||
if (!$token) {
|
||||
return response()->json([
|
||||
@@ -134,36 +232,57 @@ class TelemetryController extends Controller
|
||||
$deviceWithTelemetry = Device::with([
|
||||
'deviceProfile',
|
||||
'timeseriesKeys' => function ($query) {
|
||||
$query->select('key_name', 'display_name', 'device_profile_xid');
|
||||
$query->where(function ($q) {
|
||||
$q->where('display_on_dashboard', true)
|
||||
->orWhere('display_on_popup', true);
|
||||
})
|
||||
->select('key_name', 'display_name', 'device_profile_xid', 'display_on_dashboard', 'display_on_popup');
|
||||
}
|
||||
])
|
||||
->where('id', $deviceId)
|
||||
->firstOrFail();
|
||||
|
||||
// Pass the token to the service method
|
||||
$displayKeys = $deviceWithTelemetry->timeseriesKeys->pluck('key_name')->toArray();
|
||||
|
||||
if (empty($displayKeys)) {
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'telemetry' => [
|
||||
'device_id' => (string) $deviceWithTelemetry->id,
|
||||
'device_name' => $deviceWithTelemetry->name,
|
||||
'device_profile_name' => $deviceWithTelemetry->deviceProfile->name,
|
||||
'device_profile_id' => (string) $deviceWithTelemetry->device_profile_id,
|
||||
'telemetry_data' => [],
|
||||
]
|
||||
], 200);
|
||||
}
|
||||
|
||||
$telemetryResponse = $this->customerInfoService->getTelemetryDataDevice(
|
||||
$deviceWithTelemetry,
|
||||
$deviceWithTelemetry->timeseriesKeys->pluck('key_name')->toArray(),
|
||||
$displayKeys,
|
||||
$startTs,
|
||||
$endTs,
|
||||
$token // Added token parameter
|
||||
$token
|
||||
);
|
||||
|
||||
if (!is_array($telemetryResponse)) {
|
||||
throw new \Exception("Invalid telemetry data format received from service");
|
||||
}
|
||||
|
||||
$telemetry = collect($telemetryResponse)
|
||||
->flatMap(function ($items, $keyName) use ($deviceWithTelemetry) {
|
||||
$displayName = $deviceWithTelemetry->timeseriesKeys
|
||||
->firstWhere('key_name', $keyName)?->display_name ?? $keyName;
|
||||
$filteredResponse = array_intersect_key($telemetryResponse, array_flip($displayKeys));
|
||||
|
||||
return collect($items)->map(function ($item) use ($keyName, $displayName) {
|
||||
$telemetry = collect($filteredResponse)
|
||||
->flatMap(function ($items, $keyName) use ($deviceWithTelemetry) {
|
||||
$keyData = $deviceWithTelemetry->timeseriesKeys->firstWhere('key_name', $keyName);
|
||||
|
||||
return collect($items)->map(function ($item) use ($keyName, $keyData) {
|
||||
return [
|
||||
'key_name' => $keyName,
|
||||
'timestamp' => $item['ts'] ?? null,
|
||||
'value' => $item['value'] ?? null,
|
||||
'display_name' => $displayName,
|
||||
'display_name' => $keyData->display_name ?? $keyName,
|
||||
'display_on_dashboard' => $keyData->display_on_dashboard ?? false,
|
||||
'display_on_popup' => $keyData->display_on_popup ?? false
|
||||
];
|
||||
});
|
||||
})
|
||||
@@ -200,63 +319,63 @@ class TelemetryController extends Controller
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
public function telemetryDataDeviceDiagnostic(Request $request, $deviceId)
|
||||
{
|
||||
$devices = Device::with('deviceProfile')
|
||||
->where('id', $deviceId)
|
||||
->get();
|
||||
// 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);
|
||||
}
|
||||
// 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;
|
||||
// $startTs = $request->has('start_date') ? strtotime($request->start_date) * 1000 : null;
|
||||
// $endTs = $request->has('end_date') ? strtotime($request->end_date) * 1000 : null;
|
||||
|
||||
$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 $keyName) {
|
||||
if (isset($telemetryResponse[$keyName])) {
|
||||
foreach ($telemetryResponse[$keyName] as $item) {
|
||||
$timestamp = $item['ts'] ?? null;
|
||||
// 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,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// // ✅ 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,
|
||||
];
|
||||
}
|
||||
}
|
||||
// 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]);
|
||||
// }
|
||||
|
||||
// public function telemetryDataDeviceDiagnostic(Request $request, $deviceId)
|
||||
// {
|
||||
@@ -322,4 +441,4 @@ class TelemetryController extends Controller
|
||||
// return response()->json(['telemetry' => $response]);
|
||||
// }
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,125 +25,432 @@ class UserAssetLinkController extends Controller
|
||||
$this->adminService = $adminService;
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
try {
|
||||
$token = readHeaderToken();
|
||||
// public function index()
|
||||
// {
|
||||
// try {
|
||||
// $token = readHeaderToken();
|
||||
|
||||
// Fetch user with assets and device counts
|
||||
$user = User::with(['assets.devices'])
|
||||
->withCount([
|
||||
'assets as active_devices_count' => function ($query) {
|
||||
$query->whereHas('devices', function ($q) {
|
||||
$q->where('active', 1);
|
||||
});
|
||||
},
|
||||
'assets as inactive_devices_count' => function ($query) {
|
||||
$query->whereHas('devices', function ($q) {
|
||||
$q->where('active', 0);
|
||||
});
|
||||
}
|
||||
])
|
||||
->where('id', $token['sub'])
|
||||
->first();
|
||||
// // Fetch user with assets and device counts
|
||||
// $user = User::with(['assets.devices'])
|
||||
// ->withCount([
|
||||
// 'assets as active_devices_count' => function ($query) {
|
||||
// $query->whereHas('devices', function ($q) {
|
||||
// $q->where('active', 1);
|
||||
// });
|
||||
// },
|
||||
// 'assets as inactive_devices_count' => function ($query) {
|
||||
// $query->whereHas('devices', function ($q) {
|
||||
// $q->where('active', 0);
|
||||
// });
|
||||
// }
|
||||
// ])
|
||||
// ->where('id', $token['sub'])
|
||||
// ->first();
|
||||
|
||||
if (!$user) {
|
||||
return response()->json(['error' => 'User not found'], 404);
|
||||
}
|
||||
// if (!$user) {
|
||||
// return response()->json(['error' => 'User not found'], 404);
|
||||
// }
|
||||
|
||||
$bearerToken = $this->adminService->getToken();
|
||||
$apiBaseUrl = env('THINGSBOARD_URL', 'http://65.0.131.117:8080');
|
||||
// $bearerToken = $this->adminService->getToken();
|
||||
// $apiBaseUrl = env('THINGSBOARD_URL', 'http://65.0.131.117:8080');
|
||||
|
||||
|
||||
// Log::info('Devices: ' . json_encode($user->assets->flatMap->devices));
|
||||
foreach ($user->assets->flatMap->devices as $device) {
|
||||
$device->health_status = null;
|
||||
$device->online = null;
|
||||
// // Log::info('Devices: ' . json_encode($user->assets->flatMap->devices));
|
||||
// foreach ($user->assets->flatMap->devices as $device) {
|
||||
// $device->health_status = null;
|
||||
// $device->online = null;
|
||||
|
||||
// Fetch device details from API using customer_id
|
||||
$deviceResponse = Http::withHeaders([
|
||||
'accept' => 'application/json',
|
||||
'Authorization' => 'Bearer ' . $bearerToken,
|
||||
])->get("$apiBaseUrl/api/customer/{$device->customer_id}/deviceInfos", [
|
||||
'pageSize' => 100,
|
||||
'page' => 0
|
||||
]);
|
||||
// // Fetch device details from API using customer_id
|
||||
// $deviceResponse = Http::withHeaders([
|
||||
// 'accept' => 'application/json',
|
||||
// 'Authorization' => 'Bearer ' . $bearerToken,
|
||||
// ])->get("$apiBaseUrl/api/customer/{$device->customer_id}/deviceInfos", [
|
||||
// 'pageSize' => 100,
|
||||
// 'page' => 0
|
||||
// ]);
|
||||
|
||||
if (!$deviceResponse->successful()) {
|
||||
Log::error("Failed to fetch device info for Customer ID: {$device->customer_id}, Error: " . $deviceResponse->body());
|
||||
continue;
|
||||
// if (!$deviceResponse->successful()) {
|
||||
// Log::error("Failed to fetch device info for Customer ID: {$device->customer_id}, Error: " . $deviceResponse->body());
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// $deviceData = collect($deviceResponse->json()['data'] ?? []);
|
||||
// $matchingDevice = $deviceData->firstWhere('id.id', $device->id);
|
||||
|
||||
// // Explicitly set online status for all devices
|
||||
// $device->online = $matchingDevice['active'] ?? null;
|
||||
|
||||
// // Fetch telemetry data for the device
|
||||
// $telemetryResponse = Http::withHeaders([
|
||||
// 'accept' => 'application/json',
|
||||
// 'Authorization' => 'Bearer ' . $bearerToken,
|
||||
// ])->get("$apiBaseUrl/api/plugins/telemetry/DEVICE/{$device->id}/values/timeseries", [
|
||||
// 'useStrictDataTypes' => 'false'
|
||||
// ]);
|
||||
|
||||
// if (!$telemetryResponse->successful()) {
|
||||
// Log::error("Failed to fetch telemetry for Device ID: {$device->id}, Error: " . $telemetryResponse->body());
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// $telemetryData = $telemetryResponse->json();
|
||||
// // Log::info('Telemetry data: ' . json_encode($telemetryData));
|
||||
|
||||
// $engineName = $telemetryData['Engine_Name'][0]['value'] ?? null;
|
||||
|
||||
// // Extract values from telemetry data
|
||||
// $mechanicalHealthValue1 = isset($telemetryData['MechanicalHealth_value'][0]['value'])
|
||||
// ? (float) $telemetryData['MechanicalHealth_value'][0]['value']
|
||||
// : null;
|
||||
|
||||
// $engineEfficiencyValue1 = isset($telemetryData['EngineEfficiency_value'][0]['value'])
|
||||
// ? (float) $telemetryData['EngineEfficiency_value'][0]['value']
|
||||
// : null;
|
||||
|
||||
// $engineEfficiencyValue4 = isset($telemetryData['EngineEfficiency_valueInHealth'][0]['value'])
|
||||
// ? (float) $telemetryData['EngineEfficiency_valueInHealth'][0]['value']
|
||||
// : null;
|
||||
|
||||
// $powerLossValue1 = isset($telemetryData['PowerLoss_value'][0]['value'])
|
||||
// ? (float) $telemetryData['PowerLoss_value'][0]['value']
|
||||
// : null;
|
||||
|
||||
// // Default health status
|
||||
// $healthStatusColor = '#0EC23E'; // Green
|
||||
|
||||
// if ($engineName === "Torque") {
|
||||
// $healthStatusColor = '#0EC23E'; // Green
|
||||
// } elseif (
|
||||
// ($mechanicalHealthValue1 > 0 && $mechanicalHealthValue1 < 31) ||
|
||||
// ($engineEfficiencyValue1 > 0 && $engineEfficiencyValue1 < 31) ||
|
||||
// ($engineEfficiencyValue4 > 0 && $engineEfficiencyValue4 < 31) ||
|
||||
// ($powerLossValue1 > 0 && $powerLossValue1 < 31)
|
||||
// ) {
|
||||
// $healthStatusColor = '#EF7F30'; // Red
|
||||
// } elseif (
|
||||
// ($mechanicalHealthValue1 > 30 && $mechanicalHealthValue1 < 71) ||
|
||||
// ($engineEfficiencyValue1 > 30 && $engineEfficiencyValue1 < 71) ||
|
||||
// ($engineEfficiencyValue4 > 30 && $engineEfficiencyValue4 < 71) ||
|
||||
// ($powerLossValue1 > 30 && $powerLossValue1 < 71)
|
||||
// ) {
|
||||
// $healthStatusColor = '#FFC164'; // Yellow
|
||||
// }
|
||||
|
||||
// $device->health_status = "<div style='height: 10px; width: 10px; border-radius: 50%; display: inline-block; margin-left: 10px; background-color: $healthStatusColor'></div>";
|
||||
// }
|
||||
|
||||
|
||||
// return response()->json($user);
|
||||
// } catch (Exception $e) {
|
||||
// Log::error('Error fetching telemetry data: ' . $e->getMessage());
|
||||
// return response()->json(['error' => 'Failed to fetch data'], 500);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// public function index()
|
||||
// {
|
||||
// try {
|
||||
// $token = readHeaderToken();
|
||||
|
||||
// // Fetch user with assets and device counts
|
||||
// $user = User::with(['assets.devices'])
|
||||
// ->withCount([
|
||||
// 'assets as active_devices_count' => function ($query) {
|
||||
// $query->whereHas('devices', function ($q) {
|
||||
// $q->where('active', 1);
|
||||
// });
|
||||
// },
|
||||
// 'assets as inactive_devices_count' => function ($query) {
|
||||
// $query->whereHas('devices', function ($q) {
|
||||
// $q->where('active', 0);
|
||||
// });
|
||||
// }
|
||||
// ])
|
||||
// ->where('id', $token['sub'])
|
||||
// ->first();
|
||||
|
||||
// if (!$user) {
|
||||
// return response()->json(['error' => 'User not found'], 404);
|
||||
// }
|
||||
|
||||
// $bearerToken = $this->adminService->getToken();
|
||||
// $apiBaseUrl = env('THINGSBOARD_URL', 'http://65.0.131.117:8080');
|
||||
|
||||
// foreach ($user->assets->flatMap->devices as $device) {
|
||||
// $device->health_status = null;
|
||||
// $device->online = null;
|
||||
// $device->has_alarm = false; // Initialize alarm status to false
|
||||
|
||||
// // Fetch device details from API using customer_id
|
||||
// $deviceResponse = Http::withHeaders([
|
||||
// 'accept' => 'application/json',
|
||||
// 'Authorization' => 'Bearer ' . $bearerToken,
|
||||
// ])->get("$apiBaseUrl/api/customer/{$device->customer_id}/deviceInfos", [
|
||||
// 'pageSize' => 100,
|
||||
// 'page' => 0
|
||||
// ]);
|
||||
|
||||
// if (!$deviceResponse->successful()) {
|
||||
// Log::error("Failed to fetch device info for Customer ID: {$device->customer_id}, Error: " . $deviceResponse->body());
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// $deviceData = collect($deviceResponse->json()['data'] ?? []);
|
||||
// $matchingDevice = $deviceData->firstWhere('id.id', $device->id);
|
||||
|
||||
// // Set online status
|
||||
// $device->online = $matchingDevice['active'] ?? null;
|
||||
|
||||
// // Fetch alarm data for the device (last 24 hours)
|
||||
// $twentyFourHoursAgo = (time() - 86400) * 1000; // 24 hours ago in milliseconds
|
||||
// $currentTime = time() * 1000; // current time in milliseconds
|
||||
|
||||
// $alarmResponse = Http::withHeaders([
|
||||
// 'accept' => 'application/json',
|
||||
// 'Authorization' => 'Bearer ' . $bearerToken,
|
||||
// ])->get("$apiBaseUrl/api/alarm/DEVICE/{$device->id}", [
|
||||
// 'pageSize' => 100,
|
||||
// 'page' => 0,
|
||||
// 'startTime' => $twentyFourHoursAgo,
|
||||
// 'endTime' => $currentTime,
|
||||
// 'fetchOriginator' => 'false',
|
||||
// 'searchStatus' => 'ACTIVE,ACKNOWLEDGED,CLEARED'
|
||||
// ]);
|
||||
|
||||
// if ($alarmResponse->successful()) {
|
||||
// $alarms = $alarmResponse->json()['data'] ?? [];
|
||||
// $device->has_alarm = !empty($alarms); // Set to true if any alarms exist
|
||||
// } else {
|
||||
// Log::error("Failed to fetch alarms for Device ID: {$device->id}, Error: " . $alarmResponse->body());
|
||||
// }
|
||||
|
||||
// // Fetch telemetry data for the device
|
||||
// $telemetryResponse = Http::withHeaders([
|
||||
// 'accept' => 'application/json',
|
||||
// 'Authorization' => 'Bearer ' . $bearerToken,
|
||||
// ])->get("$apiBaseUrl/api/plugins/telemetry/DEVICE/{$device->id}/values/timeseries", [
|
||||
// 'useStrictDataTypes' => 'false'
|
||||
// ]);
|
||||
|
||||
// if (!$telemetryResponse->successful()) {
|
||||
// Log::error("Failed to fetch telemetry for Device ID: {$device->id}, Error: " . $telemetryResponse->body());
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// $telemetryData = $telemetryResponse->json();
|
||||
|
||||
// $engineName = $telemetryData['Engine_Name'][0]['value'] ?? null;
|
||||
|
||||
// // Extract values from telemetry data
|
||||
// $mechanicalHealthValue1 = isset($telemetryData['MechanicalHealth_value'][0]['value'])
|
||||
// ? (float) $telemetryData['MechanicalHealth_value'][0]['value']
|
||||
// : null;
|
||||
|
||||
// $engineEfficiencyValue1 = isset($telemetryData['EngineEfficiency_value'][0]['value'])
|
||||
// ? (float) $telemetryData['EngineEfficiency_value'][0]['value']
|
||||
// : null;
|
||||
|
||||
// $engineEfficiencyValue4 = isset($telemetryData['EngineEfficiency_valueInHealth'][0]['value'])
|
||||
// ? (float) $telemetryData['EngineEfficiency_valueInHealth'][0]['value']
|
||||
// : null;
|
||||
|
||||
// $powerLossValue1 = isset($telemetryData['PowerLoss_value'][0]['value'])
|
||||
// ? (float) $telemetryData['PowerLoss_value'][0]['value']
|
||||
// : null;
|
||||
|
||||
// // Default health status
|
||||
// $healthStatusColor = '#0EC23E'; // Green
|
||||
|
||||
// if ($engineName === "Torque") {
|
||||
// $healthStatusColor = '#0EC23E'; // Green
|
||||
// } elseif (
|
||||
// ($mechanicalHealthValue1 > 0 && $mechanicalHealthValue1 < 31) ||
|
||||
// ($engineEfficiencyValue1 > 0 && $engineEfficiencyValue1 < 31) ||
|
||||
// ($engineEfficiencyValue4 > 0 && $engineEfficiencyValue4 < 31) ||
|
||||
// ($powerLossValue1 > 0 && $powerLossValue1 < 31)
|
||||
// ) {
|
||||
// $healthStatusColor = '#EF7F30'; // Red
|
||||
// } elseif (
|
||||
// ($mechanicalHealthValue1 > 30 && $mechanicalHealthValue1 < 71) ||
|
||||
// ($engineEfficiencyValue1 > 30 && $engineEfficiencyValue1 < 71) ||
|
||||
// ($engineEfficiencyValue4 > 30 && $engineEfficiencyValue4 < 71) ||
|
||||
// ($powerLossValue1 > 30 && $powerLossValue1 < 71)
|
||||
// ) {
|
||||
// $healthStatusColor = '#FFC164'; // Yellow
|
||||
// }
|
||||
|
||||
// $device->health_status = "<div style='height: 10px; width: 10px; border-radius: 50%; display: inline-block; margin-left: 10px; background-color: $healthStatusColor'></div>";
|
||||
// }
|
||||
|
||||
// return response()->json($user);
|
||||
// } catch (Exception $e) {
|
||||
// Log::error('Error fetching telemetry data: ' . $e->getMessage());
|
||||
// return response()->json(['error' => 'Failed to fetch data'], 500);
|
||||
// }
|
||||
// }
|
||||
|
||||
public function index()
|
||||
{
|
||||
try {
|
||||
$token = readHeaderToken();
|
||||
|
||||
// Fetch user with assets and device counts
|
||||
$user = User::with(['assets.devices'])
|
||||
->withCount([
|
||||
'assets as active_devices_count' => function ($query) {
|
||||
$query->whereHas('devices', function ($q) {
|
||||
$q->where('active', 1);
|
||||
});
|
||||
},
|
||||
'assets as inactive_devices_count' => function ($query) {
|
||||
$query->whereHas('devices', function ($q) {
|
||||
$q->where('active', 0);
|
||||
});
|
||||
}
|
||||
])
|
||||
->where('id', $token['sub'])
|
||||
->first();
|
||||
|
||||
$deviceData = collect($deviceResponse->json()['data'] ?? []);
|
||||
$matchingDevice = $deviceData->firstWhere('id.id', $device->id);
|
||||
|
||||
// Explicitly set online status for all devices
|
||||
$device->online = $matchingDevice['active'] ?? null;
|
||||
|
||||
// Fetch telemetry data for the device
|
||||
$telemetryResponse = Http::withHeaders([
|
||||
'accept' => 'application/json',
|
||||
'Authorization' => 'Bearer ' . $bearerToken,
|
||||
])->get("$apiBaseUrl/api/plugins/telemetry/DEVICE/{$device->id}/values/timeseries", [
|
||||
'useStrictDataTypes' => 'false'
|
||||
]);
|
||||
|
||||
if (!$telemetryResponse->successful()) {
|
||||
Log::error("Failed to fetch telemetry for Device ID: {$device->id}, Error: " . $telemetryResponse->body());
|
||||
continue;
|
||||
}
|
||||
|
||||
$telemetryData = $telemetryResponse->json();
|
||||
// Log::info('Telemetry data: ' . json_encode($telemetryData));
|
||||
|
||||
$engineName = $telemetryData['Engine_Name'][0]['value'] ?? null;
|
||||
|
||||
// Extract values from telemetry data
|
||||
$mechanicalHealthValue1 = isset($telemetryData['MechanicalHealth_value'][0]['value'])
|
||||
? (float) $telemetryData['MechanicalHealth_value'][0]['value']
|
||||
: null;
|
||||
|
||||
$engineEfficiencyValue1 = isset($telemetryData['EngineEfficiency_value'][0]['value'])
|
||||
? (float) $telemetryData['EngineEfficiency_value'][0]['value']
|
||||
: null;
|
||||
|
||||
$engineEfficiencyValue4 = isset($telemetryData['EngineEfficiency_valueInHealth'][0]['value'])
|
||||
? (float) $telemetryData['EngineEfficiency_valueInHealth'][0]['value']
|
||||
: null;
|
||||
|
||||
$powerLossValue1 = isset($telemetryData['PowerLoss_value'][0]['value'])
|
||||
? (float) $telemetryData['PowerLoss_value'][0]['value']
|
||||
: null;
|
||||
|
||||
// Default health status
|
||||
$healthStatusColor = '#0EC23E'; // Green
|
||||
|
||||
if ($engineName === "Torque") {
|
||||
$healthStatusColor = '#0EC23E'; // Green
|
||||
} elseif (
|
||||
($mechanicalHealthValue1 > 0 && $mechanicalHealthValue1 < 31) ||
|
||||
($engineEfficiencyValue1 > 0 && $engineEfficiencyValue1 < 31) ||
|
||||
($engineEfficiencyValue4 > 0 && $engineEfficiencyValue4 < 31) ||
|
||||
($powerLossValue1 > 0 && $powerLossValue1 < 31)
|
||||
) {
|
||||
$healthStatusColor = '#EF7F30'; // Red
|
||||
} elseif (
|
||||
($mechanicalHealthValue1 > 30 && $mechanicalHealthValue1 < 71) ||
|
||||
($engineEfficiencyValue1 > 30 && $engineEfficiencyValue1 < 71) ||
|
||||
($engineEfficiencyValue4 > 30 && $engineEfficiencyValue4 < 71) ||
|
||||
($powerLossValue1 > 30 && $powerLossValue1 < 71)
|
||||
) {
|
||||
$healthStatusColor = '#FFC164'; // Yellow
|
||||
}
|
||||
|
||||
$device->health_status = "<div style='height: 10px; width: 10px; border-radius: 50%; display: inline-block; margin-left: 10px; background-color: $healthStatusColor'></div>";
|
||||
}
|
||||
|
||||
|
||||
return response()->json($user);
|
||||
} catch (Exception $e) {
|
||||
Log::error('Error fetching telemetry data: ' . $e->getMessage());
|
||||
return response()->json(['error' => 'Failed to fetch data'], 500);
|
||||
if (!$user) {
|
||||
return response()->json(['error' => 'User not found'], 404);
|
||||
}
|
||||
|
||||
$bearerToken = $this->adminService->getToken();
|
||||
$apiBaseUrl = env('THINGSBOARD_URL', 'http://65.0.131.117:8080');
|
||||
|
||||
foreach ($user->assets->flatMap->devices as $device) {
|
||||
$device->health_status = null;
|
||||
$device->online = null;
|
||||
$device->has_alarm = false;
|
||||
|
||||
// 1. First check if device exists in ThingsBoard
|
||||
$deviceCheckResponse = Http::withHeaders([
|
||||
'accept' => 'application/json',
|
||||
'Authorization' => 'Bearer ' . $bearerToken,
|
||||
])->get("$apiBaseUrl/api/device/{$device->id}");
|
||||
|
||||
if (!$deviceCheckResponse->successful()) {
|
||||
Log::warning("Device not found in ThingsBoard: {$device->id}");
|
||||
continue; // Skip this device
|
||||
}
|
||||
|
||||
// Fetch device details from API using customer_id
|
||||
$deviceResponse = Http::withHeaders([
|
||||
'accept' => 'application/json',
|
||||
'Authorization' => 'Bearer ' . $bearerToken,
|
||||
])->get("$apiBaseUrl/api/customer/{$device->customer_id}/deviceInfos", [
|
||||
'pageSize' => 100,
|
||||
'page' => 0
|
||||
]);
|
||||
|
||||
if (!$deviceResponse->successful()) {
|
||||
Log::error("Failed to fetch device info for Customer ID: {$device->customer_id}, Error: " . $deviceResponse->body());
|
||||
continue;
|
||||
}
|
||||
|
||||
$deviceData = collect($deviceResponse->json()['data'] ?? []);
|
||||
$matchingDevice = $deviceData->firstWhere('id.id', $device->id);
|
||||
|
||||
// Set online status
|
||||
$device->online = $matchingDevice['active'] ?? null;
|
||||
|
||||
// Fetch alarm data for the device (last 24 hours)
|
||||
$twentyFourHoursAgo = (time() - 86400) * 1000;
|
||||
$currentTime = time() * 1000;
|
||||
|
||||
// Fixed: Use separate API calls for each alarm status
|
||||
$alarmStatuses = ['ACTIVE', 'ACKNOWLEDGED', 'CLEARED'];
|
||||
$hasAlarm = false;
|
||||
|
||||
foreach ($alarmStatuses as $status) {
|
||||
$alarmResponse = Http::withHeaders([
|
||||
'accept' => 'application/json',
|
||||
'Authorization' => 'Bearer ' . $bearerToken,
|
||||
])->get("$apiBaseUrl/api/alarm/DEVICE/{$device->id}", [
|
||||
'pageSize' => 100,
|
||||
'page' => 0,
|
||||
'startTime' => $twentyFourHoursAgo,
|
||||
'endTime' => $currentTime,
|
||||
'fetchOriginator' => 'false',
|
||||
'searchStatus' => $status
|
||||
]);
|
||||
|
||||
if ($alarmResponse->successful()) {
|
||||
$alarms = $alarmResponse->json()['data'] ?? [];
|
||||
if (!empty($alarms)) {
|
||||
$hasAlarm = true;
|
||||
break; // No need to check other statuses if we found an alarm
|
||||
}
|
||||
} else {
|
||||
Log::error("Failed to fetch $status alarms for Device ID: {$device->id}, Error: " . $alarmResponse->body());
|
||||
}
|
||||
}
|
||||
|
||||
$device->has_alarm = $hasAlarm;
|
||||
|
||||
// Fetch telemetry data for the device
|
||||
$telemetryResponse = Http::withHeaders([
|
||||
'accept' => 'application/json',
|
||||
'Authorization' => 'Bearer ' . $bearerToken,
|
||||
])->get("$apiBaseUrl/api/plugins/telemetry/DEVICE/{$device->id}/values/timeseries", [
|
||||
'useStrictDataTypes' => 'false'
|
||||
]);
|
||||
|
||||
if (!$telemetryResponse->successful()) {
|
||||
Log::error("Failed to fetch telemetry for Device ID: {$device->id}, Error: " . $telemetryResponse->body());
|
||||
continue;
|
||||
}
|
||||
|
||||
$telemetryData = $telemetryResponse->json();
|
||||
|
||||
$engineName = $telemetryData['Engine_Name'][0]['value'] ?? null;
|
||||
|
||||
// Extract values from telemetry data
|
||||
$mechanicalHealthValue1 = isset($telemetryData['MechanicalHealth_value'][0]['value'])
|
||||
? (float) $telemetryData['MechanicalHealth_value'][0]['value']
|
||||
: null;
|
||||
|
||||
$engineEfficiencyValue1 = isset($telemetryData['EngineEfficiency_value'][0]['value'])
|
||||
? (float) $telemetryData['EngineEfficiency_value'][0]['value']
|
||||
: null;
|
||||
|
||||
$engineEfficiencyValue4 = isset($telemetryData['EngineEfficiency_valueInHealth'][0]['value'])
|
||||
? (float) $telemetryData['EngineEfficiency_valueInHealth'][0]['value']
|
||||
: null;
|
||||
|
||||
$powerLossValue1 = isset($telemetryData['PowerLoss_value'][0]['value'])
|
||||
? (float) $telemetryData['PowerLoss_value'][0]['value']
|
||||
: null;
|
||||
|
||||
// Default health status
|
||||
$healthStatusColor = '#0EC23E'; // Green
|
||||
|
||||
if ($engineName === "Torque") {
|
||||
$healthStatusColor = '#0EC23E'; // Green
|
||||
} elseif (
|
||||
($mechanicalHealthValue1 > 0 && $mechanicalHealthValue1 < 31) ||
|
||||
($engineEfficiencyValue1 > 0 && $engineEfficiencyValue1 < 31) ||
|
||||
($engineEfficiencyValue4 > 0 && $engineEfficiencyValue4 < 31) ||
|
||||
($powerLossValue1 > 0 && $powerLossValue1 < 31)
|
||||
) {
|
||||
$healthStatusColor = '#EF7F30'; // Red
|
||||
} elseif (
|
||||
($mechanicalHealthValue1 > 30 && $mechanicalHealthValue1 < 71) ||
|
||||
($engineEfficiencyValue1 > 30 && $engineEfficiencyValue1 < 71) ||
|
||||
($engineEfficiencyValue4 > 30 && $engineEfficiencyValue4 < 71) ||
|
||||
($powerLossValue1 > 30 && $powerLossValue1 < 71)
|
||||
) {
|
||||
$healthStatusColor = '#FFC164'; // Yellow
|
||||
}
|
||||
|
||||
$device->health_status = "<div style='height: 10px; width: 10px; border-radius: 50%; display: inline-block; margin-left: 10px; background-color: $healthStatusColor'></div>";
|
||||
}
|
||||
|
||||
return response()->json($user);
|
||||
} catch (Exception $e) {
|
||||
Log::error('Error fetching telemetry data: ' . $e->getMessage());
|
||||
return response()->json(['error' => 'Failed to fetch data'], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -354,6 +354,8 @@ class CustomerInfoService
|
||||
}
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
|
||||
public function getToken()
|
||||
{
|
||||
@@ -385,4 +387,5 @@ class CustomerInfoService
|
||||
throw new Exception('Unable to authenticate with ThingsBoard: ' . $response->body());
|
||||
}
|
||||
}
|
||||
>>>>>>> 7c6c6a5243ba835684e8006b373cce70f901415f
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user