Files
backend_vib360_laravel/app/Http/Controllers/AlarmController.php
2025-03-26 19:53:56 +05:30

98 lines
3.2 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Services\AlarmService;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
use Exception;
use Illuminate\Http\Request;
class AlarmController extends Controller
{
protected $alarmService;
public function __construct(AlarmService $alarmService)
{
$this->alarmService = $alarmService;
}
public function createOrUpdateAlarm(Request $request)
{
try {
$alarmData = [
'type' => $request->type ?? null,
'severity' => $request->severity ?? null,
'acknowledged' => false ?? null,
'cleared' => $request->cleared ?? Carbon::now()->timestamp,
'startTs' => $request->startTs ?? Carbon::now()->timestamp,
'endTs' => $request->endTs ?? Carbon::now()->timestamp,
'details' => $request->details ?? [],
'propagate' => $request->propagate ?? false,
'propagateToOwner' => $request->propagateToOwner ?? false,
'propagateToTenant' => $request->propagateToTenant ?? false,
'originator' => $request->originator ?? 1,
'assigneeId' => $request->assigneeId ?? 1,
];
// Handle updating existing alarm
if (!empty($request->id)) {
$alarmData['id'] = $request->id;
}
// Call Service to create/update device
$apiResponse = $this->alarmService->createOrUpdateAlarm($alarmData);
return jsonResponseWithSuccessMessage(
!empty($request->id) ? 'Alarm updated successfully' : 'Alarm created successfully',
['api_response' => $apiResponse]
);
} catch (Exception $e) {
Log::error("Error: " . $e->getMessage());
$errorResponse = json_decode($e->getMessage(), true);
if (json_last_error() === JSON_ERROR_NONE) {
return jsonResponseWithErrorMessage($errorResponse['message'] ?? 'Something went wrong', 400, $errorResponse);
}
return jsonResponseWithErrorMessage($e->getMessage(), 500);
}
}
public function filterAlarm(Request $request)
{
try {
$alarmData = $request->only([
// 'statusList',
'severity',
// 'type',
// 'assigneeId',
'pageSize',
'page',
// 'textSearch',
// 'sortProperty',
// 'sortOrder',
'startTs',
'endTs'
]);
$apiResponse = $this->alarmService->filterAlarm($alarmData);
return jsonResponseWithSuccessMessage('Alarm data retrieved successfully', ['api_response' => $apiResponse]);
} catch (Exception $e) {
Log::error("Error: " . $e->getMessage());
$errorResponse = json_decode($e->getMessage(), true);
if (json_last_error() === JSON_ERROR_NONE) {
return jsonResponseWithErrorMessage($errorResponse['message'] ?? 'Something went wrong', 400, $errorResponse);
}
return jsonResponseWithErrorMessage($e->getMessage(), 500);
}
}
}