rts-sim-testing-service/ts/simulation/wayside/memory/wayside_memory_train.go
2023-10-30 10:48:07 +08:00

95 lines
3.1 KiB
Go

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/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)
// 映射link、偏移量、运行方向
linkId, loffset, up, pointTo, kilometer := QueryEcsLinkByDeviceInfo(vs.Repo, mapId, status)
status.Up = up
status.PointTo = pointTo
status.TrainKilometer = kilometer
err := dynamics.Default().RequestAddTrain(&message.InitTrainInfo{
TrainIndex: uint16(trainIndex),
LinkIndex: uint16(linkId),
LinkOffset: uint32(loffset),
Speed: uint16(math.Round(float64(status.Speed * 10))),
Up: status.Up,
TrainLength: uint16(status.TrainLength),
})
slog.Debug("添加列车", "trainIndex", trainIndex, "HeadDeviceId", status.HeadDeviceId, "HeadOffset", status.HeadOffset)
slog.Debug("列车初始化", "trainIndex", trainIndex, "linkId", linkId, "loffset", loffset)
if err != nil {
panic(dto.ErrorDto{Code: dto.DynamicsError, Message: err.Error()})
}
// 调用成功后初始化列车的动力学
status.DynamicState = &state.TrainDynamicState{}
status.VobcState = &state.TrainVobcState{}
// 将信息合并到当前设备状态中
allTrainMap.Store(status.Id, status)
// 将变更信息放入变更状态队列中
vs.Memory.ChangeStatus.TrainStateMap.Store(status.Id, proto.Clone(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)
// 将变更信息放入变更状态队列中
vs.Memory.ChangeStatus.TrainStateMap.Store(t.Id, proto.Clone(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)
// 将列车Id放入移除列表
vs.Memory.ChangeStatus.RemoveTrainId = append(vs.Memory.ChangeStatus.RemoveTrainId, id)
} else {
panic(fmt.Sprintf("列车【%s】不存在", id))
}
}