Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

/rfid endpoint - Serve with chunked response #280

Merged
merged 8 commits into from
Dec 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
104 changes: 82 additions & 22 deletions src/Web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ void webserverStart(void) {

// RFID
wServer.on("/rfid", HTTP_GET, handleGetRFIDRequest);
wServer.addRewrite(new OneParamRewrite("/rfid/ids-only", "/rfid?ids-only=true"));
wServer.addHandler(new AsyncCallbackJsonWebHandler("/rfid", handlePostRFIDRequest));
wServer.addRewrite(new OneParamRewrite("/rfid/{id}", "/rfid?id={id}"));
wServer.on("/rfid", HTTP_DELETE, handleDeleteRFIDRequest);
Expand Down Expand Up @@ -1685,34 +1686,93 @@ bool DumpNvsToJSONCallback(const char *key, void *data) {
return tagIdToJSON(key, obj);
}

// callback for writing a NVS entry to list
bool DumpNvsToArrayCallback(const char *key, void *data) {
std::vector<String> *keys = (std::vector<String> *) data;
keys->push_back(key);
return true;
}

static String tagIdToJsonStr(const char *key, const bool nameOnly) {
if (nameOnly) {
return "\"" + String(key) + "\"";
} else {
StaticJsonDocument<512> doc;
JsonObject entry = doc.createNestedObject(key);
if (!tagIdToJSON(key, entry)) {
return "";
}
String serializedJsonString;
serializeJson(entry, serializedJsonString);
return serializedJsonString;
}
}

// Handles rfid-assignments requests (GET)
// /rfid returns an array of tag-ids and details. Optional GET param "id" to list only a single assignment.
// /rfid/ids-only returns an array of tag-id keys
static void handleGetRFIDRequest(AsyncWebServerRequest *request) {

String tagId = "";

if (request->hasParam("id")) {
tagId = request->getParam("id")->value();
}
if (tagId == "") {
// return all RFID assignments
AsyncJsonResponse *response = new AsyncJsonResponse(true, 8192);
JsonArray Arr = response->getRoot();
if (listNVSKeys("rfidTags", &Arr, DumpNvsToJSONCallback)) {
response->setLength();
request->send(response);
} else {
request->send(500, "error reading entries from NVS");
}
} else {
if (gPrefsRfid.isKey(tagId.c_str())) {
// return single RFID assignment
AsyncJsonResponse *response = new AsyncJsonResponse(false);
JsonObject obj = response->getRoot();
tagIdToJSON(tagId, obj);
response->setLength();
request->send(response);
} else {
// RFID assignment not found
request->send(404);
}

if ((tagId != "") && gPrefsRfid.isKey(tagId.c_str())) {
// return single RFID entry
String json = tagIdToJsonStr(tagId.c_str(), true);
request->send(200, "application/json", json);
return;
}
// get tag details or just an array of id's
bool idsOnly = request->hasParam("ids-only");

std::vector<String> nvsKeys {};
static size_t nvsIndex;
nvsKeys.clear();
// Dumps all RFID-keys from NVS into key array
listNVSKeys("rfidTags", &nvsKeys, DumpNvsToArrayCallback);
if (nvsKeys.size() == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would not need this special handling for size == 0 if in the chunked response there was no special handling for the first tag.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Writing the first tag there is no comma in JSON, it is starting with the second entry.
That's is the reason to handle the special case size==0 here and not even start a chunked response.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not starting the chunked response in the first place is probably a good idea.

// no entries
request->send(200, "application/json", "[]");
return;
}
// construct chunked repsonse
nvsIndex = 0;
AsyncWebServerResponse *response = request->beginChunkedResponse("application/json",
[nvsKeys = std::move(nvsKeys), idsOnly](uint8_t *buffer, size_t maxLen, size_t index) -> size_t {
maxLen = maxLen >> 1; // some sort of bug with actual size available, reduce the len
size_t len = 0;
String json;

if (nvsIndex == 0) {
// start, write first tag
json = tagIdToJsonStr(nvsKeys[nvsIndex].c_str(), idsOnly);
if (json.length() >= maxLen) {
Log_Println("/rfid: Buffer too small", LOGLEVEL_ERROR);
return len;
}
len += snprintf(((char *) buffer), maxLen - len, "[%s", json.c_str());
nvsIndex++;
}
while (nvsIndex < nvsKeys.size()) {
// write tags as long we have enough room
json = tagIdToJsonStr(nvsKeys[nvsIndex].c_str(), idsOnly);
if ((len + json.length()) >= maxLen) {
break;
}
len += snprintf(((char *) buffer + len), maxLen - len, ",%s", json.c_str());
nvsIndex++;
}
if (nvsIndex == nvsKeys.size()) {
// finish
len += snprintf(((char *) buffer + len), maxLen - len, "]");
nvsIndex++;
}
return len;
});
request->send(response);
}

static void handlePostRFIDRequest(AsyncWebServerRequest *request, JsonVariant &json) {
Expand Down