Compare commits

..

5 Commits

Author SHA1 Message Date
Cyrille Nofficial
419a1ddd72 refactor(cli): rewrite log configuration 2022-06-16 12:00:23 +02:00
Cyrille Nofficial
8312e7a3bd fix: publish patched payload 2022-06-16 09:10:32 +02:00
Cyrille Nofficial
41f99b7f57 build: use cache to build go binary 2022-06-15 17:13:34 +02:00
Cyrille Nofficial
8740f5eaf9 build: exclude binary from git history 2022-06-15 16:54:16 +02:00
Cyrille Nofficial
72e93141d9 feat: implement max throttle limiter on user drive
close robocars/robocar-setup#7
2022-06-14 15:55:33 +02:00
4 changed files with 55 additions and 37 deletions

View File

@ -26,11 +26,12 @@ image_build(){
printf "\n\nBuild go binary %s\n\n" "${BINARY}.${binary_suffix}" printf "\n\nBuild go binary %s\n\n" "${BINARY}.${binary_suffix}"
CGO_ENABLED=0 GOOS=${GOOS} GOARCH=${GOARCH} GOARM=${GOARM} go build -mod vendor -a ${GOTAGS} -o "${BINARY}.${binary_suffix}" ./cmd/${BINARY}/ mkdir -p build
CGO_ENABLED=0 GOOS=${GOOS} GOARCH=${GOARCH} GOARM=${GOARM} go build -mod vendor ${GOTAGS} -o "build/${BINARY}.${binary_suffix}" ./cmd/${BINARY}/
buildah --os "$GOOS" --arch "$GOARCH" $VARIANT --name "$containerName" from gcr.io/distroless/static buildah --os "$GOOS" --arch "$GOARCH" $VARIANT --name "$containerName" from gcr.io/distroless/static
buildah config --user 1234 "$containerName" buildah config --user 1234 "$containerName"
buildah copy "$containerName" "${BINARY}.${binary_suffix}" /go/bin/$BINARY buildah copy "$containerName" "build/${BINARY}.${binary_suffix}" /go/bin/$BINARY
buildah config --entrypoint '["/go/bin/'$BINARY'"]' "${containerName}" buildah config --entrypoint '["/go/bin/'$BINARY'"]' "${containerName}"
buildah commit --rm --manifest $IMAGE_NAME "${containerName}" "${containerName}" buildah commit --rm --manifest $IMAGE_NAME "${containerName}" "${containerName}"

View File

@ -18,7 +18,6 @@ func main() {
var mqttBroker, username, password, clientId string var mqttBroker, username, password, clientId string
var throttleTopic, driveModeTopic, rcThrottleTopic string var throttleTopic, driveModeTopic, rcThrottleTopic string
var minThrottle, maxThrottle float64 var minThrottle, maxThrottle float64
var debug bool
err := cli.SetFloat64DefaultValueFromEnv(&minThrottle, "THROTTLE_MIN", DefaultThrottleMin) err := cli.SetFloat64DefaultValueFromEnv(&minThrottle, "THROTTLE_MIN", DefaultThrottleMin)
if err != nil { if err != nil {
@ -39,7 +38,7 @@ func main() {
flag.StringVar(&rcThrottleTopic, "mqtt-topic-rc-throttle", os.Getenv("MQTT_TOPIC_RC_THROTTLE"), "Mqtt topic that contains RC Throttle value, use MQTT_TOPIC_RC_THROTTLE if args not set") flag.StringVar(&rcThrottleTopic, "mqtt-topic-rc-throttle", os.Getenv("MQTT_TOPIC_RC_THROTTLE"), "Mqtt topic that contains RC Throttle value, use MQTT_TOPIC_RC_THROTTLE if args not set")
flag.Float64Var(&minThrottle, "throttle-min", minThrottle, "Minimum throttle value, use THROTTLE_MIN if args not set") flag.Float64Var(&minThrottle, "throttle-min", minThrottle, "Minimum throttle value, use THROTTLE_MIN if args not set")
flag.Float64Var(&maxThrottle, "throttle-max", maxThrottle, "Minimum throttle value, use THROTTLE_MAX if args not set") flag.Float64Var(&maxThrottle, "throttle-max", maxThrottle, "Minimum throttle value, use THROTTLE_MAX if args not set")
flag.BoolVar(&debug, "debug", false, "Display raw value to debug") logLevel := zap.LevelFlag("log", zap.InfoLevel, "log level")
flag.Parse() flag.Parse()
if len(os.Args) <= 1 { if len(os.Args) <= 1 {
@ -48,11 +47,7 @@ func main() {
} }
config := zap.NewDevelopmentConfig() config := zap.NewDevelopmentConfig()
if debug { config.Level = zap.NewAtomicLevelAt(*logLevel)
config.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
} else {
config.Level = zap.NewAtomicLevelAt(zap.InfoLevel)
}
lgr, err := config.Build() lgr, err := config.Build()
if err != nil { if err != nil {
log.Fatalf("unable to init logger: %v", err) log.Fatalf("unable to init logger: %v", err)

View File

@ -98,9 +98,26 @@ func (p *ThrottlePart) onRCThrottle(_ mqtt.Client, message mqtt.Message) {
p.muDriveMode.RLock() p.muDriveMode.RLock()
defer p.muDriveMode.RUnlock() defer p.muDriveMode.RUnlock()
if p.driveMode == events.DriveMode_USER { if p.driveMode == events.DriveMode_USER {
zap.S().Debug("publish new throttle value from rc")
// Republish same content // Republish same content
payload := message.Payload() payload := message.Payload()
var throttleMsg events.ThrottleMessage
err := proto.Unmarshal(payload, &throttleMsg)
if err != nil {
zap.S().Errorf("unable to unmarshall throttle msg to check throttle value: %v", err)
return
}
zap.S().Debugf("publish new throttle value from rc: %v", throttleMsg.GetThrottle())
if throttleMsg.GetThrottle() > p.maxThrottle {
zap.S().Debugf("throttle upper that max value allowed, patch value from %v to %v", throttleMsg.GetThrottle(), p.maxThrottle)
throttleMsg.Throttle = p.maxThrottle
payloadPatched, err := proto.Marshal(&throttleMsg)
if err != nil {
zap.S().Errorf("unable to marshall throttle msg: %v", err)
return
}
publish(p.client, p.throttleTopic, &payloadPatched)
return
}
publish(p.client, p.throttleTopic, &payload) publish(p.client, p.throttleTopic, &payload)
} }
} }

View File

@ -38,23 +38,27 @@ func TestDefaultThrottle(t *testing.T) {
p := NewPart(nil, throttleTopic, driveModeTopic, rcThrottleTopic, minValue, 1., 200) p := NewPart(nil, throttleTopic, driveModeTopic, rcThrottleTopic, minValue, 1., 200)
cases := []struct { cases := []struct {
name string
maxThrottle float32
driveMode events.DriveModeMessage driveMode events.DriveModeMessage
rcThrottle events.ThrottleMessage rcThrottle events.ThrottleMessage
expectedThrottle events.ThrottleMessage expectedThrottle events.ThrottleMessage
}{ }{
{events.DriveModeMessage{DriveMode: events.DriveMode_USER}, events.ThrottleMessage{Throttle: 0.3, Confidence: 1.0}, events.ThrottleMessage{Throttle: 0.3, Confidence: 1.0}}, {"test1", 1., events.DriveModeMessage{DriveMode: events.DriveMode_USER}, events.ThrottleMessage{Throttle: 0.3, Confidence: 1.0}, events.ThrottleMessage{Throttle: 0.3, Confidence: 1.0}},
{events.DriveModeMessage{DriveMode: events.DriveMode_PILOT}, events.ThrottleMessage{Throttle: 0.5, Confidence: 1.0}, events.ThrottleMessage{Throttle: minValue, Confidence: 1.0}}, {"test2", 1., events.DriveModeMessage{DriveMode: events.DriveMode_PILOT}, events.ThrottleMessage{Throttle: 0.5, Confidence: 1.0}, events.ThrottleMessage{Throttle: minValue, Confidence: 1.0}},
{events.DriveModeMessage{DriveMode: events.DriveMode_PILOT}, events.ThrottleMessage{Throttle: 0.4, Confidence: 1.0}, events.ThrottleMessage{Throttle: minValue, Confidence: 1.0}}, {"test3", 1., events.DriveModeMessage{DriveMode: events.DriveMode_PILOT}, events.ThrottleMessage{Throttle: 0.4, Confidence: 1.0}, events.ThrottleMessage{Throttle: minValue, Confidence: 1.0}},
{events.DriveModeMessage{DriveMode: events.DriveMode_USER}, events.ThrottleMessage{Throttle: 0.5, Confidence: 1.0}, events.ThrottleMessage{Throttle: 0.5, Confidence: 1.0}}, {"test4", 1., events.DriveModeMessage{DriveMode: events.DriveMode_USER}, events.ThrottleMessage{Throttle: 0.5, Confidence: 1.0}, events.ThrottleMessage{Throttle: 0.5, Confidence: 1.0}},
{events.DriveModeMessage{DriveMode: events.DriveMode_USER}, events.ThrottleMessage{Throttle: 0.4, Confidence: 1.0}, events.ThrottleMessage{Throttle: 0.4, Confidence: 1.0}}, {"test5", 1., events.DriveModeMessage{DriveMode: events.DriveMode_USER}, events.ThrottleMessage{Throttle: 0.4, Confidence: 1.0}, events.ThrottleMessage{Throttle: 0.4, Confidence: 1.0}},
{events.DriveModeMessage{DriveMode: events.DriveMode_USER}, events.ThrottleMessage{Throttle: 0.6, Confidence: 1.0}, events.ThrottleMessage{Throttle: 0.6, Confidence: 1.0}}, {"test6", 1., events.DriveModeMessage{DriveMode: events.DriveMode_USER}, events.ThrottleMessage{Throttle: 0.6, Confidence: 1.0}, events.ThrottleMessage{Throttle: 0.6, Confidence: 1.0}},
{"limit max throttle on user mode", 0.4, events.DriveModeMessage{DriveMode: events.DriveMode_USER}, events.ThrottleMessage{Throttle: 0.6, Confidence: 1.0}, events.ThrottleMessage{Throttle: 0.4, Confidence: 1.0}},
} }
go p.Start() go p.Start()
defer func() { close(p.cancel) }() defer func() { close(p.cancel) }()
for _, c := range cases { for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
p.maxThrottle = c.maxThrottle
p.onDriveMode(nil, testtools.NewFakeMessageFromProtobuf(driveModeTopic, &c.driveMode)) p.onDriveMode(nil, testtools.NewFakeMessageFromProtobuf(driveModeTopic, &c.driveMode))
p.onRCThrottle(nil, testtools.NewFakeMessageFromProtobuf(rcThrottleTopic, &c.rcThrottle)) p.onRCThrottle(nil, testtools.NewFakeMessageFromProtobuf(rcThrottleTopic, &c.rcThrottle))
@ -80,5 +84,6 @@ func TestDefaultThrottle(t *testing.T) {
time.Sleep(1 * time.Millisecond) time.Sleep(1 * time.Millisecond)
} }
})
} }
} }