feat(camera): 摄像头预览弹窗新增云台控制面板(方向/转速/预置点)
- src/api/camera: 新增 ptzControl / ptzStop / ptzPresetGoto / ptzPresetSet 及 PtzDirection、转速与预置点校验 - src/components/CameraPtzPanel: 8 方向按住转动(松手停止)、转速 1-7、预置点 1-8 快捷键与编号跳转/覆盖保存 - 防呆: 按下请求未返回时先记录、返回后补发 stop;window pointerup/pointercancel/blur 与卸载时兜底停止 - newcamara 预览弹窗接入云台面板
This commit is contained in:
parent
3e9095c73e
commit
60318d9b07
@ -128,3 +128,137 @@ export function del(data: { id: number }) {
|
||||
isSystemApi: true
|
||||
} as any)
|
||||
}
|
||||
|
||||
/** 云台方向:8 个位置(大小写不敏感,服务端统一转小写校验) */
|
||||
export type PtzDirection =
|
||||
| 'up'
|
||||
| 'down'
|
||||
| 'left'
|
||||
| 'right'
|
||||
| 'upleft'
|
||||
| 'upright'
|
||||
| 'downleft'
|
||||
| 'downright'
|
||||
|
||||
/** 云台转速默认值(可选 1-7 的整数) */
|
||||
export const PTZ_DEFAULT_SPEED = 4
|
||||
|
||||
/** 云台转速范围:1-7 的整数 */
|
||||
export function normalizePtzSpeed(speed?: number): number {
|
||||
const n = Number(speed)
|
||||
if (!Number.isFinite(n)) return PTZ_DEFAULT_SPEED
|
||||
const int = Math.round(n)
|
||||
if (int < 1) return 1
|
||||
if (int > 7) return 7
|
||||
return int
|
||||
}
|
||||
|
||||
/** 云台预置点范围:1-256 的整数 */
|
||||
export function normalizePtzPresetId(presetId: number): number {
|
||||
const n = Number(presetId)
|
||||
if (!Number.isFinite(n)) return 0
|
||||
const int = Math.round(n)
|
||||
if (int < 1 || int > 256) return 0
|
||||
return int
|
||||
}
|
||||
|
||||
/**
|
||||
* 云台方向控制(按下):按指定方向持续转动摄像头
|
||||
* 必须配对调用 ptzStop 停止
|
||||
*/
|
||||
export function ptzControl(data: {
|
||||
id: number
|
||||
action: PtzDirection
|
||||
speed?: number
|
||||
}) {
|
||||
const body: Record<string, unknown> = {
|
||||
id: Number(data.id),
|
||||
action: String(data.action || '').toLowerCase()
|
||||
}
|
||||
if (data.speed !== undefined && data.speed !== null) {
|
||||
body.speed = normalizePtzSpeed(data.speed)
|
||||
}
|
||||
return request({
|
||||
url: '/system',
|
||||
method: 'post',
|
||||
params: {
|
||||
method: 'camera.ptzControl',
|
||||
v,
|
||||
timestamp: new Date().getTime()
|
||||
},
|
||||
data: body,
|
||||
isSystemApi: true
|
||||
} as any)
|
||||
}
|
||||
|
||||
/**
|
||||
* 云台停止(松手):停止转动
|
||||
* action 必须与按下时(ptzControl)一致
|
||||
*/
|
||||
export function ptzStop(data: {
|
||||
id: number
|
||||
action: PtzDirection
|
||||
}) {
|
||||
return request({
|
||||
url: '/system',
|
||||
method: 'post',
|
||||
params: {
|
||||
method: 'camera.ptzStop',
|
||||
v,
|
||||
timestamp: new Date().getTime()
|
||||
},
|
||||
data: {
|
||||
id: Number(data.id),
|
||||
action: String(data.action || '').toLowerCase()
|
||||
},
|
||||
isSystemApi: true
|
||||
} as any)
|
||||
}
|
||||
|
||||
/**
|
||||
* 云台预置点跳转:跳转到指定预置点
|
||||
* 一次调用即可,无需 ptzStop
|
||||
*/
|
||||
export function ptzPresetGoto(data: {
|
||||
id: number
|
||||
presetId: number
|
||||
}) {
|
||||
return request({
|
||||
url: '/system',
|
||||
method: 'post',
|
||||
params: {
|
||||
method: 'camera.ptzPresetGoto',
|
||||
v,
|
||||
timestamp: new Date().getTime()
|
||||
},
|
||||
data: {
|
||||
id: Number(data.id),
|
||||
presetId: normalizePtzPresetId(data.presetId)
|
||||
},
|
||||
isSystemApi: true
|
||||
} as any)
|
||||
}
|
||||
|
||||
/**
|
||||
* 云台预置点设置:把摄像头当前机位保存为指定预置点
|
||||
* 一次调用即可;已存在的编号会被当前机位覆盖
|
||||
*/
|
||||
export function ptzPresetSet(data: {
|
||||
id: number
|
||||
presetId: number
|
||||
}) {
|
||||
return request({
|
||||
url: '/system',
|
||||
method: 'post',
|
||||
params: {
|
||||
method: 'camera.ptzPresetSet',
|
||||
v,
|
||||
timestamp: new Date().getTime()
|
||||
},
|
||||
data: {
|
||||
id: Number(data.id),
|
||||
presetId: normalizePtzPresetId(data.presetId)
|
||||
},
|
||||
isSystemApi: true
|
||||
} as any)
|
||||
}
|
||||
|
||||
435
src/components/CameraPtzPanel/index.vue
Normal file
435
src/components/CameraPtzPanel/index.vue
Normal file
@ -0,0 +1,435 @@
|
||||
<template>
|
||||
<div class="camera-ptz" :class="{ 'is-disabled': disabled }">
|
||||
<div class="camera-ptz__head">
|
||||
<span class="camera-ptz__title">云台控制</span>
|
||||
<span class="camera-ptz__meta">按住方向键持续转动,松手自动停止</span>
|
||||
</div>
|
||||
|
||||
<div class="camera-ptz__body">
|
||||
<!-- 8 方向:按下持续转动,松手停止 -->
|
||||
<div class="camera-ptz__pad">
|
||||
<button
|
||||
v-for="item in directionButtons"
|
||||
:key="item.value"
|
||||
type="button"
|
||||
class="camera-ptz__btn"
|
||||
:class="[`is-${item.value}`, { 'is-active': activeDirection === item.value }]"
|
||||
:style="{ gridArea: item.area }"
|
||||
:disabled="disabled || !ready"
|
||||
:title="item.label"
|
||||
@pointerdown.prevent="handlePress(item.value)"
|
||||
@pointerup.prevent="handleRelease"
|
||||
@pointerleave="handleRelease"
|
||||
@pointercancel="handleRelease"
|
||||
@contextmenu.prevent
|
||||
>
|
||||
{{ item.symbol }}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="camera-ptz__side">
|
||||
<div class="camera-ptz__row">
|
||||
<span class="camera-ptz__label">转速</span>
|
||||
<el-select v-model="speed" size="small" style="width: 84px" :disabled="disabled">
|
||||
<el-option v-for="n in 7" :key="n" :label="String(n)" :value="n" />
|
||||
</el-select>
|
||||
</div>
|
||||
|
||||
<div class="camera-ptz__row camera-ptz__row--preset">
|
||||
<span class="camera-ptz__label">预置点</span>
|
||||
<div class="camera-ptz__presets">
|
||||
<el-button
|
||||
v-for="n in quickPresets"
|
||||
:key="n"
|
||||
size="small"
|
||||
class="camera-ptz__preset-btn"
|
||||
:type="presetId === n ? 'primary' : 'default'"
|
||||
:disabled="disabled || !ready || presetLoading"
|
||||
@click="handlePresetGoto(n)"
|
||||
>
|
||||
{{ n }}
|
||||
</el-button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="camera-ptz__row">
|
||||
<span class="camera-ptz__label">编号</span>
|
||||
<el-input-number
|
||||
v-model="presetId"
|
||||
size="small"
|
||||
:min="1"
|
||||
:max="256"
|
||||
:step="1"
|
||||
:precision="0"
|
||||
controls-position="right"
|
||||
style="width: 110px"
|
||||
:disabled="disabled || !ready"
|
||||
/>
|
||||
<el-button
|
||||
type="primary"
|
||||
size="small"
|
||||
:loading="presetLoading"
|
||||
:disabled="disabled || !ready"
|
||||
@click="handlePresetGoto()"
|
||||
>
|
||||
跳转
|
||||
</el-button>
|
||||
<el-button
|
||||
size="small"
|
||||
:loading="presetSaving"
|
||||
:disabled="disabled || !ready"
|
||||
@click="handlePresetSave()"
|
||||
>
|
||||
设为预置点
|
||||
</el-button>
|
||||
</div>
|
||||
<div class="camera-ptz__tips">按住方向键持续转动,松手自动停止;「设为预置点」会把当前机位覆盖保存到该编号</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onBeforeUnmount, ref, watch } from 'vue'
|
||||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||
import {
|
||||
ptzControl,
|
||||
ptzStop,
|
||||
ptzPresetGoto,
|
||||
ptzPresetSet,
|
||||
PTZ_DEFAULT_SPEED,
|
||||
normalizePtzPresetId,
|
||||
type PtzDirection
|
||||
} from '@/api/camera/camera'
|
||||
|
||||
const props = defineProps<{
|
||||
/** 摄像头ID(必填) */
|
||||
cameraId?: number | null
|
||||
/** 是否禁用全部操作 */
|
||||
disabled?: boolean
|
||||
}>()
|
||||
|
||||
const speed = ref(PTZ_DEFAULT_SPEED)
|
||||
const activeDirection = ref<PtzDirection | null>(null)
|
||||
/** 正在请求「按下」的方向 */
|
||||
let controlPendingDir: PtzDirection | null = null
|
||||
/** 请求返回前已松手的方向,收到响应后补发停止 */
|
||||
let pendingStopDir: PtzDirection | null = null
|
||||
let errorShown = false
|
||||
|
||||
const quickPresets = [1, 2, 3, 4, 5, 6, 7, 8]
|
||||
const presetId = ref<number | undefined>(undefined)
|
||||
const presetLoading = ref(false)
|
||||
const presetSaving = ref(false)
|
||||
|
||||
const directionButtons: Array<{ value: PtzDirection; label: string; symbol: string; area: string }> = [
|
||||
{ value: 'upleft', label: '左上', symbol: '↖', area: '1 / 1' },
|
||||
{ value: 'up', label: '上', symbol: '↑', area: '1 / 2' },
|
||||
{ value: 'upright', label: '右上', symbol: '↗', area: '1 / 3' },
|
||||
{ value: 'left', label: '左', symbol: '←', area: '2 / 1' },
|
||||
{ value: 'right', label: '右', symbol: '→', area: '2 / 3' },
|
||||
{ value: 'downleft', label: '左下', symbol: '↙', area: '3 / 1' },
|
||||
{ value: 'down', label: '下', symbol: '↓', area: '3 / 2' },
|
||||
{ value: 'downright', label: '右下', symbol: '↘', area: '3 / 3' }
|
||||
]
|
||||
|
||||
const ready = computed(() => !!props.cameraId)
|
||||
const disabled = computed(() => !!props.disabled)
|
||||
|
||||
const isOk = (res: any) => res && [0, 1, 200].includes(res.code)
|
||||
|
||||
function notifyError(message: string) {
|
||||
if (errorShown) return
|
||||
errorShown = true
|
||||
ElMessage.error(message)
|
||||
}
|
||||
|
||||
/** 按下:持续转动 */
|
||||
async function handlePress(direction: PtzDirection) {
|
||||
if (disabled.value || !ready.value) return
|
||||
if (activeDirection.value || controlPendingDir) return
|
||||
|
||||
const id = props.cameraId as number
|
||||
|
||||
activeDirection.value = direction
|
||||
controlPendingDir = direction
|
||||
pendingStopDir = null
|
||||
errorShown = false
|
||||
|
||||
try {
|
||||
const res: any = await ptzControl({ id, action: direction, speed: speed.value })
|
||||
if (!isOk(res)) notifyError(res?.message || '云台控制失败')
|
||||
} catch (e: any) {
|
||||
notifyError(e?.message || '云台控制失败')
|
||||
} finally {
|
||||
controlPendingDir = null
|
||||
// 请求期间已松手 → 现在补发停止,避免摄像头一直转动
|
||||
if (pendingStopDir === direction) {
|
||||
pendingStopDir = null
|
||||
void stopDirection(direction)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** 松手:停止转动 */
|
||||
function handleRelease() {
|
||||
const direction = activeDirection.value
|
||||
if (!direction) return
|
||||
activeDirection.value = null
|
||||
|
||||
if (controlPendingDir === direction) {
|
||||
// 「按下」请求尚未返回,先记下,等响应回来再停止(避免停止请求先于启动到达)
|
||||
pendingStopDir = direction
|
||||
return
|
||||
}
|
||||
void stopDirection(direction)
|
||||
}
|
||||
|
||||
async function stopDirection(direction: PtzDirection) {
|
||||
const id = props.cameraId
|
||||
if (!id) return
|
||||
try {
|
||||
await ptzStop({ id, action: direction })
|
||||
} catch (e: any) {
|
||||
notifyError(e?.message || '云台停止失败')
|
||||
}
|
||||
}
|
||||
|
||||
/** 停止所有正在进行的转动(弹窗关闭、失焦等场景兜底) */
|
||||
function stopAll() {
|
||||
pendingStopDir = null
|
||||
if (activeDirection.value) {
|
||||
handleRelease()
|
||||
}
|
||||
}
|
||||
|
||||
/** 停止转动并等待停止请求发出(预置点跳转/保存前调用,避免带着转动记录机位) */
|
||||
async function stopAllAndWait() {
|
||||
pendingStopDir = null
|
||||
const direction = activeDirection.value
|
||||
if (!direction) return
|
||||
activeDirection.value = null
|
||||
|
||||
// 「按下」请求还没返回 → 先等它回来,避免 stop 先于 start 到达后端
|
||||
if (controlPendingDir === direction) {
|
||||
const startAt = Date.now()
|
||||
while (controlPendingDir === direction && Date.now() - startAt < 1500) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 50))
|
||||
}
|
||||
}
|
||||
await stopDirection(direction)
|
||||
}
|
||||
|
||||
async function handlePresetGoto(value?: number) {
|
||||
const target = normalizePtzPresetId(value ?? presetId.value ?? 0)
|
||||
if (!target) {
|
||||
ElMessage.warning('请输入 1-256 的预置点编号')
|
||||
return
|
||||
}
|
||||
if (disabled.value || !ready.value) return
|
||||
|
||||
// 跳转前先停止当前转动
|
||||
await stopAllAndWait()
|
||||
|
||||
presetId.value = target
|
||||
presetLoading.value = true
|
||||
errorShown = false
|
||||
try {
|
||||
const res: any = await ptzPresetGoto({
|
||||
id: props.cameraId as number,
|
||||
presetId: target
|
||||
})
|
||||
if (isOk(res)) {
|
||||
ElMessage.success(`已跳转预置点 ${target}`)
|
||||
} else {
|
||||
ElMessage.error(res?.message || '预置点跳转失败')
|
||||
}
|
||||
} catch (e: any) {
|
||||
ElMessage.error(e?.message || '预置点跳转失败')
|
||||
} finally {
|
||||
presetLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/** 保存预置点:把当前机位覆盖保存到指定编号 */
|
||||
async function handlePresetSave(value?: number) {
|
||||
const target = normalizePtzPresetId(value ?? presetId.value ?? 0)
|
||||
if (!target) {
|
||||
ElMessage.warning('请输入 1-256 的预置点编号')
|
||||
return
|
||||
}
|
||||
if (disabled.value || !ready.value) return
|
||||
|
||||
// 先把转动停下来,避免保存到移动中的机位
|
||||
await stopAllAndWait()
|
||||
|
||||
try {
|
||||
await ElMessageBox.confirm(
|
||||
`将摄像头当前机位保存为预置点 ${target}?该编号已存在的预置点会被覆盖。`,
|
||||
'保存预置点',
|
||||
{ type: 'warning', confirmButtonText: '保存', cancelButtonText: '取消' }
|
||||
)
|
||||
} catch {
|
||||
return
|
||||
}
|
||||
|
||||
presetId.value = target
|
||||
presetSaving.value = true
|
||||
errorShown = false
|
||||
try {
|
||||
const res: any = await ptzPresetSet({
|
||||
id: props.cameraId as number,
|
||||
presetId: target
|
||||
})
|
||||
if (isOk(res)) {
|
||||
ElMessage.success(`已保存为预置点 ${target}`)
|
||||
} else {
|
||||
ElMessage.error(res?.message || '预置点保存失败')
|
||||
}
|
||||
} catch (e: any) {
|
||||
ElMessage.error(e?.message || '预置点保存失败')
|
||||
} finally {
|
||||
presetSaving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function handleWindowBlur() {
|
||||
stopAll()
|
||||
}
|
||||
|
||||
/** 在按钮外松手时也要停止转动,隐藏弹窗时兜底 */
|
||||
function handleGlobalPointerUp() {
|
||||
handleRelease()
|
||||
}
|
||||
|
||||
watch(
|
||||
() => props.cameraId,
|
||||
(val) => {
|
||||
stopAll()
|
||||
if (val) presetId.value = undefined
|
||||
}
|
||||
)
|
||||
|
||||
// 窗口失焦 / 全局松手时停止转动,避免摄像头一直转
|
||||
if (typeof window !== 'undefined') {
|
||||
window.addEventListener('blur', handleWindowBlur)
|
||||
window.addEventListener('pointerup', handleGlobalPointerUp)
|
||||
window.addEventListener('pointercancel', handleGlobalPointerUp)
|
||||
}
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
if (typeof window !== 'undefined') {
|
||||
window.removeEventListener('blur', handleWindowBlur)
|
||||
window.removeEventListener('pointerup', handleGlobalPointerUp)
|
||||
window.removeEventListener('pointercancel', handleGlobalPointerUp)
|
||||
}
|
||||
stopAll()
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.camera-ptz {
|
||||
margin-top: 16px;
|
||||
padding: 12px 16px;
|
||||
border: 1px solid #ebeef5;
|
||||
border-radius: 4px;
|
||||
background: #fafafa;
|
||||
}
|
||||
.camera-ptz__head {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
.camera-ptz__title {
|
||||
font-size: 14px;
|
||||
font-weight: 600;
|
||||
color: #303133;
|
||||
}
|
||||
.camera-ptz__meta {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
font-size: 12px;
|
||||
color: #909399;
|
||||
}
|
||||
.camera-ptz__body {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
gap: 24px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
.camera-ptz__pad {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(3, 36px);
|
||||
grid-template-rows: repeat(3, 36px);
|
||||
gap: 4px;
|
||||
}
|
||||
.camera-ptz__btn {
|
||||
width: 36px;
|
||||
height: 36px;
|
||||
padding: 0;
|
||||
border: 1px solid #dcdfe6;
|
||||
border-radius: 4px;
|
||||
background: #fff;
|
||||
color: #303133;
|
||||
font-size: 16px;
|
||||
line-height: 1;
|
||||
cursor: pointer;
|
||||
user-select: none;
|
||||
touch-action: none;
|
||||
transition: all 0.15s;
|
||||
}
|
||||
.camera-ptz__btn:hover:not(:disabled) {
|
||||
border-color: #409eff;
|
||||
color: #409eff;
|
||||
}
|
||||
.camera-ptz__btn.is-active {
|
||||
background: #409eff;
|
||||
border-color: #409eff;
|
||||
color: #fff;
|
||||
}
|
||||
.camera-ptz__btn:disabled {
|
||||
cursor: not-allowed;
|
||||
color: #c0c4cc;
|
||||
background: #f5f7fa;
|
||||
}
|
||||
.camera-ptz__side {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
min-width: 260px;
|
||||
}
|
||||
.camera-ptz__row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
.camera-ptz__label {
|
||||
width: 44px;
|
||||
font-size: 13px;
|
||||
color: #606266;
|
||||
flex: none;
|
||||
}
|
||||
.camera-ptz__presets {
|
||||
display: flex;
|
||||
gap: 4px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
.camera-ptz__preset-btn {
|
||||
width: 30px;
|
||||
padding: 0;
|
||||
}
|
||||
.camera-ptz__preset-btn + .camera-ptz__preset-btn {
|
||||
margin-left: 0;
|
||||
}
|
||||
.camera-ptz__tips {
|
||||
font-size: 12px;
|
||||
color: #909399;
|
||||
line-height: 1.5;
|
||||
}
|
||||
.camera-ptz.is-disabled {
|
||||
opacity: 0.7;
|
||||
}
|
||||
</style>
|
||||
@ -47,6 +47,9 @@
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
</div>
|
||||
|
||||
<!-- 云台控制(按下持续转动,松手停止) -->
|
||||
<CameraPtzPanel v-if="streamReady && cameraRow?.id" :camera-id="cameraRow.id" />
|
||||
</div>
|
||||
<template #footer>
|
||||
<el-button @click="onDialogVisibleChange(false)">关闭</el-button>
|
||||
@ -61,6 +64,7 @@ import { ElMessage } from 'element-plus'
|
||||
import { getLiveStreamUrl } from '@/api/camera_server/localServerApi'
|
||||
import { list as serverList, detail as serverDetail } from '@/api/camera_server/camera_server'
|
||||
import { setOsd } from '@/api/camera/camera'
|
||||
import CameraPtzPanel from '@/components/CameraPtzPanel/index.vue'
|
||||
|
||||
export interface CameraPreviewRow {
|
||||
id?: number
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user