forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeterStore.cpp
More file actions
299 lines (235 loc) · 8.88 KB
/
MeterStore.cpp
File metadata and controls
299 lines (235 loc) · 8.88 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
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
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2024
// MIT License
#include <MicroOcpp/Model/Metering/MeterStore.h>
#include <MicroOcpp/Core/FilesystemUtils.h>
#include <MicroOcpp/Debug.h>
#include <algorithm>
#ifndef MO_MAX_STOPTXDATA_LEN
#define MO_MAX_STOPTXDATA_LEN 4
#endif
using namespace MicroOcpp;
TransactionMeterData::TransactionMeterData(unsigned int connectorId, unsigned int txNr, std::shared_ptr<FilesystemAdapter> filesystem)
: MemoryManaged("v16.Metering.TransactionMeterData"), connectorId(connectorId), txNr(txNr), filesystem{filesystem}, txData{makeVector<std::unique_ptr<MeterValue>>(getMemoryTag())} {
if (!filesystem) {
MO_DBG_DEBUG("volatile mode");
}
}
bool TransactionMeterData::addTxData(std::unique_ptr<MeterValue> mv) {
if (isFinalized()) {
MO_DBG_ERR("immutable");
return false;
}
if (!mv) {
MO_DBG_ERR("null");
return false;
}
if (MO_MAX_STOPTXDATA_LEN <= 0) {
//txData off
return true;
}
bool replaceLast = mvCount >= MO_MAX_STOPTXDATA_LEN; //txData size exceeded? overwrite last entry instead of appending
if (filesystem) {
unsigned int mvIndex = 0;
if (replaceLast) {
mvIndex = mvCount - 1;
} else {
mvIndex = mvCount ;
}
char fn [MO_MAX_PATH_SIZE] = {'\0'};
auto ret = snprintf(fn, MO_MAX_PATH_SIZE, MO_FILENAME_PREFIX "sd" "-%u-%u-%u.jsn", connectorId, txNr, mvIndex);
if (ret < 0 || ret >= MO_MAX_PATH_SIZE) {
MO_DBG_ERR("fn error: %i", ret);
return false;
}
auto mvDoc = mv->toJson();
if (!mvDoc) {
MO_DBG_ERR("MV not ready yet");
return false;
}
if (!FilesystemUtils::storeJson(filesystem, fn, *mvDoc)) {
MO_DBG_ERR("FS error");
return false;
}
if (!replaceLast) {
mvCount++;
}
}
if (replaceLast) {
txData.back() = std::move(mv);
MO_DBG_DEBUG("updated latest sd");
} else {
txData.push_back(std::move(mv));
MO_DBG_DEBUG("added sd");
}
return true;
}
Vector<std::unique_ptr<MeterValue>> TransactionMeterData::retrieveStopTxData() {
if (isFinalized()) {
MO_DBG_ERR("Can only retrieve once");
return makeVector<std::unique_ptr<MeterValue>>(getMemoryTag());
}
finalize();
MO_DBG_DEBUG("creating sd");
return std::move(txData);
}
bool TransactionMeterData::restore(MeterValueBuilder& mvBuilder) {
if (!filesystem) {
MO_DBG_DEBUG("No FS - nothing to restore");
return true;
}
const unsigned int MISSES_LIMIT = 3;
unsigned int i = 0;
unsigned int misses = 0;
while (misses < MISSES_LIMIT) { //search until region without mvs found
char fn [MO_MAX_PATH_SIZE] = {'\0'};
auto ret = snprintf(fn, MO_MAX_PATH_SIZE, MO_FILENAME_PREFIX "sd" "-%u-%u-%u.jsn", connectorId, txNr, i);
if (ret < 0 || ret >= MO_MAX_PATH_SIZE) {
MO_DBG_ERR("fn error: %i", ret);
return false; //all files have same length
}
auto doc = FilesystemUtils::loadJson(filesystem, fn, getMemoryTag());
if (!doc) {
misses++;
i++;
continue;
}
JsonObject mvJson = doc->as<JsonObject>();
std::unique_ptr<MeterValue> mv = mvBuilder.deserializeSample(mvJson);
if (!mv) {
MO_DBG_ERR("Deserialization error");
misses++;
i++;
continue;
}
if (txData.size() >= MO_MAX_STOPTXDATA_LEN) {
MO_DBG_ERR("corrupted memory");
return false;
}
txData.push_back(std::move(mv));
i++;
mvCount = i;
misses = 0;
}
MO_DBG_DEBUG("Restored %zu meter values from sd-%u-%u-0 to %u (exclusive)", txData.size(), connectorId, txNr, mvCount);
return true;
}
MeterStore::MeterStore(std::shared_ptr<FilesystemAdapter> filesystem) : MemoryManaged("v16.Metering.MeterStore"), filesystem {filesystem}, txMeterData{makeVector<std::weak_ptr<TransactionMeterData>>(getMemoryTag())} {
if (!filesystem) {
MO_DBG_DEBUG("volatile mode");
}
}
std::shared_ptr<TransactionMeterData> MeterStore::getTxMeterData(MeterValueBuilder& mvBuilder, Transaction *transaction) {
if (!transaction || transaction->isSilent()) {
//no tx assignment -> don't store txData
//tx is silent -> no StopTx will be sent and don't store txData
return nullptr;
}
auto connectorId = transaction->getConnectorId();
auto txNr = transaction->getTxNr();
auto cached = std::find_if(txMeterData.begin(), txMeterData.end(),
[connectorId, txNr] (std::weak_ptr<TransactionMeterData>& txm) {
if (auto txml = txm.lock()) {
return txml->getConnectorId() == connectorId && txml->getTxNr() == txNr;
} else {
return false;
}
});
if (cached != txMeterData.end()) {
if (auto cachedl = cached->lock()) {
return cachedl;
}
}
//clean outdated pointers before creating new object
txMeterData.erase(std::remove_if(txMeterData.begin(), txMeterData.end(),
[] (std::weak_ptr<TransactionMeterData>& txm) {
return txm.expired();
}),
txMeterData.end());
//create new object and cache weak pointer
auto tx = std::allocate_shared<TransactionMeterData>(makeAllocator<TransactionMeterData>(getMemoryTag()), connectorId, txNr, filesystem);
if (filesystem) {
char fn [MO_MAX_PATH_SIZE] = {'\0'};
auto ret = snprintf(fn, MO_MAX_PATH_SIZE, MO_FILENAME_PREFIX "sd" "-%u-%u-%u.jsn", connectorId, txNr, 0);
if (ret < 0 || ret >= MO_MAX_PATH_SIZE) {
MO_DBG_ERR("fn error: %i", ret);
return nullptr; //cannot store
}
size_t size = 0;
bool exists = filesystem->stat(fn, &size) == 0;
if (exists) {
if (!tx->restore(mvBuilder)) {
remove(connectorId, txNr);
MO_DBG_ERR("removed corrupted tx entries");
}
}
}
txMeterData.push_back(tx);
MO_DBG_DEBUG("Added txNr %u, now holding %zu sds", txNr, txMeterData.size());
return tx;
}
bool MeterStore::remove(unsigned int connectorId, unsigned int txNr) {
unsigned int mvCount = 0;
auto cached = std::find_if(txMeterData.begin(), txMeterData.end(),
[connectorId, txNr] (std::weak_ptr<TransactionMeterData>& txm) {
if (auto txml = txm.lock()) {
return txml->getConnectorId() == connectorId && txml->getTxNr() == txNr;
} else {
return false;
}
});
if (cached != txMeterData.end()) {
if (auto cachedl = cached->lock()) {
mvCount = cachedl->getPathsCount();
cachedl->finalize();
}
}
bool success = true;
if (filesystem) {
if (mvCount == 0) {
const unsigned int MISSES_LIMIT = 3;
unsigned int misses = 0;
unsigned int i = 0;
while (misses < MISSES_LIMIT) { //search until region without mvs found
char fn [MO_MAX_PATH_SIZE] = {'\0'};
auto ret = snprintf(fn, MO_MAX_PATH_SIZE, MO_FILENAME_PREFIX "sd" "-%u-%u-%u.jsn", connectorId, txNr, i);
if (ret < 0 || ret >= MO_MAX_PATH_SIZE) {
MO_DBG_ERR("fn error: %i", ret);
return false; //all files have same length
}
size_t nsize = 0;
if (filesystem->stat(fn, &nsize) != 0) {
misses++;
i++;
continue;
}
i++;
mvCount = i;
misses = 0;
}
}
MO_DBG_DEBUG("remove %u mvs for txNr %u", mvCount, txNr);
for (unsigned int i = 0; i < mvCount; i++) {
unsigned int sd = mvCount - 1U - i;
char fn [MO_MAX_PATH_SIZE] = {'\0'};
auto ret = snprintf(fn, MO_MAX_PATH_SIZE, MO_FILENAME_PREFIX "sd" "-%u-%u-%u.jsn", connectorId, txNr, sd);
if (ret < 0 || ret >= MO_MAX_PATH_SIZE) {
MO_DBG_ERR("fn error: %i", ret);
return false;
}
success &= filesystem->remove(fn);
}
}
//clean outdated pointers
txMeterData.erase(std::remove_if(txMeterData.begin(), txMeterData.end(),
[] (std::weak_ptr<TransactionMeterData>& txm) {
return txm.expired();
}),
txMeterData.end());
if (success) {
MO_DBG_DEBUG("Removed meter values for cId %u, txNr %u", connectorId, txNr);
} else {
MO_DBG_DEBUG("corrupted fs");
}
return success;
}