124 lines
3.6 KiB
Go
124 lines
3.6 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)
|
|
}
|
|
|
|
// 修改列车状态
|
|
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))
|
|
}
|
|
}
|
|
|
|
/*
|
|
// 列车占用状态
|
|
func GetDeviceOccByTrainState(vs *VerifySimulation, ts *state.TrainState) {
|
|
if ts.DevicePort == "" { // 区段
|
|
if ts.PointTo { // 如果是运行方向是从A -> B
|
|
if ts.HeadOffset >= ts.TrainLength { // 如果车头偏移量大于车身长度,只占用当前区段
|
|
fmt.Println(ts.HeadDeviceUId)
|
|
} else { // 如果不够,向后找占用设备
|
|
s1 := vs.Repo.FindPhysicalSection(ts.HeadDeviceUId)
|
|
l := int64(math.Abs(float64(s1.BLinkPosition().Offset() - s1.ALinkPosition().Offset())))
|
|
// 剩余长度
|
|
sl := ts.TrainLength - l
|
|
// 寻找下一段设备
|
|
ar := s1.ARelation()
|
|
if condition {
|
|
|
|
}
|
|
if ar.Device().Type() == rtproto.DeviceType_DeviceType_PhysicalSection { // 区段
|
|
|
|
}
|
|
|
|
}
|
|
} else {
|
|
|
|
}
|
|
} else { // 道岔
|
|
|
|
}
|
|
// 车头所在位置、列车长度
|
|
// 运行方向
|
|
// 占用设备类型、设备长度
|
|
// 占用设备ID、设备偏移
|
|
}
|
|
*/
|