Browse Source

feature:增加设置wifi的demo功能

zeng.chen 6 months ago
parent
commit
24ec1eb07a

+ 1 - 1
app.js

@@ -12,7 +12,7 @@ const iosHost = 'wxs://mqtt.test.radio1964.com:8884';
 const androidHost = 'wxs://mqtt.ssl.keepradioon.net:8884'
 App({
   globalData: {
-    isRelease: true,
+    isRelease: false,
     is_debug: 2, // 1 测试环境 // 2正式环境
     baseUrl: env.prod.baseUrl,
 

+ 2 - 1
app.json

@@ -19,7 +19,8 @@
     "pages/deviceDetail/detail",
     "pages/piano/wallpaper/wallpaper",
     "pages/ota/ota",
-    "pages/piano/cropper/cropper"
+    "pages/piano/cropper/cropper",
+    "pages/setWifi/setWifi"
   ],
   "window": {
     "backgroundTextStyle": "light",

+ 3 - 3
devices/bluetooth/bt_cmd.js

@@ -252,9 +252,9 @@ class BtCmd {
         let cmd = this._build(CmdBase.wallPaperData, uint8Array, true, false);
         return cmd;
     }
-    // 背景图指令 1开始, 0结束
-    static backgroudImg(value) {
-        return this._build(CmdBase.heijiaoBackImg, [value]);
+    // 设置wifi
+    static otaSetWifi(value) {
+        return this._build(CmdBase.heijiaoBackImg, value, true, false);
     }   // 背景图指令
     static backgroudImgData(value) {
         return this._build(CmdBase.heijiaoBackImgData, [value]);

+ 2 - 2
devices/bluetooth/bt_parse.js

@@ -499,10 +499,10 @@ class BtParse {
 
                 break;
             case CmdBase.heijiaoBackImg:
-                // [54:44:44:48:01:0A:74:01:01:00]
+                // 0x76
                 {
                     let kind = cmd[9]
-                    EventManager.fire(CmdEvent.otaUrl({ value: value, kind: kind }));
+                    EventManager.fire(CmdEvent.otaWifi({ value: value, kind: kind }));
 
                 }
 

+ 3 - 3
devices/bt_helper.js

@@ -757,10 +757,10 @@ class BtHelper {
 
   }
 
-  // 背景图指令
-  backgroudImg(value) {
+  // wifi指令
+  otaSetWifi(value) {
     var that = this;
-    that.send(BtCmd.backgroudImg(value));
+    that.send(BtCmd.otaSetWifi(value));
   } // 背景图指令
   backgroudImgData(value) {
     var that = this;

+ 7 - 0
devices/cmd_key_event.js

@@ -48,6 +48,7 @@ const EnumCmdEvent = {
     btMac: 'btMac',
     otaCmd: 'otaCmd',
     otaUrl: 'otaUrl',
+    otaWifi: 'otaWifi',
     wallpaper: 'wallpaper'
 };
 
@@ -346,6 +347,12 @@ class CmdEvent {
         event.heiJiaoKind = kind;
         return event;
     }
+    static otaWifi({ value, kind }) {
+        const event = new CmdEvent({ cmdEvent: EnumCmdEvent.otaWifi });
+        event.otaUrl = value;
+        event.heiJiaoKind = kind;
+        return event;
+    }
     static wallpaper({ value, kind }) {
         const event = new CmdEvent({ cmdEvent: EnumCmdEvent.wallpaper });
         console.log("wallpaper:", value, kind)

+ 8 - 1
pages/deviceDetail/detail.js

@@ -47,6 +47,13 @@ Page({
   },
 
   goToOta: function () {
+    if (this.data.updateData.hasNewVersion != 1) {
+      wx.showToast({
+        title: '当前已是最新版本',
+        icon: 'none'
+      })
+      return
+    }
     let param = {
       "device": this.data.device,
       "otaData": this.data.updateData,
@@ -116,7 +123,7 @@ Page({
     if (isShowOta) {
       btHelper.getVersion()
     }
-    btHelper.getPauseSleep()
+    // btHelper.getPauseSleep()
 
     let _this = this;
     EventManager.addNotification(CmdEvent.eventName, function (event) {

+ 54 - 21
pages/ota/ota.js

@@ -2,6 +2,8 @@ const { BtHelper } = require("../../devices/bt_helper");
 import { EnumCmdEvent, CmdEvent } from '../../devices/cmd_key_event';
 import EventManager from '../../utils/event_bus'
 import route_util from '../../utils/route_util';
+import route_constant from '../../utils/route_constant.js';
+
 // pages/OTA/ota.js
 Page({
 
@@ -85,7 +87,8 @@ Page({
       })
       return;
     }
-    if (this.data.otaStatus != 1 || this.data.otaStatus != 4) {
+    if (this.data.otaStatus != 0) {
+      console.log("没有可升级的固件2")
       return;
     }
 
@@ -101,6 +104,9 @@ Page({
         }
 
         if (res.confirm) {
+          wx.showLoading({
+            title: '升级设备中,请稍后',
+          })
           _this.sendOtaCmd(1)
 
           // _this.startDownloadFile()
@@ -113,7 +119,7 @@ Page({
 
   },
   goToWifi() {
-    route_util.jump("pages/setWifi/setWifi")
+    route_util.jump(route_constant.setWifi)
   },
   sendUrlData() {
     let url = this.data.otaData.url ?? ""
@@ -329,7 +335,7 @@ Page({
     if (value == 0) {
       this.setOtaStatus(0);
     } else {
-      this.setOtaStatus(4);
+      // this.setOtaStatus(4);
     }
   },
 
@@ -363,6 +369,8 @@ Page({
       let name = event.cmdEvent;
       console.log("OTA页0:", event)
       let kind = event.heiJiaoKind;
+      // 74,1,1是wifi连接成功,74,0 2连接wifi失败/没有wifi。   
+      // 发送url给你后,你回:74 0 1是流程成功,74 0 3是流程失败。
 
       switch (name) {
         case EnumCmdEvent.otaCmd:
@@ -372,30 +380,42 @@ Page({
           if (otaCmd === 1 && kind == 1) {
             wx.hideLoading();
             // 设备收到开启OTA的回复,发送url
+            // todo 测试 去设置wifi界面
             _this.sendUrlData()
-          } else if (otaCmd === 2 && kind == 1) {
+          } else if (otaCmd === 0 && kind == 2) {
             wx.hideLoading();
             // 去设置wifi界面
             _this.goToWifi()
-          }
-          else if (otaCmd === 0 && kind == 1) {
-            // 设备回收到url,OTA结束了
-            // _this.sendOtaCmd(0)
-            var pages = getCurrentPages();
-            var length = pages.length;
-            var currentPage = pages[length - 1];
-            if (currentPage.route == 'pages/setWifi/setWifi') {
-              wx.navigateBack({
-                delta: 1
-              })
-            }
-
-            wx.hideLoading()
+          } else if (otaCmd === 0 && kind == 3) {
+            wx.hideLoading();
+            // 流程失败
             wx.showModal({
-              title: '等待设备升级中',
-              showCancel: false
+              title: '设备升级失败了',
+              showCancel: false,
+              success: function (res) {
+                if (res.confirm) {
+                  wx.navigateBack({
+                    delta: 2
+                  })
+                }
+              }
             })
-          } else if (kind == 0) {
+          } else if (otaCmd === 0 && kind == 1) {
+            wx.hideLoading();
+            // 流程成功
+            wx.showModal({
+              title: '设备开始升级中',
+              showCancel: false,
+              success: function (res) {
+                if (res.confirm) {
+                  wx.navigateBack({
+                    delta: 2
+                  })
+                }
+              }
+            })
+          }
+          else if (kind == 0) {
             wx.hideLoading()
             wx.showModal({
               title: 'WIFI连接失败了',
@@ -417,6 +437,19 @@ Page({
             })
           }
           break;
+        case EnumCmdEvent.otaWifi:
+          // 0x76, 发送wifi成功
+          if (value === 1) {
+            _this.sendUrlData()
+          } else {
+            wx.hideLoading()
+            // wifi失败
+            wx.showModal({
+              title: 'OTA升级失败了',
+              showCancel: false
+            })
+          }
+          break;
       }
     }, _this)
   },

+ 50 - 71
pages/setWifi/setWifi.js

@@ -1,6 +1,7 @@
 // pages/setWifi/setWifi.js
 const { BtHelper } = require("../../devices/bt_helper");
-
+import EventManager from '../../utils/event_bus'
+import { EnumCmdEvent, CmdEvent } from '../../devices/cmd_key_event';
 Page({
   data: {
     wifiName: '',
@@ -10,22 +11,32 @@ Page({
 
   getConnectedWifi: function () {
     const that = this;
-    wx.getConnectedWifi({
-      success: function (res) {
-        const wifiName = res.wifi.SSID;
-        that.setData({
-          wifiName: wifiName
+
+    wx.startWifi({
+      success(res) {
+        console.log(res.errMsg)
+
+        wx.getConnectedWifi({
+          success: function (res) {
+            const wifiName = res.wifi.SSID;
+            that.setData({
+              wifiName: wifiName
+            });
+          },
+          fail: function (err) {
+            console.error('获取Wi-Fi信息失败', err);
+            // wx.showToast({
+            //   title: '获取Wi-Fi信息失败',
+            //   icon: 'none'
+            // });
+
+          }
         });
-      },
-      fail: function (err) {
-        console.error('获取Wi-Fi信息失败', err);
-        // wx.showToast({
-        //   title: '获取Wi-Fi信息失败',
-        //   icon: 'none'
-        // });
 
       }
-    });
+    })
+
+
   },
 
   onWifiNameInput: function (e) {
@@ -42,9 +53,16 @@ Page({
   // string转换为List<int>
   string2ListInt(text) {
     let code = Array.from(text).map(char => char.charCodeAt(0));
-    console.log("string转换为List<int>", code)
+    console.log("string转换为List<int>", text, code)
     return code
   },
+  _int2Hex(num) {
+    let value = num ?? 0;
+    let hexStr = value.toString(16).toUpperCase();
+    hexStr = hexStr.padStart(2, '0');
+    let hexNum = parseInt(hexStr, 16);
+    return hexNum;
+  },
   sendWiFiInfo(wifiName, pwd) {
     // [0x22, (wifiList.length + pwdList.length + 6), 0x33, (wifiList.length), (wifiList), 0x44, (pwdList)];
 
@@ -63,14 +81,15 @@ Page({
 
     // 数字*3 +
     let pwdList = this.string2ListInt(pwd);
+    console.log("wifiList", wifiList, "pwdList", pwdList)
 
     // 16进制
     result.push(0x22);
-    result.push(this.int2Hex(wifiList.length + pwdList.length + 6));
+    result.push(this._int2Hex(wifiList.length + pwdList.length + 6));
 
     // 账号
     result.push(0x33);
-    result.push(this.int2Hex(wifiList.length));
+    result.push(this._int2Hex(wifiList.length));
 
     let p = result[3] + 4;
     let j = 0;
@@ -80,17 +99,17 @@ Page({
 
     // 密码
     result.splice(p, 0, 0x44);
-    result.splice(++p, 0, this.int2Hex(pwdList.length));
+    result.splice(++p, 0, this._int2Hex(pwdList.length));
     p++;
     j = 0;
-    // for (let i = p; i < p + pwdList.length; i++) {
-    //   result.splice(i, 0, pwdList[j++]);
-    // }
-    result.push(...pwdList)
+    for (let i = p; i < p + pwdList.length; i++) {
+      result.splice(i, 0, pwdList[j++]);
+    }
+    // result.push(...pwdList)
 
     console.log("发送wifi账号密码:", result.toString());
     // _ble.send({ cmd: result });
-    BtHelper.getInstance().send(result)
+    BtHelper.getInstance().otaSetWifi(result)
   },
 
   onConfirm: function () {
@@ -109,58 +128,18 @@ Page({
     let _this = this;
     EventManager.addNotification(CmdEvent.eventName, function (event) {
       let name = event.cmdEvent;
-      console.log("OTA页0:", event)
+      console.log("设置wifi0:", event)
       let kind = event.heiJiaoKind;
-
-      switch (name) {
-        case EnumCmdEvent.otaCmd:
-          let otaCmd = event.otaCmd;
-          console.log("OTA页:", otaCmd, kind)
-
-          if (otaCmd === 1 && kind == 1) {
-            wx.hideLoading();
-            // 设备收到开启OTA的回复,发送url
-            _this.sendUrlData()
-          } else if (otaCmd === 2 && kind == 1) {
-            wx.hideLoading();
-            // 去设置wifi界面
-            _this.goToWifi()
-          }
-          else if (otaCmd === 0 && kind == 1) {
-            // 设备回收到url,OTA结束了
-            // _this.sendOtaCmd(0)
-            wx.hideLoading()
-            wx.showModal({
-              title: '等待设备升级中',
-              showCancel: false
-            })
-          } else if (kind == 0) {
-            wx.hideLoading()
-            wx.showModal({
-              title: 'WIFI连接失败了',
-              showCancel: false
-            })
-          }
-          break;
-        case EnumCmdEvent.otaUrl:
-          let otaUrl = event.otaUrl;
-          if (otaUrl === 1) {
-            // 开始发送url
-            BtHelper.getInstance().otaUrl(_this.string2ListInt(_this.data.otaData.url))
-          } else {
-            wx.hideLoading()
-            // wifi失败
-            wx.showModal({
-              title: 'OTA升级失败了',
-              showCancel: false
-            })
-          }
-          break;
-      }
+      console.log("设置wifi:", name, otaCmd, kind)
+      let toast = "设置wifi:" + name + " value," + otaCmd + "kind ," + kind
+      wx.showToast({
+        title: toast,
+        icon: 'none'
+      })
     }, _this)
   },
   onLoad: function (options) {
-    let param = options.param;
+    let param = options.param ?? "{}";
     let url = JSON.parse(param).url ?? "";
     this.data._otaUrl = url;
     this.getConnectedWifi();

+ 3 - 1
pages/setWifi/setWifi.wxss

@@ -2,6 +2,7 @@
 /* pages/setWifi/setWifi.wxss */
 .container {
     padding: 20px;
+    margin-top: 88rpx;
 }
 
 .input-group {
@@ -10,7 +11,7 @@
 
 .label {
     display: block;
-    margin-bottom: 5px;
+    height: 88rpx;
 }
 
 .input {
@@ -18,6 +19,7 @@
     padding: 10px;
     width: 100%;
     box-sizing: border-box;
+    height: 88rpx;
 }
 
 .confirm-button {

+ 3 - 0
utils/route_constant.js

@@ -11,6 +11,8 @@ const deviceConnect0 = "../deviceConnect0/deviceConnect0";
 const deviceConnect1 = "../deviceConnect1/deviceConnect1";
 const deviceConnect2 = "../deviceConnect2/deviceConnect2";
 
+const setWifi = "../setWifi/setWifi";
+
 /// 抽奖记录
 const deviceList = "../deviceList/deviceList";
 const connectMode = "../deviceConMode/index";
@@ -32,4 +34,5 @@ module.exports = {
   deviceList: deviceList,
   connectMode: connectMode,
   connectBle: connectBle,
+  setWifi: setWifi,
 }