From d9753564b9c30a42735dfc6f3040da9bd95de87e Mon Sep 17 00:00:00 2001 From: zzzz Date: Wed, 12 Jun 2024 13:25:59 +0200 Subject: [PATCH] updated backend communication (untested) --- src/TeaData.hpp | 6 +++++- src/TeaNetworking.cpp | 29 +++++++++++++++++++-------- src/TeaNetworking.hpp | 2 ++ src/main.cpp | 46 +++++++++++++++++++++++++------------------ 4 files changed, 55 insertions(+), 28 deletions(-) diff --git a/src/TeaData.hpp b/src/TeaData.hpp index 15fd808..5de6cb5 100644 --- a/src/TeaData.hpp +++ b/src/TeaData.hpp @@ -4,18 +4,22 @@ struct TeaData { public: - TeaData(String teaName, String rfidCode, int waterTemp, int steepingSeconds) + TeaData(int id, String teaName, String rfidCode, String teaNotes, int waterTemp, int steepingSeconds) { + m_id = id; m_teaName = teaName; m_rfidCode = rfidCode; + m_teaNotes = teaNotes; m_waterTemp = waterTemp; m_steepingSeconds = steepingSeconds; } TeaData() {} public: + int m_id; String m_teaName; String m_rfidCode; + String m_teaNotes; int m_waterTemp; int m_steepingSeconds; }; \ No newline at end of file diff --git a/src/TeaNetworking.cpp b/src/TeaNetworking.cpp index 2466622..09c45a0 100644 --- a/src/TeaNetworking.cpp +++ b/src/TeaNetworking.cpp @@ -126,6 +126,19 @@ JSONVar TeaNetworking::httpsPOSTRequest(String serverName, String data) 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) { + JSONVar bundle; + bundle["new_steeping_seconds"] = steepigSeconds; + return JSONVar::stringify(bundle); +} + String TeaNetworking::createTeaData( String teaName, String rfidCode, @@ -133,19 +146,19 @@ String TeaNetworking::createTeaData( int steepingSeconds) { JSONVar teaData; - teaData["teaname"] = teaName; - teaData["rfidcode"] = rfidCode; - teaData["watertemp"] = waterTemp; - teaData["steepingseconds"] = steepingSeconds; + teaData["tea_name"] = teaName; + teaData["rfid_code"] = rfidCode; + teaData["water_temp"] = waterTemp; + teaData["steeping_seconds"] = steepingSeconds; return JSONVar::stringify(teaData); } String TeaNetworking::createTeaData(TeaData tea) { JSONVar teaData; - teaData["teaname"] = tea.m_teaName; - teaData["rfidcode"] = tea.m_rfidCode; - teaData["watertemp"] = tea.m_waterTemp; - teaData["steepingseconds"] = tea.m_steepingSeconds; + teaData["tea_name"] = tea.m_teaName; + teaData["rfid_code"] = tea.m_rfidCode; + teaData["water_temp"] = tea.m_waterTemp; + teaData["steeping_seconds"] = tea.m_steepingSeconds; return JSONVar::stringify(teaData); } \ No newline at end of file diff --git a/src/TeaNetworking.hpp b/src/TeaNetworking.hpp index 3febbd6..b5f3b35 100644 --- a/src/TeaNetworking.hpp +++ b/src/TeaNetworking.hpp @@ -19,4 +19,6 @@ public: int watertemp, int steepingseconds); String createTeaData(TeaData tea); + String bundleNewTeaInfo(String rfidCode, int steepigSeconds); + String bundleSteepingTimeChange(int steepigSeconds); }; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 567c675..227baad 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -38,7 +38,7 @@ SmartButton buttonRight(TACTILE_BTN_RIGHT); // networking String shuttleServer = "https://ias-tea-axum.shuttleapp.rs"; -TeaData currentTea("Teafault", "c0derfid", -1, 30); +TeaData currentTea(-1, "Teafault", "c0derfid", "", -1, 30); enum State { @@ -118,13 +118,13 @@ void loop() state = WAIT_FOR_RFID_SCAN; break; } - JSONVar jsonData = teaNet.httpsGETRequest(shuttleServer + "/alltea"); + JSONVar jsonData = teaNet.httpsGETRequest(shuttleServer + "/types-of-tea"); Serial.println("JSON:"); - Serial.println(jsonData[2]["teaname"]); + Serial.println(jsonData[0]["tea_name"]); delay(2000); - String payload = teaNet.createTeaData("Unnamed Tea", "g8OhgB5", 90, 180); - teaNet.httpsPOSTRequest("https://ias-tea-axum.shuttleapp.rs/addtea", payload); + String payload = teaNet.bundleNewTeaInfo("espDemoRfid", 180); + teaNet.httpsPOSTRequest("https://ias-tea-axum.shuttleapp.rs/add-tea", payload); state = WAIT_FOR_RFID_SCAN; break; @@ -137,7 +137,7 @@ void loop() smartStrip.setGreen(7); smartStrip.setGreen(14); - // this blocks execution until a tag has been detected (5s timeout) + // this blocks execution for 100ms to check for a tag String uid = smartRFID.readTag(100); if (uid != "no_tag_detected" && uid != "") { @@ -150,7 +150,7 @@ void loop() { smartDisplay.playTeaAnimationPartOne(32, "", "Loading...", "", ""); - JSONVar result = teaNet.httpsGETRequest(shuttleServer + "/teabyrfid/" + currentTea.m_rfidCode); + JSONVar result = teaNet.httpsGETRequest(shuttleServer + "/tea-by-rfid/" + currentTea.m_rfidCode); Serial.print("Result:"); Serial.println(result); @@ -166,14 +166,14 @@ void loop() { Serial.print("TEA DOES NOT EXIST YET!"); state = State::DISPLAY_ADD_NEW_TEA_CONFIG; - currentTea = TeaData("New Tea", currentTea.m_rfidCode, -1, 120); + currentTea = TeaData(-1, "New Tea", currentTea.m_rfidCode, "", -1, 120); smartDisplay.playTeaAnimationPartTwo(32, "", "Done!", "", ""); break; } else { state = State::DISPLAY_SCANNED_TEA_CONFIG; - currentTea = TeaData(result["teaname"], currentTea.m_rfidCode, result["watertemp"], result["steepingseconds"]); + currentTea = TeaData(result["id"], result["tea_name"], result["rfid_code"], result["tea_notes"], result["water_temp"], result["steeping_seconds"]); smartDisplay.playTeaAnimationPartTwo(32, "", "Done!", "", ""); } @@ -194,6 +194,10 @@ void loop() if (buttonLeft.isJustPressed()) { currentTea.m_steepingSeconds -= 10; + if (currentTea.m_steepingSeconds < 0) + { + currentTea.m_steepingSeconds = 0; + } } if (buttonRight.isJustPressed()) { @@ -256,10 +260,10 @@ void loop() if (uid != "no_tag_detected" && uid != "") { // we still want to save the last configuration (treated as perfect, no steeping time change) - String payload = teaNet.createTeaData(currentTea); + String payload = teaNet.bundleSteepingTimeChange(currentTea.m_steepingSeconds); Serial.println(payload); smartDisplay.playTeaAnimationPartOne(32, "", "Loading...", "", ""); - JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/updatetea", payload); + JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time/" + currentTea.m_id, payload); // catch possible failure if (res == JSON.parse("{}")) { @@ -280,10 +284,10 @@ void loop() // keep steeping time for next brew state = State::WAIT_FOR_RFID_SCAN; - String payload = teaNet.createTeaData(currentTea); + String payload = teaNet.bundleSteepingTimeChange(currentTea.m_steepingSeconds); Serial.println(payload); smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(perfect)", "", "saving..."); - JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/updatetea", payload); + JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time/" + currentTea.m_id, payload); // catch possible failure if (res == JSON.parse("{}")) { @@ -299,10 +303,10 @@ void loop() // decrease steeping time for next brew state = State::WAIT_FOR_RFID_SCAN; - String payload = teaNet.createTeaData(currentTea); + String payload = teaNet.bundleSteepingTimeChange(currentTea.m_steepingSeconds); Serial.println(payload); smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(shorter)", "", "saving..."); - JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/updatetea", payload); + JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time/" + currentTea.m_id, payload); // catch possible failure if (res == JSON.parse("{}")) { @@ -318,10 +322,10 @@ void loop() // increase steeping time for next brew state = State::WAIT_FOR_RFID_SCAN; - String payload = teaNet.createTeaData(currentTea); + String payload = teaNet.bundleSteepingTimeChange(currentTea.m_steepingSeconds); Serial.println(payload); smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(longer)", "", "saving..."); - JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/updatetea", payload); + JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time/" + currentTea.m_id, payload); // catch possible failure if (res == JSON.parse("{}")) { @@ -351,6 +355,10 @@ void loop() if (buttonLeft.isJustPressed()) { currentTea.m_steepingSeconds -= 10; + if (currentTea.m_steepingSeconds < 0) + { + currentTea.m_steepingSeconds = 0; + } } if (buttonRight.isJustPressed()) { @@ -361,8 +369,8 @@ void loop() { smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "", "", "saving..."); - String payload = teaNet.createTeaData("Unnamed Tea", currentTea.m_rfidCode, -1, currentTea.m_steepingSeconds); - JSONVar result = teaNet.httpsPOSTRequest(shuttleServer + "/addtea", payload); + String payload = teaNet.bundleNewTeaInfo(currentTea.m_rfidCode, currentTea.m_steepingSeconds); + JSONVar result = teaNet.httpsPOSTRequest(shuttleServer + "/add-tea", payload); // catch possible failure if (result == JSON.parse("{}")) {