4
0
Fork 0
mirror of https://github.com/zzzzDev4/IAS-Better-Tea.git synced 2025-04-21 07:31:20 +02:00

updated backend communication (untested)

This commit is contained in:
zzzz 2024-06-12 13:25:59 +02:00
parent c558338b63
commit d9753564b9
4 changed files with 55 additions and 28 deletions

View file

@ -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;
};

View file

@ -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);
}

View file

@ -19,4 +19,6 @@ public:
int watertemp,
int steepingseconds);
String createTeaData(TeaData tea);
String bundleNewTeaInfo(String rfidCode, int steepigSeconds);
String bundleSteepingTimeChange(int steepigSeconds);
};

View file

@ -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("{}"))
{