diff options
author | Ben Burwell <ben@benburwell.com> | 2018-05-24 23:19:26 -0400 |
---|---|---|
committer | Ben Burwell <ben@benburwell.com> | 2018-05-24 23:19:26 -0400 |
commit | 65b8fb514db9fc5125494b3393699bf666ed9cb2 (patch) | |
tree | 1403327124796b215ebdb1982af135943732e6cd | |
parent | a3786c8698f8b7c85d09e241cf09dc0e8ced9da2 (diff) |
Get lat/long from config file
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | colortemp.go | 9 | ||||
-rw-r--r-- | config.go | 39 | ||||
-rw-r--r-- | main.go | 16 |
4 files changed, 57 insertions, 9 deletions
@@ -1,2 +1,2 @@ -phlux: main.go colortemp.go +phlux: main.go colortemp.go config.go go build . diff --git a/colortemp.go b/colortemp.go index 8cf3454..f7209a1 100644 --- a/colortemp.go +++ b/colortemp.go @@ -11,18 +11,15 @@ import ( type ColorTemperature uint -const LATITUDE = 42.348333 -const LONGITUDE = 71.1675 - // Get the correct color temperature for a given time. If the time is between // sunrise and sunset, a high CT is selected. Otherise, during night, a low // color temperature is used. // // TODO: This is a naive approach and should be revisited -func getDesiredColorTemperature(t time.Time) ColorTemperature { +func getDesiredColorTemperature(t time.Time, latitude, longitude float64) ColorTemperature { log.Printf("Calculating sunrise/sunset for: %s\n", t.Format(time.RFC3339)) - sunrise := astrotime.CalcSunrise(t, LATITUDE, LONGITUDE) - sunset := astrotime.CalcSunset(t, LATITUDE, LONGITUDE) + sunrise := astrotime.CalcSunrise(t, latitude, longitude) + sunset := astrotime.CalcSunset(t, latitude, longitude) log.Printf("Sunrise: %s, Sunset: %s\n", sunrise.Format(time.RFC3339), sunset.Format(time.RFC3339)) if t.After(sunrise) && t.Before(sunset) { log.Println("Daytime, setting high CT") diff --git a/config.go b/config.go new file mode 100644 index 0000000..a12a240 --- /dev/null +++ b/config.go @@ -0,0 +1,39 @@ +package main + +import ( + "io/ioutil" + "log" + + "github.com/BurntSushi/xdg" + "gopkg.in/yaml.v2" +) + +const XDG_CONFIG_NAME = "phlux" + +type PhluxConfig struct { + Latitude float64 `yaml:"latitude"` + Longitude float64 `yaml:"longitude"` + Bridges []struct { + BridgeID string `yaml:"id"` + Token string `yaml:"token"` + } `yaml:"bridges"` +} + +func (c *PhluxConfig) Read() { + var paths xdg.Paths + configFile, err := paths.ConfigFile(XDG_CONFIG_NAME) + if err != nil { + log.Printf("No config file found: %s\n", err.Error()) + return + } + yamlFile, err := ioutil.ReadFile(configFile) + if err != nil { + log.Printf("Error reading config file %s: %s\n", configFile, err.Error()) + return + } + err = yaml.Unmarshal(yamlFile, c) + if err != nil { + log.Printf("Error unmarshalling yaml: %s\n", err.Error()) + return + } +} @@ -11,12 +11,16 @@ import ( const username = "phlux" func main() { + var config PhluxConfig + config.Read() + log.Println("Config:", config) + bridges, err := hue.FindBridges() if err != nil { log.Fatalf("Error finding bridges: %s\n", err.Error()) } log.Printf("Found %d bridge(s)\n", len(bridges)) - desiredColorTemp := getDesiredColorTemperature(time.Now()) + desiredColorTemp := getDesiredColorTemperature(time.Now(), config.Latitude, config.Longitude) for _, bridge := range bridges { log.Printf("Bridge: %s\n", bridge.IPAddress) updateBridge(bridge, desiredColorTemp) @@ -31,9 +35,17 @@ func updateBridge(bridge hue.Bridge, ct ColorTemperature) { //fmt.Printf("Made user %s for bridge %s\n", username, bridge.IPAddress) err := bridge.Login(os.Getenv("HUE_LOGIN")) if err != nil { - log.Fatalf("Could not log in to bridge: %s", err.Error()) + log.Fatalf("Could not log in to bridge: %s\n", err.Error()) } log.Println("Logged in to bridge") + + config, err := bridge.GetConfig() + if err != nil { + log.Fatalf("Could not get bridge info: %s\n", err.Error()) + } + log.Printf("Bridge name: %s\n", config.Name) + log.Printf("Bridge ID: %s\n", config.BridgeID) + lights, err := bridge.GetAllLights() if err != nil { log.Fatalf("Error getting lights: %s\n", err.Error()) |