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 getAlarmById($id) { try { $token = app('App\Services\AdminService')->getToken(); // Fetch cached token $url = env('THINGSBOARD_URL') . "/api/alarm/{$id}"; $response = Http::withHeaders([ 'X-Authorization' => 'Bearer ' . $token, 'accept' => 'application/json', ])->get($url); if ($response->successful()) { $responseData = $response->json(); $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('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); } } }