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

Esp form uiupdate #36

Merged
merged 29 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions lib/groundlight/src/groundlight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,20 +367,26 @@ String get_query_id(const String &jsonResults) {

StaticJsonDocument<DET_DOC_SIZE> groundlight_json_doc;

// Parses the detectors from the Groundlight API.
detector_list get_detector_list(const char *endpoint, const char *apiToken) {
deserializeJson(groundlight_json_doc, get_detectors(endpoint, apiToken));
JsonArray detectors = groundlight_json_doc["results"];
detector *_detector_list = new detector[detectors.size()];
for (int i = 0; i < detectors.size(); i++)
{
_detector_list[i].confidence_threshold = detectors[i]["confidence_threshold"];
strcpy(_detector_list[i].id, detectors[i]["id"]);
strcpy(_detector_list[i].type, detectors[i]["type"]);
strcpy(_detector_list[i].created_at, detectors[i]["created_at"]);
strcpy(_detector_list[i].name, detectors[i]["name"]);
strcpy(_detector_list[i].query, detectors[i]["query"]);
strcpy(_detector_list[i].group_name, detectors[i]["group_name"]);
strlcpy(_detector_list[i].id, detectors[i]["id"], sizeof(_detector_list[i].id));
strlcpy(_detector_list[i].type, detectors[i]["type"], sizeof(_detector_list[i].type));
strlcpy(_detector_list[i].created_at, detectors[i]["created_at"], sizeof((_detector_list[i].created_at)));
strlcpy(_detector_list[i].name, detectors[i]["name"], sizeof(_detector_list[i].name));
strlcpy(_detector_list[i].query, detectors[i]["query"], sizeof(_detector_list[i].query));
strlcpy(_detector_list[i].group_name, detectors[i]["group_name"], sizeof(_detector_list[i].group_name));
if (!detectors[i]["metadata"].isNull()) {
String metadataStr;
serializeJson(detectors[i]["metadata"], metadataStr);
strlcpy(_detector_list[i].metadata, metadataStr.c_str(), sizeof(_detector_list[i].metadata));
} else {
_detector_list[i].metadata[0] = '\0';
}
Copy link
Contributor

Choose a reason for hiding this comment

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

An awesome bonus here would be to check the length of the string before you copy it into the char array. We've had memory overflow problems before on, for example, detectors with ridiculously long queries.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought if I use strlcpy to copy the string, it specifies the size of string so that overflow won't happen?

Copy link
Contributor

Choose a reason for hiding this comment

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

Admittedly not strictly a part of your PR, but just above here are some unsafe strcpy calls

}
detector_list res = { _detector_list, detectors.size() };
return res;
Expand All @@ -401,16 +407,38 @@ String detector_to_string(detector d) {
res += d.group_name;
res += "\n\tConfidence threshold: ";
res += d.confidence_threshold;
res += "\n\tMetadata: ";
res += d.metadata;
return res;
}

detector get_detector_by_id(const char *endpoint, const char *detectorId, const char *apiToken) {
detector_list detectors = get_detector_list(endpoint, apiToken);
detector det = { "NONE", "NONE", "NONE", "NONE", "NONE", "NONE", 0.0, "None" };
for (int i = 0; i < detectors.size; i++) {
if (String(detectors.detectors[i].id) == String(detectorId)) {
return detectors.detectors[i];
det = detectors.detectors[i];
Copy link
Contributor

Choose a reason for hiding this comment

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

can probably stop iterating ?

break;
}
}
if (0<detectors.size){
delete[] detectors.detectors;
}
return det;
}

detector get_detector_by_name(const char *endpoint, const char *detectorName, const char *apiToken) {
detector_list detectors = get_detector_list(endpoint, apiToken);
detector det = { "NONE", "NONE", "NONE", "NONE", "NONE", "NONE", 0.0, "None" };
for (int i = 0; i < detectors.size; i++) {
if (String(detectors.detectors[i].name) == String(detectorName)) {
det = detectors.detectors[i];
}
}
return detector { "NONE", "NONE", "NONE", "NONE", "NONE", "NONE", 0.0 };
if (0<detectors.size){
delete[] detectors.detectors;
}

return det;
}
#endif
4 changes: 3 additions & 1 deletion lib/groundlight/src/groundlight.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ struct detector
char query[200];
char group_name[40];
float confidence_threshold;
};
char metadata[1024];
};

struct detector_list
{
Expand All @@ -46,6 +47,7 @@ struct detector_list
detector_list get_detector_list(const char *endpoint, const char *apiToken);
String detector_to_string(detector d);
detector get_detector_by_id(const char *endpoint, const char *detectorId, const char *apiToken);
detector get_detector_by_name(const char *endpoint, const char *detectorName, const char *apiToken);
float get_query_confidence(const String &jsonResults);
String get_query_id(const String &jsonResults);
#endif
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ build_flags =
'-D CAMERA_MODEL_M5STACK_PSRAM'
'-D NAME="GROUNDLIGHT_DEMO_UNIT"'
'-D ENABLE_AP'
# '-D ENABLE_STACKLIGHT'
Copy link
Contributor

Choose a reason for hiding this comment

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

Do the demo units no longer work with Stacklight?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes we are skipping stacklight connection for now due to connection issues on my end, and once I confirmed the stacklight is functioning, I will enable it again.

lib_deps =
${env.lib_deps}
https://github.com/me-no-dev/ESPAsyncWebServer.git#master
Expand Down
Loading
Loading