[AI Generated]: feat(*): 补全分享功能、新增 lefu OTA 升级封装及多环境请求配置
This commit is contained in:
@@ -8,4 +8,6 @@ export type {
|
||||
LeFuConfig,
|
||||
DeviceSetting,
|
||||
DeviceMember,
|
||||
DeviceInfo,
|
||||
FirmwareVersion,
|
||||
} from './types'
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { get } from '../utils/request/index'
|
||||
import type {
|
||||
RawDevice,
|
||||
LeFuPlugin,
|
||||
@@ -9,14 +10,18 @@ import type {
|
||||
LeFuConfig,
|
||||
DeviceSetting,
|
||||
DeviceMember,
|
||||
DeviceInfo,
|
||||
FirmwareVersion,
|
||||
} from './types'
|
||||
|
||||
/** 乐福 SDK 配置 */
|
||||
const LEFU_CONFIG: LeFuConfig = {
|
||||
key: 'lefudc91611833e18d94',
|
||||
secret: 'eQ50JYimMp0X7uhNLj6D3lTEk4TUUvEG7dvaqKM9t1U=',
|
||||
domain1: 'http://device.shuziweidao.com:80/gateway',
|
||||
domain2: 'http://device.shuziweidao.com:80/weight',
|
||||
domain1: 'http://192.168.1.31:8889', // 测试环境(老版本 无鉴权)
|
||||
domain2: 'http://192.168.1.31:8889/weight', // 测试环境(新版本 有鉴权)
|
||||
// domain1: 'http://device.shuziweidao.com:80/gateway', // 正式环境
|
||||
// domain2: 'http://device.shuziweidao.com:80/weight', // 正式环境
|
||||
}
|
||||
|
||||
/** 支持的设备型号配置列表(5款) */
|
||||
@@ -76,6 +81,9 @@ class LeFuService {
|
||||
/** 保活心跳定时器 */
|
||||
private _keepAliveTimer: number | null = null
|
||||
|
||||
/** 连接后从 deviceInfo 事件获取的固件信息 */
|
||||
private _deviceInfo: DeviceInfo | null = null
|
||||
|
||||
/** 回调注册(页面通过 on* 方法注册,stopScan 时清空) */
|
||||
private _onDevicesListCb: ((devices: ScannedDevice[]) => void) | null = null
|
||||
private _onConnectStateCb: ((state: string) => void) | null = null
|
||||
@@ -161,6 +169,11 @@ class LeFuService {
|
||||
this._activeProtocol = res
|
||||
})
|
||||
|
||||
// 连接成功后获取固件信息(用于后续 OTA 版本比对)
|
||||
plugin.bus.subscribe('deviceInfo', (res: DeviceInfo) => {
|
||||
this._deviceInfo = res
|
||||
})
|
||||
|
||||
// 连接成功
|
||||
plugin.bus.subscribe('deviceConnect', () => {
|
||||
this._reconnectCount = 0
|
||||
@@ -225,6 +238,7 @@ class LeFuService {
|
||||
this._stopReconnectTimer()
|
||||
this._p.Blue.stop()
|
||||
this._activeProtocol = null
|
||||
this._deviceInfo = null
|
||||
this._onDevicesListCb = null
|
||||
this._onConnectStateCb = null
|
||||
this._onProgressCb = null
|
||||
@@ -326,6 +340,61 @@ class LeFuService {
|
||||
})
|
||||
}
|
||||
|
||||
// ─── OTA 升级 ─────────────────────────────────
|
||||
|
||||
/**
|
||||
* 检查固件版本并按需触发 OTA 升级
|
||||
* @returns true = 已触发升级,false = 无需升级
|
||||
* 调用时机由业务层决定,不自动触发
|
||||
*/
|
||||
checkAndUpgrade(): Promise<boolean> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!this._activeProtocol) {
|
||||
reject(new Error('[LeFu] 设备未连接'))
|
||||
return
|
||||
}
|
||||
if (!this._deviceInfo) {
|
||||
reject(new Error('[LeFu] 固件信息未就绪,请稍后重试'))
|
||||
return
|
||||
}
|
||||
|
||||
const { firmwareRevision, modelNumber } = this._deviceInfo
|
||||
const [mcuVer, bleVer, wifiVer, resVer] = (firmwareRevision || '').split('.')
|
||||
|
||||
// 设备单段版本(如 "006")vs 服务端 x.y.z 格式(如 "0.0.6")比对
|
||||
const isServerNewer = (deviceVer: string, serverVer: string): boolean => {
|
||||
const deviceNum = parseInt(deviceVer, 10)
|
||||
const serverNum = (serverVer || '').split('.').map(v => parseInt(v, 10)).reduce((acc, v) => acc * 1000 + v, 0)
|
||||
return serverNum > deviceNum
|
||||
}
|
||||
|
||||
get<FirmwareVersion>('weighingScale/v2/firmware/version', { type: modelNumber }, { loading: false })
|
||||
.then(res => {
|
||||
const { mcuVersion, bleVersion, wifiVersion, resVersion } = res.result
|
||||
const needUpdate =
|
||||
isServerNewer(mcuVer, mcuVersion) ||
|
||||
isServerNewer(bleVer, bleVersion) ||
|
||||
isServerNewer(wifiVer, wifiVersion) ||
|
||||
isServerNewer(resVer, resVersion)
|
||||
|
||||
if (!needUpdate) {
|
||||
resolve(false)
|
||||
return
|
||||
}
|
||||
|
||||
// status: false = 成功,true = 失败
|
||||
this._activeProtocol.codeOtaUpdate((status: boolean) => {
|
||||
if (!status) {
|
||||
resolve(true)
|
||||
} else {
|
||||
reject(new Error('[LeFu] OTA 升级失败'))
|
||||
}
|
||||
})
|
||||
})
|
||||
.catch(reject)
|
||||
})
|
||||
}
|
||||
|
||||
// ─── 私有:保活与重连 ─────────────────────────
|
||||
|
||||
private _startKeepAlive(): void {
|
||||
|
||||
@@ -80,6 +80,23 @@ export interface LeFuConfig {
|
||||
domain2: string
|
||||
}
|
||||
|
||||
/** 设备固件信息(deviceInfo 事件返回) */
|
||||
export interface DeviceInfo {
|
||||
/** 固件版本号,格式:mcu.ble.wifi.res,如 "006.005.004.305" */
|
||||
firmwareRevision: string
|
||||
/** 设备型号,用于请求服务端版本接口 */
|
||||
modelNumber: string
|
||||
[key: string]: any
|
||||
}
|
||||
|
||||
/** 服务端固件版本(firmware/version 接口 result 字段) */
|
||||
export interface FirmwareVersion {
|
||||
mcuVersion: string
|
||||
bleVersion: string
|
||||
wifiVersion: string
|
||||
resVersion: string
|
||||
}
|
||||
|
||||
/** 下发给设备的成员信息(lefu 层使用,业务层转换后传入) */
|
||||
export interface DeviceMember {
|
||||
/** 成员唯一 ID */
|
||||
|
||||
Reference in New Issue
Block a user