ota.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. const { BtHelper } = require("../../devices/bt_helper");
  2. import { EnumCmdEvent, CmdEvent } 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. navbarData: {
  11. showCapsule: 1, //
  12. title: '固件信息',
  13. },
  14. device: {},
  15. otaData: {},
  16. btHelper: null,
  17. progress: 0, // 进度条初始值
  18. otaStatus: 0,
  19. buttonTips: "固件更新",
  20. _chunks: [],
  21. progressPercent: 0,
  22. progress: 0,
  23. progressTextLeft: "0%",
  24. _timer: null,
  25. },
  26. setOtaStatus(status) {
  27. switch (status) {
  28. case 0:
  29. this.setData({
  30. otaStatus: 0,
  31. buttonTips: "固件更新"
  32. });
  33. break;
  34. case 1:
  35. this.setData({
  36. otaStatus: 1,
  37. buttonTips: "下载升级包"
  38. });
  39. break;
  40. case 2:
  41. this.setData({
  42. otaStatus: 2,
  43. buttonTips: "开始更新"
  44. });
  45. break;
  46. case 3:
  47. this.setData({
  48. otaStatus: 3,
  49. buttonTips: "更新中..."
  50. });
  51. break;
  52. case 4:
  53. this.setData({
  54. otaStatus: 4,
  55. buttonTips: "更新失败,再试一次"
  56. });
  57. break;
  58. default:
  59. break;
  60. }
  61. },
  62. startOtaTap() {
  63. let online = this.data.device.state === "online"
  64. if (!online) {
  65. wx.showToast({
  66. title: '设备已经掉线了',
  67. })
  68. return;
  69. }
  70. let hasNewVersion = this.data.otaData.hasNewVersion ?? false
  71. if (!hasNewVersion) {
  72. wx.showToast({
  73. title: '没有可升级的固件',
  74. })
  75. return;
  76. }
  77. let _this = this
  78. wx.showModal({
  79. title: '有新固件可升级',
  80. content: _this.data.otaData.content ?? '提升体验,操作更流畅',
  81. cancelText: '以后再说',
  82. confirmText: '升级',
  83. complete: (res) => {
  84. if (res.cancel) {
  85. }
  86. if (res.confirm) {
  87. _this.startDownloadFile()
  88. // _this.downloadOtaFile(_this.data.otaData.url ?? "")
  89. }
  90. }
  91. })
  92. // const btHelper = BtHelper.getInstance();
  93. // btHelper.ota(this.data.otaData.file_url)
  94. },
  95. startDownloadFile() {
  96. var urlPath = this.data.otaData.url ?? "";
  97. var list = urlPath.split("/");
  98. // let cachePath = await PathUtil.getDownloadPath(fileName: list.last ?? 'OTAUpgrade.OTA');
  99. const fs = wx.getFileSystemManager();
  100. // 构建本地文件路径
  101. var localFilePath = list[list.length - 1];
  102. console.log(list, localFilePath)
  103. let _this = this;
  104. let localPath = `${wx.env.USER_DATA_PATH}/` + localFilePath
  105. // 判断文件是否存在
  106. fs.access({
  107. path: localPath,
  108. success(res) {
  109. console.log('文件已存在:', res);
  110. if (res) {
  111. // 有文件
  112. _this.readFile(localPath);
  113. } else {
  114. }
  115. }, fail(err) {
  116. // 文件不存在或其他错误
  117. console.log(err)
  118. // 文件不存在,开始下载
  119. _this.downloadOtaFile(urlPath, localPath)
  120. }
  121. }
  122. );
  123. },
  124. downloadOtaFile(urlPath, localPath) {
  125. let _this = this;
  126. // const fs = wx.getFileSystemManager();
  127. wx.downloadFile({
  128. url: urlPath, // 下载资源的URL
  129. success: (res) => {
  130. console.log('文件下载成功2', res);
  131. if (res.statusCode === 200) {
  132. // 下载成功,保存文件路径
  133. const filePath = res.tempFilePath;
  134. // _this.readFile(filePath);
  135. _this.readFile(filePath);
  136. // 将下载的文件移动到本地路径
  137. // fs.rename({
  138. // oldPath: filePath,
  139. // newPath: localPath,
  140. // success: () => {
  141. // // this.setData({
  142. // // localFilePath: localFilePath
  143. // // });
  144. // // 调用 OTA 更新方法
  145. // // const btHelper = BtHelper.getInstance();
  146. // // btHelper.ota(localFilePath);
  147. // wx.showToast({
  148. // title: '文件下载成功',
  149. // });
  150. // _this.readFile(localPath);
  151. // },
  152. // fail: (err) => {
  153. // wx.showToast({
  154. // title: '下载文件失败,检查下手机内存吧',
  155. // });
  156. // console.error('文件移动失败:', err);
  157. // }
  158. // });
  159. } else {
  160. wx.showToast({
  161. title: '文件下载失败',
  162. });
  163. }
  164. },
  165. fail: (err) => {
  166. wx.showToast({
  167. title: '文件下载失败',
  168. });
  169. console.error('下载文件失败:', err);
  170. }
  171. });
  172. },
  173. readFile(localFilePath) {
  174. if (!localFilePath) {
  175. wx.showToast({
  176. title: '下载文件为空',
  177. });
  178. return;
  179. }
  180. wx.showLoading({
  181. title: '开始传输文件',
  182. })
  183. this.setOtaStatus(2);
  184. const fs = wx.getFileSystemManager();
  185. let _this = this;
  186. fs.readFile({
  187. filePath: localFilePath,
  188. encoding: 'base64', // 可以选择 'utf8' 或 'base64'
  189. success: (res) => {
  190. // const btHelper = BtHelper.getInstance();
  191. // btHelper.ota(localFilePath);
  192. console.log('文件内容:', res.data.length);
  193. _this.startSend(res.data);
  194. },
  195. fail: (err) => {
  196. wx.showToast({
  197. title: 'OTA文件下载失败',
  198. });
  199. console.error('文件读取失败:', err);
  200. }
  201. });
  202. },
  203. startSend(data) {
  204. const chunkSize = 20; // 每次发送的数据块大小
  205. const chunks = [];
  206. for (let i = 0; i < data.length; i += chunkSize) {
  207. chunks.push(data.slice(i, i + chunkSize));
  208. }
  209. this.data._chunks = chunks;
  210. this.endOta(1)
  211. // this.startSendOtaData()
  212. },
  213. startSendOtaData() {
  214. let _this = this
  215. let length = this.data._chunks.length
  216. if (length === 0) {
  217. wx.showToast({
  218. title: '图片裁剪失败',
  219. icon: 'none'
  220. })
  221. _this.endOta(2)
  222. return;
  223. }
  224. _this.sendOtaData(this.data._chunks, 0)
  225. // }
  226. }, sendOtaData(imageBuffer, index) {
  227. let _this = this
  228. if (index >= imageBuffer.length) {
  229. _this.endOta(0)
  230. return;
  231. }
  232. let chunkSize = 20;
  233. let total = imageBuffer.length
  234. let next = index + chunkSize;
  235. const chunk = imageBuffer.slice(index, next);
  236. BtHelper.getInstance().wallPaperData(chunk, function (res) {
  237. console.log("发送OTA数据:", next)
  238. if (res) {
  239. _this.updateProgress(next, total);
  240. _this.sendOtaData(imageBuffer)
  241. } else {
  242. wx.showModal({
  243. title: 'OTA升级失败了',
  244. showCancel: false
  245. })
  246. }
  247. })
  248. },
  249. endOta(value) {
  250. BtHelper.getInstance().otaCmd(value)
  251. },
  252. startProgress() {
  253. this.setData({
  254. progress: 0,
  255. showProgress: true
  256. })
  257. },
  258. updateProgress(chunk, total) {
  259. let progress = chunk / total * 100;
  260. let _this = this
  261. if (progress >= 100) {
  262. _this.setData({
  263. progress: 0,
  264. showProgress: false,
  265. // showCropImg: false
  266. });
  267. wx.showToast({
  268. title: '图片上传成功',
  269. })
  270. } else {
  271. _this.setData({
  272. progress: progress,
  273. });
  274. }
  275. },
  276. /**
  277. * 生命周期函数--监听页面加载
  278. */
  279. onLoad(options) {
  280. let param = JSON.parse(options.param)
  281. console.log("OTA界面:", param)
  282. let otaData = param.otaData
  283. let device = param.device
  284. this.setData({
  285. otaData: otaData,
  286. device: device,
  287. isShowOta: device.clientType === 'MW-S2(BLE)' || device.clientType === 'MW-S2'
  288. })
  289. let _this = this;
  290. EventManager.addNotification(CmdEvent.eventName, function (event) {
  291. let name = event.cmdEvent;
  292. console.log("OTA页0:", name, EnumCmdEvent.otaCmd, EnumCmdEvent.otaCmd === name)
  293. switch (name) {
  294. case EnumCmdEvent.otaCmd:
  295. let otaCmd = event.otaCmd;
  296. let kind = event.heiJiaoKind;
  297. console.log("OTA页:", otaCmd, kind)
  298. if (otaCmd === 1 && kind == 1) {
  299. wx.hideLoading();
  300. // 开始发送
  301. _this.startSendOtaData()
  302. } else if (otaCmd === 0 && kind == 1) {
  303. // 发送结束
  304. _this.endOta(0)
  305. } else if (kind == 0) {
  306. wx.hideLoading()
  307. wx.showModal({
  308. title: 'OTA升级失败了',
  309. showCancel: false
  310. })
  311. }
  312. break;
  313. }
  314. }, this)
  315. },
  316. onUnload() {
  317. EventManager.removeNotification(CmdEvent.eventName, this)
  318. },
  319. })