2020-09-19 01:29:16 +00:00
|
|
|
#include "meshwifi/meshhttp.h"
|
|
|
|
#include "NodeDB.h"
|
2020-09-13 23:58:36 +00:00
|
|
|
#include "configuration.h"
|
|
|
|
#include "main.h"
|
2020-09-17 03:15:00 +00:00
|
|
|
#include "meshwifi/meshwifi.h"
|
2020-09-19 01:29:16 +00:00
|
|
|
#include <WebServer.h>
|
|
|
|
#include <WiFi.h>
|
2020-09-13 23:58:36 +00:00
|
|
|
|
|
|
|
WebServer webserver(80);
|
|
|
|
|
2020-09-19 23:38:59 +00:00
|
|
|
const uint16_t maxMessages = 50;
|
|
|
|
|
|
|
|
struct message_t {
|
2020-09-19 19:50:43 +00:00
|
|
|
char sender[10];
|
|
|
|
char message[250];
|
|
|
|
int32_t gpsLat;
|
|
|
|
int32_t gpsLong;
|
|
|
|
uint32_t time;
|
|
|
|
bool fromMe;
|
|
|
|
};
|
|
|
|
|
2020-09-19 23:38:59 +00:00
|
|
|
struct messages_t
|
|
|
|
{
|
|
|
|
message_t history[maxMessages]; // 900 positions to save up to 1200 seconds (15 minutes). uInt for each temerature sensor, Input and Setpoint.
|
|
|
|
};
|
|
|
|
|
|
|
|
messages_t messages_history;
|
2020-09-19 19:50:43 +00:00
|
|
|
|
2020-09-13 23:58:36 +00:00
|
|
|
String something = "";
|
|
|
|
String sender = "";
|
|
|
|
|
2020-09-19 01:29:16 +00:00
|
|
|
void handleWebResponse()
|
|
|
|
{
|
2020-09-17 03:15:00 +00:00
|
|
|
if (isWifiAvailable() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-19 01:29:16 +00:00
|
|
|
// We're going to handle the DNS responder here so it
|
|
|
|
// will be ignored by the NRF boards.
|
|
|
|
handleDNSResponse();
|
|
|
|
|
2020-09-13 23:58:36 +00:00
|
|
|
webserver.handleClient();
|
|
|
|
}
|
|
|
|
|
2020-09-19 01:29:16 +00:00
|
|
|
void initWebServer()
|
|
|
|
{
|
2020-09-13 23:58:36 +00:00
|
|
|
webserver.onNotFound(handleNotFound);
|
2020-09-19 18:24:55 +00:00
|
|
|
webserver.on("/json/chat/send/channel", handleJSONChatHistory);
|
|
|
|
webserver.on("/json/chat/send/user", handleJSONChatHistory);
|
|
|
|
webserver.on("/json/chat/history/channel", handleJSONChatHistory);
|
|
|
|
webserver.on("/json/chat/history/user", handleJSONChatHistory);
|
|
|
|
webserver.on("/json/stats", handleJSONChatHistory);
|
2020-09-18 22:33:03 +00:00
|
|
|
webserver.on("/hotspot-detect.html", handleHotspot);
|
2020-09-19 03:42:35 +00:00
|
|
|
webserver.on("/", handleRoot);
|
2020-09-13 23:58:36 +00:00
|
|
|
webserver.begin();
|
|
|
|
}
|
|
|
|
|
2020-09-19 01:29:16 +00:00
|
|
|
void handleJSONChatHistory()
|
|
|
|
{
|
2020-09-19 23:38:59 +00:00
|
|
|
int i;
|
2020-09-13 23:58:36 +00:00
|
|
|
|
|
|
|
String out = "";
|
|
|
|
out += "{\n";
|
|
|
|
out += " \"data\" : {\n";
|
|
|
|
out += " \"chat\" : ";
|
|
|
|
out += "[";
|
|
|
|
out += "\"" + sender + "\"";
|
|
|
|
out += ",";
|
|
|
|
out += "\"" + something + "\"";
|
|
|
|
out += "]\n";
|
|
|
|
|
2020-09-19 23:38:59 +00:00
|
|
|
for (i = 0; i < maxMessages; i++) {
|
|
|
|
out += "[";
|
|
|
|
out += "\"" + String(messages_history.history[i].sender) + "\"";
|
|
|
|
out += ",";
|
|
|
|
out += "\"" + String(messages_history.history[i].message) + "\"";
|
|
|
|
out += "]\n";
|
|
|
|
}
|
|
|
|
|
2020-09-13 23:58:36 +00:00
|
|
|
out += "\n";
|
|
|
|
out += " }\n";
|
|
|
|
out += "}\n";
|
|
|
|
|
2020-09-19 01:29:16 +00:00
|
|
|
webserver.send(200, "application/json", out);
|
2020-09-13 23:58:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-19 01:29:16 +00:00
|
|
|
void handleNotFound()
|
|
|
|
{
|
2020-09-19 18:24:55 +00:00
|
|
|
String message = "";
|
|
|
|
message += "File Not Found\n\n";
|
2020-09-13 23:58:36 +00:00
|
|
|
message += "URI: ";
|
|
|
|
message += webserver.uri();
|
|
|
|
message += "\nMethod: ";
|
|
|
|
message += (webserver.method() == HTTP_GET) ? "GET" : "POST";
|
|
|
|
message += "\nArguments: ";
|
|
|
|
message += webserver.args();
|
|
|
|
message += "\n";
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < webserver.args(); i++) {
|
|
|
|
message += " " + webserver.argName(i) + ": " + webserver.arg(i) + "\n";
|
|
|
|
}
|
2020-09-18 22:33:03 +00:00
|
|
|
Serial.println(message);
|
2020-09-13 23:58:36 +00:00
|
|
|
webserver.send(404, "text/plain", message);
|
|
|
|
}
|
|
|
|
|
2020-09-18 22:33:03 +00:00
|
|
|
/*
|
|
|
|
This supports the Apple Captive Network Assistant (CNA) Portal
|
|
|
|
*/
|
2020-09-19 01:29:16 +00:00
|
|
|
void handleHotspot()
|
|
|
|
{
|
2020-09-18 22:33:03 +00:00
|
|
|
DEBUG_MSG("Hotspot Request\n");
|
2020-09-13 23:58:36 +00:00
|
|
|
|
2020-09-19 03:42:35 +00:00
|
|
|
/*
|
|
|
|
If we don't do a redirect, be sure to return a "Success" message
|
|
|
|
otherwise iOS will have trouble detecting that the connection to the SoftAP worked.
|
|
|
|
*/
|
|
|
|
|
2020-09-18 22:33:03 +00:00
|
|
|
String out = "";
|
2020-09-19 01:29:16 +00:00
|
|
|
// out += "Success\n";
|
2020-09-18 22:33:03 +00:00
|
|
|
out += "<meta http-equiv=\"refresh\" content=\"0;url=http://meshtastic.org/\" />\n";
|
2020-09-19 01:29:16 +00:00
|
|
|
webserver.send(200, "text/html", out);
|
2020-09-18 22:33:03 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-09-13 23:58:36 +00:00
|
|
|
|
2020-09-19 03:42:35 +00:00
|
|
|
void handleRoot()
|
|
|
|
{
|
|
|
|
DEBUG_MSG("Hotspot Request\n");
|
|
|
|
|
|
|
|
/*
|
|
|
|
If we don't do a redirect, be sure to return a "Success" message
|
|
|
|
otherwise iOS will have trouble detecting that the connection to the SoftAP worked.
|
|
|
|
*/
|
|
|
|
|
|
|
|
String out = "";
|
|
|
|
// out += "Success\n";
|
|
|
|
out += "<!doctype html>\n"
|
|
|
|
"<html>\n"
|
|
|
|
"<head>\n"
|
|
|
|
"<meta charset=\"utf-8\">\n"
|
|
|
|
"<title>Meshtastic - WebUI</title>\n"
|
|
|
|
"</head>\n"
|
|
|
|
"\n"
|
|
|
|
"<body>\n"
|
|
|
|
"<center>\n"
|
|
|
|
" <form>\n"
|
|
|
|
" <table width=\"900\" height=\"100%\" border=\"0\">\n"
|
|
|
|
" <tbody>\n"
|
|
|
|
" <tr>\n"
|
|
|
|
" <td align=\"center\">Meshtastic - WebUI (Pre-Alpha)</td>\n"
|
|
|
|
" </tr>\n"
|
|
|
|
" <tr>\n"
|
|
|
|
" <td><table width=\"100%\" height=\"100%\" border=\"1\">\n"
|
|
|
|
" <tbody>\n"
|
|
|
|
" <tr>\n"
|
|
|
|
" <td width=\"50\" height=\"100%\">Channels:<br>\n"
|
|
|
|
" <br>\n"
|
|
|
|
" #general<br>\n"
|
|
|
|
" #stuff<br>\n"
|
|
|
|
" #things<br>\n"
|
|
|
|
" <br></td>\n"
|
|
|
|
" <td height=\"100%\" valign=\"top\"><div>Everything is awesome!</div></td>\n"
|
|
|
|
" </tr>\n"
|
|
|
|
" </tbody>\n"
|
|
|
|
" </table></td>\n"
|
|
|
|
" </tr>\n"
|
|
|
|
" <tr>\n"
|
|
|
|
" <td align=\"center\"><input type=\"text\" size=\"50\">\n"
|
|
|
|
" <input type=\"submit\"></td>\n"
|
|
|
|
" </tr>\n"
|
|
|
|
" </tbody>\n"
|
|
|
|
" </table>\n"
|
|
|
|
" </form>\n"
|
|
|
|
"</center>\n"
|
|
|
|
"</body>\n"
|
|
|
|
"</html>\n";
|
|
|
|
webserver.send(200, "text/html", out);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-19 01:29:16 +00:00
|
|
|
void notifyWebUI()
|
|
|
|
{
|
2020-09-13 23:58:36 +00:00
|
|
|
DEBUG_MSG("************ Got a message! ************\n");
|
|
|
|
MeshPacket &mp = devicestate.rx_text_message;
|
|
|
|
NodeInfo *node = nodeDB.getNode(mp.from);
|
|
|
|
sender = (node && node->has_user) ? node->user.long_name : "???";
|
2020-09-19 01:29:16 +00:00
|
|
|
|
2020-09-13 23:58:36 +00:00
|
|
|
static char tempBuf[256]; // mesh.options says this is MeshPacket.encrypted max_size
|
|
|
|
assert(mp.decoded.which_payload == SubPacket_data_tag);
|
|
|
|
snprintf(tempBuf, sizeof(tempBuf), "%s", mp.decoded.data.payload.bytes);
|
|
|
|
|
|
|
|
something = tempBuf;
|
|
|
|
}
|