ota.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. case 5:
  59. this.setData({
  60. otaStatus: 4,
  61. buttonTips: "更新完成"
  62. });
  63. break;
  64. default:
  65. break;
  66. }
  67. },
  68. startOtaTap() {
  69. let online = this.data.device.state === "online"
  70. if (!online) {
  71. wx.showToast({
  72. title: '设备已经掉线了',
  73. })
  74. return;
  75. }
  76. let hasNewVersion = this.data.otaData.hasNewVersion ?? false
  77. if (!hasNewVersion) {
  78. wx.showToast({
  79. title: '没有可升级的固件',
  80. })
  81. return;
  82. }
  83. if (this.data.otaStatus != 1 || this.data.otaStatus != 4) {
  84. return;
  85. }
  86. let _this = this
  87. wx.showModal({
  88. title: '有新固件可升级',
  89. content: _this.data.otaData.content ?? '提升体验,操作更流畅',
  90. cancelText: '以后再说',
  91. confirmText: '升级',
  92. complete: (res) => {
  93. if (res.cancel) {
  94. }
  95. if (res.confirm) {
  96. _this.startDownloadFile()
  97. // _this.downloadOtaFile(_this.data.otaData.url ?? "")
  98. }
  99. }
  100. })
  101. // const btHelper = BtHelper.getInstance();
  102. // btHelper.ota(this.data.otaData.file_url)
  103. },
  104. sendWiFiInfo(wifiName, pwd) {
  105. // [0x22, (wifiList.length + pwdList.length + 6), 0x33, (wifiList.length), (wifiList), 0x44, (pwdList)];
  106. let result = [];
  107. // 字母*6 +
  108. let wifiList = this.string2ListInt(wifiName);
  109. // 数字*3 +
  110. let pwdList = this.string2ListInt(pwd);
  111. // 16进制
  112. result.push(0x22);
  113. result.push(this.int2Hex(wifiList.length + pwdList.length + 6));
  114. // 账号
  115. result.push(0x33);
  116. result.push(this.int2Hex(wifiList.length));
  117. let p = result[3] + 4;
  118. let j = 0;
  119. for (let i = 4; i < p; i++) {
  120. result.splice(i, 0, wifiList[j++]);
  121. }
  122. // 密码
  123. result.splice(p, 0, 0x44);
  124. result.splice(++p, 0, this.int2Hex(pwdList.length));
  125. p++;
  126. j = 0;
  127. // for (let i = p; i < p + pwdList.length; i++) {
  128. // result.splice(i, 0, pwdList[j++]);
  129. // }
  130. // result.push(...pwdList);
  131. result.concat(pwdList)
  132. console.log("发送wifi账号密码:", result.toString());
  133. // _ble.send({ cmd: result });
  134. const btHelper = BtHelper.getInstance();
  135. btHelper.otaData(result)
  136. },
  137. int2Hex(num) {
  138. return parseInt(num, 16);
  139. },
  140. // string转换为List<int>
  141. string2ListInt(text) {
  142. let code = Array.from(text).map(char => char.charCodeAt(0));
  143. console.log("string转换为List<int>", code)
  144. return code
  145. },
  146. startDownloadFile() {
  147. var urlPath = this.data.otaData.url ?? "";
  148. var list = urlPath.split("/");
  149. // let cachePath = await PathUtil.getDownloadPath(fileName: list.last ?? 'OTAUpgrade.OTA');
  150. const fs = wx.getFileSystemManager();
  151. // 构建本地文件路径
  152. var localFilePath = list[list.length - 1];
  153. console.log(list, localFilePath)
  154. let _this = this;
  155. _this.setOtaStatus(2);
  156. let localPath = `${wx.env.USER_DATA_PATH}/` + localFilePath
  157. // 判断文件是否存在
  158. fs.access({
  159. path: localPath,
  160. success(res) {
  161. console.log('文件已存在:', res);
  162. if (res) {
  163. // 有文件
  164. _this.readFile(localPath);
  165. } else {
  166. _this.downloadOtaFile(urlPath, localPath)
  167. }
  168. }, fail(err) {
  169. // 文件不存在或其他错误
  170. console.log(err)
  171. // 文件不存在,开始下载
  172. _this.downloadOtaFile(urlPath, localPath)
  173. }
  174. }
  175. );
  176. },
  177. downloadOtaFile(urlPath, localPath) {
  178. let _this = this;
  179. // const fs = wx.getFileSystemManager();
  180. wx.downloadFile({
  181. url: urlPath, // 下载资源的URL
  182. success: (res) => {
  183. console.log('文件下载成功2', res);
  184. if (res.statusCode === 200) {
  185. // 下载成功,保存文件路径
  186. const filePath = res.tempFilePath;
  187. // _this.readFile(filePath);
  188. // 将下载的文件移动到本地路径
  189. fs.rename({
  190. oldPath: filePath,
  191. newPath: localPath,
  192. success: () => {
  193. wx.showToast({
  194. title: '文件下载成功',
  195. });
  196. _this.readFile(localPath);
  197. },
  198. fail: (err) => {
  199. _this.setOtaStatus(4);
  200. wx.showToast({
  201. title: '下载文件失败,检查下手机内存吧',
  202. });
  203. console.error('文件移动失败:', err);
  204. }
  205. });
  206. } else {
  207. _this.setOtaStatus(4);
  208. wx.showToast({
  209. title: '文件下载失败',
  210. });
  211. }
  212. },
  213. fail: (err) => {
  214. _this.setOtaStatus(4);
  215. wx.showToast({
  216. title: '文件下载失败',
  217. });
  218. console.error('下载文件失败:', err);
  219. }
  220. });
  221. },
  222. readFile(localFilePath) {
  223. if (!localFilePath) {
  224. wx.showToast({
  225. title: '下载文件为空',
  226. });
  227. return;
  228. }
  229. wx.showLoading({
  230. title: '开始传输文件',
  231. })
  232. const fs = wx.getFileSystemManager();
  233. let _this = this;
  234. fs.readFile({
  235. filePath: localFilePath,
  236. encoding: 'base64', // 可以选择 'utf8' 或 'base64'
  237. success: (res) => {
  238. // const btHelper = BtHelper.getInstance();
  239. // btHelper.ota(localFilePath);
  240. console.log('文件内容:', res.data.length);
  241. _this.startSend(res.data);
  242. },
  243. fail: (err) => {
  244. wx.showToast({
  245. title: 'OTA文件下载失败',
  246. });
  247. console.error('文件读取失败:', err);
  248. _this.setOtaStatus(4);
  249. }
  250. });
  251. },
  252. startSend(data) {
  253. const chunkSize = 20; // 每次发送的数据块大小
  254. const chunks = [];
  255. for (let i = 0; i < data.length; i += chunkSize) {
  256. chunks.push(data.slice(i, i + chunkSize));
  257. }
  258. this.data._chunks = chunks;
  259. this.endOta(1)
  260. // this.startSendOtaData()
  261. },
  262. startSendOtaData() {
  263. let _this = this
  264. let length = this.data._chunks.length
  265. if (length === 0) {
  266. wx.showToast({
  267. title: '下载文件失败',
  268. icon: 'none'
  269. })
  270. _this.endOta(2)
  271. return;
  272. }
  273. _this.sendOtaData(this.data._chunks, 0)
  274. // }
  275. }, sendOtaData(imageBuffer, index) {
  276. let _this = this
  277. if (index >= imageBuffer.length) {
  278. _this.endOta(0)
  279. return;
  280. }
  281. let chunkSize = 20;
  282. let total = imageBuffer.length
  283. let next = index + chunkSize;
  284. const chunk = imageBuffer.slice(index, next);
  285. // todo 改成设备wifi下载
  286. // BtHelper.getInstance().wallPaperData(chunk, function (res) {
  287. // console.log("发送OTA数据:", next)
  288. // if (res) {
  289. // _this.updateProgress(next, total);
  290. // _this.sendOtaData(imageBuffer)
  291. // } else {
  292. // wx.showModal({
  293. // title: 'OTA升级失败了',
  294. // showCancel: false
  295. // })
  296. // }
  297. // })
  298. },
  299. endOta(value) {
  300. BtHelper.getInstance().otaCmd(value)
  301. if (value == 0) {
  302. this.setOtaStatus(0);
  303. } else {
  304. this.setOtaStatus(4);
  305. }
  306. },
  307. startProgress() {
  308. this.setData({
  309. progress: 0,
  310. showProgress: true
  311. })
  312. },
  313. updateProgress(chunk, total) {
  314. let progress = chunk / total * 100;
  315. let _this = this
  316. if (progress >= 100) {
  317. _this.setData({
  318. progress: 0,
  319. showProgress: false,
  320. // showCropImg: false
  321. });
  322. wx.showToast({
  323. title: '图片上传成功',
  324. })
  325. } else {
  326. _this.setData({
  327. progress: progress,
  328. });
  329. }
  330. },
  331. /**
  332. * 生命周期函数--监听页面加载
  333. */
  334. onLoad(options) {
  335. let param = JSON.parse(options.param)
  336. console.log("OTA界面:", param)
  337. let otaData = param.otaData
  338. let device = param.device
  339. this.setData({
  340. otaData: otaData,
  341. device: device,
  342. isShowOta: device.clientType === 'MW-S2(BLE)' || device.clientType === 'MW-S2'
  343. })
  344. let _this = this;
  345. EventManager.addNotification(CmdEvent.eventName, function (event) {
  346. let name = event.cmdEvent;
  347. console.log("OTA页0:", name, EnumCmdEvent.otaCmd, EnumCmdEvent.otaCmd === name)
  348. switch (name) {
  349. case EnumCmdEvent.otaCmd:
  350. let otaCmd = event.otaCmd;
  351. let kind = event.heiJiaoKind;
  352. console.log("OTA页:", otaCmd, kind)
  353. if (otaCmd === 1 && kind == 1) {
  354. wx.hideLoading();
  355. // 开始发送
  356. _this.startSendOtaData()
  357. } else if (otaCmd === 0 && kind == 1) {
  358. // 发送结束
  359. _this.endOta(0)
  360. } else if (kind == 0) {
  361. wx.hideLoading()
  362. wx.showModal({
  363. title: 'OTA升级失败了',
  364. showCancel: false
  365. })
  366. }
  367. break;
  368. }
  369. }, this)
  370. },
  371. onUnload() {
  372. EventManager.removeNotification(CmdEvent.eventName, this)
  373. },
  374. })