Skip to content

Commit f7d1f9e

Browse files
committed
MATTER-2748 Porting EEVSE backend APIs to chip-tool-web2
Porting the EEVSE backend APIs event_trigger, eevse_control, eevse_read to chip-tool-web2 test with below 3 JSON for 3 APIs, all passed. event_trigger: { "nodeAlias": "EEVSE", "nodeId": "1", "key": "000102030405060708090a0b0c0d0e0f", "type": "triggerBasic" } eevse_control { "nodeAlias": "EEVSE", "nodeId": "1", "endPointId": "1", "chargingEnabledUntil": "null", "minimumChargeCurrent": "6000", "maximumChargeCurrent": "32000", "type": "enablecharging" } event_read { "nodeAlias": "EEVSE", "nodeId": "1", "endPointId": "1", "type": "state" } Change-Id: Iaa682a7aa938b06626f0226340144b4f50a411ce Signed-off-by: Junmeng Li <junmeng.li_1@nxp.com> Reviewed-on: http://androidsource.nxp.com/project/26536 Reviewed-by: Elven Wang <elven.wang@nxp.com>
1 parent a4fc9d4 commit f7d1f9e

File tree

1 file changed

+253
-0
lines changed

1 file changed

+253
-0
lines changed

examples/chip-tool/webui-2_0/MatterApplicationBackend/src/chip_tool_web.cpp

+253
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,259 @@ int main()
11771177
}
11781178
});
11791179

