Skip to content

Commit 52418ea

Browse files
misc adjustments and stop logic for pouring
1 parent a99f851 commit 52418ea

File tree

4 files changed

+49
-14
lines changed

4 files changed

+49
-14
lines changed

ui/src/components/HoldToPour.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@ import { connect } from 'react-redux';
33
import { Container, Form } from 'react-bootstrap';
44
import { useWaterPumpAPI } from '../contexts/WaterPumpAPIContext';
55

6-
export function HoldToPourComponent({ interval }) {
6+
export function HoldToPourComponent({ interval, estimatedTeaLevel }) {
77
const { API }= useWaterPumpAPI();
88
const [isPouring, setIsPouring] = useState(false);
99
const [clickToPour, setClickToPour] = useState(false);
1010
// continuously pour water while the button is pressed
1111
const lastPouringTime = React.useRef(0);
12+
// stop pouring if estimated level is greater than 100%
13+
useEffect(() => {
14+
if(!isPouring) return;
15+
if(estimatedTeaLevel >= 100) {
16+
setIsPouring(false);
17+
}
18+
}, [isPouring, estimatedTeaLevel]);
19+
1220
const onTick = React.useCallback(
1321
async () => {
1422
if(Date.now() < lastPouringTime.current) return;
@@ -77,6 +85,7 @@ function HoldToPourComponent_withExtras({ pouringTime, ...props }) {
7785
export default connect(
7886
state => ({
7987
pouringTime: state.UI.pouringTime,
88+
estimatedTeaLevel: state.Temp.estimatedTeaLevel,
8089
}),
8190
{ }
8291
)(HoldToPourComponent_withExtras);

ui/src/components/TeaLevel.css

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
max-width: 100%;
1414
width: fit-content;
1515
display: flex;
16+
padding: 1rem;
1617
}
1718

1819
.cup-image {
@@ -31,6 +32,7 @@
3132
height: 3px;
3233
background-color: #76b7b2;
3334
z-index: 2;
35+
color: #76b7b2;
3436
}
3537

3638
.est-tea-level {
@@ -40,4 +42,19 @@
4042
height: 3px;
4143
border-bottom: 3px dashed red;
4244
z-index: 3;
45+
text-align: right;
46+
padding-bottom: 2rem;
47+
color: red;
4348
}
49+
50+
.prev-tea-level {
51+
position: absolute;
52+
left: 0px;
53+
right: 0px;
54+
height: 3px;
55+
border-bottom: 3px dashed black;
56+
z-index: 3;
57+
text-align: center;
58+
padding-bottom: 2rem;
59+
color: black;
60+
}

ui/src/components/TeaLevel.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ function toPercentage(realLevel) {
2020

2121
function TeaLevel({
2222
lastOperationDuration, speed, startTeaLevel, estimatedTeaLevel,
23-
changeStartTeaLevel, changeEstimatedTeaLevel, changeSpeed, lastTeaLevel
23+
changeStartTeaLevel, changeEstimatedTeaLevel, changeSpeed, lastTeaLevel,
2424
}) {
2525
const [calcSpeed, setCalcSpeed] = useState(speed);
2626
// update the estimated level if speed or duration changes
2727
useEffect(() => {
2828
const estimatedLevel = startTeaLevel + speed * lastOperationDuration / 1000;
29-
console.log(startTeaLevel, speed, lastOperationDuration, estimatedLevel);
3029
changeEstimatedTeaLevel(estimatedLevel);
3130
}, [lastOperationDuration, speed, startTeaLevel, changeEstimatedTeaLevel]);
3231

@@ -37,9 +36,10 @@ function TeaLevel({
3736
const clickedPercentage = (clickedPosition / height) * 100;
3837
const newLevel = toPercentage(clickedPercentage);
3938
// limit the new level to the range [0, 100]
40-
changeStartTeaLevel( Math.min(Math.max(newLevel, 0), 100) );
39+
const level = Math.min(Math.max(newLevel, 0), 100);
40+
changeStartTeaLevel( level );
4141
// find speed
42-
const newSpeed = (newLevel - lastTeaLevel) / (lastOperationDuration / 1000);
42+
const newSpeed = (level - lastTeaLevel) / (lastOperationDuration / 1000);
4343
setCalcSpeed(newSpeed);
4444
};
4545

@@ -56,8 +56,17 @@ function TeaLevel({
5656
<img src={cup} alt="Cup" className="cup-image" draggable="false"
5757
onClick={handleCupClick}
5858
/>
59-
<div className="tea-level" style={{ bottom: `${toRealLevel(startTeaLevel)}%` }}></div>
60-
<div className="est-tea-level" style={{ bottom: `${toRealLevel(estimatedTeaLevel)}%` }}></div>
59+
<div className="tea-level" style={{ bottom: `${toRealLevel(startTeaLevel)}%` }}>
60+
{startTeaLevel.toFixed(0)}%
61+
</div>
62+
63+
<div className="est-tea-level" style={{ bottom: `${toRealLevel(estimatedTeaLevel)}%` }}>
64+
{estimatedTeaLevel.toFixed(0)}%
65+
</div>
66+
67+
<div className="prev-tea-level" style={{ bottom: `${toRealLevel(lastTeaLevel)}%` }}>
68+
{lastTeaLevel.toFixed(0)}%
69+
</div>
6170
</div>
6271
</div>
6372
</div>
@@ -67,7 +76,7 @@ function TeaLevel({
6776
<p>last tea level: {lastTeaLevel.toFixed(2)}%</p>
6877
</div>
6978
<div>
70-
<input type="text" value={calcSpeed.toFixed(2)} readOnly />
79+
<input type="number" step="0.01" value={calcSpeed.toFixed(2)} />
7180
<button onClick={onSpeedSet}>Set Speed</button>
7281
</div>
7382
</>

ui/src/store/slices/Temp.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const initialState = {
55
prevTeaLevel: 0,
66
startTeaLevel: 0,
77
estimatedTeaLevel: 50,
8+
estimation: true,
89
}
910

1011
export const TempSlice = createSlice({
@@ -15,18 +16,18 @@ export const TempSlice = createSlice({
1516
state.lastOperationDuration = action.payload;
1617
},
1718
changeEstimatedTeaLevel: (state, action) => {
18-
state.estimatedTeaLevel = action.payload;
19-
},
20-
changePrevTeaLevel: (state, action) => {
21-
state.prevTeaLevel = action.payload;
19+
if (state.estimation) {
20+
state.estimatedTeaLevel = action.payload;
21+
}
2222
},
2323
pumpStartedEvent: (state, action) => {
2424
state.prevTeaLevel = state.startTeaLevel;
25+
state.estimation = true;
2526
},
2627
changeStartTeaLevel: (state, action) => {
2728
state.startTeaLevel = action.payload;
2829
state.estimatedTeaLevel = state.startTeaLevel;
29-
state.lastOperationDuration = 0;
30+
state.estimation = false;
3031
},
3132
}
3233
});
@@ -35,7 +36,6 @@ export const actions = TempSlice.actions;
3536
export const {
3637
changeLastOperationDuration,
3738
changeEstimatedTeaLevel,
38-
changePrevTeaLevel,
3939
pumpStartedEvent,
4040
changeStartTeaLevel
4141
} = actions;

0 commit comments

Comments
 (0)