city_vocational_college/src/views/resource/device/index.vue

180 lines
9.1 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<div class="app-container resource-device-page">
<el-form :inline="true" :model="query">
<el-form-item label="机器人">
<el-select
v-model="query.robotId"
filterable
clearable
:loading="robotLoading"
placeholder="全部机器人"
style="width: 280px"
@change="handleRobotChange"
>
<el-option label="全部机器人" value="" />
<el-option
v-for="robot in robots"
:key="robot.robotId"
:label="robotOptionLabel(robot)"
:value="robot.robotId"
/>
</el-select>
</el-form-item>
<el-form-item label="设备用途"><el-input v-model="query.deviceRole" clearable @keyup.enter="search" /></el-form-item>
<el-form-item label="在线状态">
<el-select v-model="onlineFilter" clearable style="width: 120px" @change="search">
<el-option label="在线" value="online" /><el-option label="离线" value="offline" />
</el-select>
</el-form-item>
<el-form-item><el-button type="primary" icon="Search" @click="search">查询</el-button><el-button icon="Refresh" @click="reset">重置</el-button></el-form-item>
</el-form>
<div class="device-table-wrap">
<el-table v-loading="loading" :data="rows" row-key="id" height="100%">
<el-table-column label="机器人" prop="robotId" min-width="200" show-overflow-tooltip />
<el-table-column label="设备" min-width="210">
<template #default="{ row }"><strong>{{ row.deviceName || row.deviceId }}</strong><div class="secondary">{{ row.deviceId }}</div></template>
</el-table-column>
<el-table-column label="类型" width="120"><template #default="{ row }">{{ kindName(row.deviceKind) }}</template></el-table-column>
<el-table-column label="用途" prop="deviceRole" min-width="150"><template #default="{ row }">{{ row.deviceRole || '未设置' }}</template></el-table-column>
<el-table-column label="状态" width="100"><template #default="{ row }"><el-tag :type="row.onlineStatus === '1' ? 'success' : 'info'">{{ row.onlineStatus === '1' ? '在线' : '离线' }}</el-tag></template></el-table-column>
<el-table-column label="启用" width="90"><template #default="{ row }">{{ row.enabled === '0' ? '否' : '是' }}</template></el-table-column>
<el-table-column label="健康" width="100"><template #default="{ row }"><el-tag :type="row.hasError === '1' ? 'danger' : 'success'">{{ row.hasError === '1' ? '故障' : '正常' }}</el-tag></template></el-table-column>
<el-table-column label="最后发现" prop="lastSeenTime" width="175" />
<el-table-column label="操作" width="170" fixed="right">
<template #default="{ row }">
<el-button v-if="controlPath(row)" link type="primary" icon="VideoPlay" :disabled="row.onlineStatus !== '1'" @click="openTeach(row)">示教</el-button>
<el-button link type="primary" icon="Setting" @click="openConfig(row)">配置</el-button>
</template>
</el-table-column>
</el-table>
</div>
<pagination v-show="total > 0" :total="total" v-model:page="query.pageNum" v-model:limit="query.pageSize" @pagination="load" />
<el-drawer v-model="configOpen" title="设备配置" size="min(720px, 94vw)" destroy-on-close>
<el-form :model="active" label-width="90px">
<el-form-item label="设备ID"><span class="identity">{{ active.deviceId }}</span></el-form-item>
<el-form-item label="设备名称"><el-input v-model="active.deviceName" /></el-form-item>
<el-form-item label="设备用途"><el-input v-model="active.deviceRole" placeholder="例如:前摄像头、左机械臂、环境麦克风" /></el-form-item>
<el-form-item label="启用"><el-switch v-model="active.enabled" active-value="1" inactive-value="0" /></el-form-item>
<el-form-item label="备注"><el-input v-model="active.remark" type="textarea" /></el-form-item>
</el-form>
<template #footer><el-button @click="configOpen = false">取消</el-button><el-button type="primary" :loading="saving" @click="saveConfig">保存</el-button></template>
</el-drawer>
</div>
</template>
<script setup name="ResourceDevice">
import { onActivated, onMounted, reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { listResourceDevices, updateResourceDevice } from '@/api/resource/device'
import { listResourceRobots } from '@/api/resource/robot'
const kindNames = { 1: '移动底盘', 2: '机械臂', 3: '电池', 4: '仿生头', 5: '摄像头', 6: 'CAN总线', 7: '灵巧手', 8: '夹爪', 9: '麦克风', 10: '电机', 11: '电机系统', 12: '机器人本体', 13: '扬声器' }
const router = useRouter()
const loading = ref(false), robotLoading = ref(false), saving = ref(false), rows = ref([]), robots = ref([]), total = ref(0), configOpen = ref(false), onlineFilter = ref('')
const query = reactive({ pageNum: 1, pageSize: 20, robotId: '', deviceRole: '', onlineOnly: false })
const active = reactive({})
/** 按后端设备种类显示名称,保留未知编号便于排查新增硬件类型。 */
const kindName = value => kindNames[value] || `未知(${value ?? '-'})`
/** 使用名称和永久 ID 区分同名机器人,缺少名称时仍能选择已登记资源。 */
const robotOptionLabel = robot => robot.robotName
? `${robot.robotName}(${robot.robotId})`
: robot.robotId
/** 仅为已有示教页面的设备种类返回路由段,其他硬件不提供操作入口。 */
const controlPath = device => ({
2: 'mechanical_arm', 4: 'head', 5: 'camera', 7: 'hand', 9: 'microphone', 13: 'speaker',
})[device.deviceKind]
/**
* 打开所选设备的现有示教页,通过查询参数保留机器人和设备的永久身份。
* @param {Object} device 资源列表中的设备记录;不支持示教的种类直接返回。
*/
function openTeach(device) {
const path = controlPath(device)
if (!path) return
router.push({
path: `/device/${path}/${device.id}`,
query: { robotId: device.robotId, deviceId: device.deviceId },
})
}
/**
* 按当前条件刷新设备分页;在线筛选交给接口,离线筛选兼容接口现有返回。
* @returns {Promise<void>} 刷新列表和分页总数,失败时由请求层提示并释放加载状态。
*/
async function load() {
loading.value = true
try {
const params = { ...query, onlineOnly: onlineFilter.value === 'online' }
const res = await listResourceDevices(params)
const data = res.rows || res.data || []
rows.value = onlineFilter.value === 'offline' ? data.filter(item => item.onlineStatus !== '1') : data
total.value = onlineFilter.value === 'offline' ? rows.value.length : (res.total ?? rows.value.length)
} finally { loading.value = false }
}
/**
* 加载可筛选的已登记机器人,过滤缺少永久 ID 的旧记录以避免无效查询。
* @returns {Promise<void>} 更新下拉选项;请求失败清空选项,并始终结束加载状态。
*/
async function loadRobots() {
robotLoading.value = true
try {
const res = await listResourceRobots({ pageNum: 1, pageSize: 1000 })
robots.value = (res.rows || res.data || []).filter(robot => robot?.robotId)
} catch (error) {
robots.value = []
console.error('加载机器人列表失败:', error)
} finally {
robotLoading.value = false
}
}
/** 修改筛选条件后从首页重新查询,避免沿用上一条件的无效页码。 */
function search() { query.pageNum = 1; load() }
/** 机器人切换与其他筛选共用查询入口,保持分页重置规则一致。 */
function handleRobotChange() { search() }
/** 清空资源和状态筛选并重新查询,保留用户当前的每页条数。 */
function reset() { Object.assign(query, { pageNum: 1, robotId: '', deviceRole: '', onlineOnly: false }); onlineFilter.value = ''; load() }
/**
* 将设备记录复制到编辑抽屉,避免未保存的输入直接修改列表展示。
* @param {Object} row 待配置的设备资源记录。
*/
function openConfig(row) {
Object.assign(active, JSON.parse(JSON.stringify(row)))
configOpen.value = true
}
/**
* 按资源记录 ID 保存用途和启用配置,成功后关闭抽屉并重新读取列表。
* @returns {Promise<void>} 保存失败保留编辑内容,且始终释放提交状态。
*/
async function saveConfig() {
saving.value = true
try {
await updateResourceDevice(active.id, active)
ElMessage.success('设备配置已保存')
configOpen.value = false
load()
} finally { saving.value = false }
}
onMounted(() => {
load()
loadRobots()
})
onActivated(() => {
load()
if (!robots.value.length) loadRobots()
})
</script>
<style scoped>
.resource-device-page { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.resource-device-page > .el-form { flex: 0 0 auto; }
.device-table-wrap { flex: 1 1 auto; min-height: 0; overflow: hidden; }
.resource-device-page :deep(.pagination-container) { flex: 0 0 auto; padding: 16px 0 0; }
.secondary { color: var(--el-text-color-secondary); font-size: 12px; margin-top: 3px; }
.identity { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; }
</style>