rts-sim-module/fi/psd.go
2023-12-08 17:43:20 +08:00

354 lines
10 KiB
Go

package fi
import (
"errors"
"fmt"
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/component/component_proto"
"joylink.club/rtsssimulation/entity"
)
func SetInterlockKm(world ecs.World, psdId string, group int32) error {
result := <-ecs.Request[ecs.EmptyType](world, func() ecs.Result[ecs.EmptyType] {
wd := entity.GetWorldData(world)
entry, ok := wd.EntityMap[psdId]
if ok {
err := setInterlockKm(wd, entry, group)
if err != nil {
return ecs.NewErrResult(err)
}
} else {
return ecs.NewErrResult(fmt.Errorf("未找到id=%s的屏蔽门", psdId))
}
return ecs.NewOkEmptyResult()
})
return result.Err
}
func CancelInterlockKm(world ecs.World, id string, group int32) error {
result := <-ecs.Request[ecs.EmptyType](world, func() ecs.Result[ecs.EmptyType] {
wd := entity.GetWorldData(world)
entry, ok := wd.EntityMap[id]
if ok {
err := cancelInterlockKm(wd, entry, group)
if err != nil {
return ecs.NewErrResult(err)
}
} else {
return ecs.NewErrResult(fmt.Errorf("未找到id=%s的屏蔽门", id))
}
return ecs.NewOkEmptyResult()
})
return result.Err
}
func SetInterlockGm(world ecs.World, id string) error {
result := <-ecs.Request[ecs.EmptyType](world, func() ecs.Result[ecs.EmptyType] {
wd := entity.GetWorldData(world)
entry, ok := wd.EntityMap[id]
if ok {
err := setInterlockGm(wd, entry)
if err != nil {
return ecs.NewErrResult(err)
}
} else {
return ecs.NewErrResult(fmt.Errorf("未找到id=%s的屏蔽门", id))
}
return ecs.NewOkEmptyResult()
})
return result.Err
}
func CancelInterlockGm(world ecs.World, id string) error {
result := <-ecs.Request[ecs.EmptyType](world, func() ecs.Result[ecs.EmptyType] {
wd := entity.GetWorldData(world)
entry, ok := wd.EntityMap[id]
if ok {
err := cancelInterlockGm(wd, entry)
if err != nil {
return ecs.NewErrResult(err)
}
} else {
return ecs.NewErrResult(fmt.Errorf("未找到id=%s的屏蔽门", id))
}
return ecs.NewOkEmptyResult()
})
return result.Err
}
func SetPsdFault(world ecs.World, id string, fault component_proto.Psd_Fault, asdCodes []int32) error {
result := <-ecs.Request[ecs.EmptyType](world, func() ecs.Result[ecs.EmptyType] {
wd := entity.GetWorldData(world)
psdEntry, ok := wd.EntityMap[id]
if !ok {
return ecs.NewErrResult(fmt.Errorf("未找到id=%s的屏蔽门", id))
}
tag, err := component.GetAsdFaultTag(fault)
if err != nil {
return ecs.NewErrResult(err)
}
asdList := component.AsdListType.Get(psdEntry).List
for _, code := range asdCodes {
asdEntry := asdList[int(code-1)]
asdEntry.AddComponent(tag)
}
return ecs.NewOkEmptyResult()
})
return result.Err
}
func CancelPsdFault(world ecs.World, id string, fault component_proto.Psd_Fault, asdCodes []int32) error {
result := <-ecs.Request[ecs.EmptyType](world, func() ecs.Result[ecs.EmptyType] {
wd := entity.GetWorldData(world)
psdEntry, ok := wd.EntityMap[id]
if !ok {
return ecs.NewErrResult(fmt.Errorf("未找到id=%s的屏蔽门", id))
}
tag, err := component.GetAsdFaultTag(fault)
if err != nil {
return ecs.NewErrResult(err)
}
asdList := component.AsdListType.Get(psdEntry).List
for _, code := range asdCodes {
asdEntry := asdList[int(code-1)]
asdEntry.RemoveComponent(tag)
}
return ecs.NewOkEmptyResult()
})
return result.Err
}
func SetQDTC(world ecs.World, id string) error {
result := <-ecs.Request[ecs.EmptyType](world, func() ecs.Result[ecs.EmptyType] {
wd := entity.GetWorldData(world)
entry, ok := wd.EntityMap[id]
if ok {
err := setQDTC(wd, entry)
if err != nil {
return ecs.NewErrResult(err)
}
} else {
return ecs.NewErrResult(fmt.Errorf("未找到id=%s的屏蔽门", id))
}
return ecs.NewOkEmptyResult()
})
return result.Err
}
func CancelQDTC(world ecs.World, id string) error {
result := <-ecs.Request[ecs.EmptyType](world, func() ecs.Result[ecs.EmptyType] {
wd := entity.GetWorldData(world)
entry, ok := wd.EntityMap[id]
if ok {
err := cancelQDTC(wd, entry)
if err != nil {
return ecs.NewErrResult(err)
}
} else {
return ecs.NewErrResult(fmt.Errorf("未找到id=%s的屏蔽门", id))
}
return ecs.NewOkEmptyResult()
})
return result.Err
}
func SetTZTC(world ecs.World, id string) error {
result := <-ecs.Request[ecs.EmptyType](world, func() ecs.Result[ecs.EmptyType] {
wd := entity.GetWorldData(world)
entry, ok := wd.EntityMap[id]
if ok {
err := setTZTC(wd, entry)
if err != nil {
return ecs.NewErrResult(err)
}
} else {
return ecs.NewErrResult(fmt.Errorf("未找到id=%s的屏蔽门", id))
}
return ecs.NewOkEmptyResult()
})
return result.Err
}
func CancelTZTC(world ecs.World, id string) error {
result := <-ecs.Request[ecs.EmptyType](world, func() ecs.Result[ecs.EmptyType] {
wd := entity.GetWorldData(world)
entry, ok := wd.EntityMap[id]
if ok {
err := cancelTZTC(wd, entry)
if err != nil {
return ecs.NewErrResult(err)
}
} else {
return ecs.NewErrResult(fmt.Errorf("未找到id=%s的屏蔽门", id))
}
return ecs.NewOkEmptyResult()
})
return result.Err
}
func cancelTZTC(wd *component.WorldData, psdEntry *ecs.Entry) error {
if psdEntry.HasComponent(component.PsdCircuitType) {
circuit := component.PsdCircuitType.Get(psdEntry)
return wd.SetQdBit(component.UidType.Get(circuit.TZTCJ).Id, false)
} else {
psc := component.PscType.Get(psdEntry)
psc.TZTC = false
}
return nil
}
func setTZTC(wd *component.WorldData, psdEntry *ecs.Entry) error {
if psdEntry.HasComponent(component.PsdCircuitType) {
circuit := component.PsdCircuitType.Get(psdEntry)
return wd.SetQdBits([]*component.QdBitParam{
component.NewQdBitParam(component.UidType.Get(circuit.QDTCJ).Id, false),
component.NewQdBitParam(component.UidType.Get(circuit.TZTCJ).Id, true),
})
} else {
psc := component.PscType.Get(psdEntry)
psc.QDTC = false
psc.TZTC = true
}
return nil
}
func cancelQDTC(wd *component.WorldData, psdEntry *ecs.Entry) error {
if psdEntry.HasComponent(component.PsdCircuitType) {
circuit := component.PsdCircuitType.Get(psdEntry)
return wd.SetQdBit(component.UidType.Get(circuit.QDTCJ).Id, false)
} else {
psc := component.PscType.Get(psdEntry)
psc.QDTC = false
}
return nil
}
func setQDTC(wd *component.WorldData, psdEntry *ecs.Entry) error {
if psdEntry.HasComponent(component.PsdCircuitType) {
circuit := component.PsdCircuitType.Get(psdEntry)
return wd.SetQdBits([]*component.QdBitParam{
component.NewQdBitParam(component.UidType.Get(circuit.QDTCJ).Id, true),
component.NewQdBitParam(component.UidType.Get(circuit.TZTCJ).Id, false),
})
} else {
psc := component.PscType.Get(psdEntry)
psc.QDTC = true
psc.TZTC = false
}
return nil
}
// 设置联锁开门
func setInterlockKm(wd *component.WorldData, psdEntry *ecs.Entry, group int32) error {
if psdEntry.HasComponent(component.PsdCircuitType) { //有联锁区段电路
circuit := component.PsdCircuitType.Get(psdEntry)
if circuit.KMJMap[0] != nil { //0编组意味着屏蔽门仅有一个开门继电器
return setRelayDriveKm(wd, circuit, 0)
} else {
kmj := circuit.KMJMap[group]
if kmj == nil {
id := component.UidType.Get(psdEntry).Id
return errors.New(fmt.Sprintf("屏蔽门[id:%s]不支持[%d]编组操作", id, group))
}
return setRelayDriveKm(wd, circuit, group)
}
} else {
psc := component.PscType.Get(psdEntry)
psc.InterlockKmGroup[group] = true
}
return nil
}
// 取消联锁开门
func cancelInterlockKm(wd *component.WorldData, psdEntry *ecs.Entry, group int32) error {
if psdEntry.HasComponent(component.PsdCircuitType) { //有联锁区段电路
circuit := component.PsdCircuitType.Get(psdEntry)
if circuit.KMJMap[0] != nil { //0编组意味着屏蔽门仅有一个开门继电器
return cancelRelayDriveKm(wd, circuit, 0)
} else {
kmj := circuit.KMJMap[group]
if kmj == nil {
id := component.UidType.Get(psdEntry).Id
return errors.New(fmt.Sprintf("屏蔽门[id:%s]不支持[%d]编组操作", id, group))
}
return cancelRelayDriveKm(wd, circuit, group)
}
} else {
psc := component.PscType.Get(psdEntry)
psc.InterlockKmGroup[group] = false
}
return nil
}
// 设置联锁关门
func setInterlockGm(wd *component.WorldData, psdEntry *ecs.Entry) error {
if psdEntry.HasComponent(component.PsdCircuitType) { //有联锁区段电路
circuit := component.PsdCircuitType.Get(psdEntry)
return setRelayDriveGm(wd, circuit)
} else {
psc := component.PscType.Get(psdEntry)
for i, _ := range psc.InterlockKmGroup {
psc.InterlockKmGroup[i] = false
}
psc.InterlockGM = true
}
return nil
}
// 取消联锁关门
func cancelInterlockGm(wd *component.WorldData, psdEntry *ecs.Entry) error {
if psdEntry.HasComponent(component.PsdCircuitType) { //有联锁区段电路
circuit := component.PsdCircuitType.Get(psdEntry)
return cancelRelayDriveGm(wd, circuit)
} else {
psc := component.PscType.Get(psdEntry)
psc.InterlockGM = false
}
return nil
}
// 设置继电器驱动开门
func setRelayDriveKm(wd *component.WorldData, circuit *component.PsdCircuit, group int32) error {
var params []*component.QdBitParam
params = append(params, component.NewQdBitParam(component.UidType.Get(circuit.GMJ).Id, false))
for g, entry := range circuit.KMJMap {
if g == group {
params = append(params, component.NewQdBitParam(component.UidType.Get(entry).Id, true))
} else {
params = append(params, component.NewQdBitParam(component.UidType.Get(entry).Id, false))
}
}
return wd.SetQdBits(params)
}
// 取消继电器驱动开门
func cancelRelayDriveKm(wd *component.WorldData, circuit *component.PsdCircuit, group int32) error {
var params []*component.QdBitParam
for _, entry := range circuit.KMJMap {
params = append(params, component.NewQdBitParam(component.UidType.Get(entry).Id, false))
}
return wd.SetQdBits(params)
}
// 设置继电器驱动关门
func setRelayDriveGm(wd *component.WorldData, circuit *component.PsdCircuit) error {
var params []*component.QdBitParam
params = append(params, component.NewQdBitParam(component.UidType.Get(circuit.GMJ).Id, true))
for _, entry := range circuit.KMJMap {
params = append(params, component.NewQdBitParam(component.UidType.Get(entry).Id, false))
}
return wd.SetQdBits(params)
}
// 取消继电器驱动关门
func cancelRelayDriveGm(wd *component.WorldData, circuit *component.PsdCircuit) error {
var params []*component.QdBitParam
params = append(params, component.NewQdBitParam(component.UidType.Get(circuit.GMJ).Id, false))
return wd.SetQdBits(params)
}