Skip to content

Commit

Permalink
conditionally call unsafe/placeLabware command based on the runs/curr…
Browse files Browse the repository at this point in the history
…entState
  • Loading branch information
vegano1 committed Oct 22, 2024
1 parent 2a7a4b4 commit d47f2a9
Showing 1 changed file with 72 additions and 42 deletions.
114 changes: 72 additions & 42 deletions app/src/resources/modules/hooks/usePlacePlateReaderLid.ts
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

View workflow job for this annotation

GitHub Actions / js checks

All imports in the declaration are only used as types. Use `import type`

Check failure on line 3 in app/src/resources/modules/hooks/usePlacePlateReaderLid.ts

View workflow job for this annotation

GitHub Actions / js checks

All imports in the declaration are only used as types. Use `import type`
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

View workflow job for this annotation

GitHub Actions / js checks

All imports in the declaration are only used as types. Use `import type`

Check failure on line 9 in app/src/resources/modules/hooks/usePlacePlateReaderLid.ts

View workflow job for this annotation

GitHub Actions / js checks

All imports in the declaration are only used as types. Use `import type`

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

View workflow job for this annotation

GitHub Actions / js checks

An empty interface is equivalent to `{}`

Check failure on line 16 in app/src/resources/modules/hooks/usePlacePlateReaderLid.ts

View workflow job for this annotation

GitHub Actions / js checks

An empty interface is equivalent to `{}`

}

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 },
}
}

0 comments on commit d47f2a9

Please sign in to comment.