forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResetService.cpp
More file actions
113 lines (90 loc) · 3.68 KB
/
Copy pathResetService.cpp
File metadata and controls
113 lines (90 loc) · 3.68 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
// matth-x/ArduinoOcpp
// Copyright Matthias Akstaller 2019 - 2023
// MIT License
#include <ArduinoOcpp/Model/Reset/ResetService.h>
#include <ArduinoOcpp/Core/Context.h>
#include <ArduinoOcpp/Model/Model.h>
#include <ArduinoOcpp/Core/SimpleRequestFactory.h>
#include <ArduinoOcpp/Core/Configuration.h>
#include <ArduinoOcpp/Operations/Reset.h>
#include <ArduinoOcpp/Operations/Authorize.h>
#include <ArduinoOcpp/Operations/StartTransaction.h>
#include <ArduinoOcpp/Operations/StatusNotification.h>
#include <ArduinoOcpp/Operations/StopTransaction.h>
#include <ArduinoOcpp/Debug.h>
#include <memory>
#include <string.h>
#define RESET_DELAY 15000
using namespace ArduinoOcpp;
ResetService::ResetService(Context& context)
: context(context) {
resetRetries = declareConfiguration<int>("ResetRetries", 2, CONFIGURATION_FN, true, true, false, false);
context.getOperationRegistry().registerOperation("Reset", [&context] () {
return new Ocpp16::Reset(context.getModel());});
}
ResetService::~ResetService() {
}
void ResetService::loop() {
if (outstandingResetRetries > 0 && ao_tick_ms() - t_resetRetry >= RESET_DELAY) {
t_resetRetry = ao_tick_ms();
outstandingResetRetries--;
if (executeReset) {
AO_DBG_INFO("Reset device");
executeReset(isHardReset);
} else {
AO_DBG_ERR("No Reset function set! Abort");
outstandingResetRetries = 0;
}
AO_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::NOT_SET;
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 + *resetRetries; //one initial try + no. of retries
if (outstandingResetRetries > 5) {
AO_DBG_ERR("no. of reset trials exceeds 5");
outstandingResetRetries = 5;
}
t_resetRetry = ao_tick_ms();
for (unsigned int cId = 0; cId < context.getModel().getNumConnectors(); cId++) {
auto connector = context.getModel().getConnector(cId);
connector->setAvailabilityVolatile(false);
}
}
#if AO_PLATFORM == AO_PLATFORM_ARDUINO && (defined(ESP32) || defined(ESP8266))
std::function<void(bool isHard)> ArduinoOcpp::makeDefaultResetFn() {
return [] (bool isHard) {
AO_DBG_DEBUG("Perform ESP reset");
ESP.restart();
};
}
#endif //AO_PLATFORM == AO_PLATFORM_ARDUINO && (defined(ESP32) || defined(ESP8266))