forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteStartTransaction.cpp
More file actions
155 lines (126 loc) · 5.1 KB
/
Copy pathRemoteStartTransaction.cpp
File metadata and controls
155 lines (126 loc) · 5.1 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
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2024
// MIT License
#include <MicroOcpp/Operations/RemoteStartTransaction.h>
#include <MicroOcpp/Core/Configuration.h>
#include <MicroOcpp/Model/Model.h>
#include <MicroOcpp/Model/ConnectorBase/Connector.h>
#include <MicroOcpp/Model/SmartCharging/SmartChargingService.h>
#include <MicroOcpp/Model/Transactions/Transaction.h>
#include <MicroOcpp/Debug.h>
using MicroOcpp::Ocpp16::RemoteStartTransaction;
using MicroOcpp::JsonDoc;
RemoteStartTransaction::RemoteStartTransaction(Model& model) : MemoryManaged("v16.Operation.", "RemoteStartTransaction"), model(model) {
}
const char* RemoteStartTransaction::getOperationType() {
return "RemoteStartTransaction";
}
void RemoteStartTransaction::processReq(JsonObject payload) {
int connectorId = payload["connectorId"] | -1;
// OCPP 1.6 specification: connectorId SHALL be > 0 (TC_027_CS)
if (connectorId == 0) {
MO_DBG_INFO("RemoteStartTransaction rejected: connectorId SHALL not be 0");
accepted = false;
return;
}
if (!payload.containsKey("idTag")) {
errorCode = "FormationViolation";
return;
}
const char *idTag = payload["idTag"] | "";
size_t len = strnlen(idTag, IDTAG_LEN_MAX + 1);
if (len == 0 || len > IDTAG_LEN_MAX) {
errorCode = "PropertyConstraintViolation";
errorDescription = "idTag empty or too long";
return;
}
std::unique_ptr<ChargingProfile> chargingProfile;
if (payload.containsKey("chargingProfile") && model.getSmartChargingService()) {
MO_DBG_INFO("Setting Charging profile via RemoteStartTransaction");
JsonObject chargingProfileJson = payload["chargingProfile"];
chargingProfile = loadChargingProfile(chargingProfileJson);
if (!chargingProfile) {
errorCode = "PropertyConstraintViolation";
errorDescription = "chargingProfile validation failed";
return;
}
if (chargingProfile->getChargingProfilePurpose() != ChargingProfilePurposeType::TxProfile) {
errorCode = "PropertyConstraintViolation";
errorDescription = "Can only set TxProfile here";
return;
}
if (chargingProfile->getChargingProfileId() < 0) {
errorCode = "PropertyConstraintViolation";
errorDescription = "RemoteStartTx profile requires non-negative chargingProfileId";
return;
}
}
Connector *selectConnector = nullptr;
if (connectorId >= 1) {
//connectorId specified for given connector, try to start Transaction here
if (auto connector = model.getConnector(connectorId)){
if (!connector->getTransaction() &&
connector->isOperative()) {
selectConnector = connector;
}
}
} else {
//connectorId not specified. Find free connector
for (unsigned int cid = 1; cid < model.getNumConnectors(); cid++) {
auto connector = model.getConnector(cid);
if (!connector->getTransaction() &&
connector->isOperative()) {
selectConnector = connector;
connectorId = cid;
break;
}
}
}
if (selectConnector) {
bool success = true;
int chargingProfileId = -1; //keep Id after moving charging profile to SCService
if (chargingProfile && model.getSmartChargingService()) {
chargingProfileId = chargingProfile->getChargingProfileId();
success = model.getSmartChargingService()->setChargingProfile(connectorId, std::move(chargingProfile));
}
if (success) {
std::shared_ptr<MicroOcpp::Transaction> tx;
auto authorizeRemoteTxRequests = declareConfiguration<bool>("AuthorizeRemoteTxRequests", false);
if (authorizeRemoteTxRequests && authorizeRemoteTxRequests->getBool()) {
tx = selectConnector->beginTransaction(idTag);
} else {
tx = selectConnector->beginTransaction_authorized(idTag);
}
selectConnector->updateTxNotification(TxNotification_RemoteStart);
if (tx) {
if (chargingProfileId >= 0) {
tx->setTxProfileId(chargingProfileId);
}
} else {
success = false;
}
}
accepted = success;
} else {
MO_DBG_INFO("No connector to start transaction");
accepted = false;
}
}
std::unique_ptr<JsonDoc> RemoteStartTransaction::createConf(){
auto doc = makeJsonDoc(getMemoryTag(), JSON_OBJECT_SIZE(1));
JsonObject payload = doc->to<JsonObject>();
if (accepted) {
payload["status"] = "Accepted";
} else {
payload["status"] = "Rejected";
}
return doc;
}
std::unique_ptr<JsonDoc> RemoteStartTransaction::createReq() {
auto doc = makeJsonDoc(getMemoryTag(), JSON_OBJECT_SIZE(1));
JsonObject payload = doc->to<JsonObject>();
payload["idTag"] = "A0000000";
return doc;
}
void RemoteStartTransaction::processConf(JsonObject payload){
}