Quellcode durchsuchen

feat: 状态同步
同步获取设备灯光状态

kindring vor 1 Jahr
Ursprung
Commit
7fb51b0e27
7 geänderte Dateien mit 143 neuen und 31 gelöschten Zeilen
  1. 2 2
      app.json
  2. 1 1
      pages/index/index.js
  3. 119 18
      pages/light/light.js
  4. 1 2
      pages/old/index.js
  5. 2 2
      utils/handle.js
  6. 7 2
      utils/light_cmd.js
  7. 11 4
      utils/mjs_wxble.js

+ 2 - 2
app.json

@@ -1,7 +1,7 @@
 {
   "pages":[
     "pages/index/index",
-    "pages/camera/camera",
+    "pages/old/index",
     "pages/light/light",
     "pages/ble/ble"
   ],
@@ -13,7 +13,7 @@
             "selectedIconPath": "icons/png/index-active.png"
         },
         {
-          "pagePath": "pages/camera/camera",
+          "pagePath": "pages/old/index",
           "iconPath": "icons/png/cam.png",
           "selectedIconPath": "icons/png/cam-active.png"
         },

+ 1 - 1
pages/index/index.js

@@ -22,7 +22,7 @@ Page({
         break;
       case 'camera':
           wx.switchTab({
-            url: '/pages/camera/camera',
+            url: '/pages/old/index',
           })
           break;
       case 'bleTool':

+ 119 - 18
pages/light/light.js

@@ -53,6 +53,7 @@ Page({
             fullOpen: false,
             // 亮度
             brightness: 50,
+            fan: 0,
             // 色温
             colorTemperature: MAX_COLOR_TEMPERATURE,
             // 色彩
@@ -144,17 +145,17 @@ Page({
         lamp: {...this.data.lamp, fullOpen: flag}
       })
 
-      let fn;
+      
       let nextMode = light_cmd.light_worker_mode.DEFAULT;
       if(flag){
         nextMode = light_cmd.light_worker_mode.FULL;
       }
 
-      fn = this.excuteBleSend(
+      let fn = this.excuteBleSend(
         light_cmd.set_mode(nextMode)
       );
       console.log(fn);
-      [_err,_res] = await handle(fn());
+      let [_err,_res] = await handle(fn);
       if(_err){
         return this.bleFail('发送失败', _err.errMsg);
       }
@@ -218,6 +219,100 @@ Page({
         }
         console.log(`开关命令发送成功,开关:${this.data.lamp.switch}`);
     },
+    registerLightCmdAction(){
+      // 灯光信息响应
+      light_cmd.registerHandle(light_cmd.light_cmd_code.CMD_QUERY, (data)=>{
+        console.log('灯光状态响应');
+        data = data.join('');
+        console.log(data);
+        // 灯光数据 开关状态 模式 亮度 色温 风扇速度
+        let switchState = data.slice(0, 2);
+        let mode = data.slice(2, 4);
+        // 亮度 占1位
+        let light = data.slice(4, 6);
+        // 色温 占2位
+        let temp = data.slice(6, 10);
+        // 风扇速度 占1位
+        let fan = data.slice(10, 12);
+        light = parseInt(light, 16);
+        temp = parseInt(temp, 16);
+        fan = parseInt(fan, 16);
+        switchState = switchState === '01';
+        let infoStr = `设备:${this.data.ble.deviceId} `
+        infoStr += `开关:${switchState ? '开':'关'} `
+        infoStr += `模式:${mode} `
+        infoStr += `色温:${temp} `
+        infoStr += `亮度:${light} `
+        infoStr += `风扇:${fan} `
+        console.log(infoStr);
+        this.setData({
+          lamp: {...this.data.lamp, 
+            switch: switchState,
+            fullOpen:  mode === light_cmd.light_worker_mode.FULL,
+            colorTemperature: temp,
+            brightness: light,
+            fan: fan
+          }
+        });
+        this.reComputeBgColor();
+      });
+      // 色温响应
+      light_cmd.registerHandle(light_cmd.light_cmd_code.CMD_TEMP, (data)=>{
+        console.log('色温调节响应');
+        data = data.join('');
+        console.log(data);
+        // hex转10进制
+        let temp = parseInt(data, 16);
+        console.log(temp);
+        this.setData({
+          lamp: {...this.data.lamp, colorTemperature: temp}
+        })
+        this.reComputeBgColor();
+      });
+      // 亮度响应
+      light_cmd.registerHandle(light_cmd.light_cmd_code.CMD_LIGTH, (data)=>{
+        console.log('亮度调节响应');
+        data = data.join('');
+        // hex转10进制
+        let light = parseInt(data, 16);
+        console.log(light);
+        this.setData({
+          lamp: {...this.data.lamp, brightness: light}
+        })
+        this.reComputeBgColor();
+      });
+      // 开灯响应
+      light_cmd.registerHandle(light_cmd.light_cmd_code.CMD_OPEN, (data)=>{
+        console.log('开灯调节响应');
+        data = data.join('');
+        console.log(data);
+        this.setData({
+          lamp: {...this.data.lamp, switch: true}
+        });
+        this.reComputeBgColor();
+      });
+      // 关灯响应
+      light_cmd.registerHandle(light_cmd.light_cmd_code.CMD_CLOSE, (data)=>{
+        console.log('关灯调节响应');
+        data = data.join('');
+        console.log(data);
+        this.setData({
+          lamp: {...this.data.lamp, switch: false}
+        });
+        this.reComputeBgColor();
+      });
+      // 模式切换响应
+      light_cmd.registerHandle(light_cmd.light_cmd_code.CMD_MODE, (data)=>{
+        console.log('模式调节响应');
+        data = data.join('');
+        console.log(data);
+        let mode = parseInt(data, 16);
+        this.setData({
+          lamp: {...this.data.lamp, fullOpen: mode === light_cmd.light_worker_mode.FULL}
+        });
+        this.reComputeBgColor();
+      });
+    },
 
     // 计算背景色
     reComputeBgColor(){
@@ -248,20 +343,6 @@ Page({
       });
       bleServices = [];
       // 灯光信息初始化
-      this.setData({
-        lamp: {
-          // 开关状态
-          switch: false,
-          // 全开状态(无色温调节)
-          fullOpen: false,
-          // 亮度
-          brightness: 50,
-          // 色温
-          colorTemperature: MAX_COLOR_TEMPERATURE,
-          // 色彩
-          bgc: "#3b3b3b"
-        }
-      })
     },
     // 连接失败
     bleFail(title,msg){
@@ -288,6 +369,7 @@ Page({
       if(err){
         return this.bleFail('断开连接失败', err.errMsg);
       }
+      light_cmd.clearHandle();
       wx.showToast({title: `断开连接成功`, })
       this.initPageInfo();
     },
@@ -357,14 +439,33 @@ Page({
       });
       // 设备服务管理
       this.connectServices(dev.deviceId);
+      // 注册监听函数
+      ble.registerListenerHandle(this.onNotifyHandle);
+      
+  },
+  onNotifyHandle(data){
+    console.log(`收到数据:${data}`);
+    // 解析数据
+    light_cmd.parseCmdData(data);
   },
-
   async connectServices(deviceId){
       // uuid FF01
       let [err,res] = await handle(ble.getBleServices(deviceId));
       if(err){ return this.bleFail('获取服务失败', err.errMsg);}
       // 保存服务信息;
       bleServices = res;
+
+      // 注册灯光响应函数
+      this.registerLightCmdAction();
+      // 查询灯光状态
+      let fn = this.excuteBleSend(
+        light_cmd.query_light()
+      );
+      [_err,_res] = await handle(fn);
+      if(_err){
+        console.log(_err);
+        return this.bleFail('发送失败', _err.errMsg);
+      }
   },
 
 

+ 1 - 2
pages/old/index.js

@@ -1,12 +1,11 @@
 // index.js
 // 获取应用实例
 const getBle =  require("../../utils/ble");
-import { handle } from '../../utils/handle.js';
+const handle = require('../../utils/handle.js');
 const sleep = require("../../utils/sleep");
 const TimeClock = require('../../utils/timer');
 const app = getApp();
 const ble = getBle();
-
 let services = [];
 const timeClock = new TimeClock();
 const connectStateMap = {

+ 2 - 2
utils/handle.js

@@ -6,7 +6,7 @@
  * @LastEditTime: 2021-12-14 17:17:09
  * @LastDescript: 
  */
-export function handle(promise) {
+function handle(promise) {
   return new Promise(resolve => {
       try{
           promise.then(val => {
@@ -20,4 +20,4 @@ export function handle(promise) {
   })
 }
 
-
+module.exports = handle;

+ 7 - 2
utils/light_cmd.js

@@ -68,7 +68,7 @@ export const light_worker_mode = {
     FULL: 1
 }
 
-const CMD_ACTION = {
+let CMD_ACTION = {
 }
 
 
@@ -264,6 +264,10 @@ export function registerHandle(cmd, handle){
     CMD_ACTION[cmd] = handle;
 }
 
+export function clearHandle(){
+    CMD_ACTION = {};
+}
+
 
 export default {
     light_worker_mode,
@@ -278,5 +282,6 @@ export default {
     set_temp,
     set_fun,
     set_mode,
-    registerHandle
+    registerHandle,
+    clearHandle
 }

+ 11 - 4
utils/mjs_wxble.js

@@ -6,6 +6,7 @@ class BLE{
   whiteList = [];
   nameList = [];
   useWhiteList = false;
+  listenHandle = null;
   constructor(whiteList = [], nameList = []){
     // this代表实例对象
     this.isInit= false;
@@ -237,14 +238,17 @@ class BLE{
         serviceId: serviceId,
         //特征值ID
         characteristicId: uuid,
-        success: function (res) {
+        success: (res) => {
           //开启通知成功
-          wx.onBLECharacteristicValueChange(function (res) {
+          wx.onBLECharacteristicValueChange((_res)=> {
             //这里坐等数据过来,res.value
             console.log('got data ');
-            console.log(res);
-            console.log(buf2hex(res.value));
+            console.log(_res);
+            console.log(buf2hex(_res.value));
             console.log('got data ');
+            if(listenHandle){
+              listenHandle(buf2hex(_res.value));
+            }
           })
           resolve(res);
         },
@@ -255,6 +259,9 @@ class BLE{
       });
     })
   }
+  registerListenerHandle(cb){
+    listenHandle = cb;
+  }
   // 断开与设备的连接
   disconnectDev(devId){
     return new Promise((resolve,reject)=>{