forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperationRegistry.cpp
More file actions
80 lines (65 loc) · 2.51 KB
/
OperationRegistry.cpp
File metadata and controls
80 lines (65 loc) · 2.51 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
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2024
// MIT License
#include <MicroOcpp/Core/OperationRegistry.h>
#include <MicroOcpp/Core/Operation.h>
#include <MicroOcpp/Core/Request.h>
#include <MicroOcpp/Core/OcppError.h>
#include <MicroOcpp/Debug.h>
#include <algorithm>
using namespace MicroOcpp;
OperationRegistry::OperationRegistry() : registry(makeVector<OperationCreator>("OperationRegistry")) {
}
OperationCreator *OperationRegistry::findCreator(const char *operationType) {
for (auto it = registry.begin(); it != registry.end(); ++it) {
if (!strcmp(it->operationType, operationType)) {
return &*it;
}
}
return nullptr;
}
void OperationRegistry::registerOperation(const char *operationType, std::function<Operation*()> creator) {
registry.erase(std::remove_if(registry.begin(), registry.end(),
[operationType] (const OperationCreator& el) {
return !strcmp(operationType, el.operationType);
}),
registry.end());
OperationCreator entry;
entry.operationType = operationType;
entry.creator = creator;
registry.push_back(entry);
MO_DBG_DEBUG("registered operation %s", operationType);
}
void OperationRegistry::setOnRequest(const char *operationType, OnReceiveReqListener onRequest) {
if (auto entry = findCreator(operationType)) {
entry->onRequest = onRequest;
} else {
MO_DBG_ERR("%s not registered", operationType);
}
}
void OperationRegistry::setOnResponse(const char *operationType, OnSendConfListener onResponse) {
if (auto entry = findCreator(operationType)) {
entry->onResponse = onResponse;
} else {
MO_DBG_ERR("%s not registered", operationType);
}
}
std::unique_ptr<Request> OperationRegistry::deserializeOperation(const char *operationType) {
if (auto entry = findCreator(operationType)) {
auto payload = entry->creator();
if (payload) {
auto result = std::unique_ptr<Request>(new Request(
std::unique_ptr<Operation>(payload)));
result->setOnReceiveReqListener(entry->onRequest);
result->setOnSendConfListener(entry->onResponse);
return result;
}
}
return std::unique_ptr<Request>(new Request(
std::unique_ptr<Operation>(new NotImplemented())));
}
void OperationRegistry::debugPrint() {
for (auto& creator : registry) {
MO_CONSOLE_PRINTF("[OCPP] > %s\n", creator.operationType);
}
}