Skip to content

Commit

Permalink
Fix confirm pressure/battery dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
Eddasol committed Jan 31, 2024
1 parent 9a8d081 commit fdba120
Show file tree
Hide file tree
Showing 12 changed files with 267 additions and 308 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Robot } from 'models/Robot'
import { useEffect, useState } from 'react'
import { ScheduleMissionWithLocalizationVerificationDialog } from 'components/Displays/ConfirmScheduleDialogs/LocalizationVerification/ScheduleMissionWithLocalizationVerification'
import { useRobotContext } from 'components/Contexts/RobotContext'
import {
InsufficientBatteryDialog,
InsufficientPressureDialog,
} from 'components/Displays/ConfirmScheduleDialogs/InsufficientValueDialogs'

interface ConfirmScheduleDialogProps {
scheduleMissions: () => void
closeDialog: () => void
robotId: string
missionDeckNames: string[]
}

export const ScheduleMissionWithConfirmDialogs = ({
robotId,
missionDeckNames,
scheduleMissions,
closeDialog,
}: ConfirmScheduleDialogProps) => {
const { enabledRobots } = useRobotContext()
const [robot, setRobot] = useState<Robot>()

const isBatteryInsufficient = (currentRobot: Robot) =>
currentRobot.batteryLevel &&
currentRobot.model.batteryWarningThreshold &&
currentRobot.batteryLevel < currentRobot.model.batteryWarningThreshold

const isPressureInsufficient = (currentRobot: Robot) =>
currentRobot.pressureLevel &&
((currentRobot.model.lowerPressureWarningThreshold &&
currentRobot.pressureLevel < currentRobot.model.lowerPressureWarningThreshold) ||
(currentRobot.model.upperPressureWarningThreshold &&
currentRobot.pressureLevel > currentRobot.model.upperPressureWarningThreshold))

useEffect(() => {
setRobot(enabledRobots.find((robot) => robot.id === robotId))
}, [robotId, enabledRobots])

if (!robot) {
return <></>
} else if (isBatteryInsufficient(robot)) {
return <InsufficientBatteryDialog robot={robot} cancel={closeDialog} />
} else if (isPressureInsufficient(robot)) {
return <InsufficientPressureDialog robot={robot} cancel={closeDialog} />
} else {
return (
<ScheduleMissionWithLocalizationVerificationDialog
scheduleMissions={scheduleMissions}
closeDialog={closeDialog}
robotId={robot!.id}
missionDeckNames={missionDeckNames}
/>
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { Button, Dialog, Typography } from '@equinor/eds-core-react'
import { useLanguageContext } from 'components/Contexts/LanguageContext'
import { Robot } from 'models/Robot'
import {
StyledDialog,
VerticalContent,
} from 'components/Displays/ConfirmScheduleDialogs/LocalizationVerification/ScheduleMissionStyles'

enum InsufficientDialogTypes {
battery = 'battery',
pressure = 'pressure',
}

interface SpecificInsufficientDialogProps {
robot: Robot
cancel: () => void
}

interface GenericInsufficientDialogProps {
robot: Robot
dialogType: InsufficientDialogTypes
value: number
lowerLimit?: number
upperLimit?: number
cancel: () => void
}

export const InsufficientBatteryDialog = ({ robot, cancel }: SpecificInsufficientDialogProps) => {
return (
<InsufficientDialog
robot={robot}
dialogType={InsufficientDialogTypes.battery}
value={robot.batteryLevel!}
lowerLimit={robot.model.batteryWarningThreshold}
cancel={cancel}
/>
)
}

export const InsufficientPressureDialog = ({ robot, cancel }: SpecificInsufficientDialogProps) => {
return (
<InsufficientDialog
robot={robot}
dialogType={InsufficientDialogTypes.pressure}
value={robot.pressureLevel!}
lowerLimit={robot.model.lowerPressureWarningThreshold}
upperLimit={robot.model.upperPressureWarningThreshold}
cancel={cancel}
/>
)
}

const InsufficientDialog = ({
robot,
dialogType,
value,
lowerLimit,
upperLimit,
cancel,
}: GenericInsufficientDialogProps) => {
const { TranslateText } = useLanguageContext()

const getValueWithUnit = (value: number): string => {
if (dialogType === InsufficientDialogTypes.battery) {
return `${Math.round(value)}%`
} else if (dialogType === InsufficientDialogTypes.pressure) {
const barToMillibar = 1000
return `${Math.round(value * barToMillibar)}mBar`
} else return value.toString()
}

const getAction = (): string => {
if (dialogType === InsufficientDialogTypes.battery) return 'charge'
else if (dialogType === InsufficientDialogTypes.pressure)
return lowerLimit && value < lowerLimit ? 'pressurize' : 'de-pressurize'
else return ''
}

let warningText = `${TranslateText(`Current ${dialogType} value for`)} ${robot.name} (${
robot.model.type
}) ${TranslateText(`is`)} ${getValueWithUnit(value)}. `

if (lowerLimit && value < lowerLimit) {
warningText += `${TranslateText('This is below recommended lower limit of')} ${getValueWithUnit(lowerLimit)}.`
} else if (upperLimit && value > upperLimit) {
warningText += `${TranslateText('This is above recommended upper limit of')} ${getValueWithUnit(upperLimit)}.`
}

const actionText = `${TranslateText(`Please ${getAction()} the robot`)}.`

return (
<StyledDialog open={true} onClose={cancel}>
<Dialog.Header>
<Typography variant="h5">{TranslateText(`${dialogType} warning`)}</Typography>
</Dialog.Header>
<Dialog.Content>
<VerticalContent>
<Typography>{warningText}</Typography>
<Typography>{actionText}</Typography>
<Dialog.Actions>
<Button variant="outlined" onClick={cancel}>
{TranslateText('Close')}
</Button>
</Dialog.Actions>
</VerticalContent>
</Dialog.Content>
</StyledDialog>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Button, Checkbox, Dialog, Typography } from '@equinor/eds-core-react'
import { DeckMapView } from 'utils/DeckMapView'
import { HorizontalContent, StyledDialog, VerticalContent } from './ScheduleMissionStyles'
import { useLanguageContext } from 'components/Contexts/LanguageContext'
import { Robot } from 'models/Robot'
import { ChangeEvent, useState } from 'react'
import { useInstallationContext } from 'components/Contexts/InstallationContext'

interface ConfirmLocalizationDialogProps {
closeDialog: () => void
scheduleMissions: () => void
robot: Robot
newDeckName: string
}

export const ConfirmLocalizationDialog = ({
closeDialog,
scheduleMissions,
robot,
newDeckName,
}: ConfirmLocalizationDialogProps) => {
const { TranslateText } = useLanguageContext()
const { installationDecks } = useInstallationContext()
const [isCheckboxClicked, setIsCheckboxClicked] = useState<boolean>(false)

const newDeck = installationDecks.find((deck) => deck.deckName === newDeckName)

return (
<StyledDialog open={true} onClose={closeDialog}>
<Dialog.Header>
<Typography variant="h5">{TranslateText('Confirm placement of robot')}</Typography>
</Dialog.Header>
<Dialog.Content>
<VerticalContent>
<Typography>
{`${robot.name} (${robot.model.type}) ${TranslateText(
'needs to be placed on marked position on'
)} ${newDeckName} `}
<b>{TranslateText('before')}</b>
{` ${TranslateText('clicking confirm')}.`}
</Typography>
{newDeck && newDeck.defaultLocalizationPose && (
<DeckMapView deck={newDeck} markedRobotPosition={newDeck.defaultLocalizationPose}></DeckMapView>
)}
<HorizontalContent>
<Checkbox
crossOrigin={undefined}
onChange={(e: ChangeEvent<HTMLInputElement>) => setIsCheckboxClicked(e.target.checked)}
/>
<Typography>
{`${TranslateText('I confirm that')} ${robot.name} (${robot.model.type}) ${TranslateText(
'has been placed on marked position on'
)} `}
<b>{newDeckName}</b>
</Typography>
</HorizontalContent>
</VerticalContent>
</Dialog.Content>
<Dialog.Actions>
<HorizontalContent>
<Button variant="outlined" onClick={closeDialog}>
{TranslateText('Cancel')}
</Button>
<Button onClick={scheduleMissions} disabled={!isCheckboxClicked}>
{TranslateText('Confirm')}
</Button>
</HorizontalContent>
</Dialog.Actions>
</StyledDialog>
)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Robot } from 'models/Robot'
import { useEffect, useState } from 'react'
import { ScheduleMissionChecklistDialog } from './ScheduleMissionChecklistDialog'
import { ConfirmLocalizationDialog } from './ConfirmLocalizationDialog'
import { ConflictingMissionDecksDialog, ConflictingRobotDeckDialog } from './ConflictingLocalizationDialog'
import { UnknownDeckDialog } from './UnknownDeckDialog'
import { useRobotContext } from 'components/Contexts/RobotContext'
Expand Down Expand Up @@ -76,11 +76,11 @@ export const ScheduleMissionWithLocalizationVerificationDialog = ({
return (
<>
{dialogToOpen === DialogTypes.verifyDeck && (
<ScheduleMissionChecklistDialog
<ConfirmLocalizationDialog
closeDialog={closeDialog}
scheduleMissions={scheduleMissions}
robot={selectedRobot!}
missionDeckName={unikMissionDeckNames![0]}
newDeckName={unikMissionDeckNames![0]}
/>
)}
{dialogToOpen === DialogTypes.conflictingMissionDecks && (
Expand Down
Loading

0 comments on commit fdba120

Please sign in to comment.