ota.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. },
  120. // 读取文件废弃代码
  121. startDownloadFile() {
  122. var urlPath = this.data.otaData.url ?? "";
  123. var list = urlPath.split("/");
  124. // let cachePath = await PathUtil.getDownloadPath(fileName: list.last ?? 'OTAUpgrade.OTA');
  125. const fs = wx.getFileSystemManager();
  126. // 构建本地文件路径
  127. var localFilePath = list[list.length - 1];
  128. console.log(list, localFilePath)
  129. let _this = this;
  130. _this.setOtaStatus(2);
  131. let localPath = `${wx.env.USER_DATA_PATH}/` + localFilePath
  132. // 判断文件是否存在
  133. fs.access({
  134. path: localPath,
  135. success(res) {
  136. console.log('文件已存在:', res);
  137. if (res) {
  138. // 有文件
  139. _this.readFile(localPath);
  140. } else {
  141. _this.downloadOtaFile(urlPath, localPath)
  142. }
  143. }, fail(err) {
  144. // 文件不存在或其他错误
  145. console.log(err)
  146. // 文件不存在,开始下载
  147. _this.downloadOtaFile(urlPath, localPath)
  148. }
  149. }
  150. );
  151. },
  152. downloadOtaFile(urlPath, localPath) {
  153. let _this = this;
  154. // const fs = wx.getFileSystemManager();
  155. wx.downloadFile({
  156. url: urlPath, // 下载资源的URL
  157. success: (res) => {
  158. console.log('文件下载成功2', res);
  159. if (res.statusCode === 200) {
  160. // 下载成功,保存文件路径
  161. const filePath = res.tempFilePath;
  162. // _this.readFile(filePath);
  163. // 将下载的文件移动到本地路径
  164. fs.rename({
  165. oldPath: filePath,
  166. newPath: localPath,
  167. success: () => {
  168. wx.showToast({
  169. title: '文件下载成功',
  170. });
  171. _this.readFile(localPath);
  172. },
  173. fail: (err) => {
  174. _this.setOtaStatus(4);
  175. wx.showToast({
  176. title: '下载文件失败,检查下手机内存吧',
  177. });
  178. console.error('文件移动失败:', err);
  179. }
  180. });
  181. } else {
  182. _this.setOtaStatus(4);
  183. wx.showToast({
  184. title: '文件下载失败',
  185. });
  186. }
  187. },
  188. fail: (err) => {
  189. _this.setOtaStatus(4);
  190. wx.showToast({
  191. title: '文件下载失败',
  192. });
  193. console.error('下载文件失败:', err);
  194. }
  195. });
  196. },
  197. readFile(localFilePath) {
  198. if (!localFilePath) {
  199. wx.showToast({
  200. title: '下载文件为空',
  201. });
  202. return;
  203. }
  204. wx.showLoading({
  205. title: '开始传输文件',
  206. })
  207. const fs = wx.getFileSystemManager();
  208. let _this = this;
  209. fs.readFile({
  210. filePath: localFilePath,
  211. encoding: 'base64', // 可以选择 'utf8' 或 'base64'
  212. success: (res) => {
  213. // const btHelper = BtHelper.getInstance();
  214. // btHelper.ota(localFilePath);
  215. console.log('文件内容:', res.data.length);
  216. _this.startSend(res.data);
  217. },
  218. fail: (err) => {
  219. wx.showToast({
  220. title: 'OTA文件下载失败',
  221. });
  222. console.error('文件读取失败:', err);
  223. _this.setOtaStatus(4);
  224. }
  225. });
  226. },
  227. startSend(data) {
  228. // const chunkSize = 20; // 每次发送的数据块大小
  229. // const chunks = [];
  230. // for (let i = 0; i < data.length; i += chunkSize) {
  231. // chunks.push(data.slice(i, i + chunkSize));
  232. // }
  233. // this.data._chunks = chunks;
  234. this.sendOtaCmd(1)
  235. // this.startSendOtaData()
  236. },
  237. startSendOtaData() {
  238. let _this = this
  239. let length = this.data._chunks.length
  240. if (length === 0) {
  241. wx.showToast({
  242. title: '下载文件失败',
  243. icon: 'none'
  244. })
  245. _this.sendOtaCmd(2)
  246. return;
  247. }
  248. _this.sendOtaData(this.data._chunks, 0)
  249. // }
  250. }, sendOtaData(imageBuffer, index) {
  251. let _this = this
  252. if (index >= imageBuffer.length) {
  253. _this.sendOtaCmd(0)
  254. return;
  255. }
  256. let chunkSize = 20;
  257. let total = imageBuffer.length
  258. let next = index + chunkSize;
  259. const chunk = imageBuffer.slice(index, next);
  260. // todo 改成设备wifi下载
  261. // BtHelper.getInstance().wallPaperData(chunk, function (res) {
  262. // console.log("发送OTA数据:", next)
  263. // if (res) {
  264. // _this.updateProgress(next, total);
  265. // _this.sendOtaData(imageBuffer)
  266. // } else {
  267. // wx.showModal({
  268. // title: 'OTA升级失败了',
  269. // showCancel: false
  270. // })
  271. // }
  272. // })
  273. },
  274. sendOtaCmd(value) {
  275. BtHelper.getInstance().otaCmd(value)
  276. if (value == 0) {
  277. this.setOtaStatus(0);
  278. } else {
  279. // this.setOtaStatus(4);
  280. }
  281. },
  282. startProgress() {
  283. this.setData({
  284. progress: 0,
  285. showProgress: true
  286. })
  287. },
  288. updateProgress(chunk, total) {
  289. let progress = chunk / total * 100;
  290. let _this = this
  291. if (progress >= 100) {
  292. _this.setData({
  293. progress: 0,
  294. showProgress: false,
  295. // showCropImg: false
  296. });
  297. wx.showToast({
  298. title: '图片上传成功',
  299. })
  300. } else {
  301. _this.setData({
  302. progress: progress,
  303. });
  304. }
  305. },
  306. otaSuccess() {
  307. wx.hideLoading();
  308. // 流程成功
  309. wx.showModal({
  310. title: '设备开始升级中',
  311. showCancel: false,
  312. success: function (res) {
  313. if (res.confirm) {
  314. wx.navigateBack({
  315. delta: 3
  316. })
  317. }
  318. }
  319. })
  320. },
  321. otaFailure() {
  322. wx.hideLoading();
  323. // 流程失败
  324. wx.showModal({
  325. title: '设备升级失败了',
  326. showCancel: false,
  327. success: function (res) {
  328. if (res.confirm) {
  329. wx.navigateBack({
  330. delta: 3
  331. })
  332. }
  333. }
  334. })
  335. },
  336. addNotification() {
  337. let _this = this;
  338. EventManager.addNotification(CmdEvent.eventName, function (event) {
  339. let name = event.cmdEvent;
  340. let kind = event.heiJiaoKind;
  341. // 74,1,1是wifi连接成功,74,0 2连接wifi失败/没有wifi。
  342. // 发送url给你后,你回:74 0 1是流程成功,74 0 3是流程失败。
  343. let otaCmd = event.otaCmd;
  344. console.log("OTA页:", otaCmd, kind, name)
  345. switch (name) {
  346. case EnumCmdEvent.otaCmd:
  347. if (otaCmd === 1 && kind == 1) {
  348. wx.hideLoading();
  349. // 设备收到开启OTA的回复,发送url
  350. _this.sendUrlData()
  351. } else if (otaCmd === 0 && kind == 2) {
  352. wx.hideLoading();
  353. // 去设置wifi界面
  354. _this.goToWifi()
  355. } else if (otaCmd === 0 && kind == 3) {
  356. _this.otaFailure()
  357. } else if (otaCmd === 0 && kind == 1) {
  358. _this.otaSuccess()
  359. }
  360. else if (kind == 0) {
  361. }
  362. break;
  363. case EnumCmdEvent.otaUrl:
  364. // 小程序发:0x75, 0
  365. if (otaCmd === 1 && kind == 1) {
  366. // _this.sendOtaCmd(0)
  367. _this.otaSuccess()
  368. // BtHelper.getInstance().otaUrl(BtCmd.stringToUint8Array(_this.data.otaData.url))
  369. } else {
  370. _this.otaFailure()
  371. }
  372. break;
  373. case EnumCmdEvent.otaWifi:
  374. //0x76
  375. wx.hideLoading()
  376. // 0x76, 发送wifi成功
  377. if (otaCmd === 1 && kind == 1) {
  378. _this.sendUrlData()
  379. } else {
  380. // wifi失败
  381. _this.otaFailure()
  382. }
  383. break;
  384. }
  385. }, _this)
  386. },
  387. /**
  388. * 生命周期函数--监听页面加载
  389. */
  390. onLoad(options) {
  391. let param = JSON.parse(options.param)
  392. console.log("OTA界面:", param)
  393. let otaData = param.otaData
  394. let device = param.device
  395. this.setData({
  396. otaData: otaData,
  397. device: device,
  398. isShowOta: device.clientType === 'MW-S2(BLE)' || device.clientType === 'MW-S2'
  399. })
  400. this.addNotification()
  401. },
  402. onUnload() {
  403. EventManager.removeNotification(CmdEvent.eventName, this)
  404. },
  405. })