diff --git a/src/drawApp/lineApp.ts b/src/drawApp/lineApp.ts index 1eca009..2decdbf 100644 --- a/src/drawApp/lineApp.ts +++ b/src/drawApp/lineApp.ts @@ -87,7 +87,7 @@ export function destroyLineApp(): void { } } -export function initLineApp(lineId: number): IGraphicApp { +export function initLineApp(): IGraphicApp { if (lineApp) return lineApp; const lineAppDataLoader: IGraphicAppConfig['dataLoader'] = async () => { const lineStore = useLineStore(); @@ -186,6 +186,65 @@ export function initLineApp(lineId: number): IGraphicApp { token: getJwtToken() as string, }); + let msgNotify: null | ((props?: QNotifyUpdateOptions | undefined) => void) = + null; + lineApp.on('websocket-connect-state-change', (connected) => { + if (!connected && !msgNotify) { + msgNotify = Notify.create({ + type: 'negative', + timeout: 0, + position: 'top-right', + message: '通信链接已断开!', + }); + } else if (msgNotify && connected) { + msgNotify(); + msgNotify = null; + } + }); + + lineApp.reload().then(() => { + if (!lineApp) return; + const quickJumpMenu = new ContextMenu({ + name: '快捷跳转', + groups: [ + { + items: lineApp.queryStore + .queryByType(Station.Type) + .map((station) => ({ + name: station.datas.name ?? '', + handler: () => { + lineApp?.makeGraphicCenterShow(station); + }, + })), + }, + ], + }); + lineApp.registerMenu(quickJumpMenu); + lineApp.canvas.on('_rightclick', (e) => { + quickJumpMenu.open(e.global); + }); + + const axleCountings = lineApp.queryStore.queryByType( + AxleCounting.Type + ); + axleCountings.forEach((axleCounting) => { + axleCounting.visible = false; + }); + const trainWindows = lineApp.queryStore.queryByType( + TrainWindow.Type + ); + trainWindows.forEach((trainWindow) => { + trainWindow.visible = false; + }); + handleSubscribe(lineApp); + }); + + return lineApp; +} + +function handleSubscribe(lineApp: IGraphicApp) { + const lineStore = useLineStore(); + const lineId = lineStore.lineId; lineApp.subscribe({ destination: `/queue/line/${lineId}/device`, messageConverter: (message: Uint8Array) => { @@ -235,21 +294,6 @@ export function initLineApp(lineId: number): IGraphicApp { return states; }, }); - let msgNotify: null | ((props?: QNotifyUpdateOptions | undefined) => void) = - null; - lineApp.on('websocket-connect-state-change', (connected) => { - if (!connected && !msgNotify) { - msgNotify = Notify.create({ - type: 'negative', - timeout: 0, - position: 'top-right', - message: '通信链接已断开!', - }); - } else if (msgNotify && connected) { - msgNotify(); - msgNotify = null; - } - }); const lineNetStore = useLineNetStore(); lineApp.subscribe({ destination: '/queue/xian/ncc/alert', @@ -258,42 +302,4 @@ export function initLineApp(lineId: number): IGraphicApp { lineNetStore.setAlarmInfo(storage.messages as []); }, }); - - lineApp.reload().then(() => { - if (!lineApp) return; - const quickJumpMenu = new ContextMenu({ - name: '快捷跳转', - groups: [ - { - items: lineApp.queryStore - .queryByType(Station.Type) - .map((station) => ({ - name: station.datas.name ?? '', - handler: () => { - lineApp?.makeGraphicCenterShow(station); - }, - })), - }, - ], - }); - lineApp.registerMenu(quickJumpMenu); - lineApp.canvas.on('_rightclick', (e) => { - quickJumpMenu.open(e.global); - }); - - const axleCountings = lineApp.queryStore.queryByType( - AxleCounting.Type - ); - axleCountings.forEach((axleCounting) => { - axleCounting.visible = false; - }); - const trainWindows = lineApp.queryStore.queryByType( - TrainWindow.Type - ); - trainWindows.forEach((trainWindow) => { - trainWindow.visible = false; - }); - }); - - return lineApp; } diff --git a/src/stores/line-store.ts b/src/stores/line-store.ts index a58caa7..2572c55 100644 --- a/src/stores/line-store.ts +++ b/src/stores/line-store.ts @@ -39,7 +39,7 @@ export const useLineStore = defineStore('line', { }, initLineApp(lineId: number) { this.setLineId(lineId); - const app = initLineApp(lineId); + const app = initLineApp(); app.on('graphicselected', (graphics) => { this.selectedGraphics = markRaw(graphics); });