add chart,filter,anda rechange display history to theme folwbite,and add pagination to shorter data view on website
This commit is contained in:
parent
b20bae2f1c
commit
3381eb11b5
|
@ -6,6 +6,8 @@
|
||||||
use Kreait\Firebase\Factory;
|
use Kreait\Firebase\Factory;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
use Illuminate\Support\Facades\Event;
|
||||||
|
use App\Events\ReportUpdated;
|
||||||
|
|
||||||
class FetchFirebaseData extends Command
|
class FetchFirebaseData extends Command
|
||||||
{
|
{
|
||||||
|
@ -25,6 +27,9 @@ public function handle()
|
||||||
$securityData = $firebase->getReference('security')->getValue() ?? [];
|
$securityData = $firebase->getReference('security')->getValue() ?? [];
|
||||||
$smartcabData = $firebase->getReference('smartcab')->getValue() ?? [];
|
$smartcabData = $firebase->getReference('smartcab')->getValue() ?? [];
|
||||||
$dht11Data = $firebase->getReference('dht11')->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
|
// Baca history data yang sudah ada
|
||||||
$historyData = [];
|
$historyData = [];
|
||||||
|
@ -38,14 +43,17 @@ public function handle()
|
||||||
// Ambil data terakhir jika ada
|
// Ambil data terakhir jika ada
|
||||||
$lastEntry = !empty($historyData) ? end($historyData) : null;
|
$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;
|
$hasChanges = false;
|
||||||
if ($lastEntry === null) {
|
if ($lastEntry === null) {
|
||||||
$hasChanges = true;
|
$hasChanges = true;
|
||||||
} else {
|
} else {
|
||||||
$securityChanged = $this->hasDataChanged($lastEntry['security'] ?? [], $securityData);
|
$securityChanged = $this->hasDataChanged($lastEntry['security'] ?? [], $securityData);
|
||||||
$smartcabChanged = $this->hasDataChanged($lastEntry['smartcab'] ?? [], $smartcabData);
|
$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
|
// Hanya simpan jika ada perubahan
|
||||||
|
@ -54,25 +62,40 @@ public function handle()
|
||||||
'id' => Str::uuid()->toString(), // Generate ID unik
|
'id' => Str::uuid()->toString(), // Generate ID unik
|
||||||
'timestamp' => now()->toIso8601String(),
|
'timestamp' => now()->toIso8601String(),
|
||||||
'security' => $securityData,
|
'security' => $securityData,
|
||||||
'smartcab' => $smartcabData
|
'smartcab' => $smartcabData,
|
||||||
|
'control' => $controlData,
|
||||||
|
'logs' => $logsData
|
||||||
];
|
];
|
||||||
|
|
||||||
if (!empty($dht11Data)) {
|
if (!empty($dht11Data)) {
|
||||||
$newData['dht11'] = $dht11Data;
|
$newData['dht11'] = $dht11Data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!empty($deviceData)) {
|
||||||
|
$newData['device'] = $deviceData;
|
||||||
|
}
|
||||||
|
|
||||||
$historyData[] = $newData;
|
$historyData[] = $newData;
|
||||||
Storage::put('reports.json', json_encode($historyData, JSON_PRETTY_PRINT));
|
Storage::put('reports.json', json_encode($historyData, JSON_PRETTY_PRINT));
|
||||||
$this->info('Data baru tersimpan dengan ID: ' . $newData['id']);
|
$this->info('Data baru tersimpan dengan ID: ' . $newData['id']);
|
||||||
|
|
||||||
|
// Broadcast event untuk realtime update
|
||||||
|
event(new ReportUpdated($newData));
|
||||||
|
|
||||||
if (isset($securityChanged) && $securityChanged) {
|
if (isset($securityChanged) && $securityChanged) {
|
||||||
$this->info('Perubahan terdeteksi pada security');
|
$this->info('Perubahan terdeteksi pada security');
|
||||||
}
|
}
|
||||||
if (isset($smartcabChanged) && $smartcabChanged) {
|
if (isset($smartcabChanged) && $smartcabChanged) {
|
||||||
$this->info('Perubahan terdeteksi pada smartcab');
|
$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 {
|
} 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) {
|
} catch (\Exception $e) {
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Events;
|
||||||
|
|
||||||
|
use Illuminate\Broadcasting\Channel;
|
||||||
|
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||||
|
use Illuminate\Broadcasting\PresenceChannel;
|
||||||
|
use Illuminate\Broadcasting\PrivateChannel;
|
||||||
|
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||||
|
use Illuminate\Foundation\Events\Dispatchable;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
|
||||||
|
class ReportUpdated implements ShouldBroadcast
|
||||||
|
{
|
||||||
|
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||||
|
|
||||||
|
public $report;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new event instance.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct($report)
|
||||||
|
{
|
||||||
|
$this->report = $report;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the channels the event should broadcast on.
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Broadcasting\Channel|array
|
||||||
|
*/
|
||||||
|
public function broadcastOn()
|
||||||
|
{
|
||||||
|
return new Channel('reports');
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,7 +9,7 @@
|
||||||
class ReportController extends Controller
|
class ReportController extends Controller
|
||||||
{
|
{
|
||||||
public function getReports()
|
public function getReports()
|
||||||
{
|
{
|
||||||
$jsonPath = storage_path('app/reports.json');
|
$jsonPath = storage_path('app/reports.json');
|
||||||
|
|
||||||
if (!File::exists($jsonPath)) {
|
if (!File::exists($jsonPath)) {
|
||||||
|
@ -22,6 +22,13 @@ public function getReports()
|
||||||
return response()->json([]);
|
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) {
|
foreach ($jsonData as &$report) {
|
||||||
$carbonTime = Carbon::parse($report['timestamp'])->setTimezone('Asia/Jakarta');
|
$carbonTime = Carbon::parse($report['timestamp'])->setTimezone('Asia/Jakarta');
|
||||||
$report['tanggal'] = $carbonTime->format('d M Y'); // Format tanggal
|
$report['tanggal'] = $carbonTime->format('d M Y'); // Format tanggal
|
||||||
|
@ -29,7 +36,36 @@ public function getReports()
|
||||||
}
|
}
|
||||||
|
|
||||||
return response()->json($jsonData);
|
return response()->json($jsonData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getLatestReport()
|
||||||
|
{
|
||||||
|
$jsonPath = storage_path('app/reports.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);
|
||||||
|
}
|
||||||
|
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
|
@ -49,6 +85,13 @@ public function index()
|
||||||
return view('reports', ['reports' => []]);
|
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
|
// Konversi timestamp ke Waktu Indonesia Barat (WIB) dan pisahkan tanggal & waktu
|
||||||
foreach ($jsonData as &$report) {
|
foreach ($jsonData as &$report) {
|
||||||
$carbonTime = Carbon::parse($report['timestamp'])->setTimezone('Asia/Jakarta');
|
$carbonTime = Carbon::parse($report['timestamp'])->setTimezone('Asia/Jakarta');
|
||||||
|
|
|
@ -13,8 +13,8 @@ public function register()
|
||||||
{
|
{
|
||||||
$this->app->singleton(FirebaseAuth::class, function ($app) {
|
$this->app->singleton(FirebaseAuth::class, function ($app) {
|
||||||
$factory = (new Factory)
|
$factory = (new Factory)
|
||||||
->withServiceAccount(json_decode(env('FIREBASE_CREDENTIALS'), true))
|
->withServiceAccount(config('firebase.credentials'))
|
||||||
->withDatabaseUri(env('FIREBASE_DATABASE_URL'));
|
->withDatabaseUri(config('firebase.database_url'));
|
||||||
|
|
||||||
return $factory->createAuth();
|
return $factory->createAuth();
|
||||||
});
|
});
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -3,6 +3,7 @@
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
use App\Http\Controllers\Api\SensorDataController;
|
use App\Http\Controllers\Api\SensorDataController;
|
||||||
|
use App\Http\Controllers\ReportController;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
|
@ -20,3 +21,5 @@
|
||||||
});
|
});
|
||||||
|
|
||||||
Route::get('/sensor-data', [SensorDataController::class, 'getData']);
|
Route::get('/sensor-data', [SensorDataController::class, 'getData']);
|
||||||
|
Route::get('/reports', [ReportController::class, 'getReports']);
|
||||||
|
Route::get('/reports/latest', [ReportController::class, 'getLatestReport']);
|
||||||
|
|
|
@ -35,4 +35,4 @@
|
||||||
|
|
||||||
Route::get('/profile', [ProfileController::class, 'index'])->name('profile');
|
Route::get('/profile', [ProfileController::class, 'index'])->name('profile');
|
||||||
Route::post('/profile/update', [ProfileController::class, 'update'])->name('profile.update');
|
Route::post('/profile/update', [ProfileController::class, 'update'])->name('profile.update');
|
||||||
Route::get('/reports', [ReportController::class, 'index']);
|
Route::get('/reports', [ReportController::class, 'index'])->name('reports');
|
Loading…
Reference in New Issue