Skip to content

Commit ab2382b

Browse files
committed
feat: add tariffs and costs
1 parent 8e3ae0a commit ab2382b

File tree

3 files changed

+90
-3
lines changed

3 files changed

+90
-3
lines changed

ocpp2.1/transactions/transaction_event.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ type TransactionEventRequest struct {
3636
EvseSleep *bool `json:"evseSleep,omitempty"`
3737
TransactionInfo Transaction `json:"transactionInfo" validate:"required"` // Contains transaction specific information.
3838
IDToken *types.IdToken `json:"idToken,omitempty" validate:"omitempty,dive"`
39-
Evse *types.EVSE `json:"evse,omitempty" validate:"omitempty"` // Identifies which evse (and connector) of the Charging Station is used.
40-
MeterValue []types.MeterValue `json:"meterValue,omitempty" validate:"omitempty,dive"` // Contains the relevant meter values.
41-
// todo CostDetails types.Cos
39+
Evse *types.EVSE `json:"evse,omitempty" validate:"omitempty"` // Identifies which evse (and connector) of the Charging Station is used.
40+
MeterValue []types.MeterValue `json:"meterValue,omitempty" validate:"omitempty,dive"` // Contains the relevant meter values.
41+
CostDetails *types.CostDetails `json:"costDetails,omitempty" validate:"omitempty,dive"` // Contains the cost details for this transaction. This can be used to inform the CSMS about the cost of this transaction.
4242
}
4343

4444
// This field definition of the TransactionEventResponse payload, sent by the CSMS to the Charging Station in response to a TransactionEventRequest.

ocpp2.1/types/cost.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package types
22

33
import (
4+
"github.com/lorenzodonini/ocpp-go/ocpp2.0.1/types"
45
"gopkg.in/go-playground/validator.v9"
56
)
67

@@ -74,3 +75,87 @@ func NewSalesTariff(id int, salesTariffEntries []SalesTariffEntry) *SalesTariff
7475
SalesTariffEntry: salesTariffEntries,
7576
}
7677
}
78+
79+
type TariffCost string
80+
81+
const (
82+
TariffCostNormal = "NormalCost"
83+
TariffCostMinimum = "MinCost"
84+
TariffCostMaximum = "MaxCost"
85+
)
86+
87+
func isValidTariffCost(fl validator.FieldLevel) bool {
88+
costType := TariffCost(fl.Field().String())
89+
switch costType {
90+
case TariffCostNormal, TariffCostMinimum, TariffCostMaximum:
91+
return true
92+
default:
93+
return false
94+
}
95+
}
96+
97+
type CostDimensionType string
98+
99+
const (
100+
CostDimensionTypeEnergy CostDimensionType = "Energy" // Cost dimension for energy consumed.
101+
CostDimensionTypeChargingTime CostDimensionType = "ChargingTime" // Cost dimension for the time the EV was charging.
102+
CostDimensionTypeIdleTime CostDimensionType = "IdleTime" // Cost dimension for the time the EV was idle.
103+
CostDimensionMinCurrent CostDimensionType = "MinCurrent" // Cost dimension for the minimum current used during the charging session.
104+
CostDimensionMaxCurrent CostDimensionType = "MaxCurrent" // Cost dimension for the maximum current used during the charging session.
105+
CostDimensionMinPower CostDimensionType = "MinPower" // Cost dimension for the minimum power used during the charging session.
106+
CostDimensionMaxPower CostDimensionType = "MaxPower" // Cost dimension for the maximum power used during the charging session.
107+
)
108+
109+
func isValidCostDimensionType(fl validator.FieldLevel) bool {
110+
dimensionType := CostDimensionType(fl.Field().String())
111+
switch dimensionType {
112+
case CostDimensionTypeEnergy, CostDimensionTypeChargingTime, CostDimensionTypeIdleTime,
113+
CostDimensionMinCurrent, CostDimensionMaxCurrent, CostDimensionMinPower, CostDimensionMaxPower:
114+
return true
115+
default:
116+
return false
117+
}
118+
}
119+
120+
type CostDetails struct {
121+
FailureToCalculate *bool `json:"failureToCalculate,omitempty"`
122+
FailureReason *string `json:"failureReason,omitempty" validate:"omitempty,max=500"`
123+
ChargingPeriods []ChargingPeriod `json:"chargingPeriods,omitempty" validate:"omitempty,dive"`
124+
TotalCost TotalCost `json:"totalCost" validate:"required,dive"`
125+
TotalUsage TotalUsage `json:"totalUsage" validate:"required,dive"` // Total usage of the EV during the charging session.
126+
}
127+
128+
type TotalCost struct {
129+
Currency string `json:"currency" validate:"required,max=3"` // The currency of the total cost.
130+
TypeOfCost TariffCost `json:"typeOfCost" validate:"required,tariffCost21"` // The type of cost, e.g. NormalCost, MinCost, MaxCost.
131+
Fixed *Price `json:"fixedPrice,omitempty" validate:"omitempty,dive"`
132+
Energy *Price `json:"energy,omitempty" validate:"omitempty,dive"`
133+
ChargingTime *Price `json:"chargingTime,omitempty" validate:"omitempty,dive"`
134+
IdleTime *Price `json:"idleTime,omitempty" validate:"omitempty,dive"`
135+
ReservationTime *Price `json:"reservationTime,omitempty" validate:"omitempty,dive"`
136+
Total TotalPrice `json:"total" validate:"required,dive"` // Total cost of the charging session.
137+
ReservationFixed *Price `json:"reservationFixed,omitempty" validate:"omitempty,dive"`
138+
}
139+
140+
type TotalPrice struct {
141+
ExclTax *float64 `json:"exclTax,omitempty" validate:"omitempty"` // Total price excluding tax.
142+
InclTax *float64 `json:"inclTax,omitempty" validate:"omitempty"` // Total price including tax.
143+
}
144+
145+
type TotalUsage struct {
146+
Energy float64 `json:"energy" validate:"required"`
147+
ChargingTime int `json:"chargingTime" validate:"required"`
148+
IdleTime int `json:"idleTime" validate:"required"` // Total idle time of the EV during the charging session, in seconds.
149+
ReservationTime *int `json:"reservationTime,omitempty" validate:"omitempty"` // Total reservation time of the EV during the charging session, in seconds.
150+
}
151+
152+
type ChargingPeriod struct {
153+
TariffId *string `json:"tariffId,omitempty" validate:"omitempty,max=60"` // The ID of the tariff used for this charging period.
154+
StartPeriod types.DateTime `json:"startPeriod" validate:"required"` // The start of the charging period.
155+
Dimensions []CostDimension `json:"dimensions,omitempty" validate:"omitempty,dive"`
156+
}
157+
158+
type CostDimension struct {
159+
Type CostDimensionType `json:"type" validate:"required,costDimension21"`
160+
Volume float64 `json:"volume" validate:"required"`
161+
}

ocpp2.1/types/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ func init() {
193193
_ = Validate.RegisterValidation("15118EVCertificate21", isValidCertificate15118EVStatus)
194194
_ = Validate.RegisterValidation("costKind21", isValidCostKind)
195195
_ = Validate.RegisterValidation("operationMode21", isValidOperationMode)
196+
_ = Validate.RegisterValidation("tariffCost21", isValidTariffCost)
197+
_ = Validate.RegisterValidation("costDimension21", isValidCostDimensionType)
196198

197199
Validate.RegisterStructValidation(isValidIdToken, IdToken{})
198200
Validate.RegisterStructValidation(isValidGroupIdToken, GroupIdToken{})

0 commit comments

Comments
 (0)