ota.js 12 KB

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