.
This commit is contained in:
parent
16075c670a
commit
2bedb35863
@ -70,6 +70,8 @@ func main() {
|
|||||||
// PASTIKAN BARIS INI ADA DI SINI:
|
// PASTIKAN BARIS INI ADA DI SINI:
|
||||||
r.POST("/api/hardware/control", controllers.ControlHardware)
|
r.POST("/api/hardware/control", controllers.ControlHardware)
|
||||||
|
|
||||||
|
r.GET("/api/hardware/status", controllers.GetHardwareStatus)
|
||||||
|
|
||||||
r.GET("/api/hardware/power-status", controllers.GetPowerStatus)
|
r.GET("/api/hardware/power-status", controllers.GetPowerStatus)
|
||||||
|
|
||||||
r.Run(":8080")
|
r.Run(":8080")
|
||||||
|
|||||||
@ -24,6 +24,14 @@ type VerifyRequest struct {
|
|||||||
Token string `json:"token"`
|
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
|
// FUNGSI 1: VERIFIKASI HARDWARE & HITUNG SISA WAKTU DINAMIS
|
||||||
// =========================================================================
|
// =========================================================================
|
||||||
@ -131,6 +139,8 @@ func ControlHardware(c *gin.Context) {
|
|||||||
token := client.Publish(topic, 0, false, req.Action)
|
token := client.Publish(topic, 0, false, req.Action)
|
||||||
token.Wait()
|
token.Wait()
|
||||||
|
|
||||||
|
DeviceStatusCache[req.Device] = req.Action
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"status": "success",
|
"status": "success",
|
||||||
"message": fmt.Sprintf("Berhasil mengirim perintah %s ke %s via MQTT", req.Action, req.Device),
|
"message": fmt.Sprintf("Berhasil mengirim perintah %s ke %s via MQTT", req.Action, req.Device),
|
||||||
@ -222,3 +232,13 @@ func GetPowerStatus(c *gin.Context) {
|
|||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{"power": powerStr})
|
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,
|
||||||
|
})
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user