修复7305设备上报问题
This commit is contained in:
@ -301,6 +301,17 @@
|
||||
these.showBleUnConnect();
|
||||
return;
|
||||
}
|
||||
|
||||
// 确保设备对象的detailPageUrl正确设置,以便ReceiveData能找到对应的handler
|
||||
if (f.device) {
|
||||
f.device.detailPageUrl = "/pages/7305/BJQ7305";
|
||||
} else {
|
||||
f.device = {
|
||||
...device,
|
||||
detailPageUrl: "/pages/7305/BJQ7305"
|
||||
};
|
||||
}
|
||||
|
||||
let form = f.formData;
|
||||
if (form) {
|
||||
let keys = Object.keys(form);
|
||||
@ -314,9 +325,23 @@
|
||||
these.formData.img = device.devicePic;
|
||||
these.formData.id = device.id;
|
||||
these.formData.deviceId = f.deviceId;
|
||||
// 更新缓存中的设备信息
|
||||
ble.updateCache();
|
||||
|
||||
ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then(res => {
|
||||
console.log("连接成功")
|
||||
console.log("连接成功");
|
||||
these.formData.bleStatu = true;
|
||||
|
||||
// 确保订阅消息已开启
|
||||
let linkedDevice = ble.data.LinkedList.find(v => v.deviceId == f.deviceId);
|
||||
if (linkedDevice && !linkedDevice.notifyState) {
|
||||
console.log("连接成功但未订阅,主动订阅消息");
|
||||
ble.subScribe(f.deviceId, true).then(() => {
|
||||
console.log("订阅消息成功");
|
||||
}).catch(err => {
|
||||
console.error("订阅消息失败:", err);
|
||||
});
|
||||
}
|
||||
});
|
||||
these.setBleFormData();
|
||||
these.getDetail();
|
||||
@ -386,6 +411,18 @@
|
||||
}
|
||||
if (res.deviceId == these.formData.deviceId) {
|
||||
this.formData.bleStatu = true;
|
||||
|
||||
// 连接恢复后,确保重新订阅消息以接收设备上报的数据
|
||||
let f = this.getDevice();
|
||||
if (f && (f.notifyServiceid || f.notifyCharactId)) {
|
||||
console.log("连接恢复,重新订阅消息");
|
||||
ble.subScribe(res.deviceId, true).then(() => {
|
||||
console.log("订阅消息成功,等待设备上报数据");
|
||||
}).catch(err => {
|
||||
console.error("订阅消息失败:", err);
|
||||
});
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
hideLoading(these, 1000);
|
||||
});
|
||||
@ -475,23 +512,21 @@
|
||||
}
|
||||
let json = recei.ReceiveData(receive, device, path, recArr);
|
||||
|
||||
if (!json) {
|
||||
// 检查返回的数据是否有效(应该是解析后的对象,不是原始receive对象)
|
||||
// 如果返回的是原始receive对象(有bytes属性),说明handler没有匹配或解析失败
|
||||
if (!json || (json.hasOwnProperty('bytes') && (!json.battary && json.battary !== 0))) {
|
||||
console.log("收到7305数据但未解析或解析失败:", json);
|
||||
return;
|
||||
}
|
||||
let keys = Object.keys(json);
|
||||
keys.forEach((key) => {
|
||||
if (key in these.formData) {
|
||||
these.formData[key] = json[key];
|
||||
}
|
||||
});
|
||||
if ('statu' in json) {
|
||||
these.formData.statu = json.statu == '1' ? '充电中' : '未充电';
|
||||
}
|
||||
if ('xuhang' in json) {
|
||||
these.formData.xuhang = json.xuhang;
|
||||
}
|
||||
|
||||
if ('battary' in json && this.formData.battary <= 20) {
|
||||
console.log("收到7305解析数据:", json);
|
||||
console.log("formData更新前 - battary:", these.formData.battary, "xuhang:", these.formData.xuhang, "statu:", these.formData.statu);
|
||||
|
||||
// 确保电量和续航时间正确更新(使用 $set 确保 Vue 响应式)
|
||||
if (json.battary !== undefined && json.battary !== null) {
|
||||
this.$set(these.formData, 'battary', json.battary);
|
||||
console.log("更新电量:", json.battary, "->", these.formData.battary);
|
||||
if (json.battary <= 20) {
|
||||
this.showPop({
|
||||
message: "设备电量低",
|
||||
iconUrl: "/static/images/6155/DeviceDetail/uploadErr.png",
|
||||
@ -499,6 +534,27 @@
|
||||
buttonBgColor: "#E03434",
|
||||
});
|
||||
}
|
||||
}
|
||||
if (json.xuhang !== undefined && json.xuhang !== null) {
|
||||
this.$set(these.formData, 'xuhang', json.xuhang);
|
||||
console.log("更新续航时间:", json.xuhang, "->", these.formData.xuhang);
|
||||
}
|
||||
|
||||
// 更新充电状态
|
||||
if (json.statu !== undefined && json.statu !== null) {
|
||||
this.$set(these.formData, 'statu', json.statu);
|
||||
console.log("更新充电状态:", json.statu, "->", these.formData.statu);
|
||||
}
|
||||
|
||||
// 更新其他字段
|
||||
let keys = Object.keys(json);
|
||||
keys.forEach((key) => {
|
||||
if (key in these.formData && key !== 'battary' && key !== 'xuhang' && key !== 'statu') {
|
||||
this.$set(these.formData, key, json[key]);
|
||||
}
|
||||
});
|
||||
|
||||
console.log("formData更新后 - battary:", these.formData.battary, "xuhang:", these.formData.xuhang, "statu:", these.formData.statu);
|
||||
|
||||
},
|
||||
|
||||
|
||||
@ -752,6 +752,8 @@ class BleHelper {
|
||||
}
|
||||
|
||||
try {
|
||||
let trimmedStr = str.trim();
|
||||
if (trimmedStr && (trimmedStr.startsWith('{') || trimmedStr.startsWith('['))) {
|
||||
let receivJson = JSON.parse(str);
|
||||
let key = "sta_address"; //HBY100以此方式上传mac地址
|
||||
if (key in receivJson) {
|
||||
@ -774,8 +776,11 @@ class BleHelper {
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
} catch (convertException) {
|
||||
console.error("文本无法转json", convertException)
|
||||
if (str && (str.trim().startsWith('{') || str.trim().startsWith('['))) {
|
||||
console.error("JSON解析失败(可能是格式错误的数据)", convertException);
|
||||
}
|
||||
}
|
||||
|
||||
if (isUpdate) {
|
||||
@ -1029,7 +1034,7 @@ class BleHelper {
|
||||
|
||||
//订阅消息
|
||||
subScribe(deviceId, state) {
|
||||
// console.log("开始订阅消息", state);
|
||||
console.log("开始订阅消息", deviceId, state);
|
||||
return new Promise((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
|
||||
@ -1150,13 +1155,12 @@ class BleHelper {
|
||||
|
||||
results.forEach((result, index) => {
|
||||
if (result.status === "fulfilled") {
|
||||
// console.log(`操作${index + 1}成功:`, result.value);
|
||||
console.log(`订阅消息操作${index + 1}成功:`, result.value);
|
||||
} else {
|
||||
// console.log(`操作${index + 1}失败:`, result.reason
|
||||
// .message);
|
||||
console.error(`订阅消息操作${index + 1}失败:`, result.reason);
|
||||
}
|
||||
});
|
||||
// console.log("订阅消息成功");
|
||||
console.log("订阅消息完成,deviceId:", deviceId);
|
||||
resolve();
|
||||
}).catch((ex) => {
|
||||
console.error("异常,ex=", ex);
|
||||
@ -1547,9 +1551,19 @@ class BleHelper {
|
||||
}
|
||||
} else { //已连接过,直接订阅消息
|
||||
// console.log("11111111");
|
||||
if (fIndex > -1 && f && !f.notifyState) {
|
||||
|
||||
this.subScribe(deviceId, true);
|
||||
if (fIndex > -1 && f) {
|
||||
if (!f.notifyState) {
|
||||
console.log("设备已连接但未订阅,开始订阅消息");
|
||||
return this.subScribe(deviceId, true).then(() => {
|
||||
console.log("订阅消息完成");
|
||||
return Promise.resolve(true);
|
||||
}).catch(err => {
|
||||
console.error("订阅消息失败:", err);
|
||||
return Promise.resolve(true);
|
||||
});
|
||||
} else {
|
||||
console.log("设备已连接且已订阅消息");
|
||||
}
|
||||
}
|
||||
return Promise.resolve(true); //已连接过的设备无需获取服务
|
||||
}
|
||||
@ -1698,8 +1712,32 @@ class BleHelper {
|
||||
if (this.data.platform == 'web') {
|
||||
return Promise.resolve("h5平台默认成功");
|
||||
}
|
||||
// console.log("deviceid=" + deviceid + ",writeServiceId=" + writeServiceId + ",wirteCharactId=" +
|
||||
// wirteCharactId + ",timeout=" + ms)
|
||||
|
||||
// 打印发送的蓝牙指令
|
||||
let bufferHex = '';
|
||||
if (buffer) {
|
||||
let bytes = [];
|
||||
// 处理不同类型的buffer(ArrayBuffer、Uint8Array等)
|
||||
if (buffer instanceof ArrayBuffer) {
|
||||
let dataView = new DataView(buffer);
|
||||
for (let i = 0; i < buffer.byteLength; i++) {
|
||||
bytes.push(dataView.getUint8(i));
|
||||
}
|
||||
} else if (buffer.byteLength !== undefined) {
|
||||
// 如果是 Uint8Array 或其他类型
|
||||
for (let i = 0; i < buffer.byteLength; i++) {
|
||||
bytes.push(buffer[i] || 0);
|
||||
}
|
||||
} else if (Array.isArray(buffer)) {
|
||||
bytes = buffer;
|
||||
}
|
||||
if (bytes.length > 0) {
|
||||
bufferHex = bytes.map(b => '0x' + b.toString(16).padStart(2, '0').toUpperCase()).join(' ');
|
||||
}
|
||||
}
|
||||
console.log("准备发送蓝牙指令 - deviceId:", deviceid, "writeServiceId:", writeServiceId, "writeCharactId:", wirteCharactId);
|
||||
console.log("发送数据(Hex):", bufferHex || "(空数据)");
|
||||
console.log("发送数据(原始buffer长度):", buffer ? (buffer.byteLength || buffer.length || 0) : 0);
|
||||
if (ms === undefined) {
|
||||
ms = 50;
|
||||
}
|
||||
@ -1743,13 +1781,14 @@ class BleHelper {
|
||||
serviceId: device.writeServiceId,
|
||||
characteristicId: device.wirteCharactId,
|
||||
value: buffer,
|
||||
writeType: 'write',
|
||||
success: () => {
|
||||
// console.log("发送数据成功");
|
||||
console.log("✓ 蓝牙指令发送成功 - deviceId:", device.deviceId);
|
||||
succ();
|
||||
},
|
||||
fail: (ex) => {
|
||||
ex = this.getError(ex);
|
||||
console.error("发送数据失败", ex);
|
||||
console.error("✗ 蓝牙指令发送失败 - deviceId:", device.deviceId, "错误:", ex);
|
||||
|
||||
err(ex);
|
||||
}
|
||||
@ -1787,10 +1826,10 @@ class BleHelper {
|
||||
|
||||
}
|
||||
if (c.Linked) {
|
||||
// console.log("蓝牙已连接,直接发送");
|
||||
console.log("蓝牙已连接,直接发送数据");
|
||||
return sendBuffer();
|
||||
} else {
|
||||
// console.log("先连接蓝牙再发送");
|
||||
console.log("蓝牙未连接,先连接蓝牙再发送数据");
|
||||
return new Promise((resolve, reject) => {
|
||||
let f = this.data.LinkedList.find((v) => {
|
||||
return v.deviceId == deviceid;
|
||||
|
||||
@ -44,24 +44,27 @@ class BleReceive {
|
||||
if (f && f.macAddress && f.device && f.device.id) {
|
||||
let handler = null;
|
||||
let keys = Object.keys(this.HandlerMap);
|
||||
let devKey = f.device.detailPageUrl ? f.device.detailPageUrl.replace(/\//g, '').toLowerCase() : '';
|
||||
console.log("查找handler - detailPageUrl:", f.device.detailPageUrl, "转换后:", devKey);
|
||||
for (let index = 0; index < keys.length; index++) {
|
||||
let key = keys[index].replaceAll('/', '').toLowerCase();
|
||||
let devKey = f.device.detailPageUrl ? f.device.detailPageUrl.replaceAll('/', '').toLowerCase() : '';
|
||||
let key = keys[index].replace(/\//g, '').toLowerCase();
|
||||
if (key == devKey) {
|
||||
handler = this.HandlerMap[keys[index]];
|
||||
console.log("找到匹配的handler:", keys[index]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (handler) {
|
||||
let data = handler(receive, f, path, recArr);
|
||||
console.log("handler返回的数据:", data);
|
||||
return data;
|
||||
} else {
|
||||
console.log("已收到消息,但无指定处理程序", receive);
|
||||
console.log("已收到消息,但无指定处理程序, deviceUrl:", f.device.detailPageUrl, "可用handlers:", keys);
|
||||
}
|
||||
|
||||
} else {
|
||||
console.log("已收到该消息,但无法处理", receive);
|
||||
console.log("已收到该消息,但无法处理", receive, "f:", f);
|
||||
}
|
||||
|
||||
return receive;
|
||||
@ -467,11 +470,12 @@ class BleReceive {
|
||||
formData.statu = warn;
|
||||
formData.xuhang = lightingTime;
|
||||
|
||||
console.log("7305解析结果 - 电量:", batteryLevel, "续航:", lightingTime, "完整数据:", formData);
|
||||
|
||||
let recCnt = recArr.find(v => {
|
||||
|
||||
return v.key.replaceAll('/', '').toLowerCase() === f.device.detailPageUrl.replaceAll(
|
||||
'/', '').toLowerCase();
|
||||
return v.key.replace(/\//g, '').toLowerCase() === f.device.detailPageUrl.replace(
|
||||
/\//g, '').toLowerCase();
|
||||
});
|
||||
if (!recCnt) {
|
||||
if (batteryLevel <= 20) {
|
||||
@ -487,9 +491,11 @@ class BleReceive {
|
||||
this.setBleFormData(formData, f);
|
||||
return formData;
|
||||
} catch (error) {
|
||||
console.log('数据解析错误:', error);
|
||||
console.log('7305数据解析错误:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Receive_4877(receive,f,path,recArr){
|
||||
|
||||
Reference in New Issue
Block a user