--- /dev/null
+// basic stuff
+package main
+
+import (
+ "encoding/json"
+ //"fmt"
+ //"io"
+ "log"
+ "math"
+ //"net/http"
+ "os"
+ //"strconv"
+ "time"
+
+ //"github.com/leekchan/timeutil"
+ "unpiege.net/rabbit_go.git/helper"
+)
+
+// TimeFloat allows user to specify times in a more meaningful way
+type TimeFloat struct {
+ Hour int
+ Minute int
+ Second int
+}
+
+// TimeColor is one instance of RGB values changing over time
+type TimeColor struct{
+ PeakTime TimeFloat
+ Red float64
+ Green float64
+ Blue float64
+ Extent TimeFloat
+}
+
+type RGB struct{
+ Red float64
+ Green float64
+ Blue float64
+}
+
+// make sure no value is greater than 1
+func (rgb *RGB) Clamp() {
+ rgb.Red = math.Min(1, rgb.Red)
+ rgb.Green = math.Min(1, rgb.Green)
+ rgb.Blue = math.Min(1, rgb.Blue)
+}
+
+func (rgb *RGB) Mult(mult float64) {
+ rgb.Red = rgb.Red * mult
+ rgb.Green = rgb.Green * mult
+ rgb.Blue = rgb.Blue * mult
+}
+
+
+func (rgb *RGB) Add(newrgb RGB) (RGB){
+ retval := RGB{
+ Red: rgb.Red+newrgb.Red,
+ Green: rgb.Green+newrgb.Green,
+ Blue: rgb.Blue+newrgb.Blue,
+ }
+
+ retval.Clamp()
+ return retval
+}
+
+func (tf *TimeFloat) Float() float64 {
+ return float64(tf.Hour*3600 + tf.Minute*60 + tf.Second)/86400.0
+}
+
+// hack to get the same values from an actual time, since we can't define another method
+func RealTimeFloat(t time.Time) float64 {
+ return float64(t.Hour()*3600 + t.Minute()*60 + t.Second())/86400.0
+}
+
+
+
+func NewTimeColor(peak TimeFloat, red float64, green float64, blue float64, extent TimeFloat) (TimeColor) {
+ var retval TimeColor
+ retval.PeakTime = peak
+ retval.Red = red
+ retval.Green = green
+ retval.Blue = blue
+ retval.Extent = extent
+ return retval
+}
+
+func (tc *TimeColor) RGBValues(evaluationTime time.Time) (RGB){
+ retval := RGB{tc.Red, tc.Green, tc.Blue}
+ var multiplier float64
+ // get the distance between the peak time, and where we are right now
+ curFloat := RealTimeFloat(evaluationTime)
+ tcFloat := tc.PeakTime.Float()
+ // this accounts for all the wraparound issues when the date ticks over
+ distances := []float64 {
+ math.Abs(tcFloat - curFloat),
+ math.Abs(tcFloat + 1 - curFloat),
+ math.Abs(curFloat + 1 - tcFloat),
+ }
+ minDistance := distances[0]
+ for _, val := range distances[1:] {
+ if val < minDistance {
+ minDistance = val
+ }
+ }
+ //log.Printf("minDistance: %f", minDistance)
+
+ if minDistance > tc.Extent.Float()/2 {
+ //log.Printf("turned off")
+ // multiplier is already initialized zero so do nothing
+ } else {
+ extentMultiplier := 1/tc.Extent.Float()
+ multiplier = math.Cos(math.Pi * extentMultiplier * minDistance )
+ }
+ //log.Printf("multiplier is %f", multiplier)
+ retval.Mult(multiplier)
+
+ return retval
+}
+
+func failOnError(err error, msg string) {
+ if err != nil {
+ log.Panicf("%s: %s", msg, err)
+ }
+}
+
+func sum(arr []float64) float64 {
+ sum := 0.0
+ for _, value := range arr {
+ sum += value
+ }
+ return sum
+}
+
+func timeLoop(channel chan []byte, timeColors []TimeColor) {
+ loc, _ := time.LoadLocation("America/Los_Angeles")
+ for {
+ //log.Printf("okay just looping here")
+ curTime := time.Now().UTC().In(loc)
+ //log.Printf("here's the current time %s", curTime)
+ totalRGB := RGB{}
+ for _, tc := range timeColors {
+ //log.Printf("Evaluating RGB for %+v", tc)
+ rgb := tc.RGBValues(curTime)
+ //log.Printf("%+v", rgb)
+ totalRGB = totalRGB.Add(rgb)
+ }
+ totalRGB.Mult(0.877)
+ //log.Printf("TotalRGB: %+v", totalRGB)
+
+ sendObj := make(map[string]interface{})
+ sendObj["red"] = totalRGB.Red
+ sendObj["green"] = totalRGB.Green
+ sendObj["blue"] = totalRGB.Blue
+
+ data, _ := json.Marshal(sendObj)
+
+ channel <- data
+ time.Sleep(time.Duration(0.5*float64(time.Second)))
+
+ }
+}
+
+func sendLoop(channel chan []byte, rabbit helper.RabbitConfig){
+ for sendData := range channel {
+ //log.Printf("would try to send here %s", sendData)
+ helper.SendJSON(sendData, rabbit, false)
+ }
+}
+
+func main() {
+ rabbit := helper.SetupRabbit(os.Args[1], "timecolorshift.py") // config file, routing key
+ const sleepTime time.Duration = 30 * time.Second
+
+ //currentTemp, err := fetchTemp(weatherStation)
+ channel := make(chan []byte)
+ all_tcs := make([]TimeColor, 0)
+ all_tcs = append(all_tcs, NewTimeColor(TimeFloat{Hour: 12}, 0, 1, 0.5, TimeFloat{Hour:12}))
+ all_tcs = append(all_tcs, NewTimeColor(TimeFloat{Hour: 2}, 1, 0.1, 0, TimeFloat{Hour:8}))
+ all_tcs = append(all_tcs, NewTimeColor(TimeFloat{Hour: 20}, 0.25, 0.25, 0, TimeFloat{Hour:12}))
+
+
+ go timeLoop(channel, all_tcs)
+ go sendLoop(channel, rabbit)
+ for true {
+ time.Sleep(sleepTime)
+ }
+
+}