From 4d24adb77964ff76bd0403205e766039db041960 Mon Sep 17 00:00:00 2001 From: "[Valentino Heman Budiarto]" <[hemanvalentino@gmail.com]> Date: Fri, 12 Jun 2026 10:44:48 +0700 Subject: [PATCH] benerin bookingcontroller.go --- backend/controllers/hardwarecontroller.go | 34 +++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/backend/controllers/hardwarecontroller.go b/backend/controllers/hardwarecontroller.go index ab81294..54d9d58 100644 --- a/backend/controllers/hardwarecontroller.go +++ b/backend/controllers/hardwarecontroller.go @@ -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, }) -} +} \ No newline at end of file