@@ -63,7 +63,7 @@ void NetworkHandler::onWiFiEvent(WiFiEvent_t event) {
63
63
Serial.print (" STA IP: " );
64
64
Serial.println (localhost = WiFi.localIP ());
65
65
66
- if (DEFAULT_TARGET_BROADCAST == IPAddress ((uint ) 0 )) {
66
+ if (DEFAULT_TARGET_BROADCAST == IPAddress ((uint32_t ) 0 )) {
67
67
targetBroadcast = WiFi.broadcastIP ();
68
68
}
69
69
break ;
@@ -142,67 +142,15 @@ void NetworkHandler::setupOTA() {
142
142
}
143
143
144
144
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 ());
172
147
request->send (200 , " text/html" , response.c_str ());
173
148
}
174
149
175
150
void NetworkHandler::onIndexPost (AsyncWebServerRequest *request) {
176
151
if (request->hasParam (" device" , true ) && request->hasParam (" target" , true )) {
177
152
String device = request->getParam (" device" , true )->value ();
178
153
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 ());
206
154
207
155
IPAddress originalBroadcastTarget = targetBroadcast;
208
156
@@ -211,6 +159,7 @@ void NetworkHandler::onIndexPost(AsyncWebServerRequest *request) {
211
159
message += device.c_str ();
212
160
message += " \" ." ;
213
161
162
+ std::string response = prepareIndexResponse (device, target);
214
163
response = std::regex_replace (response, std::regex (" \\ $message_type\" hidden>\\ $message" ), message.c_str ());
215
164
request->send (400 , " text/html" , response.c_str ());
216
165
} else if (!targetBroadcast.fromString (target)) {
@@ -219,9 +168,14 @@ void NetworkHandler::onIndexPost(AsyncWebServerRequest *request) {
219
168
message += target.c_str ();
220
169
message += " \" ." ;
221
170
171
+ std::string response = prepareIndexResponse (device, target);
222
172
response = std::regex_replace (response, std::regex (" \\ $message_type\" hidden>\\ $message" ), message.c_str ());
223
173
request->send (400 , " text/html" , response.c_str ());
224
174
} 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
+
225
179
Serial.print (" Waking up device " );
226
180
Serial.print (device);
227
181
Serial.println (' .' );
@@ -233,10 +187,63 @@ void NetworkHandler::onIndexPost(AsyncWebServerRequest *request) {
233
187
message += device.c_str ();
234
188
message += ' .' ;
235
189
190
+ std::string response = prepareIndexResponse (device, target);
236
191
response = std::regex_replace (response, std::regex (" \\ $message_type\" hidden>\\ $message" ), message.c_str ());
237
192
request->send (200 , " text/html" , response.c_str ());
238
193
}
239
194
} else {
240
195
onIndexGet (request);
241
196
}
242
197
}
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
+ }
0 commit comments