forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBootService.cpp
More file actions
200 lines (163 loc) · 6.29 KB
/
Copy pathBootService.cpp
File metadata and controls
200 lines (163 loc) · 6.29 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
// matth-x/ArduinoOcpp
// Copyright Matthias Akstaller 2019 - 2023
// MIT License
#include <limits>
#include <ArduinoOcpp/Model/Boot/BootService.h>
#include <ArduinoOcpp/Core/Context.h>
#include <ArduinoOcpp/Model/Model.h>
#include <ArduinoOcpp/Core/Configuration.h>
#include <ArduinoOcpp/Core/SimpleRequestFactory.h>
#include <ArduinoOcpp/Core/FilesystemUtils.h>
#include <ArduinoOcpp/Operations/BootNotification.h>
#include <ArduinoOcpp/Platform.h>
#include <ArduinoOcpp/Debug.h>
#ifndef AO_BOOTSTATS_LONGTIME_MS
#define AO_BOOTSTATS_LONGTIME_MS 180 * 1000
#endif
using namespace ArduinoOcpp;
RegistrationStatus ArduinoOcpp::deserializeRegistrationStatus(const char *serialized) {
if (!strcmp(serialized, "Accepted")) {
return RegistrationStatus::Accepted;
} else if (!strcmp(serialized, "Pending")) {
return RegistrationStatus::Pending;
} else if (!strcmp(serialized, "Rejected")) {
return RegistrationStatus::Rejected;
} else {
AO_DBG_ERR("deserialization error");
return RegistrationStatus::UNDEFINED;
}
}
BootService::BootService(Context& context, std::shared_ptr<FilesystemAdapter> filesystem) : context(context), filesystem(filesystem) {
//if transactions can start before the BootNotification succeeds
preBootTransactions = declareConfiguration<bool>("AO_PreBootTransactions", false, CONFIGURATION_FN, true, true, true, false);
if (!preBootTransactions) {
AO_DBG_ERR("initialization error");
(void)0;
}
//Register message handler for TriggerMessage operation
context.getOperationRegistry().registerOperation("BootNotification", [this] () {
return new Ocpp16::BootNotification(this->context.getModel(), getChargePointCredentials());});
}
void BootService::loop() {
if (!executedFirstTime) {
executedFirstTime = true;
firstExecutionTimestamp = ao_tick_ms();
}
if (!executedLongTime && ao_tick_ms() - firstExecutionTimestamp >= AO_BOOTSTATS_LONGTIME_MS) {
executedLongTime = true;
AO_DBG_DEBUG("boot success timer reached");
BootStats bootstats;
loadBootStats(filesystem, bootstats);
bootstats.lastBootSuccess = bootstats.bootNr;
storeBootStats(filesystem, bootstats);
}
if (!activatedPostBootCommunication && status == RegistrationStatus::Accepted) {
context.activatePostBootCommunication();
activatedPostBootCommunication = true;
}
if (!activatedModel && (status == RegistrationStatus::Accepted || *preBootTransactions)) {
context.getModel().activateTasks();
activatedModel = true;
}
if (status == RegistrationStatus::Accepted) {
return;
}
if (ao_tick_ms() - lastBootNotification < (interval_s * 1000UL)) {
return;
}
/*
* Create BootNotification. The BootNotifaction object will fetch its paremeters from
* this class and notify this class about the response
*/
auto bootNotification = makeRequest(new Ocpp16::BootNotification(context.getModel(), getChargePointCredentials()));
bootNotification->setTimeout(interval_s * 1000UL);
context.initiatePreBootOperation(std::move(bootNotification));
lastBootNotification = ao_tick_ms();
}
void BootService::setChargePointCredentials(JsonObject credentials) {
auto written = serializeJson(credentials, cpCredentials);
if (written < 2) {
AO_DBG_ERR("serialization error");
cpCredentials = "{}";
}
}
void BootService::setChargePointCredentials(const char *credentials) {
cpCredentials = credentials;
if (cpCredentials.size() < 2) {
cpCredentials = "{}";
}
}
std::unique_ptr<DynamicJsonDocument> BootService::getChargePointCredentials() {
if (cpCredentials.size() <= 2) {
return createEmptyDocument();
}
std::unique_ptr<DynamicJsonDocument> doc;
size_t capacity = JSON_OBJECT_SIZE(9) + cpCredentials.size();
DeserializationError err = DeserializationError::NoMemory;
while (err == DeserializationError::NoMemory && capacity <= AO_MAX_JSON_CAPACITY) {
doc.reset(new DynamicJsonDocument(capacity));
err = deserializeJson(*doc, cpCredentials);
capacity *= 2;
}
if (!err) {
return doc;
} else {
AO_DBG_ERR("could not parse stored credentials: %s", err.c_str());
return nullptr;
}
}
void BootService::notifyRegistrationStatus(RegistrationStatus status) {
this->status = status;
lastBootNotification = ao_tick_ms();
}
void BootService::setRetryInterval(unsigned long interval_s) {
if (interval_s == 0) {
this->interval_s = AO_BOOT_INTERVAL_DEFAULT;
} else {
this->interval_s = interval_s;
}
lastBootNotification = ao_tick_ms();
}
bool BootService::loadBootStats(std::shared_ptr<FilesystemAdapter> filesystem, BootStats& bstats) {
if (!filesystem) {
return false;
}
size_t msize = 0;
if (filesystem->stat(AO_FILENAME_PREFIX "bootstats.jsn", &msize) == 0) {
bool success = true;
auto json = FilesystemUtils::loadJson(filesystem, AO_FILENAME_PREFIX "bootstats.jsn");
if (json) {
int bootNrIn = (*json)["bootNr"] | -1;
if (bootNrIn >= 0 && bootNrIn <= std::numeric_limits<uint16_t>::max()) {
bstats.bootNr = (uint16_t) bootNrIn;
} else {
success = false;
}
int lastSuccessIn = (*json)["lastSuccess"] | -1;
if (lastSuccessIn >= 0 && lastSuccessIn <= std::numeric_limits<uint16_t>::max()) {
bstats.lastBootSuccess = (uint16_t) lastSuccessIn;
} else {
success = false;
}
} else {
success = false;
}
if (!success) {
AO_DBG_ERR("bootstats corrupted");
filesystem->remove(AO_FILENAME_PREFIX "bootstats.jsn");
bstats = BootStats();
}
return success;
} else {
return false;
}
}
bool BootService::storeBootStats(std::shared_ptr<FilesystemAdapter> filesystem, BootStats bstats) {
if (!filesystem) {
return false;
}
DynamicJsonDocument json {JSON_OBJECT_SIZE(2)};
json["bootNr"] = bstats.bootNr;
json["lastSuccess"] = bstats.lastBootSuccess;
return FilesystemUtils::storeJson(filesystem, AO_FILENAME_PREFIX "bootstats.jsn", json);
}