Skip to content

Commit

Permalink
Fix fetching map on mission page
Browse files Browse the repository at this point in the history
  • Loading branch information
mrica-equinor committed Jan 8, 2025
1 parent 662842a commit d72a6c7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
3 changes: 2 additions & 1 deletion backend/api/Services/MissionDefinitionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ private IQueryable<MissionDefinition> GetMissionDefinitionsWithSubModels(
|| accessibleInstallationCodes.Result.Contains(
m.InspectionArea.Installation.InstallationCode.ToUpper()
)
);
)
.Include(missionDefinition => missionDefinition.Map);
return readOnly ? query.AsNoTracking() : query.AsTracking();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { BackendAPICaller } from 'api/ApiCaller'
import { TaskStatus } from 'models/Task'
import { MapCompass } from 'utils/MapCompass'
import { useRobotContext } from 'components/Contexts/RobotContext'
import { useQuery } from '@tanstack/react-query'

interface MissionProps {
mission: Mission
Expand Down Expand Up @@ -48,16 +49,32 @@ export const MissionMapView = ({ mission }: MissionProps) => {

const imageObjectURL = useRef<string>('')

const fetchMapInfo = (mission: Mission) => {
const map = useQuery({
queryKey: [mission.missionId],
queryFn: async () => {
const missionDefinition = await BackendAPICaller.getMissionDefinitionById(mission.missionId!)
return missionDefinition.map
},
retryDelay: 60 * 1000,
staleTime: 60 * 1000,
enabled: mission.missionId !== undefined,
})
return map.data
}

const mapInfo = fetchMapInfo(mission)

const updateMap = useCallback(() => {
const context = mapCanvas.getContext('2d')
if (!context || !mission.map) {
if (!context || !mapInfo) {
return
}
context.clearRect(0, 0, mapCanvas.width, mapCanvas.height)
context.drawImage(mapImage, 0, 0)
placeTagsInMap(mission.tasks, mission.map!, mapCanvas, currentTaskOrder)
if (missionRobot?.pose && mission.map) {
placeRobotInMap(mission.map, mapCanvas, missionRobot.pose)
placeTagsInMap(mission.tasks, mapInfo, mapCanvas, currentTaskOrder)
if (missionRobot?.pose && mapInfo) {
placeRobotInMap(mapInfo, mapCanvas, missionRobot.pose)
}
}, [currentTaskOrder, mapCanvas, mapImage, mission, missionRobot?.pose])

Expand All @@ -76,12 +93,12 @@ export const MissionMapView = ({ mission }: MissionProps) => {
[mission.tasks]
)

let displayedMapName = mission.map?.mapName.split('.')[0].replace(/[^0-9a-z-A-Z ]/g, ' ')
let displayedMapName = mapInfo?.mapName.split('.')[0].replace(/[^0-9a-z-A-Z ]/g, ' ')
displayedMapName = displayedMapName ? displayedMapName.charAt(0).toUpperCase() + displayedMapName.slice(1) : ' '

useEffect(() => {
if (mission.map?.mapName) {
BackendAPICaller.getMap(mission.installationCode!, mission.map?.mapName)
if (mapInfo?.mapName) {
BackendAPICaller.getMap(mission.installationCode!, mapInfo.mapName)
.then((imageBlob) => {
imageObjectURL.current = URL.createObjectURL(imageBlob)
})
Expand All @@ -107,7 +124,7 @@ export const MissionMapView = ({ mission }: MissionProps) => {
} else {
imageObjectURL.current = NoMap
}
}, [mission.installationCode, mission.id, mission.map?.mapName])
}, [mission.installationCode, mission.id, mapInfo?.mapName])

useEffect(() => {
if (mission.isCompleted) {
Expand Down
3 changes: 1 addition & 2 deletions frontend/src/models/Mission.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { MapMetadata } from './MapMetadata'
import { InspectionArea } from './InspectionArea'
import { Robot, placeholderRobot } from './Robot'
import { Task } from './Task'
Expand Down Expand Up @@ -41,8 +40,8 @@ export interface Mission {
endTime?: Date
estimatedDuration?: number
tasks: Task[]
map?: MapMetadata
}

export const placeholderMission: Mission = {
id: 'placeholderId',
name: 'placeholderMission',
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/models/MissionDefinition.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { InspectionArea } from './InspectionArea'
import { Mission } from './Mission'
import { MapMetadata } from './MapMetadata'

export interface MissionDefinition {
id: string
Expand All @@ -11,6 +12,7 @@ export interface MissionDefinition {
inspectionArea?: InspectionArea
isDeprecated: boolean
sourceId: string
map?: MapMetadata
}

export interface PlantInfo {
Expand Down

0 comments on commit d72a6c7

Please sign in to comment.