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 fetching map on mission page #1937

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Comment on lines +59 to +60
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These things should probably be abstracted away, but no need to do it in this PR

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
Loading