فهرست منبع

feature:新增OTA的wifi界面

zeng.chen 6 ماه پیش
والد
کامیت
7d0a392487
4فایلهای تغییر یافته به همراه217 افزوده شده و 0 حذف شده
  1. 172 0
      pages/setWifi/setWifi.js
  2. 3 0
      pages/setWifi/setWifi.json
  3. 12 0
      pages/setWifi/setWifi.wxml
  4. 30 0
      pages/setWifi/setWifi.wxss

+ 172 - 0
pages/setWifi/setWifi.js

@@ -0,0 +1,172 @@
+// pages/setWifi/setWifi.js
+const { BtHelper } = require("../../devices/bt_helper");
+
+Page({
+  data: {
+    wifiName: '',
+    wifiPassword: '',
+    _otaUrl: "",
+  },
+
+  getConnectedWifi: function () {
+    const that = this;
+    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'
+        // });
+
+      }
+    });
+  },
+
+  onWifiNameInput: function (e) {
+    this.setData({
+      wifiName: e.detail.value
+    });
+  },
+
+  onWifiPasswordInput: function (e) {
+    this.setData({
+      wifiPassword: e.detail.value
+    });
+  },
+  // string转换为List<int>
+  string2ListInt(text) {
+    let code = Array.from(text).map(char => char.charCodeAt(0));
+    console.log("string转换为List<int>", code)
+    return code
+  },
+  sendWiFiInfo(wifiName, pwd) {
+    // [0x22, (wifiList.length + pwdList.length + 6), 0x33, (wifiList.length), (wifiList), 0x44, (pwdList)];
+
+    if (!wifiName || !pwd) {
+      wx.showToast({
+        title: '请输入正确的账号密码',
+      })
+      wx.hideLoading()
+      return;
+    }
+
+    let result = [];
+
+    // 字母*6 +
+    let wifiList = this.string2ListInt(wifiName);
+
+    // 数字*3 +
+    let pwdList = this.string2ListInt(pwd);
+
+    // 16进制
+    result.push(0x22);
+    result.push(this.int2Hex(wifiList.length + pwdList.length + 6));
+
+    // 账号
+    result.push(0x33);
+    result.push(this.int2Hex(wifiList.length));
+
+    let p = result[3] + 4;
+    let j = 0;
+    for (let i = 4; i < p; i++) {
+      result.splice(i, 0, wifiList[j++]);
+    }
+
+    // 密码
+    result.splice(p, 0, 0x44);
+    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)
+
+    console.log("发送wifi账号密码:", result.toString());
+    // _ble.send({ cmd: result });
+    BtHelper.getInstance().send(result)
+  },
+
+  onConfirm: function () {
+    const { wifiName, wifiPassword } = this.data;
+    if (!wifiName || !wifiPassword) {
+      wx.showToast({
+        title: '请输入完整的Wi-Fi信息',
+        icon: 'none'
+      });
+      return;
+    }
+    // 这里可以添加进一步的逻辑,比如连接Wi-Fi或保存信息
+    this.sendWiFiInfo(wifiName, wifiPassword);
+  },
+  addNotification() {
+    let _this = this;
+    EventManager.addNotification(CmdEvent.eventName, function (event) {
+      let name = event.cmdEvent;
+      console.log("OTA页0:", 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;
+      }
+    }, _this)
+  },
+  onLoad: function (options) {
+    let param = options.param;
+    let url = JSON.parse(param).url ?? "";
+    this.data._otaUrl = url;
+    this.getConnectedWifi();
+    this.addNotification()
+  },
+  onUnload: function () {
+    EventManager.removeNotification(CmdEvent.eventName)
+  },
+});

+ 3 - 0
pages/setWifi/setWifi.json

@@ -0,0 +1,3 @@
+{
+  "usingComponents": {}
+}

+ 12 - 0
pages/setWifi/setWifi.wxml

@@ -0,0 +1,12 @@
+<!-- pages/setWifi/setWifi.wxml -->
+<view class="container">
+    <view class="input-group">
+        <label class="label">Wi-Fi 名称</label>
+        <input class="input" type="text" placeholder="请输入Wi-Fi名称" value="{{wifiName}}" bindinput="onWifiNameInput" />
+    </view>
+    <view class="input-group">
+        <label class="label">密码</label>
+        <input class="input" type="password" placeholder="请输入密码" value="{{wifiPassword}}" bindinput="onWifiPasswordInput" />
+    </view>
+    <button class="confirm-button" bindtap="onConfirm">确认</button>
+</view>

+ 30 - 0
pages/setWifi/setWifi.wxss

@@ -0,0 +1,30 @@
+/* pages/setWifi/setWifi.wxss */
+/* pages/setWifi/setWifi.wxss */
+.container {
+    padding: 20px;
+}
+
+.input-group {
+    margin-bottom: 15px;
+}
+
+.label {
+    display: block;
+    margin-bottom: 5px;
+}
+
+.input {
+    border: 1px solid #ccc;
+    padding: 10px;
+    width: 100%;
+    box-sizing: border-box;
+}
+
+.confirm-button {
+    background-color: #1aad19;
+    color: white;
+    padding: 10px;
+    border: none;
+    width: 100%;
+    border-radius: 5px;
+}