Files
Thing/src/modules/smarthome/components/controls/CoverControl.vue
T
2026-10-23 17:25:17 +08:00

122 lines
3.5 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.
<script setup lang="ts">
import { computed, ref } from 'vue'
import { ArrowDown, ArrowUp, Square } from '@lucide/vue'
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle
} from '@/components/ui/alert-dialog'
import { Button } from '@/components/ui/button'
import { useSmartHomeStore, type EntitySummary } from '@/stores/smartHomeStore'
import { COVER_FEATURE, hasFeature } from '../../format'
import ControlSlider from './ControlSlider.vue'
import { controlFail } from './shared'
/**
* 窗帘:升 / 停 / 降 + 位置滑杆。
*
* 三个按钮都按 `supported_features` 位决定是否出现 —— 只支持"开关"的窗帘
* 不该看到一个点了没反应的"停"。
* **全开**是计划里列的危险操作(§5.3),确认后才下发。
*/
const props = defineProps<{ entity: EntitySummary; disabled?: boolean }>()
const store = useSmartHomeStore()
const canOpen = computed(() => hasFeature(props.entity, COVER_FEATURE.OPEN))
const canClose = computed(() => hasFeature(props.entity, COVER_FEATURE.CLOSE))
const canStop = computed(() => hasFeature(props.entity, COVER_FEATURE.STOP))
const canPosition = computed(() => hasFeature(props.entity, COVER_FEATURE.SET_POSITION))
const position = computed(() => props.entity.position ?? 0)
const confirmOpen = ref(false)
async function run(service: string, params = {}, optimistic: Partial<EntitySummary> = {}) {
try {
await store.control(props.entity, service, params, optimistic)
} catch (e) {
controlFail(e)
}
}
function doOpen() {
void run('open_cover', {}, { state: 'open', position: 100 })
}
function requestOpen() {
if (store.ui?.confirmDangerous) {
confirmOpen.value = true
return
}
doOpen()
}
function setPosition(v: number) {
void run('set_cover_position', { position: v }, { position: v, state: v > 0 ? 'open' : 'closed' })
}
</script>
<template>
<div class="flex flex-col gap-2">
<div class="flex items-center gap-1.5">
<Button
v-if="canOpen"
variant="outline"
size="sm"
class="min-w-0 flex-1"
:disabled="disabled"
@click="requestOpen"
>
<ArrowUp class="size-4" />开
</Button>
<Button
v-if="canStop"
variant="outline"
size="sm"
:disabled="disabled"
aria-label="停止"
@click="run('stop_cover')"
>
<Square class="size-3.5" />
</Button>
<Button
v-if="canClose"
variant="outline"
size="sm"
class="min-w-0 flex-1"
:disabled="disabled"
@click="run('close_cover', {}, { state: 'closed', position: 0 })"
>
<ArrowDown class="size-4" />关
</Button>
</div>
<ControlSlider
v-if="canPosition"
label="位置"
:value="position"
:min="0"
:max="100"
suffix="%"
:disabled="disabled"
@commit="setPosition"
/>
</div>
<AlertDialog :open="confirmOpen" @update:open="(v) => (confirmOpen = v)">
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>确认全开?</AlertDialogTitle>
<AlertDialogDescription>{{ entity.name }} 将完全打开。</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>取消</AlertDialogCancel>
<AlertDialogAction @click="doOpen">确认全开</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</template>