forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotifyReport.cpp
More file actions
242 lines (202 loc) · 8.81 KB
/
NotifyReport.cpp
File metadata and controls
242 lines (202 loc) · 8.81 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2024
// MIT License
#include <MicroOcpp/Version.h>
#if MO_ENABLE_V201
#include <MicroOcpp/Operations/NotifyReport.h>
#include <MicroOcpp/Model/Model.h>
#include <MicroOcpp/Model/Variables/Variable.h>
#include <MicroOcpp/Debug.h>
using namespace MicroOcpp::Ocpp201;
using MicroOcpp::JsonDoc;
NotifyReport::NotifyReport(Model& model, int requestId, const Timestamp& generatedAt, bool tbc, int seqNo, const Vector<Variable*>& reportData)
: MemoryManaged("v201.Operation.", "NotifyReport"), model(model), requestId(requestId), generatedAt(generatedAt), tbc(tbc), seqNo(seqNo), reportData(reportData) {
}
const char* NotifyReport::getOperationType() {
return "NotifyReport";
}
std::unique_ptr<JsonDoc> NotifyReport::createReq() {
#define VALUE_BUFSIZE 30 // for primitives (int)
const Variable::AttributeType enumerateAttributeTypes [] = {
Variable::AttributeType::Actual,
Variable::AttributeType::Target,
Variable::AttributeType::MinSet,
Variable::AttributeType::MaxSet
};
size_t capacity =
JSON_OBJECT_SIZE(5) + //total of 5 fields
JSONDATE_LENGTH + 1; //timestamp string
capacity += JSON_ARRAY_SIZE(reportData.size());
for (auto variable : reportData) {
capacity += JSON_OBJECT_SIZE(4); //total of 4 fields
capacity += 2 * JSON_OBJECT_SIZE(2); //component composite
capacity += JSON_OBJECT_SIZE(1); //variable composite
size_t nAttributes = 0;
size_t valueCapacity = 0;
for (auto attributeType : enumerateAttributeTypes) {
if (!variable->hasAttribute(attributeType)) {
continue;
}
nAttributes++;
switch (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", 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(variable->getString()); // TODO limit by ReportingValueSize
break;
default:
MO_DBG_ERR("internal error");
break;
}
}
capacity += nAttributes * JSON_OBJECT_SIZE(5); //variableAttribute composite
capacity += valueCapacity; //variableAttribute value total size
capacity += JSON_OBJECT_SIZE(2); //variableCharacteristics composite: only send two data fields
}
auto doc = makeJsonDoc(getMemoryTag(), capacity);
JsonObject payload = doc->to<JsonObject>();
payload["requestId"] = requestId;
char generatedAtCstr [JSONDATE_LENGTH + 1];
generatedAt.toJsonString(generatedAtCstr, sizeof(generatedAtCstr));
payload["generatedAt"] = generatedAtCstr;
if (tbc) {
payload["tbc"] = true;
}
payload["seqNo"] = seqNo;
JsonArray reportDataJsonArray = payload.createNestedArray("reportData");
for (auto variable : reportData) {
JsonObject reportDataJson = reportDataJsonArray.createNestedObject();
reportDataJson["component"]["name"] = (char*) variable->getComponentId().name; // force copy-mode
if (variable->getComponentId().evse.id >= 0) {
reportDataJson["component"]["evse"]["id"] = variable->getComponentId().evse.id;
}
if (variable->getComponentId().evse.connectorId >= 0) {
reportDataJson["component"]["evse"]["connectorId"] = variable->getComponentId().evse.connectorId;
}
reportDataJson["variable"]["name"] = (char*) variable->getName(); // force copy-mode
JsonArray variableAttribute = reportDataJson.createNestedArray("variableAttribute");
for (auto attributeType : enumerateAttributeTypes) {
if (!variable->hasAttribute(attributeType)) {
continue;
}
JsonObject attribute = variableAttribute.createNestedObject();
const char *attributeTypeCstr = nullptr;
switch (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) {
attribute["type"] = attributeTypeCstr;
}
if (variable->getMutability() != Variable::Mutability::WriteOnly) {
switch (variable->getInternalDataType()) {
case Variable::InternalDataType::Int: {
char valbuf [VALUE_BUFSIZE];
auto ret = snprintf(valbuf, VALUE_BUFSIZE, "%i", variable->getInt());
if (ret < 0 || ret >= VALUE_BUFSIZE) {
break;
}
attribute["value"] = valbuf;
break;
}
case Variable::InternalDataType::Bool:
attribute["value"] = variable->getBool() ? "true" : "false";
break;
case Variable::InternalDataType::String:
attribute["value"] = (char*) variable->getString(); // force zero-copy mode
break;
default:
MO_DBG_ERR("internal error");
break;
}
}
const char *mutabilityCstr = nullptr;
switch (variable->getMutability()) {
case Variable::Mutability::ReadOnly:
mutabilityCstr = "ReadOnly";
break;
case Variable::Mutability::WriteOnly:
mutabilityCstr = "WriteOnly";
break;
case Variable::Mutability::ReadWrite:
// leave blank when ReadWrite
break;
default:
MO_DBG_ERR("internal error");
break;
}
if (mutabilityCstr) {
attribute["mutability"] = mutabilityCstr;
}
if (variable->isPersistent()) {
attribute["persistent"] = true;
}
if (variable->isConstant()) {
attribute["constant"] = true;
}
}
JsonObject variableCharacteristics = reportDataJson.createNestedObject("variableCharacteristics");
const char *dataTypeCstr = "";
switch (variable->getVariableDataType()) {
case VariableCharacteristics::DataType::string:
dataTypeCstr = "string";
break;
case VariableCharacteristics::DataType::decimal:
dataTypeCstr = "decimal";
break;
case VariableCharacteristics::DataType::integer:
dataTypeCstr = "integer";
break;
case VariableCharacteristics::DataType::dateTime:
dataTypeCstr = "dateTime";
break;
case VariableCharacteristics::DataType::boolean:
dataTypeCstr = "boolean";
break;
case VariableCharacteristics::DataType::OptionList:
dataTypeCstr = "OptionList";
break;
case VariableCharacteristics::DataType::SequenceList:
dataTypeCstr = "SequenceList";
break;
case VariableCharacteristics::DataType::MemberList:
dataTypeCstr = "MemberList";
break;
default:
MO_DBG_ERR("internal error");
break;
}
variableCharacteristics["dataType"] = dataTypeCstr;
variableCharacteristics["supportsMonitoring"] = variable->getSupportsMonitoring();
}
return doc;
}
void NotifyReport::processConf(JsonObject payload) {
// empty payload
}
#endif // MO_ENABLE_V201