benerin bookingcontroller.go

This commit is contained in:
[Valentino Heman Budiarto] 2026-06-12 10:44:48 +07:00
parent d9522437ec
commit 4d24adb779

View File

@ -34,6 +34,9 @@ var DeviceStatusCache = map[string]string{
"projector": "off",
}
// =========================================================================
// FUNGSI 1: VERIFIKASI TOKEN (Dan Buka Gembok Relay)
// =========================================================================
func VerifyHardwareCode(c *gin.Context) {
var req VerifyRequest
@ -135,11 +138,38 @@ func VerifyHardwareCode(c *gin.Context) {
}
// -------------------------------------------------------------
// JIKA LOLOS SEMUA UJIAN: IZINKAN MASUK
// JIKA LOLOS SEMUA UJIAN: IZINKAN MASUK & BUKA GEMBOK
// -------------------------------------------------------------
DeviceStatusCache["lampu1"] = "on"
DeviceStatusCache["lampu2"] = "on"
// --- START BLOK MQTT UNLOCK ---
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 {
// 1. Mengirim sinyal otorisasi "UNLOCK" ke Relay dengan retain=true
client.Publish("sclass/d101/auth", 0, true, "UNLOCK").Wait()
// 2. Opsional: otomatis nyalakan lampu saat token dimasukkan
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 berhasil dikirim ke Relay D101\n")
} else {
fmt.Printf("[MQTT ERROR] Gagal menghubungi broker: %v\n", tokenMQTT.Error())
}
// --- END BLOK MQTT UNLOCK ---
c.JSON(http.StatusOK, gin.H{
"status": "success",
"message": "Token Valid",
@ -289,4 +319,4 @@ func GetHardwareStatus(c *gin.Context) {
"status": "success",
"data": DeviceStatusCache,
})
}
}