Ocpp: move get/set charging profile to connector (#15660)

This commit is contained in:
premultiply 2024-09-05 18:47:19 +02:00 • committed by GitHub
parent 04c5429011
commit 16107f9c2b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 58 additions and 45 deletions

View file

@ -13,7 +13,6 @@ import (
"github.com/evcc-io/evcc/core/loadpoint"
"github.com/evcc-io/evcc/util"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/core"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/smartcharging"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/types"
)
@ -289,7 +288,7 @@ func (c *OCPP) Enabled() (bool, error) {
}
// fallback to querying the active charging profile schedule limit
if v, err := c.getScheduleLimit(); err == nil {
if v, err := c.conn.GetScheduleLimit(60); err == nil {
return v > 0, nil
}
@ -329,22 +328,9 @@ func (c *OCPP) initTransaction() error {
return ocpp.Wait(err, rc)
}
func (c *OCPP) setChargingProfile(profile *types.ChargingProfile) error {
rc := make(chan error, 1)
err := ocpp.Instance().SetChargingProfile(c.cp.ID(), func(resp *smartcharging.SetChargingProfileConfirmation, err error) {
if err == nil && resp != nil && resp.Status != smartcharging.ChargingProfileStatusAccepted {
err = errors.New(string(resp.Status))
}
rc <- err
}, c.conn.ID(), profile)
return ocpp.Wait(err, rc)
}
// setCurrent sets the TxDefaultChargingProfile with given current
func (c *OCPP) setCurrent(current float64) error {
err := c.setChargingProfile(c.createTxDefaultChargingProfile(math.Trunc(10*current) / 10))
err := c.conn.SetChargingProfile(c.createTxDefaultChargingProfile(math.Trunc(10*current) / 10))
if err != nil {
err = fmt.Errorf("set charging profile: %w", err)
}
@ -352,35 +338,6 @@ func (c *OCPP) setCurrent(current float64) error {
return err
}
// getScheduleLimit queries the current or power limit the charge point is currently set to offer
func (c *OCPP) getScheduleLimit() (float64, error) {
const duration = 60 // duration of requested schedule in seconds
var limit float64
rc := make(chan error, 1)
err := ocpp.Instance().GetCompositeSchedule(c.cp.ID(), func(resp *smartcharging.GetCompositeScheduleConfirmation, err error) {
if err == nil && resp != nil && resp.Status != smartcharging.GetCompositeScheduleStatusAccepted {
err = errors.New(string(resp.Status))
}
if err == nil {
if resp.ChargingSchedule != nil && len(resp.ChargingSchedule.ChargingSchedulePeriod) > 0 {
// return first (current) period limit
limit = resp.ChargingSchedule.ChargingSchedulePeriod[0].Limit
} else {
err = fmt.Errorf("invalid ChargingSchedule")
}
}
rc <- err
}, c.conn.ID(), duration)
err = ocpp.Wait(err, rc)
return limit, err
}
// createTxDefaultChargingProfile returns a TxDefaultChargingProfile with given current
func (c *OCPP) createTxDefaultChargingProfile(current float64) *types.ChargingProfile {
phases := c.phases

View file

@ -71,6 +71,27 @@ func (conn *Connector) TriggerMessageRequest(feature remotetrigger.MessageTrigge
})
}
func (conn *Connector) SetChargingProfile(profile *types.ChargingProfile) error {
return Instance().SetChargingProfileRequest(conn.cp.ID(), conn.id, profile)
}
// getScheduleLimit queries the current or power limit the charge point is currently set to offer
func (conn *Connector) GetScheduleLimit(duration int) (float64, error) {
var limit float64
schedule, err := Instance().GetCompositeScheduleRequest(conn.cp.ID(), conn.id, duration)
if err == nil {
if schedule != nil && len(schedule.ChargingSchedulePeriod) > 0 {
// return first (current) period limit
limit = schedule.ChargingSchedulePeriod[0].Limit
} else {
err = fmt.Errorf("invalid ChargingSchedule")
}
}
return limit, err
}
// WatchDog triggers meter values messages if older than timeout.
// Must be wrapped in a goroutine.
func (conn *Connector) WatchDog(timeout time.Duration) {

View file

@ -6,6 +6,8 @@ import (
"github.com/lorenzodonini/ocpp-go/ocpp1.6/core"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/firmware"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/remotetrigger"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/smartcharging"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/types"
)
// cs actions
@ -52,6 +54,39 @@ func (cs *CS) ChangeAvailabilityRequest(id string, connector int, availabilityTy
return Wait(err, rc)
}
func (cs *CS) SetChargingProfileRequest(id string, connector int, profile *types.ChargingProfile) error {
rc := make(chan error, 1)
err := cs.SetChargingProfile(id, func(request *smartcharging.SetChargingProfileConfirmation, err error) {
if err == nil && request != nil && request.Status != smartcharging.ChargingProfileStatusAccepted {
err = errors.New(string(request.Status))
}
rc <- err
}, connector, profile)
return Wait(err, rc)
}
func (cs *CS) GetCompositeScheduleRequest(id string, connector int, duration int) (*types.ChargingSchedule, error) {
var schedule *types.ChargingSchedule
rc := make(chan error, 1)
err := cs.GetCompositeSchedule(id, func(request *smartcharging.GetCompositeScheduleConfirmation, err error) {
if err == nil && request != nil && request.Status != smartcharging.GetCompositeScheduleStatusAccepted {
err = errors.New(string(request.Status))
}
schedule = request.ChargingSchedule
rc <- err
}, connector, duration)
err = Wait(err, rc)
return schedule, err
}
// cp actions
func (cs *CS) OnAuthorize(id string, request *core.AuthorizeRequest) (*core.AuthorizeConfirmation, error) {