Skip to content

Commit

Permalink
Fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Nov 29, 2023
1 parent 0f90f88 commit 87cdfa4
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 26 deletions.
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
],
"version": "4.0.2",
"frameworks": "arduino",
"platforms": "espressif",
"platforms": ["espressif32", "espressif8266"],
"dependencies":
[
{
Expand Down
28 changes: 13 additions & 15 deletions src/ESPDash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,24 @@ struct ChartNames chartTags[] = {
/*
Constructor
*/
ESPDash::ESPDash(AsyncWebServer* server, bool enable_default_stats) {
ESPDash::ESPDash(AsyncWebServer* server, bool enable_default_stats, const char *location) {
_server = server;
default_stats_enabled = enable_default_stats;

// Initialize AsyncWebSocket
_ws = new AsyncWebSocket("/dashws");

// Attach AsyncWebServer Routes
_server->on("/", HTTP_GET, [this](AsyncWebServerRequest *request){
_server->on(location, HTTP_GET, [this](AsyncWebServerRequest *request){
if(basic_auth){
if(!request->authenticate(username, password))
if(!request->authenticate(username.c_str(), password.c_str()))
return request->requestAuthentication();
}
// respond with the compressed frontend
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", DASH_HTML, sizeof(DASH_HTML));
response->addHeader("Content-Encoding","gzip");
request->send(response);
response->addHeader("Cache-Control","public, max-age=900");
request->send(response);
});

// Websocket Callback Handler
Expand Down Expand Up @@ -94,11 +95,11 @@ ESPDash::ESPDash(AsyncWebServer* server, bool enable_default_stats) {
}


void ESPDash::setAuthentication(const char *user, const char *pass) {
void ESPDash::setAuthentication(const String &user, const String &pass) {
username = user;
password = pass;
basic_auth = true;
_ws->setAuthentication(user, pass);
_ws->setAuthentication(username.c_str(), password.c_str());
}


Expand Down Expand Up @@ -167,7 +168,7 @@ size_t ESPDash::generateLayoutJSON(AsyncWebSocketClient *client, bool changes_on
} else {
buf += "{\"command\":\"update:layout\",";
}

buf += "\"cards\":[";

StaticJsonDocument<DASH_CARD_JSON_SIZE> carddoc;
Expand Down Expand Up @@ -273,20 +274,17 @@ size_t ESPDash::generateLayoutJSON(AsyncWebSocketClient *client, bool changes_on

// Loop through user defined stats
StaticJsonDocument<128> obj;
bool prevStatWritten = default_stats_enabled;
for (int i=0; i < statistics.Size(); i++) {
Statistic *s = statistics[i];
if (changes_only) {
if (s->_changed) {
s->_changed = false;
} else {
continue;
}
if (prevStatWritten) {
buf += ",";
}
buf += ",";
obj["k"] = s->_key;
obj["v"] = s->_value;
serializeJson(obj, buf);
obj.clear();
prevStatWritten = true;
}

buf += "]";
Expand Down Expand Up @@ -399,7 +397,7 @@ void ESPDash::generateComponentJSON(JsonObject& doc, Chart* chart, bool change_o
// blank value
break;
}

JsonArray yAxis = doc["y_axis"].to<JsonArray>();
switch (chart->_y_axis_type) {
case GraphAxisType::INTEGER:
Expand Down
18 changes: 9 additions & 9 deletions src/ESPDash.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
____ ____ ___ ___ ____ ____ _ _
|___ [__ |__] | \ |__| [__ |__|
|___ ___] | |__/ | | ___] | |
____ ____ ___ ___ ____ ____ _ _
|___ [__ |__] | \ |__| [__ |__|
|___ ___] | |__/ | | ___] | |
ESP-DASH V3
---------------------
Author: Ayush Sharma
Expand Down Expand Up @@ -76,8 +76,8 @@ class ESPDash{
Vector<Statistic*> statistics;
bool default_stats_enabled = false;
bool basic_auth = false;
const char *username;
const char *password;
String username;
String password;

// Generate layout json
size_t generateLayoutJSON(AsyncWebSocketClient *client, bool changes_only = false);
Expand All @@ -91,10 +91,10 @@ class ESPDash{

public:

ESPDash(AsyncWebServer* server, bool enable_default_stats = true);
ESPDash(AsyncWebServer* server, bool enable_default_stats = true, const char *location = "/");

// Set Authentication
void setAuthentication(const char *user, const char *pass);
void setAuthentication(const String &user, const String &pass);

// Add Card
void add(Card *card);
Expand All @@ -115,7 +115,7 @@ class ESPDash{
void sendUpdates(bool force = false);

void refreshStatistics();

~ESPDash();
};

Expand Down
6 changes: 6 additions & 0 deletions src/Statistic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ void Statistic::set(const char *key, const char *value) {
_changed = true;
}

void Statistic::set(const char *value) {
// Safe copy
strncpy(_value, value, sizeof(_value));
_changed = true;
}

Statistic::~Statistic() {
_dashboard->remove(this);
}
3 changes: 2 additions & 1 deletion src/Statistic.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ class Statistic {
bool _changed = false;

public:
Statistic(ESPDash *dashboard, const char *key, const char *value);
Statistic(ESPDash *dashboard, const char *key, const char *value = "");
void set(const char *key, const char *value);
void set(const char *value);
~Statistic();

friend class ESPDash;
Expand Down

0 comments on commit 87cdfa4

Please sign in to comment.