ota.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. const { BtHelper } = require("../../devices/bt_helper");
  2. import { EnumCmdEvent, CmdEvent } from '../../devices/cmd_key_event';
  3. import EventManager from '../../utils/event_bus'
  4. import route_util from '../../utils/route_util';
  5. import route_constant from '../../utils/route_constant.js';
  6. import { BtCmd } from '../../devices/bluetooth/bt_cmd.js';
  7. // pages/OTA/ota.js
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. navbarData: {
  14. showCapsule: 1, //
  15. title: '固件信息',
  16. },
  17. device: {},
  18. otaData: {},
  19. btHelper: null,
  20. progress: 0, // 进度条初始值
  21. otaStatus: 0,
  22. buttonTips: "固件更新",
  23. _chunks: [],
  24. progressPercent: 0,
  25. progress: 0,
  26. progressTextLeft: "0%",
  27. _timer: null,
  28. },
  29. setOtaStatus(status) {
  30. switch (status) {
  31. case 0:
  32. this.setData({
  33. otaStatus: 0,
  34. buttonTips: "固件更新"
  35. });
  36. break;
  37. case 1:
  38. this.setData({
  39. otaStatus: 1,
  40. buttonTips: "下载升级包"
  41. });
  42. break;
  43. case 2:
  44. this.setData({
  45. otaStatus: 2,
  46. buttonTips: "开始更新"
  47. });
  48. break;
  49. case 3:
  50. this.setData({
  51. otaStatus: 3,
  52. buttonTips: "更新中..."
  53. });
  54. break;
  55. case 4:
  56. this.setData({
  57. otaStatus: 4,
  58. buttonTips: "更新失败,再试一次"
  59. });
  60. break;
  61. case 5:
  62. this.setData({
  63. otaStatus: 4,
  64. buttonTips: "更新完成"
  65. });
  66. break;
  67. default:
  68. break;
  69. }
  70. },
  71. startOtaTap() {
  72. let online = this.data.device.state === "online"
  73. if (!online) {
  74. wx.showToast({
  75. title: '设备已经掉线了',
  76. })
  77. return;
  78. }
  79. let hasNewVersion = this.data.otaData.hasNewVersion ?? false
  80. if (!hasNewVersion) {
  81. wx.showToast({
  82. title: '没有可升级的固件',
  83. })
  84. return;
  85. }
  86. if (this.data.otaStatus != 0) {
  87. console.log("没有可升级的固件2")
  88. return;
  89. }
  90. let _this = this
  91. wx.showModal({
  92. title: '有新固件可升级',
  93. content: _this.data.otaData.content ?? '提升体验,操作更流畅',
  94. cancelText: '以后再说',
  95. confirmText: '升级',
  96. complete: (res) => {
  97. if (res.cancel) {
  98. }
  99. if (res.confirm) {
  100. wx.showLoading({
  101. title: '升级设备中,请稍后',
  102. })
  103. _this.sendOtaCmd(1)
  104. // _this.startDownloadFile()
  105. // _this.downloadOtaFile(_this.data.otaData.url ?? "")
  106. }
  107. }
  108. })
  109. // const btHelper = BtHelper.getInstance();
  110. // btHelper.ota(this.data.otaData.file_url)
  111. },
  112. goToWifi() {
  113. route_util.jump(route_constant.setWifi)
  114. },
  115. sendUrlData() {
  116. let url = this.data.otaData.url ?? ""
  117. let codeUrl = BtCmd.stringToUint8Array(url)
  118. BtHelper.getInstance().otaUrl(codeUrl);
  119. wx.showModal({
  120. title: '发送成功',
  121. content: '开始升级OTA,请等待设备升级成功',
  122. showCancel: false,
  123. success: (res) => {
  124. if (res.confirm) {
  125. route_util.goBack()
  126. }
  127. }
  128. })
  129. },
  130. // 读取文件废弃代码
  131. startDownloadFile() {
  132. var urlPath = this.data.otaData.url ?? "";
  133. var list = urlPath.split("/");
  134. // let cachePath = await PathUtil.getDownloadPath(fileName: list.last ?? 'OTAUpgrade.OTA');
  135. const fs = wx.getFileSystemManager();
  136. // 构建本地文件路径
  137. var localFilePath = list[list.length - 1];
  138. console.log(list, localFilePath)
  139. let _this = this;
  140. _this.setOtaStatus(2);
  141. let localPath = `${wx.env.USER_DATA_PATH}/` + localFilePath
  142. // 判断文件是否存在
  143. fs.access({
  144. path: localPath,
  145. success(res) {
  146. console.log('文件已存在:', res);
  147. if (res) {
  148. // 有文件
  149. _this.readFile(localPath);
  150. } else {
  151. _this.downloadOtaFile(urlPath, localPath)
  152. }
  153. }, fail(err) {
  154. // 文件不存在或其他错误
  155. console.log(err)
  156. // 文件不存在,开始下载
  157. _this.downloadOtaFile(urlPath, localPath)
  158. }
  159. }
  160. );
  161. },
  162. downloadOtaFile(urlPath, localPath) {
  163. let _this = this;
  164. // const fs = wx.getFileSystemManager();
  165. wx.downloadFile({
  166. url: urlPath, // 下载资源的URL
  167. success: (res) => {
  168. console.log('文件下载成功2', res);
  169. if (res.statusCode === 200) {
  170. // 下载成功,保存文件路径
  171. const filePath = res.tempFilePath;
  172. // _this.readFile(filePath);
  173. // 将下载的文件移动到本地路径
  174. fs.rename({
  175. oldPath: filePath,
  176. newPath: localPath,
  177. success: () => {
  178. wx.showToast({
  179. title: '文件下载成功',
  180. });
  181. _this.readFile(localPath);
  182. },
  183. fail: (err) => {
  184. _this.setOtaStatus(4);
  185. wx.showToast({
  186. title: '下载文件失败,检查下手机内存吧',
  187. });
  188. console.error('文件移动失败:', err);
  189. }
  190. });
  191. } else {
  192. _this.setOtaStatus(4);
  193. wx.showToast({
  194. title: '文件下载失败',
  195. });
  196. }
  197. },
  198. fail: (err) => {
  199. _this.setOtaStatus(4);
  200. wx.showToast({
  201. title: '文件下载失败',
  202. });
  203. console.error('下载文件失败:', err);
  204. }
  205. });
  206. },
  207. readFile(localFilePath) {
  208. if (!localFilePath) {
  209. wx.showToast({
  210. title: '下载文件为空',
  211. });
  212. return;
  213. }
  214. wx.showLoading({
  215. title: '开始传输文件',
  216. })
  217. const fs = wx.getFileSystemManager();
  218. let _this = this;
  219. fs.readFile({
  220. filePath: localFilePath,
  221. encoding: 'base64', // 可以选择 'utf8' 或 'base64'
  222. success: (res) => {
  223. // const btHelper = BtHelper.getInstance();
  224. // btHelper.ota(localFilePath);
  225. console.log('文件内容:', res.data.length);
  226. _this.startSend(res.data);
  227. },
  228. fail: (err) => {
  229. wx.showToast({
  230. title: 'OTA文件下载失败',
  231. });
  232. console.error('文件读取失败:', err);
  233. _this.setOtaStatus(4);
  234. }
  235. });
  236. },
  237. startSend(data) {
  238. // const chunkSize = 20; // 每次发送的数据块大小
  239. // const chunks = [];
  240. // for (let i = 0; i < data.length; i += chunkSize) {
  241. // chunks.push(data.slice(i, i + chunkSize));
  242. // }
  243. // this.data._chunks = chunks;
  244. this.sendOtaCmd(1)
  245. // this.startSendOtaData()
  246. },
  247. startSendOtaData() {
  248. let _this = this
  249. let length = this.data._chunks.length
  250. if (length === 0) {
  251. wx.showToast({
  252. title: '下载文件失败',
  253. icon: 'none'
  254. })
  255. _this.sendOtaCmd(2)
  256. return;
  257. }
  258. _this.sendOtaData(this.data._chunks, 0)
  259. // }
  260. }, sendOtaData(imageBuffer, index) {
  261. let _this = this
  262. if (index >= imageBuffer.length) {
  263. _this.sendOtaCmd(0)
  264. return;
  265. }
  266. let chunkSize = 20;
  267. let total = imageBuffer.length
  268. let next = index + chunkSize;
  269. const chunk = imageBuffer.slice(index, next);
  270. // todo 改成设备wifi下载
  271. // BtHelper.getInstance().wallPaperData(chunk, function (res) {
  272. // console.log("发送OTA数据:", next)
  273. // if (res) {
  274. // _this.updateProgress(next, total);
  275. // _this.sendOtaData(imageBuffer)
  276. // } else {
  277. // wx.showModal({
  278. // title: 'OTA升级失败了',
  279. // showCancel: false
  280. // })
  281. // }
  282. // })
  283. },
  284. sendOtaCmd(value) {
  285. BtHelper.getInstance().otaCmd(value)
  286. if (value == 0) {
  287. this.setOtaStatus(0);
  288. } else {
  289. // this.setOtaStatus(4);
  290. }
  291. },
  292. startProgress() {
  293. this.setData({
  294. progress: 0,
  295. showProgress: true
  296. })
  297. },
  298. updateProgress(chunk, total) {
  299. let progress = chunk / total * 100;
  300. let _this = this
  301. if (progress >= 100) {
  302. _this.setData({
  303. progress: 0,
  304. showProgress: false,
  305. // showCropImg: false
  306. });
  307. wx.showToast({
  308. title: '图片上传成功',
  309. })
  310. } else {
  311. _this.setData({
  312. progress: progress,
  313. });
  314. }
  315. },
  316. addNotification() {
  317. let _this = this;
  318. EventManager.addNotification(CmdEvent.eventName, function (event) {
  319. let name = event.cmdEvent;
  320. console.log("OTA页0:", event)
  321. let kind = event.heiJiaoKind;
  322. // 74,1,1是wifi连接成功,74,0 2连接wifi失败/没有wifi。
  323. // 发送url给你后,你回:74 0 1是流程成功,74 0 3是流程失败。
  324. let otaCmd = event.otaCmd;
  325. switch (name) {
  326. case EnumCmdEvent.otaCmd:
  327. console.log("OTA页:", otaCmd, kind)
  328. if (otaCmd === 1 && kind == 1) {
  329. wx.hideLoading();
  330. // 设备收到开启OTA的回复,发送url
  331. // todo 测试 去设置wifi界面
  332. _this.sendUrlData()
  333. } else if (otaCmd === 0 && kind == 2) {
  334. wx.hideLoading();
  335. // 去设置wifi界面
  336. _this.goToWifi()
  337. } else if (otaCmd === 0 && kind == 3) {
  338. wx.hideLoading();
  339. // 流程失败
  340. wx.showModal({
  341. title: '设备升级失败了',
  342. showCancel: false,
  343. success: function (res) {
  344. if (res.confirm) {
  345. wx.navigateBack({
  346. delta: 2
  347. })
  348. }
  349. }
  350. })
  351. } else if (otaCmd === 0 && kind == 1) {
  352. wx.hideLoading();
  353. // 流程成功
  354. wx.showModal({
  355. title: '设备开始升级中',
  356. showCancel: false,
  357. success: function (res) {
  358. if (res.confirm) {
  359. wx.navigateBack({
  360. delta: 2
  361. })
  362. }
  363. }
  364. })
  365. }
  366. else if (kind == 0) {
  367. wx.hideLoading()
  368. wx.showModal({
  369. title: 'WIFI连接失败了',
  370. showCancel: false
  371. })
  372. }
  373. break;
  374. case EnumCmdEvent.otaUrl:
  375. let otaUrl = event.otaUrl;
  376. if (otaUrl === 1 && kind == 1) {
  377. // 开始发送url
  378. BtHelper.getInstance().otaUrl(BtCmd.stringToUint8Array(_this.data.otaData.url))
  379. } else {
  380. wx.hideLoading()
  381. // wifi失败
  382. wx.showModal({
  383. title: 'OTA升级失败了',
  384. showCancel: false
  385. })
  386. }
  387. break;
  388. case EnumCmdEvent.otaWifi:
  389. wx.hideLoading()
  390. // 0x76, 发送wifi成功
  391. if (otaCmd === 1 && kind == 1) {
  392. _this.sendUrlData()
  393. } else {
  394. // wifi失败
  395. wx.showModal({
  396. title: 'OTA升级失败了',
  397. showCancel: false
  398. })
  399. }
  400. break;
  401. }
  402. }, _this)
  403. },
  404. /**
  405. * 生命周期函数--监听页面加载
  406. */
  407. onLoad(options) {
  408. let param = JSON.parse(options.param)
  409. console.log("OTA界面:", param)
  410. let otaData = param.otaData
  411. let device = param.device
  412. this.setData({
  413. otaData: otaData,
  414. device: device,
  415. isShowOta: device.clientType === 'MW-S2(BLE)' || device.clientType === 'MW-S2'
  416. })
  417. this.addNotification()
  418. },
  419. onUnload() {
  420. EventManager.removeNotification(CmdEvent.eventName, this)
  421. },
  422. })