forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChangeAvailability.cpp
More file actions
161 lines (127 loc) · 4.24 KB
/
ChangeAvailability.cpp
File metadata and controls
161 lines (127 loc) · 4.24 KB
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
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2024
// MIT License
#include <MicroOcpp/Operations/ChangeAvailability.h>
#include <MicroOcpp/Model/Model.h>
#include <MicroOcpp/Model/ConnectorBase/Connector.h>
#include <MicroOcpp/Version.h>
#include <functional>
namespace MicroOcpp {
namespace Ocpp16 {
ChangeAvailability::ChangeAvailability(Model& model) : MemoryManaged("v16.Operation.", "ChangeAvailability"), model(model) {
}
const char* ChangeAvailability::getOperationType(){
return "ChangeAvailability";
}
void ChangeAvailability::processReq(JsonObject payload) {
int connectorIdRaw = payload["connectorId"] | -1;
if (connectorIdRaw < 0) {
errorCode = "FormationViolation";
return;
}
unsigned int connectorId = (unsigned int)connectorIdRaw;
if (connectorId >= model.getNumConnectors()) {
errorCode = "PropertyConstraintViolation";
return;
}
const char *type = payload["type"] | "INVALID";
bool available = false;
if (!strcmp(type, "Operative")) {
accepted = true;
available = true;
} else if (!strcmp(type, "Inoperative")) {
accepted = true;
available = false;
} else {
errorCode = "PropertyConstraintViolation";
return;
}
if (connectorId == 0) {
for (unsigned int cId = 0; cId < model.getNumConnectors(); cId++) {
auto connector = model.getConnector(cId);
connector->setAvailability(available);
if (connector->isOperative() && !available) {
scheduled = true;
}
}
} else {
auto connector = model.getConnector(connectorId);
connector->setAvailability(available);
if (connector->isOperative() && !available) {
scheduled = true;
}
}
}
std::unique_ptr<JsonDoc> ChangeAvailability::createConf(){
auto doc = makeJsonDoc(getMemoryTag(), JSON_OBJECT_SIZE(1));
JsonObject payload = doc->to<JsonObject>();
if (!accepted) {
payload["status"] = "Rejected";
} else if (scheduled) {
payload["status"] = "Scheduled";
} else {
payload["status"] = "Accepted";
}
return doc;
}
} // namespace Ocpp16
} // namespace MicroOcpp
#if MO_ENABLE_V201
#include <MicroOcpp/Model/Availability/AvailabilityService.h>
namespace MicroOcpp {
namespace Ocpp201 {
ChangeAvailability::ChangeAvailability(AvailabilityService& availabilityService) : MemoryManaged("v201.Operation.", "ChangeAvailability"), availabilityService(availabilityService) {
}
const char* ChangeAvailability::getOperationType(){
return "ChangeAvailability";
}
void ChangeAvailability::processReq(JsonObject payload) {
unsigned int evseId = 0;
if (payload.containsKey("evse")) {
int evseIdRaw = payload["evse"]["id"] | -1;
if (evseIdRaw < 0) {
errorCode = "FormationViolation";
return;
}
evseId = (unsigned int)evseIdRaw;
if ((payload["evse"]["connectorId"] | 1) != 1) {
errorCode = "PropertyConstraintViolation";
return;
}
}
auto availabilityEvse = availabilityService.getEvse(evseId);
if (!availabilityEvse) {
errorCode = "PropertyConstraintViolation";
return;
}
const char *type = payload["operationalStatus"] | "_Undefined";
bool operative = false;
if (!strcmp(type, "Operative")) {
operative = true;
} else if (!strcmp(type, "Inoperative")) {
operative = false;
} else {
errorCode = "PropertyConstraintViolation";
return;
}
status = availabilityEvse->changeAvailability(operative);
}
std::unique_ptr<JsonDoc> ChangeAvailability::createConf(){
auto doc = makeJsonDoc(getMemoryTag(), JSON_OBJECT_SIZE(1));
JsonObject payload = doc->to<JsonObject>();
switch (status) {
case ChangeAvailabilityStatus::Accepted:
payload["status"] = "Accepted";
break;
case ChangeAvailabilityStatus::Scheduled:
payload["status"] = "Scheduled";
break;
case ChangeAvailabilityStatus::Rejected:
payload["status"] = "Rejected";
break;
}
return doc;
}
} // namespace Ocpp201
} // namespace MicroOcpp
#endif //MO_ENABLE_V201