Ver código fonte

Merge branch 'master' of git.beswell.com:duanchangpeng/flyLong

# Conflicts:
#	online/src/page/mainpage.vue
Changpeng Duan 5 anos atrás
pai
commit
a6fb61e6f5
1 arquivos alterados com 136 adições e 107 exclusões
  1. 136 107
      online/src/page/mainpage.vue

+ 136 - 107
online/src/page/mainpage.vue

@@ -1,7 +1,7 @@
 <template>
     <div class="content">
         <div class="top" :style="{backgroundColor:bgColor}">
-            <span class="watchState">{{watchText}}</span>
+            <span class="watchState">{{watchText}} {{battery}}</span>
             <div class="toprow">
                 <div class="lt">
                     <div class="activeLevel">
@@ -96,7 +96,9 @@
                             {'normalJump':activeLevel > 30 &&  activeLevel <= 90},
                             {'fastJump':activeLevel > 90},
                             {'slowJump':activeLevel < 30},
-                            ]">{{heartRate}}</em>
+                            ]"
+                :style="{bottom:parseInt(screenHeight -  30) +'px'}"
+            >{{heartRate}}</em>
         </div>
         <mu-dialog title="" width="600" max-width="80%" :esc-press-close="true"
                    :overlay-close="true" :open.sync="openAlert">
@@ -120,13 +122,16 @@
 
 <script>
     import LineExample from '../components/LineExample'
