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

Implement SmartFeedback adjustments

This commit is contained in:
zzzz 2024-06-21 15:17:18 +02:00
parent 76e4454761
commit 308381cfb2
4 changed files with 85 additions and 11 deletions

44
src/SmartFeedback.cpp Normal file
View file

@ -0,0 +1,44 @@
#include "SmartFeedback.hpp"
// bool: false => feedback less brewtime; true => feedback more brewtime
int SmartFeedback::calculateNewTime(bool feedback, JSONVar steepingLog, int currentSteepingTime)
{
int numLogEntries = steepingLog.length();
int steepingTimes[numLogEntries];
for (int i = 0; i < numLogEntries; i++)
{
steepingTimes[i] = steepingLog[i]["steeping_seconds"];
}
// if there is no log yet, we just add/substract MAX_STEP
if (numLogEntries == 0)
return feedback ? currentSteepingTime + MAX_STEP : currentSteepingTime - MAX_STEP;
// check the last brewing time
int lastSteepingTime = steepingTimes[numLogEntries-1];
Serial.print("last steeping time: ");
Serial.println(lastSteepingTime);
if (feedback)
{
// we want a longer brewing time
if (lastSteepingTime >= currentSteepingTime)
return currentSteepingTime + MIDDLE_STEP;
if (lastSteepingTime < currentSteepingTime)
return currentSteepingTime + MAX_STEP;
}
else
{
// we want a shorter brewing time
if (lastSteepingTime > currentSteepingTime)
return currentSteepingTime - MAX_STEP;
if (lastSteepingTime <= currentSteepingTime)
return currentSteepingTime - MIDDLE_STEP;
}
// something went wrong
return 0;
}

13
src/SmartFeedback.hpp Normal file
View file

@ -0,0 +1,13 @@
#pragma once
#include <Arduino_JSON.h>
class SmartFeedback
{
public:
// bool: false => feedback less brewtime; true => feedback more brewtime
int calculateNewTime(bool feedback, JSONVar steepingLog, int currentBrewtime);
private:
int MAX_STEP = 20;
int MIDDLE_STEP = 10;
int MIN_STEP = 5;
};

View file

@ -31,6 +31,7 @@ SmartDisplay smartDisplay(&display);
TeaNetworking teaNet;
SmartSound sound(BUZZER_PIN);
SmartFeedback smartFeedback;
SmartButton buttonLeft(TACTILE_BTN_LEFT);
SmartButton buttonRight(TACTILE_BTN_RIGHT);
@ -48,7 +49,13 @@ int defaultSteepingTime = 120;
unsigned long timeoutTime;
// macros
#define CATCH_NET_FAILURE(netResult, msg) if (netResult == JSON.parse("{}")) {smartDisplay.printMsg(msg);delay(500);break;}
#define CATCH_NET_FAILURE(netResult, msg) \
if (netResult == JSON.parse("{}")) \
{ \
smartDisplay.printMsg(msg); \
delay(500); \
break; \
}
enum State
{
@ -317,12 +324,12 @@ void loop()
if (isTouchBtnPressed())
{
smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(perfect)", "", "saving...");
// keep steeping time for next brew
String steeepingTimePayload = teaNet.bundleSteepingTimeLogEntry(currentTea.m_steepingSeconds);
String teaMetaPayload = teaNet.bundleTeaMetaChange(currentTea.m_teaName, currentTea.m_teaNotes, currentTea.m_waterTemp, currentTea.m_steepingSeconds);
Serial.println(steeepingTimePayload);
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);
@ -335,16 +342,21 @@ void loop()
}
if (buttonLeft.isJustPressed())
{
// decrease steeping time for next brew // TODO: this should be smarter
// decrease steeping time for next brew
smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(shorter)", "", "saving...");
// request steeping log
JSONVar steepingLog = teaNet.httpsGETRequest(shuttleServer + "/brewing-events/" + currentTea.m_id);
CATCH_NET_FAILURE(steepingLog, "TRY AGAIN")
String steeepingTimePayload = teaNet.bundleSteepingTimeLogEntry(currentTea.m_steepingSeconds);
Serial.println(steeepingTimePayload);
smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(shorter)", "", "saving...");
// log old steeping time
JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time-log/" + currentTea.m_id, steeepingTimePayload);
// now update steeping time for next use
currentTea.m_steepingSeconds -= 10;
currentTea.m_steepingSeconds = smartFeedback.calculateNewTime(false, steepingLog, 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);
@ -357,15 +369,19 @@ void loop()
}
if (buttonRight.isJustPressed())
{
// increase steeping time for next brew // TODO: this should be smarter
// increase steeping time for next brew
smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(longer)", "", "saving...");
// request steeping log
JSONVar steepingLog = teaNet.httpsGETRequest(shuttleServer + "/brewing-events/" + currentTea.m_id);
CATCH_NET_FAILURE(steepingLog, "TRY AGAIN")
String steeepingTimePayload = teaNet.bundleSteepingTimeLogEntry(currentTea.m_steepingSeconds);
Serial.println(steeepingTimePayload);
smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(longer)", "", "saving...");
JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time-log/" + currentTea.m_id, steeepingTimePayload);
// now update steeping time for next use
currentTea.m_steepingSeconds += 10;
currentTea.m_steepingSeconds = smartFeedback.calculateNewTime(true, steepingLog, 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);

View file

@ -19,5 +19,6 @@
#include "TeaData.hpp"
#include "SmartButton.hpp"
#include "SmartSound.hpp"
#include "SmartFeedback.hpp"
bool isTouchBtnPressed();