From b87d97e2bebce99eac35ca7f356130dbaf0f372f Mon Sep 17 00:00:00 2001 From: zzzz Date: Fri, 14 Jun 2024 14:35:49 +0200 Subject: [PATCH] implemented timeout --- src/main.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 227baad..bd237e1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -40,6 +40,13 @@ String shuttleServer = "https://ias-tea-axum.shuttleapp.rs"; TeaData currentTea(-1, "Teafault", "c0derfid", "", -1, 30); +// config +long feedbackTimeout = 15; +int defaultSteepingTime = 120; + +// timeout +unsigned long timeoutTime; + enum State { // lifecycle states @@ -80,7 +87,20 @@ void setup() teaNet.init(ssid, password); smartDisplay.playTeaAnimationPartTwo(32, "", "Success!", "", ""); - delay(250); + + // load config + JSONVar result = teaNet.httpsGETRequest(shuttleServer + "/configuration"); + // catch possible failure + if (result == JSON.parse("{}")) + { + smartDisplay.printMsg("failed loading config"); + delay(500); + } + else + { + defaultSteepingTime = result["default_steeping_time"]; + feedbackTimeout = result["feedback_timeout"]; + } smartDisplay.playTeaAnimation(); delay(250); @@ -166,7 +186,7 @@ void loop() { Serial.print("TEA DOES NOT EXIST YET!"); state = State::DISPLAY_ADD_NEW_TEA_CONFIG; - currentTea = TeaData(-1, "New Tea", currentTea.m_rfidCode, "", -1, 120); + currentTea = TeaData(-1, "New Tea", currentTea.m_rfidCode, "", -1, defaultSteepingTime); smartDisplay.playTeaAnimationPartTwo(32, "", "Done!", "", ""); break; } @@ -235,6 +255,7 @@ void loop() smartStrip.animateBottomToTop(0, 1, 0); delay(1000); state = State::REQUEST_FEEDBACK; + timeoutTime = millis() + (feedbackTimeout * 1000 * 60); break; } @@ -249,11 +270,33 @@ void loop() smartDisplay.printMsg("Ready!"); smartStrip.animateBottomToTop(0, 1, 0); state = State::REQUEST_FEEDBACK; + timeoutTime = millis() + (feedbackTimeout * 1000 * 60); } break; } case REQUEST_FEEDBACK: { + + // timeout + 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); + smartDisplay.playTeaAnimationPartOne(32, "", "Loading...", "", ""); + JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time/" + currentTea.m_id, payload); + // catch possible failure + if (res == JSON.parse("{}")) + { + smartDisplay.printMsg("TRY AGAIN"); + delay(500); + break; + } + state = State::WAIT_FOR_RFID_SCAN; + smartStrip.reset(); + break; + } + smartDisplay.printRequestFeedbackScreen(currentTea); String uid = smartRFID.readTag(50); // just enough time to detect tag @@ -282,8 +325,6 @@ void loop() if (isTouchBtnPressed()) { // keep steeping time for next brew - state = State::WAIT_FOR_RFID_SCAN; - String payload = teaNet.bundleSteepingTimeChange(currentTea.m_steepingSeconds); Serial.println(payload); smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(perfect)", "", "saving..."); @@ -295,6 +336,7 @@ void loop() delay(1000); break; } + state = State::WAIT_FOR_RFID_SCAN; smartDisplay.playTeaAnimationPartTwo(32, "", "Done!", "", ""); smartStrip.reset(); }