diff --git a/src/api/is/project/index.js b/src/api/is/project/index.js index 1f86dda..710c6fd 100644 --- a/src/api/is/project/index.js +++ b/src/api/is/project/index.js @@ -16,10 +16,11 @@ export const addProject = (data) => { }) } -export const updateProject = (data) => { +export const updateProject = (data, params) => { return request({ url: '/ti/project', method: 'put', + params, data }) } @@ -47,6 +48,11 @@ export const getExecuteRunParams = (params) => { }) } +export const getProjectDetectionItems = () => request({ + url: '/ti/project/detectionItems', + method: 'get' +}) + export const projectExecute = (data) => { return request({ url: '/ti/project/execute', diff --git a/src/utils/flowExecution.js b/src/utils/flowExecution.js index 56bc7f9..e38af3b 100644 --- a/src/utils/flowExecution.js +++ b/src/utils/flowExecution.js @@ -35,7 +35,8 @@ const assertCompatibleDefinitions = (roleGroups) => { const slotsByKey = new Map((existing.deviceSlots || []).map((slot) => [slot.slotKey, slot])) ;(role.deviceSlots || []).forEach((slot) => { const existingSlot = slotsByKey.get(slot.slotKey) - if (existingSlot && String(existingSlot.deviceKind ?? '') !== String(slot.deviceKind ?? '')) { + if (existingSlot && (String(existingSlot.deviceKind ?? '') !== String(slot.deviceKind ?? '') + || existingSlot.requiredDeviceId !== slot.requiredDeviceId)) { throw new Error(`机器人角色“${existing.displayName}”的设备用途“${slot.displayName}”在不同流程中类型不一致`) } if (!existingSlot) existing.deviceSlots.push(clone([slot])[0]) diff --git a/src/utils/flowResources.js b/src/utils/flowResources.js index 5d129d4..6bf9af2 100644 --- a/src/utils/flowResources.js +++ b/src/utils/flowResources.js @@ -1,4 +1,4 @@ -import { getDeviceKindForAction } from '@/utils/robotDevice' +import { getDeviceKindForAction, getRequiredDeviceIdForAction, isNeckDeviceId, NECK_DEVICE_ID } from '@/utils/robotDevice' export const FLOW_SCHEMA_VERSION = 2 export const DEFAULT_ROLE_KEY = 'default_executor' @@ -51,27 +51,39 @@ export const createDefaultRobotRole = (legacyRobotId = '') => ({ ...(legacyRobotId ? { legacyRobotId } : {}), }) -export const createDeviceSlot = (deviceKind, existingSlots = [], displayName = '') => { - const meta = DEVICE_KIND_META[Number(deviceKind)] || { key: 'device', name: '执行设备' } +export const createDeviceSlot = (deviceKind, existingSlots = [], displayName = '', requiredDeviceId) => { + const meta = requiredDeviceId === NECK_DEVICE_ID + ? { key: 'neck', name: '颈部' } + : DEVICE_KIND_META[Number(deviceKind)] || { key: 'device', name: '执行设备' } const usedKeys = new Set(existingSlots.map((slot) => slot.slotKey)) let index = 1 let slotKey = meta.key while (usedKeys.has(slotKey)) slotKey = `${meta.key}_${++index}` - const sameKindCount = existingSlots.filter((slot) => Number(slot.deviceKind) === Number(deviceKind)).length + const sameKindCount = existingSlots.filter((slot) => ( + Number(slot.deviceKind) === Number(deviceKind) && slot.requiredDeviceId === requiredDeviceId + )).length return { slotKey, displayName: displayName || `${meta.name}${sameKindCount ? sameKindCount + 1 : ''}`, deviceKind: deviceKind == null ? null : Number(deviceKind), + ...(requiredDeviceId ? { requiredDeviceId } : {}), required: true, allowReuse: false, } } -export const getStartNode = (graph = globalThis.lf?.getGraphData?.()) => ( +// Resolve the live editor in the function body so the production transform +// preserves the receiver for the optional getGraphData call. +const getLiveGraph = () => { + const editor = globalThis.lf + return editor && typeof editor.getGraphData === 'function' ? editor.getGraphData() : undefined +} + +export const getStartNode = (graph = getLiveGraph()) => ( graph?.nodes?.find((node) => node.type === 'start') ) -export const getResourceRoles = (graph = globalThis.lf?.getGraphData?.()) => ( +export const getResourceRoles = (graph = getLiveGraph()) => ( clone(getStartNode(graph)?.properties?.resourceRoles || []) || [] ) @@ -95,6 +107,17 @@ const resolveLegacyRobotId = (startProperties) => ( const findOrCreateLegacySlot = (role, node, legacySlotByIdentity, legacyDeviceId = '') => { const kind = getDeviceKindForAction(node.properties?.action) + const requiredDeviceId = getRequiredDeviceIdForAction(node.properties?.action) + if (requiredDeviceId) { + const existing = role.deviceSlots.find((slot) => ( + slot.requiredDeviceId === requiredDeviceId && Number(slot.deviceKind) === kind + )) + if (existing) return existing + const slot = createDeviceSlot(kind, role.deviceSlots, '', requiredDeviceId) + slot.managedBy = FLOW_NODE_RESOURCE_OWNER + role.deviceSlots.push(slot) + return slot + } const normalizedDeviceId = normalizedText(legacyDeviceId) const identity = `${role.roleKey}:${kind ?? 'any'}:${normalizedDeviceId || 'default'}` if (legacySlotByIdentity.has(identity)) return legacySlotByIdentity.get(identity) @@ -176,7 +199,10 @@ export const normalizeFlowResourceSchema = (sourceGraph) => { role.required = role.required !== false role.quantity ||= { min: 1, max: 1 } role.deviceSlots = Array.isArray(role.deviceSlots) ? role.deviceSlots : [] - role.deviceSlots.forEach((slot) => delete slot.requiredCapabilities) + role.deviceSlots.forEach((slot) => { + delete slot.requiredCapabilities + if (isNeckDeviceId(slot.requiredDeviceId)) slot.requiredDeviceId = NECK_DEVICE_ID + }) }) startProperties.schemaVersion = FLOW_SCHEMA_VERSION @@ -186,6 +212,7 @@ export const normalizeFlowResourceSchema = (sourceGraph) => { const defaultRole = roles[0] const legacySlotByIdentity = new Map() roles.forEach((role) => role.deviceSlots.forEach((slot) => { + if (slot.requiredDeviceId) return const identity = `${role.roleKey}:${slot.deviceKind ?? 'any'}:${slot.legacyDeviceId || 'default'}` legacySlotByIdentity.set(identity, slot) })) @@ -215,7 +242,7 @@ export const normalizeFlowResourceSchema = (sourceGraph) => { const legacyDeviceId = normalizedText(properties.nodeParams ?.find((param) => param.name === spec.parameterName)?.input) const matchingSlots = targetRole.deviceSlots.filter((slot) => ( - Number(slot.deviceKind) === spec.deviceKind + !slot.requiredDeviceId && Number(slot.deviceKind) === spec.deviceKind && (!legacyDeviceId || !slot.legacyDeviceId || slot.legacyDeviceId === legacyDeviceId) )) let slot = matchingSlots.length === 1 ? matchingSlots[0] : null @@ -228,7 +255,13 @@ export const normalizeFlowResourceSchema = (sourceGraph) => { if (slot) target.deviceBindings.push({ parameterName: spec.parameterName, slotKey: slot.slotKey }) }) } - if (nodeRequiresDevice(node) && !target.deviceSlotKey) { + const requiredDeviceId = getRequiredDeviceIdForAction(properties.action) + const selectedSlot = targetRole.deviceSlots.find((slot) => slot.slotKey === target.deviceSlotKey) + const needsSpecificSlot = requiredDeviceId && ( + selectedSlot?.requiredDeviceId !== requiredDeviceId + || Number(selectedSlot?.deviceKind) !== getDeviceKindForAction(properties.action) + ) + if (nodeRequiresDevice(node) && (!target.deviceSlotKey || needsSpecificSlot)) { const slot = findOrCreateLegacySlot(targetRole, node, legacySlotByIdentity, deviceParam?.input) if (slot) target.deviceSlotKey = slot.slotKey } @@ -484,13 +517,25 @@ export const mergeCopiedResourceRoles = (targetGraph, sourceRoles = [], copiedNo // 设备用途可能只存在于来源流程,粘贴后需要映射到目标流程中可用的 slotKey。 const remapSlot = (slotKey) => { - if (!slotKey || targetRole.deviceSlots?.some((slot) => slot.slotKey === slotKey)) return slotKey + if (!slotKey) return slotKey const sourceSlot = sourceRole?.deviceSlots?.find((slot) => slot.slotKey === slotKey) + if (targetRole.deviceSlots?.some((slot) => ( + slot.slotKey === slotKey && slot.requiredDeviceId === sourceSlot?.requiredDeviceId + ))) return slotKey const sameSlot = targetRole.deviceSlots?.find((slot) => ( sourceSlot && slot.displayName === sourceSlot.displayName && Number(slot.deviceKind) === Number(sourceSlot.deviceKind) + && slot.requiredDeviceId === sourceSlot.requiredDeviceId )) if (sameSlot) return sameSlot.slotKey - const newSlot = sourceSlot ? clone(sourceSlot) : createDeviceSlot(getDeviceKindForAction(node.properties?.action), targetRole.deviceSlots) + const newSlot = sourceSlot ? clone(sourceSlot) : createDeviceSlot( + getDeviceKindForAction(node.properties?.action), + targetRole.deviceSlots, + '', + getRequiredDeviceIdForAction(node.properties?.action), + ) + if (targetRole.deviceSlots.some((slot) => slot.slotKey === newSlot.slotKey)) { + newSlot.slotKey = createResourceKey('device') + } newSlot.managedBy = FLOW_NODE_RESOURCE_OWNER delete newSlot.legacyDeviceId targetRole.deviceSlots.push(newSlot) diff --git a/src/utils/robotDevice.js b/src/utils/robotDevice.js index 21c8aed..d205f03 100644 --- a/src/utils/robotDevice.js +++ b/src/utils/robotDevice.js @@ -12,6 +12,9 @@ export const DEVICE_KIND = Object.freeze({ SPEAKER: 13, }) +export const NECK_DEVICE_ID = 'neck_arm' +export const isNeckDeviceId = (deviceId) => deviceId === NECK_DEVICE_ID || deviceId === 'xiaozhao_head' + /** * 流程动作到所需设备类型的全局映射。 * 作用范围为所有根据 action 判断执行设备的流程模块;未列出的动作视为没有固定设备类型。 @@ -25,10 +28,28 @@ export const DEVICE_KIND_BY_ACTION = Object.freeze({ // 这样运行前的资源校验和设备下拉筛选才能为节点分配正确的机械臂终端。 ARM_ENABLE: DEVICE_KIND.ARM, ARM_DISABLE: DEVICE_KIND.ARM, + ARM_NECK_GET_INFO: DEVICE_KIND.ARM, + ARM_NECK_GET_JOINT_STATE: DEVICE_KIND.ARM, + ARM_NECK_GET_STATE: DEVICE_KIND.ARM, + ARM_NECK_MOVE_J: DEVICE_KIND.ARM, + ARM_NECK_STOP: DEVICE_KIND.ARM, + ARM_NECK_ENABLE: DEVICE_KIND.ARM, + ARM_NECK_DISABLE: DEVICE_KIND.ARM, TOUCH: DEVICE_KIND.ARM, BIO_HEAD_SPEAK_START: DEVICE_KIND.BIO_HEAD, BIO_HEAD_SPEAK_STOP: DEVICE_KIND.BIO_HEAD, BIO_HEAD_SPECIAL_EXPRESSION: DEVICE_KIND.BIO_HEAD, + BIO_HEAD_JOINT_GET_STATE: DEVICE_KIND.BIO_HEAD, + BIO_HEAD_JOINT_MOVE: DEVICE_KIND.BIO_HEAD, + BIO_HEAD_JOINT_HOME: DEVICE_KIND.BIO_HEAD, + BIO_HEAD_JOINT_RECONNECT: DEVICE_KIND.BIO_HEAD, + BIO_HEAD_JOINT_MODE: DEVICE_KIND.BIO_HEAD, + BIO_HEAD_JOINT_CONSTRAINTS: DEVICE_KIND.BIO_HEAD, + BIO_HEAD_JOINT_POSITION_GAIN: DEVICE_KIND.BIO_HEAD, + BIO_HEAD_JOINT_UPDATE_PERIOD: DEVICE_KIND.BIO_HEAD, + BIO_HEAD_JOINT_SET_EXPRESSION: DEVICE_KIND.BIO_HEAD, + BIO_HEAD_JOINT_PLAY_EXPRESSION: DEVICE_KIND.BIO_HEAD, + BIO_HEAD_JOINT_CANCEL_EXPRESSION: DEVICE_KIND.BIO_HEAD, CAMERA_START: DEVICE_KIND.CAMERA, CAMERA_STOP: DEVICE_KIND.CAMERA, CAMERA_GETRGBIMAGE: DEVICE_KIND.CAMERA, @@ -46,6 +67,16 @@ export const DEVICE_KIND_BY_ACTION = Object.freeze({ */ export const getDeviceKindForAction = (action) => DEVICE_KIND_BY_ACTION[action] +export const getRequiredDeviceIdForAction = (action) => ( + typeof action === 'string' && action.startsWith('ARM_NECK_') ? NECK_DEVICE_ID : undefined +) + +export const deviceMatchesSlot = (device, slot) => Boolean(device?.deviceId) + && (slot.deviceKind == null || Number(device.deviceKind) === Number(slot.deviceKind)) + && (!slot.requiredDeviceId || (isNeckDeviceId(slot.requiredDeviceId) + ? isNeckDeviceId(device.deviceId) + : device.deviceId === slot.requiredDeviceId)) + /** * 将机器人设备接口数据转换为 Element Plus 下拉框选项,供全局设备选择器复用。 * 每项以 deviceId 作为值;设备名有效且不同于 ID 时展示“名称 (ID)”,否则仅展示 ID。 diff --git a/src/views/flow/components/RuntimeAssignmentsEditor.vue b/src/views/flow/components/RuntimeAssignmentsEditor.vue index 96a41e3..bce34d0 100644 --- a/src/views/flow/components/RuntimeAssignmentsEditor.vue +++ b/src/views/flow/components/RuntimeAssignmentsEditor.vue @@ -55,7 +55,7 @@