forked from lorenzodonini/ocpp-go
/
types.go
332 lines (296 loc) · 14.8 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Contains common and shared data types between OCPP 1.6 messages.
package types
import (
"github.com/voicecom/ocpp-go/ocppj"
"gopkg.in/go-playground/validator.v9"
)
const (
V16Subprotocol = "ocpp1.6"
)
type PropertyViolation struct {
error
Property string
}
func (e *PropertyViolation) Error() string {
return ""
}
type AuthorizationStatus string
const (
AuthorizationStatusAccepted AuthorizationStatus = "Accepted"
AuthorizationStatusBlocked AuthorizationStatus = "Blocked"
AuthorizationStatusExpired AuthorizationStatus = "Expired"
AuthorizationStatusInvalid AuthorizationStatus = "Invalid"
AuthorizationStatusConcurrentTx AuthorizationStatus = "ConcurrentTx"
)
func isValidAuthorizationStatus(fl validator.FieldLevel) bool {
status := AuthorizationStatus(fl.Field().String())
switch status {
case AuthorizationStatusAccepted, AuthorizationStatusBlocked, AuthorizationStatusExpired, AuthorizationStatusInvalid, AuthorizationStatusConcurrentTx:
return true
default:
return false
}
}
type IdTagInfo struct {
ExpiryDate *DateTime `json:"expiryDate,omitempty" validate:"omitempty"`
ParentIdTag string `json:"parentIdTag,omitempty" validate:"omitempty,max=20"`
Status AuthorizationStatus `json:"status" validate:"required,authorizationStatus"`
}
func NewIdTagInfo(status AuthorizationStatus) *IdTagInfo {
return &IdTagInfo{Status: status}
}
// Charging Profiles
type ChargingProfilePurposeType string
type ChargingProfileKindType string
type RecurrencyKindType string
type ChargingRateUnitType string
const (
ChargingProfilePurposeChargePointMaxProfile ChargingProfilePurposeType = "ChargePointMaxProfile"
ChargingProfilePurposeTxDefaultProfile ChargingProfilePurposeType = "TxDefaultProfile"
ChargingProfilePurposeTxProfile ChargingProfilePurposeType = "TxProfile"
ChargingProfileKindAbsolute ChargingProfileKindType = "Absolute"
ChargingProfileKindRecurring ChargingProfileKindType = "Recurring"
ChargingProfileKindRelative ChargingProfileKindType = "Relative"
RecurrencyKindDaily RecurrencyKindType = "Daily"
RecurrencyKindWeekly RecurrencyKindType = "Weekly"
ChargingRateUnitWatts ChargingRateUnitType = "W"
ChargingRateUnitAmperes ChargingRateUnitType = "A"
)
func isValidChargingProfilePurpose(fl validator.FieldLevel) bool {
purposeType := ChargingProfilePurposeType(fl.Field().String())
switch purposeType {
case ChargingProfilePurposeChargePointMaxProfile, ChargingProfilePurposeTxDefaultProfile, ChargingProfilePurposeTxProfile:
return true
default:
return false
}
}
func isValidChargingProfileKind(fl validator.FieldLevel) bool {
purposeType := ChargingProfileKindType(fl.Field().String())
switch purposeType {
case ChargingProfileKindAbsolute, ChargingProfileKindRecurring, ChargingProfileKindRelative:
return true
default:
return false
}
}
func isValidRecurrencyKind(fl validator.FieldLevel) bool {
purposeType := RecurrencyKindType(fl.Field().String())
switch purposeType {
case RecurrencyKindDaily, RecurrencyKindWeekly:
return true
default:
return false
}
}
func isValidChargingRateUnit(fl validator.FieldLevel) bool {
purposeType := ChargingRateUnitType(fl.Field().String())
switch purposeType {
case ChargingRateUnitWatts, ChargingRateUnitAmperes:
return true
default:
return false
}
}
type ChargingSchedulePeriod struct {
StartPeriod int `json:"startPeriod" validate:"gte=0"`
Limit float64 `json:"limit" validate:"gte=0"`
NumberPhases *int `json:"numberPhases,omitempty" validate:"omitempty,gte=0"`
}
func NewChargingSchedulePeriod(startPeriod int, limit float64) ChargingSchedulePeriod {
return ChargingSchedulePeriod{StartPeriod: startPeriod, Limit: limit}
}
type ChargingSchedule struct {
Duration *int `json:"duration,omitempty" validate:"omitempty,gte=0"`
StartSchedule *DateTime `json:"startSchedule,omitempty"`
ChargingRateUnit ChargingRateUnitType `json:"chargingRateUnit" validate:"required,chargingRateUnit"`
ChargingSchedulePeriod []ChargingSchedulePeriod `json:"chargingSchedulePeriod" validate:"required,min=1"`
MinChargingRate *float64 `json:"minChargingRate,omitempty" validate:"omitempty,gte=0"`
}
func NewChargingSchedule(chargingRateUnit ChargingRateUnitType, schedulePeriod ...ChargingSchedulePeriod) *ChargingSchedule {
return &ChargingSchedule{ChargingRateUnit: chargingRateUnit, ChargingSchedulePeriod: schedulePeriod}
}
type ChargingProfile struct {
ChargingProfileId int `json:"chargingProfileId" validate:"gte=0"`
TransactionId int `json:"transactionId,omitempty"`
StackLevel int `json:"stackLevel" validate:"gt=0"`
ChargingProfilePurpose ChargingProfilePurposeType `json:"chargingProfilePurpose" validate:"required,chargingProfilePurpose"`
ChargingProfileKind ChargingProfileKindType `json:"chargingProfileKind" validate:"required,chargingProfileKind"`
RecurrencyKind RecurrencyKindType `json:"recurrencyKind,omitempty" validate:"omitempty,recurrencyKind"`
ValidFrom *DateTime `json:"validFrom,omitempty"`
ValidTo *DateTime `json:"validTo,omitempty"`
ChargingSchedule *ChargingSchedule `json:"chargingSchedule" validate:"required"`
}
func NewChargingProfile(chargingProfileId int, stackLevel int, chargingProfilePurpose ChargingProfilePurposeType, chargingProfileKind ChargingProfileKindType, schedule *ChargingSchedule) *ChargingProfile {
return &ChargingProfile{ChargingProfileId: chargingProfileId, StackLevel: stackLevel, ChargingProfilePurpose: chargingProfilePurpose, ChargingProfileKind: chargingProfileKind, ChargingSchedule: schedule}
}
// Remote Start/Stop
type RemoteStartStopStatus string
const (
RemoteStartStopStatusAccepted RemoteStartStopStatus = "Accepted"
RemoteStartStopStatusRejected RemoteStartStopStatus = "Rejected"
)
func isValidRemoteStartStopStatus(fl validator.FieldLevel) bool {
status := RemoteStartStopStatus(fl.Field().String())
switch status {
case RemoteStartStopStatusAccepted, RemoteStartStopStatusRejected:
return true
default:
return false
}
}
// Meter Value
type ReadingContext string
type ValueFormat string
type Measurand string
type Phase string
type Location string
type UnitOfMeasure string
const (
ReadingContextInterruptionBegin ReadingContext = "Interruption.Begin"
ReadingContextInterruptionEnd ReadingContext = "Interruption.End"
ReadingContextOther ReadingContext = "Other"
ReadingContextSampleClock ReadingContext = "Sample.Clock"
ReadingContextSamplePeriodic ReadingContext = "Sample.Periodic"
ReadingContextTransactionBegin ReadingContext = "Transaction.Begin"
ReadingContextTransactionEnd ReadingContext = "Transaction.End"
ReadingContextTrigger ReadingContext = "Trigger"
ValueFormatRaw ValueFormat = "Raw"
ValueFormatSignedData ValueFormat = "SignedData"
MeasurandCurrentExport Measurand = "Current.Export"
MeasurandCurrentImport Measurand = "Current.Import"
MeasurandCurrentOffered Measurand = "Current.Offered"
MeasurandEnergyActiveExportRegister Measurand = "Energy.Active.Export.Register"
MeasurandEnergyActiveImportRegister Measurand = "Energy.Active.Import.Register"
MeasurandEnergyReactiveExportRegister Measurand = "Energy.Reactive.Export.Register"
MeasurandEnergyReactiveImportRegister Measurand = "Energy.Reactive.Import.Register"
MeasurandEnergyActiveExportInterval Measurand = "Energy.Active.Export.Interval"
MeasurandEnergyActiveImportInterval Measurand = "Energy.Active.Import.Interval"
MeasurandEnergyReactiveExportInterval Measurand = "Energy.Reactive.Export.Interval"
MeasurandEnergyReactiveImportInterval Measurand = "Energy.Reactive.Import.Interval"
MeasurandFrequency Measurand = "Frequency"
MeasurandPowerActiveExport Measurand = "Power.Active.Export"
MeasurandPowerActiveImport Measurand = "Power.Active.Import"
MeasurandPowerFactor Measurand = "Power.Factor"
MeasurandPowerOffered Measurand = "Power.Offered"
MeasurandPowerReactiveExport Measurand = "Power.Reactive.Export"
MeasurandPowerReactiveImport Measurand = "Power.Reactive.Import"
MeasurandRPM Measurand = "RPM"
MeasueandSoC Measurand = "SoC"
MeasurandTemperature Measurand = "Temperature"
MeasurandVoltage Measurand = "Voltage"
PhaseL1 Phase = "L1"
PhaseL2 Phase = "L2"
PhaseL3 Phase = "L3"
PhaseN Phase = "N"
PhaseL1N Phase = "L1-N"
PhaseL2N Phase = "L2-N"
PhaseL3N Phase = "L3-N"
PhaseL1L2 Phase = "L1-L2"
PhaseL2L3 Phase = "L2-L3"
PhaseL3L1 Phase = "L3-L1"
LocationBody Location = "Body"
LocationCable Location = "Cable"
LocationEV Location = "EV"
LocationInlet Location = "Inlet"
LocationOutlet Location = "Outlet"
UnitOfMeasureWh UnitOfMeasure = "Wh"
UnitOfMeasureKWh UnitOfMeasure = "kWh"
UnitOfMeasureVarh UnitOfMeasure = "varh"
UnitOfMeasureKvarh UnitOfMeasure = "kvarh"
UnitOfMeasureW UnitOfMeasure = "W"
UnitOfMeasureKW UnitOfMeasure = "kW"
UnitOfMeasureVA UnitOfMeasure = "VA"
UnitOfMeasureKVA UnitOfMeasure = "kVA"
UnitOfMeasureVar UnitOfMeasure = "var"
UnitOfMeasureKvar UnitOfMeasure = "kvar"
UnitOfMeasureA UnitOfMeasure = "A"
UnitOfMeasureV UnitOfMeasure = "V"
UnitOfMeasureCelsius UnitOfMeasure = "Celsius"
UnitOfMeasureFahrenheit UnitOfMeasure = "Fahrenheit"
UnitOfMeasureK UnitOfMeasure = "K"
UnitOfMeasurePercent UnitOfMeasure = "Percent"
)
func isValidReadingContext(fl validator.FieldLevel) bool {
readingContext := ReadingContext(fl.Field().String())
switch readingContext {
case ReadingContextInterruptionBegin, ReadingContextInterruptionEnd, ReadingContextOther, ReadingContextSampleClock, ReadingContextSamplePeriodic, ReadingContextTransactionBegin, ReadingContextTransactionEnd, ReadingContextTrigger:
return true
default:
return false
}
}
func isValidValueFormat(fl validator.FieldLevel) bool {
valueFormat := ValueFormat(fl.Field().String())
switch valueFormat {
case ValueFormatRaw, ValueFormatSignedData:
return true
default:
return false
}
}
func isValidMeasurand(fl validator.FieldLevel) bool {
measurand := Measurand(fl.Field().String())
switch measurand {
case MeasueandSoC, MeasurandCurrentExport, MeasurandCurrentImport, MeasurandCurrentOffered, MeasurandEnergyActiveExportInterval, MeasurandEnergyActiveExportRegister, MeasurandEnergyReactiveExportInterval, MeasurandEnergyReactiveExportRegister, MeasurandEnergyReactiveImportRegister, MeasurandEnergyReactiveImportInterval, MeasurandEnergyActiveImportInterval, MeasurandEnergyActiveImportRegister, MeasurandFrequency, MeasurandPowerActiveExport, MeasurandPowerActiveImport, MeasurandPowerReactiveImport, MeasurandPowerReactiveExport, MeasurandPowerOffered, MeasurandPowerFactor, MeasurandVoltage, MeasurandTemperature, MeasurandRPM:
return true
default:
return false
}
}
func isValidPhase(fl validator.FieldLevel) bool {
phase := Phase(fl.Field().String())
switch phase {
case PhaseL1, PhaseL2, PhaseL3, PhaseN, PhaseL1N, PhaseL2N, PhaseL3N, PhaseL1L2, PhaseL2L3, PhaseL3L1:
return true
default:
return false
}
}
func isValidLocation(fl validator.FieldLevel) bool {
location := Location(fl.Field().String())
switch location {
case LocationBody, LocationCable, LocationEV, LocationInlet, LocationOutlet:
return true
default:
return false
}
}
func isValidUnitOfMeasure(fl validator.FieldLevel) bool {
unitOfMeasure := UnitOfMeasure(fl.Field().String())
switch unitOfMeasure {
case UnitOfMeasureA, UnitOfMeasureWh, UnitOfMeasureKWh, UnitOfMeasureVarh, UnitOfMeasureKvarh, UnitOfMeasureW, UnitOfMeasureKW, UnitOfMeasureVA, UnitOfMeasureKVA, UnitOfMeasureVar, UnitOfMeasureKvar, UnitOfMeasureV, UnitOfMeasureCelsius, UnitOfMeasureFahrenheit, UnitOfMeasureK, UnitOfMeasurePercent:
return true
default:
return false
}
}
type SampledValue struct {
Value string `json:"value" validate:"required"`
Context ReadingContext `json:"context,omitempty" validate:"omitempty,readingContext"`
Format ValueFormat `json:"format,omitempty" validate:"omitempty,valueFormat"`
Measurand Measurand `json:"measurand,omitempty" validate:"omitempty,measurand"`
Phase Phase `json:"phase,omitempty" validate:"omitempty,phase"`
Location Location `json:"location,omitempty" validate:"omitempty,location"`
Unit UnitOfMeasure `json:"unit,omitempty" validate:"omitempty,unitOfMeasure"`
}
type MeterValue struct {
Timestamp *DateTime `json:"timestamp" validate:"required"`
SampledValue []SampledValue `json:"sampledValue" validate:"required,min=0,dive"`
}
// Initialize validator
var Validate = ocppj.Validate
func init() {
_ = Validate.RegisterValidation("authorizationStatus", isValidAuthorizationStatus)
_ = Validate.RegisterValidation("chargingProfilePurpose", isValidChargingProfilePurpose)
_ = Validate.RegisterValidation("chargingProfileKind", isValidChargingProfileKind)
_ = Validate.RegisterValidation("recurrencyKind", isValidRecurrencyKind)
_ = Validate.RegisterValidation("chargingRateUnit", isValidChargingRateUnit)
_ = Validate.RegisterValidation("remoteStartStopStatus", isValidRemoteStartStopStatus)
_ = Validate.RegisterValidation("readingContext", isValidReadingContext)
_ = Validate.RegisterValidation("valueFormat", isValidValueFormat)
_ = Validate.RegisterValidation("measurand", isValidMeasurand)
_ = Validate.RegisterValidation("phase", isValidPhase)
_ = Validate.RegisterValidation("location", isValidLocation)
_ = Validate.RegisterValidation("unitOfMeasure", isValidUnitOfMeasure)
}