-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update header bar : gpio and exploration timer config (#179)
* add exploration timer * fix lint * re fix lint * move gpio config to header tab * try fix pr comment * try fix pr comment * debug * clean up
- Loading branch information
1 parent
e490156
commit da5381a
Showing
9 changed files
with
314 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/renderer/components/ExplorationStatus/ExplorationStatus.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React, { FC, useState } from 'react'; | ||
import { styled } from '@/renderer/globalStyles/styled'; | ||
import { ExplorationTimer } from './ExplorationTimer'; | ||
import { | ||
StyledPopup, | ||
StyledPopupContent, | ||
StyledPopupContainer, | ||
} from '@/renderer/components/styles'; | ||
import { GoTelescope } from 'react-icons/go'; | ||
|
||
export const ExplorationStatus: FC = () => { | ||
const [timerDisplay, setTimerDisplay] = useState('00:00'); | ||
|
||
const setTimerDisplayProps = (timerToDisplay: string) => { | ||
setTimerDisplay(timerToDisplay); | ||
}; | ||
|
||
const isShowTimerDisplay = () => { | ||
return timerDisplay !== '00:00'; | ||
}; | ||
|
||
return ( | ||
<StyledPopup | ||
trigger={ | ||
<StyledPopupContainer> | ||
{isShowTimerDisplay() && timerDisplay} | ||
<StyledGoTelescope /> | ||
</StyledPopupContainer> | ||
} | ||
on="click" | ||
position="bottom center" | ||
arrow={false} | ||
repositionOnResize={true} | ||
> | ||
<StyledPopupContent> | ||
<ExplorationTimer setTimerDisplayProps={setTimerDisplayProps} /> | ||
</StyledPopupContent> | ||
</StyledPopup> | ||
); | ||
}; | ||
|
||
const StyledGoTelescope = styled(GoTelescope)` | ||
height: 1.25em; | ||
width: 1.25em; | ||
margin-left: 0.5em; | ||
`; |
135 changes: 135 additions & 0 deletions
135
src/renderer/components/ExplorationStatus/ExplorationTimer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import { Button } from '@/renderer/components/common/Button'; | ||
import { rosClient } from '@/renderer/utils/ros/rosClient'; | ||
import React, { useEffect, FC, useState, ChangeEvent } from 'react'; | ||
import { LabeledInput } from '@/renderer/components/common/LabeledInput'; | ||
import { log } from '@/renderer/logger'; | ||
import { styled } from '@/renderer/globalStyles/styled'; | ||
|
||
interface TimerDisplayProps { | ||
setTimerDisplayProps: (timerDisplay: string) => void; | ||
} | ||
|
||
export const ExplorationTimer: FC<TimerDisplayProps> = ({ | ||
setTimerDisplayProps, | ||
}) => { | ||
const timeDisplayDefault = '00:00'; | ||
|
||
const [duration, setDuration] = useState(2); | ||
const [timeRemaining, setTimeRemaining] = useState(0); | ||
const [isTimerActive, setIsTimerActive] = useState(false); | ||
const [timerDisplay, setTimerDisplay] = useState(timeDisplayDefault); | ||
const [countDownDate, setCountDownDate] = useState(Date.now()); | ||
|
||
const updateDuration = (e: ChangeEvent<HTMLInputElement>) => { | ||
setDuration(Number(e.target.value)); | ||
}; | ||
|
||
const startTimer = () => { | ||
setIsTimerActive(false); | ||
setCountDownDate(Date.now() + duration * 60 * 1000); | ||
setIsTimerActive(true); | ||
setRosExplorationTimer(); | ||
}; | ||
|
||
const stopTimer = () => { | ||
setIsTimerActive(false); | ||
setDuration(0); | ||
setRosExplorationTimer(); | ||
}; | ||
|
||
useEffect(() => { | ||
let interval: ReturnType<typeof setInterval> | undefined; | ||
const intervalMs = 1000; | ||
if (isTimerActive) { | ||
interval = setInterval(() => { | ||
setTimerDisplayProps(getTimeRemaining()); | ||
setTimerDisplay(getTimeRemaining()); | ||
setTimeRemaining(timeRemaining - intervalMs); | ||
}, intervalMs); | ||
} else if (!isTimerActive && timeRemaining !== 0) { | ||
if (interval !== undefined) { | ||
clearInterval(interval); | ||
} | ||
setTimerDisplay(timeDisplayDefault); | ||
setTimerDisplayProps(timeDisplayDefault); | ||
setTimerDisplay(timeDisplayDefault); | ||
setTimeRemaining(0); | ||
} | ||
|
||
const getTimeRemaining = () => { | ||
const total = countDownDate - Date.now(); | ||
|
||
const minutes = Math.floor((total % (1000 * 60 * 60)) / (1000 * 60)); | ||
const seconds = Math.floor((total % (1000 * 60)) / 1000); | ||
|
||
const minutesDiplay = | ||
minutes < 10 ? '0' + minutes.toString() : minutes.toString(); | ||
const secondsDiplay = | ||
seconds < 10 ? '0' + seconds.toString() : seconds.toString(); | ||
|
||
if (total < 0) { | ||
setIsTimerActive(false); | ||
} | ||
return `${minutesDiplay}:${secondsDiplay}`; | ||
}; | ||
return () => clearInterval(interval); | ||
}, [isTimerActive, countDownDate, timeRemaining, setTimerDisplayProps]); | ||
|
||
const setRosExplorationTimer = () => { | ||
rosClient | ||
.callService( | ||
{ | ||
name: `/start_exploration`, | ||
}, | ||
{ timeout: duration * 60 } | ||
) | ||
.catch(log.error); | ||
}; | ||
|
||
return ( | ||
<StyledDiv> | ||
<div> | ||
<LabeledInput | ||
label="Duration of exploration (min)" | ||
value={duration.toString()} | ||
type="number" | ||
onChange={updateDuration} | ||
/> | ||
<StyledDivDuration> | ||
<Button | ||
onClick={startTimer} | ||
btnType="success" | ||
style={{ maxWidth: '185px' }} | ||
> | ||
Start | ||
</Button> | ||
<Button | ||
onClick={stopTimer} | ||
btnType="danger" | ||
style={{ maxWidth: '185px' }} | ||
> | ||
Stop | ||
</Button> | ||
</StyledDivDuration> | ||
</div> | ||
<StyledDivInfo> | ||
<p>Time left</p> | ||
<p>{timerDisplay}</p> | ||
</StyledDivInfo> | ||
</StyledDiv> | ||
); | ||
}; | ||
|
||
const StyledDiv = styled.div` | ||
margin: 1em; | ||
`; | ||
|
||
const StyledDivDuration = styled.div` | ||
display: flex; | ||
column-gap: 20px; | ||
`; | ||
|
||
const StyledDivInfo = styled(StyledDivDuration)` | ||
margin: 8px; | ||
column-gap: 45px; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import React, { memo } from 'react'; | ||
import { StyledPopup } from '@/renderer/components/styles'; | ||
import { useSelector } from 'react-redux'; | ||
import { selectAllGpioPins } from '@/renderer/store/modules/gpioPins'; | ||
import { GpioPin } from './GpioPin'; | ||
import { styled } from '@/renderer/globalStyles/styled'; | ||
import { BsLightbulb } from 'react-icons/bs'; | ||
import { BsLightbulbOff } from 'react-icons/bs'; | ||
|
||
const GpioPinsStatus = () => { | ||
const gpioPins = useSelector(selectAllGpioPins); | ||
const isAGpioPinOn = gpioPins.find((gpioPin) => gpioPin.isOn)?.isOn; | ||
return ( | ||
<StyledPopup | ||
trigger={ | ||
<Container> | ||
{isAGpioPinOn ? <StyledBsLightbulb /> : <StyledBsLightbulbOff />} | ||
</Container> | ||
} | ||
on="click" | ||
position="bottom center" | ||
arrow={false} | ||
repositionOnResize={true} | ||
> | ||
<StyledDiv> | ||
{gpioPins.map((gpioPin) => ( | ||
<GpioPin key={gpioPin.id} gpioPin={gpioPin} /> | ||
))} | ||
</StyledDiv> | ||
</StyledPopup> | ||
); | ||
}; | ||
|
||
const Container = styled.div` | ||
display: flex; | ||
flex-direction: row; | ||
justify-content: center; | ||
align-items: center; | ||
&:hover { | ||
cursor: pointer; | ||
} | ||
`; | ||
|
||
const StyledDiv = styled.div` | ||
display: flex; | ||
flex-direction: row; | ||
align-items: flex-start; | ||
background-color: ${({ theme }) => theme.colors.darkerBackground}; | ||
box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.5); | ||
border-radius: 4px; | ||
min-width: 150px; | ||
`; | ||
|
||
const StyledBsLightbulb = styled(BsLightbulb)` | ||
height: 1.25em; | ||
width: 1.25em; | ||
`; | ||
|
||
const StyledBsLightbulbOff = styled(BsLightbulbOff)` | ||
height: 1.25em; | ||
width: 1.25em; | ||
`; | ||
|
||
export default memo(GpioPinsStatus); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.