forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetChargingProfile.cpp
More file actions
87 lines (70 loc) · 2.78 KB
/
Copy pathSetChargingProfile.cpp
File metadata and controls
87 lines (70 loc) · 2.78 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
// matth-x/ArduinoOcpp
// Copyright Matthias Akstaller 2019 - 2023
// MIT License
#include <ArduinoOcpp/Operations/SetChargingProfile.h>
#include <ArduinoOcpp/Model/Model.h>
#include <ArduinoOcpp/Model/SmartCharging/SmartChargingService.h>
#include <ArduinoOcpp/Model/Transactions/Transaction.h>
#include <ArduinoOcpp/Debug.h>
using ArduinoOcpp::Ocpp16::SetChargingProfile;
SetChargingProfile::SetChargingProfile(Model& model, SmartChargingService& scService) : model(model), scService(scService) {
}
SetChargingProfile::~SetChargingProfile() {
}
const char* SetChargingProfile::getOperationType(){
return "SetChargingProfile";
}
void SetChargingProfile::processReq(JsonObject payload) {
int connectorId = payload["connectorId"] | -1;
if (connectorId < 0 || !payload.containsKey("csChargingProfiles")) {
errorCode = "FormationViolation";
return;
}
if ((unsigned int) connectorId >= model.getNumConnectors()) {
errorCode = "PropertyConstraintViolation";
return;
}
JsonObject csChargingProfiles = payload["csChargingProfiles"];
auto chargingProfile = loadChargingProfile(csChargingProfiles);
if (!chargingProfile) {
errorCode = "PropertyConstraintViolation";
errorDescription = "csChargingProfiles validation failed";
return;
}
if (chargingProfile->getChargingProfilePurpose() == ChargingProfilePurposeType::TxProfile) {
// if TxProfile, check if a transaction is running
if (connectorId == 0) {
errorCode = "PropertyConstraintViolation";
errorDescription = "Cannot set TxProfile at connectorId 0";
return;
}
Connector *connector = model.getConnector(connectorId);
if (!connector) {
errorCode = "PropertyConstraintViolation";
return;
}
if (!connector->getTransaction() || !connector->getTransaction()->isRunning()) {
//no transaction running, reject profile
accepted = false;
return;
}
//seems good
} else if (chargingProfile->getChargingProfilePurpose() == ChargingProfilePurposeType::ChargePointMaxProfile) {
if (connectorId > 0) {
errorCode = "PropertyConstraintViolation";
errorDescription = "Cannot set ChargePointMaxProfile at connectorId > 0";
return;
}
}
accepted = scService.setChargingProfile(connectorId, std::move(chargingProfile));
}
std::unique_ptr<DynamicJsonDocument> SetChargingProfile::createConf(){
auto doc = std::unique_ptr<DynamicJsonDocument>(new DynamicJsonDocument(JSON_OBJECT_SIZE(1)));
JsonObject payload = doc->to<JsonObject>();
if (accepted) {
payload["status"] = "Accepted";
} else {
payload["status"] = "Rejected";
}
return doc;
}