rts-sim-module/fi/axle_section.go

121 lines
3.5 KiB
Go
Raw Normal View History

package fi
import (
"fmt"
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/entity"
"strings"
)
// AxleSectionFaultOccDrive 区段故障占用设置
func AxleSectionFaultOccDrive(w ecs.World, sectionId string, set bool) error {
r := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
wd := entity.GetWorldData(w)
sectionModel := wd.Repo.FindPhysicalSection(sectionId)
if sectionModel == nil {
return ecs.NewErrResult(fmt.Errorf("区段模型[%s]不存实体", sectionId))
}
//
sectionEntry := wd.EntityMap[sectionId]
if sectionEntry == nil {
return ecs.NewErrResult(fmt.Errorf("区段[%s]实体不存在", sectionId))
}
if set { //计轴故障设置
if !sectionEntry.HasComponent(component.AxleSectionForceOccupied) {
sectionEntry.AddComponent(component.AxleSectionForceOccupied)
}
} else { //计轴故障取消
if sectionEntry.HasComponent(component.AxleSectionForceOccupied) {
sectionEntry.RemoveComponent(component.AxleSectionForceOccupied)
}
}
return ecs.NewOkEmptyResult()
})
return r.Err
}
// AxleSectionDrstDrive 直接复位
func AxleSectionDrstDrive(w ecs.World, sectionId string) (*AxleSectionState, error) {
r := <-ecs.Request[*AxleSectionState](w, func() ecs.Result[*AxleSectionState] {
wd := entity.GetWorldData(w)
entry := wd.EntityMap[sectionId]
if entry == nil {
return ecs.NewResult[*AxleSectionState](nil, fmt.Errorf("区段[%s]实体不存在", sectionId))
}
axleManager := component.AxleManagerType.Get(entry)
state := &AxleSectionState{}
if axleManager.Count != 0 {
state.Rjo = true
} else {
entry.RemoveComponent(component.AxleSectionForceOccupied)
state.Rac = true
}
return ecs.NewOkResult(state)
})
return r.Val, r.Err
}
// AxleSectionPdrstDrive 预复位
func AxleSectionPdrstDrive(w ecs.World, sectionId string) (*AxleSectionState, error) {
r := <-ecs.Request[*AxleSectionState](w, func() ecs.Result[*AxleSectionState] {
wd := entity.GetWorldData(w)
entry := wd.EntityMap[sectionId]
if entry == nil {
return ecs.NewResult[*AxleSectionState](nil, fmt.Errorf("区段[%s]实体不存在", sectionId))
}
axleManager := component.AxleManagerType.Get(entry)
state := &AxleSectionState{}
if axleManager.Count != 0 {
state.Rjo = true
} else {
state.Rac = true
}
return ecs.NewOkResult(state)
})
return r.Val, r.Err
}
// FindAxleSectionsStatus 获取计轴区段的相关状态
func FindAxleSectionsStatus(w ecs.World, sectionIds []string) ([]*AxleSectionState, error) {
r := <-ecs.Request[[]*AxleSectionState](w, func() ecs.Result[[]*AxleSectionState] {
wd := entity.GetWorldData(w)
var msg []*AxleSectionState
var esb = strings.Builder{} //收集未找到的区段的id
for _, sectionId := range sectionIds {
entry := wd.EntityMap[sectionId]
if entry == nil {
esb.WriteString(fmt.Sprintf("%s,", sectionId))
continue
}
axleManager := component.AxleManagerType.Get(entry)
msg = append(msg, &AxleSectionState{
Id: sectionId,
Clr: !axleManager.Occupied,
Occ: axleManager.Occupied,
})
}
if esb.Len() > 0 {
return ecs.NewResult(msg, fmt.Errorf("区段非计轴区段或计轴区段状态不存在:[%s]", esb.String()))
} else {
return ecs.NewResult(msg, nil)
}
})
return r.Val, r.Err
}
type AxleSectionState struct {
//uid
Id string
//0-bit7 计轴出清
Clr bool
//0-bit6 计轴占用
Occ bool
//1-bit6 计轴复位反馈
Rac bool
//1-bit5 运营原因拒绝计轴复位
Rjo bool
//1-bit4 技术原因拒绝计轴复位
Rjt bool
}