forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection.cpp
More file actions
91 lines (76 loc) · 2.5 KB
/
Copy pathConnection.cpp
File metadata and controls
91 lines (76 loc) · 2.5 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
// matth-x/ArduinoOcpp
// Copyright Matthias Akstaller 2019 - 2023
// MIT License
#include <ArduinoOcpp/Core/Connection.h>
#include <ArduinoOcpp/Debug.h>
using namespace ArduinoOcpp;
void LoopbackConnection::loop() { }
bool LoopbackConnection::sendTXT(std::string &out) {
if (!connected) {
return true;
}
if (receiveTXT) {
lastRecv = ao_tick_ms();
return receiveTXT(out.c_str(), out.length());
} else {
return false;
}
}
void LoopbackConnection::setReceiveTXTcallback(ReceiveTXTcallback &receiveTXT) {
this->receiveTXT = receiveTXT;
}
unsigned long LoopbackConnection::getLastRecv() {
return lastRecv;
}
#ifndef AO_CUSTOM_WS
using namespace ArduinoOcpp::EspWiFi;
WSClient::WSClient(WebSocketsClient *wsock) : wsock(wsock) {
}
void WSClient::loop() {
wsock->loop();
}
bool WSClient::sendTXT(std::string &out) {
return wsock->sendTXT(out.c_str(), out.length());
}
void WSClient::setReceiveTXTcallback(ReceiveTXTcallback &callback) {
auto& captureLastRecv = lastRecv;
wsock->onEvent([callback, &captureLastRecv](WStype_t type, uint8_t * payload, size_t length) {
switch (type) {
case WStype_DISCONNECTED:
AO_DBG_INFO("Disconnected");
break;
case WStype_CONNECTED:
AO_DBG_INFO("Connected to url: %s", payload);
captureLastRecv = ao_tick_ms();
break;
case WStype_TEXT:
if (callback((const char *) payload, length)) { //forward message to RequestQueue
captureLastRecv = ao_tick_ms();
} else {
AO_DBG_WARN("Processing WebSocket input event failed");
}
break;
case WStype_BIN:
AO_DBG_WARN("Binary data stream not supported");
break;
case WStype_PING:
// pong will be send automatically
AO_DBG_TRAFFIC_IN(8, "WS ping");
captureLastRecv = ao_tick_ms();
break;
case WStype_PONG:
// answer to a ping we send
AO_DBG_TRAFFIC_IN(8, "WS pong");
captureLastRecv = ao_tick_ms();
break;
case WStype_FRAGMENT_TEXT_START: //fragments are not supported
default:
AO_DBG_WARN("Unsupported WebSocket event type");
break;
}
});
}
unsigned long WSClient::getLastRecv() {
return lastRecv;
}
#endif