修复7305设备上报问题
This commit is contained in:
@ -301,6 +301,17 @@
|
|||||||
these.showBleUnConnect();
|
these.showBleUnConnect();
|
||||||
return;
|
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;
|
let form = f.formData;
|
||||||
if (form) {
|
if (form) {
|
||||||
let keys = Object.keys(form);
|
let keys = Object.keys(form);
|
||||||
@ -314,9 +325,23 @@
|
|||||||
these.formData.img = device.devicePic;
|
these.formData.img = device.devicePic;
|
||||||
these.formData.id = device.id;
|
these.formData.id = device.id;
|
||||||
these.formData.deviceId = f.deviceId;
|
these.formData.deviceId = f.deviceId;
|
||||||
|
// 更新缓存中的设备信息
|
||||||
|
ble.updateCache();
|
||||||
|
|
||||||
ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then(res => {
|
ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then(res => {
|
||||||
console.log("连接成功")
|
console.log("连接成功");
|
||||||
these.formData.bleStatu = true;
|
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.setBleFormData();
|
||||||
these.getDetail();
|
these.getDetail();
|
||||||
@ -386,6 +411,18 @@
|
|||||||
}
|
}
|
||||||
if (res.deviceId == these.formData.deviceId) {
|
if (res.deviceId == these.formData.deviceId) {
|
||||||
this.formData.bleStatu = true;
|
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(() => {
|
setTimeout(() => {
|
||||||
hideLoading(these, 1000);
|
hideLoading(these, 1000);
|
||||||
});
|
});
|
||||||
@ -475,31 +512,50 @@
|
|||||||
}
|
}
|
||||||
let json = recei.ReceiveData(receive, device, path, recArr);
|
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;
|
return;
|
||||||
}
|
}
|
||||||
let keys = Object.keys(json);
|
|
||||||
keys.forEach((key) => {
|
console.log("收到7305解析数据:", json);
|
||||||
if (key in these.formData) {
|
console.log("formData更新前 - battary:", these.formData.battary, "xuhang:", these.formData.xuhang, "statu:", these.formData.statu);
|
||||||
these.formData[key] = json[key];
|
|
||||||
|
// 确保电量和续航时间正确更新(使用 $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",
|
||||||
|
borderColor: "#e034344d",
|
||||||
|
buttonBgColor: "#E03434",
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
|
||||||
if ('statu' in json) {
|
|
||||||
these.formData.statu = json.statu == '1' ? '充电中' : '未充电';
|
|
||||||
}
|
}
|
||||||
if ('xuhang' in json) {
|
if (json.xuhang !== undefined && json.xuhang !== null) {
|
||||||
these.formData.xuhang = json.xuhang;
|
this.$set(these.formData, 'xuhang', json.xuhang);
|
||||||
|
console.log("更新续航时间:", json.xuhang, "->", these.formData.xuhang);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ('battary' in json && this.formData.battary <= 20) {
|
// 更新充电状态
|
||||||
this.showPop({
|
if (json.statu !== undefined && json.statu !== null) {
|
||||||
message: "设备电量低",
|
this.$set(these.formData, 'statu', json.statu);
|
||||||
iconUrl: "/static/images/6155/DeviceDetail/uploadErr.png",
|
console.log("更新充电状态:", json.statu, "->", these.formData.statu);
|
||||||
borderColor: "#e034344d",
|
|
||||||
buttonBgColor: "#E03434",
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 更新其他字段
|
||||||
|
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);
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
proParam: function() {
|
proParam: function() {
|
||||||
|
|||||||
@ -752,30 +752,35 @@ class BleHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let receivJson = JSON.parse(str);
|
let trimmedStr = str.trim();
|
||||||
let key = "sta_address"; //HBY100以此方式上传mac地址
|
if (trimmedStr && (trimmedStr.startsWith('{') || trimmedStr.startsWith('['))) {
|
||||||
if (key in receivJson) {
|
let receivJson = JSON.parse(str);
|
||||||
this.data.LinkedList.find((v) => {
|
let key = "sta_address"; //HBY100以此方式上传mac地址
|
||||||
if (v.deviceId == receive
|
if (key in receivJson) {
|
||||||
.deviceId) {
|
this.data.LinkedList.find((v) => {
|
||||||
let macStr = receivJson[
|
if (v.deviceId == receive
|
||||||
key];
|
.deviceId) {
|
||||||
if (macStr.includes(':')) {
|
let macStr = receivJson[
|
||||||
v.macAddress = macStr;
|
key];
|
||||||
} else {
|
if (macStr.includes(':')) {
|
||||||
v.macAddress = macStr
|
v.macAddress = macStr;
|
||||||
.replace(/(.{2})/g,
|
} else {
|
||||||
'$1:').slice(0,
|
v.macAddress = macStr
|
||||||
-1)
|
.replace(/(.{2})/g,
|
||||||
|
'$1:').slice(0,
|
||||||
|
-1)
|
||||||
|
}
|
||||||
|
|
||||||
|
isUpdate = true;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
isUpdate = true;
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
}
|
||||||
} catch (convertException) {
|
} catch (convertException) {
|
||||||
console.error("文本无法转json", convertException)
|
if (str && (str.trim().startsWith('{') || str.trim().startsWith('['))) {
|
||||||
|
console.error("JSON解析失败(可能是格式错误的数据)", convertException);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isUpdate) {
|
if (isUpdate) {
|
||||||
@ -1029,7 +1034,7 @@ class BleHelper {
|
|||||||
|
|
||||||
//订阅消息
|
//订阅消息
|
||||||
subScribe(deviceId, state) {
|
subScribe(deviceId, state) {
|
||||||
// console.log("开始订阅消息", state);
|
console.log("开始订阅消息", deviceId, state);
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
|
||||||
@ -1150,13 +1155,12 @@ class BleHelper {
|
|||||||
|
|
||||||
results.forEach((result, index) => {
|
results.forEach((result, index) => {
|
||||||
if (result.status === "fulfilled") {
|
if (result.status === "fulfilled") {
|
||||||
// console.log(`操作${index + 1}成功:`, result.value);
|
console.log(`订阅消息操作${index + 1}成功:`, result.value);
|
||||||
} else {
|
} else {
|
||||||
// console.log(`操作${index + 1}失败:`, result.reason
|
console.error(`订阅消息操作${index + 1}失败:`, result.reason);
|
||||||
// .message);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// console.log("订阅消息成功");
|
console.log("订阅消息完成,deviceId:", deviceId);
|
||||||
resolve();
|
resolve();
|
||||||
}).catch((ex) => {
|
}).catch((ex) => {
|
||||||
console.error("异常,ex=", ex);
|
console.error("异常,ex=", ex);
|
||||||
@ -1547,9 +1551,19 @@ class BleHelper {
|
|||||||
}
|
}
|
||||||
} else { //已连接过,直接订阅消息
|
} else { //已连接过,直接订阅消息
|
||||||
// console.log("11111111");
|
// console.log("11111111");
|
||||||
if (fIndex > -1 && f && !f.notifyState) {
|
if (fIndex > -1 && f) {
|
||||||
|
if (!f.notifyState) {
|
||||||
this.subScribe(deviceId, true);
|
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); //已连接过的设备无需获取服务
|
return Promise.resolve(true); //已连接过的设备无需获取服务
|
||||||
}
|
}
|
||||||
@ -1698,8 +1712,32 @@ class BleHelper {
|
|||||||
if (this.data.platform == 'web') {
|
if (this.data.platform == 'web') {
|
||||||
return Promise.resolve("h5平台默认成功");
|
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) {
|
if (ms === undefined) {
|
||||||
ms = 50;
|
ms = 50;
|
||||||
}
|
}
|
||||||
@ -1743,13 +1781,14 @@ class BleHelper {
|
|||||||
serviceId: device.writeServiceId,
|
serviceId: device.writeServiceId,
|
||||||
characteristicId: device.wirteCharactId,
|
characteristicId: device.wirteCharactId,
|
||||||
value: buffer,
|
value: buffer,
|
||||||
|
writeType: 'write',
|
||||||
success: () => {
|
success: () => {
|
||||||
// console.log("发送数据成功");
|
console.log("✓ 蓝牙指令发送成功 - deviceId:", device.deviceId);
|
||||||
succ();
|
succ();
|
||||||
},
|
},
|
||||||
fail: (ex) => {
|
fail: (ex) => {
|
||||||
ex = this.getError(ex);
|
ex = this.getError(ex);
|
||||||
console.error("发送数据失败", ex);
|
console.error("✗ 蓝牙指令发送失败 - deviceId:", device.deviceId, "错误:", ex);
|
||||||
|
|
||||||
err(ex);
|
err(ex);
|
||||||
}
|
}
|
||||||
@ -1787,10 +1826,10 @@ class BleHelper {
|
|||||||
|
|
||||||
}
|
}
|
||||||
if (c.Linked) {
|
if (c.Linked) {
|
||||||
// console.log("蓝牙已连接,直接发送");
|
console.log("蓝牙已连接,直接发送数据");
|
||||||
return sendBuffer();
|
return sendBuffer();
|
||||||
} else {
|
} else {
|
||||||
// console.log("先连接蓝牙再发送");
|
console.log("蓝牙未连接,先连接蓝牙再发送数据");
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let f = this.data.LinkedList.find((v) => {
|
let f = this.data.LinkedList.find((v) => {
|
||||||
return v.deviceId == deviceid;
|
return v.deviceId == deviceid;
|
||||||
|
|||||||
@ -44,24 +44,27 @@ class BleReceive {
|
|||||||
if (f && f.macAddress && f.device && f.device.id) {
|
if (f && f.macAddress && f.device && f.device.id) {
|
||||||
let handler = null;
|
let handler = null;
|
||||||
let keys = Object.keys(this.HandlerMap);
|
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++) {
|
for (let index = 0; index < keys.length; index++) {
|
||||||
let key = keys[index].replaceAll('/', '').toLowerCase();
|
let key = keys[index].replace(/\//g, '').toLowerCase();
|
||||||
let devKey = f.device.detailPageUrl ? f.device.detailPageUrl.replaceAll('/', '').toLowerCase() : '';
|
|
||||||
if (key == devKey) {
|
if (key == devKey) {
|
||||||
handler = this.HandlerMap[keys[index]];
|
handler = this.HandlerMap[keys[index]];
|
||||||
|
console.log("找到匹配的handler:", keys[index]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (handler) {
|
if (handler) {
|
||||||
let data = handler(receive, f, path, recArr);
|
let data = handler(receive, f, path, recArr);
|
||||||
|
console.log("handler返回的数据:", data);
|
||||||
return data;
|
return data;
|
||||||
} else {
|
} else {
|
||||||
console.log("已收到消息,但无指定处理程序", receive);
|
console.log("已收到消息,但无指定处理程序, deviceUrl:", f.device.detailPageUrl, "可用handlers:", keys);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
console.log("已收到该消息,但无法处理", receive);
|
console.log("已收到该消息,但无法处理", receive, "f:", f);
|
||||||
}
|
}
|
||||||
|
|
||||||
return receive;
|
return receive;
|
||||||
@ -467,11 +470,12 @@ class BleReceive {
|
|||||||
formData.statu = warn;
|
formData.statu = warn;
|
||||||
formData.xuhang = lightingTime;
|
formData.xuhang = lightingTime;
|
||||||
|
|
||||||
|
console.log("7305解析结果 - 电量:", batteryLevel, "续航:", lightingTime, "完整数据:", formData);
|
||||||
|
|
||||||
let recCnt = recArr.find(v => {
|
let recCnt = recArr.find(v => {
|
||||||
|
|
||||||
return v.key.replaceAll('/', '').toLowerCase() === f.device.detailPageUrl.replaceAll(
|
return v.key.replace(/\//g, '').toLowerCase() === f.device.detailPageUrl.replace(
|
||||||
'/', '').toLowerCase();
|
/\//g, '').toLowerCase();
|
||||||
});
|
});
|
||||||
if (!recCnt) {
|
if (!recCnt) {
|
||||||
if (batteryLevel <= 20) {
|
if (batteryLevel <= 20) {
|
||||||
@ -487,9 +491,11 @@ class BleReceive {
|
|||||||
this.setBleFormData(formData, f);
|
this.setBleFormData(formData, f);
|
||||||
return formData;
|
return formData;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log('数据解析错误:', error);
|
console.log('7305数据解析错误:', error);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Receive_4877(receive,f,path,recArr){
|
Receive_4877(receive,f,path,recArr){
|
||||||
|
|||||||
Reference in New Issue
Block a user