前端页面增加电源控制功能

This commit is contained in:
2026-06-19 21:31:54 +08:00
parent c884dce5bf
commit 6f9d6ed0d6
+441
View File
@@ -0,0 +1,441 @@
<template>
<div class="power-control-page">
<h2>电源控制</h2>
<!-- 电源状态概览 -->
<el-card class="status-card">
<template #header>
<div class="card-header">
<span>电源状态概览</span>
</div>
</template>
<div class="status-grid">
<div class="status-item">
<div class="status-icon" :class="hvsPowerStatus">
<span class="icon-circle"></span>
</div>
<div class="status-info">
<h3>HVS 供电</h3>
<el-tag :type="hvsPowerStatus === 'on' ? 'success' : hvsPowerStatus === 'off' ? 'danger' : 'warning'">
{{ hvsPowerStatus === 'on' ? '已开启' : hvsPowerStatus === 'off' ? '已关闭' : '未知' }}
</el-tag>
</div>
</div>
<div class="status-item">
<div class="status-icon" :class="scannerPowerStatus">
<span class="icon-circle"></span>
</div>
<div class="status-info">
<h3>Scanner 供电</h3>
<el-tag :type="scannerPowerStatus === 'on' ? 'success' : scannerPowerStatus === 'off' ? 'danger' : 'warning'">
{{ scannerPowerStatus === 'on' ? '已开启' : scannerPowerStatus === 'off' ? '已关闭' : '未知' }}
</el-tag>
</div>
</div>
</div>
</el-card>
<!-- HVS电源控制 -->
<el-card class="control-card">
<template #header>
<div class="card-header">
<span>HVS 电源控制</span>
<el-tag :type="hvsPowerStatus === 'on' ? 'success' : hvsPowerStatus === 'off' ? 'danger' : 'warning'">
{{ hvsPowerStatus === 'on' ? '运行中' : hvsPowerStatus === 'off' ? '已停止' : '未知' }}
</el-tag>
</div>
</template>
<div class="control-content">
<div class="control-description">
<p>HVS(高压电源)用于为测偏仪的高压部件提供电力。</p>
<p>GPIO 引脚: <strong>GPIO26</strong></p>
</div>
<div class="control-actions">
<el-button
type="primary"
:loading="hvsLoading"
:disabled="hvsPowerStatus === 'on'"
@click="controlPower('hvs', 'on')"
>
<span class="btn-icon">▸</span> 打开供电
</el-button>
<el-button
type="danger"
:loading="hvsLoading"
:disabled="hvsPowerStatus === 'off'"
@click="controlPower('hvs', 'off')"
>
<span class="btn-icon">■</span> 关闭供电
</el-button>
</div>
</div>
</el-card>
<!-- Scanner电源控制 -->
<el-card class="control-card">
<template #header>
<div class="card-header">
<span>Scanner 电源控制</span>
<el-tag :type="scannerPowerStatus === 'on' ? 'success' : scannerPowerStatus === 'off' ? 'danger' : 'warning'">
{{ scannerPowerStatus === 'on' ? '运行中' : scannerPowerStatus === 'off' ? '已停止' : '未知' }}
</el-tag>
</div>
</template>
<div class="control-content">
<div class="control-description">
<p>Scanner(扫描器)用于获取测量数据。</p>
<p>GPIO 引脚: <strong>GPIO17</strong></p>
</div>
<div class="control-actions">
<el-button
type="primary"
:loading="scannerLoading"
:disabled="scannerPowerStatus === 'on'"
@click="controlPower('scanner', 'on')"
>
<span class="btn-icon">▸</span> 打开供电
</el-button>
<el-button
type="danger"
:loading="scannerLoading"
:disabled="scannerPowerStatus === 'off'"
@click="controlPower('scanner', 'off')"
>
<span class="btn-icon">■</span> 关闭供电
</el-button>
</div>
</div>
</el-card>
<!-- 操作日志 -->
<el-card class="log-card">
<template #header>
<div class="card-header">
<span>操作日志</span>
</div>
</template>
<div class="log-container">
<div
v-for="(log, index) in operationLogs"
:key="index"
class="log-item"
:class="log.type"
>
<span class="log-time">{{ log.time }}</span>
<span class="log-content">{{ log.message }}</span>
</div>
<div v-if="operationLogs.length === 0" class="empty-log">
暂无操作记录
</div>
</div>
</el-card>
</div>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'
// 电源状态
const hvsPowerStatus = ref('unknown')
const scannerPowerStatus = ref('unknown')
const hvsLoading = ref(false)
const scannerLoading = ref(false)
// 操作日志
const operationLogs = ref<Array<{ time: string; message: string; type: string }>>([
{ time: new Date().toLocaleTimeString(), message: '系统已启动', type: 'info' }
])
// 获取电源状态
const fetchPowerStatus = async () => {
try {
// 获取HVS电源状态
const hvsResponse = await axios.post('/powerctrl', {
device: 'hvs',
action: 'status'
})
hvsPowerStatus.value = hvsResponse.data.status
// 获取Scanner电源状态
const scannerResponse = await axios.post('/powerctrl', {
device: 'scanner',
action: 'status'
})
scannerPowerStatus.value = scannerResponse.data.status
} catch (error) {
console.error('获取电源状态失败:', error)
hvsPowerStatus.value = 'unknown'
scannerPowerStatus.value = 'unknown'
addLog('error', '获取电源状态失败')
}
}
// 控制电源
const controlPower = async (device: string, action: string) => {
const deviceName = device === 'hvs' ? 'HVS' : 'Scanner'
const actionText = action === 'on' ? '打开' : '关闭'
// 设置加载状态
if (device === 'hvs') {
hvsLoading.value = true
} else {
scannerLoading.value = true
}
addLog('info', `正在${actionText}${deviceName}电源...`)
try {
const response = await axios.post('/powerctrl', {
device: device,
action: action
})
if (response.data.status === 'success') {
ElMessage.success(`${deviceName}电源${actionText}成功`)
addLog('success', `${deviceName}电源${actionText}成功`)
// 控制成功后立即刷新状态
setTimeout(fetchPowerStatus, 500)
} else {
ElMessage.error('操作失败')
addLog('error', `${deviceName}电源${actionText}失败`)
}
} catch (error) {
console.error('控制电源失败:', error)
ElMessage.error('控制电源失败')
addLog('error', `${deviceName}电源${actionText}失败: ${error}`)
} finally {
// 清除加载状态
if (device === 'hvs') {
hvsLoading.value = false
} else {
scannerLoading.value = false
}
}
}
// 添加日志
const addLog = (type: string, message: string) => {
operationLogs.value.unshift({
time: new Date().toLocaleTimeString(),
message: message,
type: type
})
// 保留最近50条日志
if (operationLogs.value.length > 50) {
operationLogs.value.pop()
}
}
// 页面加载时获取状态
onMounted(() => {
fetchPowerStatus()
})
</script>
<style scoped>
.power-control-page {
padding: 20px;
max-width: 800px;
margin: 0 auto;
min-height: calc(100vh - 84px);
background-color: #f5f7fa;
}
h2 {
margin-bottom: 24px;
color: #1f2f3d;
font-weight: 500;
}
.card-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 16px 20px;
border-bottom: 1px solid #ebeef5;
}
.status-card {
margin-bottom: 20px;
background: white;
border-radius: 8px;
}
.status-grid {
display: flex;
gap: 20px;
padding: 20px;
}
.status-item {
flex: 1;
display: flex;
align-items: center;
gap: 16px;
padding: 20px;
background: #f8f9fa;
border-radius: 8px;
}
.status-icon {
width: 48px;
height: 48px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
}
.status-icon.on {
background: #f0f9eb;
}
.status-icon.off {
background: #fff2f0;
}
.status-icon.unknown {
background: #fffbe6;
}
.icon-circle {
width: 24px;
height: 24px;
border-radius: 50%;
}
.status-icon.on .icon-circle {
background: #67c23a;
box-shadow: 0 0 12px rgba(103, 194, 58, 0.5);
}
.status-icon.off .icon-circle {
background: #f56c6c;
box-shadow: 0 0 12px rgba(245, 108, 108, 0.5);
}
.status-icon.unknown .icon-circle {
background: #e6a23c;
box-shadow: 0 0 12px rgba(230, 162, 60, 0.5);
}
.status-info h3 {
margin-bottom: 8px;
color: #303133;
font-size: 16px;
}
.control-card {
margin-bottom: 20px;
background: white;
border-radius: 8px;
}
.control-content {
padding: 20px;
}
.control-description {
margin-bottom: 20px;
}
.control-description p {
margin-bottom: 8px;
color: #606266;
line-height: 1.6;
}
.control-description strong {
color: #409eff;
}
.control-actions {
display: flex;
gap: 12px;
}
.btn-icon {
margin-right: 6px;
}
.log-card {
background: white;
border-radius: 8px;
}
.log-container {
padding: 20px;
max-height: 300px;
overflow-y: auto;
}
.log-item {
padding: 10px 12px;
margin-bottom: 8px;
border-radius: 4px;
font-size: 14px;
}
.log-item:last-child {
margin-bottom: 0;
}
.log-item.info {
background: #f0f5ff;
color: #409eff;
}
.log-item.success {
background: #f0f9eb;
color: #67c23a;
}
.log-item.error {
background: #fff2f0;
color: #f56c6c;
}
.log-time {
margin-right: 12px;
font-weight: 500;
}
.empty-log {
text-align: center;
color: #909399;
padding: 20px;
}
:deep(.el-card__header) {
padding: 0;
}
:deep(.el-card__body) {
padding: 0;
}
:deep(.el-tag) {
font-weight: 500;
}
@media (max-width: 768px) {
.status-grid {
flex-direction: column;
}
.control-actions {
flex-direction: column;
}
}
</style>