sneha #42

Merged
Sneha.Yadav merged 2 commits from sneha into main 2025-03-27 06:50:42 +00:00
3 changed files with 82 additions and 26 deletions

View File

@@ -409,7 +409,6 @@ class UsersController extends Controller
];
}
// ✅ Fetch ThingsBoard user by email, then by ID
$thingsboardResponse = $this->adminService->getUserByEmailThingsBoard($email);
// ✅ Return both responses

View File

@@ -6,6 +6,7 @@ use App\Services\AlarmService;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
use Exception;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Request;
@@ -63,35 +64,91 @@ class AlarmController extends Controller
}
}
public function filterAlarm(Request $request)
public function getAlarmById($id)
{
try {
$alarmData = $request->only([
// 'statusList',
'severity',
// 'type',
// 'assigneeId',
'pageSize',
'page',
// 'textSearch',
// 'sortProperty',
// 'sortOrder',
'startTs',
'endTs'
]);
$token = app('App\Services\AdminService')->getToken(); // Fetch cached token
$url = env('THINGSBOARD_URL') . "/api/alarm/{$id}";
$apiResponse = $this->alarmService->filterAlarm($alarmData);
$response = Http::withHeaders([
'X-Authorization' => 'Bearer ' . $token,
'accept' => 'application/json',
])->get($url);
return jsonResponseWithSuccessMessage('Alarm data retrieved successfully', ['api_response' => $apiResponse]);
} catch (Exception $e) {
Log::error("Error: " . $e->getMessage());
if ($response->successful()) {
$responseData = $response->json();
$errorResponse = json_decode($e->getMessage(), true);
if (json_last_error() === JSON_ERROR_NONE) {
return jsonResponseWithErrorMessage($errorResponse['message'] ?? 'Something went wrong', 400, $errorResponse);
$data = $responseData['data'] ?? $responseData ?? [];
if (empty($data)) {
return jsonResponseWithErrorMessage('No data found for the provided ID', 404);
}
$filteredData = [
'originator' => $data['originator'] ?? null,
'severity' => $data['severity'] ?? null,
'startTs' => $data['startTs'] ?? null,
'type' => $data['type'] ?? null,
'status' => $data['status'] ?? null,
'assignee' => $data['assigneeId'] ?? null,
];
return jsonResponseWithSuccessMessage('Alarm data fetched successfully', $filteredData);
}
return jsonResponseWithErrorMessage($e->getMessage(), 500);
return jsonResponseWithErrorMessage('Failed to fetch alarm data: ' . $response->body(), $response->status());
} catch (\Exception $e) {
return jsonResponseWithErrorMessage('An error occurred while fetching the alarm: ' . $e->getMessage(), 500);
}
}
public function acknowledgeAlarmById($id)
{
try {
$token = app('App\Services\AdminService')->getToken(); // Fetch cached token
$url = env('THINGSBOARD_URL') . "/api/alarm/{$id}";
$getResponse = Http::withHeaders([
'X-Authorization' => 'Bearer ' . $token,
'accept' => 'application/json',
])->get($url);
if (!$getResponse->successful()) {
return jsonResponseWithErrorMessage('Failed to fetch alarm: ' . $getResponse->body(), $getResponse->status());
}
$alarmData = $getResponse->json();
if ($alarmData['acknowledged'] ?? false) {
return jsonResponseWithSuccessMessage('Alarm is already acknowledged', [
'id' => $alarmData['id']['id'] ?? $id,
'acknowledged' => true,
'ackTs' => $alarmData['ackTs'] ?? null,
'status' => $alarmData['status'] ?? 'UNKNOWN'
]);
}
$ackUrl = env('THINGSBOARD_URL') . "/api/alarm/{$id}/ack";
$ackResponse = Http::withHeaders([
'X-Authorization' => 'Bearer ' . $token,
'accept' => 'application/json',
])->post($ackUrl);
if ($ackResponse->successful()) {
$ackData = $ackResponse->json();
return jsonResponseWithSuccessMessage('Alarm acknowledged successfully', [
'id' => $ackData['id']['id'] ?? $id,
'acknowledged' => $ackData['acknowledged'] ?? true,
'ackTs' => $ackData['ackTs'] ?? now()->timestamp,
'status' => $ackData['status'] ?? 'ACTIVE_ACK'
]);
}
return jsonResponseWithErrorMessage('Failed to acknowledge alarm: ' . $ackResponse->body(), $ackResponse->status());
} catch (\Exception $e) {
return jsonResponseWithErrorMessage('An error occurred: ' . $e->getMessage(), 500);
}
}
}

View File

@@ -63,5 +63,5 @@ Route::post('/update-device-profile-master/{deviceId}', [DeviceProfileMasterCont
//******************************************************* Alarm API ********************************************************
Route::post('/alarm/create-or-update', [AlarmController::class, 'createOrUpdateAlarm'])->name('alarm.create-or-update');
Route::post('/alarm/filter', [AlarmController::class, 'filterAlarm'])->name('alarm.filter');
Route::get('/alarm/{id}', [AlarmController::class, 'getAlarmById'])->name('get.alarm');
Route::post('/alarm/ack/{id}', [AlarmController::class, 'acknowledgeAlarmById'])->name('ack.alarm');