100J蓝牙上传语音待验证
This commit is contained in:
@ -244,7 +244,8 @@
|
||||
deviceVoiceBroadcast,
|
||||
updateBleStatus,
|
||||
parseBleData,
|
||||
fetchBlePowerStatus
|
||||
fetchBlePowerStatus,
|
||||
fetchBleLocation
|
||||
} from '@/api/100J/HBY100-J.js'
|
||||
import BleHelper from '@/utils/BleHelper.js';
|
||||
var bleTool = BleHelper.getBleTool();
|
||||
@ -613,7 +614,7 @@
|
||||
this.createThrottledFunctions();
|
||||
|
||||
// 注册蓝牙相关事件
|
||||
bleTool.addReceiveCallback(this.bleValueNotify, "HBY100J");
|
||||
bleTool.addReceiveCallback(this.bleValueNotify.bind(this), "HBY100J");
|
||||
bleTool.addDisposeCallback(this.bleStateBreak, "HBY100J");
|
||||
bleTool.addRecoveryCallback(this.bleStateRecovry, "HBY100J");
|
||||
bleTool.addStateBreakCallback(this.bleStateBreak, "HBY100J");
|
||||
@ -1082,11 +1083,17 @@
|
||||
}
|
||||
let bleDeviceId = res.deviceId;
|
||||
updateBleStatus(true, bleDeviceId, this.deviceInfo.deviceId);
|
||||
// 蓝牙连接成功后主动拉取电源状态(电量、续航时间)
|
||||
fetchBlePowerStatus().catch(() => {});
|
||||
// 蓝牙连接成功后主动拉取电源状态、定位(优先蓝牙,设备也会每1分钟主动上报)
|
||||
// 两指令间隔 150ms,避免 writeBLECharacteristicValue:fail property not support
|
||||
fetchBlePowerStatus()
|
||||
.then(() => new Promise(r => setTimeout(r, 150)))
|
||||
.then(() => fetchBleLocation())
|
||||
.catch(() => {});
|
||||
},
|
||||
previewImg(img) {},
|
||||
bleValueNotify: function(receive, device, path, recArr) { //订阅消息
|
||||
// 仅处理当前设备的数据(device 为 LinkedList 中匹配 receive.deviceId 的项)
|
||||
if (device && device.device && this.deviceInfo.deviceId && device.device.id != this.deviceInfo.deviceId) return;
|
||||
// 解析蓝牙上报数据 (协议: FC=MAC主动上报, FB=指令响应)
|
||||
if (!receive.bytes || receive.bytes.length < 3) return;
|
||||
const parsedData = parseBleData(receive.bytes);
|
||||
@ -1096,16 +1103,23 @@
|
||||
if (parsedData.type === 'mac' && parsedData.macAddress) {
|
||||
this.formData.macAddress = parsedData.macAddress;
|
||||
this.device.deviceMac = parsedData.macAddress;
|
||||
this.deviceInfo.deviceMac = parsedData.macAddress;
|
||||
this.$set(this.deviceInfo, 'deviceMac', parsedData.macAddress);
|
||||
return;
|
||||
}
|
||||
|
||||
// 5.4 设备位置 (0x03):主动查询响应或设备定时上报(1分钟),优先蓝牙
|
||||
// 使用 $set 确保 Vue2 能检测新增属性并触发视图更新
|
||||
if (parsedData.longitude !== undefined && parsedData.latitude !== undefined) {
|
||||
this.$set(this.deviceInfo, 'longitude', parsedData.longitude);
|
||||
this.$set(this.deviceInfo, 'latitude', parsedData.latitude);
|
||||
}
|
||||
|
||||
// 5.5 获取设备电源状态 (0x04)
|
||||
if (parsedData.batteryPercentage !== undefined) {
|
||||
this.deviceInfo.batteryPercentage = parsedData.batteryPercentage;
|
||||
this.$set(this.deviceInfo, 'batteryPercentage', parsedData.batteryPercentage);
|
||||
}
|
||||
if (parsedData.batteryRemainingTime !== undefined) {
|
||||
this.deviceInfo.batteryRemainingTime = parsedData.batteryRemainingTime;
|
||||
this.$set(this.deviceInfo, 'batteryRemainingTime', parsedData.batteryRemainingTime);
|
||||
}
|
||||
|
||||
if (this.deviceInfo.batteryPercentage <= 20) {
|
||||
|
||||
@ -231,25 +231,39 @@
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
//语音管理列表
|
||||
//语音管理列表(合并云端 + 本地无网络保存的语音)
|
||||
getinitData(val, isLoadMore = false) {
|
||||
let data = {
|
||||
deviceId: this.device.deviceId
|
||||
}
|
||||
deviceVoliceList(data).then((res) => {
|
||||
const deviceId = this.device.deviceId;
|
||||
if (!deviceId) return;
|
||||
const mergeLocal = (serverList) => {
|
||||
const key = `100J_local_audio_${deviceId}`;
|
||||
const localList = uni.getStorageSync(key) || [];
|
||||
const localMapped = localList.map(item => ({
|
||||
...item,
|
||||
fileNameExt: item.name || '本地语音',
|
||||
createTime: item._createTime || item.createTime || Common.DateFormat(new Date(), "yyyy年MM月dd日"),
|
||||
fileUrl: item.fileUrl || item.localPath,
|
||||
useStatus: 0,
|
||||
_isLocal: true
|
||||
}));
|
||||
return [...localMapped, ...(serverList || [])];
|
||||
};
|
||||
deviceVoliceList({ deviceId }).then((res) => {
|
||||
if (res.code == 200) {
|
||||
this.total = res.total;
|
||||
const list = res.data.map(item => ({
|
||||
const list = (res.data || []).map(item => ({
|
||||
...item,
|
||||
createTime: item.createTime || Common.DateFormat(new Date(), "yyyy年MM月dd日")
|
||||
}));
|
||||
this.dataListA = list;
|
||||
// 通知mescroll加载完成
|
||||
if (this.mescroll) {
|
||||
this.mescroll.endBySize(list.length, this.total);
|
||||
}
|
||||
this.dataListA = mergeLocal(list);
|
||||
if (this.mescroll) this.mescroll.endBySize(this.dataListA.length, this.total + (this.dataListA.length - list.length));
|
||||
}
|
||||
})
|
||||
}).catch(() => {
|
||||
// 无网络时仅显示本地保存的语音
|
||||
this.dataListA = mergeLocal([]);
|
||||
this.total = this.dataListA.length;
|
||||
if (this.mescroll) this.mescroll.endBySize(this.dataListA.length, this.total);
|
||||
});
|
||||
},
|
||||
createAudioPlayer(localPath) {
|
||||
if (innerAudioContext) {
|
||||
@ -401,22 +415,24 @@
|
||||
return;
|
||||
}
|
||||
let task = () => {
|
||||
let data = {
|
||||
fileId: item.fileId,
|
||||
deviceId: this.device.deviceId
|
||||
if (item._isLocal) {
|
||||
// 本地项:从本地存储移除
|
||||
const key = `100J_local_audio_${this.device.deviceId}`;
|
||||
let list = uni.getStorageSync(key) || [];
|
||||
list = list.filter(l => l.id !== item.id && l.Id !== item.Id);
|
||||
uni.setStorageSync(key, list);
|
||||
uni.showToast({ title: '已删除', icon: 'none', duration: 1000 });
|
||||
this.getinitData();
|
||||
this.$refs.swipeAction.closeAll();
|
||||
return;
|
||||
}
|
||||
deviceDeleteAudioFile(data).then((res) => {
|
||||
deviceDeleteAudioFile({ fileId: item.fileId, deviceId: this.device.deviceId }).then((res) => {
|
||||
if (res.code == 200) {
|
||||
uni.showToast({
|
||||
title: res.msg,
|
||||
icon: 'none',
|
||||
duration: 1000
|
||||
});
|
||||
this.getinitData()
|
||||
// 关闭所有滑动项
|
||||
uni.showToast({ title: res.msg, icon: 'none', duration: 1000 });
|
||||
this.getinitData();
|
||||
this.$refs.swipeAction.closeAll();
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
this.showPop({
|
||||
showPop: true, //是否显示弹窗
|
||||
@ -468,71 +484,72 @@
|
||||
}
|
||||
},
|
||||
Apply(item, index) {
|
||||
this.mqttClient = new MqttClient();
|
||||
let data = {
|
||||
id: item.id
|
||||
}
|
||||
deviceUpdateVoice(data).then((RES) => {
|
||||
console.log(RES,'RES');
|
||||
if (RES.code == 200) {
|
||||
this.updateProgress = 0;
|
||||
this.isUpdating = true;
|
||||
const data = {
|
||||
id: item.id,
|
||||
fileUrl: item._isLocal ? '' : (item.fileUrl || item.url),
|
||||
localPath: item._isLocal ? item.localPath : '',
|
||||
onProgress: (p) => { this.updateProgress = p; }
|
||||
};
|
||||
// 整体超时 60 秒(仅影响蓝牙上传,4G HTTP 很快返回)
|
||||
const overallTimer = setTimeout(() => {
|
||||
if (this.isUpdating) {
|
||||
uni.showToast({ title: '操作超时', icon: 'none', duration: 2000 });
|
||||
this.isUpdating = false;
|
||||
this.updateProgress = 0;
|
||||
this.isUpdating = true;
|
||||
}
|
||||
}, 60000);
|
||||
deviceUpdateVoice(data).then((RES) => {
|
||||
clearTimeout(overallTimer);
|
||||
console.log(RES, 'RES');
|
||||
if (RES.code == 200) {
|
||||
// 蓝牙上传:进度已由 onProgress 更新,直接完成
|
||||
if (RES._channel === 'ble') {
|
||||
uni.showToast({ title: '升级完成!', icon: 'success', duration: 2000 });
|
||||
this.isUpdating = false;
|
||||
setTimeout(() => { uni.navigateBack(); }, 1500);
|
||||
return;
|
||||
}
|
||||
// 4G:订阅 MQTT 获取设备端进度,6 秒超时
|
||||
this.upgradeTimer = setTimeout(() => {
|
||||
if (this.isUpdating) {
|
||||
uni.showToast({ title: '升级进度同步超时', icon: 'none', duration: 2000 });
|
||||
this.isUpdating = false;
|
||||
this.updateProgress = 0;
|
||||
}
|
||||
}, 6000);
|
||||
this.mqttClient = this.mqttClient || new MqttClient();
|
||||
this.mqttClient.connect(() => {
|
||||
// 订阅来自设备的状态更新
|
||||
const statusTopic = `status/894078/HBY100/${this.device.deviceImei}`;
|
||||
this.mqttClient.subscribe(statusTopic, (payload) => {
|
||||
console.log(payload, 'payloadpayloadpayload');
|
||||
try {
|
||||
// 解析MQTT返回的payload
|
||||
const payloadObj = typeof payload === 'string' ? JSON.parse(
|
||||
payload) : payload;
|
||||
// 取出进度值(用可选链避免字段不存在报错)
|
||||
const payloadObj = typeof payload === 'string' ? JSON.parse(payload) : payload;
|
||||
const progress = payloadObj.data?.progress;
|
||||
if (progress !== undefined && !isNaN(progress) && progress >=
|
||||
0 && progress <= 100) {
|
||||
if (progress !== undefined && !isNaN(progress) && progress >= 0 && progress <= 100) {
|
||||
this.updateProgress = progress;
|
||||
console.log('当前升级进度:', progress + '%');
|
||||
// 进度到100%时,触发升级完成逻辑
|
||||
if (progress === 100) {
|
||||
clearTimeout(this.upgradeTimer);
|
||||
uni.showToast({
|
||||
title: '升级完成!',
|
||||
icon: 'success',
|
||||
duration: 2000
|
||||
});
|
||||
uni.showToast({ title: '升级完成!', icon: 'success', duration: 2000 });
|
||||
this.isUpdating = false;
|
||||
setTimeout(() => {
|
||||
uni.navigateBack();
|
||||
}, 1500);
|
||||
setTimeout(() => { uni.navigateBack(); }, 1500);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
clearTimeout(this.upgradeTimer);
|
||||
clearTimeout(this.upgradeTimer);
|
||||
console.error('解析MQTT payload失败:', e);
|
||||
}
|
||||
})
|
||||
})
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: RES.msg,
|
||||
icon: 'none',
|
||||
duration: 1000
|
||||
});
|
||||
});
|
||||
|
||||
} else {
|
||||
this.isUpdating = false;
|
||||
uni.showToast({ title: RES.msg || '操作失败', icon: 'none', duration: 1000 });
|
||||
}
|
||||
})
|
||||
this.upgradeTimer = setTimeout(() => {
|
||||
// 超时后执行:隐藏进度条+提示超时+重置进度
|
||||
uni.showToast({
|
||||
title: '升级进度同步超时',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
this.isUpdating = false; // 关闭进度条
|
||||
this.updateProgress = 0; // 重置进度值
|
||||
// 可选:如果需要取消MQTT订阅,加这行(根据需求选择)
|
||||
// this.mqttClient.unsubscribe(statusTopic);
|
||||
}, 6000); // 6000ms = 6秒,时间可直接修改
|
||||
}).catch((err) => {
|
||||
clearTimeout(overallTimer);
|
||||
this.isUpdating = false;
|
||||
uni.showToast({ title: err.message || '操作失败', icon: 'none', duration: 2000 });
|
||||
});
|
||||
},
|
||||
closePop: function() {
|
||||
this.Status.Pop.showPop = false;
|
||||
|
||||
@ -421,6 +421,27 @@
|
||||
hideLoading(these);
|
||||
}, 1200);
|
||||
},
|
||||
// 无网络时保存到本地,供蓝牙直接发送(不依赖 OSS)
|
||||
saveLocalForBle(filePath) {
|
||||
const deviceId = these.Status.ID;
|
||||
if (!deviceId) return;
|
||||
const item = {
|
||||
...these.cEdit,
|
||||
localPath: filePath,
|
||||
fileUrl: '',
|
||||
deviceId,
|
||||
id: 'local_' + these.cEdit.Id,
|
||||
_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();
|
||||
},
|
||||
// 保存录音并上传(已修复文件格式问题)
|
||||
uploadLuYin() {
|
||||
// 文件类型验证
|
||||
@ -541,8 +562,10 @@
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('上传文件失败:', err);
|
||||
// 无网络时保存到本地,供蓝牙直接发送
|
||||
these.saveLocalForBle(filePath);
|
||||
uni.showToast({
|
||||
title: '上传失败,请检查网络',
|
||||
title: '网络不可用,已保存到本地,可通过蓝牙发送',
|
||||
icon: 'none',
|
||||
duration: 3000
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user