diff --git a/components/BottomSlideMenuPlus/BottomSlideMenuPlus.vue b/components/BottomSlideMenuPlus/BottomSlideMenuPlus.vue index ffe890d..22ffa0e 100644 --- a/components/BottomSlideMenuPlus/BottomSlideMenuPlus.vue +++ b/components/BottomSlideMenuPlus/BottomSlideMenuPlus.vue @@ -26,7 +26,7 @@ - diff --git a/manifest.json b/manifest.json index e142c32..9058167 100644 --- a/manifest.json +++ b/manifest.json @@ -24,8 +24,7 @@ "Geolocation" : {}, "Maps" : {}, "Record" : {}, - "VideoPlayer" : {}, - "Push" : {} + "VideoPlayer" : {} }, /* 应用发布信息 */ "distribute" : { diff --git a/pages/102/HBY102.vue b/pages/102/HBY102.vue index 6b0816d..ff9220c 100644 --- a/pages/102/HBY102.vue +++ b/pages/102/HBY102.vue @@ -16,7 +16,7 @@ - {{formData.sta_charge?dic.sta_charge[formData.sta_charge]:"" }} + {{formData.sta_charge?dic.sta_charge[formData.sta_charge+'']:"未充电" }} 设备状态 @@ -46,10 +46,10 @@ --> - + 闯入报警! - {{getWarDevice(formData.sta_sosadd)}} + {{item.name}} @@ -114,6 +114,7 @@ {{item.name}} + {{Distance}} @@ -160,6 +161,10 @@ + + + + @@ -218,9 +223,31 @@ show: false, showHeader: true, menuItems: [{ - text: '强光', - icon: '/static/images/6155/DeviceDetail/qiang.png' - }], + text: '2M', + value:'status_2M', + icon: '' + }, + { + text: '4M', + value:'status_4M', + icon: '' + }, + { + text: '7M', + value:'status_7M', + icon: '' + }, + { + text: '10M', + value:'status_10M', + icon: '' + }, + { + text: '关闭', + value:'status_off', + icon: '' + } + ], activeIndex: -1, bgColor: '#2a2a2a', itemBgColor: '#3a3a3a', @@ -254,12 +281,13 @@ statu: '', sta_address: '', bleStatu: false, - + sta_charge:'', sta_LedType: '', sta_RadarType: '', sta_Online: '', warnTime: '', sta_sosadd: "", + sta_sosName:'', sta_IntrusTime: '' }, @@ -349,10 +377,7 @@ showConfirm: false }, groupDevices: [], - audioData: { - packetCtn: 0, - hexs: [] - } + warnDevices:[] } }, @@ -429,7 +454,28 @@ this.Status.pageHide = false; }, computed: { - + Distance:function(){ + let f=this.Status.BottomMenu.menuItems.find((item,index)=>{ + + return item.value===this.formData.sta_RadarType; + + + }); + if(f){ + return f.text; + } + return '关闭'; + }, + activeIndex:function(){ + let active=4; + let f=this.Status.BottomMenu.menuItems.find((item,index)=>{ + + if(item.value===this.formData.sta_RadarType){ + active=index; + } + }); + return active; + } }, methods: { @@ -441,7 +487,8 @@ if (item.group == 'sta_RadarType') { - this.toggleRedar(item, index); + // this.toggleRedar(item, index); + this.showRedarSetting(item,index); return; } @@ -507,6 +554,25 @@ } + }, + showRedarSetting(){ + this.Status.BottomMenu.show=true; + this.Status.BottomMenu.showBtn=false; + + this.Status.BottomMenu.title="感应距离设置"; + + this.Status.BottomMenu.textAlign="center"; + + this.Status.BottomMenu.showMask=true; + this.Status.BottomMenu.activeIndex=this.activeIndex; + }, + btnClick(item,index){ + this.Status.BottomMenu.show=false; + console.log("选中的项:"+index+",值:"+JSON.stringify(item)); + this.toggleRedar(item,index); + }, + closeActionSheet(){ + this.Status.BottomMenu.show=false; }, toggleRedar(item, index) { //雷达启停 let f = this.getDevice(); @@ -521,7 +587,7 @@ deviceId: '12345' } // #endif - let val = item.key; + let val = item.value; if (this.formData.sta_RadarType === val) { val = 'status_off'; } @@ -533,7 +599,9 @@ ble.sendString(f.deviceId, json, f.writeServiceId, f.wirteCharactId, 30) .then(res => { + debugger; this.formData.sta_RadarType = val; + this.Status.BottomMenu.activeIndex=index; these.setBleFormData(); resolve(); }) @@ -750,33 +818,58 @@ console.log("json=", json) - let keys = Object.keys(json); + + + let active=-1; + let f=this.Status.BottomMenu.menuItems.find((item,index)=>{ + + if(item.value===json.sta_RadarType){ + active=index; + } + }); + this.Status.BottomMenu.activeIndex=active; + let msg = []; + if (json.sta_PowerPercent <= 20 && (json.sta_charge===0 || json.sta_charge==='0')) { + msg.push("设备电量低"); + } + + if(json.sta_sosadd_off){//某个设备解除报警 + this.warnDevices.filter((d,index)=>{ + if(d.mac===json.sta_sosadd_off){ + this.warnDevices.splice(index,1); + } + }); + } + + + let keys = Object.keys(json); + keys.forEach(key => { if (key in this.formData) { // console.log("key=",key+",value=",json[key]); this.formData[key] = json[key]; } }); + - let msg = []; - if (this.formData.sta_PowerPercent <= 20) { - msg.push("设备电量低"); - } - if (json.sta_sosadd_off == this.formData.sta_sosadd) { - this.formData.sta_sosadd = ""; - let name=this.getWarDevice(json.sta_sosadd_off); - msg.push('"' + name + '"取消报警'); - } - - if (this.formData.sta_sosadd !== "") { - console.log("查询设备中"); - this.searchDevice(this.formData.sta_sosadd).catch(ex => {}).then(dev => { + if (json.sta_sosadd) {//某个设备闯入报警 + console.log("查询设备中",json.sta_sosadd); + this.searchDevice(json.sta_sosadd).catch(ex => {}).then(dev => { console.log("dev=", dev); + let d=this.warnDevices.find(v=>{return v.mac===json.sta_sosadd}); + let deviceName=""; if (dev) { + // this.formData.sta_sosName=dev.deviceName; msg.push('"' + dev.deviceName + '"闯入报警中'); + deviceName=dev.deviceName; + } else { msg.push('闯入报警中'); + // this.formData.sta_sosName=""; + } + if(!d){ + this.warnDevices.push({mac:json.sta_sosadd,name:deviceName}); } this.showMsg(msg.join(',')); }); @@ -805,6 +898,7 @@ }, getWarDevice(macStr) { + console.log("macStr=",macStr); if (macStr) { if (!macStr.includes(':')) { @@ -834,13 +928,13 @@ let f = this.groupDevices.find(v => { return v.deviceMac === macStr; }); - console.log("111111111"); + // console.log("111111111"); if (f) { console.log("找到设备", f); resolve(f); return; } - console.log("111111111"); + // console.log("111111111"); if (macStr == this.device.deviceMac) { console.log("当前设备", this.device); resolve(this.device); @@ -1058,9 +1152,6 @@ buttonText: '确定', okCallback: null }); - }, - btnClick() { - } } } diff --git a/pages/4877/BJQ4877.vue b/pages/4877/BJQ4877.vue index 4beeee4..825a758 100644 --- a/pages/4877/BJQ4877.vue +++ b/pages/4877/BJQ4877.vue @@ -255,7 +255,8 @@ import { colors as groupColors } from '@/api/4877/BJQ4877.js'; - + + import MqTool from '@/utils/MqHelper.js' const pagePath = "/pages/4877/BJQ4877"; @@ -264,6 +265,7 @@ var recei = null; var interval = null; var slidTime = null; + var mq=null; export default { data() { return { @@ -389,12 +391,19 @@ onUnload() { console.log("页面卸载,释放资源"); ble.removeAllCallback(pagePath); + if(mq){ + mq.unSubscribes(); + mq.disconnect(); + } + ble=null; + mq=null; clearInterval(interval); }, onLoad: function() { these = this; recei = BleReceive.getBleReceive(); ble = BleTool.getBleTool(); + mq=MqTool.getMqTool(); this.dic.gropus = []; @@ -429,6 +438,14 @@ } var device = data.data; these.device = device; + + let arr=[{topic:'C/4877_Groups_'+these.device.id,callback:these.getCheckedColors}]; + mq.init().then(res=>{ + mq.subscribes(arr).catch(ex=>{ + console.error("ex=",ex); + }); + }) + let f = ble.data.LinkedList.find((v) => { if (v.macAddress == device.deviceMac) { // console.log("找到设备了", v); @@ -477,6 +494,30 @@ }, methods: { + getCheckedColors(rec){ + console.error("收到MQ消息:",rec); + try{ + + let str=rec.receive.payloadString; + let arr=JSON.parse(str); + + + let groups = groupColors.filter((v,index) => { + if(arr[index]===1){ + return true; + } + return false; + }); + + these.dic.groups = groups; + + + }catch(err){ + + } + + + }, ShowChannelEdit() { this.Status.ShowEditChannel = true; this.showPop({ @@ -596,7 +637,11 @@ return; } - + mq.sendData('C/4877_Groups_'+these.device.id,JSON.stringify(arr),true).then(res=>{ + console.log("发送成功,",res) + }).catch(err=>{ + console.error("err=",err); + }); var json = { ins_GroupType: arr } diff --git a/pages/670/HBY670.vue b/pages/670/HBY670.vue index b128ee6..d63d587 100644 --- a/pages/670/HBY670.vue +++ b/pages/670/HBY670.vue @@ -799,7 +799,7 @@ return className; }, handleRightClick: function(s, e) { - + if (s === 0) { console.log("消息"); uni.navigateTo({ @@ -832,9 +832,8 @@ }, initMQ() { - - return new Promise((resolve, reject) => { - + + return new Promise((resolve, reject) => { if (mqttClient) { // console.log("无需再次初始化") resolve(); @@ -953,7 +952,7 @@ if ("sta_LightGrade" in json) { - let lightingLevelText = json.sta_LightGrade === 1 ? 'hight' : json.sta_LightGrade === 2 ? 'low' : + let lightingLevelText = json.sta_LightGrade === 1 ? 'qiang' : json.sta_LightGrade === 2 ? 'ruo' : 'close'; receiveData.lightCurr = lightingLevelText; } diff --git a/pages/common/addBLE/addEquip.vue b/pages/common/addBLE/addEquip.vue index d0fa275..69ab224 100644 --- a/pages/common/addBLE/addEquip.vue +++ b/pages/common/addBLE/addEquip.vue @@ -48,12 +48,17 @@ - 搜索设备 + 发现设备:{{deviceCnt}} 刷新 + + + + + v-show="item.name.indexOf(search)>-1"> @@ -148,6 +153,7 @@ } }, + search: '', //筛选 PairEquip: [], //已配对设备 EquipMents: [], //搜索出来的设备 device: null, @@ -157,7 +163,12 @@ } }, computed: { - + deviceCnt:function(){ + let arr=this.EquipMents.filter(item=>{ + return item.name.toLowerCase().indexOf(this.search.toLowerCase())>-1; + }); + return arr.length; + } }, onHide: function() { this.Status.isPageHidden = true; @@ -171,13 +182,14 @@ } }, onLoad(option) { + debugger; let search = option.search; these = this; eventChannel = this.getOpenerEventChannel(); const systemInfo = uni.getSystemInfoSync(); - + ble = bleTool.getBleTool(); // Ensure ble is initialized - + if (systemInfo.uniPlatform == 'web') { @@ -202,6 +214,67 @@ "linkStatu": false, "isTarget": true }, + { + "RSSI": -69, + "advertisData": "", + "advertisServiceUUIDs": [ + "0000FFE0-0000-1000-8000-00805F9B34FB" + ], + "deviceId": "4F0DAC91-4391-CB07-905E-72D7F03EFCD3", + "name": "4877-BF743D", + "linkStatu": false + }, + { + "RSSI": -55, + "advertisData": "", + "advertisServiceUUIDs": [ + "0000FFE0-0000-1000-8000-00805F9B34FB" + ], + "deviceId": "EBDA4E6F-3A28-FF65-A845-AE8CC7B78375", + "name": "HBY670-BF74EA", + "linkStatu": false + }, + { + "RSSI": -61, + "advertisData": "", + "advertisServiceUUIDs": [ + "0000FFE0-0000-1000-8000-00805F9B34FB" + ], + "deviceId": "469FB381-B47E-1E40-8073-EF50B5704AAB", + "name": "EF4651", + "linkStatu": false, + "isTarget": true + }, + { + "RSSI": -69, + "advertisData": "", + "advertisServiceUUIDs": [ + "0000FFE0-0000-1000-8000-00805F9B34FB" + ], + "deviceId": "4F0DAC91-4391-CB07-905E-72D7F03EFCD3", + "name": "4877-BF743D", + "linkStatu": false + },{ + "RSSI": -55, + "advertisData": "", + "advertisServiceUUIDs": [ + "0000FFE0-0000-1000-8000-00805F9B34FB" + ], + "deviceId": "EBDA4E6F-3A28-FF65-A845-AE8CC7B78375", + "name": "HBY670-BF74EA", + "linkStatu": false + }, + { + "RSSI": -61, + "advertisData": "", + "advertisServiceUUIDs": [ + "0000FFE0-0000-1000-8000-00805F9B34FB" + ], + "deviceId": "469FB381-B47E-1E40-8073-EF50B5704AAB", + "name": "EF4651", + "linkStatu": false, + "isTarget": true + }, { "RSSI": -69, "advertisData": "", @@ -216,6 +289,7 @@ these.PairEquip = [this.EquipMents[0]]; + this.$forceUpdate(); return; } let StartSubsrib = () => { @@ -256,7 +330,7 @@ }, pagePath); - + //搜索到新设备的回调 (Always active) ble.addDeviceFound((arr) => { @@ -277,7 +351,9 @@ let f = these.EquipMents.find((v, index) => { if (v.deviceId == device.deviceId) { - console.log(`更新设备信号: ${device.name || device.deviceId}, RSSI: ${device.RSSI}`); + console.log( + `更新设备信号: ${device.name || device.deviceId}, RSSI: ${device.RSSI}` + ); these.$set(these.EquipMents[index], 'RSSI', device.RSSI); return true; } @@ -288,9 +364,11 @@ console.log("+++ 发现新设备,准备添加到列表:", JSON.stringify(device)); if (these.device && these.device.bluetoothName && device.name) { - if (these.device.bluetoothName === device.name || (device.name && device.name.indexOf(these - .device.bluetoothName) > -1) || (device.name && this.device.bluetoothName.indexOf( - device.name) > -1)) { + if (these.device.bluetoothName === device.name || (device.name && device.name + .indexOf(these + .device.bluetoothName) > -1) || (device.name && this.device + .bluetoothName.indexOf( + device.name) > -1)) { device.isTarget = true; } } @@ -302,8 +380,8 @@ } }, pagePath); } - - let startValidDevice=()=>{ + + let startValidDevice = () => { if (these.device) { console.log("进入配对模式,启用连接恢复和验证逻辑。"); //蓝牙连接已恢复的回调 @@ -324,17 +402,17 @@ showLoading(these, { text: '蓝牙连接已恢复,正在验证设备' }); - + setTimeout(() => { these.DeviceVerdict(res.deviceId); }, 0); } else { hideLoading(these); } - - + + }, pagePath); - + //收到设备的消息回调 ble.addReceiveCallback((receivData, f, path, arr) => { console.log("000000"); @@ -347,17 +425,17 @@ clearInterval(this.Status.intval); this.Status.intval = null; this.Status.time = null; - + showLoading(these, { text: '正在验证设备' }); - + setTimeout(() => { these.DeviceVerdict(f.deviceId); }, 0); } - - + + }, pagePath); } } @@ -367,6 +445,10 @@ eventChannel.on('detailData', function(rec) { console.log("接收到父页面的参数:", rec); these.device = rec.data; + if (rec.data.bluetoothName) { + these.search = rec.data.bluetoothName.replace('JQZM-', ''); + } + startValidDevice(); these.refreshBleList(); }); @@ -382,9 +464,9 @@ return new Promise((resolve) => { if (uni.getSystemInfoSync().platform !== 'android') { - return resolve(true); + return resolve(true); } - + plus.android.requestPermissions( ['android.permission.ACCESS_FINE_LOCATION'], (result) => { @@ -418,14 +500,17 @@ }); }, async refreshBleList() { - + const systemInfo = uni.getSystemInfoSync(); + if (systemInfo.uniPlatform == 'web') { + return; + } const hasPermission = await this.checkAndRequestLocationPermission(); if (!hasPermission) { console.log("缺少定位权限,已中止蓝牙扫描。"); return; } - - + + if (!ble) { ble = bleTool.getBleTool(); if (!ble) { @@ -433,9 +518,9 @@ return; } } - + ble.StopSearch().finally(() => { - + let disconnectPromises = []; if (ble.data && ble.data.LinkedList) { ble.data.LinkedList.forEach(device => { @@ -443,17 +528,17 @@ disconnectPromises.push(ble.disconnectDevice(device.deviceId)); }); } - + Promise.allSettled(disconnectPromises).finally(() => { - + these.EquipMents = []; these.PairEquip = []; - + ble.StartSearch().then(result => { console.log("Fresh scan started successfully."); }).catch(err => { console.error("Failed to start fresh scan:", err); - if (err.code === 10001) { + if (err.code === 10001) { these.showOpenSetting(); } else { uni.showModal({ @@ -517,7 +602,7 @@ } console.log("f=", f); - if (f.macAddress) { + if (f && f.macAddress) { if (f.macAddress != these.device.deviceMac) { @@ -565,7 +650,7 @@ } return false; }); - if (!f.macAddress) { + if (!(f && f.macAddress)) { removeLink(); updateLoading(these, { text: "出现错误,未收到设备Mac地址" @@ -805,11 +890,12 @@ padding: 30rpx; box-sizing: border-box; width: 100%; - height: calc(100% - 300rpx); + height: calc(100% - 240rpx); overflow-y: scroll; position: absolute; - top: 300rpx; + top: 240rpx; left: 0rpx; + /* border: 1px solid #BBE600; */ } @@ -830,17 +916,20 @@ align-content: center; align-items: center; justify-content: space-between; - margin-bottom: 20rpx; + margin-bottom: 30rpx; } .list { min-height: 120rpx; + } .searchList { width: 100%; - height: calc(100% - 186rpx); + height: calc(100% - 300rpx); overflow-y: scroll; + box-sizing: border-box; + /* border: 1px solid red; */ } .list .item { @@ -1010,4 +1099,21 @@ filter: none !important; -webkit-filter: none !important; } + + + + .uni-input{ + background-color: #121212; + width:100%; + height: 60rpx; + color: #ffffffde; + border:1rpx solid #cbcbcbde; + border-radius: 8rpx; + font-size: 26rpx; + text-indent: 8rpx; + font-family: "PingFang SC"; + line-height: 60rpx; + caret-color:#BBE600; + font-weight: 200; + } \ No newline at end of file diff --git a/pages/common/index/index.vue b/pages/common/index/index.vue index 0f008ac..6be6d24 100644 --- a/pages/common/index/index.vue +++ b/pages/common/index/index.vue @@ -57,8 +57,9 @@ v-if="item.communicationMode==0 && item.onlineStatus==1 && item.alarmStatus==1"> 报警中 - 已连接 - 未连接 + + {{item.bleStatu?'已连接':'未连接'}} + @@ -145,7 +146,8 @@ deviceReName } from '@/api/common/index.js' import BleHelper from '@/utils/BleHelper.js'; - var ble=null; + var pagePath = 'pages/common/index'; + var ble = null; export default { onPullDownRefresh() { // 执行下拉刷新时的操作,比如重新获取数据 @@ -212,6 +214,69 @@ } }, methods: { + bleStateRecovery() { + console.log("蓝牙适配器恢复可用,重连断开的设备"); + ble.linkAllDevices(); + // if (ble.data && ble.data.LinkedList) { + // for (var i = 0; i < this.deviceList.length; i++) { + // if (this.deviceList[i].communicationMode !== '0' || this.deviceList[i].communicationMode !== 0) { + // ble.data.LinkedList.find(v => { + + // if (v.macAddress && v.device && v.device.id && v.device.id == this.deviceList[i] + // .id) { + // ble.LinkBlue(v.deviceId); + // return true; + // } + // }); + // } + // } + // } + }, + bleBreak(res) { + console.log("蓝牙断开连接", res); + if (res.deviceId) { + this.updateBleStatu(res.deviceId); + } + + }, + bleRecovery(res) { + console.log("蓝牙连接成功", res); + if (res.deviceId) { + this.updateBleStatu(res.deviceId); + } + + }, + + updateBleStatu(deviceId) {//更新列表的蓝牙连接状态 + if (ble) { + + for (var i = 0; i < this.deviceList.length; i++) { + + let bleStatu = false; + if (ble.data && ble.data.LinkedList) { + ble.data.LinkedList.find(v => { + if (deviceId && v.deviceId != deviceId) { + return false; + } + if (v.macAddress && v.device && v.device.id) { + if (v.device.id == this.deviceList[i].id && v.Linked) { + + bleStatu = true; + return true; + } + + } + }); + } + this.$set(this.deviceList[i], 'bleStatu', bleStatu); + + + + } + return; + } + console.error("ble is null") + }, // 更多 allMore() { this.showshare = !this.showshare; @@ -291,6 +356,10 @@ // 如果是第一页或切换分类,替换数据 this.deviceList = this.page === 1 ? newDevices : [...this.deviceList, ...newDevices]; + + + this.updateBleStatu(); + this.total = res.total; // 判断是否加载完成 if (res.rows.length < this.size || this.deviceList.length >= this.total) { @@ -329,9 +398,25 @@ // 处理新的扫码结果 const cleanedResult = res.result.trim(); console.log('扫码结果:', cleanedResult); + let url=`/pages/common/qrcode/qrcode?deviceId=${encodeURIComponent(cleanedResult)}`; + try{ + let json=JSON.parse(cleanedResult); + if('imei' in json){ + url=`/pages/common/qrcode/qrcode?deviceId=${encodeURIComponent(json.imei)}`; + }else if('blue' in json){ + if(!json.blue.includes(':')){ + json.blue=json.blue.replace( + /(.{2})/g, '$1:') + .slice(0, -1) + } + url=`/pages/common/addBLE/LinkBle?mac=${encodeURIComponent(json.blue)}`; + } + }catch(ex){ + + } // 跳转并传递扫描结果 uni.navigateTo({ - url: `/pages/common/qrcode/qrcode?deviceId=${encodeURIComponent(cleanedResult)}` + url: url }); }, fail: (err) => { @@ -390,9 +475,9 @@ }, 500); this.deleteShow = false // 关闭所有滑动项 - this.$refs.swipeAction.closeAll(); - - ble && ble.DropDevice(data.id); + this.$refs.swipeAction.closeAll(); + + ble && ble.DropDevice(null,data.id); } else { uni.showToast({ title: res.msg, @@ -492,7 +577,7 @@ let url = item.detailPageUrl; // console.log("url=",url); // if(!url){ - //url="/pages/670/HBY670" + //url="/pages/670/HBY670" // } uni.navigateTo({ url: url, @@ -506,8 +591,9 @@ deviceType: this.tabs[this.activeTab].id || '', apiType: 'listA' //标识,根据这个参数,区分普通详情,分享跳转详情,查不一样的权限信息 }); - },fail(ex) { - console.log("ex=",ex); + }, + fail(ex) { + console.log("ex=", ex); } }) }, @@ -559,14 +645,35 @@ console.log('列表收到消息了么'); this.onIntall() }); - ble=BleHelper.getBleTool(); + ble = BleHelper.getBleTool(); + + //蓝牙连接成功的回调 + ble.addRecoveryCallback((res) => { + console.log("11111"); + this.bleRecovery(res); + }, pagePath); + + //蓝牙断开连接的回调 + ble.addDisposeCallback((res) => { + console.log("2222222"); + this.bleBreak(res); + }, pagePath); + + //蓝牙适配器恢复可用的回调,一般是重连设备 + ble.addStateRecoveryCallback(res => { + this.bleStateRecovery(); + }, pagePath); + + }, + beforeDestroy() { // 组件销毁前移除监听器 uni.$off('refreshDeviceList'); }, onUnload() { uni.$off('deviceStatusUpdate'); + ble && ble.removeAllCallback(); } } @@ -889,6 +996,8 @@ margin-left: 15rpx; padding: 10rpx 0rpx; font-size: 28rpx; + text-align: left; + text-indent: 5rpx; } .svg { diff --git a/static/zj.png b/static/zj.png deleted file mode 100644 index 73caa99..0000000 Binary files a/static/zj.png and /dev/null differ diff --git a/static/zjlogo.png b/static/zjlogo.png deleted file mode 100644 index a60cfe9..0000000 Binary files a/static/zjlogo.png and /dev/null differ diff --git a/utils/BleHelper.js b/utils/BleHelper.js index a03532b..55ac5ce 100644 --- a/utils/BleHelper.js +++ b/utils/BleHelper.js @@ -145,7 +145,7 @@ class BleHelper { let item = this.data.LinkedList[i]; if (deviceId && item.device) { if (item.device.id == deviceId) { - console.log("找到要删除的设备", item); + console.error("找到要删除的设备", item); this.data.LinkedList.splice(i, 1); this.disconnectDevice(item.deviceId); flag = true; @@ -154,7 +154,7 @@ class BleHelper { } else { if (bleId && item.deviceId == bleId) { - console.log("找到要删除的设备1,", item) + console.error("找到要删除的设备1,", item) this.data.LinkedList.splice(i, 1); this.disconnectDevice(item.deviceId); flag = true; @@ -513,58 +513,71 @@ class BleHelper { return bytes.map(byte => byte.toString(16).padStart(2, '0')); } - + let adapterStateTime=null; uni.onBluetoothAdapterStateChange((state) => { - console.log('蓝牙状态发生变化:' + JSON.stringify(state)); - this.data.discovering = state.discovering; - - if (this.data.available !== state.available) { - this.data.available = state.available; + clearTimeout(adapterStateTime); + setTimeout(()=>{ - if (this.data.available && this.data - .isOpenBlue) { //蓝牙状态再次可用,重连所有设备 - - if (this.cfg.stateRecoveryCallback.length > 0) { - this.cfg.stateRecoveryCallback.forEach( - c => { - try { - c.callback(); - } catch (error) { - console.error( - "蓝牙适配器已恢复,但回调函数发生错误", - error); - } - }) - } - + if(this.data.discovering === state.discovering && this.data.available===state.available){ + console.error("专业给Uniapp填坑,适配器状态错误"); + return; } - } - - - if (!state.available) { //蓝牙状态不可用了,将所有设备标记为断开连接 - - this.data.LinkedList.filter((v) => { - v.Linked = false; - v.notifyState = false; - return true; - }); - let keys = Object.keys(bleDeviceStates) - keys.filter(v => { - bleDeviceStates[v] = false; - }); - - this.cfg.stateBreakCallback.forEach(f => { - try { - f.callback(); - } catch (error) { - console.error("蓝牙状态不可用了,执行回调异常", - error) + console.log('蓝牙模块状态发生变化:' + JSON.stringify(state)); + this.data.discovering = state.discovering; + + if (this.data.available !== state.available) { + this.data.available = state.available; + + if (this.data.available && this.data + .isOpenBlue) { //蓝牙适配器再次可用 + + if (this.cfg.stateRecoveryCallback.length > 0) {//执行适配器恢复的回调 + console.log('蓝牙状态再次可用,执行恢复连接的回调'); + this.cfg.stateRecoveryCallback.forEach( + c => { + try { + c.callback(); + console.log("执行恢复连接的回调成功"); + } catch (error) { + console.error( + "蓝牙适配器已恢复,但回调函数发生错误", + error); + } + }) + } + } - - }) - - this.updateCache(); - } + } + + + if (!state.available) { //蓝牙状态不可用了,将所有设备标记为断开连接 + console.log("蓝牙模块不可用了,将所有设备标记为断开连接"); + this.data.LinkedList.filter((v) => { + v.Linked = false; + v.notifyState = false; + + return true; + }); + // let keys = Object.keys(bleDeviceStates) + // keys.filter(v => { + // bleDeviceStates[v] = false; + // }); + + this.cfg.stateBreakCallback.forEach(f => { + try { + f.callback(); + console.log("执行断开连接的回调成功"); + } catch (error) { + console.error("蓝牙状态不可用了,执行回调异常", + error) + } + + }) + + this.updateCache(); + } + },500); + }); @@ -594,8 +607,8 @@ class BleHelper { return false; }); this.updateCache(); - if (f && f.device && f.device.id) { - console.log("尝试5次恢复连接,", f + if (f && f.device && f.device.id && this.data.available) { + console.log("蓝牙状态可用,尝试5次恢复连接,", f .deviceId); this.LinkBlue(res.deviceId, f .writeServiceId, f @@ -609,6 +622,7 @@ class BleHelper { (c) => { try { c.callback(res); + console.log("执行蓝牙断开连接的回调成功"); } catch (error) { console.error( "执行蓝牙断开连接的回调出现异常,", @@ -618,26 +632,13 @@ class BleHelper { } } else { - console.log("蓝牙连接已恢复,", res); - if (this.cfg.recoveryCallback.length > - 0) { - this.cfg.recoveryCallback.forEach(( - c) => { - try { - c.callback(res); - } catch (error) { - console.error( - "执行蓝牙恢复连接的回调出现异常,", - error) - } - }); - } + console.log("蓝牙连接已恢复",res); } }, 0); }); - console.log("111111111") + uni.onBluetoothDeviceFound((res) => { //console.log("发现新设备:" + JSON.stringify(res,'name')); let arr = []; @@ -685,7 +686,7 @@ class BleHelper { uni.onBLECharacteristicValueChange((receive) => { //订阅消息 - // console.log("收到订阅消息", receive); + // console.log("收到订阅消息", receive); let f = this.data.LinkedList.find((v) => { return v.deviceId == receive.deviceId; }) @@ -815,7 +816,7 @@ class BleHelper { str: str, hexs: hexs }; - // console.log("监听到特征值:" + JSON.stringify(recData)); + console.log("监听到特征值:" + JSON.stringify(recData)); if (this.cfg.receivDataCallback) { if (this.cfg.receivDataCallback.length > 0) { @@ -1501,6 +1502,20 @@ class BleHelper { this.updateCache(); // console.log("LinkedList=", this.data // .LinkedList); + + //执行连接成功的回调 + if (this.cfg.recoveryCallback.length >0) { + this.cfg.recoveryCallback.forEach(( + c) => { + try { + c.callback({deviceId:deviceId,connected:true}); + } catch (error) { + console.error( + "执行蓝牙恢复连接的回调出现异常,", + error) + } + }); + } // 处理 MTU 设置 if (plus.os.name === 'Android') { @@ -1714,7 +1729,7 @@ class BleHelper { let buffer = new ArrayBuffer(bufferSize); let dataView = new DataView(buffer); for (let i = 0; i < array.length; i++) { - dataView.setUint8(i, array); + dataView.setUint8(i, array[i]); } return this.sendData(deviceid, buffer, writeServiceId, wirteCharactId, ms); @@ -1830,7 +1845,7 @@ class BleHelper { Promise.race([timeOut(ms), promise]).then(resolve).catch((ex) => { // console.error("ex=", ex); if (ex.code == -1) { - // console.error('专业给Uniapp填坑,发送消息永无回调'); + console.error('专业给Uniapp填坑,发送消息永无回调'); resolve(ex); } else { reject(ex); diff --git a/utils/BleReceive.js b/utils/BleReceive.js index 45ef6f7..ab8990e 100644 --- a/utils/BleReceive.js +++ b/utils/BleReceive.js @@ -256,7 +256,7 @@ class BleReceive { } Receive_670(receive, f, path, recArr) { - console.log("pagh=", path); + // console.log("pagh=", path); var todo = (bytes) => { // console.log("todo",receive); let receiveData = {}; @@ -283,8 +283,8 @@ class BleReceive { // console.log("todo"); // 解析照明档位 let lightingLevelByte = bytes[2]; - let lightingLevelText = lightingLevelByte === 0x6d ? 'hight' : lightingLevelByte === 0x6e ? - 'low' : 'close'; + let lightingLevelText = lightingLevelByte === 0x6d ? 'qiang' : lightingLevelByte === 0x6e ? + 'ruo' : 'close'; // 解析剩余照明时间(第三和第四字节,小端序) let lightingTime = (bytes[3] << 8) | bytes[4]; @@ -569,7 +569,7 @@ class BleReceive { try { - console.log("str=",receive.str); + // console.log("str=",receive.str); receiveData = JSON.parse(receive.str); let recCnt = recArr.find(v => { diff --git a/utils/MqHelper.js b/utils/MqHelper.js new file mode 100644 index 0000000..ccbf7d8 --- /dev/null +++ b/utils/MqHelper.js @@ -0,0 +1,169 @@ +import MqttClient from '@/utils/mqtt.js'; + + +class MqHelper { + constructor() { + this.instance = null; + this.subTopics=[]; + } + + timeout(ms) { + if (!ms) { + ms = 200; + } + return new Promise((succ, err) => { + setTimeout(() => { + err({ + code: -1, + errMsg: '超时了' + }) + }, ms); + }); + + } + //打开mq连接,topics:要订阅的主题列表格式{topic:'主题',callback:fn}或此格式的数组 + init(topics) { + let connect = () => { + return new Promise((resolve, reject) => { + this.instance = new MqttClient(); + this.instance.connect((res) => { + if(topics){ + console.error("连接成功开始订阅消息",topics); + setTimeout(this.subscribes, 50, topics) + } + + resolve(); + }); + }); + } + + let p1 = connect(); + let p2 = this.timeout(500); + + return new Promise((resolve, reject) => { + Promise.race([p1, p2]).then(()=>{ + resolve() + }).catch(ex=>{ + this.instance=null; + reject(ex); + }); + }); + + } + //订阅消息 + subscribes(topics) { + return new Promise((resolve,reject)=>{ + + + if(!this.instance || !topics){ + reject(); + return; + } + if (!Array.isArray(topics)) { + topics = [topics]; + } + for (let i = 0; i < topics.length; i++) { + let item = topics[i]; + let f=this.subTopics.find(v=>{ + return v.topic===item.topic; + }); + if(!f){ + this.subTopics.push(item); + } + this.instance.subscribe(item.topic, (payload, receive) => { + try { + console.log("开始处理回调") + item.callback({payload:payload,receive:receive}); + console.log("处理回调成功") + } catch (err) { + console.error("订阅消息回调出现异常,",err); + } + }); + + console.error("订阅消息成功"); + } + resolve(); + }); + + } + //取消订阅消息 单个或者数组 + unSubscribes(topics){ + if(!this.instance || !topics){ + return; + } + if (!Array.isArray(topics)) { + topics = [topics]; + } + + for (let i = 0; i < topics.length; i++) { + let item = topics[i]; + this.instance.unsubscribe(item.topic); + this.subTopics.find((v,index)=>{ + if(v.topic===item.topic){ + this.subTopics.splice(index,1); + return true; + } + return false; + }); + } + + } + //发送消息,主题 消息内容 是否保留消息 + sendData(topic,msg,retained){ + return new Promise((resolve,reject)=>{ + if(!this.instance || !topic){ + reject("MQTT未连接或无主题无法发布消息") + return + } + if(msg===null || msg===undefined){ + reject("msg无内容") + return; + } + + if(!retained){ + retained=false; + }else{ + retained=true; + } + + try { + let flag = this.instance.publish(topic, msg, retained); + if (flag) { + resolve({ + code: 200, + msg: 'MQTT直连发送成功' + }); + return; + } + + reject("MQTT未连接,无法发布消息"); + } catch (error) { + + reject(error); + } + }); + + + } + //断开连接 + disconnect(){ + if(!this.instance){ + return + } + this.instance.disconnect() + } + +} + + + +export default { + getMqTool: function(found, receive) { + + let instance = new MqHelper(); + + + + return instance; + } +} \ No newline at end of file diff --git a/utils/mqtt.js b/utils/mqtt.js index b92530b..2297fa2 100644 --- a/utils/mqtt.js +++ b/utils/mqtt.js @@ -281,11 +281,17 @@ class MqttClient { } } - publish(topic, message) { + publish(topic, message,retained) { if (this.client && this.client.isConnected()) { const mqttMessage = new Paho.Message(message); mqttMessage.destinationName = topic; mqttMessage.qos = 1; + console.log("typeof(retained)=",typeof(retained)) + if(typeof(retained)==='boolean'){ + console.log("retained=",retained) + mqttMessage.retained=retained; + } + this.client.send(mqttMessage); console.log(`成功发布消息到主题 ${topic}: ${message}`); return true;