forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleOcppOperationFactory.cpp
More file actions
253 lines (216 loc) · 9.68 KB
/
Copy pathSimpleOcppOperationFactory.cpp
File metadata and controls
253 lines (216 loc) · 9.68 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// matth-x/ESP8266-OCPP
// Copyright Matthias Akstaller 2019 - 2021
// MIT License
#include "Variants.h"
#include <ArduinoOcpp/SimpleOcppOperationFactory.h>
#include <ArduinoOcpp/MessagesV16/Authorize.h>
#include <ArduinoOcpp/MessagesV16/BootNotification.h>
#include <ArduinoOcpp/MessagesV16/Heartbeat.h>
#include <ArduinoOcpp/MessagesV16/MeterValues.h>
#include <ArduinoOcpp/MessagesV16/SetChargingProfile.h>
#include <ArduinoOcpp/MessagesV16/StatusNotification.h>
#include <ArduinoOcpp/MessagesV16/StartTransaction.h>
#include <ArduinoOcpp/MessagesV16/StopTransaction.h>
#include <ArduinoOcpp/MessagesV16/TriggerMessage.h>
#include <ArduinoOcpp/MessagesV16/RemoteStartTransaction.h>
#include <ArduinoOcpp/Core/OcppError.h>
#include <ArduinoOcpp/MessagesV16/RemoteStopTransaction.h>
#include <ArduinoOcpp/MessagesV16/ChangeConfiguration.h>
#include <ArduinoOcpp/MessagesV16/GetConfiguration.h>
#include <ArduinoOcpp/MessagesV16/Reset.h>
#if 0
#include "UpdateFirmware.h"
#include "FirmwareStatusNotification.h"
#endif
#include <ArduinoOcpp/Core/OcppEngine.h>
#include <string.h>
namespace ArduinoOcpp {
OnReceiveReqListener onAuthorizeRequest;
void setOnAuthorizeRequestListener(OnReceiveReqListener listener){
onAuthorizeRequest = listener;
}
OnReceiveReqListener onBootNotificationRequest;
void setOnBootNotificationRequestListener(OnReceiveReqListener listener){
onBootNotificationRequest = listener;
}
OnReceiveReqListener onTargetValuesRequest;
void setOnTargetValuesRequestListener(OnReceiveReqListener listener) {
onTargetValuesRequest = listener;
}
OnReceiveReqListener onSetChargingProfileRequest;
void setOnSetChargingProfileRequestListener(OnReceiveReqListener listener){
onSetChargingProfileRequest = listener;
}
OnReceiveReqListener onStartTransactionRequest;
void setOnStartTransactionRequestListener(OnReceiveReqListener listener){
onStartTransactionRequest = listener;
}
OnReceiveReqListener onTriggerMessageRequest;
void setOnTriggerMessageRequestListener(OnReceiveReqListener listener){
onTriggerMessageRequest = listener;
}
OnReceiveReqListener onRemoteStartTransactionReceiveRequest;
void setOnRemoteStartTransactionReceiveRequestListener(OnReceiveReqListener listener) {
onRemoteStartTransactionReceiveRequest = listener;
}
OnSendConfListener onRemoteStartTransactionSendConf;
void setOnRemoteStartTransactionSendConfListener(OnSendConfListener listener){
onRemoteStartTransactionSendConf = listener;
}
OnSendConfListener onRemoteStopTransactionSendConf;
void setOnRemoteStopTransactionSendConfListener(OnSendConfListener listener){
onRemoteStopTransactionSendConf = listener;
}
OnSendConfListener onChangeConfigurationReceiveReq;
void setOnChangeConfigurationReceiveRequestListener(OnReceiveReqListener listener){
onChangeConfigurationReceiveReq = listener;
}
OnSendConfListener onChangeConfigurationSendConf;
void setOnChangeConfigurationSendConfListener(OnSendConfListener listener){
onChangeConfigurationSendConf = listener;
}
OnSendConfListener onGetConfigurationReceiveReq;
void setOnGetConfigurationReceiveReqListener(OnSendConfListener listener){
onGetConfigurationReceiveReq = listener;
}
OnSendConfListener onGetConfigurationSendConf;
void setOnGetConfigurationSendConfListener(OnSendConfListener listener){
onGetConfigurationSendConf = listener;
}
OnSendConfListener onResetSendConf;
void setOnResetSendConfListener(OnSendConfListener listener){
onResetSendConf = listener;
}
OnReceiveReqListener onUpdateFirmwareReceiveReq;
void setOnUpdateFirmwareReceiveRequestListener(OnReceiveReqListener listener) {
onUpdateFirmwareReceiveReq = listener;
}
struct CustomOcppMessageCreatorEntry {
const char *messageType;
OcppMessageCreator creator;
OnReceiveReqListener onReceiveReq;
};
std::vector<CustomOcppMessageCreatorEntry> customMessagesRegistry;
void registerCustomOcppMessage(const char *messageType, OcppMessageCreator ocppMessageCreator, OnReceiveReqListener onReceiveReq) {
customMessagesRegistry.erase(std::remove_if(customMessagesRegistry.begin(),
customMessagesRegistry.end(),
[messageType](CustomOcppMessageCreatorEntry &el) {
return !strcmp(messageType, el.messageType);
}),
customMessagesRegistry.end());
CustomOcppMessageCreatorEntry entry;
entry.messageType = messageType;
entry.creator = ocppMessageCreator;
entry.onReceiveReq = onReceiveReq;
customMessagesRegistry.push_back(entry);
}
CustomOcppMessageCreatorEntry *makeCustomOcppMessage(const char *messageType) {
for (auto it = customMessagesRegistry.begin(); it != customMessagesRegistry.end(); ++it) {
if (!strcmp(it->messageType, messageType)) {
return &(*it);
}
}
return NULL;
}
OcppOperation* makeFromTriggerMessage(JsonObject payload) {
//int connectorID = payload["connectorId"]; <-- not used in this implementation
const char *messageType = payload["requestedMessage"];
if (DEBUG_OUT) {
Serial.print(F("[SimpleOcppOperationFactory] makeFromTriggerMessage for message type "));
Serial.print(messageType);
Serial.print(F("\n"));
}
return makeOcppOperation(messageType);
}
OcppOperation *makeFromJson(JsonDocument *json) {
const char* messageType = (*json)[2];
return makeOcppOperation(messageType);
}
OcppOperation *makeOcppOperation(const char *messageType) {
OcppOperation *operation = makeOcppOperation();
OcppMessage *msg = NULL;
if (CustomOcppMessageCreatorEntry *entry = makeCustomOcppMessage(messageType)) {
msg = entry->creator();
operation->setOnReceiveReqListener(entry->onReceiveReq);
} else if (!strcmp(messageType, "Authorize")) {
msg = new Ocpp16::Authorize();
operation->setOnReceiveReqListener(onAuthorizeRequest);
} else if (!strcmp(messageType, "BootNotification")) {
msg = new Ocpp16::BootNotification();
operation->setOnReceiveReqListener(onBootNotificationRequest);
} else if (!strcmp(messageType, "Heartbeat")) {
msg = new Ocpp16::Heartbeat();
} else if (!strcmp(messageType, "MeterValues")) {
msg = new Ocpp16::MeterValues();
} else if (!strcmp(messageType, "SetChargingProfile")) {
msg = new Ocpp16::SetChargingProfile(getSmartChargingService());
operation->setOnReceiveReqListener(onSetChargingProfileRequest);
} else if (!strcmp(messageType, "StatusNotification")) {
msg = new Ocpp16::StatusNotification();
} else if (!strcmp(messageType, "StartTransaction")) {
msg = new Ocpp16::StartTransaction(1); //connectorId 1
operation->setOnReceiveReqListener(onStartTransactionRequest);
} else if (!strcmp(messageType, "StopTransaction")) {
msg = new Ocpp16::StopTransaction();
} else if (!strcmp(messageType, "TriggerMessage")) {
msg = new Ocpp16::TriggerMessage();
operation->setOnReceiveReqListener(onTriggerMessageRequest);
} else if (!strcmp(messageType, "RemoteStartTransaction")) {
msg = new Ocpp16::RemoteStartTransaction();
operation->setOnReceiveReqListener(onRemoteStartTransactionReceiveRequest);
if (onRemoteStartTransactionSendConf == NULL)
Serial.print(F("[SimpleOcppOperationFactory] Warning: RemoteStartTransaction is without effect when the sendConf listener is not set. Set a listener which initiates the StartTransaction operation.\n"));
operation->setOnSendConfListener(onRemoteStartTransactionSendConf);
} else if (!strcmp(messageType, "RemoteStopTransaction")) {
msg = new Ocpp16::RemoteStopTransaction();
if (onRemoteStopTransactionSendConf == NULL)
Serial.print(F("[SimpleOcppOperationFactory] Warning: RemoteStopTransaction is without effect when the sendConf listener is not set. Set a listener which initiates the StopTransaction operation.\n"));
operation->setOnSendConfListener(onRemoteStopTransactionSendConf);
} else if (!strcmp(messageType, "ChangeConfiguration")) {
msg = new Ocpp16::ChangeConfiguration();
operation->setOnReceiveReqListener(onChangeConfigurationReceiveReq);
operation->setOnSendConfListener(onChangeConfigurationSendConf);
} else if (!strcmp(messageType, "GetConfiguration")) {
msg = new Ocpp16::GetConfiguration();
operation->setOnReceiveReqListener(onGetConfigurationReceiveReq);
operation->setOnSendConfListener(onGetConfigurationSendConf);
} else if (!strcmp(messageType, "Reset")) {
msg = new Ocpp16::Reset();
if (onResetSendConf == NULL)
Serial.print(F("[SimpleOcppOperationFactory] Warning: Reset is without effect when the sendConf listener is not set. Set a listener which resets your device.\n"));
operation->setOnSendConfListener(onResetSendConf);
#if 0
} else if (!strcmp(messageType, "UpdateFirmware")) {
msg = new UpdateFirmware();
if (onUpdateFirmwareReceiveReq == NULL)
Serial.print(F("[SimpleOcppOperationFactory] Warning: UpdateFirmware is without effect when the receiveReq listener is not set. Please implement a FW update routine for your device.\n"));
operation->setOnReceiveReqListener(onUpdateFirmwareReceiveReq);
} else if (!strcmp(messageType, "FirmwareStatusNotification")) {
msg = new FirmwareStatusNotification();
#endif
} else {
Serial.println(F("[SimpleOcppOperationFactory] Operation not supported"));
msg = new NotImplemented();
}
if (msg == NULL) {
delete operation;
return NULL;
} else {
operation->setOcppMessage(msg);
return operation;
}
}
OcppOperation* makeOcppOperation(OcppMessage *msg){
if (msg == NULL) {
Serial.print(F("[SimpleOcppOperationFactory] in makeOcppOperation(webSocket, ocppMessage): ocppMessage is null!\n"));
return NULL;
}
OcppOperation *operation = makeOcppOperation();
operation->setOcppMessage(msg);
return operation;
}
OcppOperation* makeOcppOperation(){
OcppOperation *result = new OcppOperation();
return result;
}
} //end namespace ArduinoOcpp