增加通过前端页面控制扫描开始和停止
This commit is contained in:
@@ -777,3 +777,36 @@ async def power_control(power_data: dict):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"电源控制时发生错误: {e}")
|
print(f"电源控制时发生错误: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
|
||||||
|
# 扫描控制API - 控制Scanner的启动与停止
|
||||||
|
@app.post("/api/scanctrl")
|
||||||
|
async def scan_control(scan_data: dict):
|
||||||
|
"""
|
||||||
|
控制Scanner的启动与停止
|
||||||
|
参数:
|
||||||
|
- action: 操作,可选值: 'start' (启动) 或 'stop' (停止)
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
action = scan_data.get("action")
|
||||||
|
|
||||||
|
# 验证操作类型
|
||||||
|
if action not in ['start', 'stop']:
|
||||||
|
raise HTTPException(status_code=400, detail="action 必须是 'start' 或 'stop'")
|
||||||
|
|
||||||
|
try:
|
||||||
|
path = "/home/admin/.config/cpy/config.json"
|
||||||
|
with open(path, "r") as f:
|
||||||
|
config = json.load(f)
|
||||||
|
sn = config.get('sn', "")
|
||||||
|
if sn == "":
|
||||||
|
raise HTTPException(status_code=400, detail="配置文件中缺少 sn 字段")
|
||||||
|
except FileNotFoundError:
|
||||||
|
raise HTTPException(status_code=400, detail="配置文件不存在")
|
||||||
|
|
||||||
|
mqtt_client.publish(f"v1/cpy/{sn}/control", json.dumps({"method": "scan_control", "action": action}))
|
||||||
|
return {"action": action, "status": "success"}
|
||||||
|
except HTTPException:
|
||||||
|
raise
|
||||||
|
except Exception as e:
|
||||||
|
print(f"扫描控制时发生错误: {e}")
|
||||||
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="power-control-page">
|
<div class="power-control-page">
|
||||||
<h2>电源控制</h2>
|
<h2>电源与扫描控制</h2>
|
||||||
|
|
||||||
<!-- 电源状态概览 -->
|
<!-- 电源状态概览 -->
|
||||||
<el-card class="status-card">
|
<el-card class="status-card">
|
||||||
@@ -113,6 +113,49 @@
|
|||||||
</div>
|
</div>
|
||||||
</el-card>
|
</el-card>
|
||||||
|
|
||||||
|
<!-- 扫描控制 -->
|
||||||
|
<el-card class="control-card scan-card">
|
||||||
|
<template #header>
|
||||||
|
<div class="card-header">
|
||||||
|
<span>扫描控制</span>
|
||||||
|
<el-tag :type="scanStatus === 'running' ? 'success' : scanStatus === 'stopped' ? 'info' : 'warning'">
|
||||||
|
{{ scanStatus === 'running' ? '扫描中' : scanStatus === 'stopped' ? '已停止' : '就绪' }}
|
||||||
|
</el-tag>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<div class="control-content">
|
||||||
|
<div class="control-description">
|
||||||
|
<p>控制扫描过程的启动与停止。</p>
|
||||||
|
<p>确保 Scanner 电源已开启后再启动扫描。</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="control-actions">
|
||||||
|
<el-button
|
||||||
|
type="success"
|
||||||
|
:loading="scanLoading"
|
||||||
|
:disabled="scanStatus === 'running'"
|
||||||
|
@click="controlScan('start')"
|
||||||
|
>
|
||||||
|
<span class="btn-icon">▶</span> 启动扫描
|
||||||
|
</el-button>
|
||||||
|
<el-button
|
||||||
|
type="warning"
|
||||||
|
:loading="scanLoading"
|
||||||
|
:disabled="scanStatus !== 'running'"
|
||||||
|
@click="controlScan('stop')"
|
||||||
|
>
|
||||||
|
<span class="btn-icon">■</span> 停止扫描
|
||||||
|
</el-button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div v-if="scanStatus === 'running'" class="scan-progress">
|
||||||
|
<el-progress :percentage="scanProgress" :stroke-width="8" />
|
||||||
|
<span class="progress-text">扫描进行中...</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</el-card>
|
||||||
|
|
||||||
<!-- 操作日志 -->
|
<!-- 操作日志 -->
|
||||||
<el-card class="log-card">
|
<el-card class="log-card">
|
||||||
<template #header>
|
<template #header>
|
||||||
@@ -151,6 +194,12 @@ const scannerPowerStatus = ref('unknown')
|
|||||||
const hvsLoading = ref(false)
|
const hvsLoading = ref(false)
|
||||||
const scannerLoading = ref(false)
|
const scannerLoading = ref(false)
|
||||||
|
|
||||||
|
// 扫描状态
|
||||||
|
const scanStatus = ref<'ready' | 'running' | 'stopped'>('ready')
|
||||||
|
const scanLoading = ref(false)
|
||||||
|
const scanProgress = ref(0)
|
||||||
|
let scanProgressInterval: number | null = null
|
||||||
|
|
||||||
// 操作日志
|
// 操作日志
|
||||||
const operationLogs = ref<Array<{ time: string; message: string; type: string }>>([
|
const operationLogs = ref<Array<{ time: string; message: string; type: string }>>([
|
||||||
{ time: new Date().toLocaleTimeString(), message: '系统已启动', type: 'info' }
|
{ time: new Date().toLocaleTimeString(), message: '系统已启动', type: 'info' }
|
||||||
@@ -223,6 +272,61 @@ const controlPower = async (device: string, action: string) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 控制扫描
|
||||||
|
const controlScan = async (action: 'start' | 'stop') => {
|
||||||
|
const actionText = action === 'start' ? '启动' : '停止'
|
||||||
|
|
||||||
|
scanLoading.value = true
|
||||||
|
addLog('info', `正在${actionText}扫描...`)
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await axios.post('/api/scanctrl', {
|
||||||
|
action: action
|
||||||
|
})
|
||||||
|
|
||||||
|
if (response.data.status === 'success') {
|
||||||
|
ElMessage.success(`扫描${actionText}成功`)
|
||||||
|
addLog('success', `扫描${actionText}成功`)
|
||||||
|
|
||||||
|
if (action === 'start') {
|
||||||
|
scanStatus.value = 'running'
|
||||||
|
startScanProgress()
|
||||||
|
} else {
|
||||||
|
scanStatus.value = 'stopped'
|
||||||
|
stopScanProgress()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ElMessage.error('操作失败')
|
||||||
|
addLog('error', `扫描${actionText}失败`)
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('扫描控制失败:', error)
|
||||||
|
ElMessage.error(`扫描${actionText}失败`)
|
||||||
|
addLog('error', `扫描${actionText}失败: ${error}`)
|
||||||
|
} finally {
|
||||||
|
scanLoading.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 开始扫描进度
|
||||||
|
const startScanProgress = () => {
|
||||||
|
scanProgress.value = 0
|
||||||
|
scanProgressInterval = window.setInterval(() => {
|
||||||
|
if (scanProgress.value < 95) {
|
||||||
|
scanProgress.value += Math.random() * 5
|
||||||
|
}
|
||||||
|
}, 500)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 停止扫描进度
|
||||||
|
const stopScanProgress = () => {
|
||||||
|
if (scanProgressInterval) {
|
||||||
|
clearInterval(scanProgressInterval)
|
||||||
|
scanProgressInterval = null
|
||||||
|
}
|
||||||
|
scanProgress.value = 100
|
||||||
|
}
|
||||||
|
|
||||||
// 添加日志
|
// 添加日志
|
||||||
const addLog = (type: string, message: string) => {
|
const addLog = (type: string, message: string) => {
|
||||||
operationLogs.value.unshift({
|
operationLogs.value.unshift({
|
||||||
@@ -342,6 +446,24 @@ h2 {
|
|||||||
border-radius: 8px;
|
border-radius: 8px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.scan-card {
|
||||||
|
border-left: 4px solid #409eff;
|
||||||
|
}
|
||||||
|
|
||||||
|
.scan-progress {
|
||||||
|
margin-top: 20px;
|
||||||
|
padding-top: 16px;
|
||||||
|
border-top: 1px dashed #ebeef5;
|
||||||
|
}
|
||||||
|
|
||||||
|
.progress-text {
|
||||||
|
display: block;
|
||||||
|
text-align: center;
|
||||||
|
margin-top: 8px;
|
||||||
|
color: #409eff;
|
||||||
|
font-size: 14px;
|
||||||
|
}
|
||||||
|
|
||||||
.control-content {
|
.control-content {
|
||||||
padding: 20px;
|
padding: 20px;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user