Skip to content

Commit

Permalink
Basic implementation working, still with frontend errors
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Chong <aaronchongth@gmail.com>
  • Loading branch information
aaronchongth committed Aug 22, 2024
1 parent 6a5ff5a commit ce848a2
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 56 deletions.
58 changes: 35 additions & 23 deletions packages/dashboard/src/components/appbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
useMediaQuery,
} from '@mui/material';
import { styled } from '@mui/system';
import { AlertRequest, FireAlarmTriggerState, TaskFavorite } from 'api-client';
import { AlertRequest, FireAlarmTriggerState, RobotTaskRequest, TaskFavorite } from 'api-client';
import { formatDistance } from 'date-fns';
import React from 'react';
import {
Expand Down Expand Up @@ -194,38 +194,49 @@ export const AppBar = React.memo(({ extraToolbarItems }: AppBarProps): React.Rea
return () => subs.forEach((s) => s.unsubscribe());
}, [rmf]);

const submitTasks = React.useCallback<Required<CreateTaskFormProps>['submitTasks']>(
async (taskRequests, schedule) => {
const dispatchTask = React.useCallback<Required<CreateTaskFormProps>['dispatchTask']>(
async (taskRequest, robotDispatchTarget) => {
if (!rmf) {
throw new Error('tasks api not available');
}
if (!schedule) {
await Promise.all(
taskRequests.map((request) => {
console.debug('submitTask:');
console.debug(request);
return rmf.tasksApi.postDispatchTaskTasksDispatchTaskPost({
type: 'dispatch_task_request',
request,
});
}),
);
if (robotDispatchTarget) {
const robotTask: RobotTaskRequest = {
type: 'robot_task_request',
robot: robotDispatchTarget.robot,
fleet: robotDispatchTarget.fleet,
request: taskRequest,
};
console.debug(`dispatch robot task:`);
console.debug(robotTask);
await rmf.tasksApi.postRobotTaskTasksRobotTaskPost(robotTask);
} else {
const scheduleRequests = taskRequests.map((req) => {
console.debug('schedule task:');
console.debug(req);
console.debug(schedule);
return toApiSchedule(req, schedule);
console.debug('dispatch task:');
console.debug(taskRequest);
await rmf.tasksApi.postDispatchTaskTasksDispatchTaskPost({
type: 'dispatch_task_request',
request: taskRequest,
});
await Promise.all(
scheduleRequests.map((req) => rmf.tasksApi.postScheduledTaskScheduledTasksPost(req)),
);
}
AppEvents.refreshTaskApp.next();
},
[rmf],
);

const scheduleTask = React.useCallback<Required<CreateTaskFormProps>['scheduleTask']>(
async (taskRequest, schedule) => {
if (!rmf) {
throw new Error('tasks api not available');
}
console.debug('schedule task:');
console.debug(taskRequest);
console.debug(schedule);
const scheduleRequest = toApiSchedule(taskRequest, schedule);
await rmf.tasksApi.postScheduledTaskScheduledTasksPost(scheduleRequest);
AppEvents.refreshTaskApp.next();
},
[rmf],
);

//#region 'Favorite Task'
React.useEffect(() => {
if (!rmf) {
Expand Down Expand Up @@ -565,7 +576,8 @@ export const AppBar = React.memo(({ extraToolbarItems }: AppBarProps): React.Rea
favoritesTasks={favoritesTasks}
open={openCreateTaskForm}
onClose={() => setOpenCreateTaskForm(false)}
submitTasks={submitTasks}
dispatchTask={dispatchTask}
scheduleTask={scheduleTask}
submitFavoriteTask={submitFavoriteTask}
deleteFavoriteTask={deleteFavoriteTask}
onSuccess={() => {
Expand Down
59 changes: 44 additions & 15 deletions packages/dashboard/src/components/tasks/task-schedule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import { DayProps } from '@aldabil/react-scheduler/views/Day';
import { MonthProps } from '@aldabil/react-scheduler/views/Month';
import { WeekProps } from '@aldabil/react-scheduler/views/Week';
import { Button, Typography } from '@mui/material';
import { ScheduledTask, ScheduledTaskScheduleOutput as ApiSchedule } from 'api-client';
import {
RobotTaskRequest,
ScheduledTask,
ScheduledTaskScheduleOutput as ApiSchedule,
} from 'api-client';
import React from 'react';
import {
ConfirmationDialog,
Expand Down Expand Up @@ -192,17 +196,45 @@ export const TaskSchedule = () => {
);
};

const submitTasks = React.useCallback<Required<CreateTaskFormProps>['submitTasks']>(
async (taskRequests, schedule) => {
const dispatchTask = React.useCallback<Required<CreateTaskFormProps>['dispatchTask']>(
async (taskRequest, robotDispatchTarget) => {
if (!rmf) {
throw new Error('tasks api not available');
}
if (robotDispatchTarget) {
const robotTask: RobotTaskRequest = {
type: 'robot_task_request',
robot: robotDispatchTarget.robot,
fleet: robotDispatchTarget.fleet,
request: taskRequest,
};
console.debug(`dispatch robot task:`);
console.debug(robotTask);
await rmf.tasksApi.postRobotTaskTasksRobotTaskPost(robotTask);
} else {
console.debug('dispatch task:');
console.debug(taskRequest);
await rmf.tasksApi.postDispatchTaskTasksDispatchTaskPost({
type: 'dispatch_task_request',
request: taskRequest,
});
}
AppEvents.refreshTaskApp.next();
},
[rmf],
);

const scheduleTask = React.useCallback<Required<CreateTaskFormProps>['scheduleTask']>(
async (taskRequest, schedule) => {
if (!rmf) {
throw new Error('tasks api not available');
}

if (!schedule || !currentScheduleTask) {
throw new Error('No schedule or task selected for submission.');
if (!currentScheduleTask) {
throw new Error('No schedule task selected for submission.');
}

const scheduleRequests = taskRequests.map((req) => toApiSchedule(req, schedule));
const scheduleRequest = toApiSchedule(taskRequest, schedule);

let exceptDate: string | undefined = undefined;
if (eventScope === EventScopes.CURRENT) {
Expand All @@ -212,14 +244,10 @@ export const TaskSchedule = () => {
console.debug(`Editing schedule id ${currentScheduleTask.id}`);
}

await Promise.all(
scheduleRequests.map((req) =>
rmf.tasksApi.updateScheduleTaskScheduledTasksTaskIdUpdatePost(
currentScheduleTask.id,
req,
exceptDate,
),
),
await rmf.tasksApi.updateScheduleTaskScheduledTasksTaskIdUpdatePost(
currentScheduleTask.id,
scheduleRequest,
exceptDate,
);

setEventScope(EventScopes.CURRENT);
Expand Down Expand Up @@ -337,7 +365,8 @@ export const TaskSchedule = () => {
setEventScope(EventScopes.CURRENT);
AppEvents.refreshTaskSchedule.next();
}}
submitTasks={submitTasks}
dispatchTask={dispatchTask}
scheduleTask={scheduleTask}
onSuccess={() => {
setOpenCreateTaskForm(false);
showAlert('success', 'Successfully created task');
Expand Down
3 changes: 2 additions & 1 deletion packages/react-components/lib/tasks/create-task.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ type Story = StoryObj<typeof CreateTaskForm>;

export const CreateTask: Story = {
args: {
submitTasks: async () => new Promise((res) => setTimeout(res, 500)),
dispatchTask: async () => new Promise((res) => setTimeout(res, 500)),
scheduleTask: async () => new Promise((res) => setTimeout(res, 500)),
cleaningZones: ['test_zone_0', 'test_zone_1'],
patrolWaypoints: ['test_waypoint_0', 'test_waypoint_1'],
pickupPoints: { test_waypoint_0: 'test_waypoint_0' },
Expand Down
Loading

0 comments on commit ce848a2

Please sign in to comment.