.
This commit is contained in:
parent
50795d806e
commit
387b098aad
@ -14,9 +14,6 @@ func main() {
|
|||||||
// 1. Konek Database
|
// 1. Konek Database
|
||||||
config.ConnectDatabase()
|
config.ConnectDatabase()
|
||||||
|
|
||||||
// 🌟 TAMBAHAN UNTUK TIMER: Nyalakan mesin Cron Timer di background
|
|
||||||
controllers.StartPowerCronWorker()
|
|
||||||
|
|
||||||
// 2. AutoMigrate
|
// 2. AutoMigrate
|
||||||
config.DB.AutoMigrate(&models.User{}, &models.Room{}, &models.Booking{}, &models.ClassSchedule{})
|
config.DB.AutoMigrate(&models.User{}, &models.Room{}, &models.Booking{}, &models.ClassSchedule{})
|
||||||
|
|
||||||
@ -67,7 +64,7 @@ func main() {
|
|||||||
protected.PUT("/schedules/:id", controllers.UpdateSchedule)
|
protected.PUT("/schedules/:id", controllers.UpdateSchedule)
|
||||||
protected.DELETE("/schedules/:id", controllers.DeleteSchedule)
|
protected.DELETE("/schedules/:id", controllers.DeleteSchedule)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. Jalur IoT ESP32 & Kontrol Daya
|
// 5. Jalur IoT ESP32 & Kontrol Daya
|
||||||
r.POST("/api/sensor/energy", controllers.UpdateRoomPower)
|
r.POST("/api/sensor/energy", controllers.UpdateRoomPower)
|
||||||
r.POST("/api/hardware/verify", controllers.VerifyHardwareCode)
|
r.POST("/api/hardware/verify", controllers.VerifyHardwareCode)
|
||||||
@ -76,10 +73,6 @@ func main() {
|
|||||||
r.GET("/api/hardware/power-status", controllers.GetPowerStatus)
|
r.GET("/api/hardware/power-status", controllers.GetPowerStatus)
|
||||||
r.POST("/api/power/global", controllers.GlobalPowerControl)
|
r.POST("/api/power/global", controllers.GlobalPowerControl)
|
||||||
|
|
||||||
// 🌟 RUTE BARU UNTUK TIMER AUTO-CUTOFF
|
|
||||||
r.GET("/api/power/timer", controllers.GetTimerConfig)
|
|
||||||
r.POST("/api/power/timer", controllers.SetTimerConfig)
|
|
||||||
|
|
||||||
r.Run(":8080")
|
r.Run(":8080")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,4 +93,4 @@ func CORSMiddleware() gin.HandlerFunc {
|
|||||||
}
|
}
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,66 +0,0 @@
|
|||||||
package controllers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Struktur Data Timer
|
|
||||||
type AutoTimer struct {
|
|
||||||
IsActive bool `json:"is_active"`
|
|
||||||
OffTime string `json:"off_time"`
|
|
||||||
OnTime string `json:"on_time"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Disimpan di RAM sementara agar cepat (reset saat server mati)
|
|
||||||
var AppTimer = AutoTimer{IsActive: false, OffTime: "22:00", OnTime: "05:00"}
|
|
||||||
|
|
||||||
// API: Ambil Pengaturan Timer
|
|
||||||
func GetTimerConfig(c *gin.Context) {
|
|
||||||
c.JSON(http.StatusOK, AppTimer)
|
|
||||||
}
|
|
||||||
|
|
||||||
// API: Simpan Pengaturan Timer
|
|
||||||
func SetTimerConfig(c *gin.Context) {
|
|
||||||
if err := c.ShouldBindJSON(&AppTimer); err != nil {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Data tidak valid"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
c.JSON(http.StatusOK, gin.H{"message": "Jadwal Auto-Cutoff berhasil disimpan", "data": AppTimer})
|
|
||||||
}
|
|
||||||
|
|
||||||
// MESIN CRON: Berjalan di latar belakang mengecek jam setiap menit
|
|
||||||
func StartPowerCronWorker() {
|
|
||||||
go func() {
|
|
||||||
ticker := time.NewTicker(1 * time.Minute)
|
|
||||||
for range ticker.C {
|
|
||||||
if !AppTimer.IsActive {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
loc, _ := time.LoadLocation("Asia/Jakarta")
|
|
||||||
now := time.Now().In(loc).Format("15:04") // Format 24 Jam (HH:MM)
|
|
||||||
|
|
||||||
if now == AppTimer.OffTime {
|
|
||||||
fmt.Println("🕰️ [TIMER] Waktunya OFF! Memutus daya...")
|
|
||||||
triggerGlobalPower("off")
|
|
||||||
} else if now == AppTimer.OnTime {
|
|
||||||
fmt.Println("🕰️ [TIMER] Waktunya ON! Menyalakan daya...")
|
|
||||||
triggerGlobalPower("on")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper untuk menembak API Global Power yang sudah kamu miliki
|
|
||||||
func triggerGlobalPower(action string) {
|
|
||||||
payload := map[string]string{"action": action}
|
|
||||||
jsonPayload, _ := json.Marshal(payload)
|
|
||||||
// Memanggil API lokal kita sendiri
|
|
||||||
http.Post("http://127.0.0.1:8080/api/power/global", "application/json", bytes.NewBuffer(jsonPayload))
|
|
||||||
}
|
|
||||||
@ -1,7 +1,7 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useState, useEffect } from "react";
|
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() {
|
export default function PowerMonitoringPage() {
|
||||||
// =========================================================================
|
// =========================================================================
|
||||||
@ -13,10 +13,6 @@ export default function PowerMonitoringPage() {
|
|||||||
// State khusus untuk menampung rincian 3 MCB di D101
|
// State khusus untuk menampung rincian 3 MCB di D101
|
||||||
const [powerDataD101, setPowerDataD101] = useState({ umum: 0, ac1: 0, ac2: 0 });
|
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
|
// 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)
|
// 3. INISIALISASI & POLLING (AUTO-REFRESH)
|
||||||
// =========================================================================
|
// =========================================================================
|
||||||
@ -106,8 +91,6 @@ export default function PowerMonitoringPage() {
|
|||||||
});
|
});
|
||||||
setRoomDeviceStatus(initialStatus);
|
setRoomDeviceStatus(initialStatus);
|
||||||
|
|
||||||
// Panggil fungsi awal
|
|
||||||
fetchTimer();
|
|
||||||
fetchPowerStatus();
|
fetchPowerStatus();
|
||||||
fetchDeviceStatus();
|
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 handleDeviceToggle = async (roomId: number, roomName: string, deviceName: string) => {
|
||||||
const roomIdKey = `room_${roomId}`;
|
const roomIdKey = `room_${roomId}`;
|
||||||
const currentStatus = roomDeviceStatus[roomIdKey]?.[deviceName] || false;
|
const currentStatus = roomDeviceStatus[roomIdKey]?.[deviceName] || false;
|
||||||
@ -184,8 +149,8 @@ export default function PowerMonitoringPage() {
|
|||||||
if (roomName === "Kelas D101") {
|
if (roomName === "Kelas D101") {
|
||||||
const actionType = currentRelayStatus ? "off" : "on";
|
const actionType = currentRelayStatus ? "off" : "on";
|
||||||
const confirmMessage = currentRelayStatus
|
const confirmMessage = currentRelayStatus
|
||||||
? `PERINGATAN FATAL: Anda yakin ingin MEMUTUS 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 3 MCB Daya di ${roomName}? (Proses berurutan ~3 detik)`;
|
: `RESTORE POWER: Anda yakin ingin MENYALAKAN KEMBALI MCB Daya di ${roomName}?`;
|
||||||
|
|
||||||
if (!window.confirm(confirmMessage)) return;
|
if (!window.confirm(confirmMessage)) return;
|
||||||
|
|
||||||
@ -233,69 +198,6 @@ export default function PowerMonitoringPage() {
|
|||||||
</div>
|
</div>
|
||||||
</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">
|
<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) => {
|
{[...rooms].sort((a, b) => a.name.localeCompare(b.name)).map((room) => {
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user