[Valentino Heman Budiarto] 2bedb35863 .
2026-06-05 20:13:35 +07:00

244 lines
6.9 KiB
Go

package controllers
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"time"
"s-class-backend/config" // IMPORT WAJIB UNTUK KONEKSI DATABASE
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/gin-gonic/gin"
)
// --- STRUKTUR DATA REQUEST ---
type DeviceControlRequest struct {
Device string `json:"device"`
Action string `json:"action"`
}
type VerifyRequest struct {
Token string `json:"token"`
}
// --- CACHE STATUS HARDWARE (Mengingat status terakhir perangkat) ---
var DeviceStatusCache = map[string]string{
"lampu1": "off",
"lampu2": "off",
"ac": "off",
"projector": "off",
}
// =========================================================================
// FUNGSI 1: VERIFIKASI HARDWARE & HITUNG SISA WAKTU DINAMIS
// =========================================================================
func VerifyHardwareCode(c *gin.Context) {
var req VerifyRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Format token tidak valid"})
return
}
tokenInput := req.Token
sekarang := time.Now()
var jamSelesai time.Time
isTokenValid := false
// Struktur penampung jam selesai dari database
type ResultTime struct {
JamSelesai time.Time `gorm:"column:end_time"` // PASTIKAN NAMA KOLOM 'end_time' SESUAI DENGAN YANG ADA DI DATABASE
}
var result ResultTime
// 1. Cek di tabel bookings (Redeem Code)
errBooking := config.DB.Table("bookings").
Select("end_time").
Where("redeem_code = ?", tokenInput).
Scan(&result).Error
if errBooking == nil && !result.JamSelesai.IsZero() {
jamSelesai = result.JamSelesai
isTokenValid = true
fmt.Println("[VERIFY] Token ditemukan di tabel bookings!")
} else {
// 2. Cek di tabel class_schedules (Kode MK)
errSchedule := config.DB.Table("class_schedules").
Select("end_time").
Where("kode_mk = ?", tokenInput).
Scan(&result).Error
if errSchedule == nil && !result.JamSelesai.IsZero() {
jamSelesai = result.JamSelesai
isTokenValid = true
fmt.Println("[VERIFY] Token ditemukan di tabel class_schedules!")
}
}
// Jika token tidak ditemukan di kedua tabel (Token Acak/Salah)
if !isTokenValid {
fmt.Printf("[VERIFY] Token %s SALAH atau tidak ditemukan.\n", tokenInput)
c.JSON(http.StatusUnauthorized, gin.H{"error": "Token salah atau tidak ditemukan"})
return
}
// 3. Kalkulasi Selisih Waktu
// Gabungkan tanggal hari ini dengan jam dari database
jamSelesaiHariIni := time.Date(sekarang.Year(), sekarang.Month(), sekarang.Day(),
jamSelesai.Hour(), jamSelesai.Minute(), jamSelesai.Second(), 0, sekarang.Location())
selisihWaktu := jamSelesaiHariIni.Sub(sekarang)
sisaMenit := int(selisihWaktu.Minutes())
// 4. Proteksi Jika Waktu Sudah Habis (Lebih dari jam selesai)
if sisaMenit <= 0 {
c.JSON(http.StatusForbidden, gin.H{"error": "Waktu peminjaman sudah habis"})
return
}
// 5. Kirim balasan sukses ke ESP32 beserta durasinya
c.JSON(http.StatusOK, gin.H{
"status": "success",
"message": "Token Valid",
"duration_minutes": sisaMenit,
})
}
// =========================================================================
// FUNGSI 2: KONTROL DEVICE VIA HOME ASSISTANT & MQTT
// =========================================================================
func ControlHardware(c *gin.Context) {
var req DeviceControlRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Format data tidak valid"})
return
}
if req.Device == "lampu1" || req.Device == "lampu2" {
broker := os.Getenv("MQTT_BROKER")
user := os.Getenv("MQTT_USER")
pass := os.Getenv("MQTT_PASSWORD")
opts := mqtt.NewClientOptions()
opts.AddBroker(broker)
opts.SetUsername(user)
opts.SetPassword(pass)
opts.SetClientID(fmt.Sprintf("Golang-SCLASS-%d", time.Now().Unix()))
client := mqtt.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Golang gagal terhubung ke MQTT Broker"})
return
}
defer client.Disconnect(250)
topic := fmt.Sprintf("sclass/d101/%s", req.Device)
token := client.Publish(topic, 0, false, req.Action)
token.Wait()
DeviceStatusCache[req.Device] = req.Action
c.JSON(http.StatusOK, gin.H{
"status": "success",
"message": fmt.Sprintf("Berhasil mengirim perintah %s ke %s via MQTT", req.Action, req.Device),
})
return
}
if req.Device == "ac" || req.Device == "projector" {
haURL := os.Getenv("HA_URL")
haToken := os.Getenv("HA_TOKEN")
var entityID string
switch req.Device {
case "ac":
if req.Action == "on" {
entityID = "scene.ac_d101_on"
} else {
entityID = "scene.ac_d101_off"
}
case "projector":
if req.Action == "on" {
entityID = "scene.projector_d101_on"
} else {
entityID = "scene.projector_d101_off"
}
}
apiURL := fmt.Sprintf("%s/api/services/scene/turn_on", haURL)
payload := map[string]string{"entity_id": entityID}
jsonPayload, _ := json.Marshal(payload)
reqHA, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonPayload))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Gagal membuat request ke HA"})
return
}
reqHA.Header.Set("Authorization", "Bearer "+haToken)
reqHA.Header.Set("Content-Type", "application/json")
httpClient := &http.Client{Timeout: 5 * time.Second}
resp, err := httpClient.Do(reqHA)
if err != nil {
c.JSON(http.StatusGatewayTimeout, gin.H{"error": "Gagal menghubungi Home Assistant"})
return
}
defer resp.Body.Close()
c.JSON(http.StatusOK, gin.H{
"status": "success",
"message": fmt.Sprintf("Berhasil memicu scene %s", entityID),
})
return
}
c.JSON(http.StatusBadRequest, gin.H{"error": "Device tidak dikenali sistem"})
}
// =========================================================================
// FUNGSI 3: MENDAPATKAN STATUS DAYA DARI HOME ASSISTANT
// =========================================================================
func GetPowerStatus(c *gin.Context) {
haURL := os.Getenv("HA_URL")
haToken := os.Getenv("HA_TOKEN")
entityID := "sensor.kwh_meter_power"
apiURL := fmt.Sprintf("%s/api/states/%s", haURL, entityID)
req, _ := http.NewRequest("GET", apiURL, nil)
req.Header.Set("Authorization", "Bearer "+haToken)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"power": 0})
return
}
defer resp.Body.Close()
var result map[string]interface{}
json.NewDecoder(resp.Body).Decode(&result)
powerStr, ok := result["state"].(string)
if !ok {
c.JSON(http.StatusOK, gin.H{"power": 0})
return
}
c.JSON(http.StatusOK, gin.H{"power": powerStr})
}
// =========================================================================
// FUNGSI 4: MENGAMBIL STATUS PERANGKAT SECARA REAL-TIME
// =========================================================================
func GetHardwareStatus(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"status": "success",
"data": DeviceStatusCache,
})
}