ota.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. const { BtHelper } = require("../../devices/bt_helper");
  2. import { EnumCmdEvent } from '../../devices/cmd_key_event';
  3. import EventManager from '../../utils/event_bus'
  4. // pages/OTA/ota.js
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. nvabarData: {
  11. showCapsule: 1, //
  12. title: '固件信息',
  13. },
  14. device: {},
  15. otaData: {},
  16. btHelper: null,
  17. progress: 0, // 进度条初始值
  18. otaStatus: 0,
  19. buttonTips: "固件更新",
  20. _chunks: [],
  21. },
  22. setOtaStatus(status) {
  23. switch (status) {
  24. case 0:
  25. this.setData({
  26. otaStatus: 0,
  27. buttonTips: "固件更新"
  28. });
  29. break;
  30. case 1:
  31. this.setData({
  32. otaStatus: 1,
  33. buttonTips: "下载升级包"
  34. });
  35. break;
  36. case 2:
  37. this.setData({
  38. otaStatus: 2,
  39. buttonTips: "开始更新"
  40. });
  41. break;
  42. case 3:
  43. this.setData({
  44. otaStatus: 3,
  45. buttonTips: "更新中..."
  46. });
  47. break;
  48. case 4:
  49. this.setData({
  50. otaStatus: 4,
  51. buttonTips: "更新失败,再试一次"
  52. });
  53. break;
  54. default:
  55. break;
  56. }
  57. },
  58. startOtaTap() {
  59. let connect = getApp().globalData.device.connect ?? false
  60. if (!connect) {
  61. wx.showToast({
  62. title: '设备已经掉线了',
  63. })
  64. return;
  65. }
  66. let _this = this
  67. wx.showModal({
  68. title: '有新固件可升级',
  69. content: _this.otaData.content ?? '提升体验,操作更流畅',
  70. cancelText: '以后再说',
  71. confirmText: '升级',
  72. complete: (res) => {
  73. if (res.cancel) {
  74. }
  75. if (res.confirm) {
  76. _this.downloadFile(_this.otaData.url)
  77. }
  78. }
  79. })
  80. // const btHelper = BtHelper.getInstance();
  81. // btHelper.ota(this.data.otaData.file_url)
  82. },
  83. downloadFile(url) {
  84. urlPath = updateData.url ?? "";
  85. content = updateData.content ?? "暂无更新";
  86. var list = urlPath.split("/");
  87. // let cachePath = await PathUtil.getDownloadPath(fileName: list.last ?? 'OTAUpgrade.OTA');
  88. const fs = wx.getFileSystemManager();
  89. // 构建本地文件路径
  90. const localFilePath = list[list.length - 1];
  91. let _this = this;
  92. // 判断文件是否存在
  93. fs.access(localFilePath, (err) => {
  94. if (err) {
  95. // 文件不存在,开始下载
  96. wx.downloadFile({
  97. url: url, // 下载资源的URL
  98. success: (res) => {
  99. if (res.statusCode === 200) {
  100. // 下载成功,保存文件路径
  101. const filePath = res.tempFilePath;
  102. // 将下载的文件移动到本地路径
  103. fs.rename({
  104. oldPath: filePath,
  105. newPath: localFilePath,
  106. success: () => {
  107. // this.setData({
  108. // localFilePath: localFilePath
  109. // });
  110. // 调用 OTA 更新方法
  111. // const btHelper = BtHelper.getInstance();
  112. // btHelper.ota(localFilePath);
  113. wx.showToast({
  114. title: '文件下载成功',
  115. });
  116. _this.readFile(localFilePath);
  117. },
  118. fail: (err) => {
  119. wx.showToast({
  120. title: '文件移动失败',
  121. });
  122. console.error('文件移动失败:', err);
  123. }
  124. });
  125. } else {
  126. wx.showToast({
  127. title: '文件下载失败',
  128. });
  129. }
  130. },
  131. fail: (err) => {
  132. wx.showToast({
  133. title: '文件下载失败',
  134. });
  135. console.error('下载文件失败:', err);
  136. }
  137. });
  138. } else {
  139. _this.readFile(localFilePath);
  140. wx.showToast({
  141. title: '文件已存在,跳过下载',
  142. });
  143. }
  144. });
  145. },
  146. readFile(localFilePath) {
  147. if (!localFilePath) {
  148. wx.showToast({
  149. title: '下载文件为空',
  150. });
  151. return;
  152. }
  153. this.setOtaStatus(2);
  154. const fs = wx.getFileSystemManager();
  155. let _this = this;
  156. fs.readFile({
  157. filePath: localFilePath,
  158. encoding: 'base64', // 可以选择 'utf8' 或 'base64'
  159. success: (res) => {
  160. // const btHelper = BtHelper.getInstance();
  161. // btHelper.ota(localFilePath);
  162. // console.log('文件内容:', res.data);
  163. _this.sendDataToBluetooth(res);
  164. },
  165. fail: (err) => {
  166. wx.showToast({
  167. title: 'OTA文件下载失败',
  168. });
  169. console.error('文件读取失败:', err);
  170. }
  171. });
  172. },
  173. sendDataToBluetooth(data) {
  174. const chunkSize = 20; // 每次发送的数据块大小
  175. const chunks = [];
  176. for (let i = 0; i < data.length; i += chunkSize) {
  177. chunks.push(data.slice(i, i + chunkSize));
  178. }
  179. this.data._chunks = chunks;
  180. },
  181. updateProgress(newProgress) {
  182. this.setData({
  183. progress: newProgress,
  184. });
  185. },
  186. // 示例:模拟进度更新
  187. simulateProgressUpdate() {
  188. let currentProgress = 0;
  189. const interval = setInterval(() => {
  190. if (currentProgress >= 100) {
  191. clearInterval(interval);
  192. wx.showToast({
  193. title: '升级完成',
  194. });
  195. } else {
  196. currentProgress += 10;
  197. this.updateProgress(currentProgress);
  198. }
  199. }, 500);
  200. },
  201. /**
  202. * 生命周期函数--监听页面加载
  203. */
  204. onLoad(options) {
  205. let param = JSON.parse(options.param)
  206. console.log(param)
  207. let otaData = param.otaData
  208. let device = param.device
  209. device.clientType = device.clientType ?? device.ProdModel
  210. this.setData({
  211. otaData: otaData,
  212. device: device
  213. })
  214. let _this = this;
  215. EventManager.addNotification(CmdEvent.eventName, function (event) {
  216. let name = event.name;
  217. console.log("OTA页:", event)
  218. switch (name) {
  219. case EnumCmdEvent.otaCmd:
  220. let otaCmd = event.otaCmd;
  221. if (otaCmd === 1) {
  222. _this.sendDataToBluetooth()
  223. } else {
  224. }
  225. break;
  226. }
  227. }, this)
  228. btHelper = BtHelper.getInstance();
  229. const sendNextChunk = (index) => {
  230. if (index >= chunks.length) {
  231. wx.showToast({
  232. title: '文件发送完成,设备开始升级',
  233. });
  234. btHelper.otaCmd(0);
  235. return;
  236. }
  237. const chunk = chunks[index];
  238. btHelper.otaData(chunk);
  239. }
  240. },
  241. /**
  242. * 生命周期函数--监听页面初次渲染完成
  243. */
  244. onReady() {
  245. },
  246. /**
  247. * 生命周期函数--监听页面显示
  248. */
  249. onShow() {
  250. },
  251. /**
  252. * 生命周期函数--监听页面隐藏
  253. */
  254. onHide() {
  255. },
  256. /**
  257. * 生命周期函数--监听页面卸载
  258. */
  259. onUnload() {
  260. },
  261. /**
  262. * 页面相关事件处理函数--监听用户下拉动作
  263. */
  264. onPullDownRefresh() {
  265. },
  266. /**
  267. * 页面上拉触底事件的处理函数
  268. */
  269. onReachBottom() {
  270. },
  271. /**
  272. * 用户点击右上角分享
  273. */
  274. onShareAppMessage() {
  275. }
  276. })