更新100J代码
This commit is contained in:
@ -41,6 +41,13 @@ class HBY100JProtocol {
|
||||
}
|
||||
}
|
||||
|
||||
/** 协议单字节:界面常传字符串或 -1,必须转成 0~255 */
|
||||
_u8(val, fallback = 0) {
|
||||
const n = Number(val);
|
||||
if (!Number.isFinite(n) || n < 0) return fallback & 0xFF;
|
||||
return n & 0xFF;
|
||||
}
|
||||
|
||||
onNotify(callback) {
|
||||
this.onNotifyCallback = callback;
|
||||
}
|
||||
@ -183,12 +190,14 @@ class HBY100JProtocol {
|
||||
getBasicInfo() { return this.sendBleData(0x02, []); }
|
||||
getLocation() { return this.sendBleData(0x03, []); }
|
||||
getPowerStatus() { return this.sendBleData(0x04, []); }
|
||||
setVoiceBroadcast(enable) { return this.sendBleData(0x06, [enable]); }
|
||||
setVolume(volume) { return this.sendBleData(0x09, [volume]); }
|
||||
setStrobeMode(enable, mode) { return this.sendBleData(0x0A, [enable, mode]); }
|
||||
setStrobeFrequency(frequency) { return this.sendBleData(0x0B, [frequency]); }
|
||||
setForceAlarm(enable, mode) { return this.sendBleData(0x0C, [enable, mode]); }
|
||||
setLightBrightness(red, blue = 0, yellow = 0) { return this.sendBleData(0x0D, [red, blue, yellow]); }
|
||||
setVoiceBroadcast(enable) { return this.sendBleData(0x06, [this._u8(enable)]); }
|
||||
setVolume(volume) { return this.sendBleData(0x09, [this._u8(volume)]); }
|
||||
setStrobeMode(enable, mode) { return this.sendBleData(0x0A, [this._u8(enable), this._u8(mode)]); }
|
||||
setStrobeFrequency(frequency) { return this.sendBleData(0x0B, [this._u8(frequency)]); }
|
||||
setForceAlarm(enable, mode) { return this.sendBleData(0x0C, [this._u8(enable), this._u8(mode)]); }
|
||||
setLightBrightness(red, blue = 0, yellow = 0) {
|
||||
return this.sendBleData(0x0D, [this._u8(red), this._u8(blue), this._u8(yellow)]);
|
||||
}
|
||||
getCurrentWorkMode() { return this.sendBleData(0x0E, []); }
|
||||
|
||||
// 0x05 文件上传:分片传输,协议 FA 05 [fileType] [phase] [data...] FF
|
||||
@ -274,16 +283,43 @@ class HBY100JProtocol {
|
||||
// 本地路径:无网络时直接读取
|
||||
readFromPath(fileUrlOrLocalPath);
|
||||
} else {
|
||||
// 网络 URL:优先用 uni.request 直接拉取 ArrayBuffer(类似 100 设备,无文件 IO),失败再走 downloadFile
|
||||
// 网络 URL:优先用 uni.request 拉取;加超时避免断网时进度长期卡在 1%~2%
|
||||
let fetchUrl = fileUrlOrLocalPath;
|
||||
if (fetchUrl.startsWith('http://')) fetchUrl = 'https://' + fetchUrl.slice(7);
|
||||
if (onProgress) onProgress(2);
|
||||
uni.request({
|
||||
url: fetchUrl,
|
||||
method: 'GET',
|
||||
responseType: 'arraybuffer',
|
||||
timeout: 60000,
|
||||
success: (res) => {
|
||||
const fallbackDownload = () => {
|
||||
uni.downloadFile({
|
||||
url: fetchUrl,
|
||||
success: (res) => {
|
||||
if (res.statusCode !== 200 || !res.tempFilePath) {
|
||||
if (onProgress) onProgress(0);
|
||||
reject(new Error('下载失败: ' + (res.statusCode || '无路径')));
|
||||
return;
|
||||
}
|
||||
Common.moveFileToDownloads(res.tempFilePath).then((p) => readFromPath(p)).catch(() => readFromPath(res.tempFilePath));
|
||||
},
|
||||
fail: (err) => {
|
||||
if (onProgress) onProgress(0);
|
||||
reject(err || new Error('下载失败'));
|
||||
}
|
||||
});
|
||||
};
|
||||
const reqTimeoutMs = 20000;
|
||||
const reqPromise = new Promise((resolveReq, rejectReq) => {
|
||||
uni.request({
|
||||
url: fetchUrl,
|
||||
method: 'GET',
|
||||
responseType: 'arraybuffer',
|
||||
timeout: reqTimeoutMs,
|
||||
success: (res) => resolveReq(res),
|
||||
fail: (e) => rejectReq(e || new Error('请求失败'))
|
||||
});
|
||||
});
|
||||
const timeoutPromise = new Promise((_, rejectT) => {
|
||||
setTimeout(() => rejectT(new Error('拉取语音超时')), reqTimeoutMs + 2000);
|
||||
});
|
||||
Promise.race([reqPromise, timeoutPromise])
|
||||
.then((res) => {
|
||||
if (res.statusCode === 200 && res.data) {
|
||||
const bytes = res.data instanceof ArrayBuffer ? new Uint8Array(res.data) : new Uint8Array(res.data || []);
|
||||
if (bytes.length > 0) {
|
||||
@ -295,22 +331,8 @@ class HBY100JProtocol {
|
||||
}
|
||||
}
|
||||
fallbackDownload();
|
||||
},
|
||||
fail: () => fallbackDownload()
|
||||
});
|
||||
const fallbackDownload = () => {
|
||||
uni.downloadFile({
|
||||
url: fetchUrl,
|
||||
success: (res) => {
|
||||
if (res.statusCode !== 200 || !res.tempFilePath) {
|
||||
reject(new Error('下载失败: ' + (res.statusCode || '无路径')));
|
||||
return;
|
||||
}
|
||||
Common.moveFileToDownloads(res.tempFilePath).then((p) => readFromPath(p)).catch(() => readFromPath(res.tempFilePath));
|
||||
},
|
||||
fail: (err) => reject(err)
|
||||
});
|
||||
};
|
||||
})
|
||||
.catch(() => fallbackDownload());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user