Skip to content

Commit a54e523

Browse files
authored
Renew privacy area handling in config server (#80) (#266)
- separate GPS status display and `Make Current Location Private` function. fixes #80 - allow to edit all available privacy areas on the page, fixes #107 - redirect to the privacy area page after update. fixes #77 - pull current gps status on page, fixes #75 - fixes #74 - redirect to main page if privacy area data was not changed
1 parent b444cfd commit a54e523

File tree

1 file changed

+141
-71
lines changed

1 file changed

+141
-71
lines changed

src/configServer.cpp

Lines changed: 141 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,21 @@ static const char* const configIndex =
360360
"<input type=submit class=btn value=Save>";
361361

362362
static const char* const privacyIndexPostfix =
363-
"<input type=submit onclick=\"window.location.href='/'\" class=btn value=Save>"
364-
"<input type=button onclick=\"window.location.href='/settings/privacy/makeCurrentLocationPrivate'\" class=btn value='Make current location private'>";
365-
366-
static const char* const makeCurrentLocationPrivateIndex =
367-
"<div>Making current location private, waiting for fix. Press device button to cancel.</div>";
363+
"<input type='submit' class='btn' value='Save'>"
364+
"<hr>"
365+
"Location: <div id='gps'>{gps}</div> <a href='javascript:window.location.reload()'>&#8635;</a>"
366+
"<input type='submit' name='addCurrent' id='addCurrent' class=btn value='Add current location' />"
367+
"<script>"
368+
"async function updateLocation() {"
369+
" if (document.readyState == 'complete') {"
370+
" const gps = await fetch('/gps').then(res => res.text());"
371+
" document.getElementById('gps').innerHTML = gps;"
372+
" }"
373+
" setTimeout(updateLocation, 1000);"
374+
"}"
375+
"setTimeout(updateLocation, 1000);"
376+
"</script>"
377+
;
368378

369379
static const char* const deleteIndex =
370380
"<h3>Flash</h3>"
@@ -462,8 +472,8 @@ static void handleDev(HTTPRequest * req, HTTPResponse * res);
462472
static void handleDevAction(HTTPRequest * req, HTTPResponse * res);
463473
#endif
464474
static void handlePrivacyAction(HTTPRequest * req, HTTPResponse * res);
475+
static void handleGps(HTTPRequest * req, HTTPResponse * res);
465476
static void handleUpload(HTTPRequest * req, HTTPResponse * res);
466-
static void handleMakeCurrentLocationPrivate(HTTPRequest * req, HTTPResponse * res);
467477
static void handlePrivacy(HTTPRequest *req, HTTPResponse *res);
468478
static void handlePrivacyDeleteAction(HTTPRequest *req, HTTPResponse *res);
469479
static void handleSd(HTTPRequest *req, HTTPResponse *res);
@@ -511,8 +521,8 @@ void registerPages(HTTPServer * httpServer) {
511521
httpServer->registerNode(new ResourceNode("/settings/development", HTTP_GET, handleDev));
512522
#endif
513523
httpServer->registerNode(new ResourceNode("/privacy_action", HTTP_POST, handlePrivacyAction));
524+
httpServer->registerNode(new ResourceNode("/gps", HTTP_GET, handleGps));
514525
httpServer->registerNode(new ResourceNode("/upload", HTTP_GET, handleUpload));
515-
httpServer->registerNode(new ResourceNode("/settings/privacy/makeCurrentLocationPrivate", HTTP_GET, handleMakeCurrentLocationPrivate));
516526
httpServer->registerNode(new ResourceNode("/settings/privacy", HTTP_GET, handlePrivacy));
517527
httpServer->registerNode(new ResourceNode("/privacy_delete", HTTP_GET, handlePrivacyDeleteAction));
518528
httpServer->registerNode(new ResourceNode("/sd", HTTP_GET, handleSd));
@@ -546,10 +556,10 @@ static void createHttpServer() {
546556
displayTest->showTextOnGrid(0, 4, "Creating ssl cert!");
547557
}
548558
serverSslCert = Https::getCertificate(progressTick);
549-
server = new HTTPSServer(serverSslCert, 443, 1);
559+
server = new HTTPSServer(serverSslCert, 443, 2);
550560
displayTest->clearProgressBar(5);
551561
displayTest->showTextOnGrid(0, 4, "");
552-
insecureServer = new HTTPServer(80, 1);
562+
insecureServer = new HTTPServer(80, 2);
553563

554564
beginPages();
555565

@@ -600,6 +610,12 @@ String replaceDefault(String html, const String& subTitle, const String& action
600610
return html;
601611
}
602612

613+
static void sendPlainText(HTTPResponse * res, const String& data) {
614+
res->setHeader("Content-Type", "text/plain");
615+
res->setHeader("Connection", "keep-alive");
616+
res->print(data);
617+
}
618+
603619
static void sendHtml(HTTPResponse * res, const String& data) {
604620
res->setHeader("Content-Type", "text/html");
605621
res->print(data);
@@ -1205,25 +1221,6 @@ static void handleDev(HTTPRequest *, HTTPResponse *res) {
12051221
}
12061222
#endif
12071223

1208-
static void handlePrivacyAction(HTTPRequest *req, HTTPResponse *res) {
1209-
const auto params = extractParameters(req);
1210-
1211-
String latitude = getParameter(params, "newlatitude");
1212-
latitude.replace(",", ".");
1213-
String longitude = getParameter(params, "newlongitude");
1214-
longitude.replace(",", ".");
1215-
String radius = getParameter(params, "newradius");
1216-
1217-
if ( (latitude != "") && (longitude != "") && (radius != "") ) {
1218-
Serial.println(F("Valid privacyArea!"));
1219-
theObsConfig->addPrivacyArea(0,
1220-
Gps::newPrivacyArea(atof(latitude.c_str()), atof(longitude.c_str()), atoi(radius.c_str())));
1221-
}
1222-
1223-
String s = "<meta http-equiv='refresh' content='0; url=/settings/privacy'><a href='/settings/privacy'>Go Back</a>";
1224-
sendHtml(res, s);
1225-
}
1226-
12271224
/* Upload tracks found on SD card to the portal server->
12281225
* This method also takes care to give appropriate feedback
12291226
* to the user about the progress. If httpRequest is true
@@ -1350,61 +1347,52 @@ static void handleUpload(HTTPRequest *, HTTPResponse * res) {
13501347
uploadTracks(res);
13511348
}
13521349

1353-
static void handleMakeCurrentLocationPrivate(HTTPRequest *, HTTPResponse *res) {
1354-
String html = createPage(makeCurrentLocationPrivateIndex);
1355-
html = replaceDefault(html, "MakeLocationPrivate");
1356-
sendHtml(res, html);
1357-
1358-
bool validGPSData = false;
1359-
buttonState = digitalRead(PushButton_PIN);
1360-
while (!validGPSData && (buttonState == LOW)) {
1361-
log_d("GPSData not valid");
1362-
buttonState = digitalRead(PushButton_PIN);
1363-
gps.handle();
1364-
validGPSData = gps.getCurrentGpsRecord().hasValidFix();
1365-
if (validGPSData) {
1366-
log_d("GPSData valid");
1367-
// FIXME: Not used?
1368-
Gps::newPrivacyArea(gps.getCurrentGpsRecord().getLatitude(),
1369-
gps.getCurrentGpsRecord().getLongitude(), 500);
1370-
}
1371-
delay(300);
1350+
static String getGpsStatusString() {
1351+
String gpsString;
1352+
auto gpsData = gps.getCurrentGpsRecord();
1353+
if (!gps.moduleIsAlive()) {
1354+
gpsString = "OFF?";
1355+
} else if (gpsData.hasValidFix()) {
1356+
gpsString = "Latitude:&nbsp;" + gpsData.getLatString() +
1357+
+" Longitude:&nbsp;" + gpsData.getLongString() +
1358+
+" Altitude:&nbsp;" + gpsData.getAltitudeMetersString()
1359+
+ "m HDOP:&nbsp;"
1360+
+ gpsData.getHdopString();
1361+
} else {
1362+
gpsString = "no&nbsp;fix " + String(gps.getValidSatellites()) + "&nbsp;satellites "
1363+
" SN:&nbsp;" + gps.getLastNoiseLevel();
13721364
}
1373-
1374-
// #77 - 200 cannot be send twice via HTTP
1375-
//String s = "<meta http-equiv='refresh' content='0; url=/settings/privacy'><a href='/settings/privacy'>Go Back</a>";
1376-
//server->send(200, "text/html", s); //Send web page
1377-
1365+
return gpsString;
13781366
}
13791367

13801368
static void handlePrivacy(HTTPRequest *, HTTPResponse *res) {
13811369
String privacyPage;
13821370
for (int idx = 0; idx < theObsConfig->getNumberOfPrivacyAreas(0); ++idx) {
13831371
auto pa = theObsConfig->getPrivacyArea(0, idx);
13841372
privacyPage += "<h3>Privacy Area #" + String(idx + 1) + "</h3>";
1385-
privacyPage += "Latitude <input name=latitude" + String(idx)
1386-
+ " placeholder='latitude' value='" + String(pa.latitude, 7) + "' disabled />";
1387-
privacyPage += "Longitude <input name='longitude" + String(idx)
1388-
+ "' placeholder='longitude' value='" + String(pa.longitude, 7) + "' disabled />";
1389-
privacyPage += "Radius (m) <input name='radius" + String(idx)
1390-
+ "' placeholder='radius' value='" + String(pa.radius) + "' disabled />";
1391-
privacyPage += "<a class='deletePrivacyArea' href='/privacy_delete?erase=" + String(idx) + "'>&#x2716;</a>";
1392-
}
1393-
1394-
privacyPage += "<h3>New Privacy Area <a href='javascript:window.location.reload()'>&#8635;</a></h3>";
1395-
gps.handle();
1396-
bool validGPSData = gps.getCurrentGpsRecord().hasValidFix();
1397-
if (validGPSData) {
1398-
privacyPage += "Latitude<input name='newlatitude' value='" + gps.getCurrentGpsRecord().getLatString() + "' />";
1399-
privacyPage += "Longitude<input name='newlongitude' value='" + gps.getCurrentGpsRecord().getLongString() + "' />";
1400-
} else {
1401-
privacyPage += "Latitude<input name='newlatitude' placeholder='48.12345' />";
1402-
privacyPage += "Longitude<input name='newlongitude' placeholder='9.12345' />";
1373+
const String &index = String(idx);
1374+
privacyPage += "Latitude <input name=latitude" + index
1375+
+ " placeholder='latitude' value='" + String(pa.latitude, 6) + "' />";
1376+
privacyPage += "<input type='hidden' name='oldLatitude" + index + "' value='" + String(pa.latitude, 6) + "' />";
1377+
1378+
privacyPage += "Longitude <input name='longitude" + index
1379+
+ "' placeholder='longitude' value='" + String(pa.longitude, 6) + "' />";
1380+
privacyPage += "<input type='hidden' name='oldLongitude" + index + "' value='" + String(pa.longitude, 6) + "' />";
1381+
1382+
privacyPage += "Radius (m) <input name='radius" + index
1383+
+ "' placeholder='radius' value='" + String(pa.radius, 0) + "' />";
1384+
privacyPage += "<input type='hidden' name='oldRadius" + index + "' value='" + String(pa.radius, 0) + "' />";
1385+
privacyPage += "<a class='deletePrivacyArea' href='/privacy_delete?erase=" + index + "'>&#x2716;</a>";
14031386
}
1387+
1388+
privacyPage += "<h3>New Privacy Area</h3>";
1389+
privacyPage += "Latitude<input name='newlatitude' placeholder='latitude' />";
1390+
privacyPage += "Longitude<input name='newlongitude' placeholder='longitude' />";
14041391
privacyPage += "Radius (m)<input name='newradius' placeholder='radius' value='500' />";
14051392

14061393
String html = createPage(privacyPage, privacyIndexPostfix);
14071394
html = replaceDefault(html, "Privacy Zones", "/privacy_action");
1395+
html.replace("{gps}", getGpsStatusString());
14081396
sendHtml(res, html);
14091397
}
14101398

@@ -1417,6 +1405,88 @@ static void handlePrivacyDeleteAction(HTTPRequest *req, HTTPResponse *res) {
14171405
sendRedirect(res, "/settings/privacy");
14181406
}
14191407

1408+
static bool makeCurrentLocationPrivate() {
1409+
bool modified = false;
1410+
auto gpsRecord = gps.getCurrentGpsRecord();
1411+
if (gpsRecord.hasValidFix()) {
1412+
theObsConfig->addPrivacyArea(
1413+
0,
1414+
Gps::newPrivacyArea(
1415+
gpsRecord.getLatitude(), gpsRecord.getLongitude(), 500));
1416+
modified = true;
1417+
}
1418+
return modified;
1419+
}
1420+
1421+
static bool updatePrivacyAreas(const std::vector<std::pair<String, String>> &params) {
1422+
bool modified = false;
1423+
for (int pos = 0; pos < theObsConfig->getNumberOfPrivacyAreas(0); ++pos) {
1424+
String idx = String(pos);
1425+
String latitude = getParameter(params, "latitude" + idx);
1426+
String oldLatitude = getParameter(params, "oldLatitude" + idx);
1427+
String longitude = getParameter(params, "longitude" + idx);
1428+
String oldLongitude = getParameter(params, "oldLongitude" + idx);
1429+
String radius = getParameter(params, "radius" + idx);
1430+
String oldRadius = getParameter(params, "oldRadius" + idx);
1431+
1432+
if ((latitude != "") && (longitude != "") && (radius != "")
1433+
&& ((latitude != oldLatitude) || (longitude != oldLongitude) || (radius != oldRadius))) {
1434+
latitude.replace(",", ".");
1435+
longitude.replace(",", ".");
1436+
log_i("Update privacyArea %d!", pos);
1437+
theObsConfig->setPrivacyArea(
1438+
0, pos,
1439+
Gps::newPrivacyArea(atof(latitude.c_str()), atof(longitude.c_str()),
1440+
atoi(radius.c_str())));
1441+
modified = true;
1442+
}
1443+
}
1444+
return modified;
1445+
}
1446+
1447+
static bool addPrivacyArea(const std::vector<std::pair<String, String>> &params) {
1448+
bool modified = false;
1449+
String latitude = getParameter(params, "newlatitude");
1450+
latitude.replace(",", ".");
1451+
String longitude = getParameter(params, "newlongitude");
1452+
longitude.replace(",", ".");
1453+
String radius = getParameter(params, "newradius");
1454+
1455+
if ((latitude != "") && (longitude != "") && (radius != "")) {
1456+
log_i("New valid privacyArea!");
1457+
theObsConfig->addPrivacyArea(
1458+
0,
1459+
Gps::newPrivacyArea(atof(latitude.c_str()), atof(longitude.c_str()),
1460+
atoi(radius.c_str())));
1461+
modified = true;
1462+
}
1463+
return modified;
1464+
}
1465+
1466+
static void handlePrivacyAction(HTTPRequest *req, HTTPResponse *res) {
1467+
const auto params = extractParameters(req);
1468+
bool modified = false;
1469+
1470+
if (!getParameter(params, "addCurrent").isEmpty()) {
1471+
modified = makeCurrentLocationPrivate();
1472+
} else {
1473+
modified = updatePrivacyAreas(params);
1474+
if (addPrivacyArea(params)) {
1475+
modified = true;
1476+
}
1477+
}
1478+
if (modified) {
1479+
theObsConfig->saveConfig();
1480+
sendRedirect(res, "/settings/privacy");
1481+
} else {
1482+
sendRedirect(res, "/");
1483+
}
1484+
}
1485+
1486+
static void handleGps(HTTPRequest * req, HTTPResponse * res) {
1487+
sendPlainText(res, getGpsStatusString());
1488+
}
1489+
14201490
static void handleFlashUpdate(HTTPRequest *, HTTPResponse * res) {
14211491
String html = createPage(updateSdIndex, xhrUpload);
14221492
html = replaceDefault(html, "Update Flash App", "/updateFlashUrl");
@@ -1993,7 +2063,7 @@ static void handleDelete(HTTPRequest *, HTTPResponse * res) {
19932063
html = replaceHtml(html, "{description}",
19942064
"Warning there is not safety question!");
19952065
sendHtml(res, html);
1996-
};
2066+
}
19972067

19982068
static void handleDeleteAction(HTTPRequest *req, HTTPResponse * res) {
19992069
// TODO: Result page with status!

0 commit comments

Comments
 (0)