Ver código fonte

feature:增加蓝牙的指令队列

zeng.chen 8 meses atrás
pai
commit
8368209772

+ 9 - 9
devices/QueueManager.js

@@ -5,20 +5,20 @@ class QueueManager {
         this._tasks = [];
         this._listenMap = {};
         this._timer = null;
-        this._interval = 60;
+        this._interval = 200;
     }
 
-    static get instance() {
-        if (!QueueManager._instance) {
-            QueueManager._instance = new QueueManager();
-        }
-        return QueueManager._instance;
-    }
+    // static get instance() {
+    //     if (!QueueManager._instance) {
+    //         QueueManager._instance = new QueueManager();
+    //     }
+    //     return QueueManager._instance;
+    // }
 
     addTask(task) {
-        console.log(`QueueManager======addTask======${task}`);
+        // console.log(`QueueManager======addTask======${task}`);
         this._tasks.unshift(task);
-        // mqttAddDebugCmd(`指令进队列:${task.toString()}, 数量:${this._tasks.length}`);
+        // console.log(`指令进队列:${task.toString()}, 数量:${this._tasks.length}`);
     }
 
     listenTask(callback) {

+ 1 - 1
devices/bluetooth/bt_cmd.js

@@ -176,7 +176,7 @@ class BtCmd {
     // 校验设备
     static checkDevice() {
         let secretKey = this.stringToUint8Array(CmdBase.secretKey)
-        console.log(`checkDevice========${secretKey}`);
+        // console.log(`checkDevice========${secretKey}`);
         return this._build(CmdBase.checkDevice, secretKey);
     }
 

+ 79 - 69
devices/bt_helper.js

@@ -10,7 +10,7 @@ const BtParse = require('./../devices/bluetooth/bt_parse');
 // const EventManager = require('./EventManager');
 // const CmdBase = require('./../devices/bluetooth/bt_cmd');
 const bleManager = require('./ble_manager');
-// const QueueManager = require('./QueueManager');
+const QueueManager = require('./QueueManager');
 
 // const EnumConnectStatus = require('./EnumConnectStatus');
 // const EnumOpen = require('./EnumOpen');
@@ -52,10 +52,12 @@ class BtHelper {
         //         this._helper.send({ cmd: task });
         //     }
         // });
-        // this.queueManager = new QueueManager();
-        // this.queueManager.listenTask((task) => {
-        //     bleManager.sendData(task);
-        // });
+        this.queueManager = new QueueManager();
+        let _this = this
+        this.queueManager.clear();
+        this.queueManager.listenTask((task) => {
+            _this.bleManager.sendData(task);
+        });
     }
 
 
@@ -90,79 +92,83 @@ class BtHelper {
     async _connectSuccess() {
         this.checkDevice()
         this.getDeviceInfo()
+
     }
 
     async connect(data, onChanged) {
         // await this._helper.connect({ data, onChanged, isClick });
-        try {
-            clearTimeout(this.timer);
-            this.timer = null;
-            this.bleManager.stopScan()
-            var res = await this.bleManager.connectToDevice(data);
-            console.log(res ? '连接成功' : '连接失败');
-            // this.setData({ connectedDeviceId: deviceId });
-            if (res === false) {
-                console.log("连接失败")
-                this.disconnect(data)
-                if (onChanged) {
-                    onChanged(false)
-                }
-                return
+
+        clearTimeout(this.timer);
+        this.timer = null;
+        this.bleManager.stopScan()
+        var res = await this.bleManager.connectToDevice(data);
+        console.log(res ? '连接成功' : '连接失败');
+        // this.setData({ connectedDeviceId: deviceId });
+        if (res === false) {
+            console.log("连接失败")
+            this.disconnect(data)
+            if (onChanged) {
+                onChanged(false)
             }
-            const serviceId = await this.bleManager.discoverServices(data.deviceId);
-            if (serviceId == "") {
-                console.log("连接失败")
-                this.disconnect(data)
-                if (onChanged) {
-                    onChanged(false)
-                }
-                return
+            return
+        }
+        const serviceId = await this.bleManager.discoverServices(data.deviceId);
+        if (serviceId == "") {
+            console.log("连接失败")
+            this.disconnect(data)
+            if (onChanged) {
+                onChanged(false)
             }
-            // this.setData({ services });
-            console.log("服务ID:" + serviceId)
-
-            var characteristics = await this.bleManager.discoverCharacteristics(data.deviceId, serviceId)
-            if (characteristics == "") {
-                console.log("连接失败")
-                this.disconnect(data)
-                if (onChanged) {
-                    onChanged(false)
-                }
-                return
+            return
+        }
+        // this.setData({ services });
+        console.log("服务ID:" + serviceId)
+
+        var characteristics = await this.bleManager.discoverCharacteristics(data.deviceId, serviceId)
+        if (characteristics == "") {
+            console.log("连接失败")
+            this.disconnect(data)
+            if (onChanged) {
+                onChanged(false)
             }
-            console.log('device特征值:', characteristics)
-            for (let i = 0; i < characteristics.length; i++) {
-                let charc = characteristics[i];
-                if (charc.properties.notify) {
-                    console.log('订阅数据:');
-                    // 订阅的
-                    this.bleManager.notifyCharacteristicValueChange(charc.uuid, (res) => {
-                        BtParse.parseTLV(res);
-                    })
-                }
-                if (charc.properties.write || charc.properties.writeWithoutResponse) {
-                    // 写入的
-                    this.bleManager.setWrite(charc, charc.uuid)
-
-                    setTimeout(() => {
-                        this._connectSuccess()
-                    }, 1000);
-
-                    if (onChanged) {
-                        onChanged(true)
-                    }
-                }
-                if (charc.properties.read) {
-                    var chara = await this.bleManager.readCharacteristicValue(charc.uuid,)
+            return
+        }
+        console.log('device特征值:', characteristics)
+        // let uuids = ["ab00", "ffc0"];
+        for (let i = 0; i < characteristics.length; i++) {
+            let charc = characteristics[i];
+            // if (!uuids.includes(charc.uuid)) {
+            //     if (onChanged) {
+            //         console.log('没有特征值:', charc.uuid);
+            //         onChanged(false)
+            //     }
+            //     return;
+            // }
+            if (charc.properties.notify) {
+                console.log('订阅数据:', charc.uuid);
+                // 订阅的
+                this.bleManager.notifyCharacteristicValueChange(charc.uuid, (res) => {
+                    BtParse.parseTLV(res);
+                })
+            }
+            if (charc.properties.write || charc.properties.writeWithoutResponse) {
+                // 写入的
+                this.bleManager.setWrite(charc, charc.uuid)
+                setTimeout(() => {
+                    this._connectSuccess()
+                }, 500);
+                if (onChanged) {
+                    onChanged(true)
                 }
             }
+            // if (charc.properties.read) {
+            //     var chara = await this.bleManager.readCharacteristicValue(charc.uuid,)
+            // }
+        }
 
 
-            // this.setData({ characteristics: { ...this.data.characteristics, [service.uuid]: characteristics } });
+        // this.setData({ characteristics: { ...this.data.characteristics, [service.uuid]: characteristics } });
 
-        } catch (error) {
-            console.error(error);
-        }
     }
 
     async disconnect(data) {
@@ -173,11 +179,15 @@ class BtHelper {
         // await this._helper.dispose();
     }
 
-    async send(cmd, type) {
+    async sendNow(cmd) {
         if (cmd) {
-            // this.queueManager.addTask(cmd)
             this.bleManager.sendData(cmd)
         }
+    }
+    async send(cmd, type) {
+        if (cmd) {
+            this.queueManager.addTask(cmd)
+        }
         // QueueManager.instance.addTask({ task: cmd });
     }
 
@@ -238,7 +248,7 @@ class BtHelper {
 
     async checkDevice() {
         // console.log("校验设备:", BtCmd); // 输出: EarPhone Info
-        this.send(BtCmd.checkDevice());
+        this.sendNow(BtCmd.checkDevice());
         //2.0有发这个,不知道是啥
         this.send([0x54, 0x44, 0x44, 0x48, 0x01, 0x09, 0x26, 0x01, 0x01]);
     }

BIN
images/common/icon_add.png


+ 5 - 7
pages/connectBle/connectBle.js

@@ -119,11 +119,8 @@ Page({
         var connectDevice = _this.data.connectDevice;
         getCurrentPages()[0].addConnectBlueDevice(connectDevice);
         setTimeout(() => {
-          // wx.redirectTo({
-          //   url: '/pages/index/index'
-          // });
-          wx.navigateTo({
-            url: '/pages/deviceDetail/detail'
+          wx.redirectTo({
+            url: '/pages/index/index'
           });
         }, 200);
       }
@@ -172,8 +169,8 @@ Page({
     if (
       // deviceId.includes("D8:24:07:89:31") ||
       // // 2axk
-      // element.deviceId.includes("F5:A5:43:70:C8:F1") ||
-      // // 黑色2x
+      element.deviceId.includes("F5:A5:43:70:C8:F1") ||
+      // 黑色2x
       // element.deviceId.includes("F6:61:D8:24:E5:98")
       // /// mac: D7:92:84:87:09:7D
       // ||
@@ -182,6 +179,7 @@ Page({
       deviceId.includes("00:33:52:A7:3E:D0")
     ) {
       if (element.connectable == true) {
+        console.log("找到设备ble", element);
         _this.data.connectDevice.mac = element.mac
         _this.data.connectDevice.deviceId = element.deviceId
         _this.data.btHelper.stopSearch()

+ 4 - 4
pages/deviceDetail/detail.js

@@ -1,6 +1,6 @@
 // pages/deviceDetail/detail.js
 
-const BtHelper = require('../../devices/bt_helper');
+const BtHelper = require('../../devices/bt_helper')
 const { deviceVersion } = require('../../request/deviceListRequest')
 import { CmdEvent } from '../../devices/cmd_key_event';
 import EventManager from '../../utils/event_bus'
@@ -14,7 +14,7 @@ Page({
     },
     hasNew: true,
     // 页面数据
-    device: getApp().globalData.device,
+    device: {},
     btHelper: null,
     updateData: {},
   },
@@ -51,8 +51,8 @@ Page({
   /**
    * 生命周期函数--监听页面加载
    */
-  onLoad: function () {
-    let device = getApp().globalData.device ?? {};
+  onLoad: function (options) {
+    let device = JSON.parse(options.param);
     let isConnect = device.state === 'online'
     if (!isConnect) {
       // btHelper

+ 68 - 11
pages/piano/wallpaper/wallpaper.js

@@ -1,31 +1,42 @@
 // pages/piano/wallpaper/wallpaper.js
+const { deviceWallPaper } = require('../../../request/deviceListRequest')
+
 Page({
 
   /**
    * 页面的初始数据
    */
   data: {
-    topImg: "https://inews.gtimg.com/om_bt/OAVMydtx9BsJxf5i_thi4Oll9sR1px-Esmtv6UHSxoisEAA/641",
+    topImg: {
+      pic: "wxfile://tmp_d3e57489ead39c698676ff860df9cb8a37f66ee1a4777dbb.jpg",
+      id: "",
+      name: ""
+    },
     imageList: [
       {
-        img: "https://inews.gtimg.com/om_bt/OAVMydtx9BsJxf5i_thi4Oll9sR1px-Esmtv6UHSxoisEAA/641",
-        imgId: ""
+        pic: "https://inews.gtimg.com/om_bt/OAVMydtx9BsJxf5i_thi4Oll9sR1px-Esmtv6UHSxoisEAA/641",
+        id: "",
+        name: ""
       },
       {
-        img: "https://inews.gtimg.com/om_bt/OAVMydtx9BsJxf5i_thi4Oll9sR1px-Esmtv6UHSxoisEAA/641",
-        imgId: ""
+        pic: "https://inews.gtimg.com/om_bt/OAVMydtx9BsJxf5i_thi4Oll9sR1px-Esmtv6UHSxoisEAA/641",
+        id: "",
+        name: ""
       },
       {
-        img: "https://inews.gtimg.com/om_bt/OAVMydtx9BsJxf5i_thi4Oll9sR1px-Esmtv6UHSxoisEAA/641",
-        imgId: ""
+        pic: "https://inews.gtimg.com/om_bt/OAVMydtx9BsJxf5i_thi4Oll9sR1px-Esmtv6UHSxoisEAA/641",
+        id: "",
+        name: ""
       },
       {
-        img: "https://inews.gtimg.com/om_bt/OAVMydtx9BsJxf5i_thi4Oll9sR1px-Esmtv6UHSxoisEAA/641",
-        imgId: ""
+        pic: "https://inews.gtimg.com/om_bt/OAVMydtx9BsJxf5i_thi4Oll9sR1px-Esmtv6UHSxoisEAA/641",
+        id: "",
+        name: ""
       },
       {
-        img: "https://inews.gtimg.com/om_bt/OAVMydtx9BsJxf5i_thi4Oll9sR1px-Esmtv6UHSxoisEAA/641",
-        imgId: ""
+        pic: "https://inews.gtimg.com/om_bt/OAVMydtx9BsJxf5i_thi4Oll9sR1px-Esmtv6UHSxoisEAA/641",
+        id: "",
+        name: ""
       }
     ],
     selectIndex: 0,
@@ -33,6 +44,52 @@ Page({
       showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
       title: '壁纸设置', //导航栏 中间的标题
     },
+  }, footerTap() {
+    const that = this;
+    wx.chooseImage({
+      count: 1, // 最多可以选择的图片张数
+      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
+      sourceType: ['album'], // 从相册选择
+      success: function (res) {
+        const tempFilePaths = res.tempFilePaths;
+        console.log('获取图片信息成功', res);
+
+        wx.getImageInfo({
+          src: tempFilePaths[0],
+          success: function (imageInfo) {
+            console.log(imageInfo);
+            wx.navigateTo({
+              url: `/pages/crop/crop?path=${tempFilePaths[0]}&width=${imageInfo.width}&height=${imageInfo.height}`
+            });
+          },
+          fail: function (err) {
+            console.error('获取图片信息失败', err);
+          }
+        });
+      },
+      fail: function (err) {
+        console.error('选择图片失败', err);
+      }
+    });
+  }, imageTap(e) {
+    console.log(e)
+    // wxfile://tmp_d3e57489ead39c698676ff860df9cb8a37f66ee1a4777dbb.jpg
+    let image = e.currentTarget.dataset.image;
+    _this.setData({
+      topImg: image ?? {}
+    })
+  }, wallpaperList() {
+    let _this = this;
+    deviceWallPaper().then(res => {
+      console.log("壁纸列表", res);
+      topImg = res.data[0];
+
+      _this.setData({
+        imageList: res.data ?? [],
+        topImg: res.data[0] ?? {}
+      })
+
+    })
   },
 
   /**

+ 7 - 3
pages/piano/wallpaper/wallpaper.wxml

@@ -2,12 +2,16 @@
 <view class="container">
     <nav-bar bind:goBack="_goBack" nav-bgc-class="ex-nav-bgc-class" nav-title-class="ex-nav-title-class" ex-back-pre="ex-back-pre" navbar-data='{{nvabarData}}'></nav-bar>
     <view class="img_section">
-        <image src="{{topImg}}" class="select_img"></image>
-        <image src="../../images/common/icon_icon.png" class="select_icon"></image>
+        <image src="{{topImg.pic}}" class="select_img"></image>
+        <image src="../../../images/common/icon_sel.png" class="select_icon"></image>
     </view>
     <view class="grid-container">
         <block wx:for="{{imageList}}" wx:key="index" wx:for-item="item" wx:for-index="index">
-            <image class="device_img" src="{{item.img}}" mode="aspectFill" bind:tap="deviceTap" data-device="{{item}}" />
+            <image class="device_img" src="{{item.pic}}" mode="aspectFill" bind:tap="deviceTap" data-image="{{item}}" />
         </block>
     </view>
+    <view class="footer" bind:tap="footerTap">
+        <image src="../../../images/common/icon_add.png" mode="aspectFill" />
+        <view>上传图片</view>
+    </view>
 </view>

+ 36 - 9
pages/piano/wallpaper/wallpaper.wxss

@@ -1,19 +1,19 @@
 /* pages/piano/wallpaper/wallpaper.wxss */
 
 .img_section {
-    width: 100%;
-    height: 400rpx;
+    margin-top: 32rpx;
+    margin-left: 108rpx;
+    width: 533rpx;
     position: relative;
     align-items: center;
     justify-content: center;
 }
 
 .select_img {
-    margin-top: 32rpx;
-    padding-inline: 108rpx;
     width: 533rpx;
     height: 400rpx;
     border-radius: 16rpx;
+    overflow: hidden;
 }
 
 .select_icon {
@@ -27,17 +27,44 @@
 
 
 .grid-container {
+    margin-top: 32rpx;
     display: grid;
     grid-template-columns: repeat(2, 1fr);
-    grid-column-gap: 32rpx;
-    /* grid-row: 32rpx; */
-    padding-inline: 32rpx;
-    padding-top: 16rpx;
+    grid-gap: 32rpx;
+    margin-inline: 32rpx;
 }
 
-.cell {
+.device_img {
     width: 320rpx;
     height: 240rpx;
     border-radius: 16rpx;
+}
+
+
+.footer {
+    position: fixed;
+    bottom: 0;
+    left: 0;
+    right: 0;
+    display: flex;
+    width: 750rpx;
+    height: 170rpx;
+    background: #FFFFFF;
+    box-shadow: 0rpx -4rpx 16rpx 0rpx #E8EAEB;
+    border-radius: 72rpx 72rpx 0rpx 0rpx;
+    justify-content: center;
+    align-items: center;
+}
+
+.footer>image {
+    margin-right: 8rpx;
+    width: 48rpx;
+    height: 48rpx;
+}
 
+.footer>view {
+    color: #6546A3;
+    width: 128rpx;
+    font-weight: Medium;
+    font-size: 32rpx;
 }

+ 8 - 0
request/deviceListRequest.js

@@ -23,5 +23,13 @@ module.exports = {
       version: version,
     }, "GET",)
   },
+  // 壁纸列表
+  deviceWallPaper: () => {
+    return request("/mini/wx/config/wallpaperList", {
+      clientType: clientType,
+      type: type,
+      version: version,
+    }, "GET",)
+  },
 
 }