Skip to content

Commit

Permalink
Split configuration and definition, only handle configurations in res…
Browse files Browse the repository at this point in the history
…ource manager level

Signed-off-by: Aaron Chong <aaronchongth@gmail.com>
  • Loading branch information
aaronchongth committed Jun 19, 2024
1 parent d452aff commit 7f7eb62
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 144 deletions.
3 changes: 2 additions & 1 deletion packages/dashboard/src/components/appbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export const AppBar = React.memo(({ extraToolbarItems }: AppBarProps): React.Rea
const location = useLocation();
const tabValue = React.useMemo(() => locationToTabValue(location.pathname), [location]);
const logoResourcesContext = React.useContext(ResourcesContext)?.logos;
const taskResourcesContext = React.useContext(ResourcesContext)?.tasks;
const [anchorEl, setAnchorEl] = React.useState<HTMLElement | null>(null);
const { authenticator } = React.useContext(AppConfigContext);
const profile = React.useContext(UserProfileContext);
Expand Down Expand Up @@ -617,7 +618,7 @@ export const AppBar = React.memo(({ extraToolbarItems }: AppBarProps): React.Rea
{openCreateTaskForm && (
<CreateTaskForm
user={username ? username : 'unknown user'}
supportedTasks={resourceManager?.supportedTasks}
tasksToDisplay={taskResourcesContext?.tasks}
patrolWaypoints={waypointNames}
cleaningZones={cleaningZoneNames}
pickupZones={resourceManager?.pickupZones}
Expand Down
8 changes: 4 additions & 4 deletions packages/dashboard/src/components/tasks/task-schedule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const disablingCellsWithoutEvents = (

export const TaskSchedule = () => {
const rmf = React.useContext(RmfAppContext);
const resourceManager = React.useContext(ResourcesContext);
const taskResourcesContext = React.useContext(ResourcesContext)?.tasks;
const { showAlert } = React.useContext(AppControllerContext);
const profile = React.useContext(UserProfileContext);

Expand Down Expand Up @@ -137,7 +137,7 @@ export const TaskSchedule = () => {
return tasks.flatMap((t: ScheduledTask) =>
t.schedules.flatMap<ProcessedEvent>((s: ApiSchedule) => {
const events = scheduleToEvents(params.start, params.end, s, t, getEventId, () =>
getScheduledTaskTitle(t, resourceManager?.supportedTasks),
getScheduledTaskTitle(t, taskResourcesContext?.tasks),
);
events.forEach((ev) => {
eventsMap.current[Number(ev.event_id)] = t;
Expand All @@ -147,7 +147,7 @@ export const TaskSchedule = () => {
}),
);
},
[rmf, resourceManager],
[rmf, taskResourcesContext],
);

const CustomCalendarEditor = ({ scheduler, value, onChange }: CustomCalendarEditorProps) => {
Expand Down Expand Up @@ -320,7 +320,7 @@ export const TaskSchedule = () => {
{openCreateTaskForm && (
<CreateTaskForm
user={username ? username : 'unknown user'}
supportedTasks={resourceManager?.supportedTasks}
tasksToDisplay={taskResourcesContext?.tasks}
patrolWaypoints={waypointNames}
cleaningZones={cleaningZoneNames}
pickupPoints={pickupPoints}
Expand Down
29 changes: 22 additions & 7 deletions packages/dashboard/src/managers/resource-manager-tasks.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
import { TaskDefinition } from 'react-components';
import { getTaskDefinition, TaskDefinition } from 'react-components';

export interface TaskResource {
task_definition_id: string;
display_name?: string;
}

export class TaskResourceManager {
supportedTasks: Record<string, TaskDefinition>;
tasks: TaskDefinition[];

constructor(supportedTasks: TaskDefinition[] | undefined) {
this.supportedTasks = {};
if (supportedTasks) {
for (const t of supportedTasks) {
this.supportedTasks[t.taskDefinitionId] = t;
constructor(taskResources: TaskResource[]) {
this.tasks = [];
for (const taskResource of taskResources) {
const definition = getTaskDefinition(taskResource.task_definition_id);
if (!definition) {
console.error(
`Invalid tasks configured for dashboard: [${taskResource.task_definition_id}]`,
);
continue;
}

if (taskResource.display_name !== undefined) {
definition.taskDisplayName = taskResource.display_name;
}

this.tasks.push(definition);
}
}
}
34 changes: 19 additions & 15 deletions packages/dashboard/src/managers/resource-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ import Debug from 'debug';
import { DispenserResourceManager, RawDispenserResource } from './resource-manager-dispensers';
import { LogoResource, LogoResourceManager } from './resource-manager-logos';
import { RobotResource, RobotResourceManager } from './resource-manager-robots';
import {
DefaultPatrolTaskDefinition,
DefaultDeliveryTaskDefinition,
DefaultComposeCleanTaskDefinition,
TaskDefinition,
DefaultCustomComposeTaskDefinition,
} from 'react-components';
import { TaskResource, TaskResourceManager } from './resource-manager-tasks';

const debug = Debug('ResourceManager');
const ResourceFile = 'resources/main.json';
Expand All @@ -25,7 +19,7 @@ export interface ResourceConfigurationsType {
attributionPrefix?: string;
cartIds?: string[];
loggedInDisplayLevel?: string;
supportedTasks?: TaskDefinition[];
allowedTasks?: TaskResource[];
}

export default class ResourceManager {
Expand All @@ -40,7 +34,7 @@ export default class ResourceManager {
attributionPrefix?: string;
cartIds?: string[];
loggedInDisplayLevel?: string;
supportedTasks?: TaskDefinition[];
tasks: TaskResourceManager;

/**
* Gets the default resource manager using the embedded resource file (aka "assets/resources/main.json").
Expand All @@ -62,6 +56,22 @@ export default class ResourceManager {
constructor(resources: ResourceConfigurationsType) {
this.robots = new RobotResourceManager(resources.robots || {});
this.logos = new LogoResourceManager(resources.logos || {});
this.tasks = new TaskResourceManager(
resources.allowedTasks || [
{
task_definition_id: 'patrol',
display_name: 'Patrol',
},
{
task_definition_id: 'delivery',
display_name: 'Delivery',
},
{
task_definition_id: 'clean',
display_name: 'Clean',
},
],
);
if (resources.dispensers) {
this.dispensers = new DispenserResourceManager(resources.dispensers);
}
Expand All @@ -80,12 +90,6 @@ export default class ResourceManager {
this.attributionPrefix = resources.attributionPrefix || 'OSRC-SG';
this.cartIds = resources.cartIds || [];
this.loggedInDisplayLevel = resources.loggedInDisplayLevel;
this.supportedTasks = resources.supportedTasks || [
DefaultPatrolTaskDefinition,
DefaultDeliveryTaskDefinition,
DefaultComposeCleanTaskDefinition,
DefaultCustomComposeTaskDefinition,
];
}
}

Expand Down
Loading

0 comments on commit 7f7eb62

Please sign in to comment.