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
223 lines (185 loc) · 5.92 KB
/
evse.cpp
File metadata and controls
223 lines (185 loc) · 5.92 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
#include "evse.h"
#include <ArduinoOcpp.h>
#include <ArduinoOcpp/Context.h>
#include <ArduinoOcpp/Model/Model.h>
#include <ArduinoOcpp/Model/Transactions/Transaction.h>
#include <ArduinoOcpp/Operations/StatusNotification.h>
#include <ArduinoOcpp/Debug.h>
#include <cstring>
#include <cstdlib>
#define SIMULATOR_FN AO_FILENAME_PREFIX "simulator.jsn"
Evse::Evse(unsigned int connectorId) : connectorId{connectorId} {
}
ArduinoOcpp::Connector *getConnector(unsigned int connectorId) {
if (!getOcppContext()) {
AO_DBG_ERR("unitialized");
return nullptr;
}
return getOcppContext()->getModel().getConnector(connectorId);
}
void Evse::setup() {
auto connector = getConnector(connectorId);
if (!connector) {
AO_DBG_ERR("invalid state");
return;
}
char key [30] = {'\0'};
snprintf(key, 30, "evPlugged_cId_%u", connectorId);
trackEvPlugged = ArduinoOcpp::declareConfiguration(key, false, SIMULATOR_FN, false, false);
snprintf(key, 30, "evReady_cId_%u", connectorId);
trackEvReady = ArduinoOcpp::declareConfiguration(key, false, SIMULATOR_FN, false, false);
snprintf(key, 30, "evseReady_cId_%u", connectorId);
trackEvseReady = ArduinoOcpp::declareConfiguration(key, false, SIMULATOR_FN, false, false);
connector->setConnectorPluggedSampler([this] () -> bool {
return *trackEvPlugged; //return if J1772 is in State B or C
});
connector->setEvRequestsEnergySampler([this] () -> bool {
return *trackEvReady; //return if J1772 is in State C
});
connector->setConnectorEnergizedSampler([this] () -> bool {
return *trackEvseReady;
});
connector->addConnectorErrorCodeSampler([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;
});
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) {
AO_DBG_DEBUG("set limit: %f", limit);
this->limit_power = limit;
}, connectorId);
}
void Evse::loop() {
if (auto connector = getConnector(connectorId)) {
auto curStatus = connector->inferenceStatus();
if (status.compare(ArduinoOcpp::Ocpp16::cstrFromOcppEveState(curStatus))) {
status = ArduinoOcpp::Ocpp16::cstrFromOcppEveState(curStatus);
}
}
bool simulate_isCharging = ocppPermitsCharge(connectorId) && *trackEvPlugged && *trackEvReady && *trackEvseReady;
if (simulate_isCharging) {
if (simulate_power >= 1.f) {
simulate_energy += (float) (ao_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 += (((ao_tick_ms() / 5000) * 3483947) % 20000) * 0.001f - 10.f;
simulate_energy_track_time = ao_tick_ms();
} else {
simulate_power = 0.f;
}
}
void Evse::presentNfcTag(const char *uid_cstr) {
if (!uid_cstr) {
AO_DBG_ERR("invalid argument");
return;
}
std::string uid = uid_cstr;
auto connector = getConnector(connectorId);
if (!connector) {
AO_DBG_ERR("invalid state");
return;
}
if (connector->getTransaction() && connector->getTransaction()->isActive()) {
if (!uid.compare(connector->getTransaction()->getIdTag())) {
connector->endTransaction("Local");
} else {
AO_DBG_INFO("RFID card denied");
}
} else {
connector->beginTransaction(uid.c_str());
}
}
void Evse::setEvPlugged(bool plugged) {
if (!trackEvPlugged) return;
*trackEvPlugged = plugged;
ArduinoOcpp::configuration_save();
}
bool Evse::getEvPlugged() {
if (!trackEvPlugged) return false;
return *trackEvPlugged;
}
void Evse::setEvReady(bool ready) {
if (!trackEvReady) return;
*trackEvReady = ready;
ArduinoOcpp::configuration_save();
}
bool Evse::getEvReady() {
if (!trackEvReady) return false;
return *trackEvReady;
}
void Evse::setEvseReady(bool ready) {
if (!trackEvseReady) return;
*trackEvseReady = ready;
ArduinoOcpp::configuration_save();
}
bool Evse::getEvseReady() {
if (!trackEvseReady) return false;
return *trackEvseReady;
}
const char *Evse::getSessionIdTag() {
auto connector = getConnector(connectorId);
if (!connector) {
AO_DBG_ERR("invalid state");
return nullptr;
}
return connector->getSessionIdTag();
}
int Evse::getTransactionId() {
auto connector = getConnector(connectorId);
if (!connector) {
AO_DBG_ERR("invalid state");
return -1;
}
return connector->getTransactionId();
}
bool Evse::chargingPermitted() {
auto connector = getConnector(connectorId);
if (!connector) {
AO_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 + (((ao_tick_ms() / 5000) * 7484311) % 4000) * 0.001f;
} else {
return 0.f;
}
}