Skip to content

Commit f1585ac

Browse files
committed
Add manually entered MAC addresses to the list of mac addresses
* Add manually entered MAC addresses to the list of mac addresses * Since this isn't stored in flash they will be gone after a restart tho * Also add the default target ip to the datalist for the target ip
1 parent 7cc236d commit f1585ac

File tree

4 files changed

+76
-57
lines changed

4 files changed

+76
-57
lines changed

images/web-interface.png

940 Bytes
Loading

src/NetworkHandler.cpp

Lines changed: 62 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void NetworkHandler::onWiFiEvent(WiFiEvent_t event) {
6363
Serial.print("STA IP: ");
6464
Serial.println(localhost = WiFi.localIP());
6565

66-
if (DEFAULT_TARGET_BROADCAST == IPAddress((uint) 0)) {
66+
if (DEFAULT_TARGET_BROADCAST == IPAddress((uint32_t) 0)) {
6767
targetBroadcast = WiFi.broadcastIP();
6868
}
6969
break;
@@ -142,67 +142,15 @@ void NetworkHandler::setupOTA() {
142142
}
143143

144144
void NetworkHandler::onIndexGet(AsyncWebServerRequest *request) {
145-
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"), "");
169-
response = std::regex_replace(response, std::regex("\\$target"),
170-
targetBroadcast.toString().c_str());
171-
145+
std::string response = prepareIndexResponse("",
146+
targetBroadcast.toString());
172147
request->send(200, "text/html", response.c_str());
173148
}
174149

175150
void NetworkHandler::onIndexPost(AsyncWebServerRequest *request) {
176151
if (request->hasParam("device", true) && request->hasParam("target", true)) {
177152
String device = request->getParam("device", true)->value();
178153
String target = request->getParam("target", true)->value();
179-
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-
202-
response = std::regex_replace(response, std::regex("\\$target"),
203-
target.c_str());
204-
response = std::regex_replace(response, std::regex("\\$device"),
205-
device.c_str());
206154

207155
IPAddress originalBroadcastTarget = targetBroadcast;
208156

@@ -211,6 +159,7 @@ void NetworkHandler::onIndexPost(AsyncWebServerRequest *request) {
211159
message += device.c_str();
212160
message += "\".";
213161

162+
std::string response = prepareIndexResponse(device, target);
214163
response = std::regex_replace(response, std::regex("\\$message_type\" hidden>\\$message"), message.c_str());
215164
request->send(400, "text/html", response.c_str());
216165
} else if (!targetBroadcast.fromString(target)) {
@@ -219,9 +168,14 @@ void NetworkHandler::onIndexPost(AsyncWebServerRequest *request) {
219168
message += target.c_str();
220169
message += "\".";
221170

171+
std::string response = prepareIndexResponse(device, target);
222172
response = std::regex_replace(response, std::regex("\\$message_type\" hidden>\\$message"), message.c_str());
223173
request->send(400, "text/html", response.c_str());
224174
} else {
175+
if (std::count(deviceMacs.begin(), deviceMacs.end(), std::string(device.c_str())) == 0) {
176+
deviceMacs.push_back(std::string(device.c_str()));
177+
}
178+
225179
Serial.print("Waking up device ");
226180
Serial.print(device);
227181
Serial.println('.');
@@ -233,10 +187,63 @@ void NetworkHandler::onIndexPost(AsyncWebServerRequest *request) {
233187
message += device.c_str();
234188
message += '.';
235189

190+
std::string response = prepareIndexResponse(device, target);
236191
response = std::regex_replace(response, std::regex("\\$message_type\" hidden>\\$message"), message.c_str());
237192
request->send(200, "text/html", response.c_str());
238193
}
239194
} else {
240195
onIndexGet(request);
241196
}
242197
}
198+
199+
std::string NetworkHandler::prepareIndexResponse(const String device, const String target) {
200+
std::string response = indexHtml;
201+
202+
std::string devices;
203+
for (std::string device : deviceMacs) {
204+
devices += "<option value=\"";
205+
devices += device;
206+
devices += "\">\n";
207+
}
208+
response = std::regex_replace(response, std::regex("\\$devices"),
209+
devices.c_str());
210+
211+
std::vector<std::string> targetIPs;
212+
targetIPs.reserve(4);
213+
targetIPs.push_back(std::string(target.c_str()));
214+
std::string ip(targetBroadcast.toString().c_str());
215+
if (std::count(targetIPs.begin(), targetIPs.end(), ip) == 0) {
216+
targetIPs.push_back(ip);
217+
}
218+
IPAddress globalBroadcast(255, 255, 255, 255);
219+
ip = std::string(globalBroadcast.toString().c_str());
220+
if (std::count(targetIPs.begin(), targetIPs.end(), ip) == 0) {
221+
targetIPs.push_back(ip);
222+
}
223+
if (DEFAULT_TARGET_BROADCAST == IPAddress((uint32_t) 0)) {
224+
IPAddress localBroadcast = WiFi.broadcastIP();
225+
ip = std::string(localBroadcast.toString().c_str());
226+
if (std::count(targetIPs.begin(), targetIPs.end(), ip) == 0) {
227+
targetIPs.push_back(ip);
228+
}
229+
} else {
230+
ip = std::string(DEFAULT_TARGET_BROADCAST.toString().c_str());
231+
if (std::count(targetIPs.begin(), targetIPs.end(), ip) == 0) {
232+
targetIPs.push_back(ip);
233+
}
234+
}
235+
236+
std::string targets;
237+
for (std::string target : targetIPs) {
238+
targets += "<option value=\"";
239+
targets += target;
240+
targets += "\">\n";
241+
}
242+
response = std::regex_replace(response, std::regex("\\$targets"),
243+
targets.c_str());
244+
245+
response = std::regex_replace(response, std::regex("\\$device"), device.c_str());
246+
response = std::regex_replace(response, std::regex("\\$target"), target.c_str());
247+
248+
return response;
249+
}

src/NetworkHandler.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,24 @@ class NetworkHandler {
6969

7070
static void onWiFiEvent(WiFiEvent_t event);
7171

72+
/**
73+
* Set up the Web Server.
74+
*/
7275
static void setupWebServer();
7376

7477
static void setupOTA();
7578

7679
static void onIndexGet(AsyncWebServerRequest *request);
7780
static void onIndexPost(AsyncWebServerRequest *request);
81+
82+
/**
83+
* Replaces some variables in indexHtml to prepare it for being sent to the client.
84+
*
85+
* @param device The currently selected device MAC address.
86+
* @param target The currently selected target broadcast ip address.
87+
* @return The prepared index.html string.
88+
*/
89+
static std::string prepareIndexResponse(const String device, const String target);
7890
};
7991

8092
#endif /* SRC_NETWORKHANDLER_H_ */

src/html/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ <h1>Wake up settings:</h1>
1717
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}"
1818
title="The MAC address of the device to wake up."
1919
alt="The MAC address of the device to wake up."
20-
placeholder="The MAC address of the device to wake up."
20+
placeholder="Device MAC Address"
2121
list="devices" value="$device" minlength="17" maxlength="17"
2222
size="11" required />
2323
<datalist id="devices">$devices
@@ -27,7 +27,7 @@ <h1>Wake up settings:</h1>
2727
name="target" id="target"
2828
title="The broadcast IP address to send the wake on lan packet to."
2929
alt="The broadcast IP address to send the wake on lan packet to."
30-
placeholder="The broadcast IP address to send the wake on lan packet to."
30+
placeholder="Target Broadcast IP"
3131
list="targets" value="$target" size="11" maxlength="15" required />
3232
<datalist id="targets">$targets
3333
</datalist>

0 commit comments

Comments
 (0)