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:
parent
c558338b63
commit
d9753564b9
4 changed files with 55 additions and 28 deletions
|
@ -4,18 +4,22 @@
|
||||||
struct TeaData
|
struct TeaData
|
||||||
{
|
{
|
||||||
public:
|
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_teaName = teaName;
|
||||||
m_rfidCode = rfidCode;
|
m_rfidCode = rfidCode;
|
||||||
|
m_teaNotes = teaNotes;
|
||||||
m_waterTemp = waterTemp;
|
m_waterTemp = waterTemp;
|
||||||
m_steepingSeconds = steepingSeconds;
|
m_steepingSeconds = steepingSeconds;
|
||||||
}
|
}
|
||||||
TeaData() {}
|
TeaData() {}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
int m_id;
|
||||||
String m_teaName;
|
String m_teaName;
|
||||||
String m_rfidCode;
|
String m_rfidCode;
|
||||||
|
String m_teaNotes;
|
||||||
int m_waterTemp;
|
int m_waterTemp;
|
||||||
int m_steepingSeconds;
|
int m_steepingSeconds;
|
||||||
};
|
};
|
|
@ -126,6 +126,19 @@ JSONVar TeaNetworking::httpsPOSTRequest(String serverName, String data)
|
||||||
return JSON.parse(payload);
|
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 TeaNetworking::createTeaData(
|
||||||
String teaName,
|
String teaName,
|
||||||
String rfidCode,
|
String rfidCode,
|
||||||
|
@ -133,19 +146,19 @@ String TeaNetworking::createTeaData(
|
||||||
int steepingSeconds)
|
int steepingSeconds)
|
||||||
{
|
{
|
||||||
JSONVar teaData;
|
JSONVar teaData;
|
||||||
teaData["teaname"] = teaName;
|
teaData["tea_name"] = teaName;
|
||||||
teaData["rfidcode"] = rfidCode;
|
teaData["rfid_code"] = rfidCode;
|
||||||
teaData["watertemp"] = waterTemp;
|
teaData["water_temp"] = waterTemp;
|
||||||
teaData["steepingseconds"] = steepingSeconds;
|
teaData["steeping_seconds"] = steepingSeconds;
|
||||||
return JSONVar::stringify(teaData);
|
return JSONVar::stringify(teaData);
|
||||||
}
|
}
|
||||||
|
|
||||||
String TeaNetworking::createTeaData(TeaData tea)
|
String TeaNetworking::createTeaData(TeaData tea)
|
||||||
{
|
{
|
||||||
JSONVar teaData;
|
JSONVar teaData;
|
||||||
teaData["teaname"] = tea.m_teaName;
|
teaData["tea_name"] = tea.m_teaName;
|
||||||
teaData["rfidcode"] = tea.m_rfidCode;
|
teaData["rfid_code"] = tea.m_rfidCode;
|
||||||
teaData["watertemp"] = tea.m_waterTemp;
|
teaData["water_temp"] = tea.m_waterTemp;
|
||||||
teaData["steepingseconds"] = tea.m_steepingSeconds;
|
teaData["steeping_seconds"] = tea.m_steepingSeconds;
|
||||||
return JSONVar::stringify(teaData);
|
return JSONVar::stringify(teaData);
|
||||||
}
|
}
|
|
@ -19,4 +19,6 @@ public:
|
||||||
int watertemp,
|
int watertemp,
|
||||||
int steepingseconds);
|
int steepingseconds);
|
||||||
String createTeaData(TeaData tea);
|
String createTeaData(TeaData tea);
|
||||||
|
String bundleNewTeaInfo(String rfidCode, int steepigSeconds);
|
||||||
|
String bundleSteepingTimeChange(int steepigSeconds);
|
||||||
};
|
};
|
46
src/main.cpp
46
src/main.cpp
|
@ -38,7 +38,7 @@ SmartButton buttonRight(TACTILE_BTN_RIGHT);
|
||||||
// networking
|
// networking
|
||||||
String shuttleServer = "https://ias-tea-axum.shuttleapp.rs";
|
String shuttleServer = "https://ias-tea-axum.shuttleapp.rs";
|
||||||
|
|
||||||
TeaData currentTea("Teafault", "c0derfid", -1, 30);
|
TeaData currentTea(-1, "Teafault", "c0derfid", "", -1, 30);
|
||||||
|
|
||||||
enum State
|
enum State
|
||||||
{
|
{
|
||||||
|
@ -118,13 +118,13 @@ void loop()
|
||||||
state = WAIT_FOR_RFID_SCAN;
|
state = WAIT_FOR_RFID_SCAN;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
JSONVar jsonData = teaNet.httpsGETRequest(shuttleServer + "/alltea");
|
JSONVar jsonData = teaNet.httpsGETRequest(shuttleServer + "/types-of-tea");
|
||||||
Serial.println("JSON:");
|
Serial.println("JSON:");
|
||||||
Serial.println(jsonData[2]["teaname"]);
|
Serial.println(jsonData[0]["tea_name"]);
|
||||||
delay(2000);
|
delay(2000);
|
||||||
|
|
||||||
String payload = teaNet.createTeaData("Unnamed Tea", "g8OhgB5", 90, 180);
|
String payload = teaNet.bundleNewTeaInfo("espDemoRfid", 180);
|
||||||
teaNet.httpsPOSTRequest("https://ias-tea-axum.shuttleapp.rs/addtea", payload);
|
teaNet.httpsPOSTRequest("https://ias-tea-axum.shuttleapp.rs/add-tea", payload);
|
||||||
|
|
||||||
state = WAIT_FOR_RFID_SCAN;
|
state = WAIT_FOR_RFID_SCAN;
|
||||||
break;
|
break;
|
||||||
|
@ -137,7 +137,7 @@ void loop()
|
||||||
smartStrip.setGreen(7);
|
smartStrip.setGreen(7);
|
||||||
smartStrip.setGreen(14);
|
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);
|
String uid = smartRFID.readTag(100);
|
||||||
if (uid != "no_tag_detected" && uid != "")
|
if (uid != "no_tag_detected" && uid != "")
|
||||||
{
|
{
|
||||||
|
@ -150,7 +150,7 @@ void loop()
|
||||||
{
|
{
|
||||||
smartDisplay.playTeaAnimationPartOne(32, "", "Loading...", "", "");
|
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.print("Result:");
|
||||||
Serial.println(result);
|
Serial.println(result);
|
||||||
|
|
||||||
|
@ -166,14 +166,14 @@ void loop()
|
||||||
{
|
{
|
||||||
Serial.print("TEA DOES NOT EXIST YET!");
|
Serial.print("TEA DOES NOT EXIST YET!");
|
||||||
state = State::DISPLAY_ADD_NEW_TEA_CONFIG;
|
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!", "", "");
|
smartDisplay.playTeaAnimationPartTwo(32, "", "Done!", "", "");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
state = State::DISPLAY_SCANNED_TEA_CONFIG;
|
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!", "", "");
|
smartDisplay.playTeaAnimationPartTwo(32, "", "Done!", "", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -194,6 +194,10 @@ void loop()
|
||||||
if (buttonLeft.isJustPressed())
|
if (buttonLeft.isJustPressed())
|
||||||
{
|
{
|
||||||
currentTea.m_steepingSeconds -= 10;
|
currentTea.m_steepingSeconds -= 10;
|
||||||
|
if (currentTea.m_steepingSeconds < 0)
|
||||||
|
{
|
||||||
|
currentTea.m_steepingSeconds = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (buttonRight.isJustPressed())
|
if (buttonRight.isJustPressed())
|
||||||
{
|
{
|
||||||
|
@ -256,10 +260,10 @@ void loop()
|
||||||
if (uid != "no_tag_detected" && uid != "")
|
if (uid != "no_tag_detected" && uid != "")
|
||||||
{
|
{
|
||||||
// we still want to save the last configuration (treated as perfect, no steeping time change)
|
// 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);
|
Serial.println(payload);
|
||||||
smartDisplay.playTeaAnimationPartOne(32, "", "Loading...", "", "");
|
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
|
// catch possible failure
|
||||||
if (res == JSON.parse("{}"))
|
if (res == JSON.parse("{}"))
|
||||||
{
|
{
|
||||||
|
@ -280,10 +284,10 @@ void loop()
|
||||||
// keep steeping time for next brew
|
// keep steeping time for next brew
|
||||||
state = State::WAIT_FOR_RFID_SCAN;
|
state = State::WAIT_FOR_RFID_SCAN;
|
||||||
|
|
||||||
String payload = teaNet.createTeaData(currentTea);
|
String payload = teaNet.bundleSteepingTimeChange(currentTea.m_steepingSeconds);
|
||||||
Serial.println(payload);
|
Serial.println(payload);
|
||||||
smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(perfect)", "", "saving...");
|
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
|
// catch possible failure
|
||||||
if (res == JSON.parse("{}"))
|
if (res == JSON.parse("{}"))
|
||||||
{
|
{
|
||||||
|
@ -299,10 +303,10 @@ void loop()
|
||||||
// decrease steeping time for next brew
|
// decrease steeping time for next brew
|
||||||
state = State::WAIT_FOR_RFID_SCAN;
|
state = State::WAIT_FOR_RFID_SCAN;
|
||||||
|
|
||||||
String payload = teaNet.createTeaData(currentTea);
|
String payload = teaNet.bundleSteepingTimeChange(currentTea.m_steepingSeconds);
|
||||||
Serial.println(payload);
|
Serial.println(payload);
|
||||||
smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(shorter)", "", "saving...");
|
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
|
// catch possible failure
|
||||||
if (res == JSON.parse("{}"))
|
if (res == JSON.parse("{}"))
|
||||||
{
|
{
|
||||||
|
@ -318,10 +322,10 @@ void loop()
|
||||||
// increase steeping time for next brew
|
// increase steeping time for next brew
|
||||||
state = State::WAIT_FOR_RFID_SCAN;
|
state = State::WAIT_FOR_RFID_SCAN;
|
||||||
|
|
||||||
String payload = teaNet.createTeaData(currentTea);
|
String payload = teaNet.bundleSteepingTimeChange(currentTea.m_steepingSeconds);
|
||||||
Serial.println(payload);
|
Serial.println(payload);
|
||||||
smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(longer)", "", "saving...");
|
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
|
// catch possible failure
|
||||||
if (res == JSON.parse("{}"))
|
if (res == JSON.parse("{}"))
|
||||||
{
|
{
|
||||||
|
@ -351,6 +355,10 @@ void loop()
|
||||||
if (buttonLeft.isJustPressed())
|
if (buttonLeft.isJustPressed())
|
||||||
{
|
{
|
||||||
currentTea.m_steepingSeconds -= 10;
|
currentTea.m_steepingSeconds -= 10;
|
||||||
|
if (currentTea.m_steepingSeconds < 0)
|
||||||
|
{
|
||||||
|
currentTea.m_steepingSeconds = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (buttonRight.isJustPressed())
|
if (buttonRight.isJustPressed())
|
||||||
{
|
{
|
||||||
|
@ -361,8 +369,8 @@ void loop()
|
||||||
{
|
{
|
||||||
smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "", "", "saving...");
|
smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "", "", "saving...");
|
||||||
|
|
||||||
String payload = teaNet.createTeaData("Unnamed Tea", currentTea.m_rfidCode, -1, currentTea.m_steepingSeconds);
|
String payload = teaNet.bundleNewTeaInfo(currentTea.m_rfidCode, currentTea.m_steepingSeconds);
|
||||||
JSONVar result = teaNet.httpsPOSTRequest(shuttleServer + "/addtea", payload);
|
JSONVar result = teaNet.httpsPOSTRequest(shuttleServer + "/add-tea", payload);
|
||||||
// catch possible failure
|
// catch possible failure
|
||||||
if (result == JSON.parse("{}"))
|
if (result == JSON.parse("{}"))
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue