.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect } from "react";
|
||||
import { Activity, Power, ZapOff, Zap, AlertTriangle, Lightbulb, Wind, Projector, Clock } from "lucide-react";
|
||||
import { Activity, Power, ZapOff, Zap, AlertTriangle, Lightbulb, Wind, Projector } from "lucide-react";
|
||||
|
||||
export default function PowerMonitoringPage() {
|
||||
// =========================================================================
|
||||
@@ -13,10 +13,6 @@ export default function PowerMonitoringPage() {
|
||||
// State khusus untuk menampung rincian 3 MCB di D101
|
||||
const [powerDataD101, setPowerDataD101] = useState({ umum: 0, ac1: 0, ac2: 0 });
|
||||
|
||||
// 🌟 TAMBAHAN STATE UNTUK TIMER AUTO-CUTOFF
|
||||
const [timer, setTimer] = useState({ is_active: false, off_time: "22:00", on_time: "05:00" });
|
||||
const [isSavingTimer, setIsSavingTimer] = useState(false);
|
||||
|
||||
// =========================================================================
|
||||
// 2. FUNGSI FETCH DATA DARI BACKEND
|
||||
// =========================================================================
|
||||
@@ -70,17 +66,6 @@ export default function PowerMonitoringPage() {
|
||||
}
|
||||
};
|
||||
|
||||
// 🌟 C. Tarik Data Timer dari Golang
|
||||
const fetchTimer = async () => {
|
||||
try {
|
||||
const res = await fetch("http://172.17.172.17:8080/api/power/timer");
|
||||
const data = await res.json();
|
||||
setTimer(data);
|
||||
} catch (e) {
|
||||
console.error("Gagal mengambil data timer:", e);
|
||||
}
|
||||
};
|
||||
|
||||
// =========================================================================
|
||||
// 3. INISIALISASI & POLLING (AUTO-REFRESH)
|
||||
// =========================================================================
|
||||
@@ -106,8 +91,6 @@ export default function PowerMonitoringPage() {
|
||||
});
|
||||
setRoomDeviceStatus(initialStatus);
|
||||
|
||||
// Panggil fungsi awal
|
||||
fetchTimer();
|
||||
fetchPowerStatus();
|
||||
fetchDeviceStatus();
|
||||
|
||||
@@ -120,26 +103,8 @@ export default function PowerMonitoringPage() {
|
||||
}, []);
|
||||
|
||||
// =========================================================================
|
||||
// 4. FUNGSI KONTROL DEVICE, GLOBAL POWER & SIMPAN TIMER
|
||||
// 4. FUNGSI KONTROL DEVICE & CUT OFF
|
||||
// =========================================================================
|
||||
|
||||
// 🌟 D. Simpan Jadwal Timer ke Golang
|
||||
const saveTimer = async () => {
|
||||
setIsSavingTimer(true);
|
||||
try {
|
||||
const res = await fetch("http://172.17.172.17:8080/api/power/timer", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(timer)
|
||||
});
|
||||
if (res.ok) alert("Jadwal Auto-Cutoff Berhasil Disimpan!");
|
||||
} catch (e) {
|
||||
alert("Gagal menyimpan jadwal timer.");
|
||||
} finally {
|
||||
setIsSavingTimer(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleDeviceToggle = async (roomId: number, roomName: string, deviceName: string) => {
|
||||
const roomIdKey = `room_${roomId}`;
|
||||
const currentStatus = roomDeviceStatus[roomIdKey]?.[deviceName] || false;
|
||||
@@ -184,8 +149,8 @@ export default function PowerMonitoringPage() {
|
||||
if (roomName === "Kelas D101") {
|
||||
const actionType = currentRelayStatus ? "off" : "on";
|
||||
const confirmMessage = currentRelayStatus
|
||||
? `PERINGATAN FATAL: Anda yakin ingin MEMUTUS 3 MCB Daya di ${roomName}? (Proses berurutan ~3 detik)`
|
||||
: `RESTORE POWER: Anda yakin ingin MENYALAKAN KEMBALI 3 MCB Daya di ${roomName}? (Proses berurutan ~3 detik)`;
|
||||
? `PERINGATAN FATAL: Anda yakin ingin MEMUTUS MCB Daya Utama di ${roomName}?`
|
||||
: `RESTORE POWER: Anda yakin ingin MENYALAKAN KEMBALI MCB Daya di ${roomName}?`;
|
||||
|
||||
if (!window.confirm(confirmMessage)) return;
|
||||
|
||||
@@ -233,69 +198,6 @@ export default function PowerMonitoringPage() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 🌟 PANEL BARU: NIGHT MODE (AUTO-CUTOFF TIMER) */}
|
||||
<div className="bg-slate-900 rounded-xl p-5 shadow-lg border border-slate-800 flex flex-col lg:flex-row items-center justify-between gap-6 text-white mb-8">
|
||||
|
||||
<div className="flex items-center gap-4 w-full lg:w-auto">
|
||||
<div className={`p-3 rounded-full transition-colors ${timer.is_active ? 'bg-blue-500' : 'bg-slate-700'}`}>
|
||||
<Clock size={24} className="text-white" />
|
||||
</div>
|
||||
<div>
|
||||
<h3 className="font-bold text-lg">Global Night Mode <span className="text-xs font-normal text-slate-400 bg-slate-800 px-2 py-0.5 rounded-full ml-2">Auto-Cutoff</span></h3>
|
||||
<p className="text-xs text-slate-400 mt-1">Otomatis putus daya listrik (MCB) seluruh kelas pada jam malam.</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex flex-col sm:flex-row items-center gap-4 w-full lg:w-auto">
|
||||
{/* Input Jam */}
|
||||
<div className="flex items-center gap-3 bg-slate-800 p-3 rounded-lg border border-slate-700 w-full sm:w-auto justify-center">
|
||||
<div className="flex items-center gap-2">
|
||||
<label className="text-xs font-bold text-slate-400">MATI:</label>
|
||||
<input
|
||||
type="time"
|
||||
value={timer.off_time}
|
||||
onChange={(e) => setTimer({...timer, off_time: e.target.value})}
|
||||
className="bg-slate-900 border border-slate-600 rounded px-2 py-1 text-sm outline-none focus:border-blue-500 transition-colors"
|
||||
disabled={timer.is_active}
|
||||
/>
|
||||
</div>
|
||||
<span className="text-slate-600 font-bold">-</span>
|
||||
<div className="flex items-center gap-2">
|
||||
<label className="text-xs font-bold text-slate-400">NYALA:</label>
|
||||
<input
|
||||
type="time"
|
||||
value={timer.on_time}
|
||||
onChange={(e) => setTimer({...timer, on_time: e.target.value})}
|
||||
className="bg-slate-900 border border-slate-600 rounded px-2 py-1 text-sm outline-none focus:border-blue-500 transition-colors"
|
||||
disabled={timer.is_active}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Tombol Aksi */}
|
||||
<div className="flex items-center gap-3 w-full sm:w-auto">
|
||||
<button
|
||||
onClick={() => setTimer({...timer, is_active: !timer.is_active})}
|
||||
className={`px-4 py-2.5 text-sm font-bold rounded-lg border transition-all flex-1 sm:flex-none
|
||||
${timer.is_active
|
||||
? 'bg-red-500/10 text-red-500 border-red-500 hover:bg-red-500 hover:text-white shadow-[0_0_15px_rgba(239,68,68,0.2)]'
|
||||
: 'bg-slate-800 text-slate-300 border-slate-600 hover:bg-slate-700'}`}
|
||||
>
|
||||
{timer.is_active ? 'NONAKTIFKAN' : 'AKTIFKAN'}
|
||||
</button>
|
||||
|
||||
<button
|
||||
onClick={saveTimer}
|
||||
disabled={isSavingTimer}
|
||||
className="px-6 py-2.5 text-sm font-bold bg-blue-600 hover:bg-blue-700 text-white rounded-lg transition-all flex-1 sm:flex-none shadow-lg shadow-blue-900/30 disabled:opacity-50 disabled:cursor-not-allowed flex justify-center"
|
||||
>
|
||||
{isSavingTimer ? 'Menyimpan...' : 'Simpan Jadwal'}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{/* 🌟 AKHIR PANEL NIGHT MODE */}
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-6">
|
||||
{[...rooms].sort((a, b) => a.name.localeCompare(b.name)).map((room) => {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user