From 3381eb11b5739ad504d08c7587a1ffa0393586e7 Mon Sep 17 00:00:00 2001 From: Vckynando12 Date: Fri, 21 Mar 2025 17:11:03 +0700 Subject: [PATCH] add chart,filter,anda rechange display history to theme folwbite,and add pagination to shorter data view on website --- app/Console/Commands/FetchFirebaseData.php | 31 +- app/Events/ReportUpdated.php | 38 + app/Http/Controllers/ReportController.php | 75 +- app/Providers/FirebaseServiceProvider.php | 6 +- resources/views/reports.blade.php | 1412 ++++++++++++++++++-- routes/api.php | 3 + routes/web.php | 2 +- 7 files changed, 1437 insertions(+), 130 deletions(-) create mode 100644 app/Events/ReportUpdated.php diff --git a/app/Console/Commands/FetchFirebaseData.php b/app/Console/Commands/FetchFirebaseData.php index 5eca23c..e1cb508 100644 --- a/app/Console/Commands/FetchFirebaseData.php +++ b/app/Console/Commands/FetchFirebaseData.php @@ -6,6 +6,8 @@ use Kreait\Firebase\Factory; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; +use Illuminate\Support\Facades\Event; +use App\Events\ReportUpdated; class FetchFirebaseData extends Command { @@ -25,6 +27,9 @@ public function handle() $securityData = $firebase->getReference('security')->getValue() ?? []; $smartcabData = $firebase->getReference('smartcab')->getValue() ?? []; $dht11Data = $firebase->getReference('dht11')->getValue() ?? []; + $controlData = $firebase->getReference('control')->getValue() ?? []; + $deviceData = $firebase->getReference('device')->getValue() ?? []; + $logsData = $firebase->getReference('logs')->getValue() ?? []; // Baca history data yang sudah ada $historyData = []; @@ -38,14 +43,17 @@ public function handle() // Ambil data terakhir jika ada $lastEntry = !empty($historyData) ? end($historyData) : null; - // Cek apakah ada perubahan pada security atau smartcab + // Cek apakah ada perubahan pada security, smartcab, control, atau logs $hasChanges = false; if ($lastEntry === null) { $hasChanges = true; } else { $securityChanged = $this->hasDataChanged($lastEntry['security'] ?? [], $securityData); $smartcabChanged = $this->hasDataChanged($lastEntry['smartcab'] ?? [], $smartcabData); - $hasChanges = $securityChanged || $smartcabChanged; + $controlChanged = $this->hasDataChanged($lastEntry['control'] ?? [], $controlData); + $logsChanged = $this->hasDataChanged($lastEntry['logs'] ?? [], $logsData); + + $hasChanges = $securityChanged || $smartcabChanged || $controlChanged || $logsChanged; } // Hanya simpan jika ada perubahan @@ -54,25 +62,40 @@ public function handle() 'id' => Str::uuid()->toString(), // Generate ID unik 'timestamp' => now()->toIso8601String(), 'security' => $securityData, - 'smartcab' => $smartcabData + 'smartcab' => $smartcabData, + 'control' => $controlData, + 'logs' => $logsData ]; if (!empty($dht11Data)) { $newData['dht11'] = $dht11Data; } + + if (!empty($deviceData)) { + $newData['device'] = $deviceData; + } $historyData[] = $newData; Storage::put('reports.json', json_encode($historyData, JSON_PRETTY_PRINT)); $this->info('Data baru tersimpan dengan ID: ' . $newData['id']); + // Broadcast event untuk realtime update + event(new ReportUpdated($newData)); + if (isset($securityChanged) && $securityChanged) { $this->info('Perubahan terdeteksi pada security'); } if (isset($smartcabChanged) && $smartcabChanged) { $this->info('Perubahan terdeteksi pada smartcab'); } + if (isset($controlChanged) && $controlChanged) { + $this->info('Perubahan terdeteksi pada control'); + } + if (isset($logsChanged) && $logsChanged) { + $this->info('Perubahan terdeteksi pada logs'); + } } else { - $this->info('Tidak ada perubahan pada security atau smartcab, data tidak disimpan'); + $this->info('Tidak ada perubahan pada data, data tidak disimpan'); } } catch (\Exception $e) { diff --git a/app/Events/ReportUpdated.php b/app/Events/ReportUpdated.php new file mode 100644 index 0000000..964bf3f --- /dev/null +++ b/app/Events/ReportUpdated.php @@ -0,0 +1,38 @@ +report = $report; + } + + /** + * Get the channels the event should broadcast on. + * + * @return \Illuminate\Broadcasting\Channel|array + */ + public function broadcastOn() + { + return new Channel('reports'); + } +} \ No newline at end of file diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php index 813b55a..ee892db 100644 --- a/app/Http/Controllers/ReportController.php +++ b/app/Http/Controllers/ReportController.php @@ -9,28 +9,64 @@ class ReportController extends Controller { public function getReports() -{ - $jsonPath = storage_path('app/reports.json'); + { + $jsonPath = storage_path('app/reports.json'); - if (!File::exists($jsonPath)) { - return response()->json([]); + if (!File::exists($jsonPath)) { + return response()->json([]); + } + + $jsonData = json_decode(File::get($jsonPath), true); + + if (!$jsonData) { + return response()->json([]); + } + + // Urutkan data berdasarkan timestamp (terbaru dulu) + usort($jsonData, function($a, $b) { + $timeA = strtotime($a['timestamp']); + $timeB = strtotime($b['timestamp']); + return $timeB - $timeA; // Descending order + }); + + foreach ($jsonData as &$report) { + $carbonTime = Carbon::parse($report['timestamp'])->setTimezone('Asia/Jakarta'); + $report['tanggal'] = $carbonTime->format('d M Y'); // Format tanggal + $report['waktu'] = $carbonTime->format('H:i:s'); // Format waktu + } + + return response()->json($jsonData); } - $jsonData = json_decode(File::get($jsonPath), true); + public function getLatestReport() + { + $jsonPath = storage_path('app/reports.json'); - if (!$jsonData) { - return response()->json([]); + if (!File::exists($jsonPath)) { + return response()->json(null); + } + + $jsonData = json_decode(File::get($jsonPath), true); + + if (!$jsonData || empty($jsonData)) { + return response()->json(null); + } + + // Urutkan data berdasarkan timestamp (terbaru dulu) + usort($jsonData, function($a, $b) { + $timeA = strtotime($a['timestamp']); + $timeB = strtotime($b['timestamp']); + return $timeB - $timeA; // Descending order + }); + + $latestReport = $jsonData[0]; + $carbonTime = Carbon::parse($latestReport['timestamp'])->setTimezone('Asia/Jakarta'); + $latestReport['tanggal'] = $carbonTime->format('d M Y'); + $latestReport['waktu'] = $carbonTime->format('H:i:s'); + + return response()->json($latestReport); } - foreach ($jsonData as &$report) { - $carbonTime = Carbon::parse($report['timestamp'])->setTimezone('Asia/Jakarta'); - $report['tanggal'] = $carbonTime->format('d M Y'); // Format tanggal - $report['waktu'] = $carbonTime->format('H:i:s'); // Format waktu - } - - return response()->json($jsonData); -} - public function index() { // Path file JSON (sesuaikan dengan lokasi yang benar) @@ -49,6 +85,13 @@ public function index() return view('reports', ['reports' => []]); } + // Urutkan data berdasarkan timestamp (terbaru dulu) + usort($jsonData, function($a, $b) { + $timeA = strtotime($a['timestamp']); + $timeB = strtotime($b['timestamp']); + return $timeB - $timeA; // Descending order + }); + // Konversi timestamp ke Waktu Indonesia Barat (WIB) dan pisahkan tanggal & waktu foreach ($jsonData as &$report) { $carbonTime = Carbon::parse($report['timestamp'])->setTimezone('Asia/Jakarta'); diff --git a/app/Providers/FirebaseServiceProvider.php b/app/Providers/FirebaseServiceProvider.php index acea1ae..27eba0f 100644 --- a/app/Providers/FirebaseServiceProvider.php +++ b/app/Providers/FirebaseServiceProvider.php @@ -13,8 +13,8 @@ public function register() { $this->app->singleton(FirebaseAuth::class, function ($app) { $factory = (new Factory) - ->withServiceAccount(json_decode(env('FIREBASE_CREDENTIALS'), true)) - ->withDatabaseUri(env('FIREBASE_DATABASE_URL')); + ->withServiceAccount(config('firebase.credentials')) + ->withDatabaseUri(config('firebase.database_url')); return $factory->createAuth(); }); @@ -32,4 +32,4 @@ public function boot() { // } -} +} \ No newline at end of file diff --git a/resources/views/reports.blade.php b/resources/views/reports.blade.php index 8e57dc5..f383c4d 100644 --- a/resources/views/reports.blade.php +++ b/resources/views/reports.blade.php @@ -1,137 +1,1337 @@ - Report Data - + + + - + -
-

Laporan Keamanan dan Monitoring

+
+
+

Laporan Keamanan dan Monitoring

+ + + + + Kembali ke Dashboard + +
- @php - use Illuminate\Pagination\LengthAwarePaginator; + +
+
+
Ringkasan Status Sistem
+ +
+
+
+
+
Penjelasan Status
+
    +
  • + + Keamanan Normal: Sistem keamanan berjalan dengan baik +
  • +
  • + + Status Bahaya: Sistem keamanan mendeteksi potensi bahaya +
  • +
  • + + Gerakan Terdeteksi: Sensor gerakan mendeteksi aktivitas +
  • +
  • + + Tidak Ada Gerakan: Tidak ada gerakan yang terdeteksi +
  • +
  • + + Servo Terkunci: Servo dalam keadaan terkunci +
  • +
  • + + Servo Terbuka: Servo dalam keadaan terbuka +
  • +
  • + + Akses Terakhir: Perubahan pada akses terakhir +
  • +
  • + + Kontrol Diubah: Terjadi perubahan pada kontrol perangkat +
  • +
  • + + Status Perangkat: Perubahan pada status perangkat +
  • +
  • + + Error/Warning: Terdapat kesalahan atau peringatan dalam sistem +
  • +
+
+
+
+ + +
+
+
Filter Data
+ +
- $reportsCollection = collect($reports)->sortByDesc('timestamp'); - $currentPage = request()->get('page', 1); - $perPage = 1000; - $paginatedReports = new LengthAwarePaginator( - $reportsCollection->forPage($currentPage, $perPage), - $reportsCollection->count(), - $perPage, - $currentPage, - ['path' => request()->url()] - ); - @endphp + +
- @if($paginatedReports->count() > 0) -
- - - - - - - - - - - - @foreach($paginatedReports as $key => $report) - - - - - - - - @endforeach - -
#TanggalPerangkatStatusInfo Selengkapnya
{{ $paginatedReports->firstItem() + $key }}{{ date('d/m/Y H:i', strtotime($report['timestamp'])) }} - @if($loop->first) - Gerakan - @elseif($key > 0 && $report['security']['motion'] !== $reports[$key - 1]['security']['motion']) - Gerakan - @elseif($key > 0 && $report['security']['status'] !== $reports[$key - 1]['security']['status']) - Status Keamanan - @elseif($key > 0 && $report['smartcab']['last_access'] !== $reports[$key - 1]['smartcab']['last_access']) - Akses Terakhir - @elseif($key > 0 && $report['smartcab']['servo_status'] !== $reports[$key - 1]['smartcab']['servo_status']) - Status Servo - @else - - - @endif - - @if($loop->first) - {{ ucfirst($report['security']['motion']) }} - @elseif($key > 0 && $report['security']['motion'] !== $reports[$key - 1]['security']['motion']) - {{ ucfirst($report['security']['motion']) }} - @elseif($key > 0 && $report['security']['status'] !== $reports[$key - 1]['security']['status']) - {{ ucfirst($report['security']['status']) }} - @elseif($key > 0 && $report['smartcab']['last_access'] !== $reports[$key - 1]['smartcab']['last_access']) - {{ ucfirst($report['smartcab']['last_access']) }} - @elseif($key > 0 && $report['smartcab']['servo_status'] !== $reports[$key - 1]['smartcab']['servo_status']) - {{ ucfirst($report['smartcab']['servo_status']) }} - @else - Tidak ada perubahan - @endif - - -
+
+ + + + + Memperbarui data... +
+ +
+ +
+ + +
+
+ + Menampilkan 0-0 dari 0 data +
- -
- {{ $paginatedReports->links('pagination::bootstrap-4') }} +
+ +
- @else -
- Tidak ada data laporan. -
- @endif +
-