hardware n monitoring
This commit is contained in:
+1
-1
@@ -74,7 +74,7 @@ func main() {
|
||||
|
||||
r.GET("/api/hardware/power-status", controllers.GetPowerStatus)
|
||||
|
||||
r.POST("/api/cutoff", controllers.CutOffAllPower)
|
||||
r.POST("/api/power/global", controllers.GlobalPowerControl)
|
||||
|
||||
r.Run(":8080")
|
||||
}
|
||||
|
||||
@@ -25,6 +25,11 @@ type VerifyRequest struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
// Tambahan untuk kontrol daya global (ON/OFF)
|
||||
type GlobalPowerRequest struct {
|
||||
Action string `json:"action"` // "on" atau "off"
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// CACHE STATUS HARDWARE (Mengingat status terakhir perangkat untuk Web Admin)
|
||||
// =========================================================================
|
||||
@@ -313,42 +318,69 @@ func GetPowerStatus(c *gin.Context) {
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// FUNGSI 4: CUT OFF SEMUA POWER (MATIKAN 3 MCB)
|
||||
// FUNGSI 4: GLOBAL POWER CONTROL (Sequential ON/OFF dengan Jeda 1 Detik)
|
||||
// =========================================================================
|
||||
func CutOffAllPower(c *gin.Context) {
|
||||
func GlobalPowerControl(c *gin.Context) {
|
||||
var req GlobalPowerRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Format data tidak valid"})
|
||||
return
|
||||
}
|
||||
|
||||
haURL := os.Getenv("HA_URL")
|
||||
haToken := os.Getenv("HA_TOKEN")
|
||||
|
||||
apiURL := fmt.Sprintf("%s/api/services/switch/turn_off", haURL)
|
||||
// Tentukan service HA berdasarkan request
|
||||
service := "turn_off"
|
||||
if req.Action == "on" {
|
||||
service = "turn_on"
|
||||
}
|
||||
apiURL := fmt.Sprintf("%s/api/services/switch/%s", haURL, service)
|
||||
|
||||
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",
|
||||
},
|
||||
// Daftar MCB yang akan dieksekusi secara berurutan
|
||||
mcbs := []string{
|
||||
"switch.wifi_smart_meter_pro_switch", // Umum
|
||||
"switch.wifi_smart_meter_pro_2_switch", // AC1
|
||||
"switch.wifi_smart_meter_pro_3_switch", // AC2
|
||||
}
|
||||
|
||||
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")
|
||||
// Looping untuk eksekusi satu per satu
|
||||
for i, mcb := range mcbs {
|
||||
payload := map[string]string{"entity_id": mcb}
|
||||
jsonPayload, _ := json.Marshal(payload)
|
||||
|
||||
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
|
||||
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")
|
||||
|
||||
client := &http.Client{Timeout: 5 * time.Second}
|
||||
resp, err := client.Do(reqHA)
|
||||
if err == nil && resp != nil {
|
||||
resp.Body.Close()
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("[MCB] Sinyal %s dikirim ke %s\n", req.Action, mcb)
|
||||
|
||||
// Berikan jeda 1 detik, KECUALI untuk iterasi terakhir
|
||||
if i < len(mcbs)-1 {
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Update cache
|
||||
DeviceStatusCache["lampu1"] = "off"
|
||||
DeviceStatusCache["lampu2"] = "off"
|
||||
DeviceStatusCache["ac"] = "off"
|
||||
DeviceStatusCache["projector"] = "off"
|
||||
// Jika dimatikan paksa, update cache perangkat IoT menjadi mati semua
|
||||
if req.Action == "off" {
|
||||
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"})
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": "success",
|
||||
"message": fmt.Sprintf("Seluruh daya ruangan berhasil di-%s secara berurutan", req.Action),
|
||||
})
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
|
||||
Reference in New Issue
Block a user