Use generic modbus connection logging (#418)

This commit is contained in:
andig 2020-11-11 12:32:27 +01:00 • committed by GitHub
parent 0ebae4544a
commit e8f3b59acd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 31 deletions

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}