diff --git a/src/pages/device/detail/device-detail.types.ts b/src/pages/device/detail/device-detail.types.ts index ba12e73..95cf0d2 100644 --- a/src/pages/device/detail/device-detail.types.ts +++ b/src/pages/device/detail/device-detail.types.ts @@ -20,8 +20,7 @@ export interface AppDeviceRuntimeVo { fanLevel?: string tvoc?: number powerMode?: string - filterTotalDays?: string - filterRemainDays?: string + /** 滤芯寿命百分比 0-100 */ filterLife?: number filterReplaceTime?: string parkingShutdown?: string @@ -40,8 +39,7 @@ export interface UpdateDeviceRuntimeBody { tvoc?: number powerMode?: string deviceBattery?: number - filterTotalDays?: string - filterRemainDays?: string + filterLife?: number parkingShutdown?: string } diff --git a/src/pages/device/detail/device-detail.utils.ts b/src/pages/device/detail/device-detail.utils.ts index f8eb6d5..e2a4249 100644 --- a/src/pages/device/detail/device-detail.utils.ts +++ b/src/pages/device/detail/device-detail.utils.ts @@ -1,3 +1,4 @@ +import { clampPercent } from '@/utils/z2-ble-protocol' import type { AppDeviceRuntimeVo, FanLevel, RunMode } from './device-detail.types' export const RUN_MODE_OPTIONS = [ @@ -65,17 +66,48 @@ export function formatTvocLevel(tvoc?: number): string { return '差' } -export function formatFilterRemainDays(runtime: AppDeviceRuntimeVo): string { - if (runtime.filterRemainDays) { - return `预计还可使用 ${runtime.filterRemainDays} 天` +/** 抑制 BLE 电量小幅抖动:变化 <5% 时需连续两次相同读数才更新 */ +export function stabilizePercentReading( + 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) - const life = runtime.filterLife ?? 0 - if (total > 0 && life > 0) { - const remain = Math.round(total * life / 100) - return `预计还可使用 ${remain} 天` + if (normalized === previous) { + return { value: previous, pending: null } } - return '预计还可使用 -- 天' + 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} 天` } export function hasFilterReplaced(runtime: AppDeviceRuntimeVo): boolean { @@ -98,6 +130,7 @@ export interface RuntimeViewState { export function mapRuntimeToView( data: AppDeviceRuntimeVo, fallbackName = '未命名设备', + filterTotalDays?: number, ): RuntimeViewState { return { deviceName: data.name || fallbackName, @@ -106,7 +139,7 @@ export function mapRuntimeToView( tvoc: data.tvoc ?? null, powerMode: data.powerMode || '0', filterLife: data.filterLife ?? 0, - filterRemainText: formatFilterRemainDays(data), + filterRemainText: formatFilterRemainDays(data.filterLife, filterTotalDays), filterReplaced: hasFilterReplaced(data), isOnline: isDeviceOnline(data.status), } diff --git a/src/pages/device/detail/use-device-detail.ts b/src/pages/device/detail/use-device-detail.ts index 736146f..6dab233 100644 --- a/src/pages/device/detail/use-device-detail.ts +++ b/src/pages/device/detail/use-device-detail.ts @@ -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() 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(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' }) }