2025-07-22 15:10:57 +08:00
|
|
|
|
import React, {
|
|
|
|
|
createContext,
|
|
|
|
|
useState,
|
|
|
|
|
useContext,
|
|
|
|
|
ReactNode,
|
|
|
|
|
useEffect,
|
|
|
|
|
} from 'react';
|
2025-07-21 15:36:11 +08:00
|
|
|
|
import { Task } from '../types/task';
|
2025-07-22 15:10:57 +08:00
|
|
|
|
import {
|
|
|
|
|
AppConfig,
|
|
|
|
|
LocationOption,
|
|
|
|
|
PayloadOption,
|
|
|
|
|
RobotActionOption,
|
|
|
|
|
} from '../types/config';
|
|
|
|
|
import {
|
|
|
|
|
getConfig,
|
|
|
|
|
getSettings,
|
|
|
|
|
executeTask,
|
|
|
|
|
clearCachedConfig,
|
|
|
|
|
} from '../services/configService';
|
2025-07-21 15:36:11 +08:00
|
|
|
|
|
|
|
|
|
interface TasksContextData {
|
|
|
|
|
tasks: Task[];
|
2025-07-22 15:10:57 +08:00
|
|
|
|
locations: LocationOption[];
|
|
|
|
|
locationsBays: LocationOption[];
|
|
|
|
|
payloads: PayloadOption[];
|
|
|
|
|
robotActions: RobotActionOption[];
|
2025-07-21 15:36:11 +08:00
|
|
|
|
getTaskById: (id: string) => Task | undefined;
|
|
|
|
|
updateTask: (updatedTask: Task) => void;
|
|
|
|
|
runTask: (id: string) => void;
|
2025-07-22 15:10:57 +08:00
|
|
|
|
refreshConfig: () => Promise<void>;
|
|
|
|
|
isConfigLoaded: boolean;
|
2025-07-21 15:36:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const TasksContext = createContext<TasksContextData>({} as TasksContextData);
|
|
|
|
|
|
2025-07-22 15:10:57 +08:00
|
|
|
|
export const TasksProvider: React.FC<{ children: ReactNode }> = ({
|
|
|
|
|
children,
|
|
|
|
|
}) => {
|
|
|
|
|
const [tasks, setTasks] = useState<Task[]>([]);
|
|
|
|
|
const [locations, setLocations] = useState<LocationOption[]>([]);
|
|
|
|
|
const [locationsBays, setLocationsBays] = useState<LocationOption[]>([]);
|
|
|
|
|
const [payloads, setPayloads] = useState<PayloadOption[]>([]);
|
|
|
|
|
const [robotActions, setRobotActions] = useState<RobotActionOption[]>([]);
|
|
|
|
|
const [isConfigLoaded, setIsConfigLoaded] = useState(false);
|
|
|
|
|
|
|
|
|
|
// 组件初始化时加载配置
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const loadConfig = async () => {
|
|
|
|
|
try {
|
|
|
|
|
// 清除缓存以确保加载最新的配置
|
|
|
|
|
await clearCachedConfig();
|
|
|
|
|
|
|
|
|
|
const config = await getConfig();
|
|
|
|
|
if (config) {
|
|
|
|
|
applyConfig(config);
|
|
|
|
|
setIsConfigLoaded(true);
|
|
|
|
|
console.log('成功加载配置,任务数量:', config.tasks?.length || 0);
|
|
|
|
|
} else {
|
|
|
|
|
console.log('没有找到配置文件,使用空数据');
|
|
|
|
|
// 使用空数据而不是mock数据
|
|
|
|
|
applyConfig({
|
|
|
|
|
version: '0.0.0',
|
|
|
|
|
locations: [],
|
|
|
|
|
locationsBays: [],
|
|
|
|
|
payloads: [],
|
|
|
|
|
robotActions: [],
|
|
|
|
|
tasks: [],
|
|
|
|
|
});
|
|
|
|
|
setIsConfigLoaded(false);
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('加载配置失败:', error);
|
|
|
|
|
// 发生错误时也使用空数据
|
|
|
|
|
applyConfig({
|
|
|
|
|
version: '0.0.0',
|
|
|
|
|
locations: [],
|
|
|
|
|
locationsBays: [],
|
|
|
|
|
payloads: [],
|
|
|
|
|
robotActions: [],
|
|
|
|
|
tasks: [],
|
|
|
|
|
});
|
|
|
|
|
setIsConfigLoaded(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
loadConfig();
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const applyConfig = (config: AppConfig) => {
|
|
|
|
|
setTasks(config.tasks || []);
|
|
|
|
|
setLocations(config.locations || []);
|
|
|
|
|
setLocationsBays(config.locationsBays || []);
|
|
|
|
|
setPayloads(config.payloads || []);
|
|
|
|
|
setRobotActions(config.robotActions || []);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const refreshConfig = async () => {
|
|
|
|
|
try {
|
|
|
|
|
// 刷新时也清除缓存
|
|
|
|
|
await clearCachedConfig();
|
|
|
|
|
|
|
|
|
|
const config = await getConfig();
|
|
|
|
|
if (config) {
|
|
|
|
|
applyConfig(config);
|
|
|
|
|
setIsConfigLoaded(true);
|
|
|
|
|
console.log('成功刷新配置,任务数量:', config.tasks?.length || 0);
|
|
|
|
|
} else {
|
|
|
|
|
console.log('刷新配置时没有找到配置文件');
|
|
|
|
|
setIsConfigLoaded(false);
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('刷新配置失败:', error);
|
|
|
|
|
setIsConfigLoaded(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-07-21 15:36:11 +08:00
|
|
|
|
|
|
|
|
|
const getTaskById = (id: string) => {
|
|
|
|
|
return tasks.find(task => task.id === id);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const updateTask = (updatedTask: Task) => {
|
|
|
|
|
setTasks(prevTasks =>
|
2025-07-22 15:10:57 +08:00
|
|
|
|
prevTasks.map(task => (task.id === updatedTask.id ? updatedTask : task)),
|
2025-07-21 15:36:11 +08:00
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2025-07-22 15:10:57 +08:00
|
|
|
|
const runTask = async (id: string) => {
|
|
|
|
|
const task = getTaskById(id);
|
|
|
|
|
if (!task) return;
|
|
|
|
|
|
|
|
|
|
// 更新任务状态为运行中
|
2025-07-21 15:36:11 +08:00
|
|
|
|
setTasks(prevTasks =>
|
2025-07-22 15:10:57 +08:00
|
|
|
|
prevTasks.map(t => (t.id === id ? { ...t, status: 'RUNNING' } : t)),
|
2025-07-21 15:36:11 +08:00
|
|
|
|
);
|
2025-07-22 15:10:57 +08:00
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 获取服务器设置并发送任务执行请求
|
|
|
|
|
const settings = await getSettings();
|
|
|
|
|
if (settings.serverUrl) {
|
|
|
|
|
await executeTask(settings.serverUrl, task.id, {
|
|
|
|
|
name: task.name,
|
|
|
|
|
parameters: task.parameters,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 模拟任务完成(实际项目中应该通过WebSocket或轮询获取任务状态)
|
|
|
|
|
setTimeout(() => {
|
2025-07-21 15:36:11 +08:00
|
|
|
|
setTasks(prevTasks =>
|
2025-07-22 15:10:57 +08:00
|
|
|
|
prevTasks.map(t => (t.id === id ? { ...t, status: 'COMPLETED' } : t)),
|
|
|
|
|
);
|
|
|
|
|
}, 5000);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('任务执行失败:', error);
|
|
|
|
|
// 任务执行失败,更新状态为错误
|
|
|
|
|
setTasks(prevTasks =>
|
|
|
|
|
prevTasks.map(t => (t.id === id ? { ...t, status: 'ERROR' } : t)),
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-07-21 15:36:11 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2025-07-22 15:10:57 +08:00
|
|
|
|
<TasksContext.Provider
|
|
|
|
|
value={{
|
|
|
|
|
tasks,
|
|
|
|
|
locations,
|
|
|
|
|
locationsBays,
|
|
|
|
|
payloads,
|
|
|
|
|
robotActions,
|
|
|
|
|
getTaskById,
|
|
|
|
|
updateTask,
|
|
|
|
|
runTask,
|
|
|
|
|
refreshConfig,
|
|
|
|
|
isConfigLoaded,
|
|
|
|
|
}}
|
|
|
|
|
>
|
2025-07-21 15:36:11 +08:00
|
|
|
|
{children}
|
|
|
|
|
</TasksContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function useTasks() {
|
|
|
|
|
return useContext(TasksContext);
|
|
|
|
|
}
|