Refactoring to break out HTTP from WiFi

This commit is contained in:
Jm Casler 2020-09-13 16:58:36 -07:00
parent d859700497
commit e508306395
7 changed files with 127 additions and 95 deletions

View File

@ -841,6 +841,9 @@ void DebugInfo::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16
// Jm
void DebugInfo::drawFrameWiFi(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y)
{
const char *wifiName = radioConfig.preferences.wifi_ssid;
const char *wifiPsw = radioConfig.preferences.wifi_password;
displayedNodeNum = 0; // Not currently showing a node pane
display->setFont(ArialMT_Plain_10);
@ -856,6 +859,9 @@ void DebugInfo::drawFrameWiFi(OLEDDisplay *display, OLEDDisplayUiState *state, i
display->drawString(x, y + FONT_HEIGHT * 1, WiFi.localIP().toString().c_str());
display->drawString(x, y + FONT_HEIGHT * 2, wifiName);
display->drawString(x, y + FONT_HEIGHT * 3, wifiPsw);
/* Display a heartbeat pixel that blinks every time the frame is redrawn */
#ifdef SHOW_REDRAWS
if (heartbeat)

View File

@ -42,6 +42,7 @@
#include <OneButton.h>
#include <Wire.h>
#include "meshwifi/meshwifi.h"
#include "meshwifi/meshhttp.h"
// #include <driver/rtc_io.h>
#ifndef NO_ESP32
@ -426,6 +427,15 @@ void loop()
// TODO: This should go into a thread handled by FreeRTOS.
handleWebResponse();
#endif
/*
const char *wifiName = radioConfig.preferences.wifi_ssid;
const char *wifiPsw = radioConfig.preferences.wifi_password;
Serial.print("-------------------");
Serial.print(wifiName);
Serial.print(" ");
Serial.println(wifiPsw);
Serial.println("+++++++++++++++++++");
Serial.println("");
*/
delay(msecstosleep);
}

View File

@ -401,10 +401,11 @@ void NodeDB::updateFrom(const MeshPacket &mp)
powerFSM.trigger(EVENT_RECEIVED_TEXT_MSG);
notifyObservers(true); // Force an update whether or not our node counts have changed
// This is going into the wifidev feature branch
// Only update the WebUI if WiFi is enabled
#if WiFi_MODE != 0
notifyWebUI();
#endif
//#if WiFi_MODE != 0
// notifyWebUI();
//#endif
}
}
break;

89
src/meshwifi/meshhttp.cpp Normal file
View File

@ -0,0 +1,89 @@
#include <WebServer.h>
#include <WiFi.h>
#include "configuration.h"
#include "main.h"
#include "NodeDB.h"
#include "meshwifi.h"
#include "meshhttp.h"
WebServer webserver(80);
String something = "";
String sender = "";
void handleWebResponse() {
webserver.handleClient();
}
void initWebServer() {
webserver.onNotFound(handleNotFound);
//webserver.on("/", handleJSONChatHistory);
//webserver.on("/json/chat/history", handleJSONChatHistory);
webserver.on("/", []() {
webserver.send(200, "text/plain", "Everything is awesome!");
});
webserver.begin();
}
void handleJSONChatHistory() {
String out = "";
out += "{\n";
out += " \"data\" : {\n";
out += " \"chat\" : ";
out += "[";
out += "\"" + sender + "\"";
out += ",";
out += "\"" + something + "\"";
out += "]\n";
out += "\n";
out += " }\n";
out += "}\n";
webserver.send ( 200, "application/json", out );
return;
}
void handleNotFound() {
String message = "File Not Found\n\n";
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";
}
webserver.send(404, "text/plain", message);
/*
*/
}
void notifyWebUI() {
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 : "???";
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;
}

16
src/meshwifi/meshhttp.h Normal file
View File

@ -0,0 +1,16 @@
#pragma once
#include <Arduino.h>
#include <functional>
#include <WiFi.h>
void initWebServer();
void handleNotFound();
void handleWebResponse();
void handleJSONChatHistory();
void notifyWebUI();

View File

@ -3,23 +3,7 @@
#include "configuration.h"
#include "main.h"
#include "NodeDB.h"
#include <WebServer.h>
WebServer webserver(80);
String something = "";
String sender = "";
void initWebServer() {
webserver.onNotFound(handleNotFound);
webserver.on("/", handleJSONChatHistory);
webserver.on("/json/chat/history", handleJSONChatHistory);
//webserver.on("/", []() {
//webserver.send(200, "text/plain", "everything is awesome!");
//});
webserver.begin();
}
#include "meshwifi/meshhttp.h"
void initWifi()
{
@ -139,54 +123,6 @@ void WiFiEvent(WiFiEvent_t event)
}
}
void handleJSONChatHistory() {
String out = "";
out += "{\n";
out += " \"data\" : {\n";
out += " \"chat\" : ";
out += "[";
out += "\"" + sender + "\"";
out += ",";
out += "\"" + something + "\"";
out += "]\n";
out += "\n";
out += " }\n";
out += "}\n";
webserver.send ( 200, "application/json", out );
return;
}
void handleWebResponse() {
webserver.handleClient();
}
void handleNotFound() {
String message = "File Not Found\n\n";
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";
}
webserver.send(404, "text/plain", message);
/*
*/
}
void reconnectWiFi() {
if ( WiFi.status() != WL_CONNECTED ) {
DEBUG_MSG("... Reconnecting to WiFi access point");
@ -194,19 +130,3 @@ void reconnectWiFi() {
}
}
void notifyWebUI() {
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 : "???";
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;
}

View File

@ -4,19 +4,9 @@
#include <functional>
#include <WiFi.h>
void handleNotFound();
void reconnectWiFi();
void initWifi();
void initWebServer();
void handleWebResponse();
void notifyWebUI();
void handleJSONChatHistory();
void WiFiEvent(WiFiEvent_t event);