diff --git a/src/TeaNetworking.cpp b/src/TeaNetworking.cpp index 09c45a0..ba8daee 100644 --- a/src/TeaNetworking.cpp +++ b/src/TeaNetworking.cpp @@ -126,19 +126,72 @@ JSONVar TeaNetworking::httpsPOSTRequest(String serverName, String data) return JSON.parse(payload); } -String TeaNetworking::bundleNewTeaInfo(String rfidCode, int steepigSeconds) { +JSONVar TeaNetworking::httpsPUTRequest(String serverName, String data) +{ + WiFiClientSecure client; + String payload = "{}"; + + // wait for WiFi connection + if ((WiFi.status() == WL_CONNECTED)) + { + // Create a list of certificates with the server certificate + X509List cert(IRG_Root_X1); + client.setTrustAnchors(&cert); + HTTPClient http; + Serial.print("[HTTPS] begin...\n"); + // http.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS); + if (http.begin(client, serverName)) + { // HTTPS + // Send HTTPS PUT request + http.addHeader("Content-Type", "application/json"); + int httpCode = http.PUT(data); + // httpCode will be negative on error + if (httpCode > 0) + { + // HTTP header has been send and Server response header has been handled + Serial.printf("[HTTPS] PUT... code: %d\n", httpCode); + payload = http.getString(); + Serial.println(payload); + } + else + { + Serial.printf("[HTTPS] PUT... failed, error: %s\n", http.errorToString(httpCode).c_str()); + } + http.end(); + } + else + { + Serial.printf("[HTTPS] Unable to connect\n"); + } + } + return JSON.parse(payload); +} + +String TeaNetworking::bundleNewTeaInfo(String rfidCode, int steepigSeconds) +{ JSONVar bundle; bundle["rfid_code"] = rfidCode; bundle["steeping_seconds"] = steepigSeconds; return JSONVar::stringify(bundle); } -String TeaNetworking::bundleSteepingTimeChange(int steepigSeconds) { +String TeaNetworking::bundleSteepingTimeLogEntry(int steepigSeconds) +{ JSONVar bundle; bundle["new_steeping_seconds"] = steepigSeconds; return JSONVar::stringify(bundle); } +String TeaNetworking::bundleTeaMetaChange(String teaName, String teaNotes, int waterTemp, int steepingSeconds) +{ + JSONVar bundle; + bundle["tea_name"] = teaName; + bundle["tea_notes"] = teaNotes; + bundle["water_temp"] = waterTemp; + bundle["steeping_seconds"] = steepingSeconds; + return JSONVar::stringify(bundle); +} + String TeaNetworking::createTeaData( String teaName, String rfidCode, diff --git a/src/TeaNetworking.hpp b/src/TeaNetworking.hpp index b5f3b35..799c355 100644 --- a/src/TeaNetworking.hpp +++ b/src/TeaNetworking.hpp @@ -12,6 +12,7 @@ public: void init(const char *ssid, const char *password); JSONVar httpsGETRequest(String serverName); JSONVar httpsPOSTRequest(String serverName, String data); + JSONVar httpsPUTRequest(String serverName, String data); bool wifiConnected() { return WiFi.status() == WL_CONNECTED; } String createTeaData( String teaname, @@ -20,5 +21,6 @@ public: int steepingseconds); String createTeaData(TeaData tea); String bundleNewTeaInfo(String rfidCode, int steepigSeconds); - String bundleSteepingTimeChange(int steepigSeconds); + String bundleTeaMetaChange(String teaName, String teaNotes, int waterTemp, int steepingSeconds); + String bundleSteepingTimeLogEntry(int steepigSeconds); }; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index a786b26..d72b5b6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -281,11 +281,12 @@ void loop() if (millis() >= timeoutTime) { // we still want to save the last configuration (treated as perfect, no steeping time change) - String payload = teaNet.bundleSteepingTimeChange(currentTea.m_steepingSeconds); - Serial.println(payload); + String steeepingTimePayload = teaNet.bundleSteepingTimeLogEntry(currentTea.m_steepingSeconds); + String teaMetaPayload = teaNet.bundleTeaMetaChange(currentTea.m_teaName, currentTea.m_teaNotes, currentTea.m_waterTemp, currentTea.m_steepingSeconds); + Serial.println(steeepingTimePayload); smartDisplay.playTeaAnimationPartOne(32, "", "Loading...", "", ""); - JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time-log/" + currentTea.m_id, payload); - JSONVar res2 = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time/" + currentTea.m_id, payload); + JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time-log/" + currentTea.m_id, steeepingTimePayload); + JSONVar res2 = teaNet.httpsPUTRequest(shuttleServer + "/types-of-tea/" + currentTea.m_id, teaMetaPayload); // catch possible failure if (res == JSON.parse("{}")) { @@ -309,12 +310,15 @@ void loop() String uid = smartRFID.readTag(50); // just enough time to detect tag if (uid != "no_tag_detected" && uid != "") { + // we still want to save the last configuration (treated as perfect, no steeping time change) - String payload = teaNet.bundleSteepingTimeChange(currentTea.m_steepingSeconds); - Serial.println(payload); + String steeepingTimePayload = teaNet.bundleSteepingTimeLogEntry(currentTea.m_steepingSeconds); + String teaMetaPayload = teaNet.bundleTeaMetaChange(currentTea.m_teaName, currentTea.m_teaNotes, currentTea.m_waterTemp, currentTea.m_steepingSeconds); + Serial.println(steeepingTimePayload); smartDisplay.playTeaAnimationPartOne(32, "", "Loading...", "", ""); - JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time-log/" + currentTea.m_id, payload); - JSONVar res2 = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time/" + currentTea.m_id, payload); + JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time-log/" + currentTea.m_id, steeepingTimePayload); + JSONVar res2 = teaNet.httpsPUTRequest(shuttleServer + "/types-of-tea/" + currentTea.m_id, teaMetaPayload); + // catch possible failure if (res == JSON.parse("{}")) { @@ -338,12 +342,14 @@ void loop() if (isTouchBtnPressed()) { + // keep steeping time for next brew - String payload = teaNet.bundleSteepingTimeChange(currentTea.m_steepingSeconds); - Serial.println(payload); + String steeepingTimePayload = teaNet.bundleSteepingTimeLogEntry(currentTea.m_steepingSeconds); + String teaMetaPayload = teaNet.bundleTeaMetaChange(currentTea.m_teaName, currentTea.m_teaNotes, currentTea.m_waterTemp, currentTea.m_steepingSeconds); + Serial.println(steeepingTimePayload); smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(perfect)", "", "saving..."); - JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time-log/" + currentTea.m_id, payload); - JSONVar res2 = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time/" + currentTea.m_id, payload); + JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time-log/" + currentTea.m_id, steeepingTimePayload); + JSONVar res2 = teaNet.httpsPUTRequest(shuttleServer + "/types-of-tea/" + currentTea.m_id, teaMetaPayload); // catch possible failure if (res == JSON.parse("{}")) { @@ -365,16 +371,16 @@ void loop() { // decrease steeping time for next brew // TODO: this should be smarter - String payload = teaNet.bundleSteepingTimeChange(currentTea.m_steepingSeconds); - Serial.println(payload); + String steeepingTimePayload = teaNet.bundleSteepingTimeLogEntry(currentTea.m_steepingSeconds); + Serial.println(steeepingTimePayload); smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(shorter)", "", "saving..."); // log old steeping time - JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time-log/" + currentTea.m_id, payload); + JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time-log/" + currentTea.m_id, steeepingTimePayload); // now update steeping time for next use currentTea.m_steepingSeconds -= 10; - String payload2 = teaNet.bundleSteepingTimeChange(currentTea.m_steepingSeconds); - JSONVar res2 = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time/" + currentTea.m_id, payload2); + String teaMetaPayload = teaNet.bundleTeaMetaChange(currentTea.m_teaName, currentTea.m_teaNotes, currentTea.m_waterTemp, currentTea.m_steepingSeconds); + JSONVar res2 = teaNet.httpsPUTRequest(shuttleServer + "/types-of-tea/" + currentTea.m_id, teaMetaPayload); // catch possible failure if (res == JSON.parse("{}")) @@ -397,15 +403,15 @@ void loop() { // increase steeping time for next brew // TODO: this should be smarter - String payload = teaNet.bundleSteepingTimeChange(currentTea.m_steepingSeconds); - Serial.println(payload); + String steeepingTimePayload = teaNet.bundleSteepingTimeLogEntry(currentTea.m_steepingSeconds); + Serial.println(steeepingTimePayload); smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(longer)", "", "saving..."); - JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time-log/" + currentTea.m_id, payload); + JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time-log/" + currentTea.m_id, steeepingTimePayload); // now update steeping time for next use currentTea.m_steepingSeconds += 10; - String payload2 = teaNet.bundleSteepingTimeChange(currentTea.m_steepingSeconds); - JSONVar res2 = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time/" + currentTea.m_id, payload2); + String teaMetaPayload = teaNet.bundleTeaMetaChange(currentTea.m_teaName, currentTea.m_teaNotes, currentTea.m_waterTemp, currentTea.m_steepingSeconds); + JSONVar res2 = teaNet.httpsPUTRequest(shuttleServer + "/types-of-tea/" + currentTea.m_id, teaMetaPayload); // catch possible failure if (res == JSON.parse("{}"))