ota.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // pages/OTA/ota.js
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. nvabarData: {
  8. showCapsule: 0, //是否显示左上角图标 1表示显示 0表示不显示
  9. title: '固件信息', //导航栏 中间的标题
  10. },
  11. device: getApp().globalData.device,
  12. otaData: {},
  13. btHelper: null,
  14. },
  15. startOtaTap() {
  16. let connect = getApp().globalData.device.connect ?? false
  17. if (!connect) {
  18. wx.showToast({
  19. title: '设备已经掉线了',
  20. })
  21. return;
  22. }
  23. let _this = this
  24. wx.showModal({
  25. title: '有新固件可升级',
  26. content: _this.otaData.content ?? '提升体验,操作更流畅',
  27. cancelText: '以后再说',
  28. confirmText: '升级',
  29. complete: (res) => {
  30. if (res.cancel) {
  31. }
  32. if (res.confirm) {
  33. _this.downloadFile(_this.otaData.url)
  34. }
  35. }
  36. })
  37. // const btHelper = BtHelper.getInstance();
  38. // btHelper.ota(this.data.otaData.file_url)
  39. },
  40. downloadFile(url) {
  41. urlPath = updateData.url ?? "";
  42. content = updateData.content ?? "暂无更新";
  43. var list = urlPath.split("/");
  44. // let cachePath = await PathUtil.getDownloadPath(fileName: list.last ?? 'OTAUpgrade.OTA');
  45. const fs = wx.getFileSystemManager();
  46. // 构建本地文件路径
  47. const localFilePath = list[list.length - 1];
  48. let _this = this;
  49. // 判断文件是否存在
  50. fs.access(localFilePath, (err) => {
  51. if (err) {
  52. // 文件不存在,开始下载
  53. wx.downloadFile({
  54. url: url, // 下载资源的URL
  55. success: (res) => {
  56. if (res.statusCode === 200) {
  57. // 下载成功,保存文件路径
  58. const filePath = res.tempFilePath;
  59. // 将下载的文件移动到本地路径
  60. fs.rename({
  61. oldPath: filePath,
  62. newPath: localFilePath,
  63. success: () => {
  64. // this.setData({
  65. // localFilePath: localFilePath
  66. // });
  67. // 调用 OTA 更新方法
  68. // const btHelper = BtHelper.getInstance();
  69. // btHelper.ota(localFilePath);
  70. wx.showToast({
  71. title: '文件下载成功',
  72. });
  73. _this.readFile(localFilePath);
  74. },
  75. fail: (err) => {
  76. wx.showToast({
  77. title: '文件移动失败',
  78. });
  79. console.error('文件移动失败:', err);
  80. }
  81. });
  82. } else {
  83. wx.showToast({
  84. title: '文件下载失败',
  85. });
  86. }
  87. },
  88. fail: (err) => {
  89. wx.showToast({
  90. title: '文件下载失败',
  91. });
  92. console.error('下载文件失败:', err);
  93. }
  94. });
  95. } else {
  96. _this.readFile(localFilePath);
  97. wx.showToast({
  98. title: '文件已存在,跳过下载',
  99. });
  100. }
  101. });
  102. },
  103. readFile(localFilePath) {
  104. if (!localFilePath) {
  105. wx.showToast({
  106. title: '下载文件为空',
  107. });
  108. return;
  109. }
  110. const fs = wx.getFileSystemManager();
  111. fs.readFile({
  112. filePath: localFilePath,
  113. encoding: 'base64', // 可以选择 'utf8' 或 'base64'
  114. success: (res) => {
  115. // const btHelper = BtHelper.getInstance();
  116. // btHelper.ota(localFilePath);
  117. // console.log('文件内容:', res.data);
  118. },
  119. fail: (err) => {
  120. wx.showToast({
  121. title: 'OTA文件下载失败',
  122. });
  123. console.error('文件读取失败:', err);
  124. }
  125. });
  126. },
  127. sendDataToBluetooth(data) {
  128. const chunkSize = 20; // 每次发送的数据块大小
  129. const chunks = [];
  130. for (let i = 0; i < data.length; i += chunkSize) {
  131. chunks.push(data.slice(i, i + chunkSize));
  132. }
  133. const sendNextChunk = (index) => {
  134. if (index >= chunks.length) {
  135. wx.showToast({
  136. title: '文件发送完成',
  137. });
  138. return;
  139. }
  140. const chunk = chunks[index];
  141. // wx.writeBLECharacteristicValue({
  142. // deviceId: deviceId,
  143. // serviceId: 'yourServiceId', // 替换为实际的服务ID
  144. // characteristicId: 'yourCharacteristicId', // 替换为实际的特征值ID
  145. // value: new Uint8Array(Buffer.from(chunk, 'base64')),
  146. // success: () => {
  147. // setTimeout(() => {
  148. // sendNextChunk(index + 1);
  149. // }, 100); // 延迟100毫秒发送下一个数据块
  150. // },
  151. // fail: (err) => {
  152. // wx.showToast({
  153. // title: '文件发送失败',
  154. // });
  155. // console.error('文件发送失败:', err);
  156. // }
  157. // });
  158. }
  159. },
  160. /**
  161. * 生命周期函数--监听页面加载
  162. */
  163. onLoad(options) {
  164. let otaData = JSON.parse(options.param)
  165. this.setData({
  166. otaData: otaData
  167. })
  168. },
  169. /**
  170. * 生命周期函数--监听页面初次渲染完成
  171. */
  172. onReady() {
  173. },
  174. /**
  175. * 生命周期函数--监听页面显示
  176. */
  177. onShow() {
  178. },
  179. /**
  180. * 生命周期函数--监听页面隐藏
  181. */
  182. onHide() {
  183. },
  184. /**
  185. * 生命周期函数--监听页面卸载
  186. */
  187. onUnload() {
  188. },
  189. /**
  190. * 页面相关事件处理函数--监听用户下拉动作
  191. */
  192. onPullDownRefresh() {
  193. },
  194. /**
  195. * 页面上拉触底事件的处理函数
  196. */
  197. onReachBottom() {
  198. },
  199. /**
  200. * 用户点击右上角分享
  201. */
  202. onShareAppMessage() {
  203. }
  204. })