forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReset.cpp
More file actions
377 lines (285 loc) · 12.8 KB
/
Reset.cpp
File metadata and controls
377 lines (285 loc) · 12.8 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2024
// MIT License
#include <MicroOcpp/Version.h>
#if MO_ENABLE_V201
#include <MicroOcpp.h>
#include <MicroOcpp/Core/Connection.h>
#include <MicroOcpp/Core/Context.h>
#include <MicroOcpp/Core/Request.h>
#include <MicroOcpp/Model/Model.h>
#include <MicroOcpp/Model/Transactions/TransactionService.h>
#include <MicroOcpp/Model/Reset/ResetService.h>
#include <MicroOcpp/Model/Variables/VariableService.h>
#include <MicroOcpp/Operations/CustomOperation.h>
#include <MicroOcpp/Debug.h>
#include <catch2/catch.hpp>
#include "./helpers/testHelper.h"
#define BASE_TIME "2023-01-01T00:00:00.000Z"
using namespace MicroOcpp;
TEST_CASE( "Reset" ) {
printf("\nRun %s\n", "Reset");
//initialize Context with dummy socket
LoopbackConnection loopback;
mocpp_initialize(loopback,
ChargerCredentials("test-runner1234"),
makeDefaultFilesystemAdapter(FilesystemOpt::Use_Mount_FormatOnFail),
false,
ProtocolVersion(2,0,1));
auto context = getOcppContext();
mocpp_set_timer(custom_timer_cb);
getOcppContext()->getOperationRegistry().registerOperation("Authorize", [] () {
return new Ocpp16::CustomOperation("Authorize",
[] (JsonObject) {}, //ignore req
[] () {
//create conf
auto doc = makeJsonDoc("UnitTests", 2 * JSON_OBJECT_SIZE(1));
auto payload = doc->to<JsonObject>();
payload["idTokenInfo"]["status"] = "Accepted";
return doc;
});});
getOcppContext()->getOperationRegistry().registerOperation("TransactionEvent", [] () {
return new Ocpp16::CustomOperation("TransactionEvent",
[] (JsonObject) {}, //ignore req
[] () {
//create conf
auto doc = makeJsonDoc("UnitTests", 2 * JSON_OBJECT_SIZE(1));
auto payload = doc->to<JsonObject>();
payload["idTokenInfo"]["status"] = "Accepted";
return doc;
});});
// Register Reset handlers
bool checkNotified [MO_NUM_EVSEID] = {false};
bool checkExecuted [MO_NUM_EVSEID] = {false};
setOnResetNotify([&checkNotified] (bool) {
MO_DBG_DEBUG("Notify");
checkNotified[0] = true;
return true;
});
context->getModel().getResetServiceV201()->setExecuteReset([&checkExecuted] () {
MO_DBG_DEBUG("Execute");
checkExecuted[0] = true;
return false; // Reset fails because we're not actually exiting the process
});
for (size_t i = 1; i < MO_NUM_EVSEID; i++) {
context->getModel().getResetServiceV201()->setNotifyReset([&checkNotified, i] (ResetType) {
MO_DBG_DEBUG("Notify %zu", i);
checkNotified[i] = true;
return true;
}, i);
context->getModel().getResetServiceV201()->setExecuteReset([&checkExecuted, i] () {
MO_DBG_DEBUG("Execute %zu", i);
checkExecuted[i] = true;
return true;
}, i);
}
loop();
SECTION("B11 - Reset - Without ongoing transaction") {
MO_MEM_RESET();
bool checkProcessed = false;
auto resetRequest = makeRequest(new Ocpp16::CustomOperation(
"Reset",
[] () {
//create req
auto doc = makeJsonDoc("UnitTests", JSON_OBJECT_SIZE(1));
auto payload = doc->to<JsonObject>();
payload["type"] = "OnIdle";
return doc;},
[&checkProcessed] (JsonObject payload) {
//receive conf
checkProcessed = true;
REQUIRE(!strcmp(payload["status"], "Accepted"));
}
));
context->initiateRequest(std::move(resetRequest));
loop();
mtime += 30000; // Reset has some delays to ensure that the WS is not cut off immediately
loop();
REQUIRE(checkProcessed);
for (size_t i = 0; i < MO_NUM_EVSEID; i++) {
REQUIRE( checkNotified[i] );
}
MO_MEM_PRINT_STATS();
}
SECTION("Schedule full charger Reset") {
REQUIRE( context->getModel().getTransactionService()->getEvse(1)->getTransaction() == nullptr );
REQUIRE( context->getModel().getTransactionService()->getEvse(2)->getTransaction() == nullptr );
context->getModel().getTransactionService()->getEvse(1)->beginAuthorization("mIdToken");
setConnectorPluggedInput([] () {return true;}, 1);
setEvReadyInput([] () {return true;}, 1);
setEvseReadyInput([] () {return true;}, 1);
context->getModel().getTransactionService()->getEvse(2)->beginAuthorization("mIdToken2");
setConnectorPluggedInput([] () {return true;}, 2);
setEvReadyInput([] () {return true;}, 2);
setEvseReadyInput([] () {return true;}, 2);
loop();
REQUIRE( ocppPermitsCharge(1) );
REQUIRE( ocppPermitsCharge(2) );
bool checkProcessed = false;
auto resetRequest = makeRequest(new Ocpp16::CustomOperation(
"Reset",
[] () {
//create req
auto doc = makeJsonDoc("UnitTests", JSON_OBJECT_SIZE(1));
auto payload = doc->to<JsonObject>();
payload["type"] = "OnIdle";
return doc;},
[&checkProcessed] (JsonObject payload) {
//receive conf
checkProcessed = true;
REQUIRE(!strcmp(payload["status"], "Scheduled"));
}
));
context->initiateRequest(std::move(resetRequest));
loop();
mtime += 30000; // Reset has some delays to ensure that the WS is not cut off immediately
loop();
REQUIRE(checkProcessed);
for (size_t i = 0; i < MO_NUM_EVSEID; i++) {
REQUIRE( checkNotified[i] );
}
// Still scheduled
REQUIRE( ocppPermitsCharge(1) );
REQUIRE( ocppPermitsCharge(2) );
context->getModel().getTransactionService()->getEvse(1)->endAuthorization("mIdToken");
setConnectorPluggedInput([] () {return false;}, 1);
setEvReadyInput([] () {return false;}, 1);
setEvseReadyInput([] () {return false;}, 1);
loop();
// Still scheduled
REQUIRE( !ocppPermitsCharge(1) );
REQUIRE( ocppPermitsCharge(2) );
//REQUIRE( getChargePointStatus(1) == ChargePointStatus_Unavailable ); //change: Reset doesn't lead to Unavailable state
context->getModel().getTransactionService()->getEvse(2)->endAuthorization("mIdToken");
setConnectorPluggedInput([] () {return false;}, 2);
setEvReadyInput([] () {return false;}, 2);
setEvseReadyInput([] () {return false;}, 2);
loop();
mtime += 30000; // Reset has some delays to ensure that the WS is not cut off immediately
loop();
// Not scheduled anymore; execute Reset
REQUIRE( !ocppPermitsCharge(1) );
REQUIRE( !ocppPermitsCharge(2) );
REQUIRE( checkExecuted[0] );
// Technically, Reset failed at this point, because the program is still running. Check if connectors are Available agin
REQUIRE( getChargePointStatus(1) == ChargePointStatus_Available );
REQUIRE( getChargePointStatus(2) == ChargePointStatus_Available );
}
SECTION("Immediate full charger Reset") {
context->getModel().getVariableService()->declareVariable<const char*>("TxCtrlr", "TxStartPoint", "")->setString("Authorized");
REQUIRE( context->getModel().getTransactionService()->getEvse(1)->getTransaction() == nullptr );
REQUIRE( context->getModel().getTransactionService()->getEvse(2)->getTransaction() == nullptr );
context->getModel().getTransactionService()->getEvse(1)->beginAuthorization("mIdToken");
context->getModel().getTransactionService()->getEvse(2)->beginAuthorization("mIdToken2");
loop();
MO_MEM_RESET();
REQUIRE( ocppPermitsCharge(1) );
REQUIRE( ocppPermitsCharge(2) );
bool checkProcessedTx = false;
getOcppContext()->getOperationRegistry().registerOperation("TransactionEvent", [&checkProcessedTx] () {
return new Ocpp16::CustomOperation("TransactionEvent",
[&checkProcessedTx] (JsonObject payload) {
//process req
checkProcessedTx = true;
REQUIRE(!strcmp(payload["eventType"], "Ended"));
REQUIRE(!strcmp(payload["triggerReason"], "ResetCommand"));
REQUIRE(!strcmp(payload["transactionInfo"]["stoppedReason"], "ImmediateReset"));
},
[] () {
//create conf
return createEmptyDocument();
});});
bool checkProcessed = false;
auto resetRequest = makeRequest(new Ocpp16::CustomOperation(
"Reset",
[] () {
//create req
auto doc = makeJsonDoc("UnitTests", JSON_OBJECT_SIZE(1));
auto payload = doc->to<JsonObject>();
payload["type"] = "Immediate";
return doc;},
[&checkProcessed] (JsonObject payload) {
//receive conf
checkProcessed = true;
REQUIRE(!strcmp(payload["status"], "Accepted"));
}
));
context->initiateRequest(std::move(resetRequest));
loop();
mtime += 30000; // Reset has some delays to ensure that the WS is not cut off immediately
loop();
REQUIRE(checkProcessed);
REQUIRE(checkProcessedTx);
for (size_t i = 0; i < MO_NUM_EVSEID; i++) {
REQUIRE( checkNotified[i] );
}
// Stopped Tx
REQUIRE( !ocppPermitsCharge(1) );
REQUIRE( !ocppPermitsCharge(2) );
REQUIRE( checkExecuted[0] );
MO_MEM_PRINT_STATS();
loop();
// Technically, Reset failed at this point, because the program is still running. Check if connectors are Available agin
REQUIRE( getChargePointStatus(1) == ChargePointStatus_Available );
REQUIRE( getChargePointStatus(2) == ChargePointStatus_Available );
}
SECTION("Reject Reset") {
context->getModel().getResetServiceV201()->setNotifyReset([&checkNotified] (ResetType) {
MO_DBG_DEBUG("Reject Reset");
checkNotified[2] = true;
return false;
}, 2);
bool checkProcessed = false;
auto resetRequest = makeRequest(new Ocpp16::CustomOperation(
"Reset",
[] () {
//create req
auto doc = makeJsonDoc("UnitTests", JSON_OBJECT_SIZE(1));
auto payload = doc->to<JsonObject>();
payload["type"] = "Immediate";
return doc;},
[&checkProcessed] (JsonObject payload) {
//receive conf
checkProcessed = true;
REQUIRE(!strcmp(payload["status"], "Rejected"));
}
));
context->initiateRequest(std::move(resetRequest));
loop();
REQUIRE(checkProcessed);
REQUIRE(checkNotified[2]);
REQUIRE( getChargePointStatus(0) == ChargePointStatus_Available );
REQUIRE( getChargePointStatus(1) == ChargePointStatus_Available );
REQUIRE( getChargePointStatus(2) == ChargePointStatus_Available );
}
SECTION("Reset single EVSE") {
bool checkProcessed = false;
auto resetRequest = makeRequest(new Ocpp16::CustomOperation(
"Reset",
[] () {
//create req
auto doc = makeJsonDoc("UnitTests", JSON_OBJECT_SIZE(2));
auto payload = doc->to<JsonObject>();
payload["type"] = "OnIdle";
payload["evseId"] = 1;
return doc;},
[&checkProcessed] (JsonObject payload) {
//receive conf
checkProcessed = true;
REQUIRE(!strcmp(payload["status"], "Accepted"));
}
));
context->initiateRequest(std::move(resetRequest));
loop();
REQUIRE(checkProcessed);
REQUIRE(checkNotified[1]);
//REQUIRE( getChargePointStatus(1) == ChargePointStatus_Unavailable ); //change: Reset doesn't lead to Unavailable state
REQUIRE( getChargePointStatus(2) == ChargePointStatus_Available );
mtime += 30000; // Reset has some delays to ensure that the WS is not cut off immediately
loop();
REQUIRE(checkExecuted[1]);
REQUIRE( getChargePointStatus(1) == ChargePointStatus_Available );
}
mocpp_deinitialize();
}
#endif // MO_ENABLE_V201