Simplified things even more, now all the logic is done on the hue bridge and we just update a virtual sensor
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
3dbac64e0a
commit
ba0fbd4f9c
105
main.go
105
main.go
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/tls"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
@ -10,67 +9,11 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
|
||||||
|
|
||||||
MQTT "github.com/eclipse/paho.mqtt.golang"
|
MQTT "github.com/eclipse/paho.mqtt.golang"
|
||||||
"github.com/joho/godotenv"
|
"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
|
// 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) {
|
var defaultHandler MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
|
||||||
fmt.Printf("TOPIC: %s\n", msg.Topic())
|
fmt.Printf("TOPIC: %s\n", msg.Topic())
|
||||||
|
@ -102,19 +45,22 @@ type Hue struct {
|
||||||
login string
|
login string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hue *Hue) putRequest(resource string, data string) {
|
func (hue *Hue) updateFlag(id int, value bool) {
|
||||||
url := fmt.Sprintf("https://%s/clip/v2/resource/%s", hue.ip, resource)
|
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{}
|
client := &http.Client{}
|
||||||
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(data))
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer([]byte(data)))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("hue-application-key", hue.login)
|
|
||||||
|
|
||||||
_, err = client.Do(req)
|
_, err = client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -169,10 +115,6 @@ func main() {
|
||||||
os.Exit(1)
|
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)
|
devices := make(map[string]bool)
|
||||||
isHome := false
|
isHome := false
|
||||||
|
|
||||||
|
@ -204,37 +146,14 @@ events:
|
||||||
|
|
||||||
if isHome {
|
if isHome {
|
||||||
fmt.Println("Coming home")
|
fmt.Println("Coming home")
|
||||||
if !isDay() {
|
hue.updateFlag(41, true)
|
||||||
fmt.Println("\tTurning on lights in the living room")
|
|
||||||
hue.putRequest("scene/1847ec79-3459-4d79-ae73-803a0c6e7ac2", `{"recall": { "action": "active", "status": "active"}}`)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("Leaving home")
|
fmt.Println("Leaving home")
|
||||||
hue.putRequest("grouped_light/91c400ed-7eda-4b5c-ac3f-bfff226188d7", `{"on": { "on": false}}`)
|
hue.updateFlag(41, false)
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Done")
|
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:
|
case <-halt:
|
||||||
break events
|
break events
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue
Block a user