Skip to content

Commit c0732dc

Browse files
authored
fix arduinoWebSockets HTTP path (#283)
* use WS path instead of URL (#278) * update arduinoWebSockets integration
1 parent 94eb0eb commit c0732dc

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
- Fix transaction freeze in offline mode ([#279](https://github.com/matth-x/MicroOcpp/pull/279), [#287](https://github.com/matth-x/MicroOcpp/pull/287))
4141
- Fix compilation error caused by `PRId32` ([#279](https://github.com/matth-x/MicroOcpp/pull/279))
4242
- Don't load FW-mngt. module when no handlers set ([#271](https://github.com/matth-x/MicroOcpp/pull/271))
43+
- Change arduinoWebSockets URL param to path ([#278](https://github.com/matth-x/MicroOcpp/issues/278))
4344
- Avoid creating conf when operation fails ([#290](https://github.com/matth-x/MicroOcpp/pull/290))
4445
- Fix whitespaces in MeterValues ([#301](https://github.com/matth-x/MicroOcpp/pull/301))
4546

src/MicroOcpp.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ void mocpp_initialize(const char *backendUrl, const char *chargeBoxId, const cha
9797
//parse host, port
9898
std::string host_port_path = url.substr(url.find_first_of("://") + strlen("://"));
9999
std::string host_port = host_port_path.substr(0, host_port_path.find_first_of('/'));
100+
std::string path = host_port_path.substr(host_port.length());
100101
std::string host = host_port.substr(0, host_port.find_first_of(':'));
101102
if (host.empty()) {
102103
MO_DBG_ERR("could not parse host: %s", url.c_str());
@@ -123,28 +124,29 @@ void mocpp_initialize(const char *backendUrl, const char *chargeBoxId, const cha
123124
}
124125
}
125126

127+
if (path.empty()) {
128+
path = "/";
129+
}
130+
126131
if ((!*chargeBoxId) == '\0') {
127-
if (url.back() != '/') {
128-
url += '/';
132+
if (path.back() != '/') {
133+
path += '/';
129134
}
130135

131-
url += chargeBoxId;
136+
path += chargeBoxId;
132137
}
133138

134-
MO_DBG_INFO("connecting to %s -- (host: %s, port: %u)", url.c_str(), host.c_str(), port);
139+
MO_DBG_INFO("connecting to %s -- (host: %s, port: %u, path: %s)", url.c_str(), host.c_str(), port, path.c_str());
135140

136141
if (!webSocket)
137142
webSocket = new WebSocketsClient();
138143

139-
if (isTLS)
140-
{
141-
// server address, port, URL and TLS certificate
142-
webSocket->beginSslWithCA(host.c_str(), port, url.c_str(), CA_cert, "ocpp1.6");
143-
}
144-
else
145-
{
146-
// server address, port, URL
147-
webSocket->begin(host.c_str(), port, url.c_str(), "ocpp1.6");
144+
if (isTLS) {
145+
// server address, port, path and TLS certificate
146+
webSocket->beginSslWithCA(host.c_str(), port, path.c_str(), CA_cert, "ocpp1.6");
147+
} else {
148+
// server address, port, path
149+
webSocket->begin(host.c_str(), port, path.c_str(), "ocpp1.6");
148150
}
149151

150152
// try ever 5000 again if connection has failed

src/MicroOcpp/Core/Connection.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void WSClient::setReceiveTXTcallback(ReceiveTXTcallback &callback) {
7272
MO_DBG_INFO("Disconnected");
7373
break;
7474
case WStype_CONNECTED:
75-
MO_DBG_INFO("Connected to url: %s", payload);
75+
MO_DBG_INFO("Connected (path: %s)", payload);
7676
captureLastRecv = mocpp_tick_ms();
7777
captureLastConnected = mocpp_tick_ms();
7878
break;
@@ -112,4 +112,8 @@ unsigned long WSClient::getLastConnected() {
112112
return lastConnected;
113113
}
114114

115+
bool WSClient::isConnected() {
116+
return wsock->isConnected();
117+
}
118+
115119
#endif

src/MicroOcpp/Core/Connection.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ class WSClient : public Connection {
118118
unsigned long getLastRecv() override; //get time of last successful receive in millis
119119

120120
unsigned long getLastConnected() override; //get last connection creation in millis
121+
122+
bool isConnected() override;
121123
};
122124

123125
} //end namespace EspWiFi

0 commit comments

Comments
 (0)