智方网络_吖荣 3 лет назад
Родитель
Сommit
029b52054d
5 измененных файлов с 96 добавлено и 55 удалено
  1. 15 5
      src/components/device/device.vue
  2. 12 10
      src/pages/index/index.vue
  3. 15 10
      src/pages/start/index.vue
  4. 22 0
      src/requests/game.js
  5. 32 30
      src/utils/bluetooth.js

+ 15 - 5
src/components/device/device.vue

@@ -142,7 +142,7 @@ import toy_connecting from "@/components/device/toy/connecting";
 import gameIng from "@/pages/start/index";
 //获取个人信息
 import Toast from "../../../static/vant/toast/toast";
-import {game_devices, getDeviceBySn} from "../../requests/game";
+import {game_devices, getDeviceBySn, setDeviceInDb} from "../../requests/game";
 import game_store from "@/store/game";
 import bluetooth from "@/utils/bluetooth";
 import ble_store from "../../store/bluetooth";
@@ -200,7 +200,9 @@ export default {
       game_status: 0,
 
       //当前发送的hex码
-      current_hex: ""
+      current_hex: "",
+      //判断是否已经连接教具
+      toy_connected:false
     };
   },
   methods: {
@@ -312,7 +314,7 @@ export default {
     open_choose_toy() {
       // $this.choose_toy_window.show = false;
       $this.choose_toy_window.show = true;
-
+      $this.toy_connected = false
       bluetooth.sendControlClose();
     },
     // 选好玩具
@@ -391,7 +393,13 @@ export default {
                     $this.device.deviceId = $data.device_id;
                     $this._deviceId = $data.device_id;
                   }
-                  $this.createBLEConnection()
+                  if ($this._deviceId) {
+                    console.log("finded")
+                    $this.createBLEConnection()
+                  } else {
+                    console.log('unfinded')
+                    $this.onBluetoothDeviceFound();
+                  }
                 }
               });
             } else {
@@ -444,8 +452,10 @@ export default {
               $this.device = device;
               game_store.setters.setDeviceId(device.deviceId);
               $this._deviceId = device.deviceId;
-
               $this.createBLEConnection();
+              //录入库
+              setDeviceInDb($this.code,device.deviceId)
+
             }
           });
         });

+ 12 - 10
src/pages/index/index.vue

@@ -71,8 +71,8 @@
         </van-row>
       </div>
     </div>
-    <van-toast id="van-toast" />
-    <van-dialog id="van-dialog" />
+    <van-toast id="van-toast"/>
+    <van-dialog id="van-dialog"/>
     <!--     <button class="cu-btn lg cu-btn-primary text-white text-center margin-tb" @click="auth_msg">获取消息授权-->
     <!--    </button>-->
   </div>
@@ -86,10 +86,10 @@ import personal_container from "@/components/index/personal";
 import device_container from "@/components/device/device";
 import Toast from "../../../static/vant/toast/toast";
 import Dialog from "../../../static/vant/dialog/dialog";
-import { userDetail, setUserLogin } from "../../requests/user";
+import {userDetail, setUserLogin} from "../../requests/user";
 import user_store from "../../store/index";
 import game_store from "../../store/game";
-import game_util, { post_data } from "../../utils/game";
+import game_util, {post_data} from "../../utils/game";
 
 var $this;
 export default {
@@ -111,7 +111,8 @@ export default {
     };
   },
   methods: {
-    get_init_data() {},
+    get_init_data() {
+    },
     //切换到扫码
     change_status($status) {
       this.login_status = $status;
@@ -172,10 +173,10 @@ export default {
     });
   },
   onShow() {
-    if(wx.getStorageSync("is_other_first") == false){
-       wx.setStorageSync("is_other", false);
-    } else{
-       wx.setStorageSync("is_other", true);
+    if (wx.getStorageSync("is_other_first") == false) {
+      wx.setStorageSync("is_other", false);
+    } else {
+      wx.setStorageSync("is_other", true);
     }
 
     // wx.getStorageInfo({
@@ -194,7 +195,8 @@ export default {
       user_store.setters.set_userinfo($this.userinfo);
     });
   },
