From 7ba90ae7afe538377b6cfdc6fb4f5675ed9dc378 Mon Sep 17 00:00:00 2001 From: josc146 Date: Fri, 19 May 2023 15:59:04 +0800 Subject: [PATCH] detect status --- backend-python/global_var.py | 6 +++--- backend-python/routes/config.py | 5 +++++ frontend/src/apis/index.ts | 16 +++++++++++++++- frontend/src/startup.ts | 5 +++++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/backend-python/global_var.py b/backend-python/global_var.py index 9eee59c..546880d 100644 --- a/backend-python/global_var.py +++ b/backend-python/global_var.py @@ -6,9 +6,9 @@ Model_Config = "model_config" class ModelStatus(Enum): - Offline = auto() - Loading = auto() - Working = auto() + Offline = 0 + Loading = 2 + Working = 3 def init(): diff --git a/backend-python/routes/config.py b/backend-python/routes/config.py index f4bec31..bd7a18b 100644 --- a/backend-python/routes/config.py +++ b/backend-python/routes/config.py @@ -59,3 +59,8 @@ def update_config(body: ModelConfigBody): global_var.set(global_var.Model_Config, body) return "success" + + +@router.get("/status") +def status(): + return {"status": global_var.get(global_var.Model_Status)} diff --git a/frontend/src/apis/index.ts b/frontend/src/apis/index.ts index 83a384c..72d0c1a 100644 --- a/frontend/src/apis/index.ts +++ b/frontend/src/apis/index.ts @@ -1,4 +1,4 @@ -import commonStore from '../stores/commonStore'; +import commonStore, {ModelStatus} from '../stores/commonStore'; export const readRoot = async () => { const port = commonStore.getCurrentModelConfig().apiParameters.apiPort; @@ -35,3 +35,17 @@ export const updateConfig = async (body: any) => { body: JSON.stringify(body) }); }; + +export const getStatus = async (timeout?: number): Promise => { + const controller = new AbortController(); + if (timeout) + setTimeout(() => controller.abort(), timeout); + + const port = commonStore.getCurrentModelConfig().apiParameters.apiPort; + let ret: ModelStatus | undefined; + await fetch(`http://127.0.0.1:${port}/status`, {signal: controller.signal}).then(r => r.json()).then(data => { + ret = data.status; + }).catch(() => { + }); + return ret; +}; diff --git a/frontend/src/startup.ts b/frontend/src/startup.ts index 5565670..133db29 100644 --- a/frontend/src/startup.ts +++ b/frontend/src/startup.ts @@ -1,11 +1,16 @@ import commonStore, {defaultModelConfigs} from './stores/commonStore'; import {ReadJson} from '../wailsjs/go/backend_golang/App'; import {downloadProgramFiles, LocalConfig, refreshModels} from './utils'; +import {getStatus} from './apis'; export async function startup() { downloadProgramFiles(); initCache(); await initConfig(); + getStatus(500).then(status => { + if (status) + commonStore.setModelStatus(status); + }); } async function initConfig() {