forked from matth-x/MicroOcppSimulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.cpp
More file actions
149 lines (126 loc) · 4.15 KB
/
api.cpp
File metadata and controls
149 lines (126 loc) · 4.15 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
#include "api.h"
#include <MicroOcpp/Debug.h>
#include "evse.h"
//simple matching function; takes * as a wildcard
bool str_match(const char *query, const char *pattern) {
size_t qi = 0, pi = 0;
while (query[qi] && pattern[pi]) {
if (query[qi] == pattern[pi]) {
qi++;
pi++;
} else if (pattern[pi] == '*') {
while (pattern[pi] == '*') pi++;
while (query[qi] != pattern[pi]) qi++;
} else {
break;
}
}
return !query[qi] && !pattern[pi];
}
int mocpp_api_call(const char *endpoint, MicroOcpp::Method method, const char *body, char *resp_body, size_t resp_body_size) {
MO_DBG_DEBUG("process %s, %s: %s",
endpoint,
method == MicroOcpp::Method::GET ? "GET" :
method == MicroOcpp::Method::POST ? "POST" : "error",
body);
int status = 500;
StaticJsonDocument<512> response;
if (resp_body_size >= sizeof("{}")) {
sprintf(resp_body, "%s", "{}");
}
StaticJsonDocument<512> request;
if (*body) {
auto err = deserializeJson(request, body);
if (err) {
MO_DBG_WARN("malformatted body: %s", err.c_str());
return 400;
}
}
unsigned int connectorId = 0;
if (strlen(endpoint) >= 11) {
if (endpoint[11] == '1') {
connectorId = 1;
} else if (endpoint[11] == '2') {
connectorId = 2;
}
}
MO_DBG_VERBOSE("connectorId = %u", connectorId);
Evse *evse = nullptr;
if (connectorId >= 1 && connectorId < MO_NUMCONNECTORS) {
evse = &connectors[connectorId-1];
}
//start different api endpoints
if(str_match(endpoint, "/connectors")) {
MO_DBG_VERBOSE("query connectors");
response.add("1");
response.add("2");
status = 200;
} else if(str_match(endpoint, "/connector/*/evse")){
MO_DBG_VERBOSE("query evse");
if (!evse) {
return 404;
}
if (method == MicroOcpp::Method::POST) {
if (request.containsKey("evPlugged")) {
evse->setEvPlugged(request["evPlugged"]);
}
if (request.containsKey("evReady")) {
evse->setEvReady(request["evReady"]);
}
if (request.containsKey("evseReady")) {
evse->setEvseReady(request["evseReady"]);
}
}
response["evPlugged"] = evse->getEvPlugged();
response["evReady"] = evse->getEvReady();
response["evseReady"] = evse->getEvseReady();
response["chargePointStatus"] = evse->getOcppStatus();
status = 200;
} else if(str_match(endpoint, "/connector/*/meter")){
MO_DBG_VERBOSE("query meter");
if (!evse) {
return 404;
}
response["energy"] = evse->getEnergy();
response["power"] = evse->getPower();
response["current"] = evse->getCurrent();
response["voltage"] = evse->getVoltage();
status = 200;
} else if(str_match(endpoint, "/connector/*/transaction")){
MO_DBG_VERBOSE("query transaction");
if (!evse) {
return 404;
}
if (method == MicroOcpp::Method::POST) {
if (request.containsKey("idTag")) {
evse->presentNfcTag(request["idTag"] | "");
}
}
response["idTag"] = evse->getSessionIdTag();
response["transactionId"] = evse->getTransactionId();
response["authorizationStatus"] = "";
status = 200;
} else if(str_match(endpoint, "/connector/*/smartcharging")){
MO_DBG_VERBOSE("query smartcharging");
if (!evse) {
return 404;
}
response["maxPower"] = evse->getSmartChargingMaxPower();
response["maxCurrent"] = evse->getSmartChargingMaxCurrent();
status = 200;
} else {
return 404;
}
if (response.overflowed()) {
return 500;
}
std::string out;
serializeJson(response, out);
if (out.length() >= resp_body_size) {
return 500;
}
if (!out.empty()) {
sprintf(resp_body, "%s", out.c_str());
}
return status;
}