rts-sim-testing-service/ts/simulation/wayside/memory/wayside_memory_train.go

116 lines
3.7 KiB
Go
Raw Normal View History

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"
2023-11-08 11:02:41 +08:00
"joylink.club/bj-rtsts-server/ts/protos/graphicData"
2023-10-26 17:16:07 +08:00
"joylink.club/bj-rtsts-server/ts/protos/state"
)
// 增加列车状态
2023-09-21 17:57:50 +08:00
func AddTrainState(vs *VerifySimulation, status *state.TrainState, mapId int32) {
2023-09-12 10:00:13 +08:00
allTrainMap := &vs.Memory.Status.TrainStateMap
_, ok := allTrainMap.Load(status.Id)
if ok {
panic(fmt.Sprintf("列车【%s】已存在", status.Id))
}
2023-08-02 15:50:46 +08:00
// 显示状态
status.Show = true
//向动力学发送初始化请求
trainIndex, _ := strconv.ParseUint(status.Id, 10, 16)
2023-11-08 11:02:41 +08:00
slog.Debug("添加列车", "trainIndex", trainIndex, "HeadDeviceId", status.HeadDeviceId, "HeadOffset", status.HeadOffset)
2023-08-14 16:27:03 +08:00
// 映射link、偏移量、运行方向
2023-11-08 11:02:41 +08:00
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)
2023-08-14 16:27:03 +08:00
status.Up = up
2023-08-15 10:18:39 +08:00
status.PointTo = pointTo
2023-11-08 11:02:41 +08:00
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),
2023-11-08 11:02:41 +08:00
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)
2023-08-01 17:45:26 +08:00
}
// 修改列车状态
2023-09-12 10:00:13 +08:00
func UpdateTrainState(vs *VerifySimulation, status *state.TrainState) {
allTrainMap := &vs.Memory.Status.TrainStateMap
2023-08-01 17:45:26 +08:00
d, ok := allTrainMap.Load(status.Id)
if !ok {
panic(fmt.Sprintf("列车【%s】不存在", status.Id))
}
t := d.(*state.TrainState)
2023-08-14 16:27:03 +08:00
t.RunDirection = status.RunDirection
t.PointTo = status.PointTo
2023-08-01 17:45:26 +08:00
// 合并其他信息
proto.Merge(t, status)
// 更新全量信息
allTrainMap.Store(status.Id, t)
}
// 删除列车状态
2023-09-12 10:00:13 +08:00
func RemoveTrainState(vs *VerifySimulation, id string) {
allTrainMap := &vs.Memory.Status.TrainStateMap
2023-08-02 15:50:46 +08:00
d, ok := allTrainMap.Load(id)
if ok {
2023-08-02 15:50:46 +08:00
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()})
}
// 从仿真内存中移除列车
2023-08-02 15:50:46 +08:00
t.Show = false
allTrainMap.Store(id, t)
} else {
panic(fmt.Sprintf("列车【%s】不存在", id))
}
}