From faff733ca111b95615e921457cf22e5faa963c05 Mon Sep 17 00:00:00 2001 From: Supersilvia Date: Mon, 4 Mar 2024 16:38:56 -0800 Subject: [PATCH] more cleanup --- lib/groundlight/src/groundlight.cpp | 35 +++++++++++++---------------- platformio.ini | 2 +- src/deployable_example.cpp | 33 +++------------------------ 3 files changed, 19 insertions(+), 51 deletions(-) diff --git a/lib/groundlight/src/groundlight.cpp b/lib/groundlight/src/groundlight.cpp index 818df86..2786c66 100644 --- a/lib/groundlight/src/groundlight.cpp +++ b/lib/groundlight/src/groundlight.cpp @@ -355,11 +355,9 @@ 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"]; @@ -367,38 +365,32 @@ String get_query_id(const String &jsonResults) { StaticJsonDocument 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() }; @@ -420,22 +412,26 @@ 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 (0send_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()); @@ -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()); }