418 lines
14 KiB
PHP
418 lines
14 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Kreait\Firebase\Database;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class FirebaseService
|
|
{
|
|
protected $database;
|
|
|
|
public function __construct(Database $database)
|
|
{
|
|
$this->database = $database;
|
|
}
|
|
|
|
/**
|
|
* Simpan data sensor ke Firebase
|
|
*/
|
|
public function saveSensorData($temperature, $humidity, $ph, $tds)
|
|
{
|
|
try {
|
|
$timestamp = now()->toISOString();
|
|
$data = [
|
|
'temperature' => (float) $temperature,
|
|
'humidity' => (float) $humidity,
|
|
'ph' => (float) $ph,
|
|
'tds' => (float) $tds,
|
|
'timestamp' => $timestamp,
|
|
'created_at' => time()
|
|
];
|
|
|
|
$reference = $this->database->getReference('sensor_data');
|
|
$newRef = $reference->push($data);
|
|
|
|
Log::info('Data sensor berhasil disimpan ke Firebase', ['key' => $newRef->getKey()]);
|
|
return $newRef->getKey();
|
|
} catch (\Exception $e) {
|
|
Log::error('Error saving sensor data to Firebase: ' . $e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Simpan data actuator ke Firebase
|
|
*/
|
|
public function saveActuatorData($nama, $status)
|
|
{
|
|
try {
|
|
$timestamp = now()->toISOString();
|
|
$data = [
|
|
'nama' => $nama,
|
|
'status' => $status,
|
|
'timestamp' => $timestamp,
|
|
'created_at' => time()
|
|
];
|
|
|
|
$reference = $this->database->getReference('actuator_data');
|
|
$newRef = $reference->push($data);
|
|
|
|
Log::info('Data actuator berhasil disimpan ke Firebase', ['key' => $newRef->getKey()]);
|
|
return $newRef->getKey();
|
|
} catch (\Exception $e) {
|
|
Log::error('Error saving actuator data to Firebase: ' . $e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Simpan data umur tanaman ke Firebase
|
|
*/
|
|
public function saveUmurTanamanData($jenisTanaman, $tanggalTanam, $umurHari, $status = 'aktif')
|
|
{
|
|
try {
|
|
$timestamp = now()->toISOString();
|
|
$data = [
|
|
'jenis_tanaman' => $jenisTanaman,
|
|
'tanggal_tanam' => $tanggalTanam,
|
|
'umur_hari' => (int) $umurHari,
|
|
'status' => $status,
|
|
'timestamp' => $timestamp,
|
|
'created_at' => time()
|
|
];
|
|
|
|
$reference = $this->database->getReference('umur_tanaman_data');
|
|
$newRef = $reference->push($data);
|
|
|
|
Log::info('Data umur tanaman berhasil disimpan ke Firebase', ['key' => $newRef->getKey()]);
|
|
return $newRef->getKey();
|
|
} catch (\Exception $e) {
|
|
Log::error('Error saving umur tanaman data to Firebase: ' . $e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Ambil data sensor terbaru dari Firebase
|
|
*/
|
|
public function getLatestSensorData($limit = 10)
|
|
{
|
|
try {
|
|
$reference = $this->database->getReference('sensor_data');
|
|
$snapshot = $reference->orderByChild('created_at')
|
|
->limitToLast($limit)
|
|
->getSnapshot();
|
|
|
|
$data = [];
|
|
foreach ($snapshot->getValue() ?: [] as $key => $value) {
|
|
$data[] = array_merge($value, ['firebase_key' => $key]);
|
|
}
|
|
|
|
return array_reverse($data); // Urutkan dari yang terbaru
|
|
} catch (\Exception $e) {
|
|
Log::error('Error getting sensor data from Firebase: ' . $e->getMessage());
|
|
return [];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Ambil data actuator terbaru dari Firebase
|
|
*/
|
|
public function getLatestActuatorData($limit = 10)
|
|
{
|
|
try {
|
|
$reference = $this->database->getReference('actuator_data');
|
|
$snapshot = $reference->orderByChild('created_at')
|
|
->limitToLast($limit)
|
|
->getSnapshot();
|
|
|
|
$data = [];
|
|
foreach ($snapshot->getValue() ?: [] as $key => $value) {
|
|
$data[] = array_merge($value, ['firebase_key' => $key]);
|
|
}
|
|
|
|
return array_reverse($data);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error getting actuator data from Firebase: ' . $e->getMessage());
|
|
return [];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Ambil data umur tanaman terbaru dari Firebase
|
|
*/
|
|
public function getLatestUmurTanamanData($limit = 10)
|
|
{
|
|
try {
|
|
$reference = $this->database->getReference('umur_tanaman_data');
|
|
$snapshot = $reference->orderByChild('created_at')
|
|
->limitToLast($limit)
|
|
->getSnapshot();
|
|
|
|
$data = [];
|
|
foreach ($snapshot->getValue() ?: [] as $key => $value) {
|
|
$data[] = array_merge($value, ['firebase_key' => $key]);
|
|
}
|
|
|
|
return array_reverse($data);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error getting umur tanaman data from Firebase: ' . $e->getMessage());
|
|
return [];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update status actuator di Firebase
|
|
*/
|
|
public function updateActuatorStatus($firebaseKey, $status)
|
|
{
|
|
try {
|
|
$reference = $this->database->getReference('actuator_data/' . $firebaseKey);
|
|
$reference->update([
|
|
'status' => $status,
|
|
'updated_at' => time(),
|
|
'timestamp' => now()->toISOString()
|
|
]);
|
|
|
|
Log::info('Status actuator berhasil diupdate di Firebase', ['key' => $firebaseKey, 'status' => $status]);
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
Log::error('Error updating actuator status in Firebase: ' . $e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Ambil data real-time untuk monitoring
|
|
*/
|
|
public function getRealtimeData()
|
|
{
|
|
try {
|
|
$sensorData = $this->getLatestSensorData(5);
|
|
$actuatorData = $this->getLatestActuatorData(5);
|
|
|
|
return [
|
|
'sensor' => $sensorData,
|
|
'actuator' => $actuatorData,
|
|
'last_update' => now()->toISOString()
|
|
];
|
|
} catch (\Exception $e) {
|
|
Log::error('Error getting realtime data from Firebase: ' . $e->getMessage());
|
|
return [
|
|
'sensor' => [],
|
|
'actuator' => [],
|
|
'last_update' => now()->toISOString()
|
|
];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Test koneksi Firebase
|
|
*/
|
|
public function testConnection()
|
|
{
|
|
try {
|
|
$reference = $this->database->getReference('test');
|
|
$reference->set([
|
|
'message' => 'Firebase connection test',
|
|
'timestamp' => now()->toISOString()
|
|
]);
|
|
|
|
$snapshot = $reference->getSnapshot();
|
|
return $snapshot->exists();
|
|
} catch (\Exception $e) {
|
|
Log::error('Firebase connection test failed: ' . $e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get all sensor data from Firebase
|
|
*/
|
|
public function getAllSensorData()
|
|
{
|
|
try {
|
|
$reference = $this->database->getReference('sensor_data');
|
|
$snapshot = $reference->getSnapshot();
|
|
|
|
if ($snapshot->exists()) {
|
|
$data = $snapshot->getValue();
|
|
$result = [];
|
|
|
|
foreach ($data as $key => $item) {
|
|
$result[] = [
|
|
'firebase_key' => $key,
|
|
'temperature' => $item['temperature'] ?? 0,
|
|
'humidity' => $item['humidity'] ?? 0,
|
|
'ph' => $item['ph'] ?? 0,
|
|
'tds' => $item['tds'] ?? 0,
|
|
'timestamp' => $item['timestamp'] ?? '',
|
|
'created_at' => isset($item['created_at']) ? date('Y-m-d H:i:s', $item['created_at']) : ''
|
|
];
|
|
}
|
|
|
|
// Sort by timestamp descending
|
|
usort($result, function($a, $b) {
|
|
return strtotime($b['timestamp']) - strtotime($a['timestamp']);
|
|
});
|
|
|
|
return $result;
|
|
}
|
|
|
|
return [];
|
|
} catch (\Exception $e) {
|
|
Log::error('Error getting sensor data from Firebase: ' . $e->getMessage());
|
|
return [];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get all actuator data from Firebase
|
|
*/
|
|
public function getAllActuatorData()
|
|
{
|
|
try {
|
|
$reference = $this->database->getReference('actuator_data');
|
|
$snapshot = $reference->getSnapshot();
|
|
|
|
if ($snapshot->exists()) {
|
|
$data = $snapshot->getValue();
|
|
$result = [];
|
|
|
|
foreach ($data as $key => $item) {
|
|
$result[] = [
|
|
'firebase_key' => $key,
|
|
'nama' => $item['nama'] ?? '',
|
|
'status' => $item['status'] ?? '',
|
|
'timestamp' => $item['timestamp'] ?? '',
|
|
'created_at' => isset($item['created_at']) ? date('Y-m-d H:i:s', $item['created_at']) : ''
|
|
];
|
|
}
|
|
|
|
// Sort by timestamp descending
|
|
usort($result, function($a, $b) {
|
|
return strtotime($b['timestamp']) - strtotime($a['timestamp']);
|
|
});
|
|
|
|
return $result;
|
|
}
|
|
|
|
return [];
|
|
} catch (\Exception $e) {
|
|
Log::error('Error getting actuator data from Firebase: ' . $e->getMessage());
|
|
return [];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get all umur tanaman data from Firebase
|
|
*/
|
|
public function getAllUmurTanamanData()
|
|
{
|
|
try {
|
|
$reference = $this->database->getReference('umur_tanaman_data');
|
|
$snapshot = $reference->getSnapshot();
|
|
|
|
if ($snapshot->exists()) {
|
|
$data = $snapshot->getValue();
|
|
$result = [];
|
|
|
|
foreach ($data as $key => $item) {
|
|
$result[] = [
|
|
'firebase_key' => $key,
|
|
'jenis_tanaman' => $item['jenis_tanaman'] ?? '',
|
|
'tanggal_tanam' => $item['tanggal_tanam'] ?? '',
|
|
'umur_hari' => $item['umur_hari'] ?? 0,
|
|
'status' => $item['status'] ?? '',
|
|
'timestamp' => $item['timestamp'] ?? '',
|
|
'created_at' => isset($item['created_at']) ? date('Y-m-d H:i:s', $item['created_at']) : ''
|
|
];
|
|
}
|
|
|
|
// Sort by timestamp descending
|
|
usort($result, function($a, $b) {
|
|
return strtotime($b['timestamp']) - strtotime($a['timestamp']);
|
|
});
|
|
|
|
return $result;
|
|
}
|
|
|
|
return [];
|
|
} catch (\Exception $e) {
|
|
Log::error('Error getting umur tanaman data from Firebase: ' . $e->getMessage());
|
|
return [];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get combined data for reports from Firebase
|
|
*/
|
|
public function getReportData($date_from = null, $date_to = null)
|
|
{
|
|
try {
|
|
$sensorData = $this->getAllSensorData();
|
|
$actuatorData = $this->getAllActuatorData();
|
|
$umurTanamanData = $this->getAllUmurTanamanData();
|
|
|
|
// Filter by date if provided
|
|
if ($date_from || $date_to) {
|
|
if ($date_from) {
|
|
$sensorData = array_filter($sensorData, function($item) use ($date_from) {
|
|
return strtotime($item['created_at']) >= strtotime($date_from);
|
|
});
|
|
$actuatorData = array_filter($actuatorData, function($item) use ($date_from) {
|
|
return strtotime($item['created_at']) >= strtotime($date_from);
|
|
});
|
|
$umurTanamanData = array_filter($umurTanamanData, function($item) use ($date_from) {
|
|
return strtotime($item['created_at']) >= strtotime($date_from);
|
|
});
|
|
}
|
|
|
|
if ($date_to) {
|
|
$sensorData = array_filter($sensorData, function($item) use ($date_to) {
|
|
return strtotime($item['created_at']) <= strtotime($date_to . ' 23:59:59');
|
|
});
|
|
$actuatorData = array_filter($actuatorData, function($item) use ($date_to) {
|
|
return strtotime($item['created_at']) <= strtotime($date_to . ' 23:59:59');
|
|
});
|
|
$umurTanamanData = array_filter($umurTanamanData, function($item) use ($date_to) {
|
|
return strtotime($item['created_at']) <= strtotime($date_to . ' 23:59:59');
|
|
});
|
|
}
|
|
}
|
|
|
|
return [
|
|
'sensor' => array_values($sensorData),
|
|
'actuator' => array_values($actuatorData),
|
|
'umur_tanaman' => array_values($umurTanamanData),
|
|
'summary' => [
|
|
'total_sensor_records' => count($sensorData),
|
|
'total_actuator_records' => count($actuatorData),
|
|
'total_umur_tanaman_records' => count($umurTanamanData),
|
|
'date_range' => [
|
|
'from' => $date_from,
|
|
'to' => $date_to
|
|
]
|
|
]
|
|
];
|
|
} catch (\Exception $e) {
|
|
Log::error('Error getting report data from Firebase: ' . $e->getMessage());
|
|
return [
|
|
'sensor' => [],
|
|
'actuator' => [],
|
|
'umur_tanaman' => [],
|
|
'summary' => [
|
|
'total_sensor_records' => 0,
|
|
'total_actuator_records' => 0,
|
|
'total_umur_tanaman_records' => 0,
|
|
'date_range' => [
|
|
'from' => $date_from,
|
|
'to' => $date_to
|
|
]
|
|
]
|
|
];
|
|
}
|
|
}
|
|
}
|