From: jweigele Date: Tue, 6 Dec 2022 20:17:26 +0000 (-0800) Subject: Added separate builds, and implemented new timecolorshift functionality X-Git-Url: http://git.hexthepla.net/?a=commitdiff_plain;h=e51190c366e609f98564f7599698e4b02c0fbc2c;p=rabbit_go Added separate builds, and implemented new timecolorshift functionality * Dockerfile.whatever is called to build for a particular binary, buildall.sh does all the ones we know about - Still need to call dockregassemble at the end, don't forget! * Most of the functionality in timecolorshift is ported over from the python version * This does mean that the colors are hardcoded in the binary, maybe look at grabbing them from kubernetes directly in the future * Have only really gone through one peak at midday, so unsure how the color mixing and rollover func works --- diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index f800b91..0000000 --- a/Dockerfile +++ /dev/null @@ -1,22 +0,0 @@ -# STEP 1 build executable binary -FROM golang:alpine as builder -# Install SSL ca certificates -RUN apk update && apk add git && apk add ca-certificates -# Create appuser -RUN adduser -D -g '' appuser -COPY . $GOPATH/src/mypackage/myapp/ -WORKDIR $GOPATH/src/mypackage/myapp/ -COPY ./go.mod ./ -#COPY ./go.sum ./ -RUN go get github.com/rabbitmq/amqp091-go -RUN go mod download -WORKDIR $GOPATH/src/mypackage/myapp/wunder - -#get dependancies -#RUN go get -d -v -#build the binary -RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags="-w -s" -o /go/bin/wunder - -FROM alpine:edge -COPY --from=builder /go/bin/wunder /bin/wunder -ENTRYPOINT ["/bin/wunder"] diff --git a/Dockerfile.timecolorshift b/Dockerfile.timecolorshift new file mode 100644 index 0000000..91ac406 --- /dev/null +++ b/Dockerfile.timecolorshift @@ -0,0 +1,22 @@ +# STEP 1 build executable binary +FROM golang:alpine as builder +# Install SSL ca certificates +RUN apk update && apk add git && apk add ca-certificates +# Create appuser +RUN adduser -D -g '' appuser +COPY . $GOPATH/src/mypackage/myapp/ +WORKDIR $GOPATH/src/mypackage/myapp/ +COPY ./go.mod ./ +#COPY ./go.sum ./ +RUN go mod download +WORKDIR $GOPATH/src/mypackage/myapp/timecolorshift + +#get dependancies +#RUN go get -d -v +#build the binary +RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags="-w -s" -o /go/bin/timecolorshift + +FROM alpine:edge +COPY --from=builder /go/bin/timecolorshift /bin/timecolorshift +RUN apk add tzdata +ENTRYPOINT ["/bin/timecolorshift"] diff --git a/Dockerfile.wunder b/Dockerfile.wunder new file mode 100644 index 0000000..70baabe --- /dev/null +++ b/Dockerfile.wunder @@ -0,0 +1,21 @@ +# STEP 1 build executable binary +FROM golang:alpine as builder +# Install SSL ca certificates +RUN apk update && apk add git && apk add ca-certificates +# Create appuser +RUN adduser -D -g '' appuser +COPY . $GOPATH/src/mypackage/myapp/ +WORKDIR $GOPATH/src/mypackage/myapp/ +COPY ./go.mod ./ +#COPY ./go.sum ./ +RUN go mod download +WORKDIR $GOPATH/src/mypackage/myapp/wunder + +#get dependancies +#RUN go get -d -v +#build the binary +RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags="-w -s" -o /go/bin/wunder + +FROM alpine:edge +COPY --from=builder /go/bin/wunder /bin/wunder +ENTRYPOINT ["/bin/wunder"] diff --git a/buildall.sh b/buildall.sh new file mode 100755 index 0000000..8785235 --- /dev/null +++ b/buildall.sh @@ -0,0 +1,4 @@ +#!/bin/bash +for TAG in wunder timecolorshift; do + buildprivate rabbit_go:${TAG} Dockerfile.${TAG} +done diff --git a/go.mod b/go.mod index 3c911bc..71e3d9a 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,6 @@ module unpiege.net/rabbit_go.git go 1.18 - require ( github.com/PuerkitoBio/goquery v1.8.0 github.com/leekchan/timeutil v0.0.0-20150802142658-28917288c48d diff --git a/helper/helper.go b/helper/helper.go index 7b265a6..ced2c28 100644 --- a/helper/helper.go +++ b/helper/helper.go @@ -77,7 +77,7 @@ func SetupRabbit(configFilename string, routingKey string) RabbitConfig { } -func SendJSON(data []byte, rabbit RabbitConfig) { +func SendJSON(data []byte, rabbit RabbitConfig, verboseSend bool) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() err := rabbit.Channel.PublishWithContext(ctx, @@ -90,7 +90,8 @@ func SendJSON(data []byte, rabbit RabbitConfig) { Body: data, }) failOnError(err, "Failed to publish a message") - log.Printf(" [x] Sent %s\n", data) - + if verboseSend { + log.Printf("Sent %s routing: %s\n", data, rabbit.RoutingKey) + } } diff --git a/timecolorshift/main.go b/timecolorshift/main.go new file mode 100644 index 0000000..72c2e3e --- /dev/null +++ b/timecolorshift/main.go @@ -0,0 +1,188 @@ +// 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) + } + +} diff --git a/timecolorshift/timecolorshift b/timecolorshift/timecolorshift new file mode 100755 index 0000000..ff2414c Binary files /dev/null and b/timecolorshift/timecolorshift differ diff --git a/wunder/main.go b/wunder/main.go index 8e96285..293ab4d 100644 --- a/wunder/main.go +++ b/wunder/main.go @@ -131,7 +131,7 @@ func sendTemp(tempValueF float64, tempValueC float64, rabbit helper.RabbitConfig sendObj["date"] = dateString data, err := json.Marshal(sendObj) failOnError(err, "oh no json marshal") - helper.SendJSON(data, rabbit) + helper.SendJSON(data, rabbit, true) } func main() {