|
| 1 | +package availability |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + |
| 6 | + "gopkg.in/go-playground/validator.v9" |
| 7 | + |
| 8 | + "github.com/lorenzodonini/ocpp-go/ocpp2.1/types" |
| 9 | +) |
| 10 | + |
| 11 | +// -------------------- Change Availability (CSMS -> CS) -------------------- |
| 12 | + |
| 13 | +const ChangeAvailabilityFeatureName = "ChangeAvailability" |
| 14 | + |
| 15 | +// OperationalStatus represents the requested operational status. |
| 16 | +type OperationalStatus string |
| 17 | + |
| 18 | +const ( |
| 19 | + OperationalStatusInoperative OperationalStatus = "Inoperative" |
| 20 | + OperationalStatusOperative OperationalStatus = "Operative" |
| 21 | +) |
| 22 | + |
| 23 | +func isValidOperationalStatus(fl validator.FieldLevel) bool { |
| 24 | + s := OperationalStatus(fl.Field().String()) |
| 25 | + switch s { |
| 26 | + case OperationalStatusInoperative, OperationalStatusOperative: |
| 27 | + return true |
| 28 | + default: |
| 29 | + return false |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// ChangeAvailabilityStatus represents the response status. |
| 34 | +type ChangeAvailabilityStatus string |
| 35 | + |
| 36 | +const ( |
| 37 | + ChangeAvailabilityStatusAccepted ChangeAvailabilityStatus = "Accepted" |
| 38 | + ChangeAvailabilityStatusRejected ChangeAvailabilityStatus = "Rejected" |
| 39 | + ChangeAvailabilityStatusScheduled ChangeAvailabilityStatus = "Scheduled" |
| 40 | +) |
| 41 | + |
| 42 | +func isValidChangeAvailabilityStatus(fl validator.FieldLevel) bool { |
| 43 | + s := ChangeAvailabilityStatus(fl.Field().String()) |
| 44 | + switch s { |
| 45 | + case ChangeAvailabilityStatusAccepted, ChangeAvailabilityStatusRejected, ChangeAvailabilityStatusScheduled: |
| 46 | + return true |
| 47 | + default: |
| 48 | + return false |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// ChangeAvailabilityRequest is the payload for a ChangeAvailability request from CSMS to CS. |
| 53 | +type ChangeAvailabilityRequest struct { |
| 54 | + OperationalStatus OperationalStatus `json:"operationalStatus" validate:"required,operationalStatus21"` |
| 55 | + EVSE *types.EVSE `json:"evse,omitempty" validate:"omitempty"` |
| 56 | + CustomData *types.CustomDataType `json:"customData,omitempty" validate:"omitempty"` |
| 57 | +} |
| 58 | + |
| 59 | +// ChangeAvailabilityResponse is the payload for a ChangeAvailability response from CS to CSMS. |
| 60 | +type ChangeAvailabilityResponse struct { |
| 61 | + Status ChangeAvailabilityStatus `json:"status" validate:"required,changeAvailabilityStatus21"` |
| 62 | + StatusInfo *types.StatusInfo `json:"statusInfo,omitempty" validate:"omitempty"` |
| 63 | + CustomData *types.CustomDataType `json:"customData,omitempty" validate:"omitempty"` |
| 64 | +} |
| 65 | + |
| 66 | +// ChangeAvailabilityFeature represents the ChangeAvailability feature. |
| 67 | +type ChangeAvailabilityFeature struct{} |
| 68 | + |
| 69 | +func (f ChangeAvailabilityFeature) GetFeatureName() string { |
| 70 | + return ChangeAvailabilityFeatureName |
| 71 | +} |
| 72 | + |
| 73 | +func (f ChangeAvailabilityFeature) GetRequestType() reflect.Type { |
| 74 | + return reflect.TypeOf(ChangeAvailabilityRequest{}) |
| 75 | +} |
| 76 | + |
| 77 | +func (f ChangeAvailabilityFeature) GetResponseType() reflect.Type { |
| 78 | + return reflect.TypeOf(ChangeAvailabilityResponse{}) |
| 79 | +} |
| 80 | + |
| 81 | +func (r ChangeAvailabilityRequest) GetFeatureName() string { |
| 82 | + return ChangeAvailabilityFeatureName |
| 83 | +} |
| 84 | + |
| 85 | +func (c ChangeAvailabilityResponse) GetFeatureName() string { |
| 86 | + return ChangeAvailabilityFeatureName |
| 87 | +} |
| 88 | + |
| 89 | +// NewChangeAvailabilityRequest creates a new ChangeAvailabilityRequest. |
| 90 | +func NewChangeAvailabilityRequest(operationalStatus OperationalStatus) *ChangeAvailabilityRequest { |
| 91 | + return &ChangeAvailabilityRequest{OperationalStatus: operationalStatus} |
| 92 | +} |
| 93 | + |
| 94 | +// NewChangeAvailabilityResponse creates a new ChangeAvailabilityResponse. |
| 95 | +func NewChangeAvailabilityResponse(status ChangeAvailabilityStatus) *ChangeAvailabilityResponse { |
| 96 | + return &ChangeAvailabilityResponse{Status: status} |
| 97 | +} |
| 98 | + |
| 99 | +func init() { |
| 100 | + _ = types.Validate.RegisterValidation("operationalStatus21", isValidOperationalStatus) |
| 101 | + _ = types.Validate.RegisterValidation("changeAvailabilityStatus21", isValidChangeAvailabilityStatus) |
| 102 | +} |
0 commit comments