From ba0fbd4f9c361030868b557206e04607d5a6c2ee Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Wed, 14 Sep 2022 17:57:13 +0200 Subject: [PATCH] Simplified things even more, now all the logic is done on the hue bridge and we just update a virtual sensor --- main.go | 105 +++++++------------------------------------------------- 1 file changed, 12 insertions(+), 93 deletions(-) diff --git a/main.go b/main.go index 3fc563b..82e4775 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,6 @@ package main import ( "bytes" - "crypto/tls" "fmt" "net/http" "os" @@ -10,67 +9,11 @@ import ( "strconv" "strings" "syscall" - "time" MQTT "github.com/eclipse/paho.mqtt.golang" "github.com/joho/godotenv" - "github.com/kelvins/sunrisesunset" ) -// Get the time of the next sunrise and sunset -func getNextSunriseSunset() (time.Time, time.Time) { - p := sunrisesunset.Parameters{ - Latitude: 51.9808334, - Longitude: 4.347818, - Date: time.Now(), - } - - sunrise, sunset, err := p.GetSunriseSunset() - if err != nil { - panic(err) - } - sunset = sunset.Add(-time.Minute*30) - - p2 := sunrisesunset.Parameters{ - Latitude: 51.9808334, - Longitude: 4.347818, - Date: time.Now().Add(time.Hour * 24), - } - sunrise2, sunset2, err := p2.GetSunriseSunset() - if err != nil { - panic(err) - } - sunset2 = sunset2.Add(-time.Minute*30) - - now := time.Now() - if now.After(sunrise) { - sunrise = sunrise2 - } - - if now.After(sunset) { - sunset = sunset2 - } - - return sunrise, sunset -} - -// Check if it is currently daytime -func isDay() bool { - p := sunrisesunset.Parameters{ - Latitude: 51.9808334, - Longitude: 4.347818, - Date: time.Now(), - } - - sunrise, sunset, err := p.GetSunriseSunset() - if err != nil { - panic(err) - } - sunset = sunset.Add(-time.Minute*30) - - return time.Now().After(sunrise) && time.Now().Before(sunset) -} - // This is the default message handler, it just prints out the topic and message var defaultHandler MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) { fmt.Printf("TOPIC: %s\n", msg.Topic()) @@ -102,19 +45,22 @@ type Hue struct { login string } -func (hue *Hue) putRequest(resource string, data string) { - url := fmt.Sprintf("https://%s/clip/v2/resource/%s", hue.ip, resource) +func (hue *Hue) updateFlag(id int, value bool) { + url := fmt.Sprintf("http://%s/api/%s/sensors/%d/state", hue.ip, hue.login, id) + + var data []byte + if value { + data = []byte(`{ "flag": true }`) + } else { + data = []byte(`{ "flag": false }`) + } client := &http.Client{} - http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} - - req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer([]byte(data))) + req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(data)) if err != nil { panic(err) } - req.Header.Set("hue-application-key", hue.login) - _, err = client.Do(req) if err != nil { panic(err) @@ -169,10 +115,6 @@ func main() { os.Exit(1) } - sunrise, sunset := getNextSunriseSunset() - sunriseTimer := time.NewTimer(sunrise.Sub(time.Now())) - sunsetTimer := time.NewTimer(sunset.Sub(time.Now())) - devices := make(map[string]bool) isHome := false @@ -204,37 +146,14 @@ events: if isHome { fmt.Println("Coming home") - if !isDay() { - fmt.Println("\tTurning on lights in the living room") - hue.putRequest("scene/1847ec79-3459-4d79-ae73-803a0c6e7ac2", `{"recall": { "action": "active", "status": "active"}}`) - } + hue.updateFlag(41, true) } else { fmt.Println("Leaving home") - hue.putRequest("grouped_light/91c400ed-7eda-4b5c-ac3f-bfff226188d7", `{"on": { "on": false}}`) - break + hue.updateFlag(41, false) } fmt.Println("Done") - case <-sunriseTimer.C: - fmt.Println("Sun is rising, turning off all lights") - hue.putRequest("grouped_light/91c400ed-7eda-4b5c-ac3f-bfff226188d7", `{"on": { "on": false}}`) - - // Set new timer - sunrise, _ := getNextSunriseSunset() - sunriseTimer.Reset(sunrise.Sub(time.Now())) - - case <-sunsetTimer.C: - fmt.Println("Sun is setting") - if isHome { - fmt.Println("\tGradually turning on lights in the living room") - hue.putRequest("scene/1847ec79-3459-4d79-ae73-803a0c6e7ac2", `{"recall": { "action": "active", "status": "active", "duration": 300000}}`) - } - - // Set new timer - _, sunset := getNextSunriseSunset() - sunsetTimer.Reset(sunset.Sub(time.Now())) - case <-halt: break events }