proto文件增加设备状态;实现应答器的相关接口

This commit is contained in:
joylink_zhangsai 2024-01-10 14:06:00 +08:00
parent a4c17218f7
commit e8c0d58799
7 changed files with 33 additions and 21 deletions

View File

@ -5,6 +5,7 @@ import (
"joylink.club/rtsssimulation/component/component_data" "joylink.club/rtsssimulation/component/component_data"
"joylink.club/rtsssimulation/component/component_proto" "joylink.club/rtsssimulation/component/component_proto"
"joylink.club/rtsssimulation/consts" "joylink.club/rtsssimulation/consts"
"joylink.club/rtsssimulation/repository/model/proto"
) )
var ( var (
@ -65,3 +66,4 @@ type Counter struct {
var CounterType = ecs.NewComponentType[Counter]() var CounterType = ecs.NewComponentType[Counter]()
var LinkPositionType = ecs.NewComponentType[component_proto.LinkPosition]() var LinkPositionType = ecs.NewComponentType[component_proto.LinkPosition]()
var KmType = ecs.NewComponentType[proto.Kilometer]()

View File

@ -14,13 +14,6 @@ func LoadBalises(w ecs.World) error {
balises := data.Repo.TransponderList() balises := data.Repo.TransponderList()
for _, b := range balises { for _, b := range balises {
be := newBaliseEntity(w, b, data) be := newBaliseEntity(w, b, data)
//应答器位置
be.AddComponent(component.LinkPositionType)
component.LinkPositionType.SetValue(be, component_proto.LinkPosition{
LinkId: b.LinkPosition().Link().Id(),
Offset: b.LinkPosition().Offset(),
})
//应答器类型
switch b.TransponderType() { switch b.TransponderType() {
case proto.Transponder_FB: case proto.Transponder_FB:
be.AddComponent(component.BaliseFB) be.AddComponent(component.BaliseFB)
@ -40,9 +33,14 @@ func newBaliseEntity(w ecs.World, td *repository.Transponder, worldData *compone
uid := td.Id() uid := td.Id()
entry, ok := worldData.EntityMap[uid] entry, ok := worldData.EntityMap[uid]
if !ok { if !ok {
entry = w.Entry(w.Create(component.UidType, component.BaliseStateType)) entry = w.Entry(w.Create(component.UidType, component.BaliseStateType, component.LinkPositionType, component.KmType))
component.UidType.SetValue(entry, component.Uid{Id: uid}) component.UidType.SetValue(entry, component.Uid{Id: uid})
component.BaliseStateType.Set(entry, &component.BaliseState{}) component.BaliseStateType.Set(entry, &component.BaliseState{})
component.LinkPositionType.SetValue(entry, component_proto.LinkPosition{
LinkId: td.LinkPosition().Link().Id(),
Offset: td.LinkPosition().Offset(),
})
component.KmType.Set(entry, td.Km())
worldData.EntityMap[uid] = entry worldData.EntityMap[uid] = entry
} }
return entry return entry

View File

@ -4,8 +4,8 @@ import (
"joylink.club/rtsssimulation/repository/model/proto" "joylink.club/rtsssimulation/repository/model/proto"
) )
// 转换公里标 // ConvertKilometer 转换公里标
func convertKilometer(repo *Repository, km *proto.Kilometer, cs string) (*proto.Kilometer, error) { func ConvertKilometer(repo *Repository, km *proto.Kilometer, cs string) (*proto.Kilometer, error) {
if km.CoordinateSystem == cs { if km.CoordinateSystem == cs {
return km, nil return km, nil
} }

View File

@ -70,6 +70,14 @@ func (l *Link) PhysicalSections() []*PhysicalSection {
return l.physicalSections return l.physicalSections
} }
func (l *Link) AKm() *proto.Kilometer {
return l.aKm
}
func (l *Link) BKm() *proto.Kilometer {
return l.bKm
}
func (l *Link) bindTurnoutPort(port proto.Port, turnoutPort *TurnoutPort) { func (l *Link) bindTurnoutPort(port proto.Port, turnoutPort *TurnoutPort) {
switch port { switch port {
case proto.Port_A: case proto.Port_A:

View File

@ -265,7 +265,7 @@ func completeTurnoutPortKm(repo *Repository) error {
if tpKm == nil { if tpKm == nil {
dp := turnout.findDevicePortByPort(port) dp := turnout.findDevicePortByPort(port)
otherTurnout := repo.turnoutMap[dp.Device().Id()] otherTurnout := repo.turnoutMap[dp.Device().Id()]
otherKm, err := convertKilometer(repo, otherTurnout.km, turnout.km.CoordinateSystem) otherKm, err := ConvertKilometer(repo, otherTurnout.km, turnout.km.CoordinateSystem)
if err != nil { if err != nil {
return err return err
} }
@ -613,7 +613,7 @@ func buildLinks(repo *Repository) error {
link.bindKm(endKm, proto.Port_B) link.bindKm(endKm, proto.Port_B)
} }
//计算Link长度 //计算Link长度
convertedKm, err := convertKilometer(repo, endKm, linkZeroKm.CoordinateSystem) convertedKm, err := ConvertKilometer(repo, endKm, linkZeroKm.CoordinateSystem)
if err != nil { if err != nil {
return err return err
} }
@ -688,8 +688,8 @@ func findEndTurnoutPortOrEndKm(repo *Repository, link *Link, startTp *TurnoutPor
// 关联正常非道岔物理区段与Link // 关联正常非道岔物理区段与Link
func relatePhysicalSectionAndLink(repo *Repository, section *PhysicalSection, link *Link, baseKm *proto.Kilometer) { func relatePhysicalSectionAndLink(repo *Repository, section *PhysicalSection, link *Link, baseKm *proto.Kilometer) {
link.bindPhysicalSections(section) link.bindPhysicalSections(section)
aKm, _ := convertKilometer(repo, section.aKm, baseKm.CoordinateSystem) aKm, _ := ConvertKilometer(repo, section.aKm, baseKm.CoordinateSystem)
bKm, _ := convertKilometer(repo, section.bKm, baseKm.CoordinateSystem) bKm, _ := ConvertKilometer(repo, section.bKm, baseKm.CoordinateSystem)
aOffset := number.Abs(aKm.Value - baseKm.Value) aOffset := number.Abs(aKm.Value - baseKm.Value)
bOffset := number.Abs(bKm.Value - baseKm.Value) bOffset := number.Abs(bKm.Value - baseKm.Value)
section.aLinkPosition = &LinkPosition{ section.aLinkPosition = &LinkPosition{
@ -734,7 +734,7 @@ func relateDevicesAndLink(repo *Repository, link *Link, startKm *proto.Kilometer
if km == nil || km.CoordinateSystem == "" { if km == nil || km.CoordinateSystem == "" {
continue continue
} }
convertedKm, err := convertKilometer(repo, km, startKm.CoordinateSystem) convertedKm, err := ConvertKilometer(repo, km, startKm.CoordinateSystem)
if err != nil { if err != nil {
return err return err
} }
@ -761,7 +761,7 @@ func interrelateLinkAndTurnout(repo *Repository, linkZeroKm *proto.Kilometer, tp
tp.turnout.bindLinkPort(tp.port, linkPort) tp.turnout.bindLinkPort(tp.port, linkPort)
linkPort.link.bindTurnoutPort(linkPort.port, tp) linkPort.link.bindTurnoutPort(linkPort.port, tp)
tpKm := tp.turnout.findBoundaryKmByPort(tp.port) tpKm := tp.turnout.findBoundaryKmByPort(tp.port)
tpKm, _ = convertKilometer(repo, tpKm, linkZeroKm.CoordinateSystem) tpKm, _ = ConvertKilometer(repo, tpKm, linkZeroKm.CoordinateSystem)
offset := number.Abs(tpKm.Value - linkZeroKm.Value) offset := number.Abs(tpKm.Value - linkZeroKm.Value)
tp.turnout.bindLinkPosition(tp.port, &LinkPosition{ tp.turnout.bindLinkPosition(tp.port, &LinkPosition{
link: linkPort.link, link: linkPort.link,
@ -820,7 +820,7 @@ func slopeRelateLink(repo *Repository) error {
func calculateLinkSegment(repo *Repository, startKm *proto.Kilometer, func calculateLinkSegment(repo *Repository, startKm *proto.Kilometer,
endKm *proto.Kilometer) (*LinkPosition, *LinkPosition, error) { endKm *proto.Kilometer) (*LinkPosition, *LinkPosition, error) {
endKm, err := convertKilometer(repo, endKm, startKm.CoordinateSystem) endKm, err := ConvertKilometer(repo, endKm, startKm.CoordinateSystem)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }

View File

@ -34,3 +34,7 @@ func (t *Transponder) bindLinkPosition(position *LinkPosition) {
func (t *Transponder) LinkPosition() *LinkPosition { func (t *Transponder) LinkPosition() *LinkPosition {
return t.linkPosition return t.linkPosition
} }
func (t *Transponder) Km() *proto.Kilometer {
return t.km
}

View File

@ -1,7 +1,5 @@
package number package number
import "math"
type Number interface { type Number interface {
~int | ~int8 | ~int16 | ~int32 | int64 | ~int | ~int8 | ~int16 | ~int32 | int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
@ -31,6 +29,8 @@ func Max[T Number](a T, b T) T {
} }
func Abs[T Number](num T) T { func Abs[T Number](num T) T {
abs := math.Abs(float64(num)) if num < 0 {
return T(abs) return -num
}
return num
} }