240 lines
7.7 KiB
Go
240 lines
7.7 KiB
Go
package fi
|
||
|
||
import (
|
||
"fmt"
|
||
"strings"
|
||
|
||
"joylink.club/ecs"
|
||
"joylink.club/rtsssimulation/component"
|
||
"joylink.club/rtsssimulation/entity"
|
||
)
|
||
|
||
// AxleSectionDrstDrive 计轴直接复位操作
|
||
//
|
||
// set : true-设置,false-取消
|
||
func AxleSectionDrstDrive(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))
|
||
}
|
||
// if set {
|
||
// axleSectionEntry := wd.EntityMap[sectionId]
|
||
// if !axleSectionEntry.HasComponent(component.AxleSectionFaultTag) {
|
||
// return ecs.NewErrResult(fmt.Errorf("区段[%s]非故障占用,无法进行复位操作", sectionId))
|
||
// }
|
||
// }
|
||
//
|
||
faDcAxleDeviceEntry := entity.FindAxleManageDevice(wd, sectionModel.CentralizedStation())
|
||
|
||
if faDcAxleDeviceEntry == nil {
|
||
return ecs.NewErrResult(fmt.Errorf("计轴管理设备[%s]实体不存在", sectionModel.CentralizedStation()))
|
||
}
|
||
faDcAxleDevice := component.AxleManageDeviceType.Get(faDcAxleDeviceEntry)
|
||
axleRuntime := faDcAxleDevice.FindAdr(sectionId)
|
||
if axleRuntime == nil {
|
||
return ecs.NewErrResult(fmt.Errorf("计轴管理设备[%s]中不存在区段[%s]的计轴设备", sectionModel.CentralizedStation(), sectionId))
|
||
}
|
||
axleRuntime.Drst = set
|
||
//
|
||
return ecs.NewOkEmptyResult()
|
||
})
|
||
return r.Err
|
||
}
|
||
|
||
// AxleSectionPdrstDrive 计轴预复位操作
|
||
//
|
||
// set : true-设置,false-取消
|
||
func AxleSectionPdrstDrive(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))
|
||
}
|
||
// if set {
|
||
// axleSectionEntry := wd.EntityMap[sectionId]
|
||
// if !axleSectionEntry.HasComponent(component.AxleSectionFaultTag) {
|
||
// return ecs.NewErrResult(fmt.Errorf("区段[%s]非故障占用,无法进行复位操作", sectionId))
|
||
// }
|
||
// }
|
||
//
|
||
faDcAxleDeviceEntry := entity.FindAxleManageDevice(wd, sectionModel.CentralizedStation())
|
||
|
||
if faDcAxleDeviceEntry == nil {
|
||
return ecs.NewErrResult(fmt.Errorf("计轴管理设备[%s]实体不存在", sectionModel.CentralizedStation()))
|
||
}
|
||
faDcAxleDevice := component.AxleManageDeviceType.Get(faDcAxleDeviceEntry)
|
||
axleRuntime, ok := faDcAxleDevice.Adrs[sectionId]
|
||
if !ok {
|
||
return ecs.NewErrResult(fmt.Errorf("计轴管理设备[%s]中不存在区段[%s]的计轴设备", sectionModel.CentralizedStation(), sectionId))
|
||
}
|
||
axleRuntime.Pdrst = set
|
||
//
|
||
return ecs.NewOkEmptyResult()
|
||
})
|
||
return r.Err
|
||
}
|
||
|
||
// AxleSectionFaultOccDrive 区段故障占用设置
|
||
//
|
||
// set : true - 设置故障占用;false - 取消故障占用
|
||
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.AxleSectionFaultTag) {
|
||
sectionEntry.AddComponent(component.AxleSectionFaultTag)
|
||
}
|
||
} else { //计轴故障取消
|
||
if sectionEntry.HasComponent(component.AxleSectionFaultTag) {
|
||
sectionEntry.RemoveComponent(component.AxleSectionFaultTag)
|
||
}
|
||
}
|
||
//
|
||
return ecs.NewOkEmptyResult()
|
||
})
|
||
return r.Err
|
||
}
|
||
|
||
// AxleSectionTrainDrive 计轴区段内车进入出清设置
|
||
//
|
||
// trainIn : true - 计轴区段内有车;false-计轴区段出清
|
||
func AxleSectionTrainDrive(w ecs.World, sectionId string, trainIn 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))
|
||
}
|
||
axleDevice := component.AxlePhysicalSectionType.Get(sectionEntry)
|
||
if trainIn {
|
||
axleDevice.UpdateCount(1)
|
||
} else {
|
||
axleDevice.UpdateCount(0)
|
||
}
|
||
//
|
||
return ecs.NewOkEmptyResult()
|
||
})
|
||
return 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 {
|
||
find := false
|
||
sectionModel := wd.Repo.FindPhysicalSection(sectionId)
|
||
if sectionModel != nil {
|
||
faDcAxleDeviceEntry := entity.FindAxleManageDevice(wd, sectionModel.CentralizedStation())
|
||
if faDcAxleDeviceEntry != nil {
|
||
faDcAxleDevice := component.AxleManageDeviceType.Get(faDcAxleDeviceEntry)
|
||
axleDevice := faDcAxleDevice.FindAdr(sectionId)
|
||
if axleDevice != nil {
|
||
sectionEntry, _ := entity.GetEntityByUid(w, sectionId)
|
||
if sectionEntry != nil {
|
||
if sectionEntry.HasComponent(component.AxlePhysicalSectionType) { //计轴物理区段实体
|
||
as := &AxleSectionState{}
|
||
state := component.PhysicalSectionStateType.Get(sectionEntry)
|
||
as.Id = component.UidType.Get(sectionEntry).Id
|
||
as.Clr = !state.Occ
|
||
as.Occ = state.Occ
|
||
as.Rac = axleDevice.Rac
|
||
as.Rjt = axleDevice.Rjt
|
||
as.Rjo = axleDevice.Rjo
|
||
//
|
||
msg = append(msg, as)
|
||
find = true
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
//
|
||
if !find {
|
||
esb.WriteString(fmt.Sprintf("%s,", sectionId))
|
||
}
|
||
} //for
|
||
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
|
||
}
|
||
|
||
// AxleSectionRstDrive 复位(直接复位、预复位)
|
||
func AxleSectionRstDrive(w ecs.World, cmds []*AxleSectionCmd) error {
|
||
r := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
|
||
wd := entity.GetWorldData(w)
|
||
var esb = strings.Builder{} //收集未找到的区段的id
|
||
find := false
|
||
for _, cmd := range cmds {
|
||
sectionId := cmd.SectionId
|
||
sectionModel := wd.Repo.FindPhysicalSection(sectionId)
|
||
if sectionModel != nil {
|
||
faDcAxleDeviceEntry := entity.FindAxleManageDevice(wd, sectionModel.CentralizedStation())
|
||
if faDcAxleDeviceEntry != nil {
|
||
faDcAxleDevice := component.AxleManageDeviceType.Get(faDcAxleDeviceEntry)
|
||
axleRuntime := faDcAxleDevice.FindAdr(sectionId)
|
||
if axleRuntime != nil {
|
||
axleRuntime.Pdrst = cmd.Pdrst
|
||
axleRuntime.Drst = cmd.Drst
|
||
find = true
|
||
}
|
||
}
|
||
}
|
||
if !find {
|
||
esb.WriteString(fmt.Sprintf("%s,", sectionId))
|
||
}
|
||
} //for
|
||
if esb.Len() > 0 {
|
||
return ecs.NewErrResult(fmt.Errorf("计轴区段复位操作,失败列表[%s]", esb.String()))
|
||
} else {
|
||
return ecs.NewOkEmptyResult()
|
||
}
|
||
})
|
||
return r.Err
|
||
}
|
||
|
||
type AxleSectionCmd struct {
|
||
SectionId string
|
||
Drst bool
|
||
Pdrst bool
|
||
}
|