feat(equipment): 设备页新增 OTA 升级与初始化功能

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
17792275749
2026-09-14 10:38:52 +08:00
co-authored by Claude Opus 4.7
parent 109c9e1b70
commit 9332990a77
4 changed files with 173 additions and 0 deletions
+28
View File
@@ -423,6 +423,34 @@ class LeFuService {
})
}
// ─── OTA 升级 ─────────────────────────────
/**
* 下发 OTA 固件升级指令
* 只负责调用协议层 codeOtaUpdate,是否需要升级、版本比较等业务判断由调用方处理。
*/
otaUpdate(): Promise<void> {
return new Promise((resolve, reject) => {
if (!this._activeProtocol) { reject(new Error('设备未连接')); return }
this._activeProtocol.codeOtaUpdate((status: number) => {
status === 0 ? resolve() : reject(new Error('升级指令下发失败'))
})
})
}
// ─── 设备初始化 ───────────────────────────
/**
* 初始化设备信息
* 下发 codeClearDeviceData('00') 指令清除设备信息,区别于 clearDeviceMembers 的 '01'(清空用户数据)。
*/
initDevice(): Promise<void> {
return new Promise((resolve, reject) => {
if (!this._activeProtocol) { reject(new Error('设备未连接')); return }
this._activeProtocol.codeClearDeviceData('00', (res: number) => {
res === 0 ? resolve() : reject(new Error('请求失败'))
})
})
}
// ─── 用户同步 ─────────────────────────────
/**
* 清空设备内的用户数据
@@ -294,6 +294,41 @@
}
}
}
.device-update {
width: 100%;
gap: 10px;
display: flex;
flex-wrap: nowrap;
margin-top: 20rpx;
justify-content: space-around;
> view {
flex: 1;
height: 56rpx;
display: flex;
flex-wrap: nowrap;
align-items: center;
justify-content: center;
border-radius: 28rpx;
border: 2rpx solid #EAECF1;
> view:nth-of-type(1) {
width: 24rpx;
height: 24rpx;
display: flex;
align-items: center;
margin-right: 6rpx;
}
> view:nth-of-type(2) {
color: #77849E;
height: 24rpx;
font-size: 24rpx;
line-height: 24rpx;
}
}
}
}
/* 添加设备按钮 */
+97
View File
@@ -39,6 +39,25 @@ const uploadHistoryData = (data: Record<string, any>) =>
/** SN 转 MAC:每两位加一个冒号(如 CEE9052031AC → CE:E9:05:20:31:AC */
const formatMac = (sn: string) => sn.match(/.{2}/g)?.join(':') ?? ''
/** 服务端固件版本(weighingScale/v2/firmware/version 接口 result 字段) */
interface FirmwareVersion {
mcuVersion: string
bleVersion: string
wifiVersion: string
resVersion: string
}
/** 获取服务端最新固件版本 */
const getFirmwareVersion = (modelNumber: string) =>
get<FirmwareVersion>('weighingScale/v2/firmware/version', { type: modelNumber }, { loading: false })
/** 判断服务端版本是否比设备版本新(两端都去掉点号后转数字比较) */
const isServerNewer = (deviceVer: string, serverVer: string) => {
const dn = parseInt(deviceVer, 10)
const sn = parseInt((serverVer || '').replace(/\./g, ''), 10)
return sn > dn
}
/** 展示用设备项 */
interface DeviceItem {
id: string
@@ -327,6 +346,84 @@ Page({
})
},
/** OTA 升级:拉服务端最新版本,比较后下发升级指令 */
onTapOta() {
const deviceInfo = leFuService.deviceInfo
if (!leFuService.isConnected) {
wx.showToast({ title: '请先连接设备', icon: 'none' })
return
}
if (!deviceInfo) {
wx.showToast({ title: '固件信息未就绪', icon: 'none' })
return
}
const { firmwareRevision, modelNumber } = deviceInfo
const [mcuVer, bleVer, wifiVer, resVer] = (firmwareRevision || '').split('.')
console.log('[OTA] 设备版本', { mcuVer, bleVer, wifiVer, resVer })
this.setData({ loadingShow: true, loadingTitle: '正在检测新版本' })
getFirmwareVersion(modelNumber)
.then((res) => {
const { mcuVersion, bleVersion, wifiVersion, resVersion } = res.result
console.log('[OTA] 服务端版本', { mcuVersion, bleVersion, wifiVersion, resVersion })
const needUpdate = isServerNewer(mcuVer, mcuVersion)
|| isServerNewer(bleVer, bleVersion)
|| isServerNewer(wifiVer, wifiVersion)
|| isServerNewer(resVer, resVersion)
if (!needUpdate) {
this.setData({ loadingShow: false })
wx.showToast({ title: '已是最新版本', icon: 'none' })
return
}
this.setData({ loadingTitle: '正在升级' })
leFuService.otaUpdate()
.then(() => {
// 升级后设备重启,清空连接信息
leFuService.disconnect()
this.setData({ loadingShow: false })
wx.showToast({ title: '升级指令已下发', icon: 'success' })
})
.catch(() => {
this.setData({ loadingShow: false })
wx.showToast({ title: '升级失败', icon: 'none' })
})
})
.catch(() => {
this.setData({ loadingShow: false })
})
},
/** 初始化设备信息 */
onTapInit() {
if (!leFuService.isConnected) {
wx.showToast({ title: '请先连接设备', icon: 'none' })
return
}
wx.showModal({
title: '初始化设备',
content: '是否确认初始化设备信息?',
cancelText: '取消',
confirmText: '确定',
confirmColor: '#F24439',
success: (res) => {
if (!res.confirm) return
this.setData({ loadingShow: true, loadingTitle: '正在初始化设备' })
leFuService.initDevice()
.then(() => {
// 初始化后设备归零,清空连接信息
leFuService.disconnect()
this.setData({ loadingShow: false })
wx.showToast({ title: '初始化完成', icon: 'success' })
})
.catch((err: Error) => {
this.setData({ loadingShow: false })
wx.showToast({ title: err?.message || '初始化失败', icon: 'none' })
})
},
})
},
/** 处理历史数据并上传到后台 */
_uploadHistory(mainUserId: string, dataList: any[], done?: () => void) {
if (!dataList || !dataList.length) {
@@ -107,6 +107,19 @@
</view>
</block>
</view>
<block wx:if="{{item.status}}">
<view class="device-update">
<view bind:tap="onTapOta">
<view></view>
<view>OTA升级</view>
</view>
<view bind:tap="onTapInit">
<view></view>
<view>初始化</view>
</view>
</view>
</block>
</view>
</block>
</block>