.
This commit is contained in:
parent
122115b5e9
commit
50795d806e
@ -379,61 +379,53 @@ func GetPowerStatus(c *gin.Context) {
|
|||||||
// FUNGSI 4: GLOBAL POWER CONTROL
|
// FUNGSI 4: GLOBAL POWER CONTROL
|
||||||
// =========================================================================
|
// =========================================================================
|
||||||
func GlobalPowerControl(c *gin.Context) {
|
func GlobalPowerControl(c *gin.Context) {
|
||||||
var req GlobalPowerRequest
|
var req GlobalPowerRequest
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Format data tidak valid"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Format data tidak valid"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
haURL := os.Getenv("HA_URL")
|
haURL := os.Getenv("HA_URL")
|
||||||
haToken := os.Getenv("HA_TOKEN")
|
haToken := os.Getenv("HA_TOKEN")
|
||||||
|
|
||||||
service := "turn_off"
|
service := "turn_off"
|
||||||
if req.Action == "on" {
|
if req.Action == "on" {
|
||||||
service = "turn_on"
|
service = "turn_on"
|
||||||
}
|
}
|
||||||
apiURL := fmt.Sprintf("%s/api/services/switch/%s", haURL, service)
|
apiURL := fmt.Sprintf("%s/api/services/switch/%s", haURL, service)
|
||||||
|
|
||||||
mcbs := []string{
|
// 🌟 PERBAIKAN: Hapus array dan loop, cukup gunakan 1 target saja
|
||||||
"switch.wifi_smart_meter_pro_switch", // Umum
|
targetSwitch := "switch.wifi_smart_meter_pro_switch"
|
||||||
"switch.wifi_smart_meter_pro_2_switch", // AC1
|
|
||||||
"switch.wifi_smart_meter_pro_3_switch", // AC2
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, mcb := range mcbs {
|
payload := map[string]string{"entity_id": targetSwitch}
|
||||||
payload := map[string]string{"entity_id": mcb}
|
jsonPayload, _ := json.Marshal(payload)
|
||||||
jsonPayload, _ := json.Marshal(payload)
|
|
||||||
|
|
||||||
reqHA, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonPayload))
|
reqHA, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonPayload))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
reqHA.Header.Set("Authorization", "Bearer "+haToken)
|
reqHA.Header.Set("Authorization", "Bearer "+haToken)
|
||||||
reqHA.Header.Set("Content-Type", "application/json")
|
reqHA.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
client := &http.Client{Timeout: 5 * time.Second}
|
client := &http.Client{Timeout: 5 * time.Second}
|
||||||
resp, err := client.Do(reqHA)
|
resp, err := client.Do(reqHA)
|
||||||
if err == nil && resp != nil {
|
if err == nil && resp != nil {
|
||||||
resp.Body.Close()
|
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 {
|
// Mematikan status virtual di dashboard
|
||||||
time.Sleep(1 * time.Second)
|
if req.Action == "off" {
|
||||||
}
|
DeviceStatusCache["lampu1"] = "off"
|
||||||
}
|
DeviceStatusCache["lampu2"] = "off"
|
||||||
|
DeviceStatusCache["ac"] = "off"
|
||||||
|
DeviceStatusCache["projector"] = "off"
|
||||||
|
}
|
||||||
|
|
||||||
if req.Action == "off" {
|
c.JSON(http.StatusOK, gin.H{
|
||||||
DeviceStatusCache["lampu1"] = "off"
|
"status": "success",
|
||||||
DeviceStatusCache["lampu2"] = "off"
|
"message": fmt.Sprintf("Daya utama berhasil di-%s", req.Action),
|
||||||
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),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// =========================================================================
|
// =========================================================================
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user