This commit is contained in:
fan 2023-10-11 10:37:47 +08:00
commit 05d72039bd
12 changed files with 357 additions and 164 deletions

View File

@ -2,66 +2,57 @@
<template>
<q-dialog ref="dialogRef">
<q-card style="width: 250px">
<q-card-section> <div class="text-h6">添加列车</div> </q-card-section>
<q-card-section class="q-pt-none">
<q-input
dense
outlined
:disable="true"
label="SectionLink"
v-model="props.dev.code"
/>
</q-card-section>
<q-card-section class="q-py-none">
<q-input
type="number"
dense
outlined
:label="`列车偏移(mm)[长度${props.kmLength}mm]`"
:max="kmLength"
:min="0"
v-model.number="offset"
lazy-rules
:rules="[offsetRules]"
/>
</q-card-section>
<q-card-section>
<q-select
v-model="dir"
label="运行方向"
dense
outlined
:options="dirOptions"
emitValue
mapOptions
>
</q-select>
<q-form ref="myForm" @submit="onCreate" class="q-gutter-md">
<div class="text-h6">添加列车</div>
<q-input
dense
outlined
readonly
:label="props.dev.type"
v-model="props.dev.id"
/>
<q-input
type="number"
dense
outlined
:label="`列车偏移(mm)[长度${props.kmLength}mm]`"
:max="kmLength"
:min="0"
v-model.number="offset"
lazy-rules
:rules="offsetRules"
/>
<q-select
v-model="dir"
label="运行方向"
dense
outlined
:options="dirOptions"
emitValue
mapOptions
>
</q-select>
<q-select
v-model="trainLength"
label="列车总长度(mm)"
dense
outlined
:options="lengthOptions"
>
</q-select>
<q-card-actions align="right" class="text-primary">
<q-btn flat label="取消" @click="onDialogCancel" v-close-popup />
<q-btn flat label="确认" type="submit" />
</q-card-actions>
</q-form>
</q-card-section>
<q-card-section>
<q-select
v-model="trainLength"
label="列车总长度(mm)"
dense
outlined
:options="lengthOptions"
>
</q-select>
</q-card-section>
<q-card-actions align="right" class="text-primary">
<q-btn flat label="取消" @click="onDialogCancel" v-close-popup />
<q-btn
flat
label="确认"
@click="onDialogOK({ dir, offset, trainLength })"
v-close-popup
/>
</q-card-actions>
</q-card>
</q-dialog>
</template>
<script setup lang="ts">
import { useDialogPluginComponent } from 'quasar';
import { QForm, useDialogPluginComponent } from 'quasar';
import { getProjectLinkTrainSizeByMapId } from 'src/api/ProjectLinkApi';
import { Section } from 'src/graphics/section/Section';
import { Turnout } from 'src/graphics/turnout/Turnout';
@ -89,8 +80,11 @@ defineEmits([...useDialogPluginComponent.emits]);
const { dialogRef, onDialogOK, onDialogCancel } = useDialogPluginComponent();
const offsetRules = (val: number) =>
(val >= 0 && val <= props.kmLength) || `偏移量在0到${props.kmLength}之间!`;
const offsetRules = [
(val: string) => (val !== null && val !== '') || '偏移量不能为空!',
(val: number) =>
(val >= 0 && val <= props.kmLength) || `偏移量在0到${props.kmLength}之间!`,
];
const lengthOptions = ref<string[]>([]);
const trainLength = ref('');
@ -109,6 +103,22 @@ function getLengthOption(mapId: number) {
lengthOptions.value.push(item.total_length + '');
}
});
if (lengthOptions.value[0]) {
trainLength.value = lengthOptions.value[0];
}
});
}
const myForm = ref<QForm | null>(null);
function onCreate() {
myForm.value?.validate().then(async (res) => {
if (res) {
onDialogOK({
dir: dir.value,
offset: offset.value,
trainLength: trainLength.value,
});
}
});
}
</script>

