xian-ncc-da-client/src/drawApp/lineApp.ts

293 lines
9.2 KiB
TypeScript
Raw Normal View History

2023-09-01 14:30:45 +08:00
import {
GraphicData,
GraphicState,
IGraphicApp,
newGraphicApp,
} from 'src/jl-graphic';
2023-07-06 16:43:21 +08:00
import {
TrainData,
TrainOperateInteraction,
TrainState,
} from './graphics/TrainInteraction';
import { Train, TrainTemplate } from 'src/graphics/train/Train';
2023-06-19 16:19:38 +08:00
import {
SignalData,
SignalOperateInteraction,
2023-06-20 16:22:10 +08:00
SignalState,
2023-06-19 16:19:38 +08:00
} from './graphics/SignalInteraction';
2023-06-20 16:22:10 +08:00
import { SignalTemplate, Signal } from 'src/graphics/signal/Signal';
2023-06-21 16:57:24 +08:00
import {
PlatformData,
PlatformOperateInteraction,
PlatformState,
} from './graphics/PlatformInteraction';
import { PlatformTemplate, Platform } from 'src/graphics/platform/Platform';
2023-07-04 17:04:23 +08:00
import {
StationData,
StationOperateInteraction,
StationState,
} from './graphics/StationInteraction';
import { Station, StationTemplate } from 'src/graphics/station/Station';
2023-07-26 13:07:14 +08:00
import {
TurnoutData,
TurnoutOperationPlugin,
TurnoutStates,
} from './graphics/TurnoutInteraction';
import { Turnout, TurnoutTemplate } from 'src/graphics/turnout/Turnout';
2023-06-15 13:12:37 +08:00
import { SectionData } from './graphics/SectionInteraction';
import { SectionTemplate } from 'src/graphics/section/Section';
2023-06-29 17:21:55 +08:00
import { getPublishMapInfoByLineId } from 'src/api/PublishApi';
2023-06-15 13:12:37 +08:00
import { graphicData } from 'src/protos/stationLayoutGraphics';
2023-06-15 15:23:01 +08:00
import { useLineStore } from 'src/stores/line-store';
2023-06-15 13:12:37 +08:00
import { toUint8Array } from 'js-base64';
2023-06-30 09:27:16 +08:00
import { getWebsocketUrl } from 'src/configs/UrlManage';
import { getJwtToken } from 'src/configs/TokenManage';
import { state } from 'src/protos/ws_message';
2023-07-03 16:05:41 +08:00
import {
AxleCounting,
AxleCountingTemplate,
} from 'src/graphics/axleCounting/AxleCounting';
import { AxleCountingData } from './graphics/AxleCountingInteraction';
import {
TrainWindow,
TrainWindowTemplate,
} from 'src/graphics/trainWindow/TrainWindow';
import { TrainWindowData } from './graphics/TrainWindowInteraction';
2023-07-03 16:26:49 +08:00
import { SeparatorTemplate } from 'src/graphics/separator/Separator';
import { SeparatorData } from './graphics/SeparatorInteraction';
2023-07-14 16:32:11 +08:00
import { ContextMenu } from 'src/jl-graphic/ui/ContextMenu';
import { MenuItemOptions } from 'src/jl-graphic/ui/Menu';
2023-07-26 13:07:14 +08:00
import {
LogicSection,
LogicSectionTemplate,
} from 'src/graphics/logicSection/LogicSection';
2023-07-27 15:05:43 +08:00
import {
LogicSectionData,
LogicSectionOperationPlugin,
LogicSectionState,
} from './graphics/LogicSectionInteraction';
2023-09-01 17:14:08 +08:00
import { CanvasData, IGraphicAppConfig } from 'src/jl-graphic/app/JlGraphicApp';
2023-09-05 10:57:30 +08:00
import { Notify, QNotifyUpdateOptions } from 'quasar';
import { useLineNetStore } from 'src/stores/line-net-store';
import { alert } from 'src/protos/alertInfo';
2023-07-14 16:32:11 +08:00
2023-09-01 17:14:08 +08:00
let lineApp: IGraphicApp | null = null;
2023-07-14 16:32:11 +08:00
2023-09-01 14:30:45 +08:00
export function getLineApp() {
2023-06-15 13:12:37 +08:00
return lineApp;
}
export function destroyLineApp(): void {
if (lineApp) {
lineApp.destroy();
2023-09-01 17:14:08 +08:00
lineApp = null;
2023-06-15 13:12:37 +08:00
}
}
2023-09-01 17:14:08 +08:00
export function initLineApp(lineId: number): IGraphicApp {
if (lineApp) return lineApp;
2023-09-01 14:30:45 +08:00
const lineAppDataLoader: IGraphicAppConfig['dataLoader'] = async () => {
const lineStore = useLineStore();
const lineId = lineStore.lineId;
if (!lineId) {
throw Error('请先选择线路');
}
const { proto: base64, name: lineName } = await getPublishMapInfoByLineId(
lineId,
'line'
);
lineStore.setLineName(lineName);
const datas: GraphicData[] = [];
const canvasProperty = new CanvasData();
if (base64) {
const storage = graphicData.RtssGraphicStorage.deserialize(
toUint8Array(base64)
);
canvasProperty.copyFrom(storage.canvas);
storage.Platforms.forEach((platform) => {
const g = new PlatformData(platform);
datas.push(g);
});
storage.stations.forEach((station) => {
datas.push(new StationData(station));
});
storage.train.forEach((train) => {
datas.push(new TrainData(train));
});
storage.turnouts.forEach((turnout) => {
datas.push(new TurnoutData(turnout));
});
storage.signals.forEach((signal) => {
datas.push(new SignalData(signal));
});
storage.section.forEach((section) => {
datas.push(new SectionData(section));
});
storage.logicSections.forEach((section) => {
datas.push(new LogicSectionData(section));
});
storage.separators.forEach((separator) => {
datas.push(new SeparatorData(separator));
});
storage.axleCountings.forEach((axleCounting) => {
datas.push(new AxleCountingData(axleCounting));
});
storage.trainWindows.forEach((trainWindow) => {
datas.push(new TrainWindowData(trainWindow));
});
}
return { datas, canvasProperty };
};
lineApp = newGraphicApp({
interactiveGraphicTypeIncludes: [
Signal.Type,
Platform.Type,
Station.Type,
Train.Type,
LogicSection.Type,
Turnout.Type,
],
mouseToolOptions: {
boxSelect: true,
viewportDrag: true,
wheelZoom: true,
},
dataLoader: lineAppDataLoader,
});
2023-06-15 13:12:37 +08:00
const graphicTemplate = [
2023-06-21 14:37:57 +08:00
new TrainTemplate(new TrainData(), new TrainState()),
2023-06-20 16:22:10 +08:00
new SignalTemplate(new SignalData(), new SignalState()),
2023-06-20 17:36:39 +08:00
new PlatformTemplate(new PlatformData(), new PlatformState()),
new StationTemplate(new StationData(), new StationState()),
2023-07-13 17:58:56 +08:00
new TurnoutTemplate(new TurnoutData(), new TurnoutStates()),
2023-06-20 17:36:39 +08:00
new SectionTemplate(new SectionData()),
2023-07-27 15:05:43 +08:00
new LogicSectionTemplate(new LogicSectionData(), new LogicSectionState()),
2023-07-03 16:26:49 +08:00
new SeparatorTemplate(new SeparatorData()),
2023-07-03 16:05:41 +08:00
new AxleCountingTemplate(new AxleCountingData()),
new TrainWindowTemplate(new TrainWindowData()),
2023-06-15 13:12:37 +08:00
];
lineApp.registerGraphicTemplates(...graphicTemplate);
2023-06-19 16:19:38 +08:00
SignalOperateInteraction.init(lineApp);
2023-06-21 16:57:24 +08:00
PlatformOperateInteraction.init(lineApp);
2023-07-04 17:04:23 +08:00
StationOperateInteraction.init(lineApp);
2023-07-06 16:43:21 +08:00
TrainOperateInteraction.init(lineApp);
2023-07-26 13:07:14 +08:00
TurnoutOperationPlugin.init(lineApp);
2023-07-27 15:05:43 +08:00
LogicSectionOperationPlugin.init(lineApp);
2023-09-01 14:30:45 +08:00
2023-09-01 17:14:08 +08:00
lineApp.enableWsMassaging({
wsUrl: getWebsocketUrl(),
token: getJwtToken() as string,
});
2023-09-01 14:30:45 +08:00
2023-09-01 17:14:08 +08:00
lineApp.subscribe({
destination: `/queue/line/${lineId}/device`,
messageConverter: (message: Uint8Array) => {
const states: GraphicState[] = [];
const storage = state.WsLineMessage.deserialize(message);
storage.signal.forEach((item) => {
if (item.rtuId !== 81 && item.rtuId !== 82 && item.rtuId) {
states.push(new SignalState(item));
}
});
storage.platform.forEach((item) => {
if (item.rtuId !== 81 && item.rtuId !== 82 && item.rtuId) {
states.push(new PlatformState(item));
}
});
storage.rtu.forEach((item) => {
if (item.rtuId !== 81 && item.rtuId !== 82 && item.rtuId) {
states.push(new StationState(item));
}
});
storage.switch.forEach((item) => {
if (item.rtuId !== 81 && item.rtuId !== 82 && item.rtuId) {
states.push(new TurnoutStates(item));
}
});
storage.track.forEach((item) => {
if (item.rtuId !== 81 && item.rtuId !== 82 && item.rtuId) {
states.push(new LogicSectionState(item));
}
});
return states;
},
});
2023-09-05 10:57:30 +08:00
lineApp.subscribe({
destination: `/queue/line/${lineId}/train`,
messageConverter: (message: Uint8Array) => {
const states: GraphicState[] = [];
const trainStorage = state.WsLineTrainMessage.deserialize(message);
// console.log(trainStorage, '222');
trainStorage.trainInfo.forEach((item) => {
if (item.rtuId !== 81 && item.rtuId !== 82 && item.rtuId) {
states.push(new TrainState(item));
}
});
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',
messageHandle: (message: Uint8Array) => {
const storage = alert.NccAlertInfoMessage.deserialize(message);
lineNetStore.setAlarmInfo(storage.messages as []);
},
});
2023-09-01 14:30:45 +08:00
2023-09-01 18:01:01 +08:00
lineApp.reload().then(() => {
if (!lineApp) return;
const quickJumpMenu = new ContextMenu({
name: '快捷跳转',
groups: [
{
items: lineApp.queryStore
.queryByType<Station>(Station.Type)
.map<MenuItemOptions>((station) => ({
name: station.datas.name ?? '',
handler: () => {
lineApp?.makeGraphicCenterShow(station);
},
})),
},
],
});
lineApp.registerMenu(quickJumpMenu);
lineApp.canvas.on('_rightclick', (e) => {
quickJumpMenu.open(e.global);
});
2023-06-30 09:27:16 +08:00
2023-09-01 18:01:01 +08:00
const axleCountings = lineApp.queryStore.queryByType<AxleCounting>(
AxleCounting.Type
);
axleCountings.forEach((axleCounting) => {
axleCounting.visible = false;
});
const trainWindows = lineApp.queryStore.queryByType<TrainWindow>(
TrainWindow.Type
);
trainWindows.forEach((trainWindow) => {
trainWindow.visible = false;
});
2023-09-01 17:14:08 +08:00
});
2023-09-01 18:01:01 +08:00
return lineApp;
2023-06-15 13:12:37 +08:00
}