forked from SmartBench/CMVR-IOT-UI
105 lines
3.4 KiB
JavaScript
105 lines
3.4 KiB
JavaScript
|
||
import { getFlowById } from '@/api/device/register'
|
||
|
||
const filterNodeRedData = async (id) => {
|
||
let nodeRedData = []
|
||
const data = await getFlowById(id)
|
||
if (data.nodes) {
|
||
nodeRedData = data.nodes
|
||
} else {
|
||
console.error('请求失败')
|
||
}
|
||
return nodeRedData
|
||
}
|
||
|
||
const mapNodeType = (nodeRedType) => {
|
||
const typeMap = {
|
||
'group': 'customGroup', // Node-RED的inject节点转为圆形
|
||
// 'inject': 'circle', // Node-RED的inject节点转为圆形
|
||
// 'debug': 'diamond', // debug节点转为菱形
|
||
// 'function': 'rect' // 函数节点转为矩形
|
||
};
|
||
return typeMap[nodeRedType] || 'CustomNode'; // 默认矩形
|
||
}
|
||
|
||
export const convertNodeRedToLogicFlow = async (id) => {
|
||
const nodeRedData = await filterNodeRedData(id)
|
||
// 节点数据
|
||
const nodes = nodeRedData.map((node) => {
|
||
return {
|
||
id: node.id, // 或生成新ID:`node-${index}`
|
||
type: mapNodeType(node.type), // 映射节点类型
|
||
x: node.x * 2,
|
||
y: node.y * 2,
|
||
text: node.name || node.type, // 使用节点名称或类型作为文本
|
||
children: node.type === 'group' ? node.nodes : null,
|
||
properties: {
|
||
...node,
|
||
state: 'unexecuted',
|
||
}
|
||
};
|
||
});
|
||
|
||
const edges = []
|
||
// 连线数据
|
||
nodeRedData.forEach((item) => {
|
||
if (item.wires && item.wires.length > 0) {
|
||
item.wires.forEach((w, index) => {
|
||
w.forEach((id) => {
|
||
const obj = {
|
||
id: `edge-${item.id}-${id}`, // 或使用唯一标识生成方式
|
||
type: 'bezier',
|
||
sourceNodeId: item.id,
|
||
targetNodeId: id,
|
||
sourceAnchorId: item.id + '_1',
|
||
targetAnchorId: id + '_3'
|
||
}
|
||
edges.push(obj)
|
||
})
|
||
})
|
||
}
|
||
})
|
||
return { nodes, edges }
|
||
}
|
||
|
||
export const recursiveFilter = (data, id, result = [], type = 'customGroup') => {
|
||
data.forEach((item) => {
|
||
if (item.type === type && item.properties.nodes && item.properties.nodes.includes(id)) {
|
||
result.push(item)
|
||
recursiveFilter(data, item.id, result)
|
||
}
|
||
});
|
||
return result
|
||
}
|
||
|
||
export const changeOtherNodeState = (nodes, lf) => {
|
||
const filterArr = ['inject', 'cmvrLoop', 'startCamera', 'getImage', 'stopCamera', 'startVideo', 'stopVideo', 'end']
|
||
const loopNodes = nodes.filter(item => {
|
||
return !filterArr.includes(item.properties.type)
|
||
})
|
||
loopNodes.forEach(l => {
|
||
nodes.forEach(n => {
|
||
if (l.properties.wires[0].includes(n.id)) {
|
||
const { state } = lf.getProperties(n.id)
|
||
if (state !== 'unexecuted') {
|
||
lf.setProperties(l.id, {
|
||
state: 'success'
|
||
})
|
||
}
|
||
}
|
||
|
||
if (l.properties?.wires[1]?.includes(n.id)) {
|
||
const { state } = lf.getProperties(n.id)
|
||
if (state !== 'failed') {
|
||
lf.setProperties(l.id, {
|
||
state: 'running'
|
||
})
|
||
} else {
|
||
lf.setProperties(l.id, {
|
||
state: 'failed'
|
||
})
|
||
}
|
||
}
|
||
})
|
||
})
|
||
} |