Skip to content

Commit

Permalink
Merge branch 'master' into nightly
Browse files Browse the repository at this point in the history
  • Loading branch information
GrimmiMeloni committed Sep 16, 2024
2 parents 6677f0e + 3eb1ae0 commit e7bf5ef
Show file tree
Hide file tree
Showing 17 changed files with 564 additions and 125 deletions.
73 changes: 46 additions & 27 deletions charger/keba-modbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,55 +167,74 @@ func (wb *Keba) heartbeat(timeout time.Duration) {
}
}

// Status implements the api.Charger interface
func (wb *Keba) Status() (api.ChargeStatus, error) {
func (wb *Keba) isConnected() (bool, error) {
b, err := wb.conn.ReadHoldingRegisters(kebaRegCableState, 2)
if err != nil {
return api.StatusNone, err
return false, err
}

switch status := binary.BigEndian.Uint32(b); status {
case 0, 1, 3:
return api.StatusA, nil
// 0: No cable is plugged.
// 1: Cable is connected to the charging station (not to the electric vehicle).
// 3: Cable is connected to the charging station and locked (not to the electric vehicle).
// 5: Cable is connected to the charging station and the electric vehicle (not locked).
// 7: Cable is connected to the charging station and the electric vehicle and locked (charging).

case 5:
return api.StatusB, nil
return binary.BigEndian.Uint32(b)&(1<<2) != 0, err
}

case 7:
b, err := wb.conn.ReadHoldingRegisters(kebaRegChargingState, 2)
if err != nil {
return api.StatusNone, err
}
if binary.BigEndian.Uint32(b) == 3 {
return api.StatusC, err
}
return api.StatusB, nil
func (wb *Keba) getChargingState() (uint32, error) {
b, err := wb.conn.ReadHoldingRegisters(kebaRegChargingState, 2)
if err != nil {
return 0, err
}

// 0: Start-up of the charging station
// 1: The charging station is not ready for charging. The charging station is not connected to an electric vehicle, it is locked by the authorization function or another mechanism.
// 2: The charging station is ready for charging and waits for a reaction from the electric vehicle.
// 3: A charging process is active.
// 4: An error has occurred.
// 5: The charging process is temporarily interrupted because the temperature is too high or the wallbox is in suspended mode.

default:
return api.StatusNone, fmt.Errorf("invalid status: %d", status)
return binary.BigEndian.Uint32(b), nil
}

// Status implements the api.Charger interface
func (wb *Keba) Status() (api.ChargeStatus, error) {
if connected, err := wb.isConnected(); err != nil || !connected {
return api.StatusA, err
}

s, err := wb.getChargingState()
if err != nil {
return api.StatusA, err
}
if s == 3 {
return api.StatusC, nil
}
return api.StatusB, nil
}

// statusReason implements the api.StatusReasoner interface
func (wb *Keba) statusReason() (api.Reason, error) {
res := api.ReasonUnknown
if connected, err := wb.isConnected(); err != nil || !connected {
return api.ReasonUnknown, err
}

b, err := wb.conn.ReadHoldingRegisters(kebaRegChargingState, 2)
if err == nil && binary.BigEndian.Uint32(b) == 1 {
res = api.ReasonWaitingForAuthorization
if s, err := wb.getChargingState(); err != nil || s != 1 {
return api.ReasonUnknown, err
}

return res, err
return api.ReasonWaitingForAuthorization, nil
}

// Enabled implements the api.Charger interface
func (wb *Keba) Enabled() (bool, error) {
b, err := wb.conn.ReadHoldingRegisters(kebaRegChargingState, 2)
s, err := wb.getChargingState()
if err != nil {
return false, err
}
status := binary.BigEndian.Uint32(b)
return !(status == 5 || status == 1), nil

return !(s == 5 || s == 1), nil
}

// Enable implements the api.Charger interface
Expand Down
214 changes: 214 additions & 0 deletions charger/mypv-elwa2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
package charger

// LICENSE

// Copyright (c) 2024 andig

// This module is NOT covered by the MIT license. All rights reserved.

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import (
"encoding/binary"
"sync/atomic"
"time"

"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/modbus"
"github.com/evcc-io/evcc/util/sponsor"
)

// MyPvElwa2 charger implementation
type MyPvElwa2 struct {
log *util.Logger
conn *modbus.Connection
power uint32
}

const (
elwaRegSetPower = 1000
elwaRegTemp = 1001
elwaRegTempLimit = 1002
elwaRegStatus = 1003
elwaRegPower = 1074
)

func init() {
registry.Add("ac-elwa-2", NewMyPvElwa2FromConfig)
}

// https://github.com/evcc-io/evcc/discussions/12761

// NewMyPvElwa2FromConfig creates a MyPvElwa2 charger from generic config
func NewMyPvElwa2FromConfig(other map[string]interface{}) (api.Charger, error) {
cc := modbus.TcpSettings{
ID: 1,
}

if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}

return NewMyPvElwa2(cc.URI, cc.ID)
}

// NewMyPvElwa2 creates myPV AC Elwa 2 charger
func NewMyPvElwa2(uri string, slaveID uint8) (api.Charger, error) {
conn, err := modbus.NewConnection(uri, "", "", 0, modbus.Tcp, slaveID)
if err != nil {
return nil, err
}

if !sponsor.IsAuthorized() {
return nil, api.ErrSponsorRequired
}

log := util.NewLogger("ac-elwa-2")
conn.Logger(log.TRACE)

wb := &MyPvElwa2{
log: log,
conn: conn,
}

go wb.heartbeat(30 * time.Second)

return wb, nil
}

var _ api.IconDescriber = (*MyPvElwa2)(nil)

// Icon implements the api.IconDescriber interface
func (v *MyPvElwa2) Icon() string {
return "waterheater"
}

var _ api.FeatureDescriber = (*MyPvElwa2)(nil)

// Features implements the api.FeatureDescriber interface
func (wb *MyPvElwa2) Features() []api.Feature {
return []api.Feature{api.IntegratedDevice, api.Heating}
}

func (wb *MyPvElwa2) heartbeat(timeout time.Duration) {
for range time.Tick(timeout) {
if power := uint16(atomic.LoadUint32(&wb.power)); power > 0 {
enabled, err := wb.Enabled()
if err == nil && enabled {
err = wb.setPower(power)
}
if err != nil {
wb.log.ERROR.Println("heartbeat:", err)
}
}
}
}

// Status implements the api.Charger interface
func (wb *MyPvElwa2) Status() (api.ChargeStatus, error) {
res := api.StatusA
b, err := wb.conn.ReadInputRegisters(elwaRegStatus, 1)
if err != nil {
return res, err
}

res = api.StatusB
if binary.BigEndian.Uint16(b) == 2 {
res = api.StatusC
}

return res, nil
}

// Enabled implements the api.Charger interface
func (wb *MyPvElwa2) Enabled() (bool, error) {
b, err := wb.conn.ReadHoldingRegisters(elwaRegSetPower, 1)
if err != nil {
return false, err
}

return binary.BigEndian.Uint16(b) > 0, nil
}

func (wb *MyPvElwa2) setPower(power uint16) error {
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, power)

_, err := wb.conn.WriteMultipleRegisters(elwaRegSetPower, 1, b)
return err
}

// Enable implements the api.Charger interface
func (wb *MyPvElwa2) Enable(enable bool) error {
var power uint16
if enable {
power = uint16(atomic.LoadUint32(&wb.power))
}

return wb.setPower(power)
}

// MaxCurrent implements the api.Charger interface
func (wb *MyPvElwa2) MaxCurrent(current int64) error {
return wb.MaxCurrentMillis(float64(current))
}

var _ api.ChargerEx = (*MyPvElwa2)(nil)

// MaxCurrentMillis implements the api.ChargerEx interface
func (wb *MyPvElwa2) MaxCurrentMillis(current float64) error {
power := uint16(230 * current)

err := wb.setPower(power)
if err == nil {
atomic.StoreUint32(&wb.power, uint32(power))
}

return err
}

var _ api.Meter = (*MyPvElwa2)(nil)

// CurrentPower implements the api.Meter interface
func (wb *MyPvElwa2) CurrentPower() (float64, error) {
b, err := wb.conn.ReadInputRegisters(elwaRegPower, 1)
if err != nil {
return 0, err
}

return float64(binary.BigEndian.Uint16(b)), nil
}

var _ api.Battery = (*MyPvElwa2)(nil)

// CurrentPower implements the api.Meter interface
func (wb *MyPvElwa2) Soc() (float64, error) {
b, err := wb.conn.ReadInputRegisters(elwaRegTemp, 1)
if err != nil {
return 0, err
}

return float64(binary.BigEndian.Uint16(b)) / 10, nil
}

var _ api.SocLimiter = (*MyPvElwa2)(nil)

// GetLimitSoc implements the api.SocLimiter interface
func (wb *MyPvElwa2) GetLimitSoc() (int64, error) {
b, err := wb.conn.ReadInputRegisters(elwaRegTempLimit, 1)
if err != nil {
return 0, err
}

return int64(binary.BigEndian.Uint16(b)) / 10, nil
}
31 changes: 14 additions & 17 deletions charger/ocpp/cp_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,42 +85,39 @@ func (cp *CP) MeterValues(request *core.MeterValuesRequest) (*core.MeterValuesCo
default:
}

conn := cp.connectorByID(request.ConnectorId)
if conn == nil {
return nil, ErrInvalidConnector
if conn := cp.connectorByID(request.ConnectorId); conn != nil {
conn.MeterValues(request)
}

return conn.MeterValues(request)
return new(core.MeterValuesConfirmation), nil
}

func (cp *CP) StartTransaction(request *core.StartTransactionRequest) (*core.StartTransactionConfirmation, error) {
if request == nil {
return nil, ErrInvalidRequest
}

conn := cp.connectorByID(request.ConnectorId)
if conn == nil {
return nil, ErrInvalidConnector
if conn := cp.connectorByID(request.ConnectorId); conn != nil {
return conn.StartTransaction(request)
}

return conn.StartTransaction(request)
return new(core.StartTransactionConfirmation), nil
}

func (cp *CP) StopTransaction(request *core.StopTransactionRequest) (*core.StopTransactionConfirmation, error) {
if request == nil {
return nil, ErrInvalidRequest
}

conn := cp.connectorByTransactionID(request.TransactionId)
if conn == nil {
res := &core.StopTransactionConfirmation{
IdTagInfo: &types.IdTagInfo{
Status: types.AuthorizationStatusAccepted, // accept old pending stop message during startup
},
}
if conn := cp.connectorByTransactionID(request.TransactionId); conn != nil {
return conn.StopTransaction(request)
}

return res, nil
res := &core.StopTransactionConfirmation{
IdTagInfo: &types.IdTagInfo{
Status: types.AuthorizationStatusAccepted, // accept old pending stop message during startup
},
}

return conn.StopTransaction(request)
return res, nil
}
5 changes: 3 additions & 2 deletions core/loadpoint_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ func (lp *Loadpoint) plannerActive() (active bool) {
lp.setPlanActive(active)
}()

var planStart time.Time
var planEnd time.Time
var planStart, planEnd time.Time
var planOverrun time.Duration

defer func() {
lp.publish(keys.PlanProjectedStart, planStart)
lp.publish(keys.PlanProjectedEnd, planEnd)
Expand All @@ -100,6 +100,7 @@ func (lp *Loadpoint) plannerActive() (active bool) {
if planTime.IsZero() {
return false
}

// keep overrunning plans as long as a vehicle is connected
if lp.clock.Until(planTime) < 0 && (!lp.planActive || !lp.connected()) {
lp.log.DEBUG.Println("plan: deleting expired plan")
Expand Down
Loading

0 comments on commit e7bf5ef

Please sign in to comment.