Skip to content

Commit 7cc236d

Browse files
committed
Convert mac address selection to input field with datalist
to allow for arbitrary device mac addresses. * Add a datalist to the target broadcast ip input, containing the last target mac and 255.255.255.255 * Reset target broadcast ip to last valid ip when an invalid one is received.
1 parent a11f5c7 commit 7cc236d

File tree

4 files changed

+67
-14
lines changed

4 files changed

+67
-14
lines changed

TODO.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# TODO
2-
* Add support for arbitrary mac addresses on the web interface
32
* Add Wake on Lan password support
43
* Add basic unit test(s)
54
* Add persistent config

images/web-interface.png

-1.82 KB
Loading

src/NetworkHandler.cpp

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,11 @@ void NetworkHandler::setupWebServer() {
8484

8585
std::istringstream deviceStream(deviceMacs);
8686
std::string device;
87-
std::string devices;
8887
while (std::getline(deviceStream, device, '\n')) {
8988
if (device.length() > 0) {
9089
NetworkHandler::deviceMacs.push_back(device);
91-
devices += "<option value=\"";
92-
devices += device;
93-
devices += "\">";
94-
devices += device;
95-
devices += "</option>\n";
9690
}
9791
}
98-
indexHtml = std::regex_replace(indexHtml, std::regex("\\$devices"),
99-
devices.c_str());
10092

10193
webServer.on("/", HTTP_GET, onIndexGet);
10294
webServer.on("/", HTTP_POST, onIndexPost);
@@ -151,19 +143,68 @@ void NetworkHandler::setupOTA() {
151143

152144
void NetworkHandler::onIndexGet(AsyncWebServerRequest *request) {
153145
std::string response = indexHtml;
146+
147+
std::string devices;
148+
for (std::string device : deviceMacs) {
149+
devices += "<option value=\"";
150+
devices += device;
151+
devices += "\">\n";
152+
}
153+
response = std::regex_replace(response, std::regex("\\$devices"),
154+
devices.c_str());
155+
156+
std::string targets = "<option value=\"";
157+
targets += targetBroadcast.toString().c_str();
158+
targets += "\">\n";
159+
IPAddress broadcast(255, 255, 255, 255);
160+
if (targetBroadcast != broadcast) {
161+
targets += "<option value=\"";
162+
targets += broadcast.toString().c_str();
163+
targets += "\">\n";
164+
}
165+
response = std::regex_replace(response, std::regex("\\$targets"),
166+
targets.c_str());
167+
168+
response = std::regex_replace(response, std::regex("\\$device"), "");
154169
response = std::regex_replace(response, std::regex("\\$target"),
155170
targetBroadcast.toString().c_str());
171+
156172
request->send(200, "text/html", response.c_str());
157173
}
158174

159175
void NetworkHandler::onIndexPost(AsyncWebServerRequest *request) {
160176
if (request->hasParam("device", true) && request->hasParam("target", true)) {
161177
String device = request->getParam("device", true)->value();
162178
String target = request->getParam("target", true)->value();
163-
164179
std::string response = indexHtml;
180+
181+
std::string devices;
182+
for (std::string device : deviceMacs) {
183+
devices += "<option value=\"";
184+
devices += device;
185+
devices += "\">\n";
186+
}
187+
response = std::regex_replace(response, std::regex("\\$devices"),
188+
devices.c_str());
189+
190+
std::string targets = "<option value=\"";
191+
targets += targetBroadcast.toString().c_str();
192+
targets += "\">\n";
193+
IPAddress broadcast(255, 255, 255, 255);
194+
if (targetBroadcast != broadcast) {
195+
targets += "<option value=\"";
196+
targets += broadcast.toString().c_str();
197+
targets += "\">\n";
198+
}
199+
response = std::regex_replace(response, std::regex("\\$targets"),
200+
targets.c_str());
201+
165202
response = std::regex_replace(response, std::regex("\\$target"),
166203
target.c_str());
204+
response = std::regex_replace(response, std::regex("\\$device"),
205+
device.c_str());
206+
207+
IPAddress originalBroadcastTarget = targetBroadcast;
167208

168209
if (!WakeOnLanGenerator::isValidMac(device.c_str())) {
169210
std::string message = "error\">Received invalid device mac address \"";
@@ -173,6 +214,7 @@ void NetworkHandler::onIndexPost(AsyncWebServerRequest *request) {
173214
response = std::regex_replace(response, std::regex("\\$message_type\" hidden>\\$message"), message.c_str());
174215
request->send(400, "text/html", response.c_str());
175216
} else if (!targetBroadcast.fromString(target)) {
217+
targetBroadcast = originalBroadcastTarget;
176218
std::string message = "error\">Received invalid target IP address \"";
177219
message += target.c_str();
178220
message += "\".";

src/html/index.html

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,26 @@
1212
<br />
1313
<h1>ESP Wake on Lan Server</h1>
1414
<h1>Wake up settings:</h1>
15-
<label for="device">Device: </label> <select class="input"
16-
name="device" id="device" required> $devices
17-
</select><br /> <label for="target">Target: </label> <input class="input"
15+
<label for="device">Device: </label> <input class="input" type="text"
16+
name="device" id="device"
17+
pattern="[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}"
18+
title="The MAC address of the device to wake up."
19+
alt="The MAC address of the device to wake up."
20+
placeholder="The MAC address of the device to wake up."
21+
list="devices" value="$device" minlength="17" maxlength="17"
22+
size="11" required />
23+
<datalist id="devices">$devices
24+
</datalist>
25+
<br /> <label for="target">Target: </label> <input class="input"
1826
type="text" pattern="\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
1927
name="target" id="target"
2028
title="The broadcast IP address to send the wake on lan packet to."
2129
alt="The broadcast IP address to send the wake on lan packet to."
22-
value="$target" size="11" maxlength="15" required /><br />
30+
placeholder="The broadcast IP address to send the wake on lan packet to."
31+
list="targets" value="$target" size="11" maxlength="15" required />
32+
<datalist id="targets">$targets
33+
</datalist>
34+
<br />
2335
<button class="wakeup" type="submit">Wake Up</button>
2436
</form>
2537
</body>

0 commit comments

Comments
 (0)