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

@@ -20,8 +20,7 @@ export interface AppDeviceRuntimeVo {
fanLevel?: string fanLevel?: string
tvoc?: number tvoc?: number
powerMode?: string powerMode?: string
filterTotalDays?: string /** 滤芯寿命百分比 0-100 */
filterRemainDays?: string
filterLife?: number filterLife?: number
filterReplaceTime?: string filterReplaceTime?: string
parkingShutdown?: string parkingShutdown?: string
@@ -40,8 +39,7 @@ export interface UpdateDeviceRuntimeBody {
tvoc?: number tvoc?: number
powerMode?: string powerMode?: string
deviceBattery?: number deviceBattery?: number
filterTotalDays?: string filterLife?: number
filterRemainDays?: string
parkingShutdown?: string parkingShutdown?: string
} }

View File

@@ -1,3 +1,4 @@
import { clampPercent } from '@/utils/z2-ble-protocol'
import type { AppDeviceRuntimeVo, FanLevel, RunMode } from './device-detail.types' import type { AppDeviceRuntimeVo, FanLevel, RunMode } from './device-detail.types'
export const RUN_MODE_OPTIONS = [ export const RUN_MODE_OPTIONS = [
@@ -65,17 +66,48 @@ export function formatTvocLevel(tvoc?: number): string {
return '差' return '差'
} }
export function formatFilterRemainDays(runtime: AppDeviceRuntimeVo): string { /** 抑制 BLE 电量小幅抖动:变化 <5% 时需连续两次相同读数才更新 */
if (runtime.filterRemainDays) { export function stabilizePercentReading(
return `预计还可使用 ${runtime.filterRemainDays} 天` previous: number | null,
next: number,
pending: number | null,
): { value: number | null; pending: number | null } {
const normalized = clampPercent(next)
if (previous == null) {
return { value: normalized, pending: null }
} }
const total = Number(runtime.filterTotalDays) if (normalized === previous) {
const life = runtime.filterLife ?? 0 return { value: previous, pending: null }
if (total > 0 && life > 0) { }
const remain = Math.round(total * life / 100) if (Math.abs(normalized - previous) >= 5) {
return { value: normalized, pending: null }
}
if (pending === normalized) {
return { value: normalized, pending: null }
}
return { value: previous, pending: normalized }
}
/** 滤芯设计总寿命(天),优先取产品配置 filterTotalDays */
export const DEFAULT_FILTER_TOTAL_DAYS = 180
/** 根据 filterLife(0-100)计算剩余天数 */
export function calcFilterRemainDays(
filterLife?: number | null,
filterTotalDays = DEFAULT_FILTER_TOTAL_DAYS,
): number | null {
if (filterLife == null || filterLife < 0) return null
if (filterTotalDays <= 0) return null
return Math.max(0, Math.round(filterTotalDays * filterLife / 100))
}
export function formatFilterRemainDays(
filterLife?: number | null,
filterTotalDays = DEFAULT_FILTER_TOTAL_DAYS,
): string {
const remain = calcFilterRemainDays(filterLife, filterTotalDays)
if (remain == null) return '预计还可使用 -- 天'
return `预计还可使用 ${remain} 天` return `预计还可使用 ${remain} 天`
}
return '预计还可使用 -- 天'
} }
export function hasFilterReplaced(runtime: AppDeviceRuntimeVo): boolean { export function hasFilterReplaced(runtime: AppDeviceRuntimeVo): boolean {
@@ -98,6 +130,7 @@ export interface RuntimeViewState {
export function mapRuntimeToView( export function mapRuntimeToView(
data: AppDeviceRuntimeVo, data: AppDeviceRuntimeVo,
fallbackName = '未命名设备', fallbackName = '未命名设备',
filterTotalDays?: number,
): RuntimeViewState { ): RuntimeViewState {
return { return {
deviceName: data.name || fallbackName, deviceName: data.name || fallbackName,
@@ -106,7 +139,7 @@ export function mapRuntimeToView(
tvoc: data.tvoc ?? null, tvoc: data.tvoc ?? null,
powerMode: data.powerMode || '0', powerMode: data.powerMode || '0',
filterLife: data.filterLife ?? 0, filterLife: data.filterLife ?? 0,
filterRemainText: formatFilterRemainDays(data), filterRemainText: formatFilterRemainDays(data.filterLife, filterTotalDays),
filterReplaced: hasFilterReplaced(data), filterReplaced: hasFilterReplaced(data),
isOnline: isDeviceOnline(data.status), isOnline: isDeviceOnline(data.status),
} }

View File

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