From fee0bed1513e8fd5e35e689ddad411ee6b5f0387 Mon Sep 17 00:00:00 2001 From: thesai <1021828630@qq.com> Date: Fri, 23 Feb 2024 10:43:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=88=97=E8=BD=A6=E5=90=8E?= =?UTF-8?q?=E5=8F=AF=E5=86=8D=E6=AC=A1=E6=B7=BB=E5=8A=A0=E7=9B=B8=E5=90=8C?= =?UTF-8?q?=E7=BC=96=E5=8F=B7=E5=88=97=E8=BD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/simulation.go | 6 +++- .../wayside/memory/wayside_memory_train.go | 28 +++++++++++-------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/api/simulation.go b/api/simulation.go index 817cb7d..b4c9017 100644 --- a/api/simulation.go +++ b/api/simulation.go @@ -244,7 +244,11 @@ func addTrain(c *gin.Context) { WheelDiameter: req.WheelDiameter, Speed: req.TrainSpeed, } - memory.AddTrainStateNew(simulation, rsp, req.ConfigTrain, req.TrainEndsA, req.TrainEndsB, req.MapId) + var err *sys_error.BusinessError = memory.AddTrainStateNew(simulation, + rsp, req.ConfigTrain, req.TrainEndsA, req.TrainEndsB, req.MapId) + if err != nil { + panic(sys_error.New("添加列车失败", err)) + } c.JSON(http.StatusOK, &rsp) } diff --git a/ts/simulation/wayside/memory/wayside_memory_train.go b/ts/simulation/wayside/memory/wayside_memory_train.go index eb7b8a6..5cc06c3 100644 --- a/ts/simulation/wayside/memory/wayside_memory_train.go +++ b/ts/simulation/wayside/memory/wayside_memory_train.go @@ -35,11 +35,14 @@ func CreateMsgTrainConfig(trainId int, trainLen int64, configTrainData dto.Confi // 增加列车状态 func AddTrainStateNew(vs *VerifySimulation, status *state_proto.TrainState, configTrainData dto.ConfigTrainData, trainEndsA dto.ConfigTrainEnds, - trainEndsB dto.ConfigTrainEnds, mapId int32) *state_proto.TrainState { + trainEndsB dto.ConfigTrainEnds, mapId int32) *sys_error.BusinessError { allTrainMap := &vs.Memory.Status.TrainStateMap - _, ok := allTrainMap.Load(status.Id) + value, ok := allTrainMap.Load(status.Id) if ok { - panic(fmt.Sprintf("列车【%s】已存在", status.Id)) + trainState := value.(*state_proto.TrainState) + if trainState.Show { + return sys_error.New(fmt.Sprintf("列车【%s】已存在", status.Id)) + } } // 显示状态 status.Show = true @@ -108,7 +111,7 @@ func AddTrainStateNew(vs *VerifySimulation, status *state_proto.TrainState, conf }) // 将信息合并到当前设备状态中 allTrainMap.Store(status.Id, status) - return status + return nil } // 增加列车状态 @@ -345,12 +348,14 @@ func RemoveAllTrain(vs *VerifySimulation) { return } allTrainMap.Range(func(k any, t any) bool { - train := t.(*state_proto.TrainState) - err := removeTrain(vs, train.Id, train) - if err != nil { - slog.Error("列车id:", train.Id, "移除失败,原因:", err.Error()) - } - allTrainMap.Store(train.Id, t) + id := k.(string) + RemoveTrainState(vs, id) + //train := t.(*state_proto.TrainState) + //err := removeTrain(vs, train.Id, train) + //if err != nil { + // slog.Error("列车id:", train.Id, "移除失败,原因:", err.Error()) + //} + //allTrainMap.Store(train.Id, t) return true }) } @@ -364,8 +369,7 @@ func removeTrain(vs *VerifySimulation, trainId string, train *state_proto.TrainS return err } train.Show = false - fi.RemoveTrainFromWorld(vs.World, trainId) - return nil + return fi.RemoveTrainFromWorld(vs.World, trainId) } // 删除列车状态