+
@@ -109,7 +121,8 @@
-
+
@@ -133,15 +146,17 @@
import SvgIcon from "@/components/SvgIcon";
import taskSteps from "./task-steps.vue";
import { useContainerHeight } from "@/hooks/tableHeight";
-import { getTaskList } from "@/api/inspection/task";
+import { getTaskList, getWaypointByTaskId } from "@/api/inspection/task";
import { getRobotList } from "@/api/inspection/robot";
-import { onMounted } from "vue";
+import { onMounted, onUnmounted } from "vue";
import { addRunTask, getRunTaskList, startRunTask, pauseRunTask, stopRunTask, resumeRunTask, deleteRunTask } from "@/api/inspection/runTask";
import { ElMessage, ElMessageBox } from "element-plus";
import { formatDate } from "@/utils/index";
+import { debounce } from 'lodash'; // 引入 lodash 的防抖函数
const topContainerRef = ref();
const containerHeight = useContainerHeight(topContainerRef);
+const socket = inject('ws');
const taskStatusMap = {
'0': '已完成',
@@ -176,10 +191,11 @@ const getTaskData = () => {
})
}
-const getRobotData = () => {
+const getRobotData = (mapId = null) => {
getRobotList({
pageNum: 1,
pageSize: 10000,
+ currentMapId: mapId
}).then((res) => {
robotList.value = res.rows;
})
@@ -190,6 +206,15 @@ const open = ref(false);
const addTask = () => {
open.value = true;
}
+
+const taskChange = (value) => {
+ const task = taskList.value.find(item => item.id === value)
+ const mapId = task.mapId
+ form.robotId = null;
+ robotList.value = []
+ getRobotData(mapId)
+}
+
const runTaskRef = ref();
const confirmTaskDialog = () => {
runTaskRef.value.validate((valid) => {
@@ -210,18 +235,31 @@ const cancelTaskDialog = () => {
open.value = false;
}
+
+const runingTaskList = ref([])
+/**
+ * 获取任务队列
+ */
const getList = () => {
- loading.value = true;
getRunTaskList({
statusList: ['1', '3', '5'],
}).then((res) => {
if (res.code === 200) {
runTaskList.value = res.rows;
+ runingTaskList.value = res.rows.filter(item => item.status !== '5')
+ for (let i = 0; i < runTaskList.value.length; i++) {
+ getPointByTaskId(runTaskList.value[i].taskId)
+ }
}
- loading.value = false;
- }).catch(() => {
- loading.value = false;
- });
+ })
+}
+
+const pointList_runintTask = ref({})
+const getPointByTaskId = async (taskId) => {
+ const res = await getWaypointByTaskId(taskId)
+ if (res.code === 200) {
+ pointList_runintTask.value[taskId] = res.data
+ }
}
const customColorMethod = (percentage) => {
@@ -293,6 +331,9 @@ const removeTask = async (task) => {
});
}
+/**
+ * 获取历史执行任务
+ */
const getHistoryList = () => {
loading.value = true;
getRunTaskList({
@@ -310,11 +351,73 @@ const getHistoryList = () => {
});
}
+const taskRunningInfo = ref({})
+
+const socketChannelCallbacks = ref({})
+const channel = 'InspectionTaskInstance';
+
+const subscribeDebounced = debounce(async (sub) => {
+ if (!socket) {
+ console.warn(`执行任务订阅失败: socket=${!!socket}`);
+ return;
+ }
+
+ socket.send({
+ type: 'channel_subscription',
+ action: sub ? 'subscribe' : 'unsubscribe',
+ channel,
+ });
+
+ if (sub) {
+ await nextTick();
+ // 清理旧回调
+ if (socketChannelCallbacks.value[channel]) {
+ socket.off(channel, socketChannelCallbacks.value[channel]);
+ console.log('清理旧回调:', channel);
+ }
+
+ const callback = (data) => {
+ if (data !== '500') {
+ if (data.status !== 0) {
+ let activeStep = pointList_runintTask.value[data.taskId].findIndex(item => item.detectItemId === data.itemId) || 0
+ if (data.progress == 100) {
+ activeStep += 1
+ }
+ taskRunningInfo.value[data.insId] = {
+ ...data,
+ activeStep
+ }
+ } else {
+ delete taskRunningInfo.value[data.insId]
+ getList()
+ }
+ console.log('taskRunningInfo', taskRunningInfo.value)
+ } else {
+ // 错误处理:取消订阅后重新订阅
+ subscribeDebounced(false);
+ subscribeDebounced(true);
+ }
+ };
+ socket.on(channel, callback);
+ socketChannelCallbacks.value[channel] = callback;
+ } else {
+ if (socketChannelCallbacks.value[channel]) {
+ socket.off(channel, socketChannelCallbacks.value[channel]);
+ delete socketChannelCallbacks.value[channel];
+ }
+ }
+}, 300);
+
onMounted(() => {
getTaskData();
getRobotData();
getList();
getHistoryList();
+ subscribeDebounced(true)
+})
+
+onUnmounted(() => {
+ subscribeDebounced(false)
})