-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoopHandlerWebservice.cpp
More file actions
67 lines (55 loc) · 1.99 KB
/
Copy pathLoopHandlerWebservice.cpp
File metadata and controls
67 lines (55 loc) · 1.99 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
#include "LoopHandlerWebservice.hpp"
#include "GroupPresenter.hpp"
#include "GroupBundle.hpp"
#include "passwords.h"
LoopHandlerWebservice::LoopHandlerWebservice(GroupBundle* bundle, bool debugOutput) : LoopHandler(bundle, debugOutput), m_nanoesp(), m_http(m_nanoesp)
{
// initalise nano esp
m_nanoesp.init(debugOutput);
Serial.println(F("Trying to connect WIFI"));
// Only station mode for web service
m_nanoesp.configWifiStation(SSID, PASSWORD);
// Wait till connected to WiFi
if (m_nanoesp.wifiConnected()) {
Serial.println(F("WIFI connected"));
// Print IP in terminal
Serial.println("IP-address: " + m_nanoesp.getIp());
//Start TCP server
if (m_nanoesp.startTcpServer(80)) {
Serial.println(F("TCP server active"));
} else {
error(F("TCP server error"));
}
} else {
error(F("WIFI connection failed"));
}
}
void LoopHandlerWebservice::loopInternal() {
String method, resource, parameter;
int id;
if (m_http.recvRequest(id, method, resource, parameter)) {
if (m_debugOutput) {
Serial.println("New Request from id :" + String(id) + ", method: " + method + ", resource: " + resource + " parameter: " + parameter);
}
// send config
WebservicePresenter presenter(id, &m_http);
// print configuration to webservice
m_bundle->printConfig(&presenter);
// close HTTP connection
m_nanoesp.closeConnection(id);
if (m_debugOutput) {
Serial.println(F("Connection closed."));
}
// check for GET parameter content
if (parameter != "") {
// id=31
String sId = parameter.substring(parameter.indexOf('=') + 1, parameter.indexOf('&'));
String sNewState = parameter.substring(parameter.indexOf('&') + 1);
sNewState = sNewState.substring(sNewState.indexOf('=') + 1);
m_bundle->switchGroup(sId.toInt(), sNewState.equals(String(F("on"))));
if (m_debugOutput) {
Serial.println(String(F("Switch ID: ")) + sId + String(F(" to state: ")) + sNewState);
}
}
}
}