Selaa lähdekoodia

feature:增加黑胶的wifi逻辑

zeng.chen 7 kuukautta sitten
vanhempi
commit
0c1528f1bb
4 muutettua tiedostoa jossa 112 lisäystä ja 86 poistoa
  1. 2 2
      devices/bluetooth/bt_cmd.js
  2. 105 42
      pages/ota/ota.js
  3. 5 4
      pages/piano/cropper/cropper.js
  4. 0 38
      pages/piano/wallpaper/wallpaper.js

+ 2 - 2
devices/bluetooth/bt_cmd.js

@@ -248,7 +248,7 @@ class BtCmd {
     // 壁纸指令 
     static wallPaperData(value) {
         let uint8Array = new Uint8Array(value);
-        let cmd = this._build(CmdBase.wallPaperData, uint8Array);
+        let cmd = this._build(CmdBase.wallPaperData, uint8Array, true, false);
         return cmd;
     }
     // 背景图指令 1开始, 0结束
@@ -262,7 +262,7 @@ class BtCmd {
     static otaCmd(value) {
         return this._build(CmdBase.heiJiaoOta, [value]);
     } static otaData(value) {
-        return this._build(CmdBase.heiJiaoOtaData, [value]);
+        return this._build(CmdBase.heiJiaoOtaData, [value], true, false);
     }
     /// 获取设备型号指令
     static getClientType() {

+ 105 - 42
pages/ota/ota.js

@@ -57,6 +57,12 @@ Page({
           buttonTips: "更新失败,再试一次"
         });
         break;
+      case 5:
+        this.setData({
+          otaStatus: 4,
+          buttonTips: "更新完成"
+        });
+        break;
       default:
         break;
     }
@@ -78,6 +84,9 @@ Page({
       })
       return;
     }
+    if (this.data.otaStatus != 1 || this.data.otaStatus != 4) {
+      return;
+    }
 
     let _this = this
     wx.showModal({
@@ -100,6 +109,57 @@ Page({
     // btHelper.ota(this.data.otaData.file_url)
 
   },
+  sendWiFiInfo(wifiName, pwd) {
+    // [0x22, (wifiList.length + pwdList.length + 6), 0x33, (wifiList.length), (wifiList), 0x44, (pwdList)];
+
+    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);
+    result.concat(pwdList)
+
+    console.log("发送wifi账号密码:", result.toString());
+    // _ble.send({ cmd: result });
+    const btHelper = BtHelper.getInstance();
+    btHelper.otaData(result)
+  },
+  int2Hex(num) {
+    return parseInt(num, 16);
+  },
+
+  // string转换为List<int>
+  string2ListInt(text) {
+    let code = Array.from(text).map(char => char.charCodeAt(0));
+    console.log("string转换为List<int>", code)
+    return code
+  },
   startDownloadFile() {
 
     var urlPath = this.data.otaData.url ?? "";
@@ -113,6 +173,8 @@ Page({
     console.log(list, localFilePath)
 
     let _this = this;
+    _this.setOtaStatus(2);
+
     let localPath = `${wx.env.USER_DATA_PATH}/` + localFilePath
     // 判断文件是否存在
     fs.access({
@@ -125,7 +187,7 @@ Page({
           _this.readFile(localPath);
 
         } else {
-
+          _this.downloadOtaFile(urlPath, localPath)
         }
       }, fail(err) {
         // 文件不存在或其他错误
@@ -149,41 +211,35 @@ Page({
           // 下载成功,保存文件路径
           const filePath = res.tempFilePath;
           // _this.readFile(filePath);
-          _this.readFile(filePath);
 
           // 将下载的文件移动到本地路径
-          // fs.rename({
-          //   oldPath: filePath,
-          //   newPath: localPath,
-          //   success: () => {
-
-          //     // this.setData({
-          //     //   localFilePath: localFilePath
-          //     // });
-
-          //     // 调用 OTA 更新方法
-          //     // const btHelper = BtHelper.getInstance();
-          //     // btHelper.ota(localFilePath);
-
-          //     wx.showToast({
-          //       title: '文件下载成功',
-          //     });
-          //     _this.readFile(localPath);
-          //   },
-          //   fail: (err) => {
-          //     wx.showToast({
-          //       title: '下载文件失败,检查下手机内存吧',
-          //     });
-          //     console.error('文件移动失败:', err);
-          //   }
-          // });
+          fs.rename({
+            oldPath: filePath,
+            newPath: localPath,
+            success: () => {
+              wx.showToast({
+                title: '文件下载成功',
+              });
+              _this.readFile(localPath);
+            },
+            fail: (err) => {
+              _this.setOtaStatus(4);
+
+              wx.showToast({
+                title: '下载文件失败,检查下手机内存吧',
+              });
+              console.error('文件移动失败:', err);
+            }
+          });
         } else {
+          _this.setOtaStatus(4);
           wx.showToast({
             title: '文件下载失败',
           });
         }
       },
       fail: (err) => {
+        _this.setOtaStatus(4);
         wx.showToast({
           title: '文件下载失败',
         });
@@ -202,7 +258,6 @@ Page({
     wx.showLoading({
       title: '开始传输文件',
     })
-    this.setOtaStatus(2);
     const fs = wx.getFileSystemManager();
 
     let _this = this;
@@ -223,7 +278,9 @@ Page({
           title: 'OTA文件下载失败',
         });
         console.error('文件读取失败:', err);
+        _this.setOtaStatus(4);
       }
+
     });
   },
   startSend(data) {
@@ -244,7 +301,7 @@ Page({
 
     if (length === 0) {
       wx.showToast({
-        title: '图片裁剪失败',
+        title: '下载文件失败',
         icon: 'none'
       })
       _this.endOta(2)
@@ -265,23 +322,29 @@ Page({
     let total = imageBuffer.length
     let next = index + chunkSize;
     const chunk = imageBuffer.slice(index, next);
-    BtHelper.getInstance().wallPaperData(chunk, function (res) {
-      console.log("发送OTA数据:", next)
-      if (res) {
-        _this.updateProgress(next, total);
-
-        _this.sendOtaData(imageBuffer)
-      } else {
-        wx.showModal({
-          title: 'OTA升级失败了',
-          showCancel: false
-        })
-      }
-    })
+    // todo 改成设备wifi下载
+    // BtHelper.getInstance().wallPaperData(chunk, function (res) {
+    //   console.log("发送OTA数据:", next)
+    //   if (res) {
+    //     _this.updateProgress(next, total);
+
+    //     _this.sendOtaData(imageBuffer)
+    //   } else {
+    //     wx.showModal({
+    //       title: 'OTA升级失败了',
+    //       showCancel: false
+    //     })
+    //   }
+    // })
 
   },
   endOta(value) {
     BtHelper.getInstance().otaCmd(value)
+    if (value == 0) {
+      this.setOtaStatus(0);
+    } else {
+      this.setOtaStatus(4);
+    }
   },
 
   startProgress() {

+ 5 - 4
pages/piano/cropper/cropper.js

@@ -94,19 +94,19 @@ Page({
   imageDataToRGB565(rgbaData, width, height) {
     const pixelCount = width * height;
     // 十六进制字符串
-    // const hexString = "04 80 07 3c ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff";
     const hexString = "04 80 07 3c";
 
-
     // 将十六进制字符串转换为字节数组
-    const bytes = hexString.split(' ').map(byte => parseInt(byte, 10));
+    // const bytes = hexString.split(' ').map(byte => parseInt(byte, 10));
+    const bytes = ["04", "80", "07", "3c"];
+
     console.log(bytes);
 
     const outputData = new Uint8Array(pixelCount * 2 + bytes.length); // 文件头 + 每像素 4 字节 (RGB888 + Alpha)
 
     // 将字节数组写入 headerView
     bytes.forEach((byte, index) => {
-      outputData[index] = byte
+      outputData[index] = parseInt(byte, 16)
     });
 
     // 写入像素数据
@@ -238,6 +238,7 @@ Page({
     //   _this.startImage();
     // });
 
+    // 读取裁剪的jpg图片
     const fs = wx.getFileSystemManager();
     this.cropper.getImg((obj) => {
       wx.showLoading({

+ 0 - 38
pages/piano/wallpaper/wallpaper.js

@@ -36,44 +36,6 @@ Page({
   },
   footerTap() {
     route_util.jump("../cropper/cropper")
-    return;
-    const that = this;
-
-    wx.chooseMedia({
-      count: 1,
-      mediaType: ['image'],
-      sourceType: ['album'],
-      // camera: 'back',
-      success(res) {
-        // console.log(res.tempFiles[0].tempFilePath)
-        // console.log(res.tempFiles[0].size)
-        //重置图片角度、缩放、位置
-
-
-        // wx.getImageInfo({
-        //   src: res.tempFiles[0].tempFilePath,
-        //   success(res) {
-        //     console.log(res)
-        //     wx.cropImage({
-        //       src: res.path,// 图片路径
-        //       cropScale: '1:1', // 裁剪比例
-        //       success(res) {
-        //         console.log("裁剪后的图片", res)
-        //         that.setData({
-        //           showCropImg: true,
-        //           topImg: { "pic": res.tempFilePath }
-        //         })
-        //         that.convertImageToRGB565(res.tempFilePath)
-
-        //       },
-
-        //     })
-        //   }
-        // })
-
-      }
-    })
-
   },
 
   imageTap(e) {