rts-sim-module/sys/circuit_sys/xcj.go

93 lines
2.7 KiB
Go
Raw Normal View History

package circuit_sys
import (
"joylink.club/ecs"
"joylink.club/ecs/filter"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/entity"
"log/slog"
)
type XcjSys struct {
query *ecs.Query
}
func NewXcjSys() *XcjSys {
return &XcjSys{query: ecs.NewQuery(filter.Contains(entity.XcjBaseComponentTypes...))}
}
func (x *XcjSys) Update(w ecs.World) {
wd := entity.GetWorldData(w)
x.query.Each(w, func(entry *ecs.Entry) {
if entry.HasComponent(component.XcjCircuitType) {
circuit := component.XcjCircuitType.Get(entry)
//处理联锁对继电器的驱动
handleDrive(wd, circuit)
//洗车流程驱动继电器
x.process(circuit)
//紧急停车
if entry.HasComponent(component.XcjFaultTag) {
component.RelayDriveType.Get(circuit.JTJ).Td = true
} else {
component.RelayDriveType.Get(circuit.JTJ).Td = false
}
}
})
}
func (x *XcjSys) process(circuit *component.XcjCircuit) {
//初始状态
if !component.BitStateType.Get(circuit.XQJ).Val && !component.BitStateType.Get(circuit.TGQJ).Val {
component.RelayDriveType.Get(circuit.XCJXJ).Td = true
component.RelayDriveType.Get(circuit.XCJXJ).Td = true
component.RelayDriveType.Get(circuit.XCYXJ).Td = false
for _, cfj := range circuit.CFJList {
component.RelayDriveType.Get(cfj).Td = false
}
component.RelayDriveType.Get(circuit.TGYXJ).Td = false
}
//洗车流程
if component.BitStateType.Get(circuit.XCJXJ).Val { //洗车就绪
if component.BitStateType.Get(circuit.XQJ).Val { //洗车请求
component.RelayDriveType.Get(circuit.XCYXJ).Td = true
component.RelayDriveType.Get(circuit.XCJXJ).Td = false
}
} else if component.BitStateType.Get(circuit.XCYXJ).Val { //洗车允许
for i, twj := range circuit.TWJList {
if component.BitStateType.Get(twj).Val {
component.RelayDriveType.Get(circuit.CFJList[i]).Td = true
}
}
if component.BitStateType.Get(circuit.TGQJ).Val {
component.RelayDriveType.Get(circuit.TGYXJ).Td = true
}
}
}
// 处理联锁对继电器的驱动
func handleDrive(wd *component.WorldData, circuit *component.XcjCircuit) {
XQJId := component.UidType.Get(circuit.XQJ).Id
XQJBit, err := wd.QueryQdBit(XQJId)
if err != nil {
slog.Error(err.Error())
} else {
component.RelayDriveType.Get(circuit.XQJ).Td = XQJBit
}
for _, twj := range circuit.TWJList {
id := component.UidType.Get(twj).Id
twjBit, err := wd.QueryQdBit(id)
if err != nil {
slog.Error(err.Error())
} else {
component.RelayDriveType.Get(twj).Td = twjBit
}
}
TGQJId := component.UidType.Get(circuit.TGQJ).Id
TGQJBit, err := wd.QueryQdBit(TGQJId)
if err != nil {
slog.Error(err.Error())
} else {
component.RelayDriveType.Get(circuit.TGQJ).Td = TGQJBit
}
}