feat:蓝牙绑定和在线客服
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import { onLoad } from '@dcloudio/uni-app'
|
||||
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 { fetchDeviceList } from '../device.api'
|
||||
import { fetchDeviceRuntime, updateDeviceRuntime } from './device-detail.api'
|
||||
import type { AppDeviceRuntimeVo, DeviceDetailRouteQuery, FanLevel, RunMode } from './device-detail.types'
|
||||
@@ -15,13 +17,27 @@ import {
|
||||
mapRuntimeToView,
|
||||
RUN_MODE_OPTIONS,
|
||||
} from './device-detail.utils'
|
||||
import {
|
||||
airQualityToLabel,
|
||||
airQualityToTvoc,
|
||||
clampPercent,
|
||||
encodeFanLevelBleCommand,
|
||||
encodeRunModeBleCommand,
|
||||
type Z2AirQuality,
|
||||
Z2_BLE_CMD,
|
||||
} from '@/utils/z2-ble-protocol'
|
||||
import type { BleRuntimePatch } from './device-bluetooth-read'
|
||||
import { useDeviceBluetoothSync } from './use-device-bluetooth-sync'
|
||||
|
||||
export function useDeviceDetail() {
|
||||
const deviceId = ref(0)
|
||||
const deviceId = ref('')
|
||||
const deviceNo = ref('')
|
||||
const deviceMac = ref<string>()
|
||||
const deviceName = ref('')
|
||||
const locationName = ref('客厅')
|
||||
const locationId = ref('')
|
||||
const homeId = ref('')
|
||||
const productModel = ref('CZ-Z2023')
|
||||
const deviceStatus = ref<string>()
|
||||
const bluetoothConnected = ref(false)
|
||||
const charging = ref(false)
|
||||
@@ -32,6 +48,7 @@ export function useDeviceDetail() {
|
||||
const runMode = ref<RunMode>('0')
|
||||
const fanLevel = ref<FanLevel>('0')
|
||||
const tvoc = ref<number | null>(null)
|
||||
const airQuality = ref<Z2AirQuality>()
|
||||
const powerMode = ref('0')
|
||||
const filterLife = ref(0)
|
||||
const filterRemainText = ref('预计还可使用 -- 天')
|
||||
@@ -41,7 +58,10 @@ export function useDeviceDetail() {
|
||||
const syncText = ref('数据实时同步中')
|
||||
|
||||
const tvocDisplay = computed(() => formatTvocValue(tvoc.value ?? undefined))
|
||||
const tvocLevel = computed(() => formatTvocLevel(tvoc.value ?? undefined))
|
||||
const tvocLevel = computed(() => {
|
||||
if (airQuality.value) return airQualityToLabel(airQuality.value)
|
||||
return formatTvocLevel(tvoc.value ?? undefined)
|
||||
})
|
||||
const powerModeText = computed(() => formatPowerMode(powerMode.value))
|
||||
const batteryDisplay = computed(() =>
|
||||
batteryLevel.value != null ? `${batteryLevel.value}%` : '--',
|
||||
@@ -58,28 +78,54 @@ export function useDeviceDetail() {
|
||||
showFanLevelControl.value ? AUTO_FAN_LEVEL_OPTIONS : [],
|
||||
)
|
||||
|
||||
function applyBleRuntime(patch: BleRuntimePatch) {
|
||||
if (patch.runMode != null) runMode.value = patch.runMode as RunMode
|
||||
if (patch.fanLevel != null) fanLevel.value = patch.fanLevel as FanLevel
|
||||
if (patch.airQuality != null) {
|
||||
airQuality.value = patch.airQuality
|
||||
tvoc.value = airQualityToTvoc(patch.airQuality) ?? tvoc.value
|
||||
} else if (patch.tvoc != null) {
|
||||
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.charging != null) charging.value = patch.charging === '1'
|
||||
}
|
||||
|
||||
function applyRuntime(data: AppDeviceRuntimeVo) {
|
||||
const view = mapRuntimeToView(data, deviceName.value)
|
||||
deviceName.value = view.deviceName
|
||||
runMode.value = view.runMode
|
||||
fanLevel.value = view.fanLevel
|
||||
tvoc.value = view.tvoc
|
||||
if (view.tvoc != null) tvoc.value = view.tvoc
|
||||
powerMode.value = view.powerMode
|
||||
filterLife.value = view.filterLife
|
||||
filterRemainText.value = view.filterRemainText
|
||||
filterReplaced.value = view.filterReplaced
|
||||
isOnline.value = view.isOnline
|
||||
if (data.deviceBattery != null) batteryLevel.value = clampPercent(data.deviceBattery)
|
||||
if (data.status !== undefined) deviceStatus.value = data.status
|
||||
syncText.value = '刚刚更新 · 数据实时同步中'
|
||||
}
|
||||
|
||||
async function refreshDeviceMeta() {
|
||||
if (!deviceId.value) return
|
||||
try {
|
||||
const list = await fetchDeviceList()
|
||||
const device = list?.find((item) => item.deviceId === deviceId.value)
|
||||
const device = list?.find((item) => String(item.deviceId) === deviceId.value)
|
||||
if (!device) return
|
||||
if (device.deviceId != null) deviceId.value = String(device.deviceId)
|
||||
if (device.deviceNo) deviceNo.value = device.deviceNo
|
||||
if (device.mac) deviceMac.value = device.mac
|
||||
const parsedLocationId = parseId(device.locationId)
|
||||
if (parsedLocationId) locationId.value = parsedLocationId
|
||||
const parsedHomeId = parseId(device.homeId)
|
||||
if (parsedHomeId) homeId.value = parsedHomeId
|
||||
if (parsedHomeId && parsedLocationId) {
|
||||
const locations = await fetchRoomList(parsedHomeId)
|
||||
const matched = locations.find((item) => sameId(item.locationId, parsedLocationId))
|
||||
if (matched) locationName.value = matched.name
|
||||
}
|
||||
bluetoothConnected.value = isBluetoothConnected(device.bluetoothConnected)
|
||||
charging.value = device.charging === '1'
|
||||
if (device.status !== undefined) deviceStatus.value = device.status
|
||||
@@ -109,6 +155,14 @@ export function useDeviceDetail() {
|
||||
if (!deviceId.value || updating.value) return
|
||||
updating.value = true
|
||||
try {
|
||||
const bleCmd = patch.runMode != null
|
||||
? encodeRunModeBleCommand(patch.runMode)
|
||||
: patch.fanLevel != null
|
||||
? encodeFanLevelBleCommand(patch.fanLevel)
|
||||
: undefined
|
||||
if (bleCmd) {
|
||||
await sendBleCommand(bleCmd)
|
||||
}
|
||||
await updateDeviceRuntime({
|
||||
deviceId: deviceId.value,
|
||||
...patch,
|
||||
@@ -135,7 +189,21 @@ export function useDeviceDetail() {
|
||||
}
|
||||
|
||||
function onSettings() {
|
||||
uni.showToast({ title: '设备设置功能开发中', icon: 'none' })
|
||||
if (!deviceId.value) {
|
||||
uni.showToast({ title: '设备信息不完整', icon: 'none' })
|
||||
return
|
||||
}
|
||||
const q = [
|
||||
`deviceId=${deviceId.value}`,
|
||||
...(deviceNo.value ? [`deviceNo=${encodeURIComponent(deviceNo.value)}`] : []),
|
||||
`name=${encodeURIComponent(deviceName.value)}`,
|
||||
`locationName=${encodeURIComponent(locationName.value)}`,
|
||||
...(locationId.value ? [`locationId=${locationId.value}`] : []),
|
||||
...(homeId.value ? [`homeId=${homeId.value}`] : []),
|
||||
`model=${encodeURIComponent(productModel.value)}`,
|
||||
`bluetoothConnected=${bluetoothConnected.value ? '1' : '0'}`,
|
||||
].join('&')
|
||||
uni.navigateTo({ url: `/pages/device/settings/index?${q}` })
|
||||
}
|
||||
|
||||
function onFilterReplace() {
|
||||
@@ -143,18 +211,20 @@ export function useDeviceDetail() {
|
||||
filterReplaceDialogVisible.value = true
|
||||
}
|
||||
|
||||
function onConfirmFilterReplace() {
|
||||
async function onConfirmFilterReplace() {
|
||||
await sendBleCommand(Z2_BLE_CMD.FILTER_RESET)
|
||||
filterReplaced.value = true
|
||||
uni.showToast({ title: '操作成功', icon: 'success' })
|
||||
}
|
||||
|
||||
const { startSyncLoop } = useDeviceBluetoothSync({
|
||||
const { startSyncLoop, sendBleCommand } = useDeviceBluetoothSync({
|
||||
getDeviceId: () => deviceId.value,
|
||||
getMac: () => deviceMac.value,
|
||||
getDeviceName: () => deviceName.value,
|
||||
getRuntimePatch: () => ({
|
||||
runMode: runMode.value,
|
||||
fanLevel: fanLevel.value,
|
||||
tvoc: tvoc.value ?? undefined,
|
||||
tvoc: tvoc.value ?? (airQuality.value ? airQualityToTvoc(airQuality.value) : undefined),
|
||||
powerMode: powerMode.value,
|
||||
charging: charging.value ? '1' : '0',
|
||||
}),
|
||||
@@ -168,19 +238,29 @@ export function useDeviceDetail() {
|
||||
onSyncTextChange: (text) => {
|
||||
syncText.value = text
|
||||
},
|
||||
onBleRuntimeRead: (patch) => {
|
||||
applyBleRuntime(patch)
|
||||
},
|
||||
onSynced: async () => {
|
||||
await refreshDeviceMeta()
|
||||
const data = await fetchDeviceRuntime({ deviceId: deviceId.value })
|
||||
applyRuntime(data)
|
||||
},
|
||||
})
|
||||
|
||||
onLoad((query?: DeviceDetailRouteQuery) => {
|
||||
deviceId.value = Number(query?.deviceId) || 0
|
||||
deviceId.value = parseDeviceId(query?.deviceId)
|
||||
if (query?.deviceNo) {
|
||||
deviceNo.value = decodeURIComponent(String(query.deviceNo))
|
||||
}
|
||||
if (query?.name) deviceName.value = decodeURIComponent(String(query.name))
|
||||
if (query?.locationName) {
|
||||
locationName.value = decodeURIComponent(String(query.locationName))
|
||||
}
|
||||
const parsedLocationId = parseId(query?.locationId)
|
||||
if (parsedLocationId) locationId.value = parsedLocationId
|
||||
|
||||
const parsedHomeId = parseId(query?.homeId)
|
||||
if (parsedHomeId) homeId.value = parsedHomeId
|
||||
if (query?.model) productModel.value = decodeURIComponent(String(query.model))
|
||||
if (query?.status) {
|
||||
deviceStatus.value = String(query.status)
|
||||
isOnline.value = query.status === '1'
|
||||
|
||||
Reference in New Issue
Block a user