Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(app, shared-data): app crashes when using python apiLevel<2.16 with fixed trash #16451

50 changes: 32 additions & 18 deletions app/src/organisms/ErrorRecoveryFlows/hooks/useDeckMapUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useMemo } from 'react'
import {
getDeckDefFromRobotType,
getLoadedLabwareDefinitionsByUri,
getFixedTrashLabwareDefinition,
getModuleDef2,
getPositionFromSlotId,
getSimplestDeckConfigForProtocol,
Expand All @@ -20,6 +21,7 @@ import type {
CutoutConfigProtocolSpec,
LoadedLabware,
RobotType,
LabwareDefinitionsByUri,
} from '@opentrons/shared-data'
import type { ErrorRecoveryFlowsProps } from '..'
import type { UseFailedLabwareUtilsResult } from './useFailedLabwareUtils'
Expand Down Expand Up @@ -50,14 +52,22 @@ export function useDeckMapUtils({
const deckConfig = getSimplestDeckConfigForProtocol(protocolAnalysis)
const deckDef = getDeckDefFromRobotType(robotType)

const labwareDefinitionsByUri = useMemo(
() =>
protocolAnalysis != null
? getLoadedLabwareDefinitionsByUri(protocolAnalysis?.commands)
: null,
[protocolAnalysis]
)

const currentModulesInfo = useMemo(
() =>
getRunCurrentModulesInfo({
runRecord,
deckDef,
protocolAnalysis,
labwareDefinitionsByUri,
}),
[runRecord, deckDef, protocolAnalysis]
[runRecord, deckDef, labwareDefinitionsByUri]
)

const runCurrentModules = useMemo(
Expand All @@ -70,8 +80,8 @@ export function useDeckMapUtils({
)

const currentLabwareInfo = useMemo(
() => getRunCurrentLabwareInfo({ runRecord, protocolAnalysis }),
[runRecord, protocolAnalysis]
() => getRunCurrentLabwareInfo({ runRecord, labwareDefinitionsByUri }),
[runRecord, labwareDefinitionsByUri]
)

const runCurrentLabware = useMemo(
Expand Down Expand Up @@ -182,13 +192,13 @@ interface RunCurrentModuleInfo {
export const getRunCurrentModulesInfo = ({
runRecord,
deckDef,
protocolAnalysis,
labwareDefinitionsByUri,
}: {
protocolAnalysis: UseDeckMapUtilsProps['protocolAnalysis']
runRecord: UseDeckMapUtilsProps['runRecord']
deckDef: DeckDefinition
labwareDefinitionsByUri?: LabwareDefinitionsByUri | null
}): RunCurrentModuleInfo[] => {
if (runRecord == null || protocolAnalysis == null) {
if (runRecord == null || labwareDefinitionsByUri == null) {
return []
} else {
return runRecord.data.modules.reduce<RunCurrentModuleInfo[]>(
Expand All @@ -203,10 +213,6 @@ export const getRunCurrentModulesInfo = ({
lw.location.moduleId === module.id
)

const labwareDefinitionsByUri = getLoadedLabwareDefinitionsByUri(
protocolAnalysis.commands
)

const nestedLabwareDef =
nestedLabware != null
? labwareDefinitionsByUri[nestedLabware.definitionUri]
Expand Down Expand Up @@ -249,21 +255,18 @@ interface RunCurrentLabwareInfo {
// Derive the labware info necessary to render labware on the deck.
export function getRunCurrentLabwareInfo({
runRecord,
protocolAnalysis,
labwareDefinitionsByUri,
}: {
runRecord: UseDeckMapUtilsProps['runRecord']
protocolAnalysis: UseDeckMapUtilsProps['protocolAnalysis']
labwareDefinitionsByUri?: LabwareDefinitionsByUri | null
}): RunCurrentLabwareInfo[] {
if (runRecord == null || protocolAnalysis == null) {
if (runRecord == null || labwareDefinitionsByUri == null) {
return []
} else {
return runRecord.data.labware.reduce((acc: RunCurrentLabwareInfo[], lw) => {
const loc = lw.location
const [slotName, labwareLocation] = getSlotNameAndLwLocFrom(loc, true) // Exclude modules since handled separately.
const labwareDefinitionsByUri = getLoadedLabwareDefinitionsByUri(
protocolAnalysis.commands
)
const labwareDef = labwareDefinitionsByUri[lw.definitionUri]
const labwareDef = getLabwareDefinition(lw, labwareDefinitionsByUri)

if (slotName == null || labwareLocation == null) {
return acc
Expand All @@ -281,6 +284,17 @@ export function getRunCurrentLabwareInfo({
}
}

const getLabwareDefinition = (
labware: LoadedLabware,
protocolLabwareDefinitionsByUri: LabwareDefinitionsByUri
): LabwareDefinition2 => {
if (labware.id === 'fixedTrash') {
return getFixedTrashLabwareDefinition()
} else {
return protocolLabwareDefinitionsByUri[labware.definitionUri]
}
}

// Get the slotName for on deck labware.
export function getSlotNameAndLwLocFrom(
location: LabwareLocation | null,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { describe, it, expect } from 'vitest'
import { getFixedTrashLabwareDefinition } from '../index'
import type { LabwareDefinition2 } from '../..'
import fixedTrashUncasted from '../../../labware/definitions/2/opentrons_1_trash_3200ml_fixed/1.json'

describe('getFixedTrashLabwareDefinition', () => {
it(`should return the fixed trash labware defition`, () => {
expect(getFixedTrashLabwareDefinition()).toEqual(
(fixedTrashUncasted as unknown) as LabwareDefinition2
)
})
})
7 changes: 7 additions & 0 deletions shared-data/js/helpers/getFixedTrashLabwareDefinition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { LabwareDefinition2 } from '..'
import fixedTrashUncasted from '../../labware/definitions/2/opentrons_1_trash_3200ml_fixed/1.json'

export function getFixedTrashLabwareDefinition(): LabwareDefinition2 {
const LabwareDefinition2 = (fixedTrashUncasted as unknown) as LabwareDefinition2
return LabwareDefinition2
}
1 change: 1 addition & 0 deletions shared-data/js/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export * from './getModuleVizDims'
export * from './getVectorDifference'
export * from './getVectorSum'
export * from './getLoadedLabwareDefinitionsByUri'
export * from './getFixedTrashLabwareDefinition'
export * from './getOccludedSlotCountForModule'
export * from './labwareInference'
export * from './getAddressableAreasInProtocol'
Expand Down
Loading