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 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) {
|
||||
|
|
|
@ -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,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');
|
||||
|
|
|
@ -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()
|
|||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -3,6 +3,7 @@
|
|||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use App\Http\Controllers\Api\SensorDataController;
|
||||
use App\Http\Controllers\ReportController;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
|
@ -20,3 +21,5 @@
|
|||
});
|
||||
|
||||
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::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