Skip to content

Commit 85c3bae

Browse files
impl. power level control in UI
1 parent 3f1d094 commit 85c3bae

File tree

12 files changed

+310
-15
lines changed

12 files changed

+310
-15
lines changed

ui/package-lock.json

Lines changed: 168 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ui/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"react": "^18.2.0",
1313
"react-bootstrap": "^2.9.2",
1414
"react-dom": "^18.2.0",
15+
"react-input-slider": "^6.0.1",
1516
"react-redux": "^9.0.4",
1617
"react-scripts": "5.0.1",
1718
"redux-persist": "^6.0.0",

ui/public/gauge.png

2.79 KB
Loading

ui/src/App.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import SystemControls from './components/SystemControls.js';
1010
import SystemStatusArea from './components/SystemStatusArea.js';
1111
import CurrentOperationInfoArea from './components/CurrentOperationInfoArea.js';
1212
import HoldToPour from './components/HoldToPour.js';
13+
import PowerLevel from './components/PowerLevel.js';
1314

1415
function App({ isConnected }) {
1516
return (
@@ -21,6 +22,7 @@ function App({ isConnected }) {
2122
<APIAddressField />
2223
{isConnected ? (
2324
<>
25+
<PowerLevel />
2426
<PourTimeField />
2527
<CurrentOperationInfoArea />
2628
<SystemControls />

ui/src/api/CWaterPumpAPI.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ class CWaterPumpAPI {
2929
});
3030
}
3131

32-
async start(runTimeMs) { return await this._impl.start(runTimeMs); }
32+
async start(runTimeMs, powerLevelInPercents) {
33+
return await this._impl.start(runTimeMs, powerLevelInPercents);
34+
}
35+
3336
async stop() { return await this._impl.stop(); }
3437
async status() { return await this._impl.status(); }
3538
}

ui/src/api/CWaterPumpAPIImpl.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,27 @@ class CWaterPumpAPIImpl {
1111
return { response, requestTime: end - start };
1212
}
1313

14-
async start(runTimeMs) {
14+
async start(runTimeMs, powerLevelInPercents) {
15+
// basic validation
16+
const isValidTime = Number.isInteger(runTimeMs) && (0 < runTimeMs);
17+
if(!isValidTime) {
18+
throw new Error('Pouring time is not a valid number');
19+
}
20+
21+
const isValidPowerLevel = Number.isInteger(powerLevelInPercents) &&
22+
(0 < powerLevelInPercents) && (powerLevelInPercents <= 100);
23+
if(!isValidPowerLevel) {
24+
throw new Error('Power level is not a valid number');
25+
}
26+
/////////////////////////////////////////////////////////////////
1527
const { response: { data }, requestTime } = await this._execute(
16-
async () => await this._client.get('/pour_tea', { params: { milliseconds: runTimeMs } })
28+
async () => await this._client.get(
29+
'/pour_tea',
30+
{ params: {
31+
milliseconds: runTimeMs,
32+
powerLevel: powerLevelInPercents,
33+
}}
34+
)
1735
);
1836
return this.preprocessResponse({ response: data, requestTime });
1937
}

ui/src/api/CWaterPumpAPIImpl.test.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,30 @@ describe('CWaterPumpAPIImpl', () => {
5252
// tests per method
5353
describe('start', () => {
5454
it('common test cases', async () => {
55-
const T = Math.random() * 1000;
56-
const callback = async (api) => await api.start(T);
55+
const T = Math.floor(Math.random() * 1000);
56+
const P = Math.floor(Math.random() * 99) + 1;
57+
const callback = async (api) => await api.start(T, P);
5758
await shouldThrowErrorFromResponse(callback);
5859
await shouldRethrowError(callback);
5960
await shouldPreprocessResponse(callback);
60-
await shouldBeCalledWith(callback, '/pour_tea', { milliseconds: T });
61+
await shouldBeCalledWith(callback, '/pour_tea', { milliseconds: T, powerLevel: P});
62+
});
63+
64+
it('should throw error if pouring time is not a valid number', async () => {
65+
const message = 'Pouring time is not a valid number';
66+
const api = new CWaterPumpAPIImpl({ client: {} });
67+
await expect(api.start('abc', 100)).rejects.toThrow(message);
68+
await expect(api.start(-1, 100)).rejects.toThrow(message);
69+
await expect(api.start(0, 100)).rejects.toThrow(message);
70+
});
71+
72+
it('should throw error if power level is not a valid number', async () => {
73+
const message = 'Power level is not a valid number';
74+
const api = new CWaterPumpAPIImpl({ client: {} });
75+
await expect(api.start(100, 'abc')).rejects.toThrow(message);
76+
await expect(api.start(100, -1)).rejects.toThrow(message);
77+
await expect(api.start(100, 0)).rejects.toThrow(message);
78+
await expect(api.start(100, 101)).rejects.toThrow(message);
6179
});
6280
});
6381

ui/src/components/HoldToPour.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,23 @@ export function HoldToPourComponent({ startPump, stopPump, interval }) {
6565
}
6666

6767
// Helper wrapper to simplify the code in the component
68-
function HoldToPourComponent_withExtras({ pouringTime, startPump, stopPump }) {
68+
function HoldToPourComponent_withExtras({ pouringTime, powerLevel, startPump, stopPump }) {
6969
const api = useWaterPumpAPI().API;
70+
// to prevent the callback from changing when the pouringTime or powerLevel changes
71+
const _pouringTime = React.useRef(pouringTime);
72+
React.useEffect(() => { _pouringTime.current = pouringTime; }, [pouringTime]);
73+
74+
const _powerLevel = React.useRef(powerLevel);
75+
React.useEffect(() => { _powerLevel.current = powerLevel; }, [powerLevel]);
7076

7177
const _startPump = React.useCallback(
72-
async () => { await startPump({ api, pouringTime }); },
73-
[api, startPump, pouringTime]
78+
async () => {
79+
await startPump({
80+
api,
81+
pouringTime: _pouringTime.current,
82+
powerLevel: _powerLevel.current,
83+
});
84+
}, [api, startPump, _pouringTime, _powerLevel]
7485
);
7586
const _stopPump = React.useCallback(
7687
async () => { await stopPump({ api }); },
@@ -88,6 +99,9 @@ function HoldToPourComponent_withExtras({ pouringTime, startPump, stopPump }) {
8899
};
89100

90101
export default connect(
91-
state => ({ pouringTime: state.UI.pouringTime }),
102+
state => ({
103+
pouringTime: state.UI.pouringTime,
104+
powerLevel: state.UI.powerLevelInPercents,
105+
}),
92106
{ startPump, stopPump }
93107
)(HoldToPourComponent_withExtras);

0 commit comments

Comments
 (0)