mirror of
https://github.com/zzzzDev4/IAS-Better-Tea.git
synced 2025-04-21 07:31:20 +02:00
moved boilerplate code for catching network failures to macro
This commit is contained in:
parent
30294c3371
commit
76e4454761
1 changed files with 22 additions and 81 deletions
103
src/main.cpp
103
src/main.cpp
|
@ -47,6 +47,9 @@ int defaultSteepingTime = 120;
|
||||||
// timeout
|
// timeout
|
||||||
unsigned long timeoutTime;
|
unsigned long timeoutTime;
|
||||||
|
|
||||||
|
// macros
|
||||||
|
#define CATCH_NET_FAILURE(netResult, msg) if (netResult == JSON.parse("{}")) {smartDisplay.printMsg(msg);delay(500);break;}
|
||||||
|
|
||||||
enum State
|
enum State
|
||||||
{
|
{
|
||||||
// lifecycle states
|
// lifecycle states
|
||||||
|
@ -171,16 +174,8 @@ void loop()
|
||||||
smartDisplay.playTeaAnimationPartOne(32, "", "Loading...", "", "");
|
smartDisplay.playTeaAnimationPartOne(32, "", "Loading...", "", "");
|
||||||
|
|
||||||
JSONVar result = teaNet.httpsGETRequest(shuttleServer + "/tea-by-rfid/" + currentTea.m_rfidCode);
|
JSONVar result = teaNet.httpsGETRequest(shuttleServer + "/tea-by-rfid/" + currentTea.m_rfidCode);
|
||||||
Serial.print("Result:");
|
|
||||||
Serial.println(result);
|
|
||||||
|
|
||||||
// catch possible failure
|
CATCH_NET_FAILURE(result, "GET FAILED")
|
||||||
if (result == JSON.parse("{}"))
|
|
||||||
{
|
|
||||||
smartDisplay.printMsg("GET FAILED");
|
|
||||||
delay(500);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (JSONVar::stringify(result) == "null")
|
if (JSONVar::stringify(result) == "null")
|
||||||
{
|
{
|
||||||
|
@ -287,19 +282,10 @@ void loop()
|
||||||
smartDisplay.playTeaAnimationPartOne(32, "", "Loading...", "", "");
|
smartDisplay.playTeaAnimationPartOne(32, "", "Loading...", "", "");
|
||||||
JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time-log/" + currentTea.m_id, steeepingTimePayload);
|
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);
|
JSONVar res2 = teaNet.httpsPUTRequest(shuttleServer + "/types-of-tea/" + currentTea.m_id, teaMetaPayload);
|
||||||
// catch possible failure
|
|
||||||
if (res == JSON.parse("{}"))
|
CATCH_NET_FAILURE(res, "TRY AGAIN")
|
||||||
{
|
CATCH_NET_FAILURE(res2, "TRY AGAIN")
|
||||||
smartDisplay.printMsg("TRY AGAIN");
|
|
||||||
delay(500);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (res2 == JSON.parse("{}"))
|
|
||||||
{
|
|
||||||
smartDisplay.printMsg("TRY AGAIN");
|
|
||||||
delay(500);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
state = State::WAIT_FOR_RFID_SCAN;
|
state = State::WAIT_FOR_RFID_SCAN;
|
||||||
smartStrip.reset();
|
smartStrip.reset();
|
||||||
break;
|
break;
|
||||||
|
@ -319,19 +305,8 @@ void loop()
|
||||||
JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time-log/" + currentTea.m_id, steeepingTimePayload);
|
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);
|
JSONVar res2 = teaNet.httpsPUTRequest(shuttleServer + "/types-of-tea/" + currentTea.m_id, teaMetaPayload);
|
||||||
|
|
||||||
// catch possible failure
|
CATCH_NET_FAILURE(res, "TRY AGAIN")
|
||||||
if (res == JSON.parse("{}"))
|
CATCH_NET_FAILURE(res2, "TRY AGAIN")
|
||||||
{
|
|
||||||
smartDisplay.printMsg("TRY AGAIN");
|
|
||||||
delay(500);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (res2 == JSON.parse("{}"))
|
|
||||||
{
|
|
||||||
smartDisplay.printMsg("TRY AGAIN");
|
|
||||||
delay(500);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
currentTea.m_rfidCode = uid;
|
currentTea.m_rfidCode = uid;
|
||||||
smartDisplay.resetDisplay();
|
smartDisplay.resetDisplay();
|
||||||
|
@ -350,19 +325,10 @@ void loop()
|
||||||
smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(perfect)", "", "saving...");
|
smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(perfect)", "", "saving...");
|
||||||
JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time-log/" + currentTea.m_id, steeepingTimePayload);
|
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);
|
JSONVar res2 = teaNet.httpsPUTRequest(shuttleServer + "/types-of-tea/" + currentTea.m_id, teaMetaPayload);
|
||||||
// catch possible failure
|
|
||||||
if (res == JSON.parse("{}"))
|
CATCH_NET_FAILURE(res, "TRY AGAIN")
|
||||||
{
|
CATCH_NET_FAILURE(res2, "TRY AGAIN")
|
||||||
smartDisplay.printMsg("POST FAILED");
|
|
||||||
delay(1000);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (res2 == JSON.parse("{}"))
|
|
||||||
{
|
|
||||||
smartDisplay.printMsg("TRY AGAIN");
|
|
||||||
delay(500);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
state = State::WAIT_FOR_RFID_SCAN;
|
state = State::WAIT_FOR_RFID_SCAN;
|
||||||
smartDisplay.playTeaAnimationPartTwo(32, "", "Done!", "", "");
|
smartDisplay.playTeaAnimationPartTwo(32, "", "Done!", "", "");
|
||||||
smartStrip.reset();
|
smartStrip.reset();
|
||||||
|
@ -382,19 +348,9 @@ void loop()
|
||||||
String teaMetaPayload = teaNet.bundleTeaMetaChange(currentTea.m_teaName, currentTea.m_teaNotes, currentTea.m_waterTemp, currentTea.m_steepingSeconds);
|
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);
|
JSONVar res2 = teaNet.httpsPUTRequest(shuttleServer + "/types-of-tea/" + currentTea.m_id, teaMetaPayload);
|
||||||
|
|
||||||
// catch possible failure
|
CATCH_NET_FAILURE(res, "TRY AGAIN")
|
||||||
if (res == JSON.parse("{}"))
|
CATCH_NET_FAILURE(res2, "TRY AGAIN")
|
||||||
{
|
|
||||||
smartDisplay.printMsg("POST FAILED");
|
|
||||||
delay(1000);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (res2 == JSON.parse("{}"))
|
|
||||||
{
|
|
||||||
smartDisplay.printMsg("TRY AGAIN");
|
|
||||||
delay(500);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
state = State::WAIT_FOR_RFID_SCAN;
|
state = State::WAIT_FOR_RFID_SCAN;
|
||||||
smartDisplay.playTeaAnimationPartTwo(32, "", "Done!", "", "");
|
smartDisplay.playTeaAnimationPartTwo(32, "", "Done!", "", "");
|
||||||
smartStrip.reset();
|
smartStrip.reset();
|
||||||
|
@ -413,19 +369,9 @@ void loop()
|
||||||
String teaMetaPayload = teaNet.bundleTeaMetaChange(currentTea.m_teaName, currentTea.m_teaNotes, currentTea.m_waterTemp, currentTea.m_steepingSeconds);
|
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);
|
JSONVar res2 = teaNet.httpsPUTRequest(shuttleServer + "/types-of-tea/" + currentTea.m_id, teaMetaPayload);
|
||||||
|
|
||||||
// catch possible failure
|
CATCH_NET_FAILURE(res, "TRY AGAIN")
|
||||||
if (res == JSON.parse("{}"))
|
CATCH_NET_FAILURE(res2, "TRY AGAIN")
|
||||||
{
|
|
||||||
smartDisplay.printMsg("POST FAILED");
|
|
||||||
delay(1000);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (res2 == JSON.parse("{}"))
|
|
||||||
{
|
|
||||||
smartDisplay.printMsg("TRY AGAIN");
|
|
||||||
delay(500);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
state = State::WAIT_FOR_RFID_SCAN;
|
state = State::WAIT_FOR_RFID_SCAN;
|
||||||
smartDisplay.playTeaAnimationPartTwo(32, "", "Done!", "", "");
|
smartDisplay.playTeaAnimationPartTwo(32, "", "Done!", "", "");
|
||||||
smartStrip.reset();
|
smartStrip.reset();
|
||||||
|
@ -465,13 +411,8 @@ void loop()
|
||||||
|
|
||||||
String payload = teaNet.bundleNewTeaInfo(currentTea.m_rfidCode, currentTea.m_steepingSeconds);
|
String payload = teaNet.bundleNewTeaInfo(currentTea.m_rfidCode, currentTea.m_steepingSeconds);
|
||||||
JSONVar result = teaNet.httpsPOSTRequest(shuttleServer + "/add-tea", payload);
|
JSONVar result = teaNet.httpsPOSTRequest(shuttleServer + "/add-tea", payload);
|
||||||
// catch possible failure
|
|
||||||
if (result == JSON.parse("{}"))
|
CATCH_NET_FAILURE(result, "POST FAILED")
|
||||||
{
|
|
||||||
smartDisplay.printMsg("POST FAILED");
|
|
||||||
delay(500);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
smartDisplay.playTeaAnimationPartTwo(32, "", "Done!", "", "");
|
smartDisplay.playTeaAnimationPartTwo(32, "", "Done!", "", "");
|
||||||
state = State::DISPLAY_SCANNED_TEA_CONFIG;
|
state = State::DISPLAY_SCANNED_TEA_CONFIG;
|
||||||
|
|
Loading…
Add table
Reference in a new issue