Skip to content

Commit

Permalink
countdown for operation and some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenWizard2015 committed Jan 8, 2024
1 parent 6e34b97 commit fe49ff7
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 26 deletions.
7 changes: 7 additions & 0 deletions ui/src/App.css
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
.App {
}

.countdown-area {
width: 100%;
text-align: center;
font-weight: bold;
font-size: 2rem;
}
11 changes: 6 additions & 5 deletions ui/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { Container, Form } from 'react-bootstrap';
import { connect } from 'react-redux';

import NotificationsArea from './components/NotificationsArea.js';
import APIAddressField from './components/APIAddressField';
import PourTimeField from './components/PourTimeField';
import SystemControls from './components/SystemControls';
import SystemStatusArea from './components/SystemStatusArea';
import APIAddressField from './components/APIAddressField.js';
import PourTimeField from './components/PourTimeField.js';
import SystemControls from './components/SystemControls.js';
import SystemStatusArea from './components/SystemStatusArea.js';
import CurrentOperationInfoArea from './components/CurrentOperationInfoArea.js';

function App({ isConnected }) {
// TODO: Add a fake countdown timer of timeLeft
return (
<Container className="App">
<h1>Tea System UI</h1>
Expand All @@ -21,6 +21,7 @@ function App({ isConnected }) {
{isConnected ? (
<>
<PourTimeField />
<CurrentOperationInfoArea />
<SystemControls />
</>
) : null}
Expand Down
20 changes: 20 additions & 0 deletions ui/src/Utils/time.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function toTimeStr(diff) {
const seconds = Math.floor(diff / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);

const secondsStr = (seconds % 60).toString().padStart(2, '0');
const minutesStr = (minutes % 60).toString().padStart(2, '0');
const hoursStr = hours.toString().padStart(2, '0');

return `${hoursStr}:${minutesStr}:${secondsStr}`;
}

export function timeBetweenAsString({endTime=null, startTime=null}) {
if (null === startTime) startTime = new Date();
if (null === endTime) endTime = new Date();

const diff = endTime - startTime; // in ms
if (diff < 0) return '-' + toTimeStr(-diff);
return toTimeStr(diff);
}
1 change: 1 addition & 0 deletions ui/src/api/CWaterPumpAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function preprocessResponse(response) {
response.updated = Date.now();
// difference between current time on client and time on device
response.timeDelta = response.updated - response.time;
// TODO: add field response.pump.estimatedEndTime
return response;
}

Expand Down
22 changes: 22 additions & 0 deletions ui/src/components/CurrentOperationInfoArea.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from "react";
import { connect } from "react-redux";
import TimerArea from "./TimerArea";

export function CurrentOperationInfoAreaComponent({
isRunning, estimatedEndTime
}) {
if (!isRunning) return null;
return (
<div className="countdown-area">
<TimerArea startTime={null} endTime={estimatedEndTime} />
</div>
);
}

export default connect(
state => ({
isRunning: state.systemStatus.pump.running,
estimatedEndTime: state.systemStatus.pump.estimatedEndTime,
}),
[]
)(CurrentOperationInfoAreaComponent);
22 changes: 2 additions & 20 deletions ui/src/components/SystemStatusArea.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
import React from 'react';
import { Card } from 'react-bootstrap';
import { connect } from 'react-redux';

// time elapsed since last update
function TimeElapsedComponent({ updated }) {
const [diffString, setDiffString] = React.useState('');
React.useEffect(() => {
const interval = setInterval(() => {
const now = new Date();
const diff = now - updated;
const newDiffString = new Date(diff).toISOString().substr(11, 8);
setDiffString(newDiffString);
}, 1000);

return () => clearInterval(interval);
}, [updated]);

return (
<span>{diffString}</span>
);
}
import TimerArea from '../components/TimerArea';

function _systemStatus(status) {
if (null === status) {
Expand All @@ -30,7 +12,7 @@ function _systemStatus(status) {
return (
<>
<strong>Time since last update:</strong>{' '}
<TimeElapsedComponent updated={status.updated} />
<TimerArea startTime={status.updated} />
<br />
<strong>Pump Running:</strong> {pump.running ? "Yes" : "No"}<br />
<strong>Time Left:</strong> {pump.timeLeft} ms
Expand Down
22 changes: 22 additions & 0 deletions ui/src/components/TimerArea.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from "react";
import { timeBetweenAsString } from "../Utils/time";

export function TimerArea({ startTime=null, endTime=null, interval=450 }) {
const [countdown, setCountdown] = React.useState('');

React.useEffect(() => {
const tid = setInterval(() => {
setCountdown(timeBetweenAsString({ startTime, endTime }));
}, interval);

return () => clearInterval(tid);
}, [startTime, endTime, interval]);

return (
<span className="timer-area">
{countdown}
</span>
);
}

export default TimerArea;
2 changes: 1 addition & 1 deletion ui/src/store/slices/SystemStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const stopPump = createAsyncThunk(

// slice for system status
const bindStatus = (state, action) => {
return preprocessSystemStatus(action.payload);
return action.payload;
};

export const SystemStatusSlice = createSlice({
Expand Down

0 comments on commit fe49ff7

Please sign in to comment.