View File

@ -35,7 +35,10 @@
<script setup lang="ts">
import DraggableDialog from 'src/components/common/DraggableDialog.vue';
import { RelayCabinetData } from 'src/drawApp/relayCabinetGraphics/RelayCabinetInteraction';
import { RelayData } from 'src/drawApp/relayCabinetGraphics/RelayInteraction';
import {
RelayData,
RelayState,
} from 'src/drawApp/relayCabinetGraphics/RelayInteraction';
import { Relay } from 'src/graphics/relay/Relay';
import {
RelayCabinet,
@ -107,6 +110,7 @@ function oneClickRelay() {
if (j == numCols - 1 && remainder !== 0 && i >= remainder) break;
const relay = new Relay();
relay.loadData(new RelayData());
relay.loadState(new RelayState());
relay.id = GraphicIdGenerator.next();
relay.position.set(
relayCabinet.x -

View File

@ -8,13 +8,6 @@
@blur="onUpdate"
label="索引"
/>
<q-input
outlined
v-model.number="platformModel.refStationIndex"
type="number"
@blur="onUpdate"
label="对应车站索引"
/>
<q-select
outlined
@blur="onUpdate"
@ -33,6 +26,26 @@
:options="optionsDirection"
label="方向"
/>
<q-input
v-if="platformModel.hasdoor"
outlined
v-model.number="platformModel.sonDoorAmount"
type="number"
@blur="onUpdate"
label="子屏蔽门的数量"
/>
<q-list bordered separator class="rounded-borders">
<q-item>
<q-item-section no-wrap class="q-gutter-y-sm column">
<q-item-label> 关联的车站 </q-item-label>
<div class="q-gutter-sm row">
<q-chip square color="primary" text-color="white">
{{ stationRelation }}
</q-chip>
</div>
</q-item-section>
</q-item>
</q-list>
</q-form>
</template>
@ -40,7 +53,15 @@
import { PlatformData } from 'src/drawApp/graphics/PlatformInteraction';
import { useFormData } from 'src/components/DrawAppFormUtils';
import { useDrawStore } from 'src/stores/draw-store';
import { computed } from 'vue';
import { Station } from 'src/graphics/station/Station';
import { Platform } from 'src/graphics/platform/Platform';
const drawStore = useDrawStore();
const { data: platformModel, onUpdate } = useFormData(
new PlatformData(),
drawStore.getDrawApp()
);
const optionsDoor = [
{ label: '是', value: true },
{ label: '否', value: false },
@ -50,8 +71,15 @@ const optionsDirection = [
{ label: '向下', value: 'down' },
];
const { data: platformModel, onUpdate } = useFormData(
new PlatformData(),
useDrawStore().getDrawApp()
);
const stationRelation = computed(() => {
const platform = drawStore.selectedGraphic as Platform;
const refStations = platform?.relationManage
.getRelationsOfGraphicAndOtherType(platform, Station.Type)
.map((relation) => relation.getOtherGraphic<Station>(platform).datas.code);
let refStation;
if (refStations) {
return (refStation = refStations[0]);
}
return refStation;
});
</script>

View File

@ -58,11 +58,17 @@ export class PlatformData extends GraphicDataBase implements IPlatformData {
set index(v: number) {
this.data.index = v;
}
get refStationIndex(): number {
return this.data.refStationIndex;
get refStation(): string {
return this.data.refStation;
}
set refStationIndex(v: number) {
this.data.refStationIndex = v;
set refStation(v: string) {
this.data.refStation = v;
}
get sonDoorAmount(): number {
return this.data.sonDoorAmount;
}
set sonDoorAmount(v: number) {
this.data.sonDoorAmount = v;
}
clone(): PlatformData {

View File

@ -1,4 +1,9 @@
import { GraphicDrawAssistant, IDrawApp } from 'src/jl-graphic';
import {
GraphicDrawAssistant,
GraphicInteractionPlugin,
IDrawApp,
JlGraphic,
} from 'src/jl-graphic';
import { IBPButton, IBPButtonTemplate, IIBPButtonData } from './IBPButton';
import { FederatedMouseEvent, Point } from 'pixi.js';
@ -14,6 +19,7 @@ export class IBPButtonDrawAssistant extends GraphicDrawAssistant<
'svguse:../../drawIcon.svg#icon-psl-button',
'IBP按钮'
);
IBPButtonInteraction.init(app);
}
get ibpButton(): IBPButton {
if (!this._ibpButton) {
@ -37,3 +43,26 @@ export class IBPButtonDrawAssistant extends GraphicDrawAssistant<
return true;
}
}
class IBPButtonInteraction extends GraphicInteractionPlugin<IBPButton> {
static Name = 'ibp_button_transform';
constructor(app: IDrawApp) {
super(IBPButtonInteraction.Name, app);
}
static init(app: IDrawApp) {
return new IBPButtonInteraction(app);
}
filter(...grahpics: JlGraphic[]): IBPButton[] {
return grahpics.filter((g): g is IBPButton => g instanceof IBPButton);
}
bind(g: IBPButton): void {
g.eventMode = 'static';
g.cursor = 'pointer';
g.scalable = true;
g.rotatable = true;
}
unbind(g: IBPButton): void {
g.eventMode = 'none';
g.scalable = false;
g.rotatable = false;
}
}

View File

@ -9,6 +9,7 @@ import {
calculateMirrorPoint,
getRectangleCenter,
} from 'src/jl-graphic';
import { Station } from '../station/Station';
export interface IPlatformData extends GraphicData {
get code(): string; // 编号
@ -19,8 +20,10 @@ export interface IPlatformData extends GraphicData {
set direction(v: string);
get index(): number;
set index(v: number);
get refStationIndex(): number;
set refStationIndex(v: number);
get refStation(): string; // 关联的车站
set refStation(v: string);
get sonDoorAmount(): number; //子屏蔽门的数量
set sonDoorAmount(v: number);
clone(): IPlatformData;
copyFrom(data: IPlatformData): void;
eq(other: IPlatformData): boolean;
@ -139,19 +142,22 @@ class smallDoorGraphic extends Container {
this.addChild(this.smallDoorGraphic);
this.addChild(this.labelGraphic);
}
draw(direction: string, stateData: IPlatformState, i: number): void {
draw(data: IPlatformData, stateData: IPlatformState, i: number): void {
const start =
-platformConsts.smallDoorWidth * 15 + platformConsts.smallDoorWidth * i;
(-platformConsts.smallDoorWidth * data.sonDoorAmount) / 2 +
platformConsts.smallDoorWidth * i;
const smallDoorGraphic = this.smallDoorGraphic;
const lineColor = PlatformColorEnum.doorGreen;
// if (stateData.psdCut) {
// lineColor = PlatformColorEnum.doorRed;
// }
smallDoorGraphic.lineStyle(platformConsts.lineWidth, new Color(lineColor));
smallDoorGraphic.moveTo(start, 0);
smallDoorGraphic.lineTo(start + platformConsts.smallDoorWidth - 3, 0);
const direction = data.direction;
smallDoorGraphic
.lineStyle(platformConsts.lineWidth, new Color(lineColor))
.moveTo(start, 0)
.lineTo(start + platformConsts.smallDoorWidth - 3, 0);
if (direction == 'down') {
this.labelGraphic.text = 30 - i;
this.labelGraphic.text = data.sonDoorAmount - i;
} else {
this.labelGraphic.text = i + 1;
}
@ -167,10 +173,10 @@ export class doorGraphic extends Container {
constructor() {
super();
}
draw(direction: string, stateData: IPlatformState): void {
for (let i = 0; i < 30; i++) {
draw(data: IPlatformData, stateData: IPlatformState): void {
for (let i = 0; i < data.sonDoorAmount; i++) {
const smallDoor = new smallDoorGraphic();
smallDoor.draw(direction, stateData, i);
smallDoor.draw(data, stateData, i);
this.addChild(smallDoor);
}
}
@ -421,9 +427,10 @@ export class Platform extends JlGraphic {
return this.datas.index + '';
}
doRepaint(): void {
this.datas.sonDoorAmount = this.datas?.sonDoorAmount || 30;
this.doorGraphic.clear();
if (this.datas.hasdoor) {
this.doorGraphic.draw(this.datas.direction, this.states);
this.doorGraphic.draw(this.datas, this.states);
}
this.platformGraphic.draw(this.states);
this.emergClose.draw();
@ -453,6 +460,32 @@ export class Platform extends JlGraphic {
}
// this.changeState();
}
buildRelation() {
const stationas = this.queryStore.queryByType<Station>(Station.Type);
for (let i = 0; i < stationas.length; i++) {
const sP = stationas[i].localBoundsToCanvasPoints();
if (this.x > sP[0].x && this.x < sP[1].x) {
this.relationManage.addRelation(this, stationas[i]);
break;
}
}
}
saveRelations() {
const refStation = this.relationManage
.getRelationsOfGraphicAndOtherType(this, Station.Type)
.map((relation) => relation.getOtherGraphic<Station>(this).datas.id);
if (refStation.length) {
this.datas.refStation = refStation[0];
}
}
loadRelations() {
if (this.datas.refStation) {
this.relationManage.addRelation(
this,
this.queryStore.queryById<Station>(this.datas.refStation)
);
}
}
// changeState(): void {
// this.doorGraphic.changeState(this.states);
// this.emergClose.changeState(this.id, this.states);

View File

@ -10,7 +10,7 @@ import { relayCabinetGraphicData } from 'src/protos/relayCabinetLayoutGraphics';
export interface IRelayData extends GraphicData {
get code(): string; // 编号
set code(v: string);
get newModel(): relayCabinetGraphicData.Relay.ModelType; // 计轴、区段边界
get newModel(): relayCabinetGraphicData.Relay.ModelType; // 型号
set newModel(v: relayCabinetGraphicData.Relay.ModelType);
clone(): IRelayData;
copyFrom(data: IRelayData): void;

View File

@ -2,26 +2,20 @@
<q-layout view="hHh LpR fFf">
<q-header reveal class="bg-primary text-white">
<q-toolbar>
<q-btn-dropdown color="info" label="切换场景">
<q-list>
<q-item
v-for="(item, index) in projectInfo.mapInfoLinks"
:key="index"
:disable="item.id == lineStore.mapId"
clickable
v-close-popup
@click="switchScene(item)"
>
<q-item-section>
<q-item-label>{{ item.name }}</q-item-label>
</q-item-section>
</q-item>
</q-list>
</q-btn-dropdown>
<q-select
dark
dense
label-color="grey-13"
v-model="sceneInfo"
@update:model-value="switchScene"
:options="sceneOptions"
:option-label="(item) => item.name"
label="切换场景"
></q-select>
<q-toolbar-title> {{ projectName }} </q-toolbar-title>
<q-btn
color="info"
label="销毁仿真"
color="red"
label="结束"
class="q-mr-md"
@click="destroySimAndBack"
/>
@ -62,7 +56,7 @@ import {
nextTick,
} from 'vue';
import { useLineStore } from 'src/stores/line-store';
import { usePslStore } from 'src/stores/psl-store.ts';
import { usePslStore } from 'src/stores/psl-store';
import { useRoute, useRouter } from 'vue-router';
import { destroySimulation } from 'src/api/Simulation';
import StateProperties from 'src/components/line-app/StateProperties.vue';
@ -73,6 +67,7 @@ import { ApiError } from 'src/boot/axios';
import { layerList } from 'src/drawApp/lineScene';
import { IGraphicScene } from 'src/jl-graphic';
import { ISceneName, getSceneName } from 'src/drawApp/lineApp';
import { useTestManageStore } from 'src/stores/testManage-store';
const $q = useQuasar();
const canvasWidth = ref(0);
@ -81,6 +76,7 @@ const headerHeight = ref(0);
const route = useRoute();
const router = useRouter();
const lineStore = useLineStore();
const testManageStore = useTestManageStore();
const pslDialog = ref(false);
const simulationId = (route.query.simulationId as string) || '';
const projectId = (route.query.projectId as string) || '';
@ -90,6 +86,11 @@ const drawerRight = ref(false);
const projectName = computed(() => projectInfo.name);
const sceneOptions = computed(() => {
return projectInfo.mapInfoLinks || [];
});
const sceneInfo = ref();
function onResize() {
const clientWidth = document.documentElement.clientWidth;
const clientHeight = document.documentElement.clientHeight;
@ -115,6 +116,7 @@ const lineApp = lineStore.initLineApp();
let scene: null | IGraphicScene = null;
onMounted(async () => {
testManageStore.socketConnect();
const dom = document.getElementById('line-app-container');
if (dom && defaultMapId && simulationId) {
lineStore.setMapId(+defaultMapId);
@ -131,6 +133,7 @@ onMounted(async () => {
return item.id == lineStore.mapId;
});
if (find) {
sceneInfo.value = find;
sceneName = getSceneNameFn(find);
}
lineStore.addAllScene(projectInfo.mapInfoLinks || []);
@ -196,23 +199,56 @@ watch(
});
}
);
function destroySimAndBack() {
if (simulationId) {
destroySimulation({ simulationId });
watch(
() => testManageStore.socketInfo,
(val) => {
const removeS = val.removeSimulations.find((item) => {
return item.simulationId == simulationId;
});
if (removeS) {
$q.dialog({
title: '确认',
message: `${projectName.value}】项目仿真已经结束,是否确认退出?`,
cancel: true,
}).onOk(() => {
backConfirm();
});
}
}
backConfirm();
);
function destroySimAndBack() {
$q.dialog({
title: '确认',
message: `确认结束项目【${projectName.value}】吗?`,
cancel: true,
}).onOk(async () => {
try {
if (simulationId) {
await destroySimulation({ simulationId });
}
backConfirm();
} catch (err) {
const error = err as ApiError;
$q.notify({
type: 'negative',
message: error.title,
});
backConfirm();
}
});
}
function switchScene(val: MapInfo) {
if (val.id == lineStore.mapId) return;
scene && scene.unbindDom();
lineStore.setMapId(val.id);
sceneName = getSceneNameFn(val);
lineStore.setSceneName(sceneName);
scene = lineApp.getScene(sceneName);
const dom = document.getElementById('line-app-container');
if (dom) {
scene.bindDom(dom);
lineApp.switchScene(sceneName, dom);
scene = lineApp.getScene(sceneName);
scene.reload();
}
}

View File

@ -123,8 +123,10 @@ import { useRouter } from 'vue-router';
import { clearJwtToken } from 'src/configs/TokenManage';
import { Dialog } from 'quasar';
import { useAuthStore } from 'src/stores/auth-store';
import { useTestManageStore } from 'src/stores/testManage-store';
const router = useRouter();
const testManageStore = useTestManageStore();
const leftDrawerOpen = ref(false);
@ -169,6 +171,7 @@ function logOut() {
cancel: true,
persistent: true,
}).onOk(() => {
testManageStore.socketClose();
clearJwtToken();
authStore.clearCurrentUser();
router.push({ name: 'login' });

View File

@ -86,7 +86,7 @@
</template>
<script setup lang="ts">
import { ref, reactive, onMounted, computed, onUnmounted } from 'vue';
import { ref, reactive, onMounted, computed, watch } from 'vue';
import { useQuasar, type QTableColumn, QForm } from 'quasar';
import { getProjectList } from '../api/ProjectApi';
import { ApiError } from 'src/boot/axios';
@ -96,18 +96,17 @@ import {
createSimulationByProject,
getSimulationList,
destroySimulation,
getSimulationChannelName,
} from 'src/api/Simulation';
import { useRouter } from 'vue-router';
import { PublishItem, getPublishList } from 'src/api/PublishApi';
import { getWebsocketUrl } from 'src/configs/UrlManage';
import { getOnlyToken } from 'src/configs/TokenManage';
import { state } from 'src/protos/device_state';
import { CentrifugeMessagingClient } from 'src/jl-graphic/message/CentrifugeBroker';
import { useTestManageStore } from 'src/stores/testManage-store';
const router = useRouter();
const $q = useQuasar();
const testManageStore = useTestManageStore();
const props = withDefaults(
defineProps<{
sizeHeight: number;
@ -123,7 +122,7 @@ onMounted(() => {
tableRef.value.requestServerInteraction();
getAllProjectList();
getMapList();
socketConnect();
testManageStore.socketConnect();
});
const columnDefs: QTableColumn[] = [
@ -311,28 +310,14 @@ function getMapNames(ids: number[]) {
return nameArr.join('');
}
let socket: CentrifugeMessagingClient | null = null;
let destination = '';
watch(
() => testManageStore.socketInfo,
(val) => {
handler(val as state.MemoryDataStatus);
}
);
async function socketConnect() {
getSimulationChannelName()
.then((res) => {
destination = res;
socket = new CentrifugeMessagingClient({
wsUrl: `${getWebsocketUrl()}`,
token: getOnlyToken() as string,
protocol: 'protobuf',
});
socket.on('connected', () => {
socket?.subscribe(destination, handler);
});
})
.catch((err) => {
console.log(err, '获取仿真信息更新通道名称失败!');
});
}
function handler(message: Uint8Array) {
const storage = state.MemoryDataStatus.deserialize(message);
function handler(storage: state.MemoryDataStatus) {
if (storage.addSimulations.length) {
const arr = storage.addSimulations.map((item) => {
return {
@ -366,13 +351,4 @@ function handler(message: Uint8Array) {
});
}
}
function closeSocket() {
socket?.unsubscribe0(destination);
socket?.close();
socket = null;
}
onUnmounted(() => {
closeSocket();
});
</script>

View File

@ -1271,7 +1271,8 @@ export namespace graphicData {
hasdoor?: boolean;
direction?: string;
index?: number;
refStationIndex?: number;
refStation?: string;
sonDoorAmount?: number;
}) {
super();
pb_1.Message.initialize(this, Array.isArray(data) ? data : [], 0, -1, [], this.#one_of_decls);
@ -1291,8 +1292,11 @@ export namespace graphicData {
if ("index" in data && data.index != undefined) {
this.index = data.index;
}
if ("refStationIndex" in data && data.refStationIndex != undefined) {
this.refStationIndex = data.refStationIndex;
if ("refStation" in data && data.refStation != undefined) {
this.refStation = data.refStation;
}
if ("sonDoorAmount" in data && data.sonDoorAmount != undefined) {
this.sonDoorAmount = data.sonDoorAmount;
}
}
}
@ -1329,11 +1333,17 @@ export namespace graphicData {
set index(value: number) {
pb_1.Message.setField(this, 5, value);
}
get refStationIndex() {
return pb_1.Message.getFieldWithDefault(this, 6, 0) as number;
get refStation() {
return pb_1.Message.getFieldWithDefault(this, 8, "") as string;
}
set refStationIndex(value: number) {
pb_1.Message.setField(this, 6, value);
set refStation(value: string) {
pb_1.Message.setField(this, 8, value);
}
get sonDoorAmount() {
return pb_1.Message.getFieldWithDefault(this, 9, 0) as number;
}
set sonDoorAmount(value: number) {
pb_1.Message.setField(this, 9, value);
}
static fromObject(data: {
common?: ReturnType<typeof CommonInfo.prototype.toObject>;
@ -1341,7 +1351,8 @@ export namespace graphicData {
hasdoor?: boolean;
direction?: string;
index?: number;
refStationIndex?: number;
refStation?: string;
sonDoorAmount?: number;
}): Platform {
const message = new Platform({});
if (data.common != null) {
@ -1359,8 +1370,11 @@ export namespace graphicData {
if (data.index != null) {
message.index = data.index;
}
if (data.refStationIndex != null) {
message.refStationIndex = data.refStationIndex;
if (data.refStation != null) {
message.refStation = data.refStation;
}
if (data.sonDoorAmount != null) {
message.sonDoorAmount = data.sonDoorAmount;
}
return message;
}
@ -1371,7 +1385,8 @@ export namespace graphicData {
hasdoor?: boolean;
direction?: string;
index?: number;
refStationIndex?: number;
refStation?: string;
sonDoorAmount?: number;
} = {};
if (this.common != null) {
data.common = this.common.toObject();
@ -1388,8 +1403,11 @@ export namespace graphicData {
if (this.index != null) {
data.index = this.index;
}
if (this.refStationIndex != null) {
data.refStationIndex = this.refStationIndex;
if (this.refStation != null) {
data.refStation = this.refStation;
}
if (this.sonDoorAmount != null) {
data.sonDoorAmount = this.sonDoorAmount;
}
return data;
}
@ -1407,8 +1425,10 @@ export namespace graphicData {
writer.writeString(4, this.direction);
if (this.index != 0)
writer.writeInt32(5, this.index);
if (this.refStationIndex != 0)
writer.writeInt32(6, this.refStationIndex);
if (this.refStation.length)
writer.writeString(8, this.refStation);
if (this.sonDoorAmount != 0)
writer.writeInt32(9, this.sonDoorAmount);
if (!w)
return writer.getResultBuffer();
}
@ -1433,8 +1453,11 @@ export namespace graphicData {
case 5:
message.index = reader.readInt32();
break;
case 6:
message.refStationIndex = reader.readInt32();
case 8:
message.refStation = reader.readString();
break;
case 9:
message.sonDoorAmount = reader.readInt32();
break;
default: reader.skipField();
}

View File

@ -0,0 +1,45 @@
import { defineStore } from 'pinia';
import { CentrifugeMessagingClient } from 'src/jl-graphic/message/CentrifugeBroker';
import { getSimulationChannelName } from 'src/api/Simulation';
import { getWebsocketUrl } from 'src/configs/UrlManage';
import { getOnlyToken } from 'src/configs/TokenManage';
import { state } from 'src/protos/device_state';
export const useTestManageStore = defineStore('testManage', {
state: () => ({
socket: null as CentrifugeMessagingClient | null, // 启动项目的socket
destination: '', // 启动项目的socket订阅路径
socketInfo: new state.MemoryDataStatus(),
}),
actions: {
socketHandler(message: Uint8Array) {
const storage = state.MemoryDataStatus.deserialize(message);
this.socketInfo = storage;
},
socketConnect() {
if (this.socket) return;
getSimulationChannelName()
.then((res) => {
this.destination = res;
this.socket = new CentrifugeMessagingClient({
wsUrl: `${getWebsocketUrl()}`,
token: getOnlyToken() as string,
protocol: 'protobuf',
});
this.socket.on('connected', () => {
this.socket?.subscribe(this.destination, this.socketHandler);
});
})
.catch((err) => {
console.log(err, '获取仿真信息更新通道名称失败!');
});
},
socketClose() {
this.socket?.unsubscribe0(this.destination);
this.socket?.close();
this.socket = null;
this.destination = '';
this.socketInfo = new state.MemoryDataStatus();
},
},
});