Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Antonio Villanueva Segura Ctrl. Shelly on Go
- https://shelly-api-docs.shelly.cloud/gen1/#settings-cloud
- curl -X GET http://192.168.1.90/relay/0?turn=on
- curl -X GET http://192.168.1.90/relay/0?turn=off
- */
- package main
- import (
- "encoding/json"
- "fmt"
- "io/ioutil"
- "net/http"
- )
- // Structure JSON dans la réponse relais uniquement
- // {"ison":false,"has_timer":false,"timer_started":0,"timer_duration":0,"timer_remaining":0,"source":"http"}
- type jsonRelayStruct struct {
- Ison bool `json:"ison"` //"ison":false
- Has_timer bool `json:"has_timer"` //"has_timer":false
- Timer_started int `json:"timer_started"` //"timer_started":0
- Timer_duration int `json:"timer_duration"` //"timer_duration":0
- Timer_remaining int `json:"timer_remaining"` //"timer_remaining":0
- Source string `json:"source"` //"source":"http"
- }
- func main() {
- shellyAddress := "192.168.1.90" //shelly PORT
- shellyPort := "80" //shelly PORT
- shellyRoot := "http://%s:%s/relay/0?turn=" //shelly relay root address
- shellyState := "off" //State on or off
- //endpoint := fmt.Sprintf("http://%s:%s/relay/0?turn=on", shellyAddress, shellyPort)
- endpoint := fmt.Sprintf(shellyRoot+shellyState, shellyAddress, shellyPort)
- _, err := http.Get(endpoint) //Send GET
- if err != nil {
- fmt.Println("Erreur on GET:", err)
- return
- }
- fmt.Println("shellyState", shellyState)
- endpoint = fmt.Sprintf("http://%s:%s/relay/0", shellyAddress, shellyPort) //Relay
- //endpoint = fmt.Sprintf("http://%s:%s/status", shellyAddress, shellyPort) //Lire tout !!
- // requête HTTP GET
- resp, err := http.Get(endpoint)
- if err != nil {
- fmt.Println("Erreur on GET:", err)
- return
- }
- defer resp.Body.Close()
- // Lecture response http
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- fmt.Println("Erreur lors de la lecture de la réponse HTTP:", err)
- return
- }
- // réponse de l'appareil Shelly tout (body est de type []byte)
- fmt.Println(" DEBUG ALL : ", string(body))
- //Analyse sous forme de données JSON
- jsonData := body
- var data jsonRelayStruct
- if err := json.Unmarshal(jsonData, &data); err != nil {
- fmt.Println("Erreur de décodage JSON:", err)
- return
- }
- fmt.Println("Ison:", data.Ison)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement