am-freescan/am-web/src/views/lab/md/fixture/FixtureTrajectory.vue

241 lines
8.2 KiB
Vue

<template>
<Dialog v-model="visible" title="夹具机械臂轨迹文件" width="860px">
<div class="heading"
><strong>{{ fixture?.fixtureName }}</strong
><el-tag :type="current?.id ? 'success' : 'info'">{{
current?.id ? '已关联 · V' + current.versionNo : '未关联'
}}</el-tag></div
>
<p class="hint"
>为此夹具关联机械臂上已录制的轨迹文件。文件保存在所选机械臂,更换关联保留历史记录;关联不代表已完成现场运动验证。</p
>
<el-alert v-if="error" :title="error" type="error" :closable="false" class="gap" />
<div v-loading="loading">
<el-descriptions v-if="current?.id" :column="2" border>
<el-descriptions-item label="当前机械臂轨迹文件" :span="2">{{
current.fileName
}}</el-descriptions-item>
<el-descriptions-item label="所属机器人">{{ current.robotId }}</el-descriptions-item>
<el-descriptions-item label="机械臂">{{ current.deviceId }}</el-descriptions-item>
<el-descriptions-item label="录制时长">{{ current.durationS }} 秒</el-descriptions-item>
<el-descriptions-item label="样本数">{{ current.samples }}</el-descriptions-item>
</el-descriptions>
<el-empty v-else-if="loaded" description="此夹具尚未关联机械臂轨迹文件" :image-size="48" />
<div class="actions">
<el-button
type="primary"
plain
v-hasPermi="['lab:fixture:update']"
:disabled="!editable || !loaded || busy || loading"
@click="pickerVisible = true"
>{{ current?.id ? '选择其他轨迹文件' : '关联已有轨迹文件' }}</el-button
>
<el-button
v-if="current?.id"
type="danger"
link
v-hasPermi="['lab:fixture:update']"
:disabled="!editable || busy || loading || !loaded"
@click="unlink"
>解除关联</el-button
>
<el-button :loading="loading" :disabled="busy" @click="load">刷新关联</el-button>
</div>
<el-alert
v-if="!editable"
title="夹具已停用,仅可查看机械臂轨迹文件关联。"
type="info"
:closable="false"
/>
<div v-if="selection" class="pending">
<strong>本次选择:{{ selection.fileName }}</strong>
<p class="hint"
>机器人 {{ selection.robotId }} · 机械臂 {{ selection.deviceId }} ·
{{ selection.durationS }} 秒</p
>
<el-button
type="primary"
:loading="busy"
:disabled="busy || loading || !loaded || !editable"
v-hasPermi="['lab:fixture:update']"
@click="save"
>{{ current?.id ? '保存并更换关联' : '保存关联' }}</el-button
>
<el-button :disabled="busy" @click="selection = undefined">取消选择</el-button>
</div>
<h4 v-if="history.length">轨迹文件关联历史</h4>
<el-table v-if="history.length" :data="history" size="small" max-height="260">
<el-table-column label="关联版本" width="85"
><template #default="{ row }">V{{ row.versionNo }}</template></el-table-column
>
<el-table-column label="机械臂轨迹文件" prop="fileName" min-width="165" show-overflow-tooltip />
<el-table-column label="机器人 / 机械臂" min-width="200"
><template #default="{ row }"
><div>{{ row.robotId }}</div
><div class="hint">{{ row.deviceId }}</div></template
></el-table-column
>
<el-table-column label="状态" width="90"
><template #default="{ row }">{{
{ LINKED: '当前关联', REPLACED: '已更换', UNLINKED: '已解除' }[row.status] || row.status
}}</template></el-table-column
>
<el-table-column label="关联时间" min-width="165"
><template #default="{ row }">{{ formatDate(row.createdAt) }}</template></el-table-column
>
</el-table>
</div>
<template #footer
><el-button :disabled="busy" @click="visible = false">关闭</el-button></template
>
</Dialog>
<ArmTrajectoryPicker
v-if="visible && pickerVisible"
association
@selected="selected"
@close="pickerVisible = false"
/>
</template>
<script setup lang="ts">
import * as Api from '@/api/lab/master/fixture'
import { formatDate } from '@/utils/formatTime'
import ArmTrajectoryPicker from '@/cmvr/common/views/flow/components/params/ArmTrajectoryPicker.vue'
const api = inject('fixtureTrajectoryApi', {
get: Api.getTrajectory,
bind: Api.bindTrajectory,
unbind: Api.unbindTrajectory
})
const message = useMessage(),
emit = defineEmits(['updated'])
const visible = ref(false),
pickerVisible = ref(false),
loading = ref(false),
busy = ref(false),
loaded = ref(false),
error = ref('')
const fixture = ref<any>(),
current = ref<any>(),
history = ref<any[]>([]),
selection = ref<any>()
const editable = computed(() => fixture.value?.status === 'ENABLED')
let generation = 0
async function load() {
const request = ++generation
loading.value = true
loaded.value = false
error.value = ''
selection.value = undefined
try {
const data = await api.get(fixture.value.id)
if (request !== generation || !visible.value) return
current.value = data.current
history.value = data.history || []
loaded.value = true
} catch (e: any) {
if (request === generation && visible.value)
error.value = e.message || '关联读取失败,请刷新重试'
} finally {
if (request === generation) loading.value = false
}
}
function open(row: any) {
generation++
fixture.value = row
current.value = undefined
history.value = []
selection.value = undefined
pickerVisible.value = false
visible.value = true
load()
}
function selected(_name: string, target: any) {
selection.value = target
pickerVisible.value = false
}
async function save() {
if (busy.value || loading.value || !loaded.value || !editable.value || !selection.value) return
const request = generation
busy.value = true
error.value = ''
try {
await api.bind({
fixtureId: fixture.value.id,
expectedVersion: current.value?.id || null,
robotId: selection.value.robotId,
deviceId: selection.value.deviceId,
fileName: selection.value.fileName
})
if (request !== generation || !visible.value) return
message.success('夹具机械臂轨迹文件已关联')
emit('updated')
await load()
} catch (e: any) {
if (request === generation && visible.value) error.value = e.message || '关联保存失败,请重试'
} finally {
busy.value = false
}
}
async function unlink() {
if (busy.value || loading.value || !loaded.value || !current.value?.id || !editable.value) return
const version = current.value.id,
request = generation
try {
await message.confirm('解除此夹具与当前机械臂轨迹文件的关联?机械臂上的轨迹文件和关联历史保留。')
} catch {
return
}
if (request !== generation || !visible.value || current.value?.id !== version || busy.value)
return
busy.value = true
error.value = ''
try {
await api.unbind({ fixtureId: fixture.value.id, expectedVersion: version })
if (request !== generation || !visible.value) return
emit('updated')
message.success('已解除机械臂轨迹文件关联')
await load()
} catch (e: any) {
if (request === generation && visible.value) error.value = e.message || '解除关联失败,请重试'
} finally {
busy.value = false
}
}
watch(visible, (value) => {
if (!value) {
generation++
pickerVisible.value = false
selection.value = undefined
}
})
onBeforeUnmount(() => generation++)
defineExpose({ open })
</script>
<style scoped>
.heading,
.actions {
display: flex;
align-items: center;
gap: 12px;
flex-wrap: wrap;
}
.hint {
color: var(--el-text-color-secondary);
font-size: 12px;
line-height: 1.7;
overflow-wrap: anywhere;
}
.actions {
margin: 18px 0;
}
.gap {
margin-bottom: 12px;
}
.pending {
padding: 16px;
margin-bottom: 20px;
border: 1px solid var(--el-color-primary-light-7);
background: var(--el-color-primary-light-9);
border-radius: 6px;
}
</style>