Sprinkler-app/page_user/lanya.vue
2024-10-17 18:00:05 +08:00

213 lines
5.7 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<view>
<u-navbar :is-back="true" title='添加设备' title-color="#000" :border-bottom="false" :background="bgc"
id="navbar">
</u-navbar>
<button @click="btnkq">开启</button>
<view class="" style="width: 100%;height: 50rpx;"></view>
<button @click="btngb">关闭</button>
</view>
</template>
<script>
var xBlufi = require("@/components/blufi/xBlufi.js");
export default {
data() {
return {
bgc: {
backgroundColor: "#fff",
},
active:1,
flag:false,
devicesList:[],
deviceId:'',
name:'',
mac:'2952BB7A4EE0',
}
},
onLoad() {
let that = this
xBlufi.initXBlufi(1);
xBlufi.listenDeviceMsgEvent(true, that.funListenDeviceMsgEvent);
xBlufi.notifyStartDiscoverBle({
'isStart': true
})
setTimeout(() => {
xBlufi.notifyStartDiscoverBle({
'isStart': false
})
if (that.devicesList.length > 0) {
xBlufi.notifyConnectBle({
isStart: true,
deviceId: that.deviceId,
name: that.name
})
}
}, 2000)
},
methods:{
btnkq(){
let that = this
uni.getNetworkType({
success(res) {
if (res.networkType !== 'none') {
uni.getConnectedBluetoothDevices({
success(res) {
setTimeout(()=> {
xBlufi.notifySendCustomData({
customData: '11open'
})
},1500)
},
fail(err) {
console.error('获取已连接蓝牙设备信息失败:',err)
}
})
} else {
console.log('手机未连接网络')
}
}
})
},
btngb(){
let that = this
uni.getNetworkType({
success(res) {
if (res.networkType !== 'none') {
uni.getConnectedBluetoothDevices({
success(res) {
setTimeout(()=> {
xBlufi.notifySendCustomData({
customData: '11close'
})
},1500)
},
fail(err) {
console.error('获取已连接蓝牙设备信息失败:',err)
}
})
} else {
console.log('手机未连接网络')
}
}
})
},
// 获取附近蓝牙设备列表
funListenDeviceMsgEvent: function(options) {
switch (options.type) {
case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED:
// that.setData({
// connected: options.result
// })
if (!options.result) {
uni.showModal({
title: '很抱歉提醒你!',
content: '小程序与设备异常断开',
showCancel: false,
//是否显示取消按钮
success: function(res) {
}
})
}
break;
case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS:
if (options.result) {
let devicesarr = options.data
console.log(devicesarr,'111');
devicesarr.forEach(device => {
const mac = device.name.substring(4);
if (device.name.slice(5, 17) == this.mac) {
this.deviceId = device.deviceId
this.name = device.name
console.log(device.name,this.mac,'222');
this.devicesList.push(device);
let uniqueDevicesList = Array.from(new Set(this.devicesList));
this.devicesList = uniqueDevicesList;
}
})
}
break;
case xBlufi.XBLUFI_TYPE.TYPE_CONNECTED:
console.log("连接回调:" + JSON.stringify(options));
if (options.result) {
{
xBlufi.notifyInitBleEsp32({
deviceId: this.deviceId
})
this.deviceIds = options.data.deviceId
this.name = this.name
}
}else{
}
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_STOP:
if (options.result) {
let uniqueDevicesList = Array.from(new Set(this.devicesList));
let filteredDevices = uniqueDevicesList.filter(device => device.name.substring(0, 5) === "WATER");
// 将过滤后的数组重新赋值给 this.devicesList
this.devicesList = filteredDevices;
}
break;
case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START:
if (!options.result) {
uni.showToast({
title: '蓝牙未开启',
icon: 'none',
duration: 3000
});
return
} else {
}
break;
}
},
// 从蓝牙拿到数据进行解析
parseCustomData(data) {
// 将字符串按照 "@" 分割成数组
const dataArray = data.split('@');
const voltage = parseFloat(dataArray[0].substring(1)); // 去除前缀 "V",并将字符串转换为浮点数
const switchState = dataArray[1].substring(1); // 去除前缀 "S"
const current = parseFloat(dataArray[2].substring(1)); // 去除前缀 "A",并将字符串转换为浮点数
const power = parseFloat(dataArray[4].substring(1)); // 去除前缀 "P",并将字符串转换为浮点数
const remainingPower = parseFloat(dataArray[5].substring(1)); // 去除前缀 "M",并将字符串转换为浮点数
const setMode = dataArray[6].substring(1); // 去除前缀 "T"
// 返回解析后的数据对象
return {
voltage,
switchState,
current,
power,
remainingPower,
setMode
};
},
}
}
</script>
<style lang="less">
/deep/ .u-title{
margin-bottom: 22rpx;
}
/deep/ .uicon-nav-back{
margin-bottom: 22rpx;
}
page{
width: 100%;
padding: 20rpx 30rpx;
box-sizing: border-box;
background-color: #fff;
}
</style>