ota.js 11 KB

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