feat:绑定设备蓝牙问题解决

This commit is contained in:
李伊帆
2026-10-16 14:07:56 +08:00
parent a4fa65de31
commit a76aa9044c
3 changed files with 97 additions and 18 deletions

View File

@@ -3,12 +3,15 @@ import { computed, ref } from 'vue'
import { fetchRoomList } from '@/api/location'
import { getErrorMessage } from '@/utils/api-error'
import { parseDeviceId, parseId, sameId } from '@/utils/device-id'
import { fetchProductOptions } from '@/pages/add-device/complete/complete.api'
import { fetchDeviceList } from '../device.api'
import { fetchDeviceRuntime, updateDeviceRuntime } from './device-detail.api'
import type { AppDeviceRuntimeVo, DeviceDetailRouteQuery, FanLevel, RunMode } from './device-detail.types'
import {
AUTO_FAN_LEVEL_OPTIONS,
DEFAULT_FILTER_TOTAL_DAYS,
formatConnectionText,
formatFilterRemainDays,
formatPowerMode,
formatTvocLevel,
formatTvocValue,
@@ -16,6 +19,7 @@ import {
isBluetoothConnected,
mapRuntimeToView,
RUN_MODE_OPTIONS,
stabilizePercentReading,
} from './device-detail.utils'
import {
airQualityToLabel,
@@ -51,10 +55,12 @@ export function useDeviceDetail() {
const airQuality = ref<Z2AirQuality>()
const powerMode = ref('0')
const filterLife = ref(0)
const filterTotalDays = ref(DEFAULT_FILTER_TOTAL_DAYS)
const filterRemainText = ref('预计还可使用 -- 天')
const filterReplaced = ref(false)
const filterReplaceDialogVisible = ref(false)
const batteryLevel = ref<number | null>(null)
let pendingBatteryReading: number | null = null
const syncText = ref('数据实时同步中')
const tvocDisplay = computed(() => formatTvocValue(tvoc.value ?? undefined))
@@ -78,6 +84,42 @@ export function useDeviceDetail() {
showFanLevelControl.value ? AUTO_FAN_LEVEL_OPTIONS : [],
)
function updateFilterRemainText() {
filterRemainText.value = formatFilterRemainDays(filterLife.value, filterTotalDays.value)
}
function applyBatteryLevel(next: number, smooth = false) {
if (!smooth) {
batteryLevel.value = clampPercent(next)
pendingBatteryReading = null
return
}
const result = stabilizePercentReading(
batteryLevel.value,
next,
pendingBatteryReading,
)
batteryLevel.value = result.value
pendingBatteryReading = result.pending
}
async function loadFilterTotalDays() {
filterTotalDays.value = DEFAULT_FILTER_TOTAL_DAYS
try {
const options = await fetchProductOptions()
const matched = options.find((item) => item.model === productModel.value)
?? options.find((item) => item.filterEnabled && item.filterTotalDays)
?? options.find((item) => item.filterTotalDays)
if (matched?.filterTotalDays && matched.filterTotalDays > 0) {
filterTotalDays.value = matched.filterTotalDays
if (matched.model) productModel.value = matched.model
}
} catch {
// 产品配置加载失败时使用默认总天数
}
updateFilterRemainText()
}
function applyBleRuntime(patch: BleRuntimePatch) {
if (patch.runMode != null) runMode.value = patch.runMode as RunMode
if (patch.fanLevel != null) fanLevel.value = patch.fanLevel as FanLevel
@@ -88,13 +130,16 @@ export function useDeviceDetail() {
tvoc.value = patch.tvoc
}
if (patch.powerMode != null) powerMode.value = patch.powerMode
if (patch.filterLife != null) filterLife.value = patch.filterLife
if (patch.deviceBattery != null) batteryLevel.value = clampPercent(patch.deviceBattery)
if (patch.filterLife != null) {
filterLife.value = patch.filterLife
updateFilterRemainText()
}
if (patch.deviceBattery != null) applyBatteryLevel(patch.deviceBattery, true)
if (patch.charging != null) charging.value = patch.charging === '1'
}
function applyRuntime(data: AppDeviceRuntimeVo) {
const view = mapRuntimeToView(data, deviceName.value)
const view = mapRuntimeToView(data, deviceName.value, filterTotalDays.value)
deviceName.value = view.deviceName
runMode.value = view.runMode
fanLevel.value = view.fanLevel
@@ -104,7 +149,7 @@ export function useDeviceDetail() {
filterRemainText.value = view.filterRemainText
filterReplaced.value = view.filterReplaced
isOnline.value = view.isOnline
if (data.deviceBattery != null) batteryLevel.value = clampPercent(data.deviceBattery)
if (data.deviceBattery != null) applyBatteryLevel(data.deviceBattery, false)
if (data.status !== undefined) deviceStatus.value = data.status
}
@@ -142,6 +187,7 @@ export function useDeviceDetail() {
const [runtime] = await Promise.all([
fetchDeviceRuntime({ deviceId: deviceId.value }),
refreshDeviceMeta(),
loadFilterTotalDays(),
])
applyRuntime(runtime)
} catch (err) {
@@ -214,6 +260,8 @@ export function useDeviceDetail() {
async function onConfirmFilterReplace() {
await sendBleCommand(Z2_BLE_CMD.FILTER_RESET)
filterReplaced.value = true
filterLife.value = 100
updateFilterRemainText()
uni.showToast({ title: '操作成功', icon: 'success' })
}