forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOcppServer.cpp
More file actions
161 lines (137 loc) · 5.34 KB
/
Copy pathOcppServer.cpp
File metadata and controls
161 lines (137 loc) · 5.34 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
// matth-x/ArduinoOcpp
// Copyright Matthias Akstaller 2019 - 2022
// MIT License
#include <ArduinoOcpp/Core/OcppServer.h>
#include <Variants.h>
#ifndef AO_CUSTOM_WS
using namespace ArduinoOcpp::EspWiFi;
OcppServer::OcppServer() {
wsockServer.begin();
wsockServer.onEvent([this](WsClient num, WStype_t type, uint8_t * payload, size_t length) {
this->wsockEvent(num, type, payload, length);
});
instance = this;
}
void OcppServer::wsockEvent(WsClient num, WStype_t type, uint8_t * payload, size_t length) {
switch(type) {
case WStype_DISCONNECTED:
{
if (DEBUG_OUT) Serial.print(F("[OcppServer] WsClient disconnected! num = "));
if (DEBUG_OUT) Serial.println(num);
}
break;
case WStype_CONNECTED:
{
IPAddress ip = wsockServer.remoteIP(num);
if (DEBUG_OUT) {
Serial.print(F("[OcppServer] WsClient connected! num = "));
Serial.print(num);
Serial.print(F(", Connected from IP = "));
ip.printTo(Serial);
Serial.print(F(", with payload "));
Serial.println((const char*) payload);
}
bool found = false;
for (auto route = receiveTXTrouting.begin(); route != receiveTXTrouting.end(); ++route) {
if (ip == (*route).ip_addr) {
if (DEBUG_OUT) Serial.print(F("[OcppServer] IPAddress matches existing route!\n"));
(*route).num = num;
found = true;
break;
}
}
if (!found) {
Serial.print(F("[OcppServer] Unknown IP address! Please see addReceiveTXTcallback(IPAddress ...)\n"));
}
}
break;
case WStype_TEXT:
{
if (DEBUG_OUT || TRAFFIC_OUT) {
Serial.print(F("[OcppServer] Get TXT from client: "));
Serial.print(num);
Serial.print(F(", TXT = "));
Serial.println((const char*) payload);
}
bool found = false;
for (auto route = receiveTXTrouting.begin(); route != receiveTXTrouting.end(); ++route) {
if (num == (*route).num) {
if (DEBUG_OUT) Serial.print(F("\n"));
if (!((*route).processTXT((const char*) payload, length))) {
Serial.print(F("[OcppServer] Processing WebSocket input event failed!\n"));
}
found = true;
break;
}
}
if (!found) {
Serial.print(F("[OcppServer] Received msg from unknown client!\n"));
}
}
break;
case WStype_PING:
// pong will be send automatically
Serial.printf("[OcppServer] get ping, client = %u\n", num);
break;
case WStype_PONG:
// answer to a ping we send
Serial.printf("[OcppServer] get pong, client = %u\n", num);
break;
case WStype_FRAGMENT_TEXT_START: //fragments are not supported
case WStype_BIN:
default:
Serial.print(F("[OcppServer] Unsupported WebSocket event type, client = "));
Serial.println(num);
break;
}
}
OcppServer *OcppServer::instance = NULL;
OcppServer *OcppServer::getInstance() {
if (!instance){
instance = new OcppServer();
}
return instance;
}
void OcppServer::loop() {
wsockServer.loop();
}
void OcppServer::setReceiveTXTcallback(IPAddress &ip_addr, ReceiveTXTcallback &callback) {
/*
* Does route identified by ip_addr already exist?
*/
std::vector<ReceiveTXTroute>::iterator result = std::find_if(receiveTXTrouting.begin(), receiveTXTrouting.end(),
[ip_addr](const ReceiveTXTroute &elem) {
return elem.ip_addr == ip_addr;
});
if (result != receiveTXTrouting.end()) {
//found a route. Update callback
(*result).processTXT = callback;
} else {
//no route found. Add new one
ReceiveTXTroute route {};
route.ip_addr = ip_addr;
route.processTXT = callback;
receiveTXTrouting.push_back(route);
}
}
void OcppServer::removeReceiveTXTcallback(IPAddress &ip_addr) {
receiveTXTrouting.erase(std::remove_if(receiveTXTrouting.begin(), receiveTXTrouting.end(),
[ip_addr](const ReceiveTXTroute &elem) {
return elem.ip_addr == ip_addr;
}), receiveTXTrouting.end());
}
bool OcppServer::sendTXT(IPAddress &ip_addr, std::string &out) {
WsClient mClient;
std::vector<ReceiveTXTroute>::iterator result = std::find_if(receiveTXTrouting.begin(), receiveTXTrouting.end(),
[ip_addr](const ReceiveTXTroute &elem) {
return elem.ip_addr == ip_addr;
});
if (result != receiveTXTrouting.end()) {
mClient = (*result).num;
} else {
Serial.print(F("[OcppServer] Tried to send TXT for unregistered IP address! Abort\n"));
return false;
}
return wsockServer.sendTXT(mClient, out.c_str(), out.length());
}
#endif //ndef AO_CUSTOM_WS