-
Notifications
You must be signed in to change notification settings - Fork 140
/
ResetService.cpp
340 lines (276 loc) · 11 KB
/
ResetService.cpp
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
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2024
// MIT License
#include <MicroOcpp/Model/Reset/ResetService.h>
#include <MicroOcpp/Core/Context.h>
#include <MicroOcpp/Model/Model.h>
#include <MicroOcpp/Core/SimpleRequestFactory.h>
#include <MicroOcpp/Core/Configuration.h>
#include <MicroOcpp/Operations/Reset.h>
#include <MicroOcpp/Operations/Authorize.h>
#include <MicroOcpp/Operations/StartTransaction.h>
#include <MicroOcpp/Operations/StatusNotification.h>
#include <MicroOcpp/Operations/StopTransaction.h>
#include <MicroOcpp/Model/Variables/VariableService.h>
#include <MicroOcpp/Model/Transactions/TransactionService.h>
#include <MicroOcpp/Debug.h>
#ifndef MO_RESET_DELAY
#define MO_RESET_DELAY 10000
#endif
using namespace MicroOcpp;
ResetService::ResetService(Context& context)
: context(context) {
resetRetriesInt = declareConfiguration<int>("ResetRetries", 2);
context.getOperationRegistry().registerOperation("Reset", [&context] () {
return new Ocpp16::Reset(context.getModel());});
}
ResetService::~ResetService() {
}
void ResetService::loop() {
if (outstandingResetRetries > 0 && mocpp_tick_ms() - t_resetRetry >= MO_RESET_DELAY) {
t_resetRetry = mocpp_tick_ms();
outstandingResetRetries--;
if (executeReset) {
MO_DBG_INFO("Reset device");
executeReset(isHardReset);
} else {
MO_DBG_ERR("No Reset function set! Abort");
outstandingResetRetries = 0;
}
MO_DBG_ERR("Reset device failure. %s", outstandingResetRetries == 0 ? "Abort" : "Retry");
if (outstandingResetRetries <= 0) {
for (unsigned int cId = 0; cId < context.getModel().getNumConnectors(); cId++) {
auto connector = context.getModel().getConnector(cId);
connector->setAvailabilityVolatile(true);
}
ChargePointStatus cpStatus = ChargePointStatus_UNDEFINED;
if (context.getModel().getNumConnectors() > 0) {
cpStatus = context.getModel().getConnector(0)->getStatus();
}
auto statusNotification = makeRequest(new Ocpp16::StatusNotification(
0,
cpStatus, //will be determined in StatusNotification::initiate
context.getModel().getClock().now(),
"ResetFailure"));
statusNotification->setTimeout(60000);
context.initiateRequest(std::move(statusNotification));
}
}
}
void ResetService::setPreReset(std::function<bool(bool)> preReset) {
this->preReset = preReset;
}
std::function<bool(bool)> ResetService::getPreReset() {
return this->preReset;
}
void ResetService::setExecuteReset(std::function<void(bool)> executeReset) {
this->executeReset = executeReset;
}
std::function<void(bool)> ResetService::getExecuteReset() {
return this->executeReset;
}
void ResetService::initiateReset(bool isHard) {
isHardReset = isHard;
outstandingResetRetries = 1 + resetRetriesInt->getInt(); //one initial try + no. of retries
if (outstandingResetRetries > 5) {
MO_DBG_ERR("no. of reset trials exceeds 5");
outstandingResetRetries = 5;
}
t_resetRetry = mocpp_tick_ms();
for (unsigned int cId = 0; cId < context.getModel().getNumConnectors(); cId++) {
auto connector = context.getModel().getConnector(cId);
connector->setAvailabilityVolatile(false);
}
}
#if MO_PLATFORM == MO_PLATFORM_ARDUINO && (defined(ESP32) || defined(ESP8266))
std::function<void(bool isHard)> MicroOcpp::makeDefaultResetFn() {
return [] (bool isHard) {
MO_DBG_DEBUG("Perform ESP reset");
ESP.restart();
};
}
#endif //MO_PLATFORM == MO_PLATFORM_ARDUINO && (defined(ESP32) || defined(ESP8266))
#if MO_ENABLE_V201
namespace MicroOcpp {
namespace Ocpp201 {
ResetService::ResetService(Context& context)
: context(context) {
auto varService = context.getModel().getVariableService();
resetRetriesInt = varService->declareVariable<int>("OCPPCommCtrlr", "ResetRetries", 0);
context.getOperationRegistry().registerOperation("Reset", [this] () {
return new Ocpp201::Reset(*this);});
}
ResetService::~ResetService() {
}
ResetService::Evse::Evse(Context& context, ResetService& resetService, unsigned int evseId) : context(context), resetService(resetService), evseId(evseId) {
auto varService = context.getModel().getVariableService();
varService->declareVariable<bool>(ComponentId("EVSE", evseId), "AllowReset", true, MO_VARIABLE_FN, Variable::Mutability::ReadOnly);
}
void ResetService::Evse::loop() {
if (outstandingResetRetries && awaitTxStop) {
for (unsigned int eId = std::max(1U, evseId); eId < (evseId == 0 ? MO_NUM_EVSE : evseId + 1); eId++) {
//If evseId > 0, execute this block one time for evseId. If evseId == 0, then iterate over all evseIds > 0
auto txService = context.getModel().getTransactionService();
if (txService && txService->getEvse(eId) && txService->getEvse(eId)->getTransaction()) {
auto tx = txService->getEvse(eId)->getTransaction();
if (!tx->stopped) {
// wait until tx stopped
return;
}
}
}
awaitTxStop = false;
MO_DBG_INFO("Reset - tx stopped");
t_resetRetry = mocpp_tick_ms(); // wait for some more time until final reset
}
if (outstandingResetRetries && mocpp_tick_ms() - t_resetRetry >= MO_RESET_DELAY) {
t_resetRetry = mocpp_tick_ms();
outstandingResetRetries--;
MO_DBG_INFO("Reset device");
bool success = executeReset();
if (success) {
outstandingResetRetries = 0;
if (evseId != 0) {
//Set this EVSE Available again
if (auto connector = context.getModel().getConnector(evseId)) {
connector->setAvailabilityVolatile(true);
}
}
} else if (!outstandingResetRetries) {
MO_DBG_ERR("Reset device failure");
if (evseId == 0) {
//Set all EVSEs Available again
for (unsigned int cId = 0; cId < context.getModel().getNumConnectors(); cId++) {
auto connector = context.getModel().getConnector(cId);
connector->setAvailabilityVolatile(true);
}
} else {
//Set only this EVSE Available
if (auto connector = context.getModel().getConnector(evseId)) {
connector->setAvailabilityVolatile(true);
}
}
}
}
}
ResetService::Evse *ResetService::getEvse(unsigned int evseId) {
for (size_t i = 0; i < evses.size(); i++) {
if (evses[i].evseId == evseId) {
return &evses[i];
}
}
return nullptr;
}
ResetService::Evse *ResetService::getOrCreateEvse(unsigned int evseId) {
if (auto evse = getEvse(evseId)) {
return evse;
}
if (evseId >= MO_NUM_EVSE) {
MO_DBG_ERR("evseId out of bound");
return nullptr;
}
evses.emplace_back(context, *this, evseId);
return &evses.back();
}
void ResetService::loop() {
for (Evse& evse : evses) {
evse.loop();
}
}
void ResetService::setNotifyReset(std::function<bool(ResetType)> notifyReset, unsigned int evseId) {
Evse *evse = getOrCreateEvse(evseId);
if (!evse) {
MO_DBG_ERR("evseId not found");
return;
}
evse->notifyReset = notifyReset;
}
std::function<bool(ResetType)> ResetService::getNotifyReset(unsigned int evseId) {
Evse *evse = getOrCreateEvse(evseId);
if (!evse) {
MO_DBG_ERR("evseId not found");
return nullptr;
}
return evse->notifyReset;
}
void ResetService::setExecuteReset(std::function<bool()> executeReset, unsigned int evseId) {
Evse *evse = getOrCreateEvse(evseId);
if (!evse) {
MO_DBG_ERR("evseId not found");
return;
}
evse->executeReset = executeReset;
}
std::function<bool()> ResetService::getExecuteReset(unsigned int evseId) {
Evse *evse = getOrCreateEvse(evseId);
if (!evse) {
MO_DBG_ERR("evseId not found");
return nullptr;
}
return evse->executeReset;
}
ResetStatus ResetService::initiateReset(ResetType type, unsigned int evseId) {
auto evse = getEvse(evseId);
if (!evse) {
MO_DBG_ERR("evseId not found");
return ResetStatus_Rejected;
}
if (!evse->executeReset) {
MO_DBG_INFO("EVSE %u does not support Reset", evseId);
return ResetStatus_Rejected;
}
//Check if EVSEs are ready for Reset
for (unsigned int eId = evseId; eId < (evseId == 0 ? MO_NUM_EVSE : evseId + 1); eId++) {
//If evseId > 0, execute this block one time for evseId. If evseId == 0, then iterate over all evseIds
if (auto it = getEvse(eId)) {
if (it->notifyReset && !it->notifyReset(type)) {
MO_DBG_INFO("EVSE %u not able to Reset", evseId);
return ResetStatus_Rejected;
}
}
}
//Set EVSEs Unavailable
if (evseId == 0) {
//Set all EVSEs Unavailable
for (unsigned int cId = 0; cId < context.getModel().getNumConnectors(); cId++) {
auto connector = context.getModel().getConnector(cId);
connector->setAvailabilityVolatile(false);
}
} else {
//Set this EVSE Unavailable
if (auto connector = context.getModel().getConnector(evseId)) {
connector->setAvailabilityVolatile(false);
}
}
bool scheduled = false;
//Tx-related behavior: if immediate Reset, stop txs; otherwise schedule Reset
for (unsigned int eId = std::max(1U, evseId); eId < (evseId == 0 ? MO_NUM_EVSE : evseId + 1); eId++) {
//If evseId > 0, execute this block one time for evseId. If evseId == 0, then iterate over all evseIds > 0
auto txService = context.getModel().getTransactionService();
if (txService && txService->getEvse(eId) && txService->getEvse(eId)->getTransaction()) {
auto tx = txService->getEvse(eId)->getTransaction();
if (tx->active) {
//Tx in progress. Check behavior
if (type == ResetType_Immediate) {
txService->getEvse(eId)->abortTransaction(Transaction::StopReason::ImmediateReset, TransactionEventTriggerReason::ResetCommand);
} else {
scheduled = true;
break;
}
}
}
}
//Actually engage Reset
if (resetRetriesInt->getInt() >= 5) {
MO_DBG_ERR("no. of reset trials exceeds 5");
evse->outstandingResetRetries = 5;
} else {
evse->outstandingResetRetries = 1 + resetRetriesInt->getInt(); //one initial try + no. of retries
}
evse->t_resetRetry = mocpp_tick_ms();
evse->awaitTxStop = scheduled;
return scheduled ? ResetStatus_Scheduled : ResetStatus_Accepted;
}
} //namespace MicroOcpp
} //namespace Ocpp201
#endif //MO_ENABLE_V201