Compare commits
4 Commits
f86e6a4fb7
...
ac59e28281
| Author | SHA1 | Date | |
|---|---|---|---|
| ac59e28281 | |||
| f943bb9b09 | |||
| a2680fc14d | |||
| ebe126d826 |
@ -1,4 +1,5 @@
|
|||||||
import request from '@/utils/request'
|
import request from '@/utils/request'
|
||||||
|
import Common from '@/utils/Common.js'
|
||||||
|
|
||||||
// ================== 蓝牙协议封装类 ==================
|
// ================== 蓝牙协议封装类 ==================
|
||||||
class HBY100JProtocol {
|
class HBY100JProtocol {
|
||||||
@ -205,45 +206,111 @@ class HBY100JProtocol {
|
|||||||
return reject(new Error('缺少文件地址或本地路径'));
|
return reject(new Error('缺少文件地址或本地路径'));
|
||||||
}
|
}
|
||||||
const isLocalPath = !/^https?:\/\//i.test(fileUrlOrLocalPath);
|
const isLocalPath = !/^https?:\/\//i.test(fileUrlOrLocalPath);
|
||||||
|
if (onProgress) onProgress(1);
|
||||||
const readFromPath = (path) => {
|
const readFromPath = (path) => {
|
||||||
|
const doSend = (bytes) => {
|
||||||
|
this._sendVoiceChunks(bytes, fileType, CHUNK_SIZE, onProgress)
|
||||||
|
.then(resolve).catch(reject);
|
||||||
|
};
|
||||||
|
// App 端 getFileSystemManager 未实现,直接用 plus.io.requestFileSystem+getFile
|
||||||
|
readFromPathPlus(path, doSend, reject);
|
||||||
|
};
|
||||||
|
const readFileEntry = (entry, doSend, reject) => {
|
||||||
|
entry.file((file) => {
|
||||||
|
const reader = new plus.io.FileReader();
|
||||||
|
reader.onloadend = (e) => {
|
||||||
|
try {
|
||||||
|
const buf = e.target.result;
|
||||||
|
const bytes = new Uint8Array(buf);
|
||||||
|
doSend(bytes);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('[100J-蓝牙] 读取ArrayBuffer异常:', err);
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
reader.onerror = () => reject(new Error('读取文件失败'));
|
||||||
|
reader.readAsArrayBuffer(file);
|
||||||
|
}, (err) => reject(err));
|
||||||
|
};
|
||||||
|
const readFromPathPlus = (path, doSend, reject) => {
|
||||||
if (typeof plus === 'undefined' || !plus.io) {
|
if (typeof plus === 'undefined' || !plus.io) {
|
||||||
|
console.error('[100J-蓝牙] 当前环境不支持文件读取(plus.io)');
|
||||||
reject(new Error('当前环境不支持文件读取'));
|
reject(new Error('当前环境不支持文件读取'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
plus.io.resolveLocalFileSystemURL(path, (entry) => {
|
// _downloads/ 用 requestFileSystem+getFile(避免 resolveLocalFileSystemURL 卡住)
|
||||||
entry.file((file) => {
|
if (path && path.startsWith('_downloads/')) {
|
||||||
const reader = new plus.io.FileReader();
|
const fileName = path.replace(/^_downloads\//, '');
|
||||||
reader.onloadend = (e) => {
|
plus.io.requestFileSystem(plus.io.PUBLIC_DOWNLOADS, (fs) => {
|
||||||
try {
|
fs.root.getFile(fileName, {}, (entry) => readFileEntry(entry, doSend, reject), (err) => reject(err));
|
||||||
const buf = e.target.result;
|
|
||||||
const bytes = new Uint8Array(buf);
|
|
||||||
this._sendVoiceChunks(bytes, fileType, CHUNK_SIZE, onProgress)
|
|
||||||
.then(resolve).catch(reject);
|
|
||||||
} catch (err) {
|
|
||||||
reject(err);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
reader.onerror = () => reject(new Error('读取文件失败'));
|
|
||||||
reader.readAsArrayBuffer(file);
|
|
||||||
}, (err) => reject(err));
|
}, (err) => reject(err));
|
||||||
}, (err) => reject(err));
|
return;
|
||||||
|
}
|
||||||
|
// _doc/ 用 requestFileSystem(PRIVATE_DOC),逐级 getDirectory 再 getFile(嵌套路径兼容)
|
||||||
|
if (path && path.startsWith('_doc/')) {
|
||||||
|
const relPath = path.replace(/^_doc\//, '');
|
||||||
|
const parts = relPath.split('/');
|
||||||
|
const fileName = parts.pop();
|
||||||
|
const dirs = parts;
|
||||||
|
plus.io.requestFileSystem(plus.io.PRIVATE_DOC, (fs) => {
|
||||||
|
let cur = fs.root;
|
||||||
|
const next = (i) => {
|
||||||
|
if (i >= dirs.length) {
|
||||||
|
cur.getFile(fileName, { create: false }, (entry) => readFileEntry(entry, doSend, reject), (err) => reject(err));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
cur.getDirectory(dirs[i], { create: false }, (dir) => { cur = dir; next(i + 1); }, (err) => reject(err));
|
||||||
|
};
|
||||||
|
next(0);
|
||||||
|
}, (err) => reject(err));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 其他路径兜底
|
||||||
|
let resolvePath = path;
|
||||||
|
if (path && path.startsWith('/') && !path.startsWith('file://')) resolvePath = 'file://' + path;
|
||||||
|
plus.io.resolveLocalFileSystemURL(resolvePath, (entry) => readFileEntry(entry, doSend, reject), (err) => reject(err));
|
||||||
};
|
};
|
||||||
if (isLocalPath) {
|
if (isLocalPath) {
|
||||||
// 本地路径:无网络时直接读取
|
// 本地路径:无网络时直接读取
|
||||||
readFromPath(fileUrlOrLocalPath);
|
readFromPath(fileUrlOrLocalPath);
|
||||||
} else {
|
} else {
|
||||||
// 网络 URL:需下载后读取
|
// 网络 URL:优先用 uni.request 直接拉取 ArrayBuffer(类似 100 设备,无文件 IO),失败再走 downloadFile
|
||||||
uni.downloadFile({
|
let fetchUrl = fileUrlOrLocalPath;
|
||||||
url: 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) => {
|
success: (res) => {
|
||||||
if (res.statusCode !== 200) {
|
if (res.statusCode === 200 && res.data) {
|
||||||
reject(new Error('下载失败: ' + res.statusCode));
|
const bytes = res.data instanceof ArrayBuffer ? new Uint8Array(res.data) : new Uint8Array(res.data || []);
|
||||||
return;
|
if (bytes.length > 0) {
|
||||||
|
const doSend = (b) => {
|
||||||
|
this._sendVoiceChunks(b, fileType, CHUNK_SIZE, onProgress).then(resolve).catch(reject);
|
||||||
|
};
|
||||||
|
doSend(bytes);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
readFromPath(res.tempFilePath);
|
fallbackDownload();
|
||||||
},
|
},
|
||||||
fail: (err) => reject(err)
|
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)
|
||||||
|
});
|
||||||
|
};
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -253,6 +320,7 @@ class HBY100JProtocol {
|
|||||||
const ft = (fileType & 0xFF) || 1;
|
const ft = (fileType & 0xFF) || 1;
|
||||||
const DELAY_AFTER_START = 80; // 开始包后、等设备响应后再发的缓冲(ms)
|
const DELAY_AFTER_START = 80; // 开始包后、等设备响应后再发的缓冲(ms)
|
||||||
const DELAY_PACKET = 80; // 数据包间延时(ms),参考6155
|
const DELAY_PACKET = 80; // 数据包间延时(ms),参考6155
|
||||||
|
if (onProgress) onProgress(1);
|
||||||
const bleToolPromise = import('@/utils/BleHelper.js').then(m => m.default.getBleTool());
|
const bleToolPromise = import('@/utils/BleHelper.js').then(m => m.default.getBleTool());
|
||||||
const send = (dataBytes) => {
|
const send = (dataBytes) => {
|
||||||
const buf = new ArrayBuffer(dataBytes.length + 3);
|
const buf = new ArrayBuffer(dataBytes.length + 3);
|
||||||
@ -268,8 +336,8 @@ class HBY100JProtocol {
|
|||||||
const startData = [ft, 0, total & 0xFF, (total >> 8) & 0xFF, (total >> 16) & 0xFF, (total >> 24) & 0xFF];
|
const startData = [ft, 0, total & 0xFF, (total >> 8) & 0xFF, (total >> 16) & 0xFF, (total >> 24) & 0xFF];
|
||||||
const waitPromise = this.waitForFileResponse(1000);
|
const waitPromise = this.waitForFileResponse(1000);
|
||||||
return send(startData)
|
return send(startData)
|
||||||
.then(() => waitPromise)
|
.then(() => { if (onProgress) onProgress(3); return waitPromise; })
|
||||||
.then(() => delay(DELAY_AFTER_START))
|
.then(() => { if (onProgress) onProgress(5); return delay(DELAY_AFTER_START); })
|
||||||
.then(() => {
|
.then(() => {
|
||||||
let seq = 0;
|
let seq = 0;
|
||||||
const sendNext = (offset) => {
|
const sendNext = (offset) => {
|
||||||
@ -303,6 +371,11 @@ export function updateBleStatus(isConnected, bleDeviceId, deviceId) {
|
|||||||
console.log('[100J] 蓝牙状态:', isConnected ? '已连接(后续指令走蓝牙)' : '已断开(后续指令走4G)', { bleDeviceId: bleDeviceId || '-', deviceId });
|
console.log('[100J] 蓝牙状态:', isConnected ? '已连接(后续指令走蓝牙)' : '已断开(后续指令走4G)', { bleDeviceId: bleDeviceId || '-', deviceId });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 暴露给页面:获取当前蓝牙连接状态(用于跨页面传递,确保语音管理等子页走蓝牙优先)
|
||||||
|
export function getBleStatus() {
|
||||||
|
return { isConnected: protocolInstance.isBleConnected, bleDeviceId: protocolInstance.bleDeviceId, deviceId: protocolInstance.deviceId };
|
||||||
|
}
|
||||||
|
|
||||||
// 暴露给页面:解析蓝牙接收到的数据
|
// 暴露给页面:解析蓝牙接收到的数据
|
||||||
export function parseBleData(buffer) {
|
export function parseBleData(buffer) {
|
||||||
return protocolInstance.parseBleData(buffer);
|
return protocolInstance.parseBleData(buffer);
|
||||||
@ -322,6 +395,29 @@ export function fetchBleLocation() {
|
|||||||
return protocolInstance.getLocation();
|
return protocolInstance.getLocation();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 等待蓝牙连接(扫描中时轮询,设备页可能在后台完成连接,100J 扫描约 15s)
|
||||||
|
function waitForBleConnection(maxWaitMs = 12000, intervalMs = 500) {
|
||||||
|
if (protocolInstance.isBleConnected && protocolInstance.bleDeviceId) return Promise.resolve(true);
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
const start = Date.now();
|
||||||
|
const tick = () => {
|
||||||
|
if (protocolInstance.isBleConnected && protocolInstance.bleDeviceId) {
|
||||||
|
console.log('[100J] 等待蓝牙连接成功');
|
||||||
|
resolve(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (Date.now() - start >= maxWaitMs) {
|
||||||
|
console.log('[100J] 等待蓝牙连接超时,将走4G');
|
||||||
|
resolve(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setTimeout(tick, intervalMs);
|
||||||
|
};
|
||||||
|
console.log('[100J] 蓝牙未连接,等待扫描/连接中...', maxWaitMs, 'ms');
|
||||||
|
tick();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// 暴露给页面:尝试重连蓝牙(优先策略:断线后发指令前先尝试重连)
|
// 暴露给页面:尝试重连蓝牙(优先策略:断线后发指令前先尝试重连)
|
||||||
export function tryReconnectBle(timeoutMs = 2500) {
|
export function tryReconnectBle(timeoutMs = 2500) {
|
||||||
if (protocolInstance.isBleConnected) return Promise.resolve(true);
|
if (protocolInstance.isBleConnected) return Promise.resolve(true);
|
||||||
@ -382,8 +478,8 @@ export function deviceDeleteAudioFile(params) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新语音,使用语音(优先蓝牙:有 fileUrl 或 localPath 且蓝牙连接时通过蓝牙上传,否则走 4G)
|
// 更新语音/使用语音:蓝牙优先,4G 兜底(不影响原有 4G 音频下发)
|
||||||
// localPath:无网络时本地文件路径,可直接通过蓝牙发送
|
// 有 fileUrl 或 localPath 且蓝牙可用时走蓝牙;否则或蓝牙失败时走 4G(与原先逻辑一致)
|
||||||
export function deviceUpdateVoice(data) {
|
export function deviceUpdateVoice(data) {
|
||||||
const httpExec = () => request({
|
const httpExec = () => request({
|
||||||
url: `/app/hby100j/device/updateVoice`,
|
url: `/app/hby100j/device/updateVoice`,
|
||||||
@ -396,10 +492,12 @@ export function deviceUpdateVoice(data) {
|
|||||||
const hasFileUrl = fileUrl && typeof fileUrl === 'string' && fileUrl.length > 0;
|
const hasFileUrl = fileUrl && typeof fileUrl === 'string' && fileUrl.length > 0;
|
||||||
const fileSource = hasLocalPath ? localPath : (hasFileUrl ? fileUrl : null);
|
const fileSource = hasLocalPath ? localPath : (hasFileUrl ? fileUrl : null);
|
||||||
if (!fileSource) {
|
if (!fileSource) {
|
||||||
return httpExec(); // 无文件源直接走 4G
|
console.log('[100J] 语音上传:无 fileUrl/localPath,走 4G');
|
||||||
|
return httpExec(); // 无文件源:直接 4G(原有逻辑)
|
||||||
}
|
}
|
||||||
const bleExec = () => protocolInstance.uploadVoiceFileBle(fileSource, 1, data.onProgress); // fileType=1 语音
|
console.log('[100J] 语音上传:有文件源,蓝牙优先', { isBleConnected: protocolInstance.isBleConnected, bleDeviceId: protocolInstance.bleDeviceId || '-' });
|
||||||
return execWithBleFirst(bleExec, httpExec, '语音文件上传');
|
const bleExec = () => protocolInstance.uploadVoiceFileBle(fileSource, 1, data.onProgress);
|
||||||
|
return execWithBleFirst(bleExec, httpExec, '语音文件上传', data.onWaiting);
|
||||||
}
|
}
|
||||||
// 100J信息
|
// 100J信息
|
||||||
export function deviceDetail(id) {
|
export function deviceDetail(id) {
|
||||||
@ -409,31 +507,22 @@ export function deviceDetail(id) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 优先蓝牙:未连接时先尝试重连;蓝牙发送失败时回退4G
|
// 蓝牙优先、4G 兜底:未连接时先等待扫描/连接,再尝试重连;蓝牙失败时回退 4G
|
||||||
function execWithBleFirst(bleExec, httpExec, logName) {
|
function execWithBleFirst(bleExec, httpExec, logName, onWaiting) {
|
||||||
const doBle = () => {
|
const doBle = () => bleExec().then(res => ({ ...(res || {}), _channel: 'ble' }));
|
||||||
return bleExec().then(res => ({ ...(res || {}), _channel: 'ble' }));
|
const do4G = () => httpExec().then(res => { res._channel = '4g'; return res; });
|
||||||
};
|
if (protocolInstance.isBleConnected && protocolInstance.bleDeviceId) {
|
||||||
const do4G = () => {
|
return doBle().catch(() => { console.log('[100J] 蓝牙失败,回退4G'); return do4G(); });
|
||||||
console.log('[100J-4G]', logName, '已通过HTTP发送', '(蓝牙不可用)');
|
}
|
||||||
return httpExec().then(res => { res._channel = '4g'; return res; });
|
// 无 bleDeviceId 时:可能扫描中,先等待连接(设备页在后台可能完成连接)
|
||||||
};
|
if (!protocolInstance.bleDeviceId) {
|
||||||
if (protocolInstance.isBleConnected) {
|
if (typeof onWaiting === 'function') onWaiting();
|
||||||
console.log('[100J-蓝牙]', logName, '(连接正常)');
|
return waitForBleConnection(12000).then(connected => {
|
||||||
return doBle().catch(err => {
|
return connected ? doBle().catch(() => { console.log('[100J] 蓝牙失败,回退4G'); return do4G(); }) : do4G();
|
||||||
console.log('[100J] 蓝牙发送失败,回退4G', err);
|
|
||||||
return do4G();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return tryReconnectBle(2500).then(reconnected => {
|
return tryReconnectBle(2500).then(reconnected => {
|
||||||
if (reconnected) {
|
return reconnected ? doBle().catch(() => { console.log('[100J] 蓝牙失败,回退4G'); return do4G(); }) : do4G();
|
||||||
console.log('[100J-蓝牙]', logName, '(重连成功)');
|
|
||||||
return doBle().catch(err => {
|
|
||||||
console.log('[100J] 蓝牙发送失败,回退4G', err);
|
|
||||||
return do4G();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return do4G();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -243,6 +243,7 @@
|
|||||||
deviceUpdateVolume,
|
deviceUpdateVolume,
|
||||||
deviceVoiceBroadcast,
|
deviceVoiceBroadcast,
|
||||||
updateBleStatus,
|
updateBleStatus,
|
||||||
|
getBleStatus,
|
||||||
parseBleData,
|
parseBleData,
|
||||||
fetchBlePowerStatus,
|
fetchBlePowerStatus,
|
||||||
fetchBleLocation
|
fetchBleLocation
|
||||||
@ -714,16 +715,17 @@
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
// 语音管理
|
// 语音管理(传递蓝牙状态,确保子页走蓝牙优先)
|
||||||
audioManager(item) {
|
audioManager(item) {
|
||||||
if (this.Status.apiType !== 'listA') {}
|
if (this.Status.apiType !== 'listA') {}
|
||||||
|
const ble = getBleStatus();
|
||||||
uni.navigateTo({
|
uni.navigateTo({
|
||||||
url: '/pages/100J/audioManager/AudioList',
|
url: '/pages/100J/audioManager/AudioList',
|
||||||
events: {},
|
events: {},
|
||||||
success: (res) => {
|
success: (res) => {
|
||||||
// 页面跳转成功后的回调函数
|
|
||||||
res.eventChannel.emit('deviceData', {
|
res.eventChannel.emit('deviceData', {
|
||||||
data: item
|
data: item,
|
||||||
|
ble
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@ -104,8 +104,10 @@
|
|||||||
deviceVoliceList,
|
deviceVoliceList,
|
||||||
videRenameAudioFile,
|
videRenameAudioFile,
|
||||||
deviceDeleteAudioFile,
|
deviceDeleteAudioFile,
|
||||||
deviceUpdateVoice
|
deviceUpdateVoice,
|
||||||
|
updateBleStatus
|
||||||
} from '@/api/100J/HBY100-J.js'
|
} from '@/api/100J/HBY100-J.js'
|
||||||
|
import { baseURL } from '@/utils/request.js'
|
||||||
import {
|
import {
|
||||||
showLoading,
|
showLoading,
|
||||||
hideLoading,
|
hideLoading,
|
||||||
@ -217,6 +219,10 @@
|
|||||||
console.log(rec, 'ressss');
|
console.log(rec, 'ressss');
|
||||||
this.blue = rec.ble;
|
this.blue = rec.ble;
|
||||||
this.device = rec.data;
|
this.device = rec.data;
|
||||||
|
// 同步蓝牙状态,确保语音上传走蓝牙优先
|
||||||
|
if (rec.ble && (rec.ble.isConnected || rec.ble.bleDeviceId)) {
|
||||||
|
updateBleStatus(!!rec.ble.isConnected, rec.ble.bleDeviceId || '', rec.data?.deviceId || '');
|
||||||
|
}
|
||||||
this.getinitData(rec.data.deviceId, true)
|
this.getinitData(rec.data.deviceId, true)
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -237,7 +243,9 @@
|
|||||||
if (!deviceId) return;
|
if (!deviceId) return;
|
||||||
const mergeLocal = (serverList) => {
|
const mergeLocal = (serverList) => {
|
||||||
const key = `100J_local_audio_${deviceId}`;
|
const key = `100J_local_audio_${deviceId}`;
|
||||||
|
const cacheKey = `100J_local_path_cache_${deviceId}`;
|
||||||
const localList = uni.getStorageSync(key) || [];
|
const localList = uni.getStorageSync(key) || [];
|
||||||
|
const pathCache = uni.getStorageSync(cacheKey) || {};
|
||||||
const localMapped = localList.map(item => ({
|
const localMapped = localList.map(item => ({
|
||||||
...item,
|
...item,
|
||||||
fileNameExt: item.name || '本地语音',
|
fileNameExt: item.name || '本地语音',
|
||||||
@ -246,7 +254,12 @@
|
|||||||
useStatus: 0,
|
useStatus: 0,
|
||||||
_isLocal: true
|
_isLocal: true
|
||||||
}));
|
}));
|
||||||
return [...localMapped, ...(serverList || [])];
|
const enriched = (serverList || []).map(item => {
|
||||||
|
const urlKey = item.fileUrl || item.url || item.filePath || item.audioUrl || item.ossUrl;
|
||||||
|
const localPath = pathCache[urlKey] || pathCache[item.id];
|
||||||
|
return localPath ? { ...item, localPath } : item;
|
||||||
|
});
|
||||||
|
return [...localMapped, ...enriched];
|
||||||
};
|
};
|
||||||
deviceVoliceList({ deviceId }).then((res) => {
|
deviceVoliceList({ deviceId }).then((res) => {
|
||||||
if (res.code == 200) {
|
if (res.code == 200) {
|
||||||
@ -486,11 +499,22 @@
|
|||||||
Apply(item, index) {
|
Apply(item, index) {
|
||||||
this.updateProgress = 0;
|
this.updateProgress = 0;
|
||||||
this.isUpdating = true;
|
this.isUpdating = true;
|
||||||
|
// 本地项优先用 localPath;云端项用 fileUrl(兼容多种字段名),相对路径补全 baseURL
|
||||||
|
let fileUrl = '';
|
||||||
|
let localPath = (item.localPath && typeof item.localPath === 'string') ? item.localPath : '';
|
||||||
|
if (!item._isLocal) {
|
||||||
|
const raw = item.fileUrl || item.url || item.filePath || item.audioUrl || item.ossUrl || '';
|
||||||
|
fileUrl = (typeof raw === 'string' && raw) ? (raw.startsWith('/') ? (baseURL + raw) : raw) : '';
|
||||||
|
} else {
|
||||||
|
// 本地项:localPath 优先,无则用 fileUrl(mergeLocal 中可能只有 fileUrl 存路径)
|
||||||
|
if (!localPath && item.fileUrl) localPath = item.fileUrl;
|
||||||
|
}
|
||||||
const data = {
|
const data = {
|
||||||
id: item.id,
|
id: item.id,
|
||||||
fileUrl: item._isLocal ? '' : (item.fileUrl || item.url),
|
fileUrl,
|
||||||
localPath: item._isLocal ? item.localPath : '',
|
localPath,
|
||||||
onProgress: (p) => { this.updateProgress = p; }
|
onProgress: (p) => { this.updateProgress = p; },
|
||||||
|
onWaiting: () => { uni.showToast({ title: '等待蓝牙连接中...', icon: 'none', duration: 2000 }); }
|
||||||
};
|
};
|
||||||
// 整体超时 60 秒(仅影响蓝牙上传,4G HTTP 很快返回)
|
// 整体超时 60 秒(仅影响蓝牙上传,4G HTTP 很快返回)
|
||||||
const overallTimer = setTimeout(() => {
|
const overallTimer = setTimeout(() => {
|
||||||
@ -502,11 +526,10 @@
|
|||||||
}, 60000);
|
}, 60000);
|
||||||
deviceUpdateVoice(data).then((RES) => {
|
deviceUpdateVoice(data).then((RES) => {
|
||||||
clearTimeout(overallTimer);
|
clearTimeout(overallTimer);
|
||||||
console.log(RES, 'RES');
|
|
||||||
if (RES.code == 200) {
|
if (RES.code == 200) {
|
||||||
// 蓝牙上传:进度已由 onProgress 更新,直接完成
|
// 蓝牙上传:进度已由 onProgress 更新,直接完成
|
||||||
if (RES._channel === 'ble') {
|
if (RES._channel === 'ble') {
|
||||||
uni.showToast({ title: '升级完成!', icon: 'success', duration: 2000 });
|
uni.showToast({ title: '音频上传成功', icon: 'success', duration: 2000 });
|
||||||
this.isUpdating = false;
|
this.isUpdating = false;
|
||||||
setTimeout(() => { uni.navigateBack(); }, 1500);
|
setTimeout(() => { uni.navigateBack(); }, 1500);
|
||||||
return;
|
return;
|
||||||
@ -514,7 +537,7 @@
|
|||||||
// 4G:订阅 MQTT 获取设备端进度,6 秒超时
|
// 4G:订阅 MQTT 获取设备端进度,6 秒超时
|
||||||
this.upgradeTimer = setTimeout(() => {
|
this.upgradeTimer = setTimeout(() => {
|
||||||
if (this.isUpdating) {
|
if (this.isUpdating) {
|
||||||
uni.showToast({ title: '升级进度同步超时', icon: 'none', duration: 2000 });
|
uni.showToast({ title: '音频进度同步超时', icon: 'none', duration: 2000 });
|
||||||
this.isUpdating = false;
|
this.isUpdating = false;
|
||||||
this.updateProgress = 0;
|
this.updateProgress = 0;
|
||||||
}
|
}
|
||||||
@ -530,7 +553,7 @@
|
|||||||
this.updateProgress = progress;
|
this.updateProgress = progress;
|
||||||
if (progress === 100) {
|
if (progress === 100) {
|
||||||
clearTimeout(this.upgradeTimer);
|
clearTimeout(this.upgradeTimer);
|
||||||
uni.showToast({ title: '升级完成!', icon: 'success', duration: 2000 });
|
uni.showToast({ title: '音频上传成功', icon: 'success', duration: 2000 });
|
||||||
this.isUpdating = false;
|
this.isUpdating = false;
|
||||||
setTimeout(() => { uni.navigateBack(); }, 1500);
|
setTimeout(() => { uni.navigateBack(); }, 1500);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -422,25 +422,42 @@
|
|||||||
}, 1200);
|
}, 1200);
|
||||||
},
|
},
|
||||||
// 无网络时保存到本地,供蓝牙直接发送(不依赖 OSS)
|
// 无网络时保存到本地,供蓝牙直接发送(不依赖 OSS)
|
||||||
|
// 将临时文件复制到持久化目录 _doc/100J_audio/,避免被系统清理
|
||||||
saveLocalForBle(filePath) {
|
saveLocalForBle(filePath) {
|
||||||
const deviceId = these.Status.ID;
|
const deviceId = these.Status.ID;
|
||||||
if (!deviceId) return;
|
if (!deviceId) return;
|
||||||
const item = {
|
const doSave = (persistentPath) => {
|
||||||
...these.cEdit,
|
const item = {
|
||||||
localPath: filePath,
|
...these.cEdit,
|
||||||
fileUrl: '',
|
localPath: persistentPath,
|
||||||
deviceId,
|
fileUrl: '',
|
||||||
id: 'local_' + these.cEdit.Id,
|
deviceId,
|
||||||
_createTime: these.cEdit.createTime || Common.DateFormat(new Date(), "yyyy年MM月dd日"),
|
id: 'local_' + these.cEdit.Id,
|
||||||
_isLocal: true
|
_createTime: these.cEdit.createTime || Common.DateFormat(new Date(), "yyyy年MM月dd日"),
|
||||||
|
_isLocal: true
|
||||||
|
};
|
||||||
|
const key = `100J_local_audio_${deviceId}`;
|
||||||
|
let list = uni.getStorageSync(key) || [];
|
||||||
|
list.unshift(item);
|
||||||
|
uni.setStorageSync(key, list);
|
||||||
|
these.AudioData.tempFilePath = "";
|
||||||
|
these.Status.isRecord = false;
|
||||||
|
uni.navigateBack();
|
||||||
};
|
};
|
||||||
const key = `100J_local_audio_${deviceId}`;
|
if (typeof plus !== 'undefined' && plus.io) {
|
||||||
let list = uni.getStorageSync(key) || [];
|
const fileName = 'audio_' + (these.cEdit.Id || Date.now()) + '.mp3';
|
||||||
list.unshift(item);
|
plus.io.resolveLocalFileSystemURL(filePath, (entry) => {
|
||||||
uni.setStorageSync(key, list);
|
plus.io.resolveLocalFileSystemURL('_doc/', (docEntry) => {
|
||||||
these.AudioData.tempFilePath = "";
|
docEntry.getDirectory('100J_audio', { create: true }, (dirEntry) => {
|
||||||
these.Status.isRecord = false;
|
entry.copyTo(dirEntry, fileName, (newEntry) => {
|
||||||
uni.navigateBack();
|
doSave(newEntry.fullPath);
|
||||||
|
}, () => { doSave(filePath); });
|
||||||
|
}, () => { doSave(filePath); });
|
||||||
|
}, () => { doSave(filePath); });
|
||||||
|
}, () => { doSave(filePath); });
|
||||||
|
} else {
|
||||||
|
doSave(filePath);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
// 保存录音并上传(已修复文件格式问题)
|
// 保存录音并上传(已修复文件格式问题)
|
||||||
uploadLuYin() {
|
uploadLuYin() {
|
||||||
@ -507,6 +524,19 @@
|
|||||||
}
|
}
|
||||||
const resData = JSON.parse(res.data);
|
const resData = JSON.parse(res.data);
|
||||||
if (resData.code === 200) {
|
if (resData.code === 200) {
|
||||||
|
// 缓存本地路径,Apply 时优先用本地文件走蓝牙,避免下载失败
|
||||||
|
const deviceId = these.Status.ID;
|
||||||
|
if (deviceId) {
|
||||||
|
const cacheKey = `100J_local_path_cache_${deviceId}`;
|
||||||
|
const d = resData.data;
|
||||||
|
const fileUrl = (d && typeof d === 'object' && d.fileUrl) || (typeof d === 'string' ? d : '');
|
||||||
|
if (filePath) {
|
||||||
|
let cache = uni.getStorageSync(cacheKey) || {};
|
||||||
|
if (fileUrl) cache[fileUrl] = filePath;
|
||||||
|
if (d && typeof d === 'object' && d.id) cache[d.id] = filePath;
|
||||||
|
uni.setStorageSync(cacheKey, cache);
|
||||||
|
}
|
||||||
|
}
|
||||||
// 合并两个存储操作
|
// 合并两个存储操作
|
||||||
Promise.all([
|
Promise.all([
|
||||||
new Promise((resolve, reject) => {
|
new Promise((resolve, reject) => {
|
||||||
|
|||||||
536
temp_hby100j_ebe126d.js
Normal file
536
temp_hby100j_ebe126d.js
Normal file
@ -0,0 +1,536 @@
|
|||||||
|
import request from '@/utils/request'
|
||||||
|
import Common from '@/utils/Common.js'
|
||||||
|
|
||||||
|
// ================== 钃濈墮鍗忚灏佽绫?==================
|
||||||
|
class HBY100JProtocol {
|
||||||
|
constructor() {
|
||||||
|
this.deviceId = ''; // 4G 鎺ュ彛鎵€闇€鐨?deviceId
|
||||||
|
this.isBleConnected = false;
|
||||||
|
this.bleDeviceId = ''; // 灏忕▼搴?APP涓繛鎺ヨ摑鐗欑殑 deviceId
|
||||||
|
|
||||||
|
// 钃濈墮鏈嶅姟涓庣壒寰佸€?UUID
|
||||||
|
this.SERVICE_UUID = '0000AE30-0000-1000-8000-00805F9B34FB'; // 0xAE30
|
||||||
|
this.WRITE_UUID = '0000AE03-0000-1000-8000-00805F9B34FB'; // 0xAE03
|
||||||
|
this.NOTIFY_UUID = '0000AE02-0000-1000-8000-00805F9B34FB'; // 0xAE02
|
||||||
|
|
||||||
|
this.onNotifyCallback = null;
|
||||||
|
this._fileResponseResolve = null; // 鏂囦欢涓婁紶鏃剁瓑寰呰澶?FB 05 鍝嶅簲
|
||||||
|
}
|
||||||
|
|
||||||
|
// 绛夊緟璁惧 FB 05 鍝嶅簲锛岃秴鏃跺悗浠?resolve锛堣澶囧彲鑳戒笉鍝嶅簲姣忓寘锛? waitForFileResponse(timeoutMs = 2000) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
const timer = setTimeout(() => {
|
||||||
|
if (this._fileResponseResolve) {
|
||||||
|
this._fileResponseResolve = null;
|
||||||
|
resolve(null);
|
||||||
|
}
|
||||||
|
}, timeoutMs);
|
||||||
|
this._fileResponseResolve = (result) => {
|
||||||
|
clearTimeout(timer);
|
||||||
|
this._fileResponseResolve = null;
|
||||||
|
resolve(result);
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
setBleConnectionStatus(status, bleDeviceId = '') {
|
||||||
|
this.isBleConnected = status;
|
||||||
|
if (bleDeviceId) {
|
||||||
|
this.bleDeviceId = bleDeviceId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onNotify(callback) {
|
||||||
|
this.onNotifyCallback = callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
parseBleData(buffer) {
|
||||||
|
const view = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
|
||||||
|
if (view.length < 3) return null;
|
||||||
|
|
||||||
|
const header = view[0];
|
||||||
|
const tail = view[view.length - 1];
|
||||||
|
|
||||||
|
// 5.1 杩炴帴钃濈墮璁惧涓诲姩涓婃姤 MAC 鍦板潃: FC + 6瀛楄妭MAC + FF
|
||||||
|
if (header === 0xFC && tail === 0xFF && view.length >= 8) {
|
||||||
|
const macBytes = view.slice(1, 7);
|
||||||
|
const macAddress = Array.from(macBytes).map(b => b.toString(16).padStart(2, '0').toUpperCase()).join(':');
|
||||||
|
const result = { type: 'mac', macAddress };
|
||||||
|
console.log('[100J-钃濈墮] 璁惧涓婃姤MAC:', macAddress, '鍘熷:', Array.from(view).map(b => b.toString(16).padStart(2, '0').toUpperCase()).join(' '));
|
||||||
|
if (this.onNotifyCallback) this.onNotifyCallback(result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (header !== 0xFB || tail !== 0xFF) return null; // 鏍¢獙澶村熬
|
||||||
|
|
||||||
|
const funcCode = view[1];
|
||||||
|
const data = view.slice(2, view.length - 1);
|
||||||
|
const hexStr = Array.from(view).map(b => b.toString(16).padStart(2, '0').toUpperCase()).join(' ');
|
||||||
|
|
||||||
|
let result = { funcCode, rawData: data };
|
||||||
|
|
||||||
|
switch (funcCode) {
|
||||||
|
case 0x01: result.resetType = data[0]; break;
|
||||||
|
case 0x02: break;
|
||||||
|
case 0x03:
|
||||||
|
// 5.4 鑾峰彇璁惧浣嶇疆锛氱粡搴?B+绾害8B 鍧囦负 float64锛岃澶囦富鍔ㄤ笂鎶?1鍒嗛挓)涓庝富鍔ㄦ煡璇㈠搷搴旀牸寮忕浉鍚? if (data.length >= 16) {
|
||||||
|
const lonBuf = new ArrayBuffer(8);
|
||||||
|
const latBuf = new ArrayBuffer(8);
|
||||||
|
new Uint8Array(lonBuf).set(data.slice(0, 8));
|
||||||
|
new Uint8Array(latBuf).set(data.slice(8, 16));
|
||||||
|
result.longitude = new DataView(lonBuf).getFloat64(0, true);
|
||||||
|
result.latitude = new DataView(latBuf).getFloat64(0, true);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 0x05:
|
||||||
|
// 05: 鏂囦欢鏇存柊鍝嶅簲 FB 05 [fileType] [status] FF锛宻tatus: 1=鎴愬姛 2=澶辫触
|
||||||
|
if (data.length >= 1) result.fileType = data[0];
|
||||||
|
if (data.length >= 2) result.fileStatus = data[1]; // 1=Success, 2=Failure
|
||||||
|
if (this._fileResponseResolve) this._fileResponseResolve(result);
|
||||||
|
break;
|
||||||
|
case 0x04:
|
||||||
|
// 5.5 鑾峰彇璁惧鐢垫簮鐘舵€? 鐢垫睜瀹归噺8B + 鐢靛帇8B + 鐧惧垎姣?B + 杞﹁浇鐢垫簮1B + 缁埅鏃堕棿2B(鍒嗛挓)
|
||||||
|
if (data.length >= 20) {
|
||||||
|
result.batteryPercentage = data[16];
|
||||||
|
result.vehiclePower = data[17];
|
||||||
|
result.batteryRemainingTime = data[18] | (data[19] << 8); // 灏忕搴忥紝鍗曚綅鍒嗛挓
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 0x06:
|
||||||
|
// 06: 璇煶鎾姤鍝嶅簲
|
||||||
|
result.voiceBroadcast = data[0];
|
||||||
|
break;
|
||||||
|
case 0x09:
|
||||||
|
// 09: 淇敼闊抽噺鍝嶅簲
|
||||||
|
result.volume = data[0];
|
||||||
|
break;
|
||||||
|
case 0x0A:
|
||||||
|
// 0A: 鐖嗛棯妯″紡鍝嶅簲
|
||||||
|
result.strobeEnable = data[0];
|
||||||
|
result.strobeMode = data[1];
|
||||||
|
break;
|
||||||
|
case 0x0B:
|
||||||
|
// 0B: 淇敼璀︾ず鐏垎闂鐜囧搷搴? result.strobeFrequency = data[0];
|
||||||
|
break;
|
||||||
|
case 0x0C:
|
||||||
|
// 0C: 寮哄埗澹板厜鎶ヨ鍝嶅簲
|
||||||
|
result.alarmEnable = data[0];
|
||||||
|
result.alarmMode = data[1];
|
||||||
|
break;
|
||||||
|
case 0x0D:
|
||||||
|
// 0D: 璀︾ず鐏?LED 浜害璋冭妭鍝嶅簲
|
||||||
|
result.redBrightness = data[0];
|
||||||
|
result.blueBrightness = data[1];
|
||||||
|
result.yellowBrightness = data[2];
|
||||||
|
break;
|
||||||
|
case 0x0E:
|
||||||
|
// 0E: 鑾峰彇褰撳墠宸ヤ綔鏂瑰紡鍝嶅簲
|
||||||
|
result.voiceBroadcast = data[0];
|
||||||
|
result.alarmEnable = data[1];
|
||||||
|
result.alarmMode = data[2];
|
||||||
|
result.strobeEnable = data[3];
|
||||||
|
result.strobeMode = data[4];
|
||||||
|
result.strobeFrequency = data[5];
|
||||||
|
result.volume = data[6];
|
||||||
|
result.redBrightness = data[7];
|
||||||
|
result.blueBrightness = data[8];
|
||||||
|
result.yellowBrightness = data[9];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
const funcNames = { 0x01: '澶嶄綅', 0x02: '鍩虹淇℃伅', 0x03: '浣嶇疆', 0x04: '鐢垫簮鐘舵€?, 0x05: '鏂囦欢鏇存柊', 0x06: '璇煶鎾姤', 0x09: '闊抽噺', 0x0A: '鐖嗛棯妯″紡', 0x0B: '鐖嗛棯棰戠巼', 0x0C: '寮哄埗鎶ヨ', 0x0D: 'LED浜害', 0x0E: '宸ヤ綔鏂瑰紡' };
|
||||||
|
const name = funcNames[funcCode] || ('0x' + funcCode.toString(16));
|
||||||
|
console.log('[100J-钃濈墮] 璁惧鍝嶅簲 FB:', name, '瑙f瀽:', JSON.stringify(result), '鍘熷:', hexStr);
|
||||||
|
|
||||||
|
if (this.onNotifyCallback) {
|
||||||
|
this.onNotifyCallback(result);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
sendBleData(funcCode, dataBytes = []) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (!this.isBleConnected || !this.bleDeviceId) {
|
||||||
|
return reject(new Error('钃濈墮鏈繛鎺?));
|
||||||
|
}
|
||||||
|
|
||||||
|
const buffer = new ArrayBuffer(dataBytes.length + 3);
|
||||||
|
const view = new Uint8Array(buffer);
|
||||||
|
view[0] = 0xFA; // 鏁版嵁澶? view[1] = funcCode; // 鍔熻兘鐮? for (let i = 0; i < dataBytes.length; i++) {
|
||||||
|
view[2 + i] = dataBytes[i];
|
||||||
|
}
|
||||||
|
view[view.length - 1] = 0xFF; // 缁撳熬
|
||||||
|
const sendHex = Array.from(view).map(b => b.toString(16).padStart(2, '0').toUpperCase()).join(' ');
|
||||||
|
console.log('[100J-钃濈墮] 涓嬪彂鎸囦护 FA:', '0x' + funcCode.toString(16).toUpperCase(), sendHex);
|
||||||
|
|
||||||
|
// 浣跨敤椤圭洰涓粺涓€鐨?BleHelper 鍙戦€佹暟鎹? import('@/utils/BleHelper.js').then(module => {
|
||||||
|
const bleTool = module.default.getBleTool();
|
||||||
|
bleTool.sendData(this.bleDeviceId, buffer, this.SERVICE_UUID, this.WRITE_UUID)
|
||||||
|
.then(res => resolve(res))
|
||||||
|
.catch(err => reject(err));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 绾摑鐗欐寚浠ゅ彂閫佹柟娉? deviceReset(type = 0) { return this.sendBleData(0x01, [type]); }
|
||||||
|
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]); }
|
||||||
|
getCurrentWorkMode() { return this.sendBleData(0x0E, []); }
|
||||||
|
|
||||||
|
// 0x05 鏂囦欢涓婁紶锛氬垎鐗囦紶杈擄紝鍗忚 FA 05 [fileType] [phase] [data...] FF
|
||||||
|
// fileType: 1=璇煶 2=鍥剧墖 3=鍔ㄥ浘 4=OTA
|
||||||
|
// phase: 0=寮€濮?1=鏁版嵁 2=缁撴潫
|
||||||
|
// 姣忓寘鏈€澶у瓧鑺?钃濈墮锛欳HUNK_SIZE=500
|
||||||
|
// 鏀寔 fileUrl(闇€缃戠粶涓嬭浇) 鎴?localPath(鏃犵綉缁滄椂鏈湴鏂囦欢)
|
||||||
|
uploadVoiceFileBle(fileUrlOrLocalPath, fileType = 1, onProgress) {
|
||||||
|
const CHUNK_SIZE = 500; // 姣忓寘鏈夋晥鏁版嵁锛屽弬鑰?6155 deviceDetail.vue
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (!this.isBleConnected || !this.bleDeviceId) {
|
||||||
|
return reject(new Error('钃濈墮鏈繛鎺?));
|
||||||
|
}
|
||||||
|
if (!fileUrlOrLocalPath) {
|
||||||
|
return reject(new Error('缂哄皯鏂囦欢鍦板潃鎴栨湰鍦拌矾寰?));
|
||||||
|
}
|
||||||
|
const isLocalPath = !/^https?:\/\//i.test(fileUrlOrLocalPath);
|
||||||
|
if (onProgress) onProgress(1);
|
||||||
|
const readFromPath = (path) => {
|
||||||
|
const doSend = (bytes) => {
|
||||||
|
this._sendVoiceChunks(bytes, fileType, CHUNK_SIZE, onProgress)
|
||||||
|
.then(resolve).catch(reject);
|
||||||
|
};
|
||||||
|
// App 绔?getFileSystemManager 鏈疄鐜帮紝鐩存帴鐢?plus.io.requestFileSystem+getFile
|
||||||
|
readFromPathPlus(path, doSend, reject);
|
||||||
|
};
|
||||||
|
const readFileEntry = (entry, doSend, reject) => {
|
||||||
|
entry.file((file) => {
|
||||||
|
const reader = new plus.io.FileReader();
|
||||||
|
reader.onloadend = (e) => {
|
||||||
|
try {
|
||||||
|
const buf = e.target.result;
|
||||||
|
const bytes = new Uint8Array(buf);
|
||||||
|
doSend(bytes);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('[100J-钃濈墮] 璇诲彇ArrayBuffer寮傚父:', err);
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
reader.onerror = () => reject(new Error('璇诲彇鏂囦欢澶辫触'));
|
||||||
|
reader.readAsArrayBuffer(file);
|
||||||
|
}, (err) => reject(err));
|
||||||
|
};
|
||||||
|
const readFromPathPlus = (path, doSend, reject) => {
|
||||||
|
if (typeof plus === 'undefined' || !plus.io) {
|
||||||
|
console.error('[100J-钃濈墮] 褰撳墠鐜涓嶆敮鎸佹枃浠惰鍙?plus.io)');
|
||||||
|
reject(new Error('褰撳墠鐜涓嶆敮鎸佹枃浠惰鍙?));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// _downloads/ 鐢?requestFileSystem+getFile锛堥伩鍏?resolveLocalFileSystemURL 鍗′綇锛? if (path && path.startsWith('_downloads/')) {
|
||||||
|
const fileName = path.replace(/^_downloads\//, '');
|
||||||
|
plus.io.requestFileSystem(plus.io.PUBLIC_DOWNLOADS, (fs) => {
|
||||||
|
fs.root.getFile(fileName, {}, (entry) => readFileEntry(entry, doSend, reject), (err) => reject(err));
|
||||||
|
}, (err) => reject(err));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// _doc/ 鐢?requestFileSystem(PRIVATE_DOC)锛岄€愮骇 getDirectory 鍐?getFile锛堝祵濂楄矾寰勫吋瀹癸級
|
||||||
|
if (path && path.startsWith('_doc/')) {
|
||||||
|
const relPath = path.replace(/^_doc\//, '');
|
||||||
|
const parts = relPath.split('/');
|
||||||
|
const fileName = parts.pop();
|
||||||
|
const dirs = parts;
|
||||||
|
plus.io.requestFileSystem(plus.io.PRIVATE_DOC, (fs) => {
|
||||||
|
let cur = fs.root;
|
||||||
|
const next = (i) => {
|
||||||
|
if (i >= dirs.length) {
|
||||||
|
cur.getFile(fileName, { create: false }, (entry) => readFileEntry(entry, doSend, reject), (err) => reject(err));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
cur.getDirectory(dirs[i], { create: false }, (dir) => { cur = dir; next(i + 1); }, (err) => reject(err));
|
||||||
|
};
|
||||||
|
next(0);
|
||||||
|
}, (err) => reject(err));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 鍏朵粬璺緞鍏滃簳
|
||||||
|
let resolvePath = path;
|
||||||
|
if (path && path.startsWith('/') && !path.startsWith('file://')) resolvePath = 'file://' + path;
|
||||||
|
plus.io.resolveLocalFileSystemURL(resolvePath, (entry) => readFileEntry(entry, doSend, reject), (err) => reject(err));
|
||||||
|
};
|
||||||
|
if (isLocalPath) {
|
||||||
|
// 鏈湴璺緞锛氭棤缃戠粶鏃剁洿鎺ヨ鍙? readFromPath(fileUrlOrLocalPath);
|
||||||
|
} else {
|
||||||
|
// 缃戠粶 URL锛氫紭鍏堢敤 uni.request 鐩存帴鎷夊彇 ArrayBuffer锛堢被浼?100 璁惧锛屾棤鏂囦欢 IO锛夛紝澶辫触鍐嶈蛋 downloadFile
|
||||||
|
let fetchUrl = fileUrlOrLocalPath;
|
||||||
|
if (fetchUrl.startsWith('http://')) fetchUrl = 'https://' + fetchUrl.slice(7);
|
||||||
|
uni.request({
|
||||||
|
url: fetchUrl,
|
||||||
|
method: 'GET',
|
||||||
|
responseType: 'arraybuffer',
|
||||||
|
timeout: 60000,
|
||||||
|
success: (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) {
|
||||||
|
const doSend = (b) => {
|
||||||
|
this._sendVoiceChunks(b, fileType, CHUNK_SIZE, onProgress).then(resolve).catch(reject);
|
||||||
|
};
|
||||||
|
doSend(bytes);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
_sendVoiceChunks(bytes, fileType, chunkSize, onProgress) {
|
||||||
|
const total = bytes.length;
|
||||||
|
const ft = (fileType & 0xFF) || 1;
|
||||||
|
const DELAY_AFTER_START = 200; // 寮€濮嬪寘鍚庛€佺瓑璁惧鍝嶅簲鍚庡啀鍙戠殑缂撳啿(ms)
|
||||||
|
const DELAY_PACKET = 200; // 鏁版嵁鍖呴棿寤舵椂(ms)锛岃澶囨敹涓嶅叏鏃堕€傚綋鍔犲ぇ
|
||||||
|
const toHex = (arr) => Array.from(arr).map(b => b.toString(16).padStart(2, '0').toUpperCase()).join(' ');
|
||||||
|
console.log('[100J-钃濈墮] 璇煶鏂囦欢鎬诲ぇ灏?', total, '瀛楄妭, fileType=', ft);
|
||||||
|
if (onProgress) onProgress(0);
|
||||||
|
const bleToolPromise = import('@/utils/BleHelper.js').then(m => m.default.getBleTool());
|
||||||
|
const send = (dataBytes, label = '') => {
|
||||||
|
const buf = new ArrayBuffer(dataBytes.length + 3);
|
||||||
|
const v = new Uint8Array(buf);
|
||||||
|
v[0] = 0xFA;
|
||||||
|
v[1] = 0x05;
|
||||||
|
for (let i = 0; i < dataBytes.length; i++) v[2 + i] = dataBytes[i];
|
||||||
|
v[v.length - 1] = 0xFF;
|
||||||
|
const hex = toHex(v);
|
||||||
|
const preview = v.length <= 32 ? hex : hex.slice(0, 96) + '...';
|
||||||
|
console.log(`[100J-钃濈墮] 鍙戦€?{label} 鍏?{v.length}瀛楄妭:`, preview);
|
||||||
|
return bleToolPromise.then(ble => ble.sendData(this.bleDeviceId, buf, this.SERVICE_UUID, this.WRITE_UUID));
|
||||||
|
};
|
||||||
|
const delay = (ms) => new Promise(r => setTimeout(r, ms));
|
||||||
|
// 寮€濮嬪寘: FA 05 [fileType] [phase=0] [size 4B LE] FF
|
||||||
|
const startData = [ft, 0, total & 0xFF, (total >> 8) & 0xFF, (total >> 16) & 0xFF, (total >> 24) & 0xFF];
|
||||||
|
const waitPromise = this.waitForFileResponse(1000);
|
||||||
|
return send(startData, ' 寮€濮嬪寘')
|
||||||
|
.then(() => { if (onProgress) onProgress(1); return waitPromise; })
|
||||||
|
.then(() => { if (onProgress) onProgress(2); return delay(DELAY_AFTER_START); })
|
||||||
|
.then(() => {
|
||||||
|
let seq = 0;
|
||||||
|
const sendNext = (offset) => {
|
||||||
|
if (offset >= total) {
|
||||||
|
return delay(DELAY_PACKET).then(() => send([ft, 2], ' 缁撴潫鍖?));
|
||||||
|
}
|
||||||
|
const chunk = bytes.slice(offset, Math.min(offset + chunkSize, total));
|
||||||
|
const chunkData = [ft, 1, seq & 0xFF, (seq >> 8) & 0xFF, ...chunk];
|
||||||
|
return send(chunkData, ` #${seq} 鏁版嵁鍖卄).then(() => {
|
||||||
|
seq++;
|
||||||
|
const pct = Math.round((offset + chunk.length) / total * 100);
|
||||||
|
if (onProgress) onProgress(Math.min(99, Math.max(3, pct)));
|
||||||
|
return delay(DELAY_PACKET).then(() => sendNext(offset + chunk.length));
|
||||||
|
});
|
||||||
|
};
|
||||||
|
return sendNext(0);
|
||||||
|
})
|
||||||
|
.then(() => delay(DELAY_PACKET))
|
||||||
|
.then(() => {
|
||||||
|
if (onProgress) onProgress(100);
|
||||||
|
return { code: 200, msg: '璇煶鏂囦欢宸查€氳繃钃濈墮涓婁紶' };
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================== 鍏ㄥ眬鍗曚緥涓庣姸鎬佺鐞?==================
|
||||||
|
const protocolInstance = new HBY100JProtocol();
|
||||||
|
|
||||||
|
// 鏆撮湶缁欓〉闈細鏇存柊钃濈墮杩炴帴鐘舵€?export function updateBleStatus(isConnected, bleDeviceId, deviceId) {
|
||||||
|
protocolInstance.setBleConnectionStatus(isConnected, bleDeviceId);
|
||||||
|
protocolInstance.deviceId = deviceId;
|
||||||
|
console.log('[100J] 钃濈墮鐘舵€?', isConnected ? '宸茶繛鎺?鍚庣画鎸囦护璧拌摑鐗?' : '宸叉柇寮€(鍚庣画鎸囦护璧?G)', { bleDeviceId: bleDeviceId || '-', deviceId });
|
||||||
|
}
|
||||||
|
|
||||||
|
// 鏆撮湶缁欓〉闈細瑙f瀽钃濈墮鎺ユ敹鍒扮殑鏁版嵁
|
||||||
|
export function parseBleData(buffer) {
|
||||||
|
return protocolInstance.parseBleData(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 鏆撮湶缁欓〉闈細钃濈墮杩炴帴鍚庝富鍔ㄦ媺鍙栫數婧愮姸鎬?鐢甸噺銆佺画鑸?
|
||||||
|
export function fetchBlePowerStatus() {
|
||||||
|
if (!protocolInstance.isBleConnected) return Promise.reject(new Error('钃濈墮鏈繛鎺?));
|
||||||
|
console.log('[100J-钃濈墮] 鎷夊彇鐢垫簮鐘舵€?宸查€氳繃钃濈墮鍙戦€?FA 04 FF');
|
||||||
|
return protocolInstance.getPowerStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 鏆撮湶缁欓〉闈細钃濈墮杩炴帴鍚庝富鍔ㄦ媺鍙栧畾浣?浼樺厛钃濈墮锛岃澶囦篃浼氭瘡1鍒嗛挓涓诲姩涓婃姤)
|
||||||
|
export function fetchBleLocation() {
|
||||||
|
if (!protocolInstance.isBleConnected) return Promise.reject(new Error('钃濈墮鏈繛鎺?));
|
||||||
|
console.log('[100J-钃濈墮] 鎷夊彇瀹氫綅 宸查€氳繃钃濈墮鍙戦€?FA 03 FF');
|
||||||
|
return protocolInstance.getLocation();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 鏆撮湶缁欓〉闈細灏濊瘯閲嶈繛钃濈墮(浼樺厛绛栫暐锛氭柇绾垮悗鍙戞寚浠ゅ墠鍏堝皾璇曢噸杩?
|
||||||
|
export function tryReconnectBle(timeoutMs = 2500) {
|
||||||
|
if (protocolInstance.isBleConnected) return Promise.resolve(true);
|
||||||
|
if (!protocolInstance.bleDeviceId) return Promise.resolve(false);
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
import('@/utils/BleHelper.js').then(module => {
|
||||||
|
const bleTool = module.default.getBleTool();
|
||||||
|
const deviceId = protocolInstance.bleDeviceId;
|
||||||
|
const f = bleTool.data.LinkedList.find(v => v.deviceId === deviceId);
|
||||||
|
if (!f) {
|
||||||
|
resolve(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const svc = f.writeServiceId || '0000AE30-0000-1000-8000-00805F9B34FB';
|
||||||
|
const write = f.wirteCharactId || '0000AE03-0000-1000-8000-00805F9B34FB';
|
||||||
|
const notify = f.notifyCharactId || '0000AE02-0000-1000-8000-00805F9B34FB';
|
||||||
|
const timer = setTimeout(() => {
|
||||||
|
resolve(protocolInstance.isBleConnected);
|
||||||
|
}, timeoutMs);
|
||||||
|
console.log('[100J] 钃濈墮浼樺厛锛氬皾璇曢噸杩?, deviceId);
|
||||||
|
bleTool.LinkBlue(deviceId, svc, write, notify, 1).then(() => {
|
||||||
|
clearTimeout(timer);
|
||||||
|
protocolInstance.setBleConnectionStatus(true, deviceId);
|
||||||
|
console.log('[100J] 钃濈墮閲嶈繛鎴愬姛');
|
||||||
|
resolve(true);
|
||||||
|
}).catch(() => {
|
||||||
|
clearTimeout(timer);
|
||||||
|
resolve(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================== API 鎺ュ彛 (鎷︽埅灞? ==================
|
||||||
|
|
||||||
|
// 鑾峰彇璇煶绠$悊鍒楄〃
|
||||||
|
export function deviceVoliceList(params) {
|
||||||
|
return request({
|
||||||
|
url: `/app/video/queryAudioFileList`,
|
||||||
|
method: 'get',
|
||||||
|
data:params
|
||||||
|
})
|
||||||
|
}
|
||||||
|
// 閲嶅懡鍚?export function videRenameAudioFile(data) {
|
||||||
|
return request({
|
||||||
|
url: `/app/video/renameAudioFile`,
|
||||||
|
method: 'post',
|
||||||
|
data:data
|
||||||
|
})
|
||||||
|
}
|
||||||
|
// 鍒犻櫎璇煶鏂囦欢鍒楄〃
|
||||||
|
export function deviceDeleteAudioFile(params) {
|
||||||
|
return request({
|
||||||
|
url: `/app/video/deleteAudioFile`,
|
||||||
|
method: 'get',
|
||||||
|
data:params
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 鏇存柊璇煶/浣跨敤璇煶锛氳摑鐗欎紭鍏堬紝4G 鍏滃簳锛堜笉褰卞搷鍘熸湁 4G 闊抽涓嬪彂锛?// 鏈?fileUrl 鎴?localPath 涓旇摑鐗欏彲鐢ㄦ椂璧拌摑鐗欙紱鍚﹀垯鎴栬摑鐗欏け璐ユ椂璧?4G锛堜笌鍘熷厛閫昏緫涓€鑷达級
|
||||||
|
export function deviceUpdateVoice(data) {
|
||||||
|
const httpExec = () => request({
|
||||||
|
url: `/app/hby100j/device/updateVoice`,
|
||||||
|
method: 'post',
|
||||||
|
data: { id: data.id }
|
||||||
|
});
|
||||||
|
const localPath = data.localPath;
|
||||||
|
const fileUrl = data.fileUrl;
|
||||||
|
const hasLocalPath = localPath && typeof localPath === 'string' && localPath.length > 0;
|
||||||
|
const hasFileUrl = fileUrl && typeof fileUrl === 'string' && fileUrl.length > 0;
|
||||||
|
const fileSource = hasLocalPath ? localPath : (hasFileUrl ? fileUrl : null);
|
||||||
|
if (!fileSource) {
|
||||||
|
return httpExec(); // 鏃犳枃浠舵簮锛氱洿鎺?4G锛堝師鏈夐€昏緫锛? }
|
||||||
|
const bleExec = () => protocolInstance.uploadVoiceFileBle(fileSource, 1, data.onProgress);
|
||||||
|
return execWithBleFirst(bleExec, httpExec, '璇煶鏂囦欢涓婁紶');
|
||||||
|
}
|
||||||
|
// 100J淇℃伅
|
||||||
|
export function deviceDetail(id) {
|
||||||
|
return request({
|
||||||
|
url: `/app/hby100j/device/${id}`,
|
||||||
|
method: 'get',
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 钃濈墮浼樺厛銆?G 鍏滃簳锛氭湭杩炴帴鏃跺皾璇曢噸杩烇紱钃濈墮澶辫触鏃跺洖閫€ 4G锛堜繚鎸佸師鏈?4G 閫氳涓嶅彉锛?function execWithBleFirst(bleExec, httpExec, logName) {
|
||||||
|
const doBle = () => bleExec().then(res => ({ ...(res || {}), _channel: 'ble' }));
|
||||||
|
const do4G = () => httpExec().then(res => { res._channel = '4g'; return res; });
|
||||||
|
if (protocolInstance.isBleConnected) {
|
||||||
|
return doBle().catch(() => { console.log('[100J] 钃濈墮澶辫触锛屽洖閫€4G'); return do4G(); });
|
||||||
|
}
|
||||||
|
return tryReconnectBle(2500).then(reconnected => {
|
||||||
|
return reconnected ? doBle().catch(() => { console.log('[100J] 钃濈墮澶辫触锛屽洖閫€4G'); return do4G(); }) : do4G();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 鐖嗛棯妯″紡
|
||||||
|
export function deviceStrobeMode(data) {
|
||||||
|
return execWithBleFirst(
|
||||||
|
() => protocolInstance.setStrobeMode(data.enable, data.mode).then(() => ({ code: 200, msg: '鎿嶄綔鎴愬姛(钃濈墮)' })),
|
||||||
|
() => request({ url: `/app/hby100j/device/strobeMode`, method: 'post', data }),
|
||||||
|
'鐖嗛棯妯″紡'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 寮哄埗鎶ヨ
|
||||||
|
export function deviceForceAlarmActivation(data) {
|
||||||
|
return execWithBleFirst(
|
||||||
|
() => protocolInstance.setForceAlarm(data.voiceStrobeAlarm, data.mode).then(() => ({ code: 200, msg: '鎿嶄綔鎴愬姛(钃濈墮)' })),
|
||||||
|
() => request({ url: `/app/hby100j/device/forceAlarmActivation`, method: 'post', data }),
|
||||||
|
'寮哄埗鎶ヨ'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 鐖嗛棯棰戠巼
|
||||||
|
export function deviceStrobeFrequency(data) {
|
||||||
|
return execWithBleFirst(
|
||||||
|
() => protocolInstance.setStrobeFrequency(data.frequency).then(() => ({ code: 200, msg: '鎿嶄綔鎴愬姛(钃濈墮)' })),
|
||||||
|
() => request({ url: `/app/hby100j/device/strobeFrequency`, method: 'post', data }),
|
||||||
|
'鐖嗛棯棰戠巼'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 鐏厜璋冭妭浜害
|
||||||
|
export function deviceLightAdjustment(data) {
|
||||||
|
return execWithBleFirst(
|
||||||
|
() => protocolInstance.setLightBrightness(data.brightness, data.brightness, data.brightness).then(() => ({ code: 200, msg: '鎿嶄綔鎴愬姛(钃濈墮)' })),
|
||||||
|
() => request({ url: `/app/hby100j/device/lightAdjustment`, method: 'post', data }),
|
||||||
|
'鐏厜浜害'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 璋冭妭闊抽噺
|
||||||
|
export function deviceUpdateVolume(data) {
|
||||||
|
return execWithBleFirst(
|
||||||
|
() => protocolInstance.setVolume(data.volume).then(() => ({ code: 200, msg: '鎿嶄綔鎴愬姛(钃濈墮)' })),
|
||||||
|
() => request({ url: `/app/hby100j/device/updateVolume`, method: 'post', data }),
|
||||||
|
'璋冭妭闊抽噺'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 璇煶鎾斁
|
||||||
|
export function deviceVoiceBroadcast(data) {
|
||||||
|
return execWithBleFirst(
|
||||||
|
() => protocolInstance.setVoiceBroadcast(data.voiceBroadcast).then(() => ({ code: 200, msg: '鎿嶄綔鎴愬姛(钃濈墮)' })),
|
||||||
|
() => request({ url: `/app/hby100j/device/voiceBroadcast`, method: 'post', data }),
|
||||||
|
'璇煶鎾姤'
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user