diff options
| author | Ben Burwell <ben@benburwell.com> | 2018-05-25 00:31:01 -0400 | 
|---|---|---|
| committer | Ben Burwell <ben@benburwell.com> | 2018-05-25 00:31:01 -0400 | 
| commit | 93649031c575e54b59407873f29d6fa12aa93d53 (patch) | |
| tree | 0cb0ab3317fe607a183cf2122787d201401b81c9 | |
| parent | 23114b25bf60ff9cf135fe794235c0cf1f7c6b18 (diff) | |
Add support for forever operation
| -rw-r--r-- | config.go | 1 | ||||
| -rw-r--r-- | main.go | 35 | 
2 files changed, 30 insertions, 6 deletions
@@ -20,6 +20,7 @@ type BridgeConfig struct {  type PhluxConfig struct {  	Latitude  float64        `yaml:"latitude"`  	Longitude float64        `yaml:"longitude"` +	Interval  int64          `yaml:"interval"`  	Bridges   []BridgeConfig `yaml:"bridges"`  } @@ -2,6 +2,7 @@ package main  import (  	"errors" +	"flag"  	"fmt"  	"log"  	"time" @@ -14,8 +15,26 @@ const USERNAME = "phlux"  func main() {  	var config PhluxConfig  	config.Read() + +	flag.Float64Var(&config.Latitude, "latitude", config.Latitude, "Latitude (used to calculate sunrise and sunset)") +	flag.Float64Var(&config.Longitude, "longitude", config.Longitude, "Longitude (used to calculate sunrise and sunset)") +	flag.Int64Var(&config.Interval, "interval", config.Interval, "Interval (in seconds) at which to run the update if --forever is set") +	forever := flag.Bool("forever", false, "Run the update every [interval], use Ctrl-C to cancel") +	flag.Parse() +  	log.Println("Config:", config) +	updateColorTemps(&config) +	if *forever { +		ticker := time.NewTicker(time.Duration(config.Interval) * time.Second) +		for { +			<-ticker.C +			updateColorTemps(&config) +		} +	} +} + +func updateColorTemps(config *PhluxConfig) {  	bridges, err := hue.FindBridges()  	if err != nil {  		log.Fatalf("Error finding bridges: %s\n", err.Error()) @@ -24,7 +43,7 @@ func main() {  	desiredColorTemp := getDesiredColorTemperature(time.Now(), config.Latitude, config.Longitude)  	for _, bridge := range bridges {  		log.Printf("Bridge: %s\n", bridge.IPAddress) -		updateBridge(&bridge, desiredColorTemp, &config) +		updateBridge(&bridge, desiredColorTemp, config)  	}  } @@ -71,22 +90,26 @@ func authenticate(bridge *hue.Bridge, config *PhluxConfig) error {  	}  	err = bridge.Login(token)  	if err != nil { -		log.Fatalf("Could not log in to bridge: %s\n", err.Error()) +		return errors.New(fmt.Sprintf("Could not log in to bridge: %s", err.Error()))  	} -	log.Println("Logged in to bridge")  	return nil  } -func updateBridge(bridge *hue.Bridge, ct ColorTemperature, config *PhluxConfig) { -	authenticate(bridge, config) +func updateBridge(bridge *hue.Bridge, ct ColorTemperature, config *PhluxConfig) error { +	err := authenticate(bridge, config) +	if err != nil { +		return err +	} +	log.Println("Logged in to bridge")  	lights, err := bridge.GetAllLights()  	if err != nil { -		log.Fatalf("Error getting lights: %s\n", err.Error()) +		return errors.New(fmt.Sprintf("Error getting lights: %s", err.Error()))  	}  	log.Printf("Found %d lights\n", len(lights))  	for _, light := range lights {  		updateLight(light, ct)  	} +	return nil  }  func updateLight(light hue.Light, ct ColorTemperature) {  | 
