Advertisement
AntonioVillanueva

Test Shellyctrl.in go

Sep 22nd, 2023
732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.32 KB | None | 0 0
  1. /*
  2. Antonio Villanueva Segura Ctrl. Shelly on Go
  3. https://shelly-api-docs.shelly.cloud/gen1/#settings-cloud
  4. curl -X GET http://192.168.1.90/relay/0?turn=on
  5. curl -X GET http://192.168.1.90/relay/0?turn=off
  6. */
  7. package main
  8.  
  9. import (
  10.     "encoding/json"
  11.     "fmt"
  12.     "io/ioutil"
  13.     "net/http"
  14. )
  15.  
  16. // Structure JSON dans la réponse relais uniquement
  17. // {"ison":false,"has_timer":false,"timer_started":0,"timer_duration":0,"timer_remaining":0,"source":"http"}
  18. type jsonRelayStruct struct {
  19.     Ison            bool   `json:"ison"`            //"ison":false
  20.     Has_timer       bool   `json:"has_timer"`       //"has_timer":false
  21.     Timer_started   int    `json:"timer_started"`   //"timer_started":0
  22.     Timer_duration  int    `json:"timer_duration"`  //"timer_duration":0
  23.     Timer_remaining int    `json:"timer_remaining"` //"timer_remaining":0
  24.     Source          string `json:"source"`          //"source":"http"
  25. }
  26.  
  27. func main() {
  28.  
  29.     shellyAddress := "192.168.1.90"            //shelly PORT
  30.     shellyPort := "80"                         //shelly PORT
  31.     shellyRoot := "http://%s:%s/relay/0?turn=" //shelly relay root address
  32.     shellyState := "off"                       //State on or off
  33.  
  34.     //endpoint := fmt.Sprintf("http://%s:%s/relay/0?turn=on", shellyAddress, shellyPort)
  35.     endpoint := fmt.Sprintf(shellyRoot+shellyState, shellyAddress, shellyPort)
  36.  
  37.     _, err := http.Get(endpoint) //Send GET
  38.     if err != nil {
  39.         fmt.Println("Erreur on GET:", err)
  40.         return
  41.     }
  42.  
  43.     fmt.Println("shellyState", shellyState)
  44.  
  45.     endpoint = fmt.Sprintf("http://%s:%s/relay/0", shellyAddress, shellyPort) //Relay
  46.     //endpoint = fmt.Sprintf("http://%s:%s/status", shellyAddress, shellyPort) //Lire tout !!
  47.  
  48.     // requête HTTP GET
  49.     resp, err := http.Get(endpoint)
  50.     if err != nil {
  51.         fmt.Println("Erreur on GET:", err)
  52.         return
  53.     }
  54.     defer resp.Body.Close()
  55.  
  56.     // Lecture response http
  57.     body, err := ioutil.ReadAll(resp.Body)
  58.     if err != nil {
  59.         fmt.Println("Erreur lors de la lecture de la réponse HTTP:", err)
  60.         return
  61.     }
  62.  
  63.     // réponse de l'appareil Shelly tout  (body est de type []byte)
  64.     fmt.Println(" DEBUG ALL : ", string(body))
  65.  
  66.     //Analyse sous forme de données JSON
  67.     jsonData := body
  68.  
  69.     var data jsonRelayStruct
  70.  
  71.     if err := json.Unmarshal(jsonData, &data); err != nil {
  72.         fmt.Println("Erreur de décodage JSON:", err)
  73.         return
  74.     }
  75.  
  76.     fmt.Println("Ison:", data.Ison)
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement