ota.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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.startSend();
  164. },
  165. fail: (err) => {
  166. wx.showToast({
  167. title: 'OTA文件下载失败',
  168. });
  169. console.error('文件读取失败:', err);
  170. }
  171. });
  172. },
  173. startSend() {
  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. const btHelper = BtHelper.getInstance();
  181. btHelper.otaCmd(1)
  182. },
  183. sendOtaData() {
  184. const btHelper = BtHelper.getInstance();
  185. this.data._chunks.forEach(element => {
  186. btHelper.otaData(element)
  187. });
  188. },
  189. endEnd() {
  190. const btHelper = BtHelper.getInstance();
  191. btHelper.otaCmd(0)
  192. },
  193. updateProgress(newProgress) {
  194. this.setData({
  195. progress: newProgress,
  196. });
  197. },
  198. // 示例:模拟进度更新
  199. simulateProgressUpdate() {
  200. let currentProgress = 0;
  201. const interval = setInterval(() => {
  202. if (currentProgress >= 100) {
  203. clearInterval(interval);
  204. wx.showToast({
  205. title: '升级完成',
  206. });
  207. } else {
  208. currentProgress += 10;
  209. this.updateProgress(currentProgress);
  210. }
  211. }, 500);
  212. },
  213. /**
  214. * 生命周期函数--监听页面加载
  215. */
  216. onLoad(options) {
  217. let param = JSON.parse(options.param)
  218. console.log(param)
  219. let otaData = param.otaData
  220. let device = param.device
  221. device.clientType = device.clientType ?? device.ProdModel
  222. this.setData({
  223. otaData: otaData,
  224. device: device
  225. })
  226. let _this = this;
  227. EventManager.addNotification(CmdEvent.eventName, function (event) {
  228. let name = event.name;
  229. console.log("OTA页:", event)
  230. switch (name) {
  231. case EnumCmdEvent.otaCmd:
  232. let otaCmd = event.otaCmd;
  233. let kind = event.kind;
  234. if (otaCmd === 1 && kind == 1) {
  235. // 开始发送
  236. _this.sendOtaData()
  237. } else if (otaCmd === 0 && kind == 1) {
  238. // 发送结束
  239. _this.endEnd()
  240. }
  241. break;
  242. }
  243. }, this)
  244. },
  245. /**
  246. * 生命周期函数--监听页面初次渲染完成
  247. */
  248. onReady() {
  249. },
  250. /**
  251. * 生命周期函数--监听页面显示
  252. */
  253. onShow() {
  254. },
  255. /**
  256. * 生命周期函数--监听页面隐藏
  257. */
  258. onHide() {
  259. },
  260. /**
  261. * 生命周期函数--监听页面卸载
  262. */
  263. onUnload() {
  264. EventManager.removeNotification(CmdEvent.eventName, this);
  265. },
  266. /**
  267. * 页面相关事件处理函数--监听用户下拉动作
  268. */
  269. onPullDownRefresh() {
  270. },
  271. /**
  272. * 页面上拉触底事件的处理函数
  273. */
  274. onReachBottom() {
  275. },
  276. /**
  277. * 用户点击右上角分享
  278. */
  279. onShareAppMessage() {
  280. }
  281. })