update timer auto cut off mcb

This commit is contained in:
[Valentino Heman Budiarto]
2026-06-22 23:45:01 +07:00
parent e210d2a424
commit 122115b5e9
3 changed files with 174 additions and 9 deletions
+100 -8
View File
@@ -1,7 +1,7 @@
"use client";
import { useState, useEffect } from "react";
import { Activity, Power, ZapOff, Zap, AlertTriangle, Lightbulb, Wind, Projector } from "lucide-react";
import { Activity, Power, ZapOff, Zap, AlertTriangle, Lightbulb, Wind, Projector, Clock } from "lucide-react";
export default function PowerMonitoringPage() {
// =========================================================================
@@ -13,6 +13,10 @@ 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
// =========================================================================
@@ -66,6 +70,17 @@ 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)
// =========================================================================
@@ -91,6 +106,8 @@ export default function PowerMonitoringPage() {
});
setRoomDeviceStatus(initialStatus);
// Panggil fungsi awal
fetchTimer();
fetchPowerStatus();
fetchDeviceStatus();
@@ -103,8 +120,26 @@ export default function PowerMonitoringPage() {
}, []);
// =========================================================================
// 4. FUNGSI KONTROL DEVICE & CUT OFF
// 4. FUNGSI KONTROL DEVICE, GLOBAL POWER & SIMPAN TIMER
// =========================================================================
// 🌟 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;
@@ -115,13 +150,11 @@ export default function PowerMonitoringPage() {
if (!window.confirm(confirmMsg)) return;
}
// Jika yang ditekan bukan D101, ubah UI saja (simulasi)
if (roomName !== "Kelas D101") {
setRoomDeviceStatus(prev => ({ ...prev, [roomIdKey]: { ...prev[roomIdKey], [deviceName]: !currentStatus } }));
return;
}
// Logika khusus D101 (Tembak ke Golang)
let backendDevice = "";
if (deviceName === 'AC') backendDevice = "ac";
else if (deviceName === 'Proyektor') backendDevice = "projector";
@@ -147,7 +180,6 @@ export default function PowerMonitoringPage() {
}
};
// FUNGSI GLOBAL POWER (ON/OFF KHUSUS D101)
const handleGlobalPower = async (roomName: string, roomId: number, currentRelayStatus: boolean) => {
if (roomName === "Kelas D101") {
const actionType = currentRelayStatus ? "off" : "on";
@@ -157,7 +189,6 @@ export default function PowerMonitoringPage() {
if (!window.confirm(confirmMessage)) return;
// Ubah UI seketika
setRooms(prev => prev.map(r => r.name === roomName ? { ...r, isRelayOn: !currentRelayStatus } : r));
try {
@@ -172,7 +203,6 @@ export default function PowerMonitoringPage() {
fetchPowerStatus();
fetchDeviceStatus();
} else {
// Rollback UI jika gagal
setRooms(prev => prev.map(r => r.name === roomName ? { ...r, isRelayOn: currentRelayStatus } : r));
alert("Gagal mengontrol MCB utama. Cek koneksi server.");
}
@@ -181,7 +211,6 @@ export default function PowerMonitoringPage() {
alert("Terjadi kesalahan jaringan saat menghubungi server.");
}
} else {
// Dummy untuk kelas lain
const actionType = currentRelayStatus ? "Mematikan" : "Menyalakan";
if (window.confirm(`Simulasi ${actionType} daya di ${roomName}?`)) {
setRooms(prev => prev.map(r => r.name === roomName ? { ...r, isRelayOn: !currentRelayStatus, power: 0 } : r));
@@ -204,6 +233,69 @@ 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) => {