ota.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. this.startProgress();
  52. break;
  53. case 4:
  54. this.setData({
  55. otaStatus: 4,
  56. buttonTips: "更新失败,再试一次"
  57. });
  58. break;
  59. default:
  60. break;
  61. }
  62. },
  63. startOtaTap() {
  64. let connect = getApp().globalData.device.connect ?? false
  65. if (!connect) {
  66. wx.showToast({
  67. title: '设备已经掉线了',
  68. })
  69. return;
  70. }
  71. let hasNewVersion = this.data.otaData.hasNewVersion ?? false
  72. if (!hasNewVersion) {
  73. wx.showToast({
  74. title: '没有可升级的固件',
  75. })
  76. return;
  77. }
  78. let _this = this
  79. wx.showModal({
  80. title: '有新固件可升级',
  81. content: _this.otaData.content ?? '提升体验,操作更流畅',
  82. cancelText: '以后再说',
  83. confirmText: '升级',
  84. complete: (res) => {
  85. if (res.cancel) {
  86. }
  87. if (res.confirm) {
  88. _this.downloadFile(_this.otaData.url)
  89. }
  90. }
  91. })
  92. // const btHelper = BtHelper.getInstance();
  93. // btHelper.ota(this.data.otaData.file_url)
  94. },
  95. downloadFile(url) {
  96. urlPath = updateData.url ?? "";
  97. content = updateData.content ?? "暂无更新";
  98. var list = urlPath.split("/");
  99. // let cachePath = await PathUtil.getDownloadPath(fileName: list.last ?? 'OTAUpgrade.OTA');
  100. const fs = wx.getFileSystemManager();
  101. // 构建本地文件路径
  102. const localFilePath = list[list.length - 1];
  103. let _this = this;
  104. // 判断文件是否存在
  105. fs.access(localFilePath, (err) => {
  106. if (err) {
  107. // 文件不存在,开始下载
  108. wx.downloadFile({
  109. url: url, // 下载资源的URL
  110. success: (res) => {
  111. if (res.statusCode === 200) {
  112. // 下载成功,保存文件路径
  113. const filePath = res.tempFilePath;
  114. // 将下载的文件移动到本地路径
  115. fs.rename({
  116. oldPath: filePath,
  117. newPath: localFilePath,
  118. success: () => {
  119. // this.setData({
  120. // localFilePath: localFilePath
  121. // });
  122. // 调用 OTA 更新方法
  123. // const btHelper = BtHelper.getInstance();
  124. // btHelper.ota(localFilePath);
  125. wx.showToast({
  126. title: '文件下载成功',
  127. });
  128. _this.readFile(localFilePath);
  129. },
  130. fail: (err) => {
  131. wx.showToast({
  132. title: '文件移动失败',
  133. });
  134. console.error('文件移动失败:', err);
  135. }
  136. });
  137. } else {
  138. wx.showToast({
  139. title: '文件下载失败',
  140. });
  141. }
  142. },
  143. fail: (err) => {
  144. wx.showToast({
  145. title: '文件下载失败',
  146. });
  147. console.error('下载文件失败:', err);
  148. }
  149. });
  150. } else {
  151. _this.readFile(localFilePath);
  152. wx.showToast({
  153. title: '文件已存在,跳过下载',
  154. });
  155. }
  156. });
  157. },
  158. readFile(localFilePath) {
  159. if (!localFilePath) {
  160. wx.showToast({
  161. title: '下载文件为空',
  162. });
  163. return;
  164. }
  165. this.setOtaStatus(2);
  166. const fs = wx.getFileSystemManager();
  167. let _this = this;
  168. fs.readFile({
  169. filePath: localFilePath,
  170. encoding: 'base64', // 可以选择 'utf8' 或 'base64'
  171. success: (res) => {
  172. // const btHelper = BtHelper.getInstance();
  173. // btHelper.ota(localFilePath);
  174. // console.log('文件内容:', res.data);
  175. _this.startSend();
  176. },
  177. fail: (err) => {
  178. wx.showToast({
  179. title: 'OTA文件下载失败',
  180. });
  181. console.error('文件读取失败:', err);
  182. }
  183. });
  184. },
  185. startSend() {
  186. const chunkSize = 20; // 每次发送的数据块大小
  187. const chunks = [];
  188. for (let i = 0; i < data.length; i += chunkSize) {
  189. chunks.push(data.slice(i, i + chunkSize));
  190. }
  191. this.data._chunks = chunks;
  192. const btHelper = BtHelper.getInstance();
  193. btHelper.otaCmd(1)
  194. },
  195. sendOtaData() {
  196. const btHelper = BtHelper.getInstance();
  197. this.data._chunks.forEach(element => {
  198. btHelper.otaData(element)
  199. });
  200. this.startProgress()
  201. },
  202. endEnd() {
  203. const btHelper = BtHelper.getInstance();
  204. btHelper.otaCmd(0)
  205. },
  206. updateProgress(newProgress) {
  207. this.setData({
  208. progress: newProgress,
  209. });
  210. },
  211. // 示例:模拟进度更新
  212. simulateProgressUpdate() {
  213. let currentProgress = 0;
  214. const interval = setInterval(() => {
  215. if (currentProgress >= 100) {
  216. clearInterval(interval);
  217. wx.showToast({
  218. title: '升级完成',
  219. });
  220. } else {
  221. currentProgress += 10;
  222. this.updateProgress(currentProgress);
  223. }
  224. }, 500);
  225. },
  226. startProgress: function () {
  227. const _this = this;
  228. let progress = 0;
  229. // 定时器,每100毫秒执行一次
  230. _this._timer = setInterval(function () {
  231. if (progress >= 100) {
  232. clearInterval(_this._timer); // 停止定时器
  233. _this.setData({
  234. progress: 0,
  235. progressPercent: 0,
  236. showProgress: false,
  237. });
  238. _this.data.otaData.hasNewVersion = false;
  239. _this.setOtaStatus(0)
  240. wx.showToast({
  241. title: '数据上传成功',
  242. })
  243. } else {
  244. progress += 1; // 每次增加1%
  245. let ptl = (progress / 2) + "%"
  246. _this.setData({
  247. progress: progress,
  248. progressPercent: progress,
  249. progressTextLeft:ptl // 文字位置跟随进度
  250. });
  251. }
  252. }, 100);
  253. },
  254. /**
  255. * 生命周期函数--监听页面加载
  256. */
  257. onLoad(options) {
  258. let param = JSON.parse(options.param)
  259. console.log(param)
  260. let otaData = param.otaData
  261. let device = param.device
  262. device.clientType = device.clientType ?? device.ProdModel
  263. this.setData({
  264. otaData: otaData,
  265. device: device
  266. })
  267. let _this = this;
  268. EventManager.addNotification(CmdEvent.eventName, function (event) {
  269. let name = event.name;
  270. console.log("OTA页:", event)
  271. switch (name) {
  272. case EnumCmdEvent.otaCmd:
  273. let otaCmd = event.otaCmd;
  274. let kind = event.kind;
  275. if (otaCmd === 1 && kind == 1) {
  276. // 开始发送
  277. _this.sendOtaData()
  278. } else if (otaCmd === 0 && kind == 1) {
  279. // 发送结束
  280. _this.endEnd()
  281. }
  282. break;
  283. }
  284. }, this)
  285. },
  286. })