From 11b53e7531e7b2402f9a996c4e5af00bf1fdac41 Mon Sep 17 00:00:00 2001 From: kerms Date: Thu, 6 Jun 2024 11:54:03 +0800 Subject: [PATCH] feat(ota): add ota page and api, add direct link update --- src/App.vue | 5 +- src/api/apiOTA.ts | 61 ++++++++ src/api/apiSystem.ts | 32 +++++ src/api/index.ts | 2 + src/composables/useSystemModule.ts | 46 +++++++ src/locales/zh.ts | 1 + src/router/index.ts | 8 +- src/stores/useSystemStore.ts | 23 ++++ src/views/About.vue | 10 +- src/views/Update.vue | 214 +++++++++++++++++++++++++++++ src/views/navigation/NavBar.vue | 26 +++- 11 files changed, 416 insertions(+), 12 deletions(-) create mode 100644 src/api/apiOTA.ts create mode 100644 src/api/apiSystem.ts create mode 100644 src/composables/useSystemModule.ts create mode 100644 src/stores/useSystemStore.ts create mode 100644 src/views/Update.vue diff --git a/src/App.vue b/src/App.vue index 8262b80..e0eca93 100644 --- a/src/App.vue +++ b/src/App.vue @@ -12,6 +12,7 @@ import {routeCtrlMsg, routeModuleServerMsg} from "@/router/msgRouter"; import {globalNotify} from "@/composables/notification"; import {isDevMode} from "@/composables/buildMode"; import {ElMessageBox} from "element-plus"; +import {useSystemModule} from "@/composables/useSystemModule"; const wsState = useWsStore(); @@ -48,9 +49,7 @@ onMounted(() => { websocketService = getWebsocketService(); websocketService.init(host, onServerMsg, onClientCtrl); changeFavicon(); - ElMessageBox.alert('欢迎参与允斯无线串口助手固件内侧,有任何问题请在Q群踢群主:642246000', '2024-05-24', { - confirmButtonText: '好的', - }) + useSystemModule(); }); onUnmounted(() => { diff --git a/src/api/apiOTA.ts b/src/api/apiOTA.ts new file mode 100644 index 0000000..9004545 --- /dev/null +++ b/src/api/apiOTA.ts @@ -0,0 +1,61 @@ +import {type ApiJsonMsg, sendJsonMsg, WtModuleID} from '@/api' + +export enum WtOTACmd { + WT_OTA_GET_UPDATE_INFO = 1, /* total_size, ver */ + WT_OTA_DO_UPDATE = 2, /* returns OK, chunk of remaining bytes and total length -> wt_event_manager */ + WT_OTA_GET_PROGRESS = 3, /* returns chunk of remaining bytes and total length */ + WT_OTA_DO_URL_UPDATE = 4, /* force update { url: "https://" } */ +} + +export enum WtOTAProgressStatus { + OK = "OK", + IDLE = "IDLE", + IN_PROGRESS = "IN_PROGRESS", + FAILED = "FAILED", +} + +export interface IOTAProgress extends ApiJsonMsg { + progress: number; + total_size: number; + status: string; +} + +export interface IOTAFmInfo extends ApiJsonMsg { + fm_size: number; + fm_ver: string; + upd_date: string; + upd_note: string; +} + +export function wt_ota_get_update_info() { + const msg: ApiJsonMsg = { + module: WtModuleID.OTA, + cmd: WtOTACmd.WT_OTA_GET_UPDATE_INFO, + }; + sendJsonMsg(msg); +} + +export function wt_ota_do_update() { + const msg: ApiJsonMsg = { + module: WtModuleID.OTA, + cmd: WtOTACmd.WT_OTA_DO_UPDATE, + }; + sendJsonMsg(msg); +} + +export function wt_ota_get_progress() { + const msg: ApiJsonMsg = { + module: WtModuleID.OTA, + cmd: WtOTACmd.WT_OTA_GET_PROGRESS, + }; + sendJsonMsg(msg); +} + +export function wt_ota_do_url_update(url: string) { + const msg: ApiJsonMsg & {url: string} = { + module: WtModuleID.OTA, + cmd: WtOTACmd.WT_OTA_DO_URL_UPDATE, + url: url, + }; + sendJsonMsg(msg); +} diff --git a/src/api/apiSystem.ts b/src/api/apiSystem.ts new file mode 100644 index 0000000..c1e8692 --- /dev/null +++ b/src/api/apiSystem.ts @@ -0,0 +1,32 @@ +import {type ApiJsonMsg, sendJsonMsg, WtModuleID} from '@/api' + +export enum WtSytemCmd { + WT_SYS_GET_FM_INFO = 1, + WT_SYS_REBOOT = 2, +} + +export interface ISysFmInfo extends ApiJsonMsg { + fm_ver: string; + upd_date: string; +} + +export interface ISysHwInfo extends ApiJsonMsg { + hw_ver: string; + mf_date: string; +} + +export function wt_sys_get_fm_info() { + const msg: ApiJsonMsg = { + module: WtModuleID.SYSTEM, + cmd: WtSytemCmd.WT_SYS_GET_FM_INFO, + }; + sendJsonMsg(msg); +} + +export function wt_sys_reboot() { + const msg: ApiJsonMsg = { + module: WtModuleID.SYSTEM, + cmd: WtSytemCmd.WT_SYS_REBOOT, + }; + sendJsonMsg(msg); +} diff --git a/src/api/index.ts b/src/api/index.ts index be2fa89..840a092 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -34,6 +34,8 @@ export enum WtModuleID { WIFI = 1, DATA_FLOW = 2, UART = 4, + OTA = 5, + SYSTEM = 6, } export function sendJsonMsg(apiJsonMsg: ApiJsonMsg) { diff --git a/src/composables/useSystemModule.ts b/src/composables/useSystemModule.ts new file mode 100644 index 0000000..88c09b1 --- /dev/null +++ b/src/composables/useSystemModule.ts @@ -0,0 +1,46 @@ +import {useSystemStore} from "@/stores/useSystemStore"; +import {registerModule} from "@/router/msgRouter"; +import {type ApiJsonMsg, ControlEvent, type ControlMsg, ControlMsgType, WtModuleID} from "@/api"; +import {type ISysFmInfo, wt_sys_get_fm_info, WtSytemCmd} from "@/api/apiSystem"; + + +export function useSystemModule() { + const sysStore = useSystemStore() + + function onClientCtrl(msg: ControlMsg) { + if (msg.type !== ControlMsgType.WS_EVENT) { + return + } + + if (msg.data === ControlEvent.CONNECTED) { + wt_sys_get_fm_info(); + sysStore.rebootInProgress = false; + } + } + + function onClientMsg(msg: ApiJsonMsg) { + switch (msg.cmd as WtSytemCmd) { + case WtSytemCmd.WT_SYS_REBOOT: + sysStore.rebootInProgress = true; + break; + case WtSytemCmd.WT_SYS_GET_FM_INFO: { + const fm_info = msg as ISysFmInfo; + sysStore.curFmInfo.date = fm_info.upd_date; + sysStore.curFmInfo.ver = fm_info.fm_ver; + break; + } + } + + console.log(msg); + } + + registerModule(WtModuleID.SYSTEM, { + ctrlCallback: onClientCtrl, + serverJsonMsgCallback: onClientMsg, + serverBinMsgCallback: () => {}, + }); +} + + + + diff --git a/src/locales/zh.ts b/src/locales/zh.ts index 17be1d1..d34f7f0 100644 --- a/src/locales/zh.ts +++ b/src/locales/zh.ts @@ -16,5 +16,6 @@ export default { uart: "UART透传", feedback: "反馈", close: "关闭", + update: "更新", }, } \ No newline at end of file diff --git a/src/router/index.ts b/src/router/index.ts index 805a54d..4101877 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -5,6 +5,7 @@ import Feedback from '@/views/Feedback.vue' import About from '@/views/About.vue' import Uart from '@/views/Uart.vue' import Page404 from '@/views/404.vue' +import Update from '@/views/Update.vue' import {translate} from "@/locales"; @@ -38,7 +39,12 @@ const router = createRouter({ meta: {title: translate('page.feedback')}, name: 'feedback', component: Feedback, - }, { + }, { + path: '/update:ext(.*)', + meta: {title: translate('page.update')}, + name: 'update', + component: Update, + }, { path: '/:catchAll(.*)', // This will match all paths that aren't matched by above routes name: 'NotFound', component: Page404, diff --git a/src/stores/useSystemStore.ts b/src/stores/useSystemStore.ts new file mode 100644 index 0000000..9fe58a2 --- /dev/null +++ b/src/stores/useSystemStore.ts @@ -0,0 +1,23 @@ +import {defineStore} from "pinia"; +import {ref} from "vue"; + +export const useSystemStore = defineStore('system', () => { + + const curFmInfo = ref({ + ver: "-", + date: "-", + }); + + const hwInfo = ref({ + ver: "-", + date: "-", + }) + + const rebootInProgress = ref(false); + + return { + curFmInfo, + hwInfo, + rebootInProgress, + } +}); diff --git a/src/views/About.vue b/src/views/About.vue index 1b1d149..7146bd9 100644 --- a/src/views/About.vue +++ b/src/views/About.vue @@ -1,6 +1,9 @@