chore: refactor ocpp handlers and functions from cs to cp (#15967)
This commit is contained in:
parent
f43cee168e
commit
a6f86051a9
8 changed files with 185 additions and 182 deletions
|
|
@ -290,7 +290,7 @@ func (c *OCPP) Enable(enable bool) error {
|
|||
|
||||
// setCurrent sets the TxDefaultChargingProfile with given current
|
||||
func (c *OCPP) setCurrent(current float64) error {
|
||||
err := c.conn.SetChargingProfile(c.createTxDefaultChargingProfile(math.Trunc(10*current) / 10))
|
||||
err := c.conn.SetChargingProfileRequest(c.createTxDefaultChargingProfile(math.Trunc(10*current) / 10))
|
||||
if err != nil {
|
||||
err = fmt.Errorf("set charging profile: %w", err)
|
||||
}
|
||||
|
|
@ -381,7 +381,7 @@ func (c *OCPP) Diagnose() {
|
|||
}
|
||||
|
||||
fmt.Printf("\tConfiguration:\n")
|
||||
if resp, err := c.cp.GetConfiguration(); err == nil {
|
||||
if resp, err := c.cp.GetConfigurationRequest(); err == nil {
|
||||
// sort configuration keys for printing
|
||||
slices.SortFunc(resp.ConfigurationKey, func(i, j core.ConfigurationKey) int {
|
||||
return cmp.Compare(i.Key, j.Key)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package ocpp
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
|
@ -12,7 +11,6 @@ import (
|
|||
"github.com/evcc-io/evcc/api"
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/lorenzodonini/ocpp-go/ocpp1.6/core"
|
||||
"github.com/lorenzodonini/ocpp-go/ocpp1.6/remotetrigger"
|
||||
"github.com/lorenzodonini/ocpp-go/ocpp1.6/types"
|
||||
)
|
||||
|
||||
|
|
@ -66,40 +64,9 @@ func (conn *Connector) IdTag() string {
|
|||
return conn.idTag
|
||||
}
|
||||
|
||||
func (conn *Connector) TriggerMessageRequest(feature remotetrigger.MessageTrigger, f ...func(request *remotetrigger.TriggerMessageRequest)) error {
|
||||
return Instance().TriggerMessageRequest(conn.cp.ID(), feature, func(request *remotetrigger.TriggerMessageRequest) {
|
||||
request.ConnectorId = &conn.id
|
||||
for _, f := range f {
|
||||
f(request)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (conn *Connector) remoteStartTransactionRequest() {
|
||||
rc := make(chan error, 1)
|
||||
err := Instance().RemoteStartTransaction(conn.cp.ID(), func(resp *core.RemoteStartTransactionConfirmation, err error) {
|
||||
if err == nil && resp != nil && resp.Status != types.RemoteStartStopStatusAccepted {
|
||||
err = errors.New(string(resp.Status))
|
||||
}
|
||||
|
||||
rc <- err
|
||||
}, conn.remoteIdTag, func(request *core.RemoteStartTransactionRequest) {
|
||||
connector := conn.id
|
||||
request.ConnectorId = &connector
|
||||
})
|
||||
|
||||
if err := wait(err, rc); err != nil {
|
||||
conn.log.ERROR.Printf("failed to start remote transaction: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
schedule, err := Instance().GetCompositeScheduleRequest(conn.cp.ID(), conn.id, duration)
|
||||
schedule, err := conn.cp.GetCompositeScheduleRequest(conn.id, duration)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ func (conn *Connector) timestampValid(t time.Time) bool {
|
|||
return !t.Before(conn.status.Timestamp.Time)
|
||||
}
|
||||
|
||||
func (conn *Connector) StatusNotification(request *core.StatusNotificationRequest) (*core.StatusNotificationConfirmation, error) {
|
||||
func (conn *Connector) OnStatusNotification(request *core.StatusNotificationRequest) (*core.StatusNotificationConfirmation, error) {
|
||||
conn.mu.Lock()
|
||||
defer conn.mu.Unlock()
|
||||
|
||||
|
|
@ -39,7 +39,7 @@ func (conn *Connector) StatusNotification(request *core.StatusNotificationReques
|
|||
|
||||
if conn.isWaitingForAuth() {
|
||||
if conn.remoteIdTag != "" {
|
||||
conn.remoteStartTransactionRequest()
|
||||
conn.RemoteStartTransactionRequest(conn.remoteIdTag)
|
||||
} else {
|
||||
conn.log.DEBUG.Printf("waiting for local authentication")
|
||||
}
|
||||
|
|
@ -56,7 +56,7 @@ func getSampleKey(s types.SampledValue) types.Measurand {
|
|||
return s.Measurand
|
||||
}
|
||||
|
||||
func (conn *Connector) MeterValues(request *core.MeterValuesRequest) (*core.MeterValuesConfirmation, error) {
|
||||
func (conn *Connector) OnMeterValues(request *core.MeterValuesRequest) (*core.MeterValuesConfirmation, error) {
|
||||
conn.mu.Lock()
|
||||
defer conn.mu.Unlock()
|
||||
|
||||
|
|
@ -87,7 +87,7 @@ func (conn *Connector) MeterValues(request *core.MeterValuesRequest) (*core.Mete
|
|||
return new(core.MeterValuesConfirmation), nil
|
||||
}
|
||||
|
||||
func (conn *Connector) StartTransaction(request *core.StartTransactionRequest) (*core.StartTransactionConfirmation, error) {
|
||||
func (conn *Connector) OnStartTransaction(request *core.StartTransactionRequest) (*core.StartTransactionConfirmation, error) {
|
||||
conn.mu.Lock()
|
||||
defer conn.mu.Unlock()
|
||||
|
||||
|
|
@ -124,7 +124,7 @@ func (conn *Connector) assumeMeterStopped() {
|
|||
}
|
||||
}
|
||||
|
||||
func (conn *Connector) StopTransaction(request *core.StopTransactionRequest) (*core.StopTransactionConfirmation, error) {
|
||||
func (conn *Connector) OnStopTransaction(request *core.StopTransactionRequest) (*core.StopTransactionConfirmation, error) {
|
||||
conn.mu.Lock()
|
||||
defer conn.mu.Unlock()
|
||||
|
||||
|
|
|
|||
28
charger/ocpp/connector_requests.go
Normal file
28
charger/ocpp/connector_requests.go
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package ocpp
|
||||
|
||||
import (
|
||||
"github.com/lorenzodonini/ocpp-go/ocpp1.6/core"
|
||||
"github.com/lorenzodonini/ocpp-go/ocpp1.6/remotetrigger"
|
||||
"github.com/lorenzodonini/ocpp-go/ocpp1.6/smartcharging"
|
||||
"github.com/lorenzodonini/ocpp-go/ocpp1.6/types"
|
||||
)
|
||||
|
||||
func (conn *Connector) ChangeAvailabilityRequest(availabilityType core.AvailabilityType) error {
|
||||
return conn.cp.ChangeAvailabilityRequest(conn.id, availabilityType)
|
||||
}
|
||||
|
||||
func (conn *Connector) GetCompositeScheduleRequest(duration int) (*smartcharging.GetCompositeScheduleConfirmation, error) {
|
||||
return conn.cp.GetCompositeScheduleRequest(conn.id, duration)
|
||||
}
|
||||
|
||||
func (conn *Connector) RemoteStartTransactionRequest(idTag string) error {
|
||||
return conn.cp.RemoteStartTransactionRequest(conn.id, idTag)
|
||||
}
|
||||
|
||||
func (conn *Connector) SetChargingProfileRequest(profile *types.ChargingProfile) error {
|
||||
return conn.cp.SetChargingProfileRequest(conn.id, profile)
|
||||
}
|
||||
|
||||
func (conn *Connector) TriggerMessageRequest(requestedMessage remotetrigger.MessageTrigger) error {
|
||||
return conn.cp.TriggerMessageRequest(conn.id, requestedMessage)
|
||||
}
|
||||
|
|
@ -14,7 +14,7 @@ var (
|
|||
ErrInvalidTransaction = errors.New("invalid transaction")
|
||||
)
|
||||
|
||||
func (cp *CP) Authorize(request *core.AuthorizeRequest) (*core.AuthorizeConfirmation, error) {
|
||||
func (cp *CP) OnAuthorize(request *core.AuthorizeRequest) (*core.AuthorizeConfirmation, error) {
|
||||
res := &core.AuthorizeConfirmation{
|
||||
IdTagInfo: &types.IdTagInfo{
|
||||
Status: types.AuthorizationStatusAccepted,
|
||||
|
|
@ -24,7 +24,7 @@ func (cp *CP) Authorize(request *core.AuthorizeRequest) (*core.AuthorizeConfirma
|
|||
return res, nil
|
||||
}
|
||||
|
||||
func (cp *CP) BootNotification(request *core.BootNotificationRequest) (*core.BootNotificationConfirmation, error) {
|
||||
func (cp *CP) OnBootNotification(request *core.BootNotificationRequest) (*core.BootNotificationConfirmation, error) {
|
||||
res := &core.BootNotificationConfirmation{
|
||||
CurrentTime: types.Now(),
|
||||
Interval: 60,
|
||||
|
|
@ -38,27 +38,27 @@ func (cp *CP) BootNotification(request *core.BootNotificationRequest) (*core.Boo
|
|||
return res, nil
|
||||
}
|
||||
|
||||
func (cp *CP) DiagnosticStatusNotification(request *firmware.DiagnosticsStatusNotificationRequest) (*firmware.DiagnosticsStatusNotificationConfirmation, error) {
|
||||
func (cp *CP) OnDiagnosticStatusNotification(request *firmware.DiagnosticsStatusNotificationRequest) (*firmware.DiagnosticsStatusNotificationConfirmation, error) {
|
||||
return new(firmware.DiagnosticsStatusNotificationConfirmation), nil
|
||||
}
|
||||
|
||||
func (cp *CP) FirmwareStatusNotification(request *firmware.FirmwareStatusNotificationRequest) (*firmware.FirmwareStatusNotificationConfirmation, error) {
|
||||
func (cp *CP) OnFirmwareStatusNotification(request *firmware.FirmwareStatusNotificationRequest) (*firmware.FirmwareStatusNotificationConfirmation, error) {
|
||||
return new(firmware.FirmwareStatusNotificationConfirmation), nil
|
||||
}
|
||||
|
||||
func (cp *CP) StatusNotification(request *core.StatusNotificationRequest) (*core.StatusNotificationConfirmation, error) {
|
||||
func (cp *CP) OnStatusNotification(request *core.StatusNotificationRequest) (*core.StatusNotificationConfirmation, error) {
|
||||
if request == nil {
|
||||
return nil, ErrInvalidRequest
|
||||
}
|
||||
|
||||
if conn := cp.connectorByID(request.ConnectorId); conn != nil {
|
||||
return conn.StatusNotification(request)
|
||||
return conn.OnStatusNotification(request)
|
||||
}
|
||||
|
||||
return new(core.StatusNotificationConfirmation), nil
|
||||
}
|
||||
|
||||
func (cp *CP) DataTransfer(request *core.DataTransferRequest) (*core.DataTransferConfirmation, error) {
|
||||
func (cp *CP) OnDataTransfer(request *core.DataTransferRequest) (*core.DataTransferConfirmation, error) {
|
||||
res := &core.DataTransferConfirmation{
|
||||
Status: core.DataTransferStatusAccepted,
|
||||
}
|
||||
|
|
@ -66,7 +66,7 @@ func (cp *CP) DataTransfer(request *core.DataTransferRequest) (*core.DataTransfe
|
|||
return res, nil
|
||||
}
|
||||
|
||||
func (cp *CP) Heartbeat(request *core.HeartbeatRequest) (*core.HeartbeatConfirmation, error) {
|
||||
func (cp *CP) OnHeartbeat(request *core.HeartbeatRequest) (*core.HeartbeatConfirmation, error) {
|
||||
res := &core.HeartbeatConfirmation{
|
||||
CurrentTime: types.Now(),
|
||||
}
|
||||
|
|
@ -74,7 +74,7 @@ func (cp *CP) Heartbeat(request *core.HeartbeatRequest) (*core.HeartbeatConfirma
|
|||
return res, nil
|
||||
}
|
||||
|
||||
func (cp *CP) MeterValues(request *core.MeterValuesRequest) (*core.MeterValuesConfirmation, error) {
|
||||
func (cp *CP) OnMeterValues(request *core.MeterValuesRequest) (*core.MeterValuesConfirmation, error) {
|
||||
if request == nil {
|
||||
return nil, ErrInvalidRequest
|
||||
}
|
||||
|
|
@ -86,19 +86,19 @@ func (cp *CP) MeterValues(request *core.MeterValuesRequest) (*core.MeterValuesCo
|
|||
}
|
||||
|
||||
if conn := cp.connectorByID(request.ConnectorId); conn != nil {
|
||||
conn.MeterValues(request)
|
||||
conn.OnMeterValues(request)
|
||||
}
|
||||
|
||||
return new(core.MeterValuesConfirmation), nil
|
||||
}
|
||||
|
||||
func (cp *CP) StartTransaction(request *core.StartTransactionRequest) (*core.StartTransactionConfirmation, error) {
|
||||
func (cp *CP) OnStartTransaction(request *core.StartTransactionRequest) (*core.StartTransactionConfirmation, error) {
|
||||
if request == nil {
|
||||
return nil, ErrInvalidRequest
|
||||
}
|
||||
|
||||
if conn := cp.connectorByID(request.ConnectorId); conn != nil {
|
||||
return conn.StartTransaction(request)
|
||||
return conn.OnStartTransaction(request)
|
||||
}
|
||||
|
||||
res := &core.StartTransactionConfirmation{
|
||||
|
|
@ -110,13 +110,13 @@ func (cp *CP) StartTransaction(request *core.StartTransactionRequest) (*core.Sta
|
|||
return res, nil
|
||||
}
|
||||
|
||||
func (cp *CP) StopTransaction(request *core.StopTransactionRequest) (*core.StopTransactionConfirmation, error) {
|
||||
func (cp *CP) OnStopTransaction(request *core.StopTransactionRequest) (*core.StopTransactionConfirmation, error) {
|
||||
if request == nil {
|
||||
return nil, ErrInvalidRequest
|
||||
}
|
||||
|
||||
if conn := cp.connectorByTransactionID(request.TransactionId); conn != nil {
|
||||
return conn.StopTransaction(request)
|
||||
return conn.OnStopTransaction(request)
|
||||
}
|
||||
|
||||
res := &core.StopTransactionConfirmation{
|
||||
|
|
|
|||
117
charger/ocpp/cp_requests.go
Normal file
117
charger/ocpp/cp_requests.go
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
package ocpp
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/lorenzodonini/ocpp-go/ocpp1.6/core"
|
||||
"github.com/lorenzodonini/ocpp-go/ocpp1.6/remotetrigger"
|
||||
"github.com/lorenzodonini/ocpp-go/ocpp1.6/smartcharging"
|
||||
"github.com/lorenzodonini/ocpp-go/ocpp1.6/types"
|
||||
)
|
||||
|
||||
func (cp *CP) ChangeAvailabilityRequest(connectorId int, availabilityType core.AvailabilityType) error {
|
||||
rc := make(chan error, 1)
|
||||
|
||||
err := Instance().ChangeAvailability(cp.id, func(request *core.ChangeAvailabilityConfirmation, err error) {
|
||||
if err == nil && request != nil && request.Status != core.AvailabilityStatusAccepted {
|
||||
err = errors.New(string(request.Status))
|
||||
}
|
||||
|
||||
rc <- err
|
||||
}, connectorId, availabilityType)
|
||||
|
||||
return wait(err, rc)
|
||||
}
|
||||
|
||||
func (cp *CP) GetCompositeScheduleRequest(connectorId int, duration int) (*smartcharging.GetCompositeScheduleConfirmation, error) {
|
||||
var res *smartcharging.GetCompositeScheduleConfirmation
|
||||
rc := make(chan error, 1)
|
||||
|
||||
err := Instance().GetCompositeSchedule(cp.id, func(request *smartcharging.GetCompositeScheduleConfirmation, err error) {
|
||||
if err == nil && request != nil && request.Status != smartcharging.GetCompositeScheduleStatusAccepted {
|
||||
err = errors.New(string(request.Status))
|
||||
}
|
||||
|
||||
res = request
|
||||
|
||||
rc <- err
|
||||
}, connectorId, duration)
|
||||
|
||||
return res, wait(err, rc)
|
||||
}
|
||||
|
||||
func (cp *CP) RemoteStartTransactionRequest(connectorId int, idTag string) error {
|
||||
rc := make(chan error, 1)
|
||||
err := Instance().RemoteStartTransaction(cp.id, func(request *core.RemoteStartTransactionConfirmation, err error) {
|
||||
if err == nil && request != nil && request.Status != types.RemoteStartStopStatusAccepted {
|
||||
err = errors.New(string(request.Status))
|
||||
}
|
||||
|
||||
rc <- err
|
||||
}, idTag, func(request *core.RemoteStartTransactionRequest) {
|
||||
if connectorId > 0 {
|
||||
request.ConnectorId = &connectorId
|
||||
}
|
||||
})
|
||||
|
||||
return wait(err, rc)
|
||||
}
|
||||
|
||||
func (cp *CP) SetChargingProfileRequest(connectorId int, profile *types.ChargingProfile) error {
|
||||
rc := make(chan error, 1)
|
||||
|
||||
err := Instance().SetChargingProfile(cp.id, func(request *smartcharging.SetChargingProfileConfirmation, err error) {
|
||||
if err == nil && request != nil && request.Status != smartcharging.ChargingProfileStatusAccepted {
|
||||
err = errors.New(string(request.Status))
|
||||
}
|
||||
|
||||
rc <- err
|
||||
}, connectorId, profile)
|
||||
|
||||
return wait(err, rc)
|
||||
}
|
||||
|
||||
func (cp *CP) TriggerMessageRequest(connectorId int, requestedMessage remotetrigger.MessageTrigger) error {
|
||||
rc := make(chan error, 1)
|
||||
|
||||
err := Instance().TriggerMessage(cp.id, func(request *remotetrigger.TriggerMessageConfirmation, err error) {
|
||||
if err == nil && request != nil && request.Status != remotetrigger.TriggerMessageStatusAccepted {
|
||||
err = errors.New(string(request.Status))
|
||||
}
|
||||
|
||||
rc <- err
|
||||
}, requestedMessage, func(request *remotetrigger.TriggerMessageRequest) {
|
||||
if connectorId > 0 {
|
||||
request.ConnectorId = &connectorId
|
||||
}
|
||||
})
|
||||
|
||||
return wait(err, rc)
|
||||
}
|
||||
|
||||
func (cp *CP) ChangeConfigurationRequest(key, value string) error {
|
||||
rc := make(chan error, 1)
|
||||
|
||||
err := Instance().ChangeConfiguration(cp.id, func(request *core.ChangeConfigurationConfirmation, err error) {
|
||||
if err == nil && request != nil && request.Status != core.ConfigurationStatusAccepted {
|
||||
err = errors.New(string(request.Status))
|
||||
}
|
||||
|
||||
rc <- err
|
||||
}, key, value)
|
||||
|
||||
return wait(err, rc)
|
||||
}
|
||||
|
||||
func (cp *CP) GetConfigurationRequest() (*core.GetConfigurationConfirmation, error) {
|
||||
rc := make(chan error, 1)
|
||||
|
||||
var res *core.GetConfigurationConfirmation
|
||||
err := Instance().GetConfiguration(cp.id, func(request *core.GetConfigurationConfirmation, err error) {
|
||||
res = request
|
||||
|
||||
rc <- err
|
||||
}, nil)
|
||||
|
||||
return res, wait(err, rc)
|
||||
}
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
package ocpp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
|
@ -16,13 +15,13 @@ import (
|
|||
const desiredMeasurands = "Power.Active.Import,Energy.Active.Import.Register,Current.Import,Voltage,Current.Offered,Power.Offered,SoC"
|
||||
|
||||
func (cp *CP) Setup(meterValues string, meterInterval time.Duration) error {
|
||||
if err := Instance().ChangeAvailabilityRequest(cp.ID(), 0, core.AvailabilityTypeOperative); err != nil {
|
||||
if err := cp.ChangeAvailabilityRequest(0, core.AvailabilityTypeOperative); err != nil {
|
||||
cp.log.DEBUG.Printf("failed configuring availability: %v", err)
|
||||
}
|
||||
|
||||
meterValuesSampledDataMaxLength := len(strings.Split(desiredMeasurands, ","))
|
||||
|
||||
resp, err := cp.GetConfiguration()
|
||||
resp, err := cp.GetConfigurationRequest()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -103,7 +102,7 @@ func (cp *CP) Setup(meterValues string, meterInterval time.Duration) error {
|
|||
|
||||
// see who's there
|
||||
if cp.HasRemoteTriggerFeature {
|
||||
if err := Instance().TriggerMessageRequest(cp.ID(), core.BootNotificationFeatureName); err != nil {
|
||||
if err := cp.TriggerMessageRequest(0, core.BootNotificationFeatureName); err != nil {
|
||||
cp.log.DEBUG.Printf("failed triggering BootNotification: %v", err)
|
||||
}
|
||||
|
||||
|
|
@ -123,7 +122,7 @@ func (cp *CP) Setup(meterValues string, meterInterval time.Duration) error {
|
|||
|
||||
// configure measurands
|
||||
if meterValues != "" {
|
||||
if err := cp.configure(KeyMeterValuesSampledData, meterValues); err == nil || meterValues == "disable" {
|
||||
if err := cp.ChangeConfigurationRequest(KeyMeterValuesSampledData, meterValues); err == nil || meterValues == "disable" {
|
||||
cp.meterValuesSample = meterValues
|
||||
} else {
|
||||
cp.log.WARN.Printf("failed configuring %s: %v", KeyMeterValuesSampledData, err)
|
||||
|
|
@ -132,7 +131,7 @@ func (cp *CP) Setup(meterValues string, meterInterval time.Duration) error {
|
|||
|
||||
// trigger initial meter values
|
||||
if cp.HasRemoteTriggerFeature {
|
||||
if err := Instance().TriggerMessageRequest(cp.ID(), core.MeterValuesFeatureName); err == nil {
|
||||
if err := cp.TriggerMessageRequest(0, core.MeterValuesFeatureName); err == nil {
|
||||
// wait for meter values
|
||||
select {
|
||||
case <-time.After(Timeout):
|
||||
|
|
@ -144,32 +143,19 @@ func (cp *CP) Setup(meterValues string, meterInterval time.Duration) error {
|
|||
|
||||
// configure sample rate
|
||||
if meterInterval > 0 {
|
||||
if err := cp.configure(KeyMeterValueSampleInterval, strconv.Itoa(int(meterInterval.Seconds()))); err != nil {
|
||||
if err := cp.ChangeConfigurationRequest(KeyMeterValueSampleInterval, strconv.Itoa(int(meterInterval.Seconds()))); err != nil {
|
||||
cp.log.WARN.Printf("failed configuring %s: %v", KeyMeterValueSampleInterval, err)
|
||||
}
|
||||
}
|
||||
|
||||
// configure websocket ping interval
|
||||
if err := cp.configure(KeyWebSocketPingInterval, "30"); err != nil {
|
||||
if err := cp.ChangeConfigurationRequest(KeyWebSocketPingInterval, "30"); err != nil {
|
||||
cp.log.DEBUG.Printf("failed configuring %s: %v", KeyWebSocketPingInterval, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetConfiguration
|
||||
func (cp *CP) GetConfiguration() (*core.GetConfigurationConfirmation, error) {
|
||||
rc := make(chan error, 1)
|
||||
|
||||
var res *core.GetConfigurationConfirmation
|
||||
err := Instance().GetConfiguration(cp.ID(), func(resp *core.GetConfigurationConfirmation, err error) {
|
||||
res = resp
|
||||
rc <- err
|
||||
}, nil)
|
||||
|
||||
return res, wait(err, rc)
|
||||
}
|
||||
|
||||
// HasMeasurement checks if meterValuesSample contains given measurement
|
||||
func (cp *CP) HasMeasurement(val types.Measurand) bool {
|
||||
return hasProperty(cp.meterValuesSample, string(val))
|
||||
|
|
@ -178,24 +164,9 @@ func (cp *CP) HasMeasurement(val types.Measurand) bool {
|
|||
func (cp *CP) tryMeasurands(measurands string, key string) []string {
|
||||
var accepted []string
|
||||
for _, m := range strings.Split(measurands, ",") {
|
||||
if err := cp.configure(key, m); err == nil {
|
||||
if err := cp.ChangeConfigurationRequest(key, m); err == nil {
|
||||
accepted = append(accepted, m)
|
||||
}
|
||||
}
|
||||
return accepted
|
||||
}
|
||||
|
||||
// configure updates CP configuration
|
||||
func (cp *CP) configure(key, val string) error {
|
||||
rc := make(chan error, 1)
|
||||
|
||||
err := Instance().ChangeConfiguration(cp.id, func(resp *core.ChangeConfigurationConfirmation, err error) {
|
||||
if err == nil && resp != nil && resp.Status != core.ConfigurationStatusAccepted {
|
||||
rc <- fmt.Errorf("ChangeConfiguration failed: %s", resp.Status)
|
||||
}
|
||||
|
||||
rc <- err
|
||||
}, key, val)
|
||||
|
||||
return wait(err, rc)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,90 +1,10 @@
|
|||
package ocpp
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"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
|
||||
|
||||
func (cs *CS) TriggerResetRequest(id string, resetType core.ResetType) error {
|
||||
rc := make(chan error, 1)
|
||||
|
||||
err := cs.Reset(id, func(request *core.ResetConfirmation, err error) {
|
||||
if err == nil && request != nil && request.Status != core.ResetStatusAccepted {
|
||||
err = errors.New(string(request.Status))
|
||||
}
|
||||
|
||||
rc <- err
|
||||
}, resetType)
|
||||
|
||||
return wait(err, rc)
|
||||
}
|
||||
|
||||
func (cs *CS) TriggerMessageRequest(id string, requestedMessage remotetrigger.MessageTrigger, props ...func(request *remotetrigger.TriggerMessageRequest)) error {
|
||||
rc := make(chan error, 1)
|
||||
|
||||
err := cs.TriggerMessage(id, func(request *remotetrigger.TriggerMessageConfirmation, err error) {
|
||||
if err == nil && request != nil && request.Status != remotetrigger.TriggerMessageStatusAccepted {
|
||||
err = errors.New(string(request.Status))
|
||||
}
|
||||
|
||||
rc <- err
|
||||
}, requestedMessage, props...)
|
||||
|
||||
return wait(err, rc)
|
||||
}
|
||||
|
||||
func (cs *CS) ChangeAvailabilityRequest(id string, connector int, availabilityType core.AvailabilityType) error {
|
||||
rc := make(chan error, 1)
|
||||
|
||||
err := cs.ChangeAvailability(id, func(request *core.ChangeAvailabilityConfirmation, err error) {
|
||||
if err == nil && request != nil && request.Status != core.AvailabilityStatusAccepted {
|
||||
err = errors.New(string(request.Status))
|
||||
}
|
||||
|
||||
rc <- err
|
||||
}, connector, availabilityType)
|
||||
|
||||
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) (*smartcharging.GetCompositeScheduleConfirmation, error) {
|
||||
var schedule *smartcharging.GetCompositeScheduleConfirmation
|
||||
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
|
||||
|
||||
rc <- err
|
||||
}, connector, duration)
|
||||
|
||||
return schedule, wait(err, rc)
|
||||
}
|
||||
|
||||
// cp actions
|
||||
|
||||
func (cs *CS) OnAuthorize(id string, request *core.AuthorizeRequest) (*core.AuthorizeConfirmation, error) {
|
||||
|
|
@ -93,7 +13,7 @@ func (cs *CS) OnAuthorize(id string, request *core.AuthorizeRequest) (*core.Auth
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return cp.Authorize(request)
|
||||
return cp.OnAuthorize(request)
|
||||
}
|
||||
|
||||
func (cs *CS) OnBootNotification(id string, request *core.BootNotificationRequest) (*core.BootNotificationConfirmation, error) {
|
||||
|
|
@ -102,7 +22,7 @@ func (cs *CS) OnBootNotification(id string, request *core.BootNotificationReques
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return cp.BootNotification(request)
|
||||
return cp.OnBootNotification(request)
|
||||
}
|
||||
|
||||
func (cs *CS) OnDataTransfer(id string, request *core.DataTransferRequest) (*core.DataTransferConfirmation, error) {
|
||||
|
|
@ -111,7 +31,7 @@ func (cs *CS) OnDataTransfer(id string, request *core.DataTransferRequest) (*cor
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return cp.DataTransfer(request)
|
||||
return cp.OnDataTransfer(request)
|
||||
}
|
||||
|
||||
func (cs *CS) OnHeartbeat(id string, request *core.HeartbeatRequest) (*core.HeartbeatConfirmation, error) {
|
||||
|
|
@ -120,7 +40,7 @@ func (cs *CS) OnHeartbeat(id string, request *core.HeartbeatRequest) (*core.Hear
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return cp.Heartbeat(request)
|
||||
return cp.OnHeartbeat(request)
|
||||
}
|
||||
|
||||
func (cs *CS) OnMeterValues(id string, request *core.MeterValuesRequest) (*core.MeterValuesConfirmation, error) {
|
||||
|
|
@ -129,7 +49,7 @@ func (cs *CS) OnMeterValues(id string, request *core.MeterValuesRequest) (*core.
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return cp.MeterValues(request)
|
||||
return cp.OnMeterValues(request)
|
||||
}
|
||||
|
||||
func (cs *CS) OnStatusNotification(id string, request *core.StatusNotificationRequest) (*core.StatusNotificationConfirmation, error) {
|
||||
|
|
@ -138,7 +58,7 @@ func (cs *CS) OnStatusNotification(id string, request *core.StatusNotificationRe
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return cp.StatusNotification(request)
|
||||
return cp.OnStatusNotification(request)
|
||||
}
|
||||
|
||||
func (cs *CS) OnStartTransaction(id string, request *core.StartTransactionRequest) (*core.StartTransactionConfirmation, error) {
|
||||
|
|
@ -147,7 +67,7 @@ func (cs *CS) OnStartTransaction(id string, request *core.StartTransactionReques
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return cp.StartTransaction(request)
|
||||
return cp.OnStartTransaction(request)
|
||||
}
|
||||
|
||||
func (cs *CS) OnStopTransaction(id string, request *core.StopTransactionRequest) (*core.StopTransactionConfirmation, error) {
|
||||
|
|
@ -156,7 +76,7 @@ func (cs *CS) OnStopTransaction(id string, request *core.StopTransactionRequest)
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return cp.StopTransaction(request)
|
||||
return cp.OnStopTransaction(request)
|
||||
}
|
||||
|
||||
func (cs *CS) OnDiagnosticsStatusNotification(id string, request *firmware.DiagnosticsStatusNotificationRequest) (confirmation *firmware.DiagnosticsStatusNotificationConfirmation, err error) {
|
||||
|
|
@ -165,7 +85,7 @@ func (cs *CS) OnDiagnosticsStatusNotification(id string, request *firmware.Diagn
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return cp.DiagnosticStatusNotification(request)
|
||||
return cp.OnDiagnosticStatusNotification(request)
|
||||
}
|
||||
|
||||
func (cs *CS) OnFirmwareStatusNotification(id string, request *firmware.FirmwareStatusNotificationRequest) (confirmation *firmware.FirmwareStatusNotificationConfirmation, err error) {
|
||||
|
|
@ -174,5 +94,5 @@ func (cs *CS) OnFirmwareStatusNotification(id string, request *firmware.Firmware
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return cp.FirmwareStatusNotification(request)
|
||||
return cp.OnFirmwareStatusNotification(request)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue