forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnlockConnector.cpp
More file actions
164 lines (123 loc) · 4.28 KB
/
UnlockConnector.cpp
File metadata and controls
164 lines (123 loc) · 4.28 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
156
157
158
159
160
161
162
163
164
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2024
// MIT License
#include <MicroOcpp/Operations/UnlockConnector.h>
#include <MicroOcpp/Model/Model.h>
#include <MicroOcpp/Debug.h>
using MicroOcpp::Ocpp16::UnlockConnector;
using MicroOcpp::JsonDoc;
UnlockConnector::UnlockConnector(Model& model) : MemoryManaged("v16.Operation.", "UnlockConnector"), model(model) {
}
const char* UnlockConnector::getOperationType(){
return "UnlockConnector";
}
void UnlockConnector::processReq(JsonObject payload) {
#if MO_ENABLE_CONNECTOR_LOCK
{
auto connectorId = payload["connectorId"] | -1;
auto connector = model.getConnector(connectorId);
if (!connector) {
// NotSupported
return;
}
unlockConnector = connector->getOnUnlockConnector();
if (!unlockConnector) {
// NotSupported
return;
}
connector->endTransaction(nullptr, "UnlockCommand");
connector->updateTxNotification(TxNotification_RemoteStop);
cbUnlockResult = unlockConnector();
timerStart = mocpp_tick_ms();
}
#endif //MO_ENABLE_CONNECTOR_LOCK
}
std::unique_ptr<JsonDoc> UnlockConnector::createConf() {
const char *status = "NotSupported";
#if MO_ENABLE_CONNECTOR_LOCK
if (unlockConnector) {
if (mocpp_tick_ms() - timerStart < MO_UNLOCK_TIMEOUT) {
//do poll and if more time is needed, delay creation of conf msg
if (cbUnlockResult == UnlockConnectorResult_Pending) {
cbUnlockResult = unlockConnector();
if (cbUnlockResult == UnlockConnectorResult_Pending) {
return nullptr; //no result yet - delay confirmation response
}
}
}
if (cbUnlockResult == UnlockConnectorResult_Unlocked) {
status = "Unlocked";
} else {
status = "UnlockFailed";
}
}
#endif //MO_ENABLE_CONNECTOR_LOCK
auto doc = makeJsonDoc(getMemoryTag(), JSON_OBJECT_SIZE(1));
JsonObject payload = doc->to<JsonObject>();
payload["status"] = status;
return doc;
}
#if MO_ENABLE_V201
#if MO_ENABLE_CONNECTOR_LOCK
#include <MicroOcpp/Model/RemoteControl/RemoteControlService.h>
namespace MicroOcpp {
namespace Ocpp201 {
UnlockConnector::UnlockConnector(RemoteControlService& rcService) : MemoryManaged("v201.Operation.UnlockConnector"), rcService(rcService) {
}
const char* UnlockConnector::getOperationType(){
return "UnlockConnector";
}
void UnlockConnector::processReq(JsonObject payload) {
int evseId = payload["evseId"] | -1;
int connectorId = payload["connectorId"] | -1;
if (evseId < 1 || evseId >= MO_NUM_EVSEID || connectorId < 1) {
errorCode = "PropertyConstraintViolation";
return;
}
if (connectorId != 1) {
status = UnlockStatus_UnknownConnector;
return;
}
rcEvse = rcService.getEvse(evseId);
if (!rcEvse) {
status = UnlockStatus_UnlockFailed;
return;
}
status = rcEvse->unlockConnector();
timerStart = mocpp_tick_ms();
}
std::unique_ptr<JsonDoc> UnlockConnector::createConf() {
if (rcEvse && status == UnlockStatus_PENDING && mocpp_tick_ms() - timerStart < MO_UNLOCK_TIMEOUT) {
status = rcEvse->unlockConnector();
if (status == UnlockStatus_PENDING) {
return nullptr; //no result yet - delay confirmation response
}
}
const char *statusStr = "";
switch (status) {
case UnlockStatus_Unlocked:
statusStr = "Unlocked";
break;
case UnlockStatus_UnlockFailed:
statusStr = "UnlockFailed";
break;
case UnlockStatus_OngoingAuthorizedTransaction:
statusStr = "OngoingAuthorizedTransaction";
break;
case UnlockStatus_UnknownConnector:
statusStr = "UnknownConnector";
break;
case UnlockStatus_PENDING:
MO_DBG_ERR("UnlockConnector timeout");
statusStr = "UnlockFailed";
break;
}
auto doc = makeJsonDoc(getMemoryTag(), JSON_OBJECT_SIZE(1));
JsonObject payload = doc->to<JsonObject>();
payload["status"] = statusStr;
return doc;
}
} // namespace Ocpp201
} // namespace MicroOcpp
#endif //MO_ENABLE_CONNECTOR_LOCK
#endif //MO_ENABLE_V201