-
+    import {
+        SchoolTimeQueryByDate,
+    } from '../api/getApiRes.js'
     export default {
         data() {
             return {
                 dataLabels: [],
                 dataDatasets: [],
                 watchText: '',
+                battery: '',
                 userHead: 'http://xhead.beswell.com/flyhead.png',
                 watchState: false,//false
                 bgColor: '#028FE1',
@@ -158,22 +163,7 @@
         },
         mounted() {
             this.getBodyInfo();
-            // test
-            // this.begin = new Date('2021-02-04 16:13:01');
-            // this.dataLabels = ['', '', '', '', '', '', '', '', ''];
-            // this.heartLine = [65];
-            // let heartRate = 0;
-            // this.timer = setInterval(() => {
-            //     heartRate = parseInt(Math.random()*100+70);
-            //     this.heartRate = heartRate;
-            //     this.activeState = true;
-            //     this.watchState = true;
-            //     this.dataLabels.push('');
-            //     this.heartLine.push(heartRate);
-            //     this.calcCalorie(heartRate);
-            //     this.calcActLevel(heartRate);
-            //     this.DrawHeartLine(this.heartLine, this.dataLabels)
-            // },1000);
+            this.init();
         },
         watch: {
             activeLevel(val) {
@@ -207,18 +197,65 @@
             }
         },
         methods: {
+            init() {
+                let that = this;
+                // 页面初始化操作
+                document.addEventListener('plusready', function (e) {
+                    plus.bluetooth.onBLECharacteristicValueChange(function (e) {
+                        console.log(e.characteristicId);
+                        if (e.characteristicId == '00002A19-0000-1000-8000-00805F9B34FB') {
+                            // 获取电量
+                            var value = that.buffer2hexPower(e.value);
+                            that.battery = value + '%';
+                        } else {
+                            // 成功获取心跳
+                            that.heartRate = parseInt(that.parseHeartRate(e.value).heartRate);
+                            that.watchText = '心率带已连接';
+                            that.activeState = true;
+                            // 运动强度计算
+                            that.calcActLevel(that.heartRate);
+                            that.openAlert = false;//关闭弹窗
+                            that.watchState = true;//关闭连接按钮
+                            // 卡路里计算
+                            that.calcCalorie(that.heartRate);
+                            if (that.heartLine.length > 30) {
+                                that.heartLine.unshift();
+                                that.dataLabels.unshift();
+                            } else {
+                                that.heartLine.push(that.heartRate);
+                                that.dataLabels.push('');
+                            }
+                            that.DrawHeartLine(that.heartLine, that.dataLabels);
+                            // that.reportInfo();
+                        }
+                    });
+                });
+            },
+            buffer2hexPower(value) {
+                var t = '';
+                if (value) {
+                    var v = new Uint8Array(value);
+                    for (var i in v) {
+                        // t += '0x' + v[i].toString(16) + ' ';
+                        t += v[i].toString(10) + ' ';
+                    }
+                } else {
+                    t = '无效值';
+                }
+                return t;
+            },
             connectWatch() {
                 // 打开蓝牙适配器
                 let that = this;
                 plus.bluetooth.openBluetoothAdapter({
                     success: function (e) {
-                        that.Toast('蓝牙适配器打开成功!','success');
+                        that.Toast('蓝牙适配器打开成功!', 'success');
                         that.openAlert = true;
                         that.searchWatch();
                         return e
                     },
                     fail: function (e) {
-                        that.Toast('打开失败! 请确认蓝牙开关已开启','error');
+                        that.Toast('打开失败! 请确认蓝牙开关已开启', 'error');
                     }
                 });
             },
@@ -232,13 +269,13 @@
                 plus.bluetooth.startBluetoothDevicesDiscovery({
                     // services: ['heart_rate'],//ios不支持
                     success: function (e) {
-                        that.Toast('开始搜索成功!','success');
+                        that.Toast('开始搜索成功!', 'success');
                         plus.bluetooth.onBluetoothDeviceFound(function (e) {
                             let devices = e.devices;
                             for (let i in devices) {
                                 let device = devices[i];
                                 // localName CL831
-                                if (device.localName.includes('CL831')) {
+                                if (device.localName.indexOf('CL831') != -1) {
                                     console.log(JSON.stringify(device));
                                     that.watchs.push(device)
                                 }
@@ -253,12 +290,12 @@
             },
             choiceWatch(w) {
                 let that = this;
-                that.Toast('连接心率带' + w.localName + '中...','success');
+                that.Toast('连接心率带' + w.localName + '中...', 'success');
                 // 建立连接
                 plus.bluetooth.createBLEConnection({
                     deviceId: w.deviceId,
                     success: function (e) {
-                        that.Toast('连接成功!','success');
+                        that.Toast('连接成功!', 'success');
                         // 渲染完毕再执行这个,所以才能没有延时的更改
                         // 需要一个回调等待5S
                         clearTimeout(that.timer);           //清除延迟执行
@@ -274,12 +311,11 @@
             },
             getServices(w) {
                 let that = this;
-                that.Toast('获取蓝牙设备服务:');
-                console.log(w.deviceId);
+                that.Toast('获取蓝牙设备服务:', 'success');
                 plus.bluetooth.getBLEDeviceServices({
                     deviceId: w.deviceId,
                     success: function (e) {
-                        that.Toast('获取服务成功!','success');
+                        that.Toast('获取服务成功!', 'success');
                         // 获取服务的特征值
                         // console.log('服务的特征值' + w.advertisServiceUUIDs[0]);
                         var HEARTuuid = "0000180D-0000-1000-8000-00805F9B34FB";//HEART RATE
@@ -287,10 +323,9 @@
                         that.getBatteryCharacteristics(w.deviceId, Batteryuuid);
                         clearTimeout(that.timer);           //清除延迟执行
                         that.timer = setTimeout(() => {   //设置延迟执行
-                            // 获取服务
+                            // 获取心跳服务
                             that.getCharacteristics(w.deviceId, HEARTuuid);
-                        }, 5000);
-
+                        }, 3000);
                     },
                     fail: function (e) {
                         console.log('获取服务失败! ' + JSON.stringify(e));
@@ -309,13 +344,17 @@
                     serviceId: serviceId,
                     success: function (e) {
                         let characteristics = e.characteristics;
-                        that.Toast('获取特征值成功! ' + characteristics.length,'success');
+                        that.Toast('获取特征值成功! ' + characteristics.length, 'success');
+                        console.log('获取特征值成功! ' + characteristics.length, 'success');
+                        console.log('特征值列表: ' + JSON.stringify(characteristics));
                         plus.bluetooth.notifyBLECharacteristicValueChange({	//监听数据变化
                             deviceId: deviceId,
                             serviceId: serviceId,
                             characteristicId: chaaracterUuid,
                             success: function (e) {
-                                that.onValuesChange();
+                                console.log('---> 获取心跳服务' + JSON.stringify(e));
+                                console.log('---> notifyBLECharacteristicValueChange ' + chaaracterUuid + ' success.');
+                                //that.onValuesChange();
                             },
                             fail: function (e) {
                                 that.Toast('notifyBLECharacteristicValueChange failed! ' + JSON.stringify(e));
@@ -334,21 +373,22 @@
                 let chaaracterUuid = '00002A19-0000-1000-8000-00805F9B34FB';
                 let characteristicId = null;
                 that.begin = new Date();
+                that.readValue(deviceId, serviceId, chaaracterUuid);
                 plus.bluetooth.getBLEDeviceCharacteristics({
                     deviceId: deviceId,
                     serviceId: serviceId,
                     success: function (e) {
-                        let characteristics = e.characteristics;
-                        that.Toast('获取特征值成功! ' + characteristics.length,'success');
+                        var characteristics = e.characteristics;
+                        that.Toast('获取心率带服务成功! ' + characteristics.length, 'success');
                         plus.bluetooth.notifyBLECharacteristicValueChange({	//监听数据变化
                             deviceId: deviceId,
                             serviceId: serviceId,
                             characteristicId: chaaracterUuid,
                             success: function (e) {
-                                that.onBatteryValuesChange();
+                                console.log('notifyBLECharacteristicValueChange' + JSON.stringify(e));
                             },
                             fail: function (e) {
-                                that.Toast('notifyBLECharacteristicValueChange failed! ' + JSON.stringify(e));
+                                that.Toast('notifyBLECharacteristicValueChange失败! ' + JSON.stringify(e));
                             }
                         });
                     },
@@ -358,63 +398,25 @@
                     }
                 })
             },
-            // readValue(deviceId, serviceId, characteristicId) {
-            //     let that = this;
-            //     plus.bluetooth.readBLECharacteristicValue({
-            //         deviceId: deviceId,
-            //         serviceId: serviceId,
-            //         characteristicId: characteristicId,
-            //         success: function (e) {
-            //             that.Toast('读取数据成功!');
-            //             that.Toast(e);
-            //             console.log("读取数据成功" + e);
-            //         },
-            //         fail: function (e) {
-            //             that.Toast('读取数据失败! ' + JSON.stringify(e));
-            //         }
-            //     });
-            // },
-            // 获取基础身体数据
-            getBodyInfo() {
-                // todo
-            },
-            onValuesChange() {
+            readValue(deviceId, serviceId, characteristicId) {
                 let that = this;
-                // todo 提出来,放在init里 有值就监听
-                // 监听低功耗蓝牙设备的特征值变化
-                plus.bluetooth.onBLECharacteristicValueChange(function (e) {
-                    console.log('onBLECharacteristicValueChange: ' + JSON.stringify(e));
-                    console.log('e.value: ' + JSON.stringify(e.value));
-                    console.log('---> parseHeartRate() = ' + that.parseHeartRate(e.value).heartRate);
-                    // 成功获取心跳
-                    that.heartRate = parseInt(that.parseHeartRate(e.value).heartRate);
-                    that.watchText = '心率带已连接';
-                    that.activeState = true;
-                    // 运动强度计算
-                    that.calcActLevel(that.heartRate);
-                    that.openAlert = false;//关闭弹窗
-                    that.watchState = true;//关闭连接按钮
-                    // 卡路里计算
-                    that.calcCalorie(that.heartRate);
-                    if (that.heartLine.length > 30) {
-                        that.heartLine.unshift();
-                        that.dataLabels.unshift();
-                    } else {
-                        that.heartLine.push(that.heartRate);
-                        that.dataLabels.push('');
+                plus.bluetooth.readBLECharacteristicValue({
+                    deviceId: deviceId,
+                    serviceId: serviceId,
+                    characteristicId: characteristicId,
+                    success: function (e) {
+                        console.log("读取电量数据成功" + JSON.stringify(e));
+                    },
+                    fail: function (e) {
+                        that.Toast('读取电量数据失败! ' + JSON.stringify(e));
                     }
-                    that.DrawHeartLine(that.heartLine,that.dataLabels);
-                });
-            },
-            onBatteryValuesChange() {
-                let that = this;
-                // 监听低功耗蓝牙设备的特征值变化
-                plus.bluetooth.onBLECharacteristicValueChange(function (e) {
-                    console.log('onBLECharacteristicValueChange: ' + JSON.stringify(e));
-                    console.log('Battery: ' + e.value);
-                    // console.log('---> Battery () = ' + that.parseHeartRate(e.value).heartRate);
                 });
             },
+            // 获取基础身体数据
+            getBodyInfo() {
+                // todo
+            }
+            ,
             parseHeartRate(data) {
                 var data = new DataView(data);
                 const flags = data.getUint8(0);
@@ -447,17 +449,20 @@
                     result.rrIntervals = rrIntervals;
                 }
                 return result;
-            },
+            }
+            ,
             // 活动强度计算公式
             calcActLevel(heartRate) {
                 let sum = 170 - this.age;
                 this.activeLevel = parseInt((heartRate / sum) * 100);
-            },
+            }
+            ,
             // 持续时间
             calcHoldTime(curTime) {
                 let res = parseInt(curTime.getTime() - this.begin.getTime()) / 1000;
                 return parseInt(res)
-            },
+            }
+            ,
             // 计算卡路里
             calcCalorie(heartRate) {
                 let that = this;
@@ -471,33 +476,38 @@
                     calorieNoVo2 = ((-20.4022 + (0.4472 * heartRate) + (0.1263 * that.weight) + (0.074 * that.age)) / 4.184) / 60 * 1;
                 }
 
-                that.cal =  that.cal + calorieNoVo2;
+                that.cal = that.cal + calorieNoVo2;
                 // 计算CK
                 that.calcCk(that.cal);
                 that.calcSportCal(calorieNoVo2, curTime)
-            },
+            }
+            ,
             // 计算CK
             calcCk(cal) {
                 this.ck = parseFloat(cal / this.weight).toFixed(2);
-            },
+            }
+            ,
             // 计算运动卡路里
             calcSportCal(calorieNoVo2) {
                 let bmrMSjDaily = (10.00 * this.weight) + (6.25 * this.height) - (5.00 * this.age) + 5.00;
                 let BmrMSjRmrcb = parseFloat((bmrMSjDaily * 1.1) / 24);
                 let calorieNoVo2Pure = (calorieNoVo2 - BmrMSjRmrcb / 3600 * 1);
                 this.sportCal = this.sportCal + calorieNoVo2Pure;
-            },
+            }
+            ,
             // 绘制心电图
             DrawHeartLine(heartLine, dataLabels) {
                 this.dataLabels = dataLabels;
                 this.dataDatasets = heartLine;
                 this.calcMaxHr(heartLine);
                 this.calcAveHr(heartLine);
-            },
+            }
+            ,
             // 最大心率
             calcMaxHr(heartLine) {
                 this.heartMax = Math.max(...heartLine);
-            },
+            }
+            ,
             // 平均心率
             calcAveHr(heartLine) {
                 var sum = 0;
@@ -506,6 +516,23 @@
                 }
                 this.heartAverage = parseInt(sum / heartLine.length);
             },
+            // 上报信息
+            reportInfo() {
+                // todo
+                let that = this;
+                let param = {
+                    token: localStorage.token,
+                    regionid: regionid,
+                };
+                let postdata = qs.stringify(param);
+                SchoolTimeQueryByDate(postdata).then(res => {
+                    let json = res;
+                    if (json.Code == 0) {
+                    } else {
+                        that.$message.error(json.Memo);
+                    }
+                })
+            },
         },
         filters: {
             parseLevel: function (value) {
@@ -518,14 +545,17 @@
                         return '弱'
                     }
                 }
-            },
-            parseToInt:function (val) {
+            }
+            ,
+            parseToInt: function (val) {
                 return parseInt(val)
-            },
-            parseToFix2:function (val) {
+            }
+            ,
+            parseToFix2: function (val) {
                 return parseFloat(val).toFixed(2)
             }
-        },
+        }
+        ,
         components: {
             LineExample
         }
@@ -551,7 +581,7 @@
         line-height: 20px;
         color: #fff;
         text-align: center;
-        margin-top: 10px;
+        margin-top: 20px;
         margin-bottom: 10px;
     }
 
@@ -999,12 +1029,11 @@
     }
 
     /deep/ .mu-dialog {
-        background-color: rgba(0, 0, 0, 0.75);
+        background-color: rgba(0, 0, 0, 0.85);
         color: #fff;
         border-radius: 20px;
         background-size: 100%;
         background-origin: content-box;
-        filter: blur(0.5px);
     }
 
     /deep/ .mu-dialog p {