add connection capture report history with fetchfirebase data
This commit is contained in:
parent
b63cc03255
commit
72cf317483
|
@ -1,4 +1,4 @@
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <FirebaseESP8266.h>
|
#include <FirebaseESP8266.h>
|
||||||
#include <DHT.h>
|
#include <DHT.h>
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,94 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use Google\Cloud\Firestore\Admin\V1\CreateDatabaseRequest;
|
||||||
|
use GuzzleHttp\Promise\Create;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Kreait\Firebase\Factory;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
class FetchFirebaseData extends Command
|
||||||
|
{
|
||||||
|
protected $signature = 'firebase:fetch';
|
||||||
|
protected $description = 'Ambil data dari Firebase jika ada perubahan';
|
||||||
|
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
// Inisialisasi Firebase
|
||||||
|
$firebase = (new Factory)
|
||||||
|
->withServiceAccount(storage_path('app/smartcab-8bb42-firebase-adminsdk-fbsvc-de33a8e45b.json'))
|
||||||
|
->withDatabaseUri(env('FIREBASE_DATABASE_URL'))
|
||||||
|
->createDatabase();
|
||||||
|
// Ambil data terbaru dari Firebase
|
||||||
|
$securityData = $firebase->getReference('security')->getValue() ?? [];
|
||||||
|
$smartcabData = $firebase->getReference('smartcab')->getValue() ?? [];
|
||||||
|
$dht11Data = $firebase->getReference('dht11')->getValue() ?? [];
|
||||||
|
|
||||||
|
// Baca history data yang sudah ada
|
||||||
|
$historyData = [];
|
||||||
|
if (Storage::exists('reports.json')) {
|
||||||
|
$historyData = json_decode(Storage::get('reports.json'), true) ?: [];
|
||||||
|
// Pastikan $historyData adalah array
|
||||||
|
if (!is_array($historyData)) {
|
||||||
|
$historyData = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ambil data terakhir jika ada
|
||||||
|
$lastEntry = !empty($historyData) ? end($historyData) : null;
|
||||||
|
|
||||||
|
// Cek apakah ada perubahan pada security atau smartcab
|
||||||
|
$hasChanges = false;
|
||||||
|
if ($lastEntry === null) {
|
||||||
|
// Jika belum ada data sama sekali, simpan data pertama
|
||||||
|
$hasChanges = true;
|
||||||
|
} else {
|
||||||
|
// Bandingkan security dan smartcab dengan data terakhir
|
||||||
|
$securityChanged = $this->hasDataChanged($lastEntry['security'] ?? [], $securityData);
|
||||||
|
$smartcabChanged = $this->hasDataChanged($lastEntry['smartcab'] ?? [], $smartcabData);
|
||||||
|
$hasChanges = $securityChanged || $smartcabChanged;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hanya simpan jika ada perubahan
|
||||||
|
if ($hasChanges) {
|
||||||
|
$newData = [
|
||||||
|
'timestamp' => now()->toIso8601String(),
|
||||||
|
'security' => $securityData,
|
||||||
|
'smartcab' => $smartcabData
|
||||||
|
];
|
||||||
|
|
||||||
|
if (!empty($dht11Data)) {
|
||||||
|
$newData['dht11'] = $dht11Data;
|
||||||
|
}
|
||||||
|
|
||||||
|
$historyData[] = $newData;
|
||||||
|
Storage::put('reports.json', json_encode($historyData, JSON_PRETTY_PRINT));
|
||||||
|
$this->info('Data baru tersimpan karena ada perubahan pada security atau smartcab');
|
||||||
|
|
||||||
|
// Debug info
|
||||||
|
if (isset($securityChanged) && $securityChanged) {
|
||||||
|
$this->info('Perubahan terdeteksi pada security');
|
||||||
|
}
|
||||||
|
if (isset($smartcabChanged) && $smartcabChanged) {
|
||||||
|
$this->info('Perubahan terdeteksi pada smartcab');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$this->info('Tidak ada perubahan pada security atau smartcab, data tidak disimpan');
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$this->error('Error: ' . $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function hasDataChanged($oldData, $newData)
|
||||||
|
{
|
||||||
|
$flags = defined('JSON_SORT_KEYS') ? JSON_SORT_KEYS : 0;
|
||||||
|
$oldJson = json_encode($oldData, $flags);
|
||||||
|
$newJson = json_encode($newData, $flags);
|
||||||
|
|
||||||
|
|
||||||
|
return $oldJson !== $newJson;
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,9 +12,8 @@ class Kernel extends ConsoleKernel
|
||||||
*/
|
*/
|
||||||
protected function schedule(Schedule $schedule): void
|
protected function schedule(Schedule $schedule): void
|
||||||
{
|
{
|
||||||
// $schedule->command('inspire')->hourly();
|
$schedule->command('firebase:fetch')->everyMinute();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the commands for the application.
|
* Register the commands for the application.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
use App\Http\Controllers\Auth\FirebaseAuthController;
|
use App\Http\Controllers\Auth\FirebaseAuthController;
|
||||||
use App\Http\Controllers\AIChatController;
|
use App\Http\Controllers\AIChatController;
|
||||||
|
use App\Http\Controllers\FirebaseController;
|
||||||
use App\Http\Controllers\WelcomeController;
|
use App\Http\Controllers\WelcomeController;
|
||||||
use App\Http\Controllers\ProfileController;
|
use App\Http\Controllers\ProfileController;
|
||||||
|
|
||||||
|
@ -34,3 +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('/save-firebase', [FirebaseController::class, 'saveFirebaseData']);
|
|
@ -0,0 +1,39 @@
|
||||||
|
::[Bat To Exe Converter]
|
||||||
|
::
|
||||||
|
::YAwzoRdxOk+EWAnk
|
||||||
|
::fBw5plQjdG8=
|
||||||
|
::YAwzuBVtJxjWCl3EqQJhSA==
|
||||||
|
::ZR4luwNxJguZRRnVphFQ
|
||||||
|
::Yhs/ulQjdF+5
|
||||||
|
::cxAkpRVqdFKZSzk=
|
||||||
|
::cBs/ulQjdFy5
|
||||||
|
::ZR41oxFsdFKZSDk=
|
||||||
|
::eBoioBt6dFKZSDk=
|
||||||
|
::cRo6pxp7LAbNWATEpCI=
|
||||||
|
::egkzugNsPRvcWATEpCI=
|
||||||
|
::dAsiuh18IRvcCxnZtBJQ
|
||||||
|
::cRYluBh/LU+EWAnk
|
||||||
|
::YxY4rhs+aU+JeA==
|
||||||
|
::cxY6rQJ7JhzQF1fEqQJQ
|
||||||
|
::ZQ05rAF9IBncCkqN+0xwdVs0
|
||||||
|
::ZQ05rAF9IAHYFVzEqQJQ
|
||||||
|
::eg0/rx1wNQPfEVWB+kM9LVsJDGQ=
|
||||||
|
::fBEirQZwNQPfEVWB+kM9LVsJDGQ=
|
||||||
|
::cRolqwZ3JBvQF1fEqQJQ
|
||||||
|
::dhA7uBVwLU+EWDk=
|
||||||
|
::YQ03rBFzNR3SWATElA==
|
||||||
|
::dhAmsQZ3MwfNWATElA==
|
||||||
|
::ZQ0/vhVqMQ3MEVWAtB9wSA==
|
||||||
|
::Zg8zqx1/OA3MEVWAtB9wSA==
|
||||||
|
::dhA7pRFwIByZRRnk
|
||||||
|
::Zh4grVQjdCyDJGyX8VAjFA1dTw+bAE+/Fb4I5/jH/f+JpkwJVaw6YIq7
|
||||||
|
::YB416Ek+ZG8=
|
||||||
|
::
|
||||||
|
::
|
||||||
|
::978f952a14a936cc963da21a135fa983
|
||||||
|
@echo off
|
||||||
|
cd /d C:\laragon\www\AsecurityDumptruckModel
|
||||||
|
:loop
|
||||||
|
php artisan schedule:run
|
||||||
|
timeout /t 2 /nobreak >nul
|
||||||
|
goto loop
|
|
@ -0,0 +1,3 @@
|
||||||
|
Set WshShell = CreateObject("WScript.Shell")
|
||||||
|
WshShell.Run "C:\path\to\run_schedule.bat", 0, False
|
||||||
|
Set WshShell = Nothing
|
Loading…
Reference in New Issue