From e8f3b59acd4fc73be86974ef1ea5c38c075fc955 Mon Sep 17 00:00:00 2001 From: andig Date: Wed, 11 Nov 2020 12:32:27 +0100 Subject: [PATCH] Use generic modbus connection logging (#418) --- charger/phoenix-emcp.go | 16 +++++----------- charger/phoenix-evcc.go | 15 +++++---------- charger/wallbe.go | 15 +++++---------- 3 files changed, 15 insertions(+), 31 deletions(-) diff --git a/charger/phoenix-emcp.go b/charger/phoenix-emcp.go index 2be9b5a06..cf16acacc 100644 --- a/charger/phoenix-emcp.go +++ b/charger/phoenix-emcp.go @@ -26,7 +26,6 @@ var phEMCPRegCurrents = []uint16{114, 116, 118} // current readings // PhoenixEMCP is an api.ChargeController implementation for Phoenix EM-CP-PP-ETH wallboxes. // It uses Modbus TCP to communicate with the wallbox at modbus client id 180. type PhoenixEMCP struct { - log *util.Logger conn *modbus.Connection } @@ -79,15 +78,15 @@ func NewPhoenixEMCPFromConfig(other map[string]interface{}) (api.Charger, error) // NewPhoenixEMCP creates a Phoenix charger func NewPhoenixEMCP(uri string, id uint8) (*PhoenixEMCP, error) { - log := util.NewLogger("emcp") - conn, err := modbus.NewConnection(uri, "", "", 0, false, id) if err != nil { return nil, err } + log := util.NewLogger("emcp") + conn.Logger(log.TRACE) + wb := &PhoenixEMCP{ - log: log, conn: conn, } @@ -97,7 +96,6 @@ func NewPhoenixEMCP(uri string, id uint8) (*PhoenixEMCP, error) { // Status implements the Charger.Status interface func (wb *PhoenixEMCP) Status() (api.ChargeStatus, error) { b, err := wb.conn.ReadInputRegisters(phEMCPRegStatus, 1) - wb.log.TRACE.Printf("read status (%d): %0 X", phEMCPRegStatus, b) if err != nil { return api.StatusNone, err } @@ -108,7 +106,6 @@ func (wb *PhoenixEMCP) Status() (api.ChargeStatus, error) { // Enabled implements the Charger.Enabled interface func (wb *PhoenixEMCP) Enabled() (bool, error) { b, err := wb.conn.ReadCoils(phEMCPRegEnable, 1) - wb.log.TRACE.Printf("read charge enable (%d): %0 X", phEMCPRegEnable, b) if err != nil { return false, err } @@ -123,8 +120,7 @@ func (wb *PhoenixEMCP) Enable(enable bool) error { u = 0xFF00 } - b, err := wb.conn.WriteSingleCoil(phEMCPRegEnable, u) - wb.log.TRACE.Printf("write charge enable (%d) %0X: %0 X", phEMCPRegEnable, u, b) + _, err := wb.conn.WriteSingleCoil(phEMCPRegEnable, u) return err } @@ -135,8 +131,7 @@ func (wb *PhoenixEMCP) MaxCurrent(current int64) error { return fmt.Errorf("invalid current %d", current) } - b, err := wb.conn.WriteSingleRegister(phEMCPRegMaxCurrent, uint16(current)) - wb.log.TRACE.Printf("write max current (%d) %0X: %0 X", phEMCPRegMaxCurrent, current, b) + _, err := wb.conn.WriteSingleRegister(phEMCPRegMaxCurrent, uint16(current)) return err } @@ -144,7 +139,6 @@ func (wb *PhoenixEMCP) MaxCurrent(current int64) error { // ChargingTime yields current charge run duration func (wb *PhoenixEMCP) ChargingTime() (time.Duration, error) { b, err := wb.conn.ReadInputRegisters(phEMCPRegChargeTime, 2) - wb.log.TRACE.Printf("read charge time (%d): %0 X", phEMCPRegChargeTime, b) if err != nil { return 0, err } diff --git a/charger/phoenix-evcc.go b/charger/phoenix-evcc.go index c66c255d9..50d095e95 100644 --- a/charger/phoenix-evcc.go +++ b/charger/phoenix-evcc.go @@ -23,7 +23,6 @@ var phRegCurrents = []uint16{334, 335, 336} // current readings // PhoenixEVCC is an api.ChargeController implementation for Phoenix EV-CC-AC1-M wallboxes. // It uses Modbus TCP to communicate with the wallbox at modbus client id 255. type PhoenixEVCC struct { - log *util.Logger conn *modbus.Connection } @@ -72,15 +71,15 @@ func NewPhoenixEVCCFromConfig(other map[string]interface{}) (api.Charger, error) // NewPhoenixEVCC creates a Phoenix charger func NewPhoenixEVCC(uri, device, comset string, baudrate int, id uint8) (*PhoenixEVCC, error) { - log := util.NewLogger("evcc") - conn, err := modbus.NewConnection(uri, device, comset, baudrate, true, id) if err != nil { return nil, err } + log := util.NewLogger("evcc") + conn.Logger(log.TRACE) + wb := &PhoenixEVCC{ - log: log, conn: conn, } @@ -90,7 +89,6 @@ func NewPhoenixEVCC(uri, device, comset string, baudrate int, id uint8) (*Phoeni // Status implements the Charger.Status interface func (wb *PhoenixEVCC) Status() (api.ChargeStatus, error) { b, err := wb.conn.ReadInputRegisters(phEVCCRegStatus, 1) - wb.log.TRACE.Printf("read status (%d): %0 X", phEVCCRegStatus, b) if err != nil { return api.StatusNone, err } @@ -101,7 +99,6 @@ func (wb *PhoenixEVCC) Status() (api.ChargeStatus, error) { // Enabled implements the Charger.Enabled interface func (wb *PhoenixEVCC) Enabled() (bool, error) { b, err := wb.conn.ReadCoils(phEVCCRegEnable, 1) - wb.log.TRACE.Printf("read charge enable (%d): %0 X", phEVCCRegEnable, b) if err != nil { return false, err } @@ -116,8 +113,7 @@ func (wb *PhoenixEVCC) Enable(enable bool) error { u = 0xFF00 } - b, err := wb.conn.WriteSingleCoil(phEVCCRegEnable, u) - wb.log.TRACE.Printf("write charge enable (%d) %0X: %0 X", phEVCCRegEnable, u, b) + _, err := wb.conn.WriteSingleCoil(phEVCCRegEnable, u) return err } @@ -128,8 +124,7 @@ func (wb *PhoenixEVCC) MaxCurrent(current int64) error { return fmt.Errorf("invalid current %d", current) } - b, err := wb.conn.WriteSingleRegister(phEVCCRegMaxCurrent, uint16(current)) - wb.log.TRACE.Printf("write max current (%d) %0X: %0 X", phEVCCRegMaxCurrent, current, b) + _, err := wb.conn.WriteSingleRegister(phEVCCRegMaxCurrent, uint16(current)) return err } diff --git a/charger/wallbe.go b/charger/wallbe.go index f1e24a9b3..ceb2efcc5 100644 --- a/charger/wallbe.go +++ b/charger/wallbe.go @@ -34,7 +34,6 @@ var wbRegCurrents = []uint16{114, 116, 118} // current readings // Phoenix EV-CC-AC1-M3-CBC-RCM-ETH controller. // It uses Modbus TCP to communicate with the wallbox at modbus client id 255. type Wallbe struct { - log *util.Logger conn *modbus.Connection factor int64 encoding string @@ -101,8 +100,10 @@ func NewWallbe(uri string) (*Wallbe, error) { return nil, err } + log := util.NewLogger("wallbe") + conn.Logger(log.TRACE) + wb := &Wallbe{ - log: util.NewLogger("wallbe"), conn: conn, factor: 10, } @@ -113,7 +114,6 @@ func NewWallbe(uri string) (*Wallbe, error) { // Status implements the Charger.Status interface func (wb *Wallbe) Status() (api.ChargeStatus, error) { b, err := wb.conn.ReadInputRegisters(wbRegStatus, 1) - wb.log.TRACE.Printf("read status (%d): %0 X", wbRegStatus, b) if err != nil { return api.StatusNone, err } @@ -124,7 +124,6 @@ func (wb *Wallbe) Status() (api.ChargeStatus, error) { // Enabled implements the Charger.Enabled interface func (wb *Wallbe) Enabled() (bool, error) { b, err := wb.conn.ReadCoils(wbRegEnable, 1) - wb.log.TRACE.Printf("read charge enable (%d): %0 X", wbRegEnable, b) if err != nil { return false, err } @@ -139,8 +138,7 @@ func (wb *Wallbe) Enable(enable bool) error { u = 0xFF00 } - b, err := wb.conn.WriteSingleCoil(wbRegEnable, u) - wb.log.TRACE.Printf("write charge enable (%d) %0X: %0 X", wbRegEnable, u, b) + _, err := wb.conn.WriteSingleCoil(wbRegEnable, u) return err } @@ -152,9 +150,7 @@ func (wb *Wallbe) MaxCurrent(current int64) error { } u := uint16(current * wb.factor) - - b, err := wb.conn.WriteSingleRegister(wbRegMaxCurrent, u) - wb.log.TRACE.Printf("write max current (%d) %0X: %0 X", wbRegMaxCurrent, u, b) + _, err := wb.conn.WriteSingleRegister(wbRegMaxCurrent, u) return err } @@ -162,7 +158,6 @@ func (wb *Wallbe) MaxCurrent(current int64) error { // ChargingTime yields current charge run duration func (wb *Wallbe) ChargingTime() (time.Duration, error) { b, err := wb.conn.ReadInputRegisters(wbRegChargeTime, 2) - wb.log.TRACE.Printf("read charge time (%d): %0 X", wbRegChargeTime, b) if err != nil { return 0, err }