package memory import ( "fmt" "log/slog" "math" "strconv" "joylink.club/bj-rtsts-server/dto" "joylink.club/bj-rtsts-server/third_party/dynamics" "joylink.club/bj-rtsts-server/third_party/message" "google.golang.org/protobuf/proto" "joylink.club/bj-rtsts-server/ts/protos/graphicData" "joylink.club/bj-rtsts-server/ts/protos/state" ) // 增加列车状态 func AddTrainState(vs *VerifySimulation, status *state.TrainState, mapId int32) { allTrainMap := &vs.Memory.Status.TrainStateMap _, ok := allTrainMap.Load(status.Id) if ok { panic(fmt.Sprintf("列车【%s】已存在", status.Id)) } // 显示状态 status.Show = true //向动力学发送初始化请求 trainIndex, _ := strconv.ParseUint(status.Id, 10, 16) slog.Debug("添加列车", "trainIndex", trainIndex, "HeadDeviceId", status.HeadDeviceId, "HeadOffset", status.HeadOffset) // 映射link、偏移量、运行方向 var uid string if status.DevicePort == "" { uid = QueryUidByMidAndComId(mapId, status.HeadDeviceId, &graphicData.Section{}) } else { uid = QueryUidByMidAndComId(mapId, status.HeadDeviceId, &graphicData.Turnout{}) } // 车头所在link、link上的偏移 linkId, loffset := QueryLinkAndOffsetByDevice(vs.Repo, uid, status.DevicePort, status.HeadOffset) // link上的运行方向、设备上的运行方向 up, pointTo := QueryUpAndABByDevice(vs.Repo, uid, status.DevicePort, status.RunDirection) // 车头所在公里标 kilometer := CalcTrainKilometer(vs.Repo, uid, status.DevicePort, status.RunDirection, status.HeadOffset) // 车尾相对车头link的偏移量 var calctailOffset int64 if up { calctailOffset = loffset - status.TrainLength } else { calctailOffset = loffset + status.TrainLength } // 车尾位置 tailLink, _, _, tailLOffset, _, _ := CalcInitializeLink(vs, linkId, calctailOffset, up) status.Up = up status.PointTo = pointTo status.TrainKilometer = kilometer.Value status.DynamicState = &state.TrainDynamicState{ HeadLinkId: linkId, HeadLinkOffset: loffset, TailLinkId: tailLink, TailLinkOffset: tailLOffset, RunningUp: up, } status.VobcState = &state.TrainVobcState{} slog.Debug("列车初始化", "trainIndex", trainIndex, "linkId", linkId, "loffset", loffset) linkIdInt, _ := strconv.Atoi(linkId) err := dynamics.Default().RequestAddTrain(&message.InitTrainInfo{ TrainIndex: uint16(trainIndex), LinkIndex: uint16(linkIdInt), LinkOffset: uint32(loffset), Speed: uint16(math.Round(float64(status.Speed * 10))), Up: status.Up, TrainLength: uint16(status.TrainLength), }) if err != nil { panic(dto.ErrorDto{Code: dto.DynamicsError, Message: err.Error()}) } // 将信息合并到当前设备状态中 allTrainMap.Store(status.Id, status) } // 修改列车状态 func UpdateTrainState(vs *VerifySimulation, status *state.TrainState) { allTrainMap := &vs.Memory.Status.TrainStateMap d, ok := allTrainMap.Load(status.Id) if !ok { panic(fmt.Sprintf("列车【%s】不存在", status.Id)) } t := d.(*state.TrainState) t.RunDirection = status.RunDirection t.PointTo = status.PointTo // 合并其他信息 proto.Merge(t, status) // 更新全量信息 allTrainMap.Store(status.Id, t) } // 删除列车状态 func RemoveTrainState(vs *VerifySimulation, id string) { allTrainMap := &vs.Memory.Status.TrainStateMap d, ok := allTrainMap.Load(id) if ok { t := d.(*state.TrainState) trainIndex, _ := strconv.ParseUint(id, 10, 16) err := dynamics.Default().RequestRemoveTrain(&message.RemoveTrainReq{ TrainIndex: uint16(trainIndex), }) if err != nil { panic(dto.ErrorDto{Code: dto.DynamicsError, Message: err.Error()}) } // 从仿真内存中移除列车 t.Show = false allTrainMap.Store(id, t) } else { panic(fmt.Sprintf("列车【%s】不存在", id)) } }