diff --git a/charger/alphatec.go b/charger/alphatec.go index 7e7001c76a..b6b04ed2c9 100644 --- a/charger/alphatec.go +++ b/charger/alphatec.go @@ -2,7 +2,7 @@ package charger // LICENSE -// Copyright (c) 2019-2022 andig +// Copyright (c) 2019-2023 andig, premultiply // This module is NOT covered by the MIT license. All rights reserved. @@ -32,13 +32,12 @@ import ( // Alphatec charger implementation type Alphatec struct { - conn *modbus.Connection - status api.ChargeStatus + conn *modbus.Connection + curr uint16 } const ( alphatecRegStatus = 0 - alphatecRegEnable = 4 alphatecRegAmpsConfig = 5 ) @@ -83,13 +82,40 @@ func NewAlphatec(uri, device, comset string, baudrate int, proto modbus.Protocol conn.Logger(log.TRACE) wb := &Alphatec{ - conn: conn, - status: api.StatusB, + conn: conn, + curr: 6, + } + + // get initial state from charger + curr, err := wb.getCurrent() + if err != nil { + return nil, fmt.Errorf("current limit: %w", err) + } + if curr > 0 { + wb.curr = curr } return wb, err } +func (wb *Alphatec) setCurrent(current uint16) error { + b := make([]byte, 2) + binary.BigEndian.PutUint16(b, uint16(current)) + + _, err := wb.conn.WriteMultipleRegisters(alphatecRegAmpsConfig, 1, b) + + return err +} + +func (wb *Alphatec) getCurrent() (uint16, error) { + b, err := wb.conn.ReadHoldingRegisters(alphatecRegAmpsConfig, 1) + if err != nil { + return 0, err + } + + return binary.BigEndian.Uint16(b), nil +} + // Status implements the api.Charger interface func (wb *Alphatec) Status() (api.ChargeStatus, error) { b, err := wb.conn.ReadHoldingRegisters(alphatecRegStatus, 1) @@ -97,56 +123,45 @@ func (wb *Alphatec) Status() (api.ChargeStatus, error) { return api.StatusNone, err } - var res api.ChargeStatus switch u := binary.BigEndian.Uint16(b); u { case 1: - res = api.StatusA + return api.StatusA, nil case 2: - res = api.StatusB + return api.StatusB, nil case 3: - res = api.StatusC - case 8: - res = wb.status - if wb.status == api.StatusC { - res = api.StatusB - } + return api.StatusC, nil default: return api.StatusNone, fmt.Errorf("invalid status: %d", u) } - - wb.status = res - - return res, nil } // Enabled implements the api.Charger interface func (wb *Alphatec) Enabled() (bool, error) { - b, err := wb.conn.ReadHoldingRegisters(alphatecRegEnable, 1) - if err != nil { - return false, err - } + curr, err := wb.getCurrent() - return binary.BigEndian.Uint16(b) == 0, nil + return curr >= 6, err } // Enable implements the api.Charger interface func (wb *Alphatec) Enable(enable bool) error { - b := make([]byte, 2) - if !enable { - binary.BigEndian.PutUint16(b, 1) + var curr uint16 + if enable { + curr = wb.curr } - _, err := wb.conn.WriteMultipleRegisters(alphatecRegEnable, 1, b) - - return err + return wb.setCurrent(curr) } // MaxCurrent implements the api.Charger interface func (wb *Alphatec) MaxCurrent(current int64) error { - b := make([]byte, 2) - binary.BigEndian.PutUint16(b, uint16(current)) + if current < 6 { + return fmt.Errorf("invalid current %d", current) + } - _, err := wb.conn.WriteMultipleRegisters(alphatecRegAmpsConfig, 1, b) + err := wb.setCurrent(uint16(current)) + if err == nil { + wb.curr = uint16(current) + } return err }