2023-08-01 17:08:45 +08:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-10-12 10:10:23 +08:00
|
|
|
"log/slog"
|
2023-08-02 15:47:48 +08:00
|
|
|
"math"
|
|
|
|
"strconv"
|
2023-08-01 17:08:45 +08:00
|
|
|
|
2023-08-02 15:54:22 +08:00
|
|
|
"joylink.club/bj-rtsts-server/dto"
|
2023-10-19 09:33:40 +08:00
|
|
|
"joylink.club/bj-rtsts-server/third_party/dynamics"
|
|
|
|
"joylink.club/bj-rtsts-server/third_party/message"
|
2023-08-02 15:54:22 +08:00
|
|
|
|
2023-08-25 15:35:00 +08:00
|
|
|
"google.golang.org/protobuf/proto"
|
2023-10-26 17:16:07 +08:00
|
|
|
"joylink.club/bj-rtsts-server/ts/protos/state"
|
2023-08-01 17:08:45 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// 增加列车状态
|
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
|
2023-08-01 17:08:45 +08:00
|
|
|
_, ok := allTrainMap.Load(status.Id)
|
|
|
|
if ok {
|
|
|
|
panic(fmt.Sprintf("列车【%s】已存在", status.Id))
|
|
|
|
}
|
2023-08-02 15:50:46 +08:00
|
|
|
// 显示状态
|
|
|
|
status.Show = true
|
2023-08-02 15:47:48 +08:00
|
|
|
//向动力学发送初始化请求
|
|
|
|
trainIndex, _ := strconv.ParseUint(status.Id, 10, 16)
|
2023-08-14 16:27:03 +08:00
|
|
|
// 映射link、偏移量、运行方向
|
2023-10-26 10:05:28 +08:00
|
|
|
linkId, loffset, up, pointTo, kilometer := QueryEcsLinkByDeviceInfo(vs.Repo, mapId, status)
|
2023-08-14 16:27:03 +08:00
|
|
|
status.Up = up
|
2023-08-15 10:18:39 +08:00
|
|
|
status.PointTo = pointTo
|
2023-09-12 16:53:25 +08:00
|
|
|
status.TrainKilometer = kilometer
|
2023-10-19 09:33:40 +08:00
|
|
|
err := dynamics.Default().RequestAddTrain(&message.InitTrainInfo{
|
2023-08-24 10:24:56 +08:00
|
|
|
TrainIndex: uint16(trainIndex),
|
|
|
|
LinkIndex: uint16(linkId),
|
|
|
|
LinkOffset: uint32(loffset),
|
|
|
|
Speed: uint16(math.Round(float64(status.Speed * 10))),
|
|
|
|
Up: status.Up,
|
|
|
|
TrainLength: uint16(status.TrainLength),
|
2023-08-02 15:47:48 +08:00
|
|
|
})
|
2023-10-12 10:10:23 +08:00
|
|
|
slog.Debug("添加列车", "trainIndex", trainIndex, "HeadDeviceId", status.HeadDeviceId, "HeadOffset", status.HeadOffset)
|
|
|
|
slog.Debug("列车初始化", "trainIndex", trainIndex, "linkId", linkId, "loffset", loffset)
|
2023-10-19 09:33:40 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.DynamicsError, Message: err.Error()})
|
2023-08-02 15:47:48 +08:00
|
|
|
}
|
2023-08-25 16:09:04 +08:00
|
|
|
// 调用成功后初始化列车的动力学
|
|
|
|
status.DynamicState = &state.TrainDynamicState{}
|
2023-10-18 16:41:41 +08:00
|
|
|
status.VobcState = &state.TrainVobcState{}
|
2023-08-01 17:08:45 +08:00
|
|
|
// 将信息合并到当前设备状态中
|
|
|
|
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-08-01 17:08:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 删除列车状态
|
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)
|
2023-08-01 17:08:45 +08:00
|
|
|
if ok {
|
2023-08-02 15:50:46 +08:00
|
|
|
t := d.(*state.TrainState)
|
2023-08-16 09:29:28 +08:00
|
|
|
trainIndex, _ := strconv.ParseUint(id, 10, 16)
|
2023-10-19 09:33:40 +08:00
|
|
|
err := dynamics.Default().RequestRemoveTrain(&message.RemoveTrainReq{
|
2023-08-16 09:29:28 +08:00
|
|
|
TrainIndex: uint16(trainIndex),
|
|
|
|
})
|
2023-10-19 09:33:40 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.DynamicsError, Message: err.Error()})
|
2023-08-16 09:29:28 +08:00
|
|
|
}
|
2023-08-01 17:08:45 +08:00
|
|
|
// 从仿真内存中移除列车
|
2023-08-02 15:50:46 +08:00
|
|
|
t.Show = false
|
|
|
|
allTrainMap.Store(id, t)
|
2023-08-01 17:08:45 +08:00
|
|
|
} else {
|
|
|
|
panic(fmt.Sprintf("列车【%s】不存在", id))
|
|
|
|
}
|
|
|
|
}
|