2023-11-09 14:59:18 +08:00
|
|
|
|
package fi
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"joylink.club/ecs"
|
|
|
|
|
"joylink.club/rtsssimulation/component"
|
|
|
|
|
"joylink.club/rtsssimulation/entity"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// AddTrainToWorld 添加列车
|
|
|
|
|
func AddTrainToWorld(w ecs.World, trainId string) error {
|
|
|
|
|
result := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
|
|
|
|
|
wd := entity.GetWorldData(w)
|
|
|
|
|
_, find := wd.EntityMap[trainId]
|
|
|
|
|
if !find {
|
2023-11-23 15:12:16 +08:00
|
|
|
|
entity.NewTrainWithBtmEntity(w, trainId)
|
2023-11-09 14:59:18 +08:00
|
|
|
|
}
|
|
|
|
|
return ecs.NewOkEmptyResult()
|
|
|
|
|
})
|
|
|
|
|
return result.Err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RemoveTrainFromWorld 移除列车
|
|
|
|
|
func RemoveTrainFromWorld(w ecs.World, trainId string) error {
|
|
|
|
|
result := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
|
|
|
|
|
wd := entity.GetWorldData(w)
|
|
|
|
|
te, find := wd.EntityMap[trainId]
|
|
|
|
|
if find {
|
|
|
|
|
te.Remove()
|
|
|
|
|
delete(wd.EntityMap, trainId)
|
|
|
|
|
}
|
|
|
|
|
return ecs.NewOkEmptyResult()
|
|
|
|
|
})
|
|
|
|
|
return result.Err
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-10 16:03:33 +08:00
|
|
|
|
// UpdateTrainPositionFromDynamics 更新列车所在的物理区段
|
|
|
|
|
func UpdateTrainPositionFromDynamics(w ecs.World, tpi TrainPositionInfo) error {
|
2023-11-09 14:59:18 +08:00
|
|
|
|
result := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
|
|
|
|
|
wd := entity.GetWorldData(w)
|
2023-11-10 16:03:33 +08:00
|
|
|
|
te, find := wd.EntityMap[tpi.TrainId]
|
2023-11-09 14:59:18 +08:00
|
|
|
|
if find {
|
2023-11-10 16:03:33 +08:00
|
|
|
|
train := component.TrainPositionInfoType.Get(te)
|
|
|
|
|
train.Up = tpi.Up
|
|
|
|
|
train.Len = int64(tpi.Len)
|
|
|
|
|
train.HeadLink = tpi.HeadLink
|
2023-11-13 13:53:46 +08:00
|
|
|
|
train.HeadLinkOffset = int64(tpi.HeadLinkOffset)
|
2023-11-10 16:03:33 +08:00
|
|
|
|
train.TailLink = tpi.TailLink
|
2023-11-13 13:53:46 +08:00
|
|
|
|
train.TailLinkOffset = int64(tpi.TailLinkOffset)
|
2023-11-09 14:59:18 +08:00
|
|
|
|
return ecs.NewOkEmptyResult()
|
|
|
|
|
} else {
|
2023-11-10 16:03:33 +08:00
|
|
|
|
return ecs.NewErrResult(fmt.Errorf("列车[%s]实体不存在", tpi.TrainId))
|
2023-11-09 14:59:18 +08:00
|
|
|
|
}
|
2023-11-10 10:39:41 +08:00
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
return result.Err
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-23 16:30:39 +08:00
|
|
|
|
// TrainBalisePowerAmplifierSwitch 车载应答器天线功率放大器开关控制
|
|
|
|
|
func TrainBalisePowerAmplifierSwitch(w ecs.World, trainId string, turnOn bool) error {
|
|
|
|
|
result := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
|
|
|
|
|
wd := entity.GetWorldData(w)
|
|
|
|
|
te, find := wd.EntityMap[trainId]
|
|
|
|
|
if find {
|
|
|
|
|
train := component.TrainBtmType.Get(te)
|
|
|
|
|
train.PowerAmplifierSwitch = turnOn
|
|
|
|
|
return ecs.NewOkEmptyResult()
|
|
|
|
|
} else {
|
|
|
|
|
return ecs.NewErrResult(fmt.Errorf("列车[%s]实体不存在", trainId))
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return result.Err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FindTrainBaliseBtmStatus 获取车载BTM的相关状态信息
|
|
|
|
|
func FindTrainBaliseBtmStatus(w ecs.World, trainId string) ecs.Result[*TrainBaliseBtmStatus] {
|
|
|
|
|
result := <-ecs.Request[*TrainBaliseBtmStatus](w, func() ecs.Result[*TrainBaliseBtmStatus] {
|
|
|
|
|
wd := entity.GetWorldData(w)
|
|
|
|
|
te, find := wd.EntityMap[trainId]
|
|
|
|
|
if find {
|
|
|
|
|
btm := component.TrainBtmType.Get(te)
|
|
|
|
|
btmStatus := &TrainBaliseBtmStatus{
|
|
|
|
|
PowerAmplifierOn: btm.PowerAmplifierSwitch,
|
|
|
|
|
PowerAmplifierFault: false,
|
|
|
|
|
AboveBalise: btm.AboveBalise,
|
|
|
|
|
AntennaFault: false,
|
|
|
|
|
}
|
|
|
|
|
return ecs.NewOkResult(btmStatus)
|
|
|
|
|
} else {
|
|
|
|
|
return ecs.NewResult[*TrainBaliseBtmStatus](nil, fmt.Errorf("列车[%s]实体不存在", trainId))
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// /////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
type TrainBaliseBtmStatus struct {
|
|
|
|
|
PowerAmplifierOn bool //BTM功率放大器是否开启
|
|
|
|
|
PowerAmplifierFault bool //BTM功率放大器是否有故障
|
|
|
|
|
AntennaFault bool //BTM应答器天线是否有故障
|
|
|
|
|
AboveBalise bool //BTM当前是否在应答器上方
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-10 10:39:41 +08:00
|
|
|
|
type TrainPositionInfo struct {
|
|
|
|
|
//列车id
|
|
|
|
|
TrainId string
|
|
|
|
|
//列车头当前运行方向(true偏移量增大/false减小方向)
|
|
|
|
|
Up bool
|
|
|
|
|
//列车长度 mm
|
|
|
|
|
Len uint32
|
|
|
|
|
//列车所在轨道link
|
2023-11-10 10:50:09 +08:00
|
|
|
|
HeadLink string
|
2023-11-10 10:39:41 +08:00
|
|
|
|
//列车所在link偏移量(mm)
|
|
|
|
|
HeadLinkOffset uint32
|
|
|
|
|
//列车所在轨道link
|
2023-11-10 10:50:09 +08:00
|
|
|
|
TailLink string
|
2023-11-10 10:39:41 +08:00
|
|
|
|
//列车所在link偏移量(mm)
|
|
|
|
|
TailLinkOffset uint32
|
|
|
|
|
}
|