This repository has been archived by the owner on Nov 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Web.ino
226 lines (200 loc) · 7.86 KB
/
Web.ino
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const char HTTP_TITLE_LABEL[] PROGMEM = "<div class='l lt'><label>{v}</label><hr /></div>";
const char HTTP_CURRENT_STATE_LABEL[] PROGMEM = "<div class='l ls'> <label id='_ls2'>{ls1}</label> <label id='_ls2'>{ls2}</label> </div>";
const char HTTP_FW_LABEL[] PROGMEM = "<div class='l c k'><label>Firmware: {fw}</label></div>";
const char HTTP_HOME_BUTTON[] PROGMEM = "<div><input class='lnkbtn' type='button' value='Zurück' onclick=\"window.location.href='/'\" /></div>";
const char HTTP_SAVE_BUTTON[] PROGMEM = "<div><button name='btnSave' value='1' type='submit'>Speichern</button></div>";
const char HTTP_CONF[] PROGMEM = "<div><label>{st}:</label></div><div><input type='text' id='ccuip' name='ccuip' pattern='((^|\\.)((25[0-5])|(2[0-4]\\d)|(1\\d\\d)|([1-9]?\\d))){4}$' maxlength=16 placeholder='{st}' value='{ccuip}'></div><div><label>Gerätename:</label></div><div><input type='text' id='devicename' name='devicename' pattern='[A-Za-z0-9_ -]+' placeholder='Gerätename' value='{dn}'></div>";
const char HTTP_CONF_LOX[] PROGMEM = "<div><label>UDP Port:</label></div><div><input type='text' id='lox_udpport' pattern='[0-9]{1,5}' maxlength='5' name='lox_udpport' placeholder='UDP Port' value='{udp}'></div>";
const char HTTP_STATUSLABEL[] PROGMEM = "<div class='l c'>{sl}</div>";
void initWebserver() {
DEBUG("initWebServer()...");
WebServer.on("/set", webSetRelay);
WebServer.on("/10", []() {
sendDefaultWebCmdReply();
switchRelay(1, RELAYSTATE_OFF, TRANSMITSTATE);
});
WebServer.on("/11", []() {
sendDefaultWebCmdReply();
switchRelay(1, RELAYSTATE_ON, TRANSMITSTATE);
});
WebServer.on("/12", []() {
sendDefaultWebCmdReply();
switchRelay(1, RELAYSTATE_TOGGLE, TRANSMITSTATE);
});
WebServer.on("/20", []() {
sendDefaultWebCmdReply();
switchRelay(2, RELAYSTATE_OFF, TRANSMITSTATE);
});
WebServer.on("/21", []() {
sendDefaultWebCmdReply();
switchRelay(2, RELAYSTATE_ON, TRANSMITSTATE);
});
WebServer.on("/22", []() {
sendDefaultWebCmdReply();
switchRelay(2, RELAYSTATE_TOGGLE, TRANSMITSTATE);
});
WebServer.on("/getState", replyRelayState);
WebServer.on("/bootConfigMode", setBootConfigMode);
WebServer.on("/reboot", []() {
WebServer.send(200, "text/plain", "rebooting");
ESP.restart();
});
WebServer.on("/restart", []() {
WebServer.send(200, "text/plain", "rebooting");
ESP.restart();
});
WebServer.on("/reloadCUxD", []() {
String ret = reloadCUxDAddress(TRANSMITSTATE);
WebServer.send(200, "text/plain", ret);
});
WebServer.on("/version", versionHtml);
WebServer.on("/firmware", versionHtml);
WebServer.on("/config", configHtml);
httpUpdater.setup(&WebServer);
WebServer.onNotFound(defaultHtml);
WebServer.begin();
DEBUG("initWebServer()... DONE");
}
void webSetRelay() {
bool _transmitstate = TRANSMITSTATE;
byte _relaynum = 0;
byte _relaystate = RELAYSTATE_OFF;
if (WebServer.args() > 0) {
for (int i = 0; i < WebServer.args(); i++) {
if (WebServer.argName(i) == "ts") {
_transmitstate = WebServer.arg(i).toInt();
}
if (WebServer.argName(i) == "ch") {
_relaynum = WebServer.arg(i).toInt();
}
if (WebServer.argName(i) == "state") {
_relaystate = WebServer.arg(i).toInt();
}
}
} else {
DEBUG(F("webSwitchRelay()"));
}
switchRelay(_relaynum, _relaystate, _transmitstate);
sendDefaultWebCmdReply();
}
void replyRelayState() {
sendDefaultWebCmdReply();
}
void defaultHtml() {
if (WebServer.args() > 0) {
for (int i = 0; i < WebServer.args(); i++) {
if (WebServer.argName(i) == "btnR1") {
//
}
}
}
String page = FPSTR(HTTP_HEAD);
page += FPSTR(HTTP_ALL_STYLE);
if (GlobalConfig.BackendType == BackendType_HomeMatic)
page += FPSTR(HTTP_HM_STYLE);
if (GlobalConfig.BackendType == BackendType_Loxone)
page += FPSTR(HTTP_LOX_STYLE);
page += FPSTR(HTTP_HEAD_END);
page += FPSTR(HTTP_DEFAULTHTML);
page.replace("{v}", GlobalConfig.DeviceName);
page.replace("{ls1}", ((Relay1State == On) ? "AN" : "AUS"));
page.replace("{ls2}", ((Relay2State == On) ? "AN" : "AUS"));
String fwurl = FPSTR(GITHUB_REPO_URL);
String fwjsurl = FPSTR(GITHUB_REPO_URL);
fwurl.replace("api.", "");
fwurl.replace("repos/", "");
page.replace("{fwurl}", fwurl);
page += F("</div><script>");
page += FPSTR(HTTP_CUSTOMSCRIPT);
page += FPSTR(HTTP_CUSTOMUPDATESCRIPT);
page.replace("{fwjsurl}", fwjsurl);
page.replace("{fw}", FIRMWARE_VERSION);
page += F("</script></div></body></html>");
WebServer.sendHeader("Content-Length", String(page.length()));
WebServer.send(200, "text/html", page);
}
void configHtml() {
bool sc = false;
bool saveSuccess = false;
bool showHMDevError = false;
if (WebServer.args() > 0) {
for (int i = 0; i < WebServer.args(); i++) {
if (WebServer.argName(i) == "btnSave")
sc = (WebServer.arg(i).toInt() == 1);
if (WebServer.argName(i) == "ccuip")
strcpy(GlobalConfig.ccuIP, WebServer.arg(i).c_str());
if (WebServer.argName(i) == "devicename")
strcpy(GlobalConfig.DeviceName, WebServer.arg(i).c_str());
if (WebServer.argName(i) == "lox_udpport")
strcpy(LoxoneConfig.UDPPort, WebServer.arg(i).c_str());
}
if (sc) {
saveSuccess = saveSystemConfig();
if (GlobalConfig.BackendType == BackendType_HomeMatic) {
String ch1 = getStateCUxD(String(GlobalConfig.DeviceName) + ":1", "Address");
String ch2 = getStateCUxD(String(GlobalConfig.DeviceName) + ":2", "Address");
if (ch1 != "null" && ch2 != "null") {
showHMDevError = false;
HomeMaticConfig.Channel1Name = "CUxD." + ch1;
HomeMaticConfig.Channel2Name = "CUxD." + ch2;
} else {
showHMDevError = true;
}
}
}
}
String page = FPSTR(HTTP_HEAD);
//page += FPSTR(HTTP_SCRIPT);
page += FPSTR(HTTP_ALL_STYLE);
if (GlobalConfig.BackendType == BackendType_HomeMatic)
page += FPSTR(HTTP_HM_STYLE);
if (GlobalConfig.BackendType == BackendType_Loxone)
page += FPSTR(HTTP_LOX_STYLE);
page += FPSTR(HTTP_HEAD_END);
page += F("<div class='fbg'>");
page += F("<form method='post' action='config'>");
page += FPSTR(HTTP_TITLE_LABEL);
page += FPSTR(HTTP_CONF);
if (GlobalConfig.BackendType == BackendType_HomeMatic) {
page.replace("{st}", "CCU2 IP");
}
if (GlobalConfig.BackendType == BackendType_Loxone) {
page += FPSTR(HTTP_CONF_LOX);
page.replace("{st}", "MiniServer IP");
page.replace("{udp}", LoxoneConfig.UDPPort);
page.replace("{remanenz}", "Remanenzeingang");
}
page.replace("{dn}", GlobalConfig.DeviceName);
page.replace("{ccuip}", GlobalConfig.ccuIP);
page += FPSTR(HTTP_STATUSLABEL);
if (sc && !showHMDevError) {
if (saveSuccess) {
page.replace("{sl}", F("<label class='green'>Speichern erfolgreich.</label>"));
} else {
page.replace("{sl}", F("<label class='red'>Speichern fehlgeschlagen.</label>"));
}
}
if (showHMDevError)
page.replace("{sl}", "<label class='red'>Gerätenamen in CUxD prüfen!<br>Es müssen 2 Geräte existieren:<br>" + String(GlobalConfig.DeviceName) + ":1<br>" + String(GlobalConfig.DeviceName) + ":2</label>");
if (!sc && !showHMDevError)
page.replace("{sl}", "");
page += FPSTR(HTTP_SAVE_BUTTON);
page += FPSTR(HTTP_HOME_BUTTON);
page += FPSTR(HTTP_FW_LABEL);
page.replace("{fw}", FIRMWARE_VERSION);
page += F("</form></div>");
page += F("</div></body></html>");
page.replace("{v}", GlobalConfig.DeviceName);
WebServer.send(200, "text/html", page);
}
void sendDefaultWebCmdReply() {
String reply = createReplyString();
DEBUG("Sending Web-Reply: " + reply);
WebServer.send(200, "application/json", reply);
}
String createReplyString() {
return "{\"relay1\": " + String(Relay1State) + ", \"relay2\": " + String(Relay2State) + "}";
}
void versionHtml() {
WebServer.send(200, "text/plain", "<fw>" + FIRMWARE_VERSION + "</fw>");
}