Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Supersilvia authored and Supersilvia committed Mar 5, 2024
1 parent f955b19 commit faff733
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 51 deletions.
35 changes: 15 additions & 20 deletions lib/groundlight/src/groundlight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,50 +355,42 @@ String get_query_id(const String &jsonResults) {
DynamicJsonDocument results(1024);
ArduinoJson::DeserializationError deserializeError = deserializeJson(results, jsonResults);
if (deserializeError != ArduinoJson::DeserializationError::Ok) {
// Serial.println("Failed to parse JSON");
return "NONE";
}
if (!results.containsKey("id")) {
// Serial.println("No query ID found in JSON");
return "NONE";
}
return results["id"];
}

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) {
String jsonResponse = get_detectors(endpoint, apiToken);
// deserializeJson(groundlight_json_doc, get_detectors(endpoint, apiToken));
//debug parsing:
auto error = deserializeJson(groundlight_json_doc, jsonResponse);
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return {}; // Return an empty detector_list if parsing fails
return {};
}
//if it is not empty:
JsonArray detectors = groundlight_json_doc["results"]; //json -> array
JsonArray detectors = groundlight_json_doc["results"];
detector *_detector_list = new detector[detectors.size()];
for (int i = 0; i < detectors.size(); i++)
{
JsonObject detectorObj = detectors[i]; //detector

_detector_list[i].confidence_threshold = detectors[i]["confidence_threshold"]; //float or str?
JsonObject detectorObj = detectors[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"]);
// serialize and copy metadata
if (!detectorObj["metadata"].isNull()) {
String metadataStr;
serializeJson(detectorObj["metadata"], metadataStr);
strlcpy(_detector_list[i].metadata, metadataStr.c_str(), sizeof(_detector_list[i].metadata));
} else {
_detector_list[i].metadata[0] = '\0'; //set to an empty string if metadata is null.
_detector_list[i].metadata[0] = '\0';
}
}
detector_list res = { _detector_list, detectors.size() };
Expand All @@ -420,30 +412,33 @@ String detector_to_string(detector d) {
res += d.group_name;
res += "\n\tConfidence threshold: ";
res += d.confidence_threshold;
res += "\n\tMetadata: "; //added metadata
res += "\n\tMetadata: ";
res += d.metadata;
return res;
}

detector get_detector_by_id(const char *endpoint, const char *detector_id, const char *apiToken) {
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(detector_id)) {
return detectors.detectors[i];
if (String(detectors.detectors[i].id) == String(detectorId)) {
det = detectors.detectors[i];
}
}
return detector { "NONE", "NONE", "NONE", "NONE", "NONE", "NONE", 0.0, "None" };
if (0<detectors.size){
delete[] detectors.detectors;
}
return det;
}

detector get_detector_by_name(const char *endpoint, const char *detectorName, const char *apiToken) { //given input: detectorName
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, "None" };
if (0<detectors.size){
delete[] detectors.detectors;
}
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ build_flags =
'-D CAMERA_MODEL_M5STACK_PSRAM'
'-D NAME="GROUNDLIGHT_DEMO_UNIT"'
'-D ENABLE_AP'
#'-D ENABLE_STACKLIGHT'
# '-D ENABLE_STACKLIGHT'
lib_deps =
${env.lib_deps}
https://github.com/me-no-dev/ESPAsyncWebServer.git#master
Expand Down
33 changes: 3 additions & 30 deletions src/deployable_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,15 +522,15 @@ void setup() {
// Send web page with input fields to client
// at http://192.168.4.1/
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html, processor);
request->send_P(200, "text/html", index_html, processor);
});

server.on("/config", HTTP_GET, [] (AsyncWebServerRequest *request) {
preferences.begin("config", false);
preferences.begin("config", false);
if (request->hasParam("ssid") && request->getParam("ssid")->value() != "") {
preferences.putString("ssid", request->getParam("ssid")->value());
strcpy(ssid, request->getParam("ssid")->value().c_str());
}
}
if (request->hasParam("pw") && request->getParam("pw")->value() != "") {
preferences.putString("password", request->getParam("pw")->value());
strcpy(password, request->getParam("pw")->value().c_str());
Expand Down Expand Up @@ -568,33 +568,6 @@ void setup() {
if (request->hasParam("sl_uuid") && request->getParam("sl_uuid")->value() != "") {
preferences.putString("sl_uuid", request->getParam("sl_uuid")->value());
}
if (request->hasParam("slack_url") && request->getParam("slack_url")->value() != "") {
preferences.putString("slack_url", request->getParam("slack_url")->value());
}
if (request->hasParam("email") && request->getParam("email")->value() != "") {
preferences.putString("email", request->getParam("email")->value());
}
if (request->hasParam("email_endpoint") && request->getParam("email_endpoint")->value() != "") {
preferences.putString("emailEndpoint", request->getParam("email_endpoint")->value());
}
if (request->hasParam("email_key") && request->getParam("email_key")->value() != "") {
preferences.putString("emailKey", request->getParam("email_key")->value());
}
if (request->hasParam("email_host") && request->getParam("email_host")->value() != "") {
preferences.putString("emailHost", request->getParam("email_host")->value());
}
if (request->hasParam("twilio_sid") && request->getParam("twilio_sid")->value() != "") {
preferences.putString("twilioSID", request->getParam("twilio_sid")->value());
}
if (request->hasParam("twilio_token") && request->getParam("twilio_token")->value() != "") {
preferences.putString("twilioKey", request->getParam("twilio_token")->value());
}
if (request->hasParam("twilio_number") && request->getParam("twilio_number")->value() != "") {
preferences.putString("twilioNumber", request->getParam("twilio_number")->value());
}
if (request->hasParam("twilio_recipient") && request->getParam("twilio_recipient")->value() != "") {
preferences.putString("twilioEndpoint", request->getParam("twilio_recipient")->value());
}
if (request->hasParam("autoconfig") && request->getParam("autoconfig")->value() != "") {
preferences.putString("autoconfig", request->getParam("autoconfig")->value());
}
Expand Down

0 comments on commit faff733

Please sign in to comment.