diff --git a/src/App.vue b/src/App.vue index dc26df1..408c114 100644 --- a/src/App.vue +++ b/src/App.vue @@ -13,6 +13,7 @@ import {globalNotify} from "@/composables/notification"; import {isDevMode} from "@/composables/buildMode"; import {useSystemModule} from "@/composables/useSystemModule"; import {useDataFlowModule} from "@/composables/useDataFlowModule"; +import {useUpdateModule} from "@/composables/useUpdateModule"; const wsState = useWsStore(); @@ -52,6 +53,7 @@ onMounted(() => { useSystemModule(); useDataFlowModule(); + useUpdateModule(); }); onUnmounted(() => { diff --git a/src/composables/useUpdateModule.ts b/src/composables/useUpdateModule.ts new file mode 100644 index 0000000..350e06f --- /dev/null +++ b/src/composables/useUpdateModule.ts @@ -0,0 +1,99 @@ +import {registerModule} from "@/router/msgRouter"; +import {type ApiJsonMsg, ControlEvent, type ControlMsg, ControlMsgType, WtModuleID} from "@/api"; +import {useUpdateStore} from "@/stores/useUpdateStore"; +import { + type IOTAFmInfo, + type IOTAProgress, + WtOTACmd, + WtOTAProgressStatus, + wt_ota_get_progress, + wt_ota_get_update_info, +} from "@/api/apiOTA"; +import {isDevMode} from "@/composables/buildMode"; +import {useSystemStore} from "@/stores/useSystemStore"; + +export function useUpdateModule() { + const updateStore = useUpdateStore() + const sysStore = useSystemStore() + + function onClientCtrl(msg: ControlMsg) { + if (msg.type !== ControlMsgType.WS_EVENT) { + return + } + + if (msg.data === ControlEvent.CONNECTED) { + wt_ota_get_update_info(); + wt_ota_get_progress(); + } + } + + function onClientMsg(msg: ApiJsonMsg) { + switch (msg.cmd as WtOTACmd) { + case WtOTACmd.WT_OTA_GET_UPDATE_INFO: { + const info = msg as IOTAFmInfo; + Object.assign(updateStore.newFmInfo, info); + if (updateStore.newFmInfo.fm_ver !== sysStore.curFmInfo.ver && updateStore.newFmInfo.fm_ver[0] !== '-' + && (updateStore.updateStatus === 'IDLE' || updateStore.updateStatus === 'FAILED')) { + updateStore.canUpdate = true; + } else { + updateStore.canUpdate = false; + } + break; + } + case WtOTACmd.WT_OTA_DO_UPDATE: + break; + case WtOTACmd.WT_OTA_GET_PROGRESS: { + const progress = msg as IOTAProgress; + updateStore.updateStatus = progress.status; + if (progress.total_size !== 0) { + updateStore.updateProgress = (progress.progress / progress.total_size) * 100; + } else { + updateStore.updateProgress = 0; + } + if (progress.status === WtOTAProgressStatus.IDLE) { + if (updateStore.newFmInfo.fm_ver !== sysStore.curFmInfo.ver && updateStore.newFmInfo.fm_ver[0] !== '-') { + updateStore.canUpdate = true; + } else { + updateStore.canUpdate = false; + } + updateStore.clearProgressInterval(); + updateStore.progressBarStatus = ''; + } else if (progress.status === WtOTAProgressStatus.FAILED) { + if (updateStore.newFmInfo.fm_ver !== sysStore.curFmInfo.ver && updateStore.newFmInfo.fm_ver[0] !== '-') { + updateStore.canUpdate = true; + } else { + updateStore.canUpdate = false; + } + updateStore.clearProgressInterval(); + updateStore.progressBarStatus = 'exception'; + } else if (progress.status === WtOTAProgressStatus.IN_PROGRESS) { + updateStore.setProgressInterval(); + updateStore.progressBarStatus = ''; + updateStore.canUpdate = false; + } else if (progress.status === WtOTAProgressStatus.OK) { + updateStore.clearProgressInterval(); + updateStore.canUpdate = false; + updateStore.progressBarStatus = 'success'; + } + break; + } + default: + break; + } + + if (isDevMode()) { + console.log(msg); + } + } + + registerModule(WtModuleID.OTA, { + ctrlCallback: onClientCtrl, + serverJsonMsgCallback: onClientMsg, + serverBinMsgCallback: () => { + }, + }); +} + + + + diff --git a/src/stores/useUpdateStore.ts b/src/stores/useUpdateStore.ts new file mode 100644 index 0000000..fed62ca --- /dev/null +++ b/src/stores/useUpdateStore.ts @@ -0,0 +1,45 @@ +import { ref } from 'vue' +import { defineStore } from 'pinia' +import {wt_ota_get_progress} from "@/api/apiOTA"; + +export const useUpdateStore = defineStore('update', () => { + const canUpdate = ref(false); + const updateProgress = ref(0); + const updateStatus = ref(''); + + const progressBarStatus = ref(''); + + let progressIntervalID = -1; + + const newFmInfo = ref({ + fm_size: 0, + fm_ver: "-", + upd_date: "-", + upd_note: "-", + }) + + function setProgressInterval() { + if (progressIntervalID < 0) { + progressIntervalID = setInterval(() => { + wt_ota_get_progress(); + }, 1000); + } + } + + function clearProgressInterval() { + if (progressIntervalID >= 0) { + clearInterval(progressIntervalID); + progressIntervalID = -1; + } + } + + return { + canUpdate, + updateProgress, + updateStatus, + progressBarStatus, + newFmInfo, + setProgressInterval, + clearProgressInterval, + } +}) diff --git a/src/views/Update.vue b/src/views/Update.vue index 7c5117f..fc900d6 100644 --- a/src/views/Update.vue +++ b/src/views/Update.vue @@ -26,21 +26,21 @@ - + 更新 - {{ newFmInfo.fm_ver }} - {{ newFmInfo.upd_date }} - {{ newFmInfo.fm_size }} + {{ updateStore.newFmInfo.fm_ver }} + {{ updateStore.newFmInfo.upd_date }} + {{ updateStore.newFmInfo.fm_size }} - - + + -
{{ newFmInfo.upd_note }}
+
{{ updateStore.newFmInfo.upd_note }}
@@ -57,121 +57,25 @@ -