From 157736a99d08209829042c4b9140498e09bd05e3 Mon Sep 17 00:00:00 2001 From: Hendrik van Wyk Date: Fri, 25 Sep 2020 15:03:49 +0200 Subject: [PATCH] Add optional debug logging for frame decoding. --- README.md | 7 +++---- cmd/invertergui/config.go | 1 + cmd/invertergui/main.go | 5 +++++ mk2driver/mk2.go | 9 ++++++++- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 38e469c..1be2004 100644 --- a/README.md +++ b/README.md @@ -27,10 +27,8 @@ Usage: invertergui [OPTIONS] Application Options: - --address= The IP/DNS and port of the machine that the application is running on. (default: :8080) - [$ADDRESS] - --data.source= Set the source of data for the inverter gui. "serial", "tcp" or "mock" (default: serial) - [$DATA_SOURCE] + --address= The IP/DNS and port of the machine that the application is running on. (default: :8080) [$ADDRESS] + --data.source= Set the source of data for the inverter gui. "serial", "tcp" or "mock" (default: serial) [$DATA_SOURCE] --data.host= Host to connect when source is set to tcp. (default: localhost:8139) [$DATA_HOST] --data.device= TTY device to use when source is set to serial. (default: /dev/ttyUSB0) [$DATA_DEVICE] --cli.enabled Enable CLI output. [$CLI_ENABLED] @@ -40,6 +38,7 @@ Application Options: --mqtt.topic= Set the MQTT topic updates published to. (default: invertergui/updates) [$MQTT_TOPIC] --mqtt.username= Set the MQTT username [$MQTT_USERNAME] --mqtt.password= Set the MQTT password [$MQTT_PASSWORD] + --loglevel= The log level to generate logs at. ("panic", "fatal", "error", "warn", "info", "debug", "trace") (default: info) [$LOGLEVEL] Help Options: -h, --help Show this help message diff --git a/cmd/invertergui/config.go b/cmd/invertergui/config.go index 79de9a2..9cdad85 100644 --- a/cmd/invertergui/config.go +++ b/cmd/invertergui/config.go @@ -22,6 +22,7 @@ type config struct { Username string `long:"mqtt.username" env:"MQTT_USERNAME" default:"" description:"Set the MQTT username"` Password string `long:"mqtt.password" env:"MQTT_PASSWORD" default:"" description:"Set the MQTT password"` } + Loglevel string `long:"loglevel" env:"LOGLEVEL" default:"info" description:"The log level to generate logs at. (\"panic\", \"fatal\", \"error\", \"warn\", \"info\", \"debug\", \"trace\")"` } func parseConfig() (*config, error) { diff --git a/cmd/invertergui/main.go b/cmd/invertergui/main.go index 4bb57c3..84f5cb0 100644 --- a/cmd/invertergui/main.go +++ b/cmd/invertergui/main.go @@ -58,6 +58,11 @@ func main() { os.Exit(1) } log.Info("Starting invertergui") + logLevel, err := logrus.ParseLevel(conf.Loglevel) + if err != nil { + log.Fatalf("Could not parse log level: %v", err) + } + logrus.SetLevel(logLevel) mk2, err := getMk2Device(conf.Data.Source, conf.Data.Host, conf.Data.Device) if err != nil { diff --git a/mk2driver/mk2.go b/mk2driver/mk2.go index 7a34be7..5c7d3b8 100644 --- a/mk2driver/mk2.go +++ b/mk2driver/mk2.go @@ -183,6 +183,7 @@ func (m *mk2Ser) updateReport() { // Checks for valid frame and chooses decoding. func (m *mk2Ser) handleFrame(l byte, frame []byte) { + logrus.Debugf("frame %#v", frame) if checkChecksum(l, frame[0], frame[1:]) { switch frame[0] { case frameHeader: @@ -243,7 +244,7 @@ func int16Abs(in int16) uint16 { // Decode the scale factor frame. func (m *mk2Ser) scaleDecode(frame []byte) { tmp := scaling{} - logrus.Infof("Scale frame(%d): 0x%x", len(frame), frame) + logrus.Debugf("Scale frame(%d): 0x%x", len(frame), frame) if len(frame) < 6 { tmp.supported = false logrus.Warnf("Skiping scaling factors for: %d", m.scaleCount) @@ -269,6 +270,7 @@ func (m *mk2Ser) scaleDecode(frame []byte) { tmp.scale = float64(scale) } } + logrus.Debugf("scalecount %v: %#v \n", m.scaleCount, tmp) m.scales = append(m.scales, tmp) m.scaleCount++ if m.scaleCount < ramVarMaxOffset { @@ -280,6 +282,7 @@ func (m *mk2Ser) scaleDecode(frame []byte) { // Decode the version number func (m *mk2Ser) versionDecode(frame []byte) { + logrus.Debugf("versiondecode %v", frame) m.info.Version = 0 m.info.Valid = true for i := 0; i < 4; i++ { @@ -344,6 +347,7 @@ func (m *mk2Ser) dcDecode(frame []byte) { m.info.BatCurrent = usedC - chargeC m.info.OutFrequency = 10 / (m.applyScale(float64(frame[13]), ramVarInverterPeriod)) + logrus.Debugf("dcDecode %#v", m.info) // Send L1 status request cmd := make([]byte, 2) @@ -364,6 +368,7 @@ func (m *mk2Ser) acDecode(frame []byte) { } else { m.info.InFrequency = 10 / (m.applyScale(float64(frame[13]), ramVarMainPeriod)) } + logrus.Debugf("acDecode %#v", m.info) // Send status request cmd := make([]byte, 1) @@ -374,6 +379,7 @@ func (m *mk2Ser) acDecode(frame []byte) { // Decode charge state of battery. func (m *mk2Ser) stateDecode(frame []byte) { m.info.ChargeState = m.applyScaleAndSign(frame[1:3], ramVarChargeState) + logrus.Debugf("battery state decode %#v", m.info) m.updateReport() } @@ -420,6 +426,7 @@ func (m *mk2Ser) sendCommand(data []byte) { } dataOut[l+2] = cr + logrus.Debugf("sendCommand %#v", dataOut) _, err := m.p.Write(dataOut) if err != nil { m.addError(fmt.Errorf("Write error: %v", err))