From 8c39f40a65da6cfc0fb71b6d6c99c24ae269ba2a Mon Sep 17 00:00:00 2001
From: "3321822538@qq.com" <3321822538@qq.com>
Date: Thu, 12 Sep 2024 18:02:54 +0800
Subject: [PATCH] 11
---
page_fenbao/zhuce.vue | 201 +++++++++++++++++++++++++++++------------
page_user/sbdetail.vue | 23 -----
2 files changed, 145 insertions(+), 79 deletions(-)
diff --git a/page_fenbao/zhuce.vue b/page_fenbao/zhuce.vue
index 32647eb..0507418 100644
--- a/page_fenbao/zhuce.vue
+++ b/page_fenbao/zhuce.vue
@@ -9,11 +9,14 @@
-
- 状态:已录入 未录入
-
+
+ 状态:已录入 未录入
+
- SN码:
+ SN码:
@@ -28,8 +31,8 @@
-
- 扫码
+ 开关
+ 扫码
录入
@@ -96,9 +99,12 @@
arrname: '',
valuexh: '',
deviceId: '',
- arrs: [],
- sn:'输入SN',
- snname:''
+ arrs: '',
+ sn: '输入SN',
+ snname: '',
+ qrResult:'',
+ name:'',
+ ver_data:null
}
},
onLoad(option) {
@@ -135,7 +141,7 @@
},
methods: {
// 扫码sn
- scanQRCode(item){
+ scanQRCode(item) {
this.snname = item.name
uni.scanCode({
onlyFromCamera: true,
@@ -149,7 +155,7 @@
let sceneValue = res.result
let decodedValue = decodeURIComponent(sceneValue)
let id = getQueryParam(decodedValue, 's')
- this.storeId = id
+ this.storeId = id
this.devicesLists.forEach(item => {
if (item.name == this.snname) {
item.sn = id
@@ -164,8 +170,8 @@
})
}
})
- },
-
+ },
+
getxingh() {
this.$u.get(`/app/model/all`).then(res => {
if (res.code == 200) {
@@ -202,23 +208,44 @@
},
funListenDeviceMsgEvent: function(options) {
switch (options.type) {
+ case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_MY_DATA:
+ let loadPercent = options.data;
+ let loadText = '文件读取中'
+ // console.log("文件读取中", options.data)
+ break;
+ case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_CUSTON_DATA:
+ this.ver_data = this.parseCustomData(options.data)
+ console.log("1收到设备发来的自定义数据结果:", this.ver_data)
+ break;
case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS:
if (options.result) {
this.devicesList = options.data
+ this.devicesList.forEach(device => {
+ const mac = device.name.substring(5)
+ if (device.name == this.qrResult) {
+ this.deviceId = device.deviceId
+ this.name = device.name
+ this.mac = device.name.slice(5, 17)
+ }
+ })
}
break;
-
case xBlufi.XBLUFI_TYPE.TYPE_CONNECTED:
- console.log("连接回调:" + JSON.stringify(options));
+ // console.log("连接回调:" + JSON.stringify(options))
if (options.result) {
- {
+ setTimeout(()=>{
+ this.bluetoothflag = false
+ },3000)
+ uni.hideLoading(); {
console.log("连接回调options.data.deviceId:" + options.data.deviceId,
- "连接回调options.data.name:" + options.data.name);
+ "连接回调options.data.name:" + options.data.name)
}
+ } else {
+ this.shibaiflag = true
+ this.bluetoothflag = false
+ uni.hideLoading()
}
-
break;
-
case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START:
if (!options.result) {
this.mengcflag = false
@@ -231,11 +258,8 @@
});
} else {
this.searching = true
-
}
-
break;
-
case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP:
if (options.result) {
let uniqueDevicesList = Array.from(new Set(this.devicesList));
@@ -248,20 +272,16 @@
return newDevice;
})
let devicesarr = options.data
-
this.devicesList.forEach(device => {
- if (device.name.substring(0, 4) == "CTKG" || device.name.substring(0, 4) ==
- "CTPO") {
+ if (device.name.substring(0, 4) == "CTKG" || device.name.substring(0, 4) == "CTPO") {
this.devicesList.push(device);
let uniqueDevicesList = Array.from(new Set(this.devicesList));
this.devicesLists = uniqueDevicesList.filter(device => {
const deviceId = device.name.substring(5);
- // return !this.lurulist.includes(deviceId);
return deviceId
})
}
})
-
// console.log('蓝牙停止搜索ok')
console.log(this.devicesLists, '蓝牙停止');
this.mengcflag = false
@@ -269,10 +289,11 @@
if (this.devicesLists.length == 0) {
this.titleflag = true
} else {
+ let arr = []
this.devicesLists.forEach(item => {
- this.arrs.push(item.name.slice(5))
+ arr.push(item.name.slice(5))
})
- this.arrs = this.arrs.join(',')
+ this.arrs = arr.join(',')
this.$u.get(`/app/device/getExistMac/${this.arrs}`).then(res => {
if (res.code == 200) {
const existingDevices = new Set(res.data); // 将 res.data 转换为 Set,以提高查找效率
@@ -282,11 +303,12 @@
return {
...device,
flags, // 直接使用 flags 变量
- sn:'',
+ sn: '',
}
})
}
})
+ console.log(this.devicesLists,'0202');
}
} else {
//蓝牙停止搜索失败
@@ -295,31 +317,73 @@
console.log('蓝牙停止搜索失败');
}
this.searching = false
- break;
+ break
+ }
+ },
+
+ // 发送开关
+ createBLEConnections(e) {
+ uni.showLoading({
+ title: '执行中...'
+ })
+ this.arrs = ''
+ this.qrResult = e.name //拿到所点击的mac号
+ if (this.ver_data == null) { //判断是否有连接蓝牙
+ xBlufi.initXBlufi(1)
+ xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent)
+ xBlufi.notifyStartDiscoverBle({
+ 'isStart': true
+ })
+ // 停止蓝牙搜索
+ setTimeout(() => {
+ xBlufi.notifyStartDiscoverBle({
+ 'isStart': false
+ })
+ xBlufi.notifyConnectBle({
+ isStart: true,
+ deviceId: this.deviceId,
+ name: this.name
+ })
+ xBlufi.notifyInitBleEsp32({
+ deviceId: this.deviceId
+ })
+ }, 1000)
+ }else{ //有连接则发送命令
+ let that = this //改变this指向
+ uni.getNetworkType({
+ success(res) {
+ if (res.networkType !== 'none') {
+ uni.getConnectedBluetoothDevices({
+ success(res) {
+ setTimeout(() => { //一秒后发送开启命令
+ xBlufi.notifySendCustomData({
+ customData: 'open'
+ })
+ setTimeout(()=>{ //发送开启命令两秒后发送关闭命令
+ xBlufi.notifySendCustomData({
+ customData: 'close'
+ })
+ setTimeout(()=>{ //发送关闭命令后一秒后断开连接
+ uni.hideLoading()
+ that.ver_data = null
+ xBlufi.listenDeviceMsgEvent(false, that.funListenDeviceMsgEvent);
+ wx.closeBLEConnection({
+ deviceId: that.deviceId,
+ })
+ // console.log('guanbi');
+ },1000)
+ },2000)
+ }, 1000)
+ },
+ fail(err) {
+ console.error('获取已连接蓝牙设备信息失败:', err)
+ }
+ })
+ }
+ }
+ })
}
},
-
- // createBLEConnections(e) {
- // xBlufi.notifyStartDiscoverBle({
- // 'isStart': false
- // });
- // console.log(e, '蓝牙信息101010')
- // const deviceId = e.deviceId
- // this.Bluetoothmac = e.localName.substring(5) //从第七位开始截取 只取后面的mac号
- // this.mac = e.localName.substring(5)
- // let name = e.name;
- // xBlufi.notifyConnectBle({
- // isStart: true,
- // deviceId: e.deviceId,
- // name:e.name
- // })
- // // 发送响铃
- // xBlufi.notifySendCustomData({
- // customData: "BEEP2@"
- // })
-
- // let that = this
- // },
// 请求已经录入设备的mac
getluru() {
// this.$u.get(`/app/device/listAllMac`).then(res => {
@@ -342,9 +406,35 @@
// }
// })
},
+ parseCustomData(data) {
+ const dataArray = data.split('@');
+ const parsedData = {};
+ const prefixMap = {
+ 'V': 'voltage',
+ 'S': 'switchState',
+ 'A': 'current',
+ 'P': 'power',
+ 'M': 'remainingPower',
+ 'T': 'setMode',
+ 'W': 'temperature'
+ };
+ // 遍历数组并解析每个字段
+ for (let i = 0; i < dataArray.length; i++) {
+ const field = dataArray[i];
+ for (const prefix in prefixMap) {
+ if (field.startsWith(prefix)) {
+ const value = field.substring(1);
+ const propertyName = prefixMap[prefix];
+ parsedData[propertyName] = isNaN(parseFloat(value)) ? value : parseFloat(value);
+ break;
+ }
+ }
+ }
+ return parsedData;
+ },
//4、建立连接
createBLEConnection(e) {
- this.deviceId = e.name
+ // this.deviceId = e.name
this.mengcflag = true
uni.showLoading({
title: '录入中...'
@@ -371,7 +461,7 @@
// }, 1000)
uni.removeStorageSync('mac');
} else {
- this.mengcflag = false
+ this.mengcflag = false
uni.hideLoading()
uni.showToast({
title: res.msg,
@@ -548,7 +638,6 @@
.rt {
margin-top: 32rpx;
- padding-top: 50rpx;
box-sizing: border-box;
text {
diff --git a/page_user/sbdetail.vue b/page_user/sbdetail.vue
index a66243e..982c686 100644
--- a/page_user/sbdetail.vue
+++ b/page_user/sbdetail.vue
@@ -230,7 +230,6 @@
-
@@ -296,28 +295,6 @@
} else {
this.opflag = true
}
-
- // xBlufi.initXBlufi(1);
- // xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent);
- // xBlufi.notifyStartDiscoverBle({
- // 'isStart': true
- // })
-
- // 停止蓝牙搜索
- // setTimeout(() => {
- // xBlufi.notifyStartDiscoverBle({
- // 'isStart': false
- // });
-
- // xBlufi.notifyConnectBle({
- // isStart: true,
- // deviceId: this.deviceId,
- // name: this.name
- // })
- // xBlufi.notifyInitBleEsp32({
- // deviceId: this.deviceId
- // });
- // }, 2000)
},
// 分享到好友(会话)