forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetVariables.cpp
More file actions
228 lines (191 loc) · 8.34 KB
/
GetVariables.cpp
File metadata and controls
228 lines (191 loc) · 8.34 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
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
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2024
// MIT License
#include <MicroOcpp/Version.h>
#if MO_ENABLE_V201
#include <MicroOcpp/Operations/GetVariables.h>
#include <MicroOcpp/Model/Variables/VariableService.h>
#include <MicroOcpp/Debug.h>
#include <cctype> //for tolower
using MicroOcpp::Ocpp201::GetVariableData;
using MicroOcpp::Ocpp201::GetVariables;
using MicroOcpp::JsonDoc;
GetVariableData::GetVariableData(const char *memory_tag) : componentName{makeString(memory_tag)}, variableName{makeString(memory_tag)} {
}
GetVariables::GetVariables(VariableService& variableService) : MemoryManaged("v201.Operation.", "GetVariables"), variableService(variableService), queries(makeVector<GetVariableData>(getMemoryTag())) {
}
const char* GetVariables::getOperationType(){
return "GetVariables";
}
void GetVariables::processReq(JsonObject payload) {
for (JsonObject getVariable : payload["getVariableData"].as<JsonArray>()) {
queries.emplace_back(getMemoryTag());
auto& data = queries.back();
if (getVariable.containsKey("attributeType")) {
const char *attributeTypeCstr = getVariable["attributeType"] | "_Undefined";
if (!strcmp(attributeTypeCstr, "Actual")) {
data.attributeType = Variable::AttributeType::Actual;
} else if (!strcmp(attributeTypeCstr, "Target")) {
data.attributeType = Variable::AttributeType::Target;
} else if (!strcmp(attributeTypeCstr, "MinSet")) {
data.attributeType = Variable::AttributeType::MinSet;
} else if (!strcmp(attributeTypeCstr, "MaxSet")) {
data.attributeType = Variable::AttributeType::MaxSet;
} else {
errorCode = "FormationViolation";
MO_DBG_ERR("invalid attributeType");
return;
}
}
const char *componentNameCstr = getVariable["component"]["name"] | (const char*) nullptr;
const char *variableNameCstr = getVariable["variable"]["name"] | (const char*) nullptr;
if (!componentNameCstr ||
!variableNameCstr) {
errorCode = "FormationViolation";
return;
}
data.componentName = componentNameCstr;
data.variableName = variableNameCstr;
// TODO check against ConfigurationValueSize
data.componentEvseId = getVariable["component"]["evse"]["id"] | -1;
data.componentEvseConnectorId = getVariable["component"]["evse"]["connectorId"] | -1;
if (getVariable["component"].containsKey("evse") && data.componentEvseId < 0) {
errorCode = "FormationViolation";
MO_DBG_ERR("malformatted / missing evseId");
return;
}
}
if (queries.empty()) {
errorCode = "FormationViolation";
return;
}
}
std::unique_ptr<JsonDoc> GetVariables::createConf(){
// process GetVariables queries
for (auto& query : queries) {
query.attributeStatus = variableService.getVariable(
query.attributeType,
ComponentId(query.componentName.c_str(),
EvseId(query.componentEvseId, query.componentEvseConnectorId)),
query.variableName.c_str(),
&query.variable);
}
#define VALUE_BUFSIZE 30 // for primitives (int)
size_t capacity = JSON_ARRAY_SIZE(queries.size());
for (const auto& data : queries) {
size_t valueCapacity = 0;
if (data.variable) {
switch (data.variable->getInternalDataType()) {
case Variable::InternalDataType::Int: {
// measure int size by printing to a dummy buf
char valbuf [VALUE_BUFSIZE];
auto ret = snprintf(valbuf, VALUE_BUFSIZE, "%i", data.variable->getInt());
if (ret < 0 || ret >= VALUE_BUFSIZE) {
continue;
}
valueCapacity = (size_t) ret + 1;
break;
}
case Variable::InternalDataType::Bool:
// bool will be stored in zero-copy mode (string literal "true" or "false")
valueCapacity = 0;
break;
case Variable::InternalDataType::String:
valueCapacity = strlen(data.variable->getString()); // TODO limit by ReportingValueSize
break;
default:
MO_DBG_ERR("internal error");
break;
}
}
capacity +=
JSON_OBJECT_SIZE(5) + // getVariableResult
valueCapacity + // capacity needed for storing the value
JSON_OBJECT_SIZE(2) + // component
data.componentName.length() + 1 +
JSON_OBJECT_SIZE(2) + // evse
JSON_OBJECT_SIZE(2) + // variable
data.variableName.length() + 1;
}
auto doc = makeJsonDoc(getMemoryTag(), capacity);
JsonObject payload = doc->to<JsonObject>();
JsonArray getVariableResult = payload.createNestedArray("getVariableResult");
for (const auto& data : queries) {
JsonObject getVariable = getVariableResult.createNestedObject();
const char *attributeStatusCstr = "Rejected";
switch (data.attributeStatus) {
case GetVariableStatus::Accepted:
attributeStatusCstr = "Accepted";
break;
case GetVariableStatus::Rejected:
attributeStatusCstr = "Rejected";
break;
case GetVariableStatus::UnknownComponent:
attributeStatusCstr = "UnknownComponent";
break;
case GetVariableStatus::UnknownVariable:
attributeStatusCstr = "UnknownVariable";
break;
case GetVariableStatus::NotSupportedAttributeType:
attributeStatusCstr = "NotSupportedAttributeType";
break;
default:
MO_DBG_ERR("internal error");
break;
}
getVariable["attributeStatus"] = attributeStatusCstr;
const char *attributeTypeCstr = nullptr;
switch (data.attributeType) {
case Variable::AttributeType::Actual:
// leave blank when Actual
break;
case Variable::AttributeType::Target:
attributeTypeCstr = "Target";
break;
case Variable::AttributeType::MinSet:
attributeTypeCstr = "MinSet";
break;
case Variable::AttributeType::MaxSet:
attributeTypeCstr = "MaxSet";
break;
default:
MO_DBG_ERR("internal error");
break;
}
if (attributeTypeCstr) {
getVariable["attributeType"] = attributeTypeCstr;
}
if (data.variable) {
switch (data.variable->getInternalDataType()) {
case Variable::InternalDataType::Int: {
char valbuf [VALUE_BUFSIZE];
auto ret = snprintf(valbuf, VALUE_BUFSIZE, "%i", data.variable->getInt());
if (ret < 0 || ret >= VALUE_BUFSIZE) {
break;
}
getVariable["attributeValue"] = valbuf;
break;
}
case Variable::InternalDataType::Bool:
getVariable["attributeValue"] = data.variable->getBool() ? "true" : "false";
break;
case Variable::InternalDataType::String:
getVariable["attributeValue"] = (char*) data.variable->getString(); // force zero-copy mode
break;
default:
MO_DBG_ERR("internal error");
break;
}
}
getVariable["component"]["name"] = (char*) data.componentName.c_str(); // force copy-mode
if (data.componentEvseId >= 0) {
getVariable["component"]["evse"]["id"] = data.componentEvseId;
}
if (data.componentEvseConnectorId >= 0) {
getVariable["component"]["evse"]["connectorId"] = data.componentEvseConnectorId;
}
getVariable["variable"]["name"] = (char*) data.variableName.c_str(); // force copy-mode
}
return doc;
}
#endif // MO_ENABLE_V201