diff --git a/src/main.cpp b/src/main.cpp index d72b5b6..66b1c2f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,6 +47,9 @@ int defaultSteepingTime = 120; // timeout unsigned long timeoutTime; +// macros +#define CATCH_NET_FAILURE(netResult, msg) if (netResult == JSON.parse("{}")) {smartDisplay.printMsg(msg);delay(500);break;} + enum State { // lifecycle states @@ -171,16 +174,8 @@ void loop() smartDisplay.playTeaAnimationPartOne(32, "", "Loading...", "", ""); JSONVar result = teaNet.httpsGETRequest(shuttleServer + "/tea-by-rfid/" + currentTea.m_rfidCode); - Serial.print("Result:"); - Serial.println(result); - // catch possible failure - if (result == JSON.parse("{}")) - { - smartDisplay.printMsg("GET FAILED"); - delay(500); - break; - } + CATCH_NET_FAILURE(result, "GET FAILED") if (JSONVar::stringify(result) == "null") { @@ -287,19 +282,10 @@ void loop() smartDisplay.playTeaAnimationPartOne(32, "", "Loading...", "", ""); 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("{}")) - { - smartDisplay.printMsg("TRY AGAIN"); - delay(500); - break; - } - if (res2 == JSON.parse("{}")) - { - smartDisplay.printMsg("TRY AGAIN"); - delay(500); - break; - } + + CATCH_NET_FAILURE(res, "TRY AGAIN") + CATCH_NET_FAILURE(res2, "TRY AGAIN") + state = State::WAIT_FOR_RFID_SCAN; smartStrip.reset(); break; @@ -319,19 +305,8 @@ void loop() 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("{}")) - { - smartDisplay.printMsg("TRY AGAIN"); - delay(500); - break; - } - if (res2 == JSON.parse("{}")) - { - smartDisplay.printMsg("TRY AGAIN"); - delay(500); - break; - } + CATCH_NET_FAILURE(res, "TRY AGAIN") + CATCH_NET_FAILURE(res2, "TRY AGAIN") currentTea.m_rfidCode = uid; smartDisplay.resetDisplay(); @@ -350,19 +325,10 @@ void loop() smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(perfect)", "", "saving..."); 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("{}")) - { - smartDisplay.printMsg("POST FAILED"); - delay(1000); - break; - } - if (res2 == JSON.parse("{}")) - { - smartDisplay.printMsg("TRY AGAIN"); - delay(500); - break; - } + + CATCH_NET_FAILURE(res, "TRY AGAIN") + CATCH_NET_FAILURE(res2, "TRY AGAIN") + state = State::WAIT_FOR_RFID_SCAN; smartDisplay.playTeaAnimationPartTwo(32, "", "Done!", "", ""); smartStrip.reset(); @@ -382,19 +348,9 @@ void loop() 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("{}")) - { - smartDisplay.printMsg("POST FAILED"); - delay(1000); - break; - } - if (res2 == JSON.parse("{}")) - { - smartDisplay.printMsg("TRY AGAIN"); - delay(500); - break; - } + CATCH_NET_FAILURE(res, "TRY AGAIN") + CATCH_NET_FAILURE(res2, "TRY AGAIN") + state = State::WAIT_FOR_RFID_SCAN; smartDisplay.playTeaAnimationPartTwo(32, "", "Done!", "", ""); smartStrip.reset(); @@ -413,19 +369,9 @@ void loop() 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("{}")) - { - smartDisplay.printMsg("POST FAILED"); - delay(1000); - break; - } - if (res2 == JSON.parse("{}")) - { - smartDisplay.printMsg("TRY AGAIN"); - delay(500); - break; - } + CATCH_NET_FAILURE(res, "TRY AGAIN") + CATCH_NET_FAILURE(res2, "TRY AGAIN") + state = State::WAIT_FOR_RFID_SCAN; smartDisplay.playTeaAnimationPartTwo(32, "", "Done!", "", ""); smartStrip.reset(); @@ -465,13 +411,8 @@ void loop() 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("{}")) - { - smartDisplay.printMsg("POST FAILED"); - delay(500); - break; - } + + CATCH_NET_FAILURE(result, "POST FAILED") smartDisplay.playTeaAnimationPartTwo(32, "", "Done!", "", ""); state = State::DISPLAY_SCANNED_TEA_CONFIG;