Skip to content

Commit

Permalink
Fix missing WS debug messages (#1851)
Browse files Browse the repository at this point in the history
Amend #1843 , since we have updated ESPAsyncWebServer
Fixes (again) #1300

Gather WS debug messages in a buffer and flush every N times
info,keys,crash actually output data now
  • Loading branch information
mcspr authored Aug 12, 2019
1 parent 2142343 commit d4dea17
Show file tree
Hide file tree
Showing 18 changed files with 4,090 additions and 4,028 deletions.
Binary file modified code/espurna/data/index.all.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.light.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.lightfox.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.rfbridge.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.rfm69.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.sensor.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.small.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.thermostat.html.gz
Binary file not shown.
831 changes: 416 additions & 415 deletions code/espurna/static/index.all.html.gz.h

Large diffs are not rendered by default.

847 changes: 424 additions & 423 deletions code/espurna/static/index.light.html.gz.h

Large diffs are not rendered by default.

161 changes: 81 additions & 80 deletions code/espurna/static/index.lightfox.html.gz.h

Large diffs are not rendered by default.

175 changes: 88 additions & 87 deletions code/espurna/static/index.rfbridge.html.gz.h

Large diffs are not rendered by default.

3,047 changes: 1,524 additions & 1,523 deletions code/espurna/static/index.rfm69.html.gz.h

Large diffs are not rendered by default.

184 changes: 93 additions & 91 deletions code/espurna/static/index.sensor.html.gz.h

Large diffs are not rendered by default.

161 changes: 81 additions & 80 deletions code/espurna/static/index.small.html.gz.h

Large diffs are not rendered by default.

2,619 changes: 1,310 additions & 1,309 deletions code/espurna/static/index.thermostat.html.gz.h

Large diffs are not rendered by default.

82 changes: 65 additions & 17 deletions code/espurna/ws.ino
Original file line number Diff line number Diff line change
Expand Up @@ -162,30 +162,75 @@ bool _wsAuth(AsyncWebSocketClient * client) {

#if DEBUG_WEB_SUPPORT

bool wsDebugSend(const char* prefix, const char* message) {
if (!wsConnected()) return false;
struct ws_debug_msg_t {
ws_debug_msg_t(const char* prefix, const char* message) :
prefix(prefix), message(message)
{}
String prefix;
String message;
};

// via: https://arduinojson.org/v6/assistant/
// we use 1 object for "weblog", 2nd one for "message". "prefix", optional
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(2)> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
JsonObject& weblog = root.createNestedObject("weblog");
struct ws_debug_t {

weblog["message"] = message;
if (prefix && (prefix[0] != '\0')) {
weblog["prefix"] = prefix;
ws_debug_t(size_t capacity) :
flush(false),
current(0),
capacity(capacity)
{
messages.reserve(capacity);
}

// TODO: avoid serializing twice and just measure json ourselves?
//const size_t len = strlen(message) + strlen(prefix)
// + strlen("{\"weblog\":}")
// + strlen("{\"message\":\"\"}")
// + (strlen(prefix) ? strlen("\",\"prefix\":\"\"") : 0);
//wsSend(root, len);
wsSend(root);
void add(const char* prefix, const char* message) {
if (current >= capacity) {
flush = true;
send();
}

messages.emplace(messages.begin() + current, prefix, message);
flush = true;
++current;
}

void send() {
if (!flush) return;
// ref: http://arduinojson.org/v5/assistant/
// {"weblog": {"msg":[...],"pre":[...]}}
DynamicJsonBuffer jsonBuffer(2*JSON_ARRAY_SIZE(messages.size()) + JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(2));

JsonObject& root = jsonBuffer.createObject();
JsonObject& weblog = root.createNestedObject("weblog");

JsonArray& msg = weblog.createNestedArray("msg");
JsonArray& pre = weblog.createNestedArray("pre");

for (auto& message : messages) {
pre.add(message.prefix.c_str());
msg.add(message.message.c_str());
}

wsSend(root);
messages.clear();
current = 0;
flush = false;
}

bool flush;
size_t current;
const size_t capacity;
std::vector<ws_debug_msg_t> messages;

};

// TODO: move to the headers?
constexpr const size_t WS_DEBUG_MSG_BUFFER = 8;
ws_debug_t _ws_debug(WS_DEBUG_MSG_BUFFER);

bool wsDebugSend(const char* prefix, const char* message) {
if (!wsConnected()) return false;
_ws_debug.add(prefix, message);
return true;
}

#endif

// Check the existing setting before saving it
Expand Down Expand Up @@ -599,6 +644,9 @@ void _wsLoop() {
if (!wsConnected()) return;
_wsDoUpdate();
_wsHandleClientData();
#if DEBUG_WEB_SUPPORT
_ws_debug.send();
#endif
}

// -----------------------------------------------------------------------------
Expand Down
11 changes: 8 additions & 3 deletions code/html/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -1642,10 +1642,15 @@ function processData(data) {
if ("weblog" === key) {
websock.send("{}");

if (value.prefix) {
$("#weblog").append(new Text(value.prefix));
msg = value["msg"];
pre = value["pre"];

for (var i=0; i < msg.length; ++i) {
if (pre[i]) {
$("#weblog").append(new Text(pre[i]));
}
$("#weblog").append(new Text(msg[i]));
}
$("#weblog").append(new Text(value.message));

$("#weblog").scrollTop($("#weblog")[0].scrollHeight - $("#weblog").height());
return;
Expand Down

0 comments on commit d4dea17

Please sign in to comment.