This commit is contained in:
3321822538@qq.com 2024-09-12 18:02:54 +08:00
parent d8b37a2a3f
commit 8c39f40a65
2 changed files with 145 additions and 79 deletions

View File

@ -10,10 +10,13 @@
<view class="cen"> <view class="cen">
<view class="name"> <view class="name">
<view class=""> <view class="">
状态<text v-if="item.flags == true" style="color: red;">已录入</text> <text v-else style="color:seagreen;">未录入</text> 状态<text v-if="item.flags == true" style="color: red;">已录入</text> <text v-else
style="color:seagreen;">未录入</text>
</view> </view>
<view class="" style="display: flex;"> <view class="" style="display: flex;">
SN码: <input style="width: 200rpx;background-color: #ccc;padding: 5rpx;box-sizing: border-box;text-align: center;" v-model="item.sn" placeholder="输入SN"/> SN码: <input
style="width: 200rpx;background-color: #ccc;padding: 5rpx;box-sizing: border-box;text-align: center;"
v-model="item.sn" placeholder="输入SN" />
</view> </view>
</view> </view>
<view class="mac"> <view class="mac">
@ -28,7 +31,7 @@
</view> </view>
</view> </view>
<view class="rt"> <view class="rt">
<!-- <text style="margin-bottom: 10rpx;" :id="item.deviceId" @tap="createBLEConnections(item)">扫码</text> --> <text style="margin-bottom: 10rpx;" :id="item.deviceId" @click="createBLEConnections(item)">开关</text>
<text @click="scanQRCode(item)" style="margin-bottom: 10rpx;">扫码</text> <text @click="scanQRCode(item)" style="margin-bottom: 10rpx;">扫码</text>
<text :id="item.deviceId" @click="createBLEConnection(item)">录入</text> <text :id="item.deviceId" @click="createBLEConnection(item)">录入</text>
</view> </view>
@ -96,9 +99,12 @@
arrname: '', arrname: '',
valuexh: '', valuexh: '',
deviceId: '', deviceId: '',
arrs: [], arrs: '',
sn: '输入SN', sn: '输入SN',
snname:'' snname: '',
qrResult:'',
name:'',
ver_data:null
} }
}, },
onLoad(option) { onLoad(option) {
@ -202,23 +208,44 @@
}, },
funListenDeviceMsgEvent: function(options) { funListenDeviceMsgEvent: function(options) {
switch (options.type) { 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: case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS:
if (options.result) { if (options.result) {
this.devicesList = options.data 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; break;
case xBlufi.XBLUFI_TYPE.TYPE_CONNECTED: case xBlufi.XBLUFI_TYPE.TYPE_CONNECTED:
console.log("连接回调:" + JSON.stringify(options)); // console.log("" + JSON.stringify(options))
if (options.result) { if (options.result) {
{ setTimeout(()=>{
this.bluetoothflag = false
},3000)
uni.hideLoading(); {
console.log("连接回调options.data.deviceId" + options.data.deviceId, 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; break;
case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START: case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START:
if (!options.result) { if (!options.result) {
this.mengcflag = false this.mengcflag = false
@ -231,11 +258,8 @@
}); });
} else { } else {
this.searching = true this.searching = true
} }
break; break;
case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP: case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP:
if (options.result) { if (options.result) {
let uniqueDevicesList = Array.from(new Set(this.devicesList)); let uniqueDevicesList = Array.from(new Set(this.devicesList));
@ -248,20 +272,16 @@
return newDevice; return newDevice;
}) })
let devicesarr = options.data let devicesarr = options.data
this.devicesList.forEach(device => { this.devicesList.forEach(device => {
if (device.name.substring(0, 4) == "CTKG" || device.name.substring(0, 4) == if (device.name.substring(0, 4) == "CTKG" || device.name.substring(0, 4) == "CTPO") {
"CTPO") {
this.devicesList.push(device); this.devicesList.push(device);
let uniqueDevicesList = Array.from(new Set(this.devicesList)); let uniqueDevicesList = Array.from(new Set(this.devicesList));
this.devicesLists = uniqueDevicesList.filter(device => { this.devicesLists = uniqueDevicesList.filter(device => {
const deviceId = device.name.substring(5); const deviceId = device.name.substring(5);
// return !this.lurulist.includes(deviceId);
return deviceId return deviceId
}) })
} }
}) })
// console.log('ok') // console.log('ok')
console.log(this.devicesLists, '蓝牙停止'); console.log(this.devicesLists, '蓝牙停止');
this.mengcflag = false this.mengcflag = false
@ -269,10 +289,11 @@
if (this.devicesLists.length == 0) { if (this.devicesLists.length == 0) {
this.titleflag = true this.titleflag = true
} else { } else {
let arr = []
this.devicesLists.forEach(item => { 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 => { this.$u.get(`/app/device/getExistMac/${this.arrs}`).then(res => {
if (res.code == 200) { if (res.code == 200) {
const existingDevices = new Set(res.data); // res.data Set const existingDevices = new Set(res.data); // res.data Set
@ -287,6 +308,7 @@
}) })
} }
}) })
console.log(this.devicesLists,'0202');
} }
} else { } else {
// //
@ -295,31 +317,73 @@
console.log('蓝牙停止搜索失败'); console.log('蓝牙停止搜索失败');
} }
this.searching = false this.searching = false
break; break
} }
}, },
// createBLEConnections(e) { //
// xBlufi.notifyStartDiscoverBle({ createBLEConnections(e) {
// 'isStart': false uni.showLoading({
// }); title: '执行中...'
// console.log(e, '101010') })
// const deviceId = e.deviceId this.arrs = ''
// this.Bluetoothmac = e.localName.substring(5) // mac this.qrResult = e.name //mac
// this.mac = e.localName.substring(5) if (this.ver_data == null) { //
// let name = e.name; xBlufi.initXBlufi(1)
// xBlufi.notifyConnectBle({ xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent)
// isStart: true, xBlufi.notifyStartDiscoverBle({
// deviceId: e.deviceId, 'isStart': true
// name:e.name })
// }) //
// // setTimeout(() => {
// xBlufi.notifySendCustomData({ xBlufi.notifyStartDiscoverBle({
// customData: "BEEP2@" 'isStart': false
// }) })
xBlufi.notifyConnectBle({
// let that = this 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)
}
})
}
}
})
}
},
// mac // mac
getluru() { getluru() {
// this.$u.get(`/app/device/listAllMac`).then(res => { // 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 //4
createBLEConnection(e) { createBLEConnection(e) {
this.deviceId = e.name // this.deviceId = e.name
this.mengcflag = true this.mengcflag = true
uni.showLoading({ uni.showLoading({
title: '录入中...' title: '录入中...'
@ -548,7 +638,6 @@
.rt { .rt {
margin-top: 32rpx; margin-top: 32rpx;
padding-top: 50rpx;
box-sizing: border-box; box-sizing: border-box;
text { text {

View File

@ -230,7 +230,6 @@
<view v-if="btnmsk" <view v-if="btnmsk"
style="width: 100%;height: 100vh;position: fixed;top: 0;left: 0;background-color: #000;opacity: .1;z-index: 999;"> style="width: 100%;height: 100vh;position: fixed;top: 0;left: 0;background-color: #000;opacity: .1;z-index: 999;">
</view> </view>
</view> </view>
</template> </template>
@ -297,28 +296,6 @@
this.opflag = true 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)
}, },
// //
onShareAppMessage: function() { onShareAppMessage: function() {