Add 1p3p phase switching support (#1213)
This commit is contained in:
parent
182833aee3
commit
f22a1e1db4
16 changed files with 550 additions and 83 deletions
|
|
@ -194,7 +194,7 @@ Available charger implementations are:
|
|||
- `mcc`: Mobile Charger Connect devices (Audi, Bentley, Porsche)
|
||||
- `nrgkick-bluetooth`: NRGkick chargers with Bluetooth connector (Linux only, not supported on Docker)
|
||||
- `nrgkick-connect`: NRGkick chargers with additional NRGkick Connect module
|
||||
- `openWB`: openWB chargers using openWB's MQTT interface
|
||||
- `openWB`: openWB chargers using openWB's MQTT interface (set `phases: true` to indicate if openWB is equipped with 1p3p capability- currently this cannot be auto detected)
|
||||
- `phoenix-em-eth`: chargers with Phoenix **EM**-CP-PP-**ETH** controllers
|
||||
- `phoenix-ev-eth`: chargers with Phoenix **EV**-CC-\*\*\*-**ETH** controllers (see [Preparation](#phoenix-emev-ethernet-controller-preparation-))
|
||||
- `phoenix-ev-ser`: chargers with Phoenix **EV**-CC-\*\*\*-**SER** serial controllers (Modbus RTU)
|
||||
|
|
@ -583,7 +583,9 @@ EVCC provides a REST and MQTT APIs.
|
|||
|
||||
- `/api/state`: EVCC state (static configuration and dynamic state)
|
||||
- `/api/loadpoints/<id>/mode`: loadpoint charge mode (writable)
|
||||
- `/api/loadpoints/<id>/minsoc`: loadpoint minimum SoC (writable)
|
||||
- `/api/loadpoints/<id>/targetsoc`: loadpoint target SoC (writable)
|
||||
- `/api/loadpoints/<id>/phases`: loadpoint enabled phases (writable)
|
||||
|
||||
Note: to modify writable settings perform a `POST` request appending the value as path segment.
|
||||
|
||||
|
|
@ -601,6 +603,7 @@ The MQTT API follows the REST API's structure, with loadpoint ids starting at `0
|
|||
- `evcc/loadpoints/<id>/mode`: loadpoint charge mode (writable)
|
||||
- `evcc/loadpoints/<id>/minSoC`: loadpoint minimum SoC (writable)
|
||||
- `evcc/loadpoints/<id>/targetSoC`: loadpoint target SoC (writable)
|
||||
- `evcc/loadpoints/<id>/phases`: loadpoint enabled phases (writable)
|
||||
|
||||
Note: to modify writable settings append `/set` to the topic for writing.
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ package api
|
|||
|
||||
import "time"
|
||||
|
||||
//go:generate mockgen -package mock -destination ../mock/mock_api.go github.com/andig/evcc/api Charger,ChargeState,Identifier,Meter,MeterEnergy,Vehicle,ChargeRater,Battery
|
||||
//go:generate mockgen -package mock -destination ../mock/mock_api.go github.com/andig/evcc/api Charger,ChargeState,ChargePhases,Identifier,Meter,MeterEnergy,Vehicle,ChargeRater,Battery
|
||||
|
||||
// ChargeMode are charge modes modeled after OpenWB
|
||||
type ChargeMode string
|
||||
|
|
@ -77,6 +77,11 @@ type ChargerEx interface {
|
|||
MaxCurrentMillis(current float64) error
|
||||
}
|
||||
|
||||
// ChargePhases provides 1p3p switching
|
||||
type ChargePhases interface {
|
||||
Phases1p3p(phases int) error
|
||||
}
|
||||
|
||||
// Diagnosis is a helper interface that allows to dump diagnostic data to console
|
||||
type Diagnosis interface {
|
||||
Diagnose()
|
||||
|
|
|
|||
|
|
@ -35,15 +35,17 @@ import (
|
|||
// Easee charger implementation
|
||||
type Easee struct {
|
||||
*request.Helper
|
||||
charger string
|
||||
site, circuit int
|
||||
status easee.ChargerStatus
|
||||
updated time.Time
|
||||
cache time.Duration
|
||||
lp core.LoadPointAPI
|
||||
lastSmartCharging bool
|
||||
lastChargeMode api.ChargeMode
|
||||
log *util.Logger
|
||||
charger string
|
||||
site, circuit int
|
||||
status easee.ChargerStatus
|
||||
updated time.Time
|
||||
cache time.Duration
|
||||
lp core.LoadPointAPI
|
||||
//lastSmartCharging bool
|
||||
//lastChargeMode api.ChargeMode
|
||||
log *util.Logger
|
||||
phases int
|
||||
current float64
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
|
@ -83,6 +85,7 @@ func NewEasee(user, password, charger string, circuit int, cache time.Duration)
|
|||
circuit: circuit,
|
||||
cache: cache,
|
||||
log: log,
|
||||
phases: 3,
|
||||
}
|
||||
|
||||
ts, err := easee.TokenSource(log, user, password)
|
||||
|
|
@ -152,6 +155,7 @@ func (c *Easee) chargerDetails() (res easee.Site, err error) {
|
|||
return res, err
|
||||
}
|
||||
|
||||
/*
|
||||
func (c *Easee) syncSmartCharging() error {
|
||||
if c.lp == nil {
|
||||
return nil
|
||||
|
|
@ -194,6 +198,7 @@ func (c *Easee) syncSmartCharging() error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
*/
|
||||
|
||||
func (c *Easee) state() (easee.ChargerStatus, error) {
|
||||
if time.Since(c.updated) < c.cache {
|
||||
|
|
@ -204,7 +209,7 @@ func (c *Easee) state() (easee.ChargerStatus, error) {
|
|||
req, err := request.New(http.MethodGet, uri, nil, request.JSONEncoding)
|
||||
if err == nil {
|
||||
if err = c.DoJSON(req, &c.status); err == nil {
|
||||
err = c.syncSmartCharging()
|
||||
// err = c.syncSmartCharging()
|
||||
c.updated = time.Now()
|
||||
}
|
||||
}
|
||||
|
|
@ -285,27 +290,40 @@ var _ api.ChargerEx = (*Easee)(nil)
|
|||
|
||||
// MaxCurrentMillis implements the api.ChargerEx interface
|
||||
func (c *Easee) MaxCurrentMillis(current float64) error {
|
||||
cur := int(current)
|
||||
var current23 float64
|
||||
if c.phases > 1 {
|
||||
current23 = current
|
||||
}
|
||||
|
||||
data := easee.CircuitSettings{
|
||||
DynamicCircuitCurrentP1: &cur,
|
||||
DynamicCircuitCurrentP2: &cur,
|
||||
DynamicCircuitCurrentP3: &cur,
|
||||
DynamicCircuitCurrentP1: ¤t,
|
||||
DynamicCircuitCurrentP2: ¤t23,
|
||||
DynamicCircuitCurrentP3: ¤t23,
|
||||
}
|
||||
|
||||
uri := fmt.Sprintf("%s/sites/%d/circuits/%d/settings", easee.API, c.site, c.circuit)
|
||||
resp, err := c.Post(uri, request.JSONContent, request.MarshalJSON(data))
|
||||
if err == nil {
|
||||
resp.Body.Close()
|
||||
}
|
||||
|
||||
c.updated = time.Time{} // clear cache
|
||||
c.updated = time.Time{} // clear cache
|
||||
c.current = current
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
var _ api.ChargePhases = (*Easee)(nil)
|
||||
|
||||
// Phases1p3p implements the api.ChargePhases interface
|
||||
func (c *Easee) Phases1p3p(phases int) error {
|
||||
c.phases = phases
|
||||
return c.MaxCurrentMillis(c.current)
|
||||
}
|
||||
|
||||
var _ api.Meter = (*Easee)(nil)
|
||||
|
||||
// CurrentPower implements the api.Meter interface.
|
||||
// CurrentPower implements the api.Meter interface
|
||||
func (c *Easee) CurrentPower() (float64, error) {
|
||||
res, err := c.state()
|
||||
return 1e3 * res.TotalPower, err
|
||||
|
|
|
|||
|
|
@ -136,14 +136,14 @@ type ChargerSettings struct {
|
|||
|
||||
// CircuitSettings is the circuit settings type
|
||||
type CircuitSettings struct {
|
||||
DynamicCircuitCurrentP1 *int `json:"dynamicCircuitCurrentP1,omitempty"`
|
||||
DynamicCircuitCurrentP2 *int `json:"dynamicCircuitCurrentP2,omitempty"`
|
||||
DynamicCircuitCurrentP3 *int `json:"dynamicCircuitCurrentP3,omitempty"`
|
||||
MaxCircuitCurrentP1 *int `json:"maxCircuitCurrentP1,omitempty"`
|
||||
MaxCircuitCurrentP2 *int `json:"maxCircuitCurrentP2,omitempty"`
|
||||
MaxCircuitCurrentP3 *int `json:"maxCircuitCurrentP3,omitempty"`
|
||||
EnableIdleCurrent *bool `json:"enableIdleCurrent,omitempty"`
|
||||
OfflineMaxCircuitCurrentP1 *int `json:"offlineMaxCircuitCurrentP1,omitempty"`
|
||||
OfflineMaxCircuitCurrentP2 *int `json:"offlineMaxCircuitCurrentP2,omitempty"`
|
||||
OfflineMaxCircuitCurrentP3 *int `json:"offlineMaxCircuitCurrentP3,omitempty"`
|
||||
DynamicCircuitCurrentP1 *float64 `json:"dynamicCircuitCurrentP1,omitempty"`
|
||||
DynamicCircuitCurrentP2 *float64 `json:"dynamicCircuitCurrentP2,omitempty"`
|
||||
DynamicCircuitCurrentP3 *float64 `json:"dynamicCircuitCurrentP3,omitempty"`
|
||||
MaxCircuitCurrentP1 *int `json:"maxCircuitCurrentP1,omitempty"`
|
||||
MaxCircuitCurrentP2 *int `json:"maxCircuitCurrentP2,omitempty"`
|
||||
MaxCircuitCurrentP3 *int `json:"maxCircuitCurrentP3,omitempty"`
|
||||
EnableIdleCurrent *bool `json:"enableIdleCurrent,omitempty"`
|
||||
OfflineMaxCircuitCurrentP1 *int `json:"offlineMaxCircuitCurrentP1,omitempty"`
|
||||
OfflineMaxCircuitCurrentP2 *int `json:"offlineMaxCircuitCurrentP2,omitempty"`
|
||||
OfflineMaxCircuitCurrentP3 *int `json:"offlineMaxCircuitCurrentP3,omitempty"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,8 +21,11 @@ type OpenWB struct {
|
|||
currentPowerG func() (float64, error)
|
||||
totalEnergyG func() (float64, error)
|
||||
currentsG []func() (float64, error)
|
||||
phasesS func(int64) error
|
||||
}
|
||||
|
||||
// go:generate go run ../cmd/tools/decorate.go -f decorateOpenWB -b *OpenWB -r api.Charger -t "api.ChargePhases,Phases1p3p,func(int) (error)"
|
||||
|
||||
// NewOpenWBFromConfig creates a new configurable charger
|
||||
func NewOpenWBFromConfig(other map[string]interface{}) (api.Charger, error) {
|
||||
cc := struct {
|
||||
|
|
@ -30,6 +33,7 @@ func NewOpenWBFromConfig(other map[string]interface{}) (api.Charger, error) {
|
|||
Topic string
|
||||
Timeout time.Duration
|
||||
ID int
|
||||
Phases bool
|
||||
}{
|
||||
Topic: openwb.RootTopic,
|
||||
Timeout: openwb.Timeout,
|
||||
|
|
@ -42,11 +46,11 @@ func NewOpenWBFromConfig(other map[string]interface{}) (api.Charger, error) {
|
|||
|
||||
log := util.NewLogger("openwb")
|
||||
|
||||
return NewOpenWB(log, cc.Config, cc.ID, cc.Topic, cc.Timeout)
|
||||
return NewOpenWB(log, cc.Config, cc.ID, cc.Topic, cc.Phases, cc.Timeout)
|
||||
}
|
||||
|
||||
// NewOpenWB creates a new configurable charger
|
||||
func NewOpenWB(log *util.Logger, mqttconf mqtt.Config, id int, topic string, timeout time.Duration) (*OpenWB, error) {
|
||||
func NewOpenWB(log *util.Logger, mqttconf mqtt.Config, id int, topic string, p1p3 bool, timeout time.Duration) (api.Charger, error) {
|
||||
client, err := mqtt.RegisteredClientOrDefault(log, mqttconf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -59,7 +63,7 @@ func NewOpenWB(log *util.Logger, mqttconf mqtt.Config, id int, topic string, tim
|
|||
|
||||
// getters
|
||||
boolG := func(topic string) func() (bool, error) {
|
||||
g := provider.NewMqtt(log, client, topic, 1, 0).BoolGetter()
|
||||
g := provider.NewMqtt(log, client, topic, 1, timeout).BoolGetter()
|
||||
return func() (val bool, err error) {
|
||||
if val, err = g(); err == nil {
|
||||
_, err = timer()
|
||||
|
|
@ -69,7 +73,7 @@ func NewOpenWB(log *util.Logger, mqttconf mqtt.Config, id int, topic string, tim
|
|||
}
|
||||
|
||||
floatG := func(topic string) func() (float64, error) {
|
||||
g := provider.NewMqtt(log, client, topic, 1, 0).FloatGetter()
|
||||
g := provider.NewMqtt(log, client, topic, 1, timeout).FloatGetter()
|
||||
return func() (val float64, err error) {
|
||||
if val, err = g(); err == nil {
|
||||
_, err = timer()
|
||||
|
|
@ -85,18 +89,18 @@ func NewOpenWB(log *util.Logger, mqttconf mqtt.Config, id int, topic string, tim
|
|||
}
|
||||
|
||||
// adapt plugged/charging to status
|
||||
plugged := boolG(fmt.Sprintf("%s/lp/%d/%s", topic, id, openwb.PluggedTopic))
|
||||
charging := boolG(fmt.Sprintf("%s/lp/%d/%s", topic, id, openwb.ChargingTopic))
|
||||
status := provider.NewOpenWBStatusProvider(plugged, charging).StringGetter
|
||||
pluggedG := boolG(fmt.Sprintf("%s/lp/%d/%s", topic, id, openwb.PluggedTopic))
|
||||
chargingG := boolG(fmt.Sprintf("%s/lp/%d/%s", topic, id, openwb.ChargingTopic))
|
||||
statusG := provider.NewOpenWBStatusProvider(pluggedG, chargingG).StringGetter
|
||||
|
||||
// remaining getters
|
||||
enabled := boolG(fmt.Sprintf("%s/lp/%d/%s", topic, id, openwb.EnabledTopic))
|
||||
enabledG := boolG(fmt.Sprintf("%s/lp/%d/%s", topic, id, openwb.EnabledTopic))
|
||||
|
||||
// setters
|
||||
enable := provider.NewMqtt(log, client,
|
||||
enableS := provider.NewMqtt(log, client,
|
||||
fmt.Sprintf("%s/set/lp%d/%s", topic, id, openwb.EnabledTopic),
|
||||
1, timeout).BoolSetter("enable")
|
||||
maxcurrent := provider.NewMqtt(log, client,
|
||||
maxcurrentS := provider.NewMqtt(log, client,
|
||||
fmt.Sprintf("%s/set/lp%d/%s", topic, id, openwb.MaxCurrentTopic),
|
||||
1, timeout).IntSetter("maxcurrent")
|
||||
|
||||
|
|
@ -110,19 +114,29 @@ func NewOpenWB(log *util.Logger, mqttconf mqtt.Config, id int, topic string, tim
|
|||
currentsG = append(currentsG, current)
|
||||
}
|
||||
|
||||
charger, err := NewConfigurable(status, enabled, enable, maxcurrent)
|
||||
charger, err := NewConfigurable(statusG, enabledG, enableS, maxcurrentS)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res := &OpenWB{
|
||||
c := &OpenWB{
|
||||
Charger: charger,
|
||||
currentPowerG: currentPowerG,
|
||||
totalEnergyG: totalEnergyG,
|
||||
currentsG: currentsG,
|
||||
}
|
||||
|
||||
return res, nil
|
||||
// optional capabilities
|
||||
c.phasesS = provider.NewMqtt(log, client,
|
||||
fmt.Sprintf("%s/set/isss/%s", topic, openwb.PhasesTopic),
|
||||
1, timeout).IntSetter("phases")
|
||||
|
||||
var phases func(int) error
|
||||
if p1p3 {
|
||||
phases = c.phases
|
||||
}
|
||||
|
||||
return decorateOpenWB(c, phases), nil
|
||||
}
|
||||
|
||||
var _ api.Meter = (*OpenWB)(nil)
|
||||
|
|
@ -155,3 +169,8 @@ func (m *OpenWB) Currents() (float64, float64, float64, error) {
|
|||
|
||||
return currents[0], currents[1], currents[2], nil
|
||||
}
|
||||
|
||||
// phases implements the api.ChargePhases interface
|
||||
func (c *OpenWB) phases(phases int) error {
|
||||
return c.phasesS(int64(phases))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ const (
|
|||
// getter/setter
|
||||
EnabledTopic = "ChargePointEnabled"
|
||||
MaxCurrentTopic = "DirectChargeAmps"
|
||||
PhasesTopic = "U1p3p"
|
||||
|
||||
// charge power
|
||||
ChargePowerTopic = "W"
|
||||
|
|
|
|||
35
charger/openwb_decorators.go
Normal file
35
charger/openwb_decorators.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package charger
|
||||
|
||||
// Code generated by github.com/andig/cmd/tools/decorate.go. DO NOT EDIT.
|
||||
|
||||
import (
|
||||
"github.com/andig/evcc/api"
|
||||
)
|
||||
|
||||
func decorateOpenWB(base *OpenWB, chargePhases func(int) error) api.Charger {
|
||||
switch {
|
||||
case chargePhases == nil:
|
||||
return base
|
||||
|
||||
case chargePhases != nil:
|
||||
return &struct {
|
||||
*OpenWB
|
||||
api.ChargePhases
|
||||
}{
|
||||
OpenWB: base,
|
||||
ChargePhases: &decorateOpenWBChargePhasesImpl{
|
||||
chargePhases: chargePhases,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type decorateOpenWBChargePhasesImpl struct {
|
||||
chargePhases func(int) error
|
||||
}
|
||||
|
||||
func (impl *decorateOpenWBChargePhasesImpl) Phases1p3p(phases int) error {
|
||||
return impl.chargePhases(phases)
|
||||
}
|
||||
|
|
@ -8,10 +8,14 @@ import (
|
|||
|
||||
func TestOpenWBDecorators(t *testing.T) {
|
||||
// host not reachable
|
||||
wb, _ := NewOpenWBFromConfig(map[string]interface{}{
|
||||
wb, err := NewOpenWBFromConfig(map[string]interface{}{
|
||||
"broker": "192.0.2.2",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Skip(err)
|
||||
}
|
||||
|
||||
if _, ok := wb.(api.Meter); !ok {
|
||||
t.Error("missing Meter api")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,9 @@ var (
|
|||
|
||||
// powerToCurrent is a helper function to convert power to per-phase current
|
||||
func powerToCurrent(power float64, phases int64) float64 {
|
||||
if Voltage == 0 {
|
||||
panic("Voltage is not set")
|
||||
}
|
||||
return power / (float64(phases) * Voltage)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -86,7 +86,7 @@ type LoadPoint struct {
|
|||
Mode api.ChargeMode `mapstructure:"mode"` // Charge mode, guarded by mutex
|
||||
|
||||
Title string `mapstructure:"title"` // UI title
|
||||
Phases int64 `mapstructure:"phases"` // Phases- required for converting power and current
|
||||
Phases int64 `mapstructure:"phases"` // Charger enabled phases
|
||||
ChargerRef string `mapstructure:"charger"` // Charger reference
|
||||
VehicleRef string `mapstructure:"vehicle"` // Vehicle reference
|
||||
VehiclesRef []string `mapstructure:"vehicles"` // Vehicles reference
|
||||
|
|
@ -103,6 +103,7 @@ type LoadPoint struct {
|
|||
GuardDuration time.Duration // charger enable/disable minimum holding time
|
||||
|
||||
enabled bool // Charger enabled state
|
||||
activePhases int64 // Charger active phases as used by vehicle
|
||||
chargeCurrent float64 // Charger current limit
|
||||
guardUpdated time.Time // Charger enabled/disabled timestamp
|
||||
socUpdated time.Time // SoC updated timestamp (poll: connected)
|
||||
|
|
@ -127,6 +128,7 @@ type LoadPoint struct {
|
|||
chargeCurrents []float64 // Phase currents
|
||||
connectedTime time.Time // Time when vehicle was connected
|
||||
pvTimer time.Time // PV enabled/disable timer
|
||||
phaseTimer time.Time // 1p3p switch timer
|
||||
|
||||
socCharge float64 // Vehicle SoC
|
||||
chargedEnergy float64 // Charged energy while connected in Wh
|
||||
|
|
@ -231,7 +233,7 @@ func NewLoadPoint(log *util.Logger) *LoadPoint {
|
|||
clock: clock, // mockable time
|
||||
bus: bus, // event bus
|
||||
Mode: api.ModeOff,
|
||||
Phases: 1,
|
||||
Phases: 3,
|
||||
status: api.StatusNone,
|
||||
MinCurrent: 6, // A
|
||||
MaxCurrent: 16, // A
|
||||
|
|
@ -345,7 +347,7 @@ func (lp *LoadPoint) evVehicleConnectHandler() {
|
|||
lp.startVehicleDetection()
|
||||
|
||||
// immediately allow pv mode activity
|
||||
lp.pvDisableTimer()
|
||||
lp.elapsePVTimer()
|
||||
|
||||
lp.pushEvent(evVehicleConnect)
|
||||
}
|
||||
|
|
@ -386,7 +388,7 @@ func (lp *LoadPoint) evChargeCurrentHandler(current float64) {
|
|||
// If physical charge meter is present this handler is not used.
|
||||
// The actual value is published by the evChargeCurrentHandler
|
||||
func (lp *LoadPoint) evChargeCurrentWrappedMeterHandler(current float64) {
|
||||
power := current * float64(lp.Phases) * Voltage
|
||||
power := current * float64(lp.activePhases) * Voltage
|
||||
|
||||
if !lp.enabled || lp.GetStatus() != api.StatusC {
|
||||
// if disabled we cannot be charging
|
||||
|
|
@ -418,6 +420,9 @@ func (lp *LoadPoint) Prepare(uiChan chan<- util.Param, pushChan chan<- push.Even
|
|||
lp.pushChan = pushChan
|
||||
lp.lpChan = lpChan
|
||||
|
||||
// assume all phases are active
|
||||
lp.activePhases = lp.Phases
|
||||
|
||||
// event handlers
|
||||
_ = lp.bus.Subscribe(evChargeStart, lp.evChargeStartHandler)
|
||||
_ = lp.bus.Subscribe(evChargeStop, lp.evChargeStopHandler)
|
||||
|
|
@ -430,7 +435,8 @@ func (lp *LoadPoint) Prepare(uiChan chan<- util.Param, pushChan chan<- push.Even
|
|||
lp.publish("minCurrent", lp.MinCurrent)
|
||||
lp.publish("maxCurrent", lp.MaxCurrent)
|
||||
lp.publish("phases", lp.Phases)
|
||||
lp.publish("activePhases", lp.Phases)
|
||||
lp.publish("activePhases", lp.activePhases)
|
||||
lp.publish("hasVehicle", len(lp.vehicles) > 0)
|
||||
|
||||
lp.Lock()
|
||||
lp.publish("mode", lp.Mode)
|
||||
|
|
@ -506,10 +512,12 @@ func (lp *LoadPoint) setLimit(chargeCurrent float64, force bool) (err error) {
|
|||
if err == nil {
|
||||
lp.chargeCurrent = chargeCurrent
|
||||
lp.bus.Publish(evChargeCurrent, chargeCurrent)
|
||||
} else {
|
||||
err = fmt.Errorf("max charge current %.2g: %w", chargeCurrent, err)
|
||||
}
|
||||
}
|
||||
|
||||
// set enabled
|
||||
// set enabled/disabled
|
||||
if enabled := chargeCurrent >= lp.GetMinCurrent(); enabled != lp.enabled && err == nil {
|
||||
if remaining := (lp.GuardDuration - lp.clock.Since(lp.guardUpdated)).Truncate(time.Second); remaining > 0 && !force {
|
||||
lp.log.DEBUG.Printf("charger %s: contactor delay %v", status[enabled], remaining)
|
||||
|
|
@ -812,27 +820,161 @@ func (lp *LoadPoint) effectiveCurrent() float64 {
|
|||
return lp.chargeCurrent
|
||||
}
|
||||
|
||||
// pvDisableTimer puts the pv enable/disable timer into elapsed state
|
||||
func (lp *LoadPoint) pvDisableTimer() {
|
||||
// elapsePVTimer puts the pv enable/disable timer into elapsed state
|
||||
func (lp *LoadPoint) elapsePVTimer() {
|
||||
lp.pvTimer = lp.clock.Now().Add(-lp.Disable.Delay)
|
||||
lp.guardUpdated = lp.clock.Now().Add(-lp.GuardDuration)
|
||||
}
|
||||
|
||||
// scalePhasesIfAvailable scales if api.ChargePhases is available
|
||||
func (lp *LoadPoint) scalePhasesIfAvailable(phases int) error {
|
||||
err := lp.scalePhases(phases)
|
||||
if errors.Is(err, api.ErrNotAvailable) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// scalePhases adjusts the number of active phases and returns the appropriate charging current.
|
||||
// Returns api.ErrNotAvailable if api.ChargePhases is not available.
|
||||
func (lp *LoadPoint) scalePhases(phases int) error {
|
||||
if phases != 1 && phases != 3 {
|
||||
return fmt.Errorf("invalid number of phases: %d", phases)
|
||||
}
|
||||
|
||||
cp, ok := lp.charger.(api.ChargePhases)
|
||||
if !ok {
|
||||
return api.ErrNotAvailable
|
||||
}
|
||||
|
||||
lp.Lock()
|
||||
if lp.Phases != int64(phases) {
|
||||
lp.Unlock()
|
||||
|
||||
// disable charger - this will also stop the car charging using the api if available
|
||||
if err := lp.setLimit(0, true); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// switch phases
|
||||
if err := cp.Phases1p3p(phases); err != nil {
|
||||
return fmt.Errorf("switch phases: %w", err)
|
||||
}
|
||||
|
||||
lp.Lock()
|
||||
lp.Phases = int64(phases)
|
||||
lp.publish("phases", lp.Phases)
|
||||
|
||||
// disable phase timer
|
||||
lp.phaseTimer = time.Time{}
|
||||
|
||||
// allow pv mode to re-enable charger right away
|
||||
lp.elapsePVTimer()
|
||||
}
|
||||
lp.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// pvScalePhases switches phases if necessary and returns if switch occured
|
||||
func (lp *LoadPoint) pvScalePhases(availablePower, minCurrent, maxCurrent float64) bool {
|
||||
var waiting bool
|
||||
|
||||
phases := lp.GetPhases()
|
||||
targetCurrent := availablePower / Voltage / float64(lp.activePhases)
|
||||
|
||||
lp.log.DEBUG.Printf("!!pvScalePhases available power %.0f for target current %.1f @ %dp/%dp", availablePower, targetCurrent, lp.activePhases, phases)
|
||||
if lp.phaseTimer.IsZero() {
|
||||
lp.log.DEBUG.Printf("!!pvScalePhases timer empty")
|
||||
} else {
|
||||
lp.log.DEBUG.Printf("!!pvScalePhases timer remaining: %v", lp.clock.Since(lp.phaseTimer).Truncate(time.Second))
|
||||
}
|
||||
|
||||
// scale down phases
|
||||
if targetCurrent < minCurrent && phases > 1 && lp.activePhases > 1 {
|
||||
lp.log.DEBUG.Printf("available power below %dp min threshold of %.0fW", lp.activePhases, float64(lp.activePhases)*Voltage*minCurrent)
|
||||
|
||||
if lp.phaseTimer.IsZero() {
|
||||
lp.log.DEBUG.Printf("start phase disable timer: %v", lp.Disable.Delay)
|
||||
lp.phaseTimer = lp.clock.Now()
|
||||
}
|
||||
|
||||
elapsed := lp.clock.Since(lp.phaseTimer)
|
||||
if elapsed >= lp.Disable.Delay {
|
||||
lp.log.DEBUG.Println("phase disable timer elapsed")
|
||||
if err := lp.scalePhases(1); err == nil {
|
||||
lp.log.DEBUG.Printf("switched phases: 1p @ %.0fW", availablePower)
|
||||
return true
|
||||
} else {
|
||||
lp.log.ERROR.Printf("switch phases: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
waiting = true
|
||||
lp.log.DEBUG.Printf("phase disable timer remaining: %v", (lp.Disable.Delay - elapsed).Round(time.Second))
|
||||
}
|
||||
|
||||
// scale up phases
|
||||
if min3pCurrent := powerToCurrent(availablePower, 3); min3pCurrent >= minCurrent && phases == 1 {
|
||||
lp.log.DEBUG.Printf("available power above 3p min threshold of %.0fW", 3*Voltage*minCurrent)
|
||||
|
||||
if lp.phaseTimer.IsZero() {
|
||||
lp.log.DEBUG.Printf("start phase enable timer: %v", lp.Enable.Delay)
|
||||
lp.phaseTimer = lp.clock.Now()
|
||||
}
|
||||
|
||||
elapsed := lp.clock.Since(lp.phaseTimer)
|
||||
if elapsed >= lp.Disable.Delay {
|
||||
lp.log.DEBUG.Println("phase enable timer elapsed")
|
||||
if err := lp.scalePhases(3); err == nil {
|
||||
lp.log.DEBUG.Printf("switched phases: 3p @ %.0fW", availablePower)
|
||||
return true
|
||||
} else {
|
||||
lp.log.ERROR.Printf("switch phases: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
waiting = true
|
||||
lp.log.DEBUG.Printf("phase enable timer remaining: %v", (lp.Disable.Delay - elapsed).Round(time.Second))
|
||||
}
|
||||
|
||||
// reset timer to disabled state
|
||||
if !waiting && !lp.phaseTimer.IsZero() {
|
||||
lp.log.DEBUG.Printf("phase timer reset")
|
||||
lp.phaseTimer = time.Time{}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// pvMaxCurrent calculates the maximum target current for PV mode
|
||||
func (lp *LoadPoint) pvMaxCurrent(mode api.ChargeMode, sitePower float64) float64 {
|
||||
// read only once to simplify testing
|
||||
minCurrent := lp.GetMinCurrent()
|
||||
maxCurrent := lp.GetMaxCurrent()
|
||||
|
||||
// calculate target charge current from delta power and actual current
|
||||
effectiveCurrent := lp.effectiveCurrent()
|
||||
deltaCurrent := powerToCurrent(-sitePower, lp.Phases)
|
||||
targetCurrent := math.Max(math.Min(effectiveCurrent+deltaCurrent, lp.GetMaxCurrent()), 0)
|
||||
deltaCurrent := powerToCurrent(-sitePower, lp.activePhases)
|
||||
targetCurrent := math.Max(effectiveCurrent+deltaCurrent, 0)
|
||||
|
||||
lp.log.DEBUG.Printf("max charge current: %.1fA = %.1fA + %.1fA (%.0fW @ %dp)", targetCurrent, effectiveCurrent, deltaCurrent, sitePower, lp.Phases)
|
||||
lp.log.DEBUG.Printf("max charge current: %.1fA = %.1fA + %.1fA (%.0fW @ %dp)", targetCurrent, effectiveCurrent, deltaCurrent, sitePower, lp.activePhases)
|
||||
|
||||
// switch phases up/down
|
||||
if _, ok := lp.charger.(api.ChargePhases); ok {
|
||||
availablePower := -sitePower + lp.chargePower
|
||||
|
||||
// in case of scaling, keep charger disabled for this cycle
|
||||
if lp.pvScalePhases(availablePower, minCurrent, maxCurrent) {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// in MinPV mode return at least minCurrent
|
||||
minCurrent := lp.GetMinCurrent()
|
||||
if mode == api.ModeMinPV && targetCurrent < minCurrent {
|
||||
return minCurrent
|
||||
}
|
||||
|
||||
// read only once to simplify testing
|
||||
if mode == api.ModePV && lp.enabled && targetCurrent < minCurrent {
|
||||
// kick off disable sequence
|
||||
if sitePower >= lp.Disable.Threshold {
|
||||
|
|
@ -889,8 +1031,13 @@ func (lp *LoadPoint) pvMaxCurrent(mode api.ChargeMode, sitePower float64) float6
|
|||
}
|
||||
|
||||
// reset timer to disabled state
|
||||
lp.log.DEBUG.Printf("pv timer reset")
|
||||
lp.pvTimer = time.Time{}
|
||||
if !lp.pvTimer.IsZero() {
|
||||
lp.log.DEBUG.Printf("pv timer reset")
|
||||
lp.pvTimer = time.Time{}
|
||||
}
|
||||
|
||||
// cap at maximum current
|
||||
targetCurrent = math.Min(targetCurrent, maxCurrent)
|
||||
|
||||
return targetCurrent
|
||||
}
|
||||
|
|
@ -920,6 +1067,17 @@ func (lp *LoadPoint) updateChargeCurrents() {
|
|||
lp.chargeCurrents = nil
|
||||
phaseMeter, ok := lp.chargeMeter.(api.MeterCurrent)
|
||||
if !ok {
|
||||
// guess active phases from power consumption
|
||||
// assumes that chargePower has been updated before
|
||||
if lp.charging() && lp.chargeCurrent > 0 {
|
||||
phases := int64(math.Round(lp.chargePower / Voltage / lp.chargeCurrent))
|
||||
if phases >= 1 && phases <= 3 {
|
||||
lp.activePhases = phases
|
||||
lp.log.DEBUG.Printf("detected phases: %dp (%.1fA @ %.0fW)", lp.activePhases, lp.chargeCurrent, lp.chargePower)
|
||||
lp.publish("activePhases", lp.activePhases)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -941,11 +1099,10 @@ func (lp *LoadPoint) updateChargeCurrents() {
|
|||
}
|
||||
}
|
||||
|
||||
if phases > 0 {
|
||||
lp.Phases = phases
|
||||
lp.log.DEBUG.Printf("detected phases: %dp %.3gA", lp.Phases, lp.chargeCurrents)
|
||||
|
||||
lp.publish("activePhases", lp.Phases)
|
||||
if phases >= 1 {
|
||||
lp.activePhases = phases
|
||||
lp.log.DEBUG.Printf("detected phases: %dp %.3gA", lp.activePhases, lp.chargeCurrents)
|
||||
lp.publish("activePhases", lp.activePhases)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1121,11 +1278,17 @@ func (lp *LoadPoint) Update(sitePower float64, cheap bool) {
|
|||
err = lp.setLimit(0, true)
|
||||
|
||||
case lp.minSocNotReached():
|
||||
err = lp.setLimit(lp.GetMaxCurrent(), true)
|
||||
lp.pvDisableTimer() // let PV mode disable immediately afterwards
|
||||
// 3p if available
|
||||
if err = lp.scalePhasesIfAvailable(3); err == nil {
|
||||
err = lp.setLimit(lp.GetMaxCurrent(), true)
|
||||
}
|
||||
lp.elapsePVTimer() // let PV mode disable immediately afterwards
|
||||
|
||||
case mode == api.ModeNow:
|
||||
err = lp.setLimit(lp.GetMaxCurrent(), true)
|
||||
// 3p if available
|
||||
if err = lp.scalePhasesIfAvailable(3); err == nil {
|
||||
err = lp.setLimit(lp.GetMaxCurrent(), true)
|
||||
}
|
||||
|
||||
// target charging
|
||||
case lp.socTimer.StartRequired():
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ func (a *adapter) SocEstimator() *soc.Estimator {
|
|||
}
|
||||
|
||||
func (a *adapter) ActivePhases() int64 {
|
||||
return a.lp.Phases
|
||||
return a.lp.activePhases
|
||||
}
|
||||
|
||||
func (a *adapter) Voltage() float64 {
|
||||
|
|
|
|||
|
|
@ -27,6 +27,8 @@ type LoadPointAPI interface {
|
|||
SetTargetSoC(int) error
|
||||
GetMinSoC() int
|
||||
SetMinSoC(int) error
|
||||
GetPhases() int
|
||||
SetPhases(int) error
|
||||
SetTargetCharge(time.Time, int)
|
||||
RemoteControl(string, RemoteDemand)
|
||||
|
||||
|
|
@ -67,7 +69,7 @@ func (lp *LoadPoint) SetMode(mode api.ChargeMode) {
|
|||
lp.publish("mode", mode)
|
||||
|
||||
// immediately allow pv mode activity
|
||||
lp.pvDisableTimer()
|
||||
lp.elapsePVTimer()
|
||||
|
||||
lp.requestUpdate()
|
||||
}
|
||||
|
|
@ -129,6 +131,19 @@ func (lp *LoadPoint) SetMinSoC(soc int) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// GetPhases returns loadpoint enabled phases
|
||||
func (lp *LoadPoint) GetPhases() int {
|
||||
lp.Lock()
|
||||
defer lp.Unlock()
|
||||
|
||||
return int(lp.Phases)
|
||||
}
|
||||
|
||||
// SetPhases sets loadpoint enabled phases
|
||||
func (lp *LoadPoint) SetPhases(phases int) error {
|
||||
return lp.scalePhases(phases)
|
||||
}
|
||||
|
||||
// SetTargetCharge sets loadpoint charge targetSoC
|
||||
func (lp *LoadPoint) SetTargetCharge(finishAt time.Time, targetSoC int) {
|
||||
lp.Lock()
|
||||
|
|
@ -221,5 +236,5 @@ func (lp *LoadPoint) GetMinPower() float64 {
|
|||
|
||||
// GetMaxPower returns the max loadpoint power taking active phases into account
|
||||
func (lp *LoadPoint) GetMaxPower() float64 {
|
||||
return Voltage * lp.GetMaxCurrent() * float64(lp.Phases)
|
||||
return Voltage * lp.GetMaxCurrent() * float64(lp.GetPhases())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ func attachListeners(t *testing.T, lp *LoadPoint) {
|
|||
func TestNew(t *testing.T) {
|
||||
lp := NewLoadPoint(util.NewLogger("foo"))
|
||||
|
||||
if lp.Phases != 1 {
|
||||
if lp.Phases != 3 {
|
||||
t.Errorf("Phases %v", lp.Phases)
|
||||
}
|
||||
if lp.MinCurrent != minA {
|
||||
|
|
@ -295,12 +295,13 @@ func TestPVHysteresis(t *testing.T) {
|
|||
|
||||
Voltage = 100
|
||||
lp := &LoadPoint{
|
||||
log: util.NewLogger("foo"),
|
||||
clock: clck,
|
||||
charger: charger,
|
||||
MinCurrent: minA,
|
||||
MaxCurrent: maxA,
|
||||
Phases: 10,
|
||||
log: util.NewLogger("foo"),
|
||||
clock: clck,
|
||||
charger: charger,
|
||||
MinCurrent: minA,
|
||||
MaxCurrent: maxA,
|
||||
Phases: 10,
|
||||
activePhases: 10,
|
||||
Enable: ThresholdConfig{
|
||||
Threshold: tc.enable,
|
||||
Delay: dt,
|
||||
|
|
@ -341,11 +342,12 @@ func TestPVHysteresisForStatusOtherThanC(t *testing.T) {
|
|||
|
||||
Voltage = 100
|
||||
lp := &LoadPoint{
|
||||
log: util.NewLogger("foo"),
|
||||
clock: clck,
|
||||
MinCurrent: minA,
|
||||
MaxCurrent: maxA,
|
||||
Phases: 10,
|
||||
log: util.NewLogger("foo"),
|
||||
clock: clck,
|
||||
MinCurrent: minA,
|
||||
MaxCurrent: maxA,
|
||||
Phases: 10,
|
||||
activePhases: 10,
|
||||
}
|
||||
|
||||
// not connected, test PV mode logic short-circuited
|
||||
|
|
@ -811,3 +813,114 @@ func TestVehicleDetectByID(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestScalePhases(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
charger := &struct {
|
||||
*mock.MockCharger
|
||||
*mock.MockChargePhases
|
||||
}{
|
||||
mock.NewMockCharger(ctrl),
|
||||
mock.NewMockChargePhases(ctrl),
|
||||
}
|
||||
|
||||
dt := time.Minute
|
||||
Voltage = 230 // V
|
||||
|
||||
tc := []struct {
|
||||
desc string
|
||||
phases, activePhases int
|
||||
availablePower float64
|
||||
toPhases int
|
||||
res bool
|
||||
prepare func(lp *LoadPoint)
|
||||
}{
|
||||
// switch up from 1p/1p configured/active
|
||||
{"1/1->3, not enough power", 1, 1, 0, 1, false, nil},
|
||||
{"1/1->3, kickoff", 1, 1, 3 * Voltage * minA, 1, false, func(lp *LoadPoint) {
|
||||
lp.phaseTimer = time.Time{}
|
||||
}},
|
||||
{"1/1->3, timer running", 1, 1, 3 * Voltage * minA, 1, false, func(lp *LoadPoint) {
|
||||
lp.phaseTimer = lp.clock.Now()
|
||||
}},
|
||||
{"1/1->3, timer elapsed", 1, 1, 3 * Voltage * minA, 3, true, func(lp *LoadPoint) {
|
||||
lp.phaseTimer = lp.clock.Now().Add(-dt)
|
||||
}},
|
||||
|
||||
// omit to switch up (again) from 3p/1p configured/active
|
||||
{"3/1->3, not enough power", 3, 1, 0, 3, false, nil},
|
||||
{"3/1->3, kickoff", 3, 1, 3 * Voltage * minA, 3, false, func(lp *LoadPoint) {
|
||||
lp.phaseTimer = time.Time{}
|
||||
}},
|
||||
{"3/1->3, timer running", 3, 1, 3 * Voltage * minA, 3, false, func(lp *LoadPoint) {
|
||||
lp.phaseTimer = lp.clock.Now()
|
||||
}},
|
||||
{"3/1->3, timer elapsed", 3, 1, 3 * Voltage * minA, 3, false, func(lp *LoadPoint) {
|
||||
lp.phaseTimer = lp.clock.Now().Add(-dt)
|
||||
}},
|
||||
|
||||
// omit to switch down from 3p/1p configured/active
|
||||
{"3/1->1, not enough power", 3, 1, 0, 3, false, nil},
|
||||
{"3/1->1, kickoff", 3, 1, 1 * Voltage * minA, 3, false, func(lp *LoadPoint) {
|
||||
lp.phaseTimer = time.Time{}
|
||||
}},
|
||||
{"3/1->1, timer running", 3, 1, 1 * Voltage * minA, 3, false, func(lp *LoadPoint) {
|
||||
lp.phaseTimer = lp.clock.Now()
|
||||
}},
|
||||
{"3/1->1, timer elapsed", 3, 1, 1 * Voltage * minA, 3, false, func(lp *LoadPoint) {
|
||||
lp.phaseTimer = lp.clock.Now().Add(-dt)
|
||||
}},
|
||||
|
||||
// switch down from 3p/3p configured/active
|
||||
{"3/3->1, enough power", 3, 3, 1 * Voltage * maxA, 3, false, nil},
|
||||
{"3/3->1, kickoff", 3, 3, 1 * Voltage * maxA, 3, false, func(lp *LoadPoint) {
|
||||
lp.phaseTimer = time.Time{}
|
||||
}},
|
||||
{"3/3->1, timer running", 3, 3, 1 * Voltage * maxA, 3, false, func(lp *LoadPoint) {
|
||||
lp.phaseTimer = lp.clock.Now()
|
||||
}},
|
||||
{"3/3->1, timer elapsed", 3, 3, 1 * Voltage * maxA, 1, true, func(lp *LoadPoint) {
|
||||
lp.phaseTimer = lp.clock.Now().Add(-dt)
|
||||
}},
|
||||
{"3/3->1, switch executed", 1, 3, 1 * Voltage * maxA, 1, false, func(lp *LoadPoint) {
|
||||
lp.phaseTimer = lp.clock.Now().Add(-dt)
|
||||
}},
|
||||
}
|
||||
|
||||
for _, tc := range tc {
|
||||
t.Logf("%+v", tc)
|
||||
clock := clock.NewMock()
|
||||
|
||||
lp := &LoadPoint{
|
||||
log: util.NewLogger("foo"),
|
||||
clock: clock,
|
||||
charger: charger,
|
||||
MinCurrent: minA,
|
||||
MaxCurrent: maxA,
|
||||
Phases: int64(tc.phases),
|
||||
activePhases: int64(tc.activePhases),
|
||||
Enable: ThresholdConfig{
|
||||
Delay: dt,
|
||||
},
|
||||
Disable: ThresholdConfig{
|
||||
Delay: dt,
|
||||
},
|
||||
}
|
||||
|
||||
if tc.prepare != nil {
|
||||
tc.prepare(lp)
|
||||
}
|
||||
|
||||
if tc.res {
|
||||
charger.MockChargePhases.EXPECT().Phases1p3p(tc.toPhases).Return(nil)
|
||||
}
|
||||
|
||||
if res := lp.pvScalePhases(tc.availablePower, minA, maxA); tc.res != res {
|
||||
t.Errorf("expected %v, got %v", tc.res, res)
|
||||
} else {
|
||||
if lp.Phases != int64(tc.toPhases) {
|
||||
t.Errorf("expected %dp, got %dp", tc.toPhases, lp.Phases)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: github.com/andig/evcc/api (interfaces: Charger,ChargeState,Identifier,Meter,MeterEnergy,Vehicle,ChargeRater,Battery)
|
||||
// Source: github.com/andig/evcc/api (interfaces: Charger,ChargeState,ChargePhases,Identifier,Meter,MeterEnergy,Vehicle,ChargeRater,Battery)
|
||||
|
||||
// Package mock is a generated GoMock package.
|
||||
package mock
|
||||
|
|
@ -130,6 +130,43 @@ func (mr *MockChargeStateMockRecorder) Status() *gomock.Call {
|
|||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Status", reflect.TypeOf((*MockChargeState)(nil).Status))
|
||||
}
|
||||
|
||||
// MockChargePhases is a mock of ChargePhases interface.
|
||||
type MockChargePhases struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockChargePhasesMockRecorder
|
||||
}
|
||||
|
||||
// MockChargePhasesMockRecorder is the mock recorder for MockChargePhases.
|
||||
type MockChargePhasesMockRecorder struct {
|
||||
mock *MockChargePhases
|
||||
}
|
||||
|
||||
// NewMockChargePhases creates a new mock instance.
|
||||
func NewMockChargePhases(ctrl *gomock.Controller) *MockChargePhases {
|
||||
mock := &MockChargePhases{ctrl: ctrl}
|
||||
mock.recorder = &MockChargePhasesMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||
func (m *MockChargePhases) EXPECT() *MockChargePhasesMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// Phases1p3p mocks base method.
|
||||
func (m *MockChargePhases) Phases1p3p(arg0 int) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Phases1p3p", arg0)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// Phases1p3p indicates an expected call of Phases1p3p.
|
||||
func (mr *MockChargePhasesMockRecorder) Phases1p3p(arg0 interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Phases1p3p", reflect.TypeOf((*MockChargePhases)(nil).Phases1p3p), arg0)
|
||||
}
|
||||
|
||||
// MockIdentifier is a mock of Identifier interface.
|
||||
type MockIdentifier struct {
|
||||
ctrl *gomock.Controller
|
||||
|
|
|
|||
|
|
@ -21,6 +21,10 @@ import (
|
|||
// Assets is the embedded assets file system
|
||||
var Assets fs.FS
|
||||
|
||||
type errorJSON struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
type chargeModeJSON struct {
|
||||
Mode api.ChargeMode `json:"mode"`
|
||||
}
|
||||
|
|
@ -33,6 +37,10 @@ type minSoCJSON struct {
|
|||
MinSoC int `json:"minSoC"`
|
||||
}
|
||||
|
||||
type phasesJSON struct {
|
||||
Phases int `json:"phases"`
|
||||
}
|
||||
|
||||
type route struct {
|
||||
Methods []string
|
||||
Pattern string
|
||||
|
|
@ -163,6 +171,7 @@ func ChargeModeHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
|
|||
mode := api.ChargeModeString(modeS)
|
||||
if mode == "" || string(mode) != modeS || !ok {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
jsonResponse(w, r, errorJSON{Error: "invalid mode"})
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -195,6 +204,7 @@ func TargetSoCHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
|
|||
|
||||
if !ok || err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
jsonResponse(w, r, errorJSON{Error: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -225,6 +235,7 @@ func MinSoCHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
|
|||
|
||||
if !ok || err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
jsonResponse(w, r, errorJSON{Error: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -233,6 +244,37 @@ func MinSoCHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
// CurrentPhasesHandler returns current minimum soc
|
||||
func CurrentPhasesHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
res := phasesJSON{Phases: loadpoint.GetPhases()}
|
||||
jsonResponse(w, r, res)
|
||||
}
|
||||
}
|
||||
|
||||
// PhasesHandler updates minimum soc
|
||||
func PhasesHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
|
||||
phasesS, ok := vars["phases"]
|
||||
phases, err := strconv.ParseInt(phasesS, 10, 32)
|
||||
|
||||
if ok && err == nil {
|
||||
err = loadpoint.SetPhases(int(phases))
|
||||
}
|
||||
|
||||
if !ok || err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
jsonResponse(w, r, errorJSON{Error: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
res := phasesJSON{Phases: loadpoint.GetPhases()}
|
||||
jsonResponse(w, r, res)
|
||||
}
|
||||
}
|
||||
|
||||
// RemoteDemandHandler updates minimum soc
|
||||
func RemoteDemandHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -249,6 +291,7 @@ func RemoteDemandHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
|
|||
|
||||
if !ok || err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
jsonResponse(w, r, errorJSON{Error: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -293,8 +336,8 @@ func TargetChargeHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
|
|||
timeV, err := time.ParseInLocation("2006-01-02T15:04:05", timeS, timezone())
|
||||
|
||||
if !ok || err != nil {
|
||||
log.DEBUG.Printf("parse time: %v", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
jsonResponse(w, r, errorJSON{Error: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -372,6 +415,8 @@ func NewHTTPd(url string, site core.SiteAPI, hub *SocketHub, cache *util.Cache)
|
|||
"settargetsoc": {[]string{"POST", "OPTIONS"}, "/targetsoc/{soc:[0-9]+}", TargetSoCHandler(lp)},
|
||||
"getminsoc": {[]string{"GET"}, "/minsoc", CurrentMinSoCHandler(lp)},
|
||||
"setminsoc": {[]string{"POST", "OPTIONS"}, "/minsoc/{soc:[0-9]+}", MinSoCHandler(lp)},
|
||||
"getphases": {[]string{"GET"}, "/phases", CurrentPhasesHandler(lp)},
|
||||
"setphases": {[]string{"POST", "OPTIONS"}, "/phases/{phases:[0-9]+}", PhasesHandler(lp)},
|
||||
"settargetcharge": {[]string{"POST", "OPTIONS"}, "/targetcharge/{soc:[0-9]+}/{time:[0-9TZ:-]+}", TargetChargeHandler(lp)},
|
||||
"remotedemand": {[]string{"POST", "OPTIONS"}, "/remotedemand/{demand:[a-z]+}/{source}", RemoteDemandHandler(lp)},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -80,6 +80,12 @@ func (m *MQTT) listenSetters(topic string, apiHandler core.LoadPointAPI) {
|
|||
_ = apiHandler.SetTargetSoC(soc)
|
||||
}
|
||||
})
|
||||
m.Handler.Listen(topic+"/phases/set", func(payload string) {
|
||||
phases, err := strconv.Atoi(payload)
|
||||
if err == nil {
|
||||
_ = apiHandler.SetPhases(phases)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Run starts the MQTT publisher for the MQTT API
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue