Skip to content

Commit

Permalink
Feat/dashboard (#3)
Browse files Browse the repository at this point in the history
* add dashboard

* change data handling

* use root scope

* tunneling frontend from external website

* remove unused code
  • Loading branch information
coding-lemur authored Aug 14, 2022
1 parent a10339e commit 5c48b09
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 2 deletions.
88 changes: 88 additions & 0 deletions lib/tunnel/ESPAsyncTunnel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include "ESPAsyncTunnel.h"
#include "mdns.h"

/**
* ClientRequestTunnel
*/
ClientRequestTunnel::ClientRequestTunnel() {
ESP_LOGD("[ClientRequestTunnel]","Constructor!");
}

bool ClientRequestTunnel::open(String target, String url) {
open(getForwardURL(target, url));
}

bool ClientRequestTunnel::open(String url) {
ESP_LOGD("[ClientRequestTunnel]","open");
this->url = url;
this->http = new HTTPClient();
if (!http->begin(url)){
ESP_LOGE("[ClientRequestTunnel]","Unable to connect!");
ok = false;
return ok;
}
ESP_LOGI("[ClientRequestTunnel]","Connected!");
this->http->collectHeaders(headerkeays, 1);
this->httpCode = http->GET();
if(this->httpCode == HTTP_CODE_OK || this->httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
ok = true;
ESP_LOGI("[ClientRequestTunnel]","HttpCode -> OK");
} else {
ok = false;
ESP_LOGE("[ClientRequestTunnel]","GET... failed, error: %s\n", http->errorToString(httpCode).c_str());
}
return ok;
}

ClientRequestTunnel::~ClientRequestTunnel() {
ESP_LOGD("[ClientRequestTunnel]","Destructor!");
if (http){
http->end();
delete(http);
}
}

bool ClientRequestTunnel::isOK() {
return this->ok;
}

int ClientRequestTunnel::getHttpCode() {
return this->httpCode;
}

String ClientRequestTunnel::getString() {
return http ? http->getString() : "";
}

Stream* ClientRequestTunnel::getStream() {
return (http && http->connected()) ? http->getStreamPtr() : NULL;
}

String ClientRequestTunnel::getContentType() {
return http ? http->header(headerkeays[0]) : "";
}

size_t ClientRequestTunnel::getSize() {
return http ? http->getSize() : 0 ;
}

HTTPClient* ClientRequestTunnel::getHTTPClient() {
return http;
}

String ClientRequestTunnel::getForwardURL(String target_address, String url) {
String forward_path;
if (target_address.isEmpty()){
forward_path = url;
} else if (url.indexOf("http",0)==0) {
// does it start with http ?
int pos = url.indexOf("/",9);
String requestd_path = url.substring(pos);
forward_path = target_address + requestd_path;
} else {
forward_path = target_address + url;
}

ESP_LOGI("[ClientRequestTunnel]","%s -> %s\n", url.c_str(),forward_path.c_str());
return forward_path;
}
36 changes: 36 additions & 0 deletions lib/tunnel/ESPAsyncTunnel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <Arduino.h>
#include "HTTPClient.h"
#include "ESPAsyncWebServer.h"

#ifndef _ESPAsyncTunnel_H_
#define _ESPAsyncTunnel_H_


/**
* Http Client which returns the data from a remove Server
*/
class ClientRequestTunnel {
public:
ClientRequestTunnel();
bool open(String targetUrl, String url);
bool open(String url);
~ClientRequestTunnel();
String getString();
Stream* getStream();
String getContentType();
size_t getSize();
HTTPClient* getHTTPClient();
bool isOK();
int getHttpCode();
String getForwardURL(String target, String url);

private:
bool ok = false;
HTTPClient *http;
String url;
const char* headerkeays[1] = {"Content-Type"};
int httpCode;
};


#endif
2 changes: 2 additions & 0 deletions lib/tunnel/source.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
thanks to
[Phil Schatzmann](https://github.com/pschatzmann/esp32_vue_example)
26 changes: 24 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>
#include <ESPAsyncTunnel.h>

#include "config.h"

Expand All @@ -32,6 +33,9 @@ const char *ntpServer = "pool.ntp.org"; // TODO move to config
const long gmtOffset_sec = 3600; // TODO move to config
const int daylightOffset_sec = 3600; // TODO move to config

const char *externalBaseUrl = "https://coding-lemur.github.io";
const char *indexPath = "/bed-room-clock-dashboard/index.html";

bool isPortalActive = false;
float lastTemperature = -100;
float lastHumidity = -100;
Expand Down Expand Up @@ -117,8 +121,26 @@ StaticJsonDocument<384> getInfoJson()

void setupWebserver()
{
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
{ request->send(200, "text/plain", "Hi! This is a sample response."); });
// rewrites
server.rewrite("/", indexPath);
server.rewrite("/index.html", indexPath);
server.rewrite("/favicon.ico", "/bed-room-clock-dashboard/favicon.ico");

// tunnel the index.html request
server.on(indexPath, HTTP_GET, [&](AsyncWebServerRequest *request)
{
ClientRequestTunnel tunnel;
if (tunnel.open(externalBaseUrl, request->url())) {
String result = tunnel.getString();
request->send(200, "text/html", result);
} else {
request->send(tunnel.getHttpCode());
} });

server.on("/bed-room-clock-dashboard/*", HTTP_GET, [&](AsyncWebServerRequest *request)
{
String moved_url = externalBaseUrl+request->url();
request->redirect(moved_url); });

server.on("/api/info", HTTP_GET, [](AsyncWebServerRequest *request)
{
Expand Down

0 comments on commit 5c48b09

Please sign in to comment.