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
140 lines (117 loc) · 4.34 KB
/
evse.cpp
File metadata and controls
140 lines (117 loc) · 4.34 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
#include "evse.h"
#include <ArduinoOcpp.h>
#include <ArduinoOcpp/Core/OcppEngine.h>
#include <ArduinoOcpp/Core/OcppModel.h>
#include <ArduinoOcpp/Tasks/Authorization/AuthorizationService.h>
#include <ArduinoOcpp/Tasks/ChargePointStatus/ChargePointStatusService.h>
#include <ArduinoOcpp/Debug.h>
#include <ArduinoOcpp/MessagesV16/StatusNotification.h>
#include <cstring>
Evse::Evse(unsigned int connectorId) : connectorId{connectorId} {
}
ArduinoOcpp::ConnectorStatus *getConnector(unsigned int connectorId) {
if (!getOcppEngine()) {
AO_DBG_ERR("unitialized");
return nullptr;
}
return getOcppEngine()->getOcppModel().getConnectorStatus(connectorId);
}
void Evse::setup() {
auto connector = getConnector(connectorId);
if (!connector) {
AO_DBG_ERR("invalid state");
return;
}
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->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);
}
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;
if (simulate_isCharging) {
if (simulate_power >= 1.f) {
simulate_energy += (float) (ao_tick_ms() - simulate_energy_track_time) * SIMULATE_ENERGY_DELTA_MS;
}
simulate_power = SIMULATE_POWER_CONST;
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 = getOcppEngine()->getOcppModel().getConnectorStatus(connectorId);
if (!connector) {
AO_DBG_ERR("invalid state");
return;
}
if (connector->getSessionIdTag()) {
if (!uid.compare(connector->getSessionIdTag())) {
connector->endSession("Local");
} else {
AO_DBG_INFO("RFID card denied");
}
} else if (isBlockedByReservation(uid.c_str(), connectorId)) {
AO_DBG_INFO("connectorId %u blocked by reservation, cannot authorize idTag %s", connectorId, uid.c_str());
} else if (getOcppEngine()->getOcppModel().getAuthorizationService() &&
getOcppEngine()->getOcppModel().getAuthorizationService()->getLocalAuthorization(uid.c_str())) {
AO_DBG_INFO("RFID tag locally authorized: %s", uid.c_str());
connector->beginSession(uid.c_str());
} else {
authorize(uid.c_str(), [uid, connector] (JsonObject response) {
if (!strcmp(response["idTagInfo"]["status"] | "", "Accepted")) {
AO_DBG_INFO("RFID tag authorized: %s", uid.c_str());
connector->beginSession(uid.c_str());
} else {
AO_DBG_INFO("RFID card denied for reason %s", response["idTagInfo"]["status"] | "undefined");
}
});
}
}
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();
}