.
This commit is contained in:
parent
122115b5e9
commit
50795d806e
@ -379,61 +379,53 @@ func GetPowerStatus(c *gin.Context) {
|
||||
// FUNGSI 4: GLOBAL POWER CONTROL
|
||||
// =========================================================================
|
||||
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
|
||||
}
|
||||
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")
|
||||
haURL := os.Getenv("HA_URL")
|
||||
haToken := os.Getenv("HA_TOKEN")
|
||||
|
||||
service := "turn_off"
|
||||
if req.Action == "on" {
|
||||
service = "turn_on"
|
||||
}
|
||||
apiURL := fmt.Sprintf("%s/api/services/switch/%s", haURL, service)
|
||||
service := "turn_off"
|
||||
if req.Action == "on" {
|
||||
service = "turn_on"
|
||||
}
|
||||
apiURL := fmt.Sprintf("%s/api/services/switch/%s", haURL, service)
|
||||
|
||||
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
|
||||
}
|
||||
// 🌟 PERBAIKAN: Hapus array dan loop, cukup gunakan 1 target saja
|
||||
targetSwitch := "switch.wifi_smart_meter_pro_switch"
|
||||
|
||||
for i, mcb := range mcbs {
|
||||
payload := map[string]string{"entity_id": mcb}
|
||||
jsonPayload, _ := json.Marshal(payload)
|
||||
payload := map[string]string{"entity_id": targetSwitch}
|
||||
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")
|
||||
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()
|
||||
}
|
||||
}
|
||||
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)
|
||||
fmt.Printf("[MCB] Sinyal %s dikirim ke %s\n", req.Action, targetSwitch)
|
||||
|
||||
if i < len(mcbs)-1 {
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}
|
||||
// Mematikan status virtual di dashboard
|
||||
if req.Action == "off" {
|
||||
DeviceStatusCache["lampu1"] = "off"
|
||||
DeviceStatusCache["lampu2"] = "off"
|
||||
DeviceStatusCache["ac"] = "off"
|
||||
DeviceStatusCache["projector"] = "off"
|
||||
}
|
||||
|
||||
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": fmt.Sprintf("Seluruh daya ruangan berhasil di-%s secara berurutan", req.Action),
|
||||
})
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": "success",
|
||||
"message": fmt.Sprintf("Daya utama berhasil di-%s", req.Action),
|
||||
})
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user