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 0
/
Copy pathWeb.ino
233 lines (211 loc) · 10.3 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
226
227
228
229
230
231
232
233
const char HTTP_TITLE_LABEL[] PROGMEM = "<div class='l lt'><label>{v}</label></div>";
const char HTTP_FW_LABEL[] PROGMEM = "<div class='l c k'><label>Firmware: {fw}</label></div>";
const char HTTP_NEWFW_BUTTON[] PROGMEM = "<div><input class='fwbtn' id='fwbtn' type='button' value='Neue Firmware verfügbar' onclick=\"window.open('{fwurl}')\" /></div><div><input class='fwbtn' id='fwbtnupdt' type='button' value='Firmwaredatei einspielen' onclick=\"window.location.href='/update'\" /></div>";
const char HTTP_CONFIG_BUTTON[] PROGMEM = "<div></div><hr /><div></div><div><input class='lnkbtn' type='button' value='Konfiguration' onclick=\"window.location.href='/config'\" /></div>";
const char HTTP_CONF_START[] PROGMEM = "<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><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>";
const char HTTP_CONF_HM[] PROGMEM = "<div><label>Motorumschaltzeit<br>(0.50 - 5.00 Sek.):</label></div><div><input type='text' id='mst' name='mst' pattern='[0-9.]+' placeholder='Motorumschaltzeit' value='{mst}'></div><div><label>Fahrzeit hoch<br>(0.10 - 600.00 Sek):</label></div><div><input type='text' id='up' name='up' pattern='[0-9.]+' placeholder='Fahrzeit hoch' value='{up}'></div><div><label>Fahrzeit runter<br>(0.10 - 600.00 Sek):</label></div><div><input type='text' id='down' name='down' pattern='[0-9.]+' placeholder='Fahrzeit hoch' value='{down}'></div><div><label>Fahrten bis zur automatischen<br>Kalibrierfahrt (0 - 255)</label></div><div><input type='text' id='cal' name='cal' pattern='[0-9]+' placeholder='Kalibrierung nach x Fahrten' value='{cal}'></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>";
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_UPDOWN_BUTTONS[] PROGMEM = "<hr /><span class='l'><div><button name='btnAction' onclick='SetState(\"/set?level=100\"); return false;'>HOCH</button></div><div class='l ls'><label id='_ls'>{ls}%</label></div><div><button name='btnAction' onclick='SetState(\"/set?level=0\"); return false;'>RUNTER</button></div><hr /><div><button class='stpbtn' name='btnAction' onclick='SetState(\"/set?direction=stop\"); return false;'>STOP</button></div></span><hr /><div><button class='btnWert' name='btnAction' onclick='SetState(\"/set?level=\"+document.getElementById(\"shutter_value\").value); return false;'>FAHRE ZU WERT</button><input class='i' type='text' id='shutter_value' name='shutter_value' placeholder='Wert %' pattern='[0-9]{1,3}' value='' maxlength='3'></div>";
void initWebServer() {
WebServer.on("/set", webSetValue);
WebServer.on("/getState", []() {
sendDefaultWebCmdReply();
});
WebServer.on("/config", configHtml);
WebServer.on("/bootConfigMode", setBootConfigMode);
WebServer.on("/reboot", []() {
WebServer.send(200, "text/plain", "rebooting");
delay(1000);
ESP.restart();
});
WebServer.on("/restart", []() {
WebServer.send(200, "text/plain", "rebooting");
delay(1000);
ESP.restart();
});
httpUpdater.setup(&WebServer);
WebServer.onNotFound(defaultHtml);
WebServer.begin();
}
void webSetValue() {
bool useCUxDValue = false;
int val = 0;
if (WebServer.args() > 0) {
for (int i = 0; i < WebServer.args(); i++) {
if (WebServer.argName(i) == "level") {
int val = atoi(WebServer.arg(i).c_str());
DEBUG("webSetValue LEVEL = " + String(val));
if (val >= 0 && val <= 100) {
ShutterControl(val);
}
break;
}
if (WebServer.argName(i) == "cuxdlevel") {
int val = atoi(WebServer.arg(i).c_str());
val = val / 10;
DEBUG("webSetValue CUXDLEVEL = " + String(val));
if (val >= 0 && val <= 100) {
ShutterControl(val);
}
break;
}
if (WebServer.argName(i) == "direction") {
if (WebServer.arg(i) == "stop") {
DEBUG("webSetValue: STOP");
switch_relay(DIRECTION_NONE);
switch_relay(DIRECTION_NONE);
break;
}
if (WebServer.arg(i) == "up") {
DEBUG("webSetValue: UP");
relay_switched_millis = millis();
switch_relay(DIRECTION_UP);
break;
}
if (WebServer.arg(i) == "down") {
DEBUG("webSetValue: DOWN");
relay_switched_millis = millis();
switch_relay(DIRECTION_DOWN);
break;
}
}
}
}
sendDefaultWebCmdReply();
}
void defaultHtml() {
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='control'>");
page += FPSTR(HTTP_TITLE_LABEL);
page.replace("{v}", GlobalConfig.DeviceName);
if (GlobalConfig.BackendType == BackendType_HomeMatic) {
page += FPSTR(HTTP_UPDOWN_BUTTONS);
page.replace("{ls}", String(ShutterConfig.CurrentValue));
}
page += FPSTR(HTTP_CONFIG_BUTTON);
page += FPSTR(HTTP_FW_LABEL);
page += FPSTR(HTTP_NEWFW_BUTTON);
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 (WebServer.argName(i) == "mst")
ShutterConfig.MotorSwitchingTime = strtod(WebServer.arg(i).c_str(), NULL);
if (WebServer.argName(i) == "down")
ShutterConfig.DriveTimeDown = strtod(WebServer.arg(i).c_str(), NULL);
if (WebServer.argName(i) == "up")
ShutterConfig.DriveTimeUp = strtod(WebServer.arg(i).c_str(), NULL);
if (WebServer.argName(i) == "cal")
ShutterConfig.DrivesUntilCalib = atoi(WebServer.arg(i).c_str());
}
if (sc) {
if (ShutterConfig.DriveTimeDown != 0.0 && ShutterConfig.DriveTimeUp != 0.0) {
saveSuccess = saveSystemConfig();
} else saveSuccess = false;
HomeMaticConfig.UseCCU = (String(GlobalConfig.ccuIP) != "0.0.0.0");
if (GlobalConfig.BackendType == BackendType_HomeMatic) {
String devName = getStateCUxD(GlobalConfig.DeviceName, "Address") ;
if (devName != "null") {
showHMDevError = false;
HomeMaticConfig.ChannelName = "CUxD." + devName;
} 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_START);
if (GlobalConfig.BackendType == BackendType_HomeMatic) {
page += FPSTR(HTTP_CONF_HM);
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("{dn}", GlobalConfig.DeviceName);
page.replace("{ccuip}", GlobalConfig.ccuIP);
page.replace("{mst}", String(ShutterConfig.MotorSwitchingTime));
page.replace("{up}", String(ShutterConfig.DriveTimeUp));
page.replace("{down}", String(ShutterConfig.DriveTimeDown));
page.replace("{cal}", String(ShutterConfig.DrivesUntilCalib));
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}", F("<label class='red'>Gerätenamen in CUxD prüfen!</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 currentDirection = "NONE";
if (DRIVING_DIRECTION == DIRECTION_UP) currentDirection = "UP";
if (DRIVING_DIRECTION == DIRECTION_DOWN) currentDirection = "DOWN";
String reply = "{\"motor_direction\": \"" + currentDirection + "\"";
if (GlobalConfig.BackendType == BackendType_HomeMatic) {
reply += " , \"state\": \"" + String(ShutterConfig.CurrentValue) + "\"";
}
reply += "}";
//DEBUG("Sending Web-Reply: " + reply);
WebServer.send(200, "application/json", reply);
}