forked from matth-x/MicroOcppSimulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevse.cpp
More file actions
271 lines (228 loc) · 8.04 KB
/
evse.cpp
File metadata and controls
271 lines (228 loc) · 8.04 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
// matth-x/MicroOcppSimulator
// Copyright Matthias Akstaller 2022 - 2024
// GPL-3.0 License
#include "evse.h"
#include <MicroOcpp.h>
#include <MicroOcpp/Core/Context.h>
#include <MicroOcpp/Model/Model.h>
#include <MicroOcpp/Model/Transactions/Transaction.h>
#include <MicroOcpp/Model/Transactions/TransactionService.h>
#include <MicroOcpp/Model/Variables/VariableService.h>
#include <MicroOcpp/Operations/StatusNotification.h>
#include <MicroOcpp/Version.h>
#include <MicroOcpp/Debug.h>
#include <cstring>
#include <cstdlib>
Evse::Evse(unsigned int connectorId) : connectorId{connectorId} {
}
MicroOcpp::Connector *getConnector(unsigned int connectorId) {
if (!getOcppContext()) {
MO_DBG_ERR("unitialized");
return nullptr;
}
return getOcppContext()->getModel().getConnector(connectorId);
}
void Evse::setup() {
#if MO_ENABLE_V201
if (auto context = getOcppContext()) {
if (context->getVersion().major == 2) {
//load some example variables for testing
if (auto varService = context->getModel().getVariableService()) {
varService->declareVariable<bool>("AuthCtrlr", "LocalAuthorizeOffline", false, MO_VARIABLE_VOLATILE);
varService->declareVariable<int>("OCPPCommCtrlr", "OfflineThreshold", false, MO_VARIABLE_VOLATILE);
varService->declareVariable<bool>("TxCtrlr", "StopTxOnInvalidId", false, MO_VARIABLE_VOLATILE);
}
}
}
#endif
auto connector = getConnector(connectorId);
if (!connector) {
MO_DBG_ERR("invalid state");
return;
}
char key [30] = {'\0'};
snprintf(key, 30, "evPlugged_cId_%u", connectorId);
trackEvPluggedKey = key;
trackEvPluggedBool = MicroOcpp::declareConfiguration(trackEvPluggedKey.c_str(), false, SIMULATOR_FN, false, false, false);
snprintf(key, 30, "evReady_cId_%u", connectorId);
trackEvReadyKey = key;
trackEvReadyBool = MicroOcpp::declareConfiguration(trackEvReadyKey.c_str(), false, SIMULATOR_FN, false, false, false);
snprintf(key, 30, "evseReady_cId_%u", connectorId);
trackEvseReadyKey = key;
trackEvseReadyBool = MicroOcpp::declareConfiguration(trackEvseReadyKey.c_str(), false, SIMULATOR_FN, false, false, false);
MicroOcpp::configuration_load(SIMULATOR_FN);
setConnectorPluggedInput([this] () -> bool {
return trackEvPluggedBool->getBool(); //return if J1772 is in State B or C
}, connectorId);
setEvReadyInput([this] () -> bool {
return trackEvReadyBool->getBool(); //return if J1772 is in State C
}, connectorId);
setEvseReadyInput([this] () -> bool {
return trackEvseReadyBool->getBool();
}, connectorId);
addErrorCodeInput([this] () -> const char* {
const char *errorCode = nullptr; //if error is present, point to error code; any number of error code samplers can be added in this project
return errorCode;
}, connectorId);
setEnergyMeterInput([this] () -> float {
return simulate_energy;
}, connectorId);
setPowerMeterInput([this] () -> float {
return simulate_power;
}, connectorId);
addMeterValueInput([this] () {
return (int32_t) getCurrent();
},
"Current.Import",
"A",
"Outlet",
nullptr,
connectorId);
addMeterValueInput([this] () {
return (int32_t) getVoltage();
},
"Voltage",
"V",
nullptr,
nullptr,
connectorId);
addMeterValueInput([this] () {
return (int32_t) (simulate_power > 1.f ? 44.f : 0.f);
},
"SoC",
nullptr,
nullptr,
nullptr,
connectorId);
setOnResetExecute([] (bool isHard) {
exit(0);
});
setSmartChargingPowerOutput([this] (float limit) {
if (limit >= 0.f) {
MO_DBG_DEBUG("set limit: %f", limit);
this->limit_power = limit;
} else {
// negative value means no limit defined
this->limit_power = SIMULATE_POWER_CONST;
}
}, connectorId);
}
void Evse::loop() {
if (auto connector = getConnector(connectorId)) {
auto curStatus = connector->getStatus();
if (status.compare(MicroOcpp::cstrFromOcppEveState(curStatus))) {
status = MicroOcpp::cstrFromOcppEveState(curStatus);
}
}
bool simulate_isCharging = ocppPermitsCharge(connectorId) && trackEvPluggedBool->getBool() && trackEvReadyBool->getBool() && trackEvseReadyBool->getBool();
simulate_isCharging &= limit_power >= 720.f; //minimum charging current is 6A (720W for 120V grids) according to J1772
if (simulate_isCharging) {
if (simulate_power >= 1.f) {
simulate_energy += (float) (mocpp_tick_ms() - simulate_energy_track_time) * simulate_power * (0.001f / 3600.f);
}
simulate_power = SIMULATE_POWER_CONST;
simulate_power = std::min(simulate_power, limit_power);
simulate_power += (((mocpp_tick_ms() / 5000) * 3483947) % 20000) * 0.001f - 10.f;
simulate_energy_track_time = mocpp_tick_ms();
} else {
simulate_power = 0.f;
}
}
void Evse::presentNfcTag(const char *uid_cstr) {
if (!uid_cstr) {
MO_DBG_ERR("invalid argument");
return;
}
std::string uid = uid_cstr;
auto connector = getConnector(connectorId);
if (!connector) {
MO_DBG_ERR("invalid state");
return;
}
#if MO_ENABLE_V201
if (auto context = getOcppContext()) {
if (context->getVersion().major == 2) {
if (auto txService = context->getModel().getTransactionService()) {
if (auto evse = txService->getEvse(connectorId)) {
if (evse->getTransaction() && evse->getTransaction()->isAuthorized) {
evse->endAuthorization(uid_cstr);
} else {
evse->beginAuthorization(uid_cstr);
}
return;
}
}
}
}
#endif
if (connector->getTransaction() && connector->getTransaction()->isActive()) {
if (!uid.compare(connector->getTransaction()->getIdTag())) {
connector->endTransaction(uid.c_str());
} else {
MO_DBG_INFO("RFID card denied");
}
} else {
connector->beginTransaction(uid.c_str());
}
}
void Evse::setEvPlugged(bool plugged) {
if (!trackEvPluggedBool) return;
trackEvPluggedBool->setBool(plugged);
MicroOcpp::configuration_save();
}
bool Evse::getEvPlugged() {
if (!trackEvPluggedBool) return false;
return trackEvPluggedBool->getBool();
}
void Evse::setEvReady(bool ready) {
if (!trackEvReadyBool) return;
trackEvReadyBool->setBool(ready);
MicroOcpp::configuration_save();
}
bool Evse::getEvReady() {
if (!trackEvReadyBool) return false;
return trackEvReadyBool->getBool();
}
void Evse::setEvseReady(bool ready) {
if (!trackEvseReadyBool) return;
trackEvseReadyBool->setBool(ready);
MicroOcpp::configuration_save();
}
bool Evse::getEvseReady() {
if (!trackEvseReadyBool) return false;
return trackEvseReadyBool->getBool();
}
const char *Evse::getSessionIdTag() {
auto connector = getConnector(connectorId);
if (!connector) {
MO_DBG_ERR("invalid state");
return nullptr;
}
return connector->getTransaction() ? connector->getTransaction()->getIdTag() : nullptr;
}
int Evse::getTransactionId() {
auto connector = getConnector(connectorId);
if (!connector) {
MO_DBG_ERR("invalid state");
return -1;
}
return connector->getTransaction() ? connector->getTransaction()->getTransactionId() : -1;
}
bool Evse::chargingPermitted() {
auto connector = getConnector(connectorId);
if (!connector) {
MO_DBG_ERR("invalid state");
return false;
}
return connector->ocppPermitsCharge();
}
int Evse::getPower() {
return (int) simulate_power;
}
float Evse::getVoltage() {
if (getPower() > 1.f) {
return 228.f + (((mocpp_tick_ms() / 5000) * 7484311) % 4000) * 0.001f;
} else {
return 0.f;
}
}