362 lines
11 KiB
Go
362 lines
11 KiB
Go
package controllers
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strconv" // Digunakan untuk mengubah teks Watt menjadi angka
|
|
"time"
|
|
|
|
"s-class-backend/config" // Import 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 untuk Web Admin)
|
|
// =========================================================================
|
|
var DeviceStatusCache = map[string]string{
|
|
"lampu1": "off",
|
|
"lampu2": "off",
|
|
"ac": "off",
|
|
"projector": "off",
|
|
}
|
|
|
|
// =========================================================================
|
|
// FUNGSI 1: VERIFIKASI TOKEN (Bisa Master & Mahasiswa)
|
|
// =========================================================================
|
|
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()
|
|
sisaMenit := 0
|
|
isTokenValid := false
|
|
|
|
// =========================================================
|
|
// 1. PENGECEKAN KELOMPOK TOKEN MASTER (Tanpa Cek Waktu)
|
|
// =========================================================
|
|
if tokenInput == "CS2026" {
|
|
fmt.Printf("[VERIFY] Master Token CS digunakan\n")
|
|
sisaMenit = 60
|
|
isTokenValid = true
|
|
} else if tokenInput == "ADM999" {
|
|
fmt.Printf("[VERIFY] Master Token Admin digunakan\n")
|
|
sisaMenit = 999
|
|
isTokenValid = true
|
|
} else {
|
|
// =========================================================
|
|
// 2. PENGECEKAN KELOMPOK TOKEN MAHASISWA (Cek Database & Waktu)
|
|
// =========================================================
|
|
var jamMulai time.Time
|
|
var jamSelesai time.Time
|
|
type ResultTime struct {
|
|
JamMulai time.Time `gorm:"column:start_time"`
|
|
JamSelesai time.Time `gorm:"column:end_time"`
|
|
}
|
|
var result ResultTime
|
|
|
|
// Cek Bookings
|
|
errBooking := config.DB.Table("bookings").
|
|
Select("start_time, end_time").
|
|
Where("redeem_code = ?", tokenInput).
|
|
Scan(&result).Error
|
|
|
|
if errBooking == nil && !result.JamSelesai.IsZero() {
|
|
jamMulai = result.JamMulai
|
|
jamSelesai = result.JamSelesai
|
|
isTokenValid = true
|
|
} else {
|
|
// Cek Jadwal Kelas Tetap
|
|
errSchedule := config.DB.Table("class_schedules").
|
|
Select("start_time, end_time").
|
|
Where("kode_mk = ?", tokenInput).
|
|
Scan(&result).Error
|
|
|
|
if errSchedule == nil && !result.JamSelesai.IsZero() {
|
|
jamMulai = result.JamMulai
|
|
jamSelesai = result.JamSelesai
|
|
isTokenValid = true
|
|
}
|
|
}
|
|
|
|
if !isTokenValid {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Token salah atau tidak ditemukan"})
|
|
return
|
|
}
|
|
|
|
jamMulaiHariIni := time.Date(sekarang.Year(), sekarang.Month(), sekarang.Day(),
|
|
jamMulai.Hour(), jamMulai.Minute(), jamMulai.Second(), 0, sekarang.Location())
|
|
jamSelesaiHariIni := time.Date(sekarang.Year(), sekarang.Month(), sekarang.Day(),
|
|
jamSelesai.Hour(), jamSelesai.Minute(), jamSelesai.Second(), 0, sekarang.Location())
|
|
|
|
batasMasukAwal := jamMulaiHariIni.Add(-15 * time.Minute)
|
|
if sekarang.Before(batasMasukAwal) {
|
|
c.JSON(http.StatusForbidden, gin.H{"error": "Jadwal kelas belum dimulai"})
|
|
return
|
|
}
|
|
|
|
selisihWaktu := jamSelesaiHariIni.Sub(sekarang)
|
|
sisaMenit = int(selisihWaktu.Minutes())
|
|
|
|
if sisaMenit <= 0 {
|
|
c.JSON(http.StatusForbidden, gin.H{"error": "Waktu peminjaman sudah habis"})
|
|
return
|
|
}
|
|
}
|
|
|
|
// =========================================================
|
|
// 3. JIKA VALID (Master / Mahasiswa), BUKA GEMBOK VIA MQTT
|
|
// =========================================================
|
|
DeviceStatusCache["lampu1"] = "on"
|
|
DeviceStatusCache["lampu2"] = "on"
|
|
|
|
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-Auth-%d", time.Now().Unix()))
|
|
|
|
client := mqtt.NewClient(opts)
|
|
if tokenMQTT := client.Connect(); tokenMQTT.Wait() && tokenMQTT.Error() == nil {
|
|
// Buka Gembok Relay
|
|
client.Publish("sclass/d101/auth", 0, true, "UNLOCK").Wait()
|
|
|
|
// Opsional: Nyalakan Lampu Otomatis saat login
|
|
client.Publish("sclass/d101/lampu1", 0, false, "on").Wait()
|
|
client.Publish("sclass/d101/lampu2", 0, false, "on").Wait()
|
|
|
|
client.Disconnect(250)
|
|
fmt.Printf("[MQTT] Perintah UNLOCK dan ON dikirim (Sisa Menit: %d)\n", sisaMenit)
|
|
} else {
|
|
fmt.Printf("[MQTT ERROR] Gagal menghubungi broker: %v\n", tokenMQTT.Error())
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// 🌟 BLOK 1: KONTROL GEMBOK / OTORISASI RELAY
|
|
if req.Device == "auth" {
|
|
broker := os.Getenv("MQTT_BROKER")
|
|
user := os.Getenv("MQTT_USER")
|
|
pass := os.Getenv("MQTT_PASSWORD")
|
|
|
|
opts := mqtt.NewClientOptions().AddBroker(broker).SetUsername(user).SetPassword(pass).SetClientID(fmt.Sprintf("Golang-Lock-%d", time.Now().Unix()))
|
|
client := mqtt.NewClient(opts)
|
|
if token := client.Connect(); token.Wait() && token.Error() == nil {
|
|
client.Publish("sclass/d101/auth", 0, true, req.Action).Wait()
|
|
client.Disconnect(250)
|
|
fmt.Println("[MQTT] Status Gembok Relay diubah menjadi:", req.Action)
|
|
} else {
|
|
fmt.Println("[MQTT ERROR] Gagal terhubung saat mencoba mengunci relay.")
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"status": "success", "message": "Gembok " + req.Action})
|
|
return
|
|
}
|
|
|
|
// --- BLOK 2: KONTROL LAMPU ---
|
|
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 %s ke %s", req.Action, req.Device)})
|
|
return
|
|
}
|
|
|
|
// --- BLOK 3: KONTROL AC & PROYEKTOR ---
|
|
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 {
|
|
reqHA.Header.Set("Authorization", "Bearer "+haToken)
|
|
reqHA.Header.Set("Content-Type", "application/json")
|
|
httpClient := &http.Client{Timeout: 5 * time.Second}
|
|
resp, _ := httpClient.Do(reqHA)
|
|
if resp != nil {
|
|
resp.Body.Close()
|
|
}
|
|
}
|
|
|
|
DeviceStatusCache[req.Device] = req.Action
|
|
c.JSON(http.StatusOK, gin.H{"status": "success", "message": "Scene dipicu: " + entityID})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Device tidak dikenali"})
|
|
}
|
|
|
|
// =========================================================================
|
|
// HELPER: MENGAMBIL STATE DARI HOME ASSISTANT
|
|
// =========================================================================
|
|
func fetchHAState(haURL string, haToken string, entityID string) float64 {
|
|
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{Timeout: 3 * time.Second}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var result map[string]interface{}
|
|
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
|
return 0
|
|
}
|
|
|
|
stateStr, ok := result["state"].(string)
|
|
if !ok {
|
|
return 0
|
|
}
|
|
|
|
// Ubah string angka menjadi float
|
|
val, _ := strconv.ParseFloat(stateStr, 64)
|
|
return val
|
|
}
|
|
|
|
// =========================================================================
|
|
// FUNGSI 3: MENDAPATKAN STATUS DAYA DARI 3 MCB SEKALIGUS
|
|
// =========================================================================
|
|
func GetPowerStatus(c *gin.Context) {
|
|
haURL := os.Getenv("HA_URL")
|
|
haToken := os.Getenv("HA_TOKEN")
|
|
|
|
powerUmum := fetchHAState(haURL, haToken, "sensor.wifi_smart_meter_pro_power")
|
|
powerAC1 := fetchHAState(haURL, haToken, "sensor.wifi_smart_meter_pro_2_power")
|
|
powerAC2 := fetchHAState(haURL, haToken, "sensor.wifi_smart_meter_pro_3_power")
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"umum": powerUmum,
|
|
"ac1": powerAC1,
|
|
"ac2": powerAC2,
|
|
})
|
|
}
|
|
|
|
// =========================================================================
|
|
// FUNGSI 4: CUT OFF SEMUA POWER (MATIKAN 3 MCB)
|
|
// =========================================================================
|
|
func CutOffAllPower(c *gin.Context) {
|
|
haURL := os.Getenv("HA_URL")
|
|
haToken := os.Getenv("HA_TOKEN")
|
|
|
|
apiURL := fmt.Sprintf("%s/api/services/switch/turn_off", haURL)
|
|
|
|
payload := map[string]interface{}{
|
|
"entity_id": []string{
|
|
"switch.wifi_smart_meter_pro_switch",
|
|
"switch.wifi_smart_meter_pro_2_switch",
|
|
"switch.wifi_smart_meter_pro_3_switch",
|
|
},
|
|
}
|
|
|
|
jsonPayload, _ := json.Marshal(payload)
|
|
req, _ := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonPayload))
|
|
req.Header.Set("Authorization", "Bearer "+haToken)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
client := &http.Client{Timeout: 5 * time.Second}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Gagal menghubungi Home Assistant"})
|
|
return
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// Update cache
|
|
DeviceStatusCache["lampu1"] = "off"
|
|
DeviceStatusCache["lampu2"] = "off"
|
|
DeviceStatusCache["ac"] = "off"
|
|
DeviceStatusCache["projector"] = "off"
|
|
|
|
c.JSON(http.StatusOK, gin.H{"status": "success", "message": "Semua daya ruangan (MCB) berhasil diputus"})
|
|
}
|
|
|
|
// =========================================================================
|
|
// FUNGSI 5: MENGIRIM STATUS REAL-TIME KE WEB FRONTEND
|
|
// =========================================================================
|
|
func GetHardwareStatus(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"status": "success",
|
|
"data": DeviceStatusCache,
|
|
})
|
|
} |