-  onHide() {},
+  onHide() {
+  },
   onLoad($option) {
     if (process.env.NODE_ENV == "development") {
       wx.setStorageSync("token", "ogsti72oUSkbPl4OL7gl9woLTxR6fk4V");

+ 15 - 10
src/pages/start/index.vue

@@ -42,8 +42,7 @@
     <div class="game_status padding-tb">
       <van-row>
         <van-col
-          span="10"
-          offset="2"
+          span="8"
         >
           <div
             class="flex flex-direction align-center justify-center"
@@ -68,7 +67,7 @@
             <text class="text-default">未正确佩戴</text>
           </div>
         </van-col>
-        <van-col span="10">
+        <van-col span="8">
           <div class="flex flex-direction align-center justify-center">
             <view class="elc_power_container">
               <img
@@ -82,16 +81,21 @@
             <text class="text-default">设备电量</text>
           </div>
         </van-col>
-        <!-- <van-col span="2">
+
+        <van-col span="8">
           <div class="flex flex-direction align-center justify-center">
-            <view>
-              <text>-{{ rssi }}dbm</text>
-            </view>
-            <view>
-              <text class="text-default">信号强度</text>
+            <view class="elc_power_container">
+              <img
+                src="https://img.shuimuai.com/web/dianchi_3.png"
+                alt=""
+                :style="{ width: toy_power + '%' }"
+                class="device_elc"
+              />
             </view>
+            <text class="text-default">{{ toy_power }}%</text>
+            <text class="text-default">教具电量</text>
           </div>
-        </van-col> -->
+        </van-col>
       </van-row>
     </div>
 
@@ -344,6 +348,7 @@ export default {
       //  蓝牙信号强度
       rssi: 0,
       game_status: 0,
+      toy_power:100
     };
   },
   methods: {

+ 22 - 0
src/requests/game.js

@@ -53,3 +53,25 @@ export function getDeviceBySn($sn){
   }
   return fly.post("equipment/equipment-search",$params)
 }
+
+//2021年10月22日11:11:03
+// 写入设备
+export function setDeviceInDb($sn,$deviceId){
+  let $params = {
+    "access_token":wx.getStorageSync("token"),
+    "sn":$sn,
+    "type":1,
+  }
+  wx.getSystemInfo({
+    success(res){
+      $params['is_android'] = res.platform != "ios"
+      if (res.platform == "ios"){
+        $params['ios_device_id'] = $deviceId
+      }else{
+        $params['device_id'] = $deviceId;
+      }
+    }
+  })
+  return fly.post("https://testadmin.shuimuai.com/equipment/factory",$params)
+}
+

+ 32 - 30
src/utils/bluetooth.js

@@ -233,7 +233,7 @@ export default {
     const that = this;
     wx.onBLECharacteristicValueChange((characteristic) => {
       let hexStr = that.ab2hex(characteristic.value);
-      if (hexStr.toUpperCase().indexOf("AADD")!=-1){
+      if (hexStr.toUpperCase().indexOf("AADD") != -1) {
         console.log(hexStr);
       }
       let $data = that.get_big_data(hexStr);
@@ -261,13 +261,17 @@ export default {
             wx.showToast({
               title: "已连接到" + $this.toy_item.name
             });
+            $this.toy_connected = true;
             $this.change_toy_connect_status(2);
+            //连接成功后 获取一次教具电量信息
+            that.SendOrder('8a')
           }
         }
       }
 
 
-      //2021年10月21日16:30:07 获取教具电量
+      //2021年10月21日16:30:07
+      // 获取教具电量
       if (hexStr.toUpperCase().indexOf("AADD8A") != -1) {//接收教具电量状态
         let $_hexStr = hexStr.substr(6);
         let $power = parseInt($_hexStr.substr(0, 2), 16)
@@ -289,35 +293,32 @@ export default {
       //2021年10月21日17:22:37
       //收到一次UUID就发送一次获取教具的电量
       //读取教具UUID
-      if (hexStr.toUpperCase().indexOf("AADD84") != -1) {//接收UUID 5个字节
-        let $_hexStr = hexStr.substr(6);
-        let $datas = $_hexStr.substr(0, 10);
-        if ($datas != "0000000000") {
-          $this.toy_UUID = $datas;
-          //读取教具电量
-          that.SendOrder('8a')
-        } else {
-          //如果游戏中则重新连接
-          if ($game_status == 1) {
-            //先发UUID 再发id 02
-            console.log($this.toy_UUID)
-            console.log(`aa cc 03 00 ${$this.toy_hex} 02 0a`)
-          } else {
-            $this.toy_UUID = ""
-            $this.toy_power = 0;
-
-          }
-        }
-
-      }
+      // if (hexStr.toUpperCase().indexOf("AADD84") != -1) {//接收UUID 5个字节
+      //   let $_hexStr = hexStr.substr(6);
+      //   let $datas = $_hexStr.substr(0, 10);
+      //   if ($datas != "0000000000") {
+      //     $this.toy_UUID = $datas;
+      //     //读取教具电量
+      //     that.SendOrder('8a')
+      //   } else {
+      //     //如果游戏中则重新连接
+      //     if ($game_status == 1) {
+      //       //先发UUID 再发id 02
+      //       console.log($this.toy_UUID)
+      //       console.log(`aa cc 03 00 ${$this.toy_hex} 02 0a`)
+      //     } else {
+      //       $this.toy_UUID = ""
+      //       $this.toy_power = 0;
+      //
+      //     }
+      //   }
+      //
+      // }
 
 
       //  监听脑环电量
       let $power = that.get_device_elc(hexStr);
       if ($power) {
-        //发送一次获取教具的UUID
-        that.SendOrder("84");
-
         // console.log("当前脑环电量:", $power)
         $this.device_power = $power;
       }
@@ -341,9 +342,10 @@ export default {
             game_store.setters.setPlayedTime($this.played_time)
             $this.played_time_text = that.formatPlaySeconds($this.played_time);
             $this.do_datas($data);
-            // if ($this.played_time_text.indexOf(":00") != -1 || $this.played_time_text.indexOf(":30") != -1) {
-            //   $this.post_data();
-            // }
+            if ($this.played_time % 10 == 0) {
+                  //读取教具电量
+                  that.SendOrder('8a')
+            }
           }
           if ($this.played_time == 0) {
             $this.post_data();
@@ -416,7 +418,7 @@ export default {
     return $errmsg;
   },
   //写入buffer
-  WriteBufferInBle($hex,$buffer_len = 8) {
+  WriteBufferInBle($hex, $buffer_len = 8) {
     let $device_id = game_store.getters.getDeviceId();
     let $service_id = game_store.getters.getServiceId()
     let $charateristic = game_store.getters.getCharacterId();