Refactor debug logs

This commit is contained in:
Cyrille Nofficial 2020-02-03 19:00:21 +01:00
parent 245029b759
commit 0d6b195b99
2 changed files with 37 additions and 47 deletions

View File

@ -37,11 +37,10 @@ type Part struct {
throttle float32 throttle float32
ctrlRecord bool ctrlRecord bool
driveMode events.DriveMode driveMode events.DriveMode
debug bool
cancel chan interface{} cancel chan interface{}
} }
func NewPart(client mqtt.Client, name string, baud int, throttleTopic, steeringTopic, driveModeTopic, switchRecordTopic string, pubFrequency float64, debug bool) *Part { func NewPart(client mqtt.Client, name string, baud int, throttleTopic, steeringTopic, driveModeTopic, switchRecordTopic string, pubFrequency float64) *Part {
c := &serial.Config{Name: name, Baud: baud} c := &serial.Config{Name: name, Baud: baud}
s, err := serial.OpenPort(c) s, err := serial.OpenPort(c)
if err != nil { if err != nil {
@ -52,11 +51,10 @@ func NewPart(client mqtt.Client, name string, baud int, throttleTopic, steeringT
serial: s, serial: s,
throttleTopic: throttleTopic, throttleTopic: throttleTopic,
steeringTopic: steeringTopic, steeringTopic: steeringTopic,
driveModeTopic:driveModeTopic, driveModeTopic: driveModeTopic,
switchRecordTopic:switchRecordTopic, switchRecordTopic: switchRecordTopic,
pubFrequency: pubFrequency, pubFrequency: pubFrequency,
driveMode: events.DriveMode_INVALID, driveMode: events.DriveMode_INVALID,
debug: debug,
cancel: make(chan interface{}), cancel: make(chan interface{}),
} }
} }
@ -106,9 +104,7 @@ func (a *Part) Stop() {
} }
func (a *Part) processChannel1(v string) { func (a *Part) processChannel1(v string) {
if a.debug { log.Debugf("channel1: %v", v)
log.Printf("channel1: %v", v)
}
value, err := strconv.Atoi(v) value, err := strconv.Atoi(v)
if err != nil { if err != nil {
log.Printf("invalid value for channel1, should be an int: %v", err) log.Printf("invalid value for channel1, should be an int: %v", err)
@ -122,9 +118,7 @@ func (a *Part) processChannel1(v string) {
} }
func (a *Part) processChannel2(v string) { func (a *Part) processChannel2(v string) {
if a.debug { log.Debugf("channel2: %v", v)
log.Printf("channel2: %v", v)
}
value, err := strconv.Atoi(v) value, err := strconv.Atoi(v)
if err != nil { if err != nil {
log.Printf("invalid value for channel2, should be an int: %v", err) log.Printf("invalid value for channel2, should be an int: %v", err)
@ -138,57 +132,49 @@ func (a *Part) processChannel2(v string) {
} }
func (a *Part) processChannel3(v string) { func (a *Part) processChannel3(v string) {
if a.debug { log.Debugf("channel3: %v", v)
log.Printf("channel3: %v", v)
}
} }
func (a *Part) processChannel4(v string) { func (a *Part) processChannel4(v string) {
if a.debug { log.Debugf("channel4: %v", v)
log.Printf("channel4: %v", v)
}
} }
func (a *Part) processChannel5(v string) { func (a *Part) processChannel5(v string) {
if a.debug { log.Debugf("channel5: %v", v)
log.Printf("channel5: %v", v)
}
value, err := strconv.Atoi(v) value, err := strconv.Atoi(v)
if err != nil { if err != nil {
log.Printf("invalid value for channel5, should be an int: %v", err) log.Errorf("invalid value for channel5, should be an int: %v", err)
} }
if value < 1800 { if value < 1800 {
if !a.ctrlRecord { if !a.ctrlRecord {
log.Printf("Update channel 5 with value %v, record: %v", true, false) log.Infof("Update channel 5 with value %v, record: %v", true, false)
a.ctrlRecord = true a.ctrlRecord = true
} }
} else { } else {
if a.ctrlRecord { if a.ctrlRecord {
log.Printf("Update channel 5 with value %v, record: %v", false, true) log.Infof("Update channel 5 with value %v, record: %v", false, true)
a.ctrlRecord = false a.ctrlRecord = false
} }
} }
} }
func (a *Part) processChannel6(v string) { func (a *Part) processChannel6(v string) {
if a.debug { log.Debugf("channel6: %v", v)
log.Printf("channel6: %v", v)
}
value, err := strconv.Atoi(v) value, err := strconv.Atoi(v)
if err != nil { if err != nil {
log.Printf("invalid value for channel6, should be an int: %v", err) log.Errorf("invalid value for channel6, should be an int: %v", err)
return return
} }
if value > 1800 { if value > 1800 {
if a.driveMode != events.DriveMode_PILOT { if a.driveMode != events.DriveMode_PILOT {
log.Printf("Update channel 6 with value %v, new user_mode: %v", value, events.DriveMode_PILOT) log.Infof("Update channel 6 with value %v, new user_mode: %v", value, events.DriveMode_PILOT)
a.driveMode = events.DriveMode_PILOT a.driveMode = events.DriveMode_PILOT
} }
} else { } else {
if a.driveMode != events.DriveMode_USER { if a.driveMode != events.DriveMode_USER {
log.Printf("Update channel 6 with value %v, new user_mode: %v", value, events.DriveMode_USER) log.Infof("Update channel 6 with value %v, new user_mode: %v", value, events.DriveMode_USER)
} }
a.driveMode = events.DriveMode_USER a.driveMode = events.DriveMode_USER
} }

View File

@ -4,7 +4,7 @@ import (
"flag" "flag"
"github.com/cyrilix/robocar-arduino/arduino" "github.com/cyrilix/robocar-arduino/arduino"
"github.com/cyrilix/robocar-base/cli" "github.com/cyrilix/robocar-base/cli"
"log" log "github.com/sirupsen/logrus"
"os" "os"
) )
@ -38,13 +38,17 @@ func main() {
os.Exit(1) os.Exit(1)
} }
if debug {
log.SetLevel(log.DebugLevel)
}
client, err := cli.Connect(mqttBroker, username, password, clientId) client, err := cli.Connect(mqttBroker, username, password, clientId)
if err != nil { if err != nil {
log.Fatalf("unable to connect to mqtt broker: %v", err) log.Fatalf("unable to connect to mqtt broker: %v", err)
} }
defer client.Disconnect(10) defer client.Disconnect(10)
a := arduino.NewPart(client, device, baud, throttleTopic, steeringTopic, driveModeTopic, switchRecordTopic, pubFrequency, debug) a := arduino.NewPart(client, device, baud, throttleTopic, steeringTopic, driveModeTopic, switchRecordTopic, pubFrequency)
err = a.Start() err = a.Start()
if err != nil { if err != nil {
log.Printf("unable to start service: %v", err) log.Printf("unable to start service: %v", err)