1180+
CROW_ROUTE(crowApplication, "/api/event_trigger").methods("POST"_method)([](const crow::request& req) {
1181+
try
1182+
{
1183+
ptree root;
1184+
auto x_body_decoded = crow::json::load(req.body);
1185+
auto nodeAlias = std::string(x_body_decoded["nodeAlias"].s());
1186+
auto nodeId = std::string(x_body_decoded["nodeId"].s());
1187+
auto key = std::string(x_body_decoded["key"].s());
1188+
auto type = std::string(x_body_decoded["type"].s());
1189+
ChipLogError(NotSpecified, "Received event trigger POST request for device with Node ID: %s", std::string(nodeId).c_str());
1190+
std::string command;
1191+
if (type == "triggerBasic") {
1192+
command = "generaldiagnostics test-event-trigger hex:" + key + " 0x0099000000000000 " + nodeId + " 0";
1193+
} else if (type == "triggerBasicClear") {
1194+
command = "generaldiagnostics test-event-trigger hex:" + key + " 0x0099000000000001 " + nodeId + " 0";
1195+
} else if (type == "triggerPluggedin") {
1196+
command = "generaldiagnostics test-event-trigger hex:" + key + " 0x0099000000000002 " + nodeId + " 0";
1197+
} else if (type == "triggerPluggedinClear") {
1198+
command = "generaldiagnostics test-event-trigger hex:" + key + " 0x0099000000000003 " + nodeId + " 0";
1199+
} else if (type == "triggerChargeDemand") {
1200+
command = "generaldiagnostics test-event-trigger hex:" + key + " 0x0099000000000004 " + nodeId + " 0";
1201+
} else if (type == "triggerChargeDemandClear") {
1202+
command = "generaldiagnostics test-event-trigger hex:" + key + " 0x0099000000000005 " + nodeId + " 0";
1203+
}
1204+
wsClient.sendMessage(command);
1205+
ChipLogError(NotSpecified, "Send general diagnostics event trigger command to chip-tool ws server");
1206+
int sleepTime = 0;
1207+
while (reportQueue.empty() && sleepTime < 20)
1208+
{
1209+
this_thread::sleep_for(chrono::seconds(1));
1210+
sleepTime++;
1211+
}
1212+
if (sleepTime == 20) {
1213+
root.put("result", RESPONSE_FAILURE);
1214+
ChipLogError(NotSpecified, "Execute general diagnostics event trigger command overtime!");
1215+
} else {
1216+
Json::Value resultsReport = wsClient.dequeueReport();
1217+
Json::Value resultsValue = resultsReport[0];
1218+
if (resultsValue.isMember("error"))
1219+
{
1220+
root.put("result", RESPONSE_FAILURE);
1221+
ChipLogError(NotSpecified, "Execute general diagnostics event trigger command failed!");
1222+
} else {
1223+
root.put("result", RESPONSE_SUCCESS);
1224+
ChipLogError(NotSpecified, "Execute general diagnostics event trigger command successfully");
1225+
}
1226+
}
1227+
stringstream ss;
1228+
write_json(ss, root);
1229+
string strContent = ss.str();
1230+
crow::response response(strContent);
1231+
response.add_header("Access-Control-Allow-Origin", "*");
1232+
return response;
1233+
} catch (const exception & e)
1234+
{
1235+
crow::response response(400, e.what());
1236+
ChipLogError(NotSpecified, "Error on onoff_report: %s", e.what());
1237+
response.add_header("Access-Control-Allow-Origin", "*");
1238+
return response;
1239+
}
1240+
});
1241+
1242+
CROW_ROUTE(crowApplication, "/api/eevse_control").methods("POST"_method)([](const crow::request& req) {
1243+
try
1244+
{
1245+
ptree root;
1246+
auto x_body_decoded = crow::json::load(req.body);
1247+
auto nodeAlias = std::string(x_body_decoded["nodeAlias"].s());
1248+
auto nodeId = std::string(x_body_decoded["nodeId"].s());
1249+
auto endPointId = std::string(x_body_decoded["endPointId"].s());
1250+
auto type = std::string(x_body_decoded["type"].s());
1251+
ChipLogError(NotSpecified, "Received EEVSE control POST request for device with Node ID: %s, End Point ID: %s",
1252+
std::string(nodeId).c_str(), std::string(endPointId).c_str());
1253+
std::string command;
1254+
if (type == "enablecharging") {
1255+
auto chargingEnabledUntil = std::string(x_body_decoded["chargingEnabledUntil"].s());
1256+
auto minimumChargeCurrent = std::string(x_body_decoded["minimumChargeCurrent"].s());
1257+
auto maximumChargeCurrent = std::string(x_body_decoded["maximumChargeCurrent"].s());
1258+
command = "energyevse enable-charging " + chargingEnabledUntil + " " + minimumChargeCurrent + " " + maximumChargeCurrent + " "
1259+
+ nodeId + " " + endPointId + " --timedInteractionTimeoutMs 3000";
1260+
} else if (type == "write") {
1261+
auto userMaximumChargeCurrent = std::string(x_body_decoded["userMaximumChargeCurrent"].s());
1262+
command = "energyevse write user-maximum-charge-current " + userMaximumChargeCurrent + " " + nodeId + " " + endPointId;
1263+
} else if (type == "disable") {
1264+
command = "energyevse disable " + nodeId + " " + endPointId + " --timedInteractionTimeoutMs 3000";
1265+
}
1266+
wsClient.sendMessage(command);
1267+
ChipLogError(NotSpecified, "Send eevse control command to chip-tool ws server");
1268+
int sleepTime = 0;
1269+
while (reportQueue.empty() && sleepTime < 20)
1270+
{
1271+
this_thread::sleep_for(chrono::seconds(1));
1272+
sleepTime++;
1273+
}
1274+
if (sleepTime == 20) {
1275+
root.put("result", RESPONSE_FAILURE);
1276+
ChipLogError(NotSpecified, "Execute eevse control command overtime!");
1277+
} else {
1278+
Json::Value resultsReport = wsClient.dequeueReport();
1279+
Json::Value resultsValue = resultsReport[0];
1280+
if (resultsValue.isMember("error"))
1281+
{
1282+
root.put("result", RESPONSE_FAILURE);
1283+
ChipLogError(NotSpecified, "Execute eevse control command failed!");
1284+
} else {
1285+
root.put("result", RESPONSE_SUCCESS);
1286+
ChipLogError(NotSpecified, "Execute eevse control command successfully");
1287+
}
1288+
}
1289+
stringstream ss;
1290+
write_json(ss, root);
1291+
string strContent = ss.str();
1292+
crow::response response(strContent);
1293+
response.add_header("Access-Control-Allow-Origin", "*");
1294+
return response;
1295+
} catch (const exception & e)
1296+
{
1297+
crow::response response(400, e.what());
1298+
ChipLogError(NotSpecified, "Error on eevse_control: %s", e.what());
1299+
response.add_header("Access-Control-Allow-Origin", "*");
1300+
return response;
1301+
}
1302+
});
1303+
1304+
CROW_ROUTE(crowApplication, "/api/eevse_read").methods("POST"_method)([](const crow::request& req)
1305+
{
1306+
try
1307+
{
1308+
ptree root;
1309+
auto x_body_decoded = crow::json::load(req.body);
1310+
auto nodeAlias = std::string(x_body_decoded["nodeAlias"].s());
1311+
auto nodeId = std::string(x_body_decoded["nodeId"].s());
1312+
auto endPointId = std::string(x_body_decoded["endPointId"].s());
1313+
auto type = std::string(x_body_decoded["type"].s());
1314+
ChipLogError(NotSpecified, "Received EEVSE read POST request for device with Node ID: %s, End Point ID: %s",
1315+
std::string(nodeId).c_str(), std::string(endPointId).c_str());
1316+
std::string command;
1317+
if (type == "state") {
1318+
command = "energyevse read state " + nodeId + " " + endPointId;
1319+
} else if (type == "supplystate") {
1320+
command = "energyevse read supply-state " + nodeId + " " + endPointId;
1321+
} else if (type == "faultstate") {
1322+
command = "energyevse read fault-state " + nodeId + " " + endPointId;
1323+
} else if (type == "chargingenableduntil") {
1324+
command = "energyevse read charging-enabled-until " + nodeId + " " + endPointId;
1325+
} else if (type == "minimumchargecurrent") {
1326+
command = "energyevse read minimum-charge-current " + nodeId + " " + endPointId;
1327+
} else if (type == "maximumchargecurrent") {
1328+
command = "energyevse read maximum-charge-current " + nodeId + " " + endPointId;
1329+
} else if (type == "sessionid") {
1330+
command = "energyevse read session-id " + nodeId + " " + endPointId;
1331+
} else if (type == "sessionduration") {
1332+
command = "energyevse read session-duration " + nodeId + " " + endPointId;
1333+
}
1334+
wsClient.sendMessage(command);
1335+
ChipLogError(NotSpecified, "Send eevse read command to chip-tool ws server");
1336+
int sleepTime = 0;
1337+
while (reportQueue.empty() && sleepTime < 20)
1338+
{
1339+
this_thread::sleep_for(chrono::seconds(1));
1340+
sleepTime++;
1341+
}
1342+
if (sleepTime == 20) {
1343+
root.put("result", RESPONSE_FAILURE);
1344+
ChipLogError(NotSpecified, "Execute eevse read command overtime!");
1345+
} else {
1346+
Json::Value resultsReport = wsClient.dequeueReport();
1347+
Json::Value resultsValue = resultsReport[0];
1348+
if (!resultsValue.isMember("error") && resultsValue.isMember("value") )
1349+
{
1350+
string report_text;
1351+
stringstream report_ss;
1352+
string value;
1353+
if (type == "state") {
1354+
switch (resultsValue["value"].asInt()) {
1355+
case 0:
1356+
value = "NotPluggedIn";
1357+
break;
1358+
case 1:
1359+
value = "PluggedInNoDemand";
1360+
break;
1361+
case 2:
1362+
value = "PluggedInDemand";
1363+
break;
1364+
case 3:
1365+
value = "PluggedInCharging";
1366+
break;
1367+
case 6:
1368+
value = "Fault";
1369+
break;
1370+
default:
1371+
value = "Other";
1372+
}
1373+
} else if (type == "supplystate") {
1374+
switch (resultsValue["value"].asInt()) {
1375+
case 0:
1376+
value = "Disabled";
1377+
break;
1378+
case 1:
1379+
value = "ChargingEnabled";
1380+
break;
1381+
case 3:
1382+
value = "DisabledError";
1383+
break;
1384+
case 4:
1385+
value = "DisabledDiagnostics";
1386+
break;
1387+
default:
1388+
value = "Other";
1389+
}
1390+
} else if (type == "faultstate") {
1391+
switch (resultsValue["value"].asInt()) {
1392+
case 0:
1393+
value = "NoError";
1394+
break;
1395+
default:
1396+
value = "Other";
1397+
}
1398+
} else if (type == "chargingenableduntil") {
1399+
if (resultsValue["value"].asString().empty()) {
1400+
value = "AlwaysEnabled";
1401+
} else {
1402+
value = resultsValue["value"].asString();
1403+
}
1404+
} else {
1405+
value = resultsValue["value"].asString();
1406+
}
1407+
report_ss << "Report from " << nodeAlias << " " << nodeId << ": " << resultsValue["endpointId"] << ". "
1408+
<< "Cluster: " << resultsValue["clusterId"] << "\r\n\r\n" << type << ": " << value;
1409+
report_text = report_ss.str();
1410+
root.put("report", report_text);
1411+
ChipLogError(NotSpecified, "Generated eevse report successfully: %s", report_text.c_str());
1412+
root.put("result", RESPONSE_SUCCESS);
1413+
} else {
1414+
root.put("result", RESPONSE_FAILURE);
1415+
ChipLogError(NotSpecified, "Execute eevse read command failed!");
1416+
}
1417+
}
1418+
stringstream ss;
1419+
write_json(ss, root);
1420+
string strContent = ss.str();
1421+
crow::response response(strContent);
1422+
response.add_header("Access-Control-Allow-Origin", "*");
1423+
return response;
1424+
} catch (const exception & e)
1425+
{
1426+
crow::response response(400, e.what());
1427+
ChipLogError(NotSpecified, "Error on onoff_report: %s", e.what());
1428+
response.add_header("Access-Control-Allow-Origin", "*");
1429+
return response;
1430+
}
1431+
});
1432+
11801433
CROW_ROUTE(crowApplication, "/api/getAvailableNetwork").methods("GET"_method)([&](const crow::request& req) {
11811434
std::thread work_thread([req] {
11821435
//this_thread::sleep_for(chrono::seconds(5));

0 commit comments

Comments
 (0)