From b09216f78396152dc55294a5d35b86ee51c0b96f Mon Sep 17 00:00:00 2001 From: xzb <223@qq.com> Date: Mon, 9 Oct 2023 10:30:44 +0800 Subject: [PATCH] signal jckxh --- component/signal_jckxh.go | 34 +++++++++++++++++++ entity/signal.go | 3 ++ entity/signal_jckxh.go | 37 ++++++++++++++++++++ sys/bind.go | 3 +- sys/circuit_sys/signal_jckxh.go | 60 +++++++++++++++++++++++++++++++++ 5 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 component/signal_jckxh.go create mode 100644 entity/signal_jckxh.go create mode 100644 sys/circuit_sys/signal_jckxh.go diff --git a/component/signal_jckxh.go b/component/signal_jckxh.go new file mode 100644 index 0000000..5305c64 --- /dev/null +++ b/component/signal_jckxh.go @@ -0,0 +1,34 @@ +package component + +import "joylink.club/ecs" + +// SignalJCKXHElectronic 电路状态:信号机JCKXH(红-白-黄) 进/出库列车兼调车信号机(三显示不封灯、有单黄显示、无引导) +type SignalJCKXHElectronic struct { + //灯丝继电器,true-吸合 + JCKXH_DJ *ecs.Entry + //调车信号继电器,true-吸合 + JCKXH_DXJ *ecs.Entry + //列车信号继电器,true-吸合 + JCKXH_LXJ *ecs.Entry +} + +// SignalJCKXHFilament 信号机JCKXH 灯丝状态 +type SignalJCKXHFilament struct { + // 物理黄灯,true-灯丝正常 + Uf bool + // 物理白灯,true-灯丝正常 + Bf bool + // 物理红灯,true-灯丝正常 + Hf bool + // 物理黄灯,true-亮 + U bool + // 物理白灯,true-亮 + B bool + // 物理红灯,true-亮 + H bool +} + +var ( + SignalJCKXHElectronicType = ecs.NewComponentType[SignalJCKXHElectronic]() + SignalJCKXHFilamentType = ecs.NewComponentType[SignalJCKXHFilament]() +) diff --git a/entity/signal.go b/entity/signal.go index 9a8937d..db1a13e 100644 --- a/entity/signal.go +++ b/entity/signal.go @@ -45,6 +45,9 @@ func LoadSignals(w ecs.World) error { return le } case consts.SIGNAL_JCKXH: + if le := loadSignalJckxh(w, signal, signalEntry, elecs, data.EntityMap); le != nil { + return le + } default: return fmt.Errorf("id=[%s]的信号机,无效组合类型[%s]", signal.Id(), group.Code()) } diff --git a/entity/signal_jckxh.go b/entity/signal_jckxh.go new file mode 100644 index 0000000..9450c6c --- /dev/null +++ b/entity/signal_jckxh.go @@ -0,0 +1,37 @@ +package entity + +import ( + "fmt" + "joylink.club/ecs" + "joylink.club/rtsssimulation/component" + "joylink.club/rtsssimulation/consts" + "joylink.club/rtsssimulation/repository" +) + +func loadSignalJckxh(w ecs.World, signal *repository.Signal, signalEntry *ecs.Entry, elecs []repository.IGroupedElectronicComponent, entityMap map[string]*ecs.Entry) error { + + if len(elecs) == 3 { //jckxh组合类型包含3个继电器 + signalEntry.AddComponent(component.SignalJCKXHElectronicType) + signalEntry.AddComponent(component.SignalJCKXHFilamentType) + // + elecState := &component.SignalJCKXHElectronic{} + for _, elec := range elecs { + switch elec.Code() { + case consts.SIGNAL_DJ: + elecState.JCKXH_DJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap) + case consts.SIGNAL_DXJ: + elecState.JCKXH_DXJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap) + case consts.SIGNAL_LXJ: + elecState.JCKXH_LXJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap) + default: + return fmt.Errorf("id=[%s]的信号机jckxh,无效电子元器件名称[%s]", signal.Id(), elec.Code()) + } + } + // + component.SignalJCKXHElectronicType.Set(signalEntry, elecState) + component.SignalJCKXHFilamentType.Set(signalEntry, &component.SignalJCKXHFilament{Uf: true, Bf: true, Hf: true, U: false, B: false, H: false}) + } else { + return fmt.Errorf("id=[%s]的信号机jckxh,电子元器件数量须为3", signal.Id()) + } + return nil +} diff --git a/sys/bind.go b/sys/bind.go index 15a062f..6d020be 100644 --- a/sys/bind.go +++ b/sys/bind.go @@ -22,5 +22,6 @@ func BindSystem(w ecs.World) { circuit_sys.NewSignal3XH2System(), circuit_sys.NewSignal3XH3System(), circuit_sys.NewSignal3XH4System(), - circuit_sys.NewSignalDCXHSystem()) + circuit_sys.NewSignalDCXHSystem(), + circuit_sys.NewSignalJCKXHSystem()) } diff --git a/sys/circuit_sys/signal_jckxh.go b/sys/circuit_sys/signal_jckxh.go new file mode 100644 index 0000000..a9f1e94 --- /dev/null +++ b/sys/circuit_sys/signal_jckxh.go @@ -0,0 +1,60 @@ +package circuit_sys + +import ( + "github.com/yohamta/donburi/filter" + "joylink.club/ecs" + "joylink.club/rtsssimulation/component" +) + +type SignalJCKXHSystem struct { + query *ecs.Query +} + +func NewSignalJCKXHSystem() *SignalJCKXHSystem { + return &SignalJCKXHSystem{query: ecs.NewQuery(filter.Contains(component.SignalJCKXHElectronicType, component.SignalJCKXHFilamentType))} +} + +// Update world 执行 +func (s *SignalJCKXHSystem) Update(w ecs.World) { + s.query.Each(w, func(e *ecs.Entry) { + state := component.SignalJCKXHElectronicType.Get(e) + filament := component.SignalJCKXHFilamentType.Get(e) + // + s.calculateU(state, filament) + s.calculateB(state, filament) + s.calculateH(state, filament) + s.calculateDJ(state, filament) + }) +} + +func (s *SignalJCKXHSystem) calculateU(state *component.SignalJCKXHElectronic, filament *component.SignalJCKXHFilament) { + lxj := component.BitStateType.Get(state.JCKXH_LXJ) + isU := filament.Uf && lxj.Val + filament.U = isU +} + +func (s *SignalJCKXHSystem) calculateB(state *component.SignalJCKXHElectronic, filament *component.SignalJCKXHFilament) { + lxj := component.BitStateType.Get(state.JCKXH_LXJ) + dxj := component.BitStateType.Get(state.JCKXH_DXJ) + isB := filament.Bf && !lxj.Val && dxj.Val + filament.B = isB +} + +func (s *SignalJCKXHSystem) calculateH(state *component.SignalJCKXHElectronic, filament *component.SignalJCKXHFilament) { + lxj := component.BitStateType.Get(state.JCKXH_LXJ) + dxj := component.BitStateType.Get(state.JCKXH_DXJ) + isH := filament.Bf && !lxj.Val && dxj.Val + filament.H = isH +} + +func (s *SignalJCKXHSystem) calculateDJ(state *component.SignalJCKXHElectronic, filament *component.SignalJCKXHFilament) { + lxj := component.BitStateType.Get(state.JCKXH_LXJ) + dxj := component.BitStateType.Get(state.JCKXH_DXJ) + dj := component.BitStateType.Get(state.JCKXH_DJ) + isDJ := filament.Uf && lxj.Val || filament.Bf && !lxj.Val && dxj.Val || filament.Bf && !lxj.Val && dxj.Val + if isDJ != dj.Val { + drive := component.RelayDriveType.Get(state.JCKXH_DJ) + drive.Td = isDJ + drive.Xq = isDJ + } +}