-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
conditionally call unsafe/placeLabware command based on the runs/curr…
…entState
- Loading branch information
Showing
1 changed file
with
72 additions
and
42 deletions.
There are no files selected for viewing
114 changes: 72 additions & 42 deletions
114
app/src/resources/modules/hooks/usePlacePlateReaderLid.ts
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 |
---|---|---|
@@ -1,64 +1,94 @@ | ||
import { useRobotControlCommands } from '/app/resources/maintenance_runs' | ||
import { useState } from 'react' | ||
|
||
import { LabwareLocation, type CreateCommand } from '@opentrons/shared-data' | ||
import type { | ||
UseRobotControlCommandsProps, | ||
UseRobotControlCommandsResult, | ||
import { LabwareLocation, type CreateCommand, ModuleLocation } from '@opentrons/shared-data' | ||
Check failure on line 3 in app/src/resources/modules/hooks/usePlacePlateReaderLid.ts GitHub Actions / js checks
|
||
import { | ||
useChainMaintenanceCommands, | ||
} from '/app/resources/maintenance_runs' | ||
import { useRunCurrentState } from '@opentrons/react-api-client' | ||
import { useCurrentRunId } from '../../runs' | ||
import { useDeleteMaintenanceRunMutation, useRunCurrentState } from '@opentrons/react-api-client' | ||
import { useCreateTargetedMaintenanceRunMutation, useCurrentRunId } from '../../runs' | ||
import { MaintenanceRun } from '@opentrons/api-client' | ||
Check failure on line 9 in app/src/resources/modules/hooks/usePlacePlateReaderLid.ts GitHub Actions / js checks
|
||
|
||
interface UsePlacePlateReaderLidResult { | ||
isPlacing: UseRobotControlCommandsResult['isExecuting'] | ||
placeReaderLid: UseRobotControlCommandsResult['executeCommands'] | ||
placeReaderLid: () => Promise<MaintenanceRun> | ||
isPlacing: boolean | ||
} | ||
|
||
export type UsePlacePlateReaderLidProps = Pick< | ||
UseRobotControlCommandsProps, | ||
'pipetteInfo' | 'onSettled' | ||
> | ||
export interface UsePlacePlateReaderLidProps { | ||
Check failure on line 16 in app/src/resources/modules/hooks/usePlacePlateReaderLid.ts GitHub Actions / js checks
|
||
|
||
} | ||
|
||
export function usePlacePlateReaderLid( | ||
props: UsePlacePlateReaderLidProps | ||
): UsePlacePlateReaderLidResult { | ||
const [isPlacing, setIsPlacing] = useState(false) | ||
const { chainRunCommands } = useChainMaintenanceCommands() | ||
const { | ||
mutateAsync: deleteMaintenanceRun, | ||
} = useDeleteMaintenanceRunMutation() | ||
|
||
const runId = useCurrentRunId() | ||
const { data: runCurrentState } = useRunCurrentState(runId) | ||
const estopEngaged = runCurrentState?.data.estopEngaged | ||
const placeLabware = runCurrentState?.data.placeLabwareState?.shouldPlaceDown | ||
const labwareId = runCurrentState?.data.placeLabwareState?.labwareId | ||
const location = runCurrentState?.data.placeLabwareState?.location | ||
const placeLabware = runCurrentState?.data.placeLabwareState ?? null | ||
|
||
const LOAD_PLATE_READER: CreateCommand = { | ||
commandType: 'loadModule' as const, | ||
params: { model: 'absorbanceReaderV1', location: location }, | ||
} | ||
const { | ||
createTargetedMaintenanceRun, | ||
} = useCreateTargetedMaintenanceRunMutation({ | ||
onSuccess: response => { | ||
const runId = response.data.id as string | ||
|
||
const PLACE_READER_LID: CreateCommand = { | ||
commandType: 'unsafe/placeLabware' as const, | ||
params: { | ||
labwareId: labwareId, | ||
location: location, | ||
}, | ||
} | ||
const loadModuleIfSupplied = (): Promise<void> => { | ||
if (placeLabware !== null && placeLabware.shouldPlaceDown) { | ||
const location = placeLabware.location | ||
const labwareId = placeLabware.labwareId | ||
const moduleLocation: ModuleLocation = location as ModuleLocation | ||
const loadModuleCommand = buildLoadModuleCommand(moduleLocation) | ||
const placeLabwareCommand = buildPlaceLabwareCommand(labwareId, location) | ||
console.log({location, labwareId}) | ||
console.log({loadModuleCommand, placeLabwareCommand}) | ||
return chainRunCommands(runId, [loadModuleCommand, placeLabwareCommand], false) | ||
.then(() => Promise.resolve()) | ||
.catch((error: Error) => { | ||
console.error(error.message) | ||
}) | ||
} | ||
return Promise.resolve() | ||
} | ||
|
||
const { executeCommands, isExecuting } = useRobotControlCommands({ | ||
...props, | ||
commands: [LOAD_PLATE_READER, PLACE_READER_LID], | ||
continuePastCommandFailure: true, | ||
loadModuleIfSupplied() | ||
.catch((error: Error) => { | ||
console.error(error.message) | ||
}) | ||
.finally(() => | ||
deleteMaintenanceRun(runId).catch((error: Error) => { | ||
console.error('Failed to delete maintenance run:', error.message) | ||
})) | ||
setIsPlacing(false) | ||
} | ||
}) | ||
|
||
const decideFunction = (): void => { | ||
console.log("DECIDE!") | ||
if (estopEngaged != null && placeLabware) { | ||
console.log("PLACE") | ||
executeCommands() | ||
} else { | ||
console.log("DONT PLACE") | ||
} | ||
const placeReaderLid = (): Promise<MaintenanceRun> => { | ||
setIsPlacing(true) | ||
return createTargetedMaintenanceRun({}) | ||
} | ||
|
||
return { placeReaderLid, isPlacing } | ||
} | ||
|
||
const buildLoadModuleCommand = ( | ||
location: ModuleLocation | ||
): CreateCommand => { | ||
return { | ||
isPlacing: isExecuting, | ||
placeReaderLid: decideFunction, | ||
commandType: 'loadModule' as const, | ||
params: { model: 'absorbanceReaderV1', location: location }, | ||
} | ||
} | ||
|
||
const buildPlaceLabwareCommand = ( | ||
labwaerId: string, | ||
location: LabwareLocation | ||
): CreateCommand => { | ||
return { | ||
commandType: 'unsafe/placeLabware' as const, | ||
params: { labwareId: labwaerId, location: location }, | ||
} | ||
} |