rts-sim-module/system/signal_3xh1_system.go

158 lines
4.1 KiB
Go
Raw Normal View History

2023-09-11 18:12:34 +08:00
package system
import (
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
sysEvent "joylink.club/rtsssimulation/system/event"
)
// 电路状态信号机3XH-1(红-绿-黄) 道岔防护信号机(三显示不封灯,有单黄显示、带引导)
type Signal3XH1State struct {
// 物理黄灯true-亮
U bool
// 物理绿灯true-亮
L bool
// 物理红灯true-亮
H bool
// 点灯继电器true-吸合,常态落下表示逻辑点灯
DDJ bool
//2DJ灯丝继电器true-吸合
EDJ bool
//灯丝继电器true-吸合
DJ bool
//列车信号继电器true-吸合
LXJ bool
//引导信号继电器true-吸合
YXJ bool
//开通正线信号继电器true-吸合
ZXJ bool
}
func NewSignal3XH1State() *Signal3XH1State {
return &Signal3XH1State{
U: false,
L: false,
H: false,
DDJ: true,
EDJ: false,
DJ: false,
LXJ: false,
YXJ: false,
ZXJ: false,
}
}
// 信号机3XH-1电路状态组件
var Signal3XH1StateComponent = ecs.NewComponentType[Signal3XH1State]()
type Signal3XH1System struct {
stateQuery *ecs.Query
}
func NewSignal3XH1System() *Signal3XH1System {
return &Signal3XH1System{
stateQuery: ecs.NewQuery(filter.Contains(Signal3XH1StateComponent)),
}
}
// 处理继电器发送来的事件
func (me *Signal3XH1System) AddEventListeners(w ecs.World) {
//注册继电器状态事件处理器
sysEvent.RelayStateChangedEventBus.Subscribe(w, func(w ecs.World, event sysEvent.RelayStateChangedEvent) {
})
}
// world 执行
func (me *Signal3XH1System) Update(w ecs.World) {
me.stateQuery.Each(w, func(e *ecs.Entry) {
signal3XH1State := Signal3XH1StateComponent.Get(e)
//
_U := signal3XH1State.U
_L := signal3XH1State.L
_H := signal3XH1State.H
//
me.calculateU(w, signal3XH1State)
me.calculateL(w, signal3XH1State)
me.calculateH(w, signal3XH1State)
me.calculateDJ(w, e, signal3XH1State)
me.calculate2DJ(w, e, signal3XH1State)
//状态改变发送事件
if _U != signal3XH1State.U || _H != signal3XH1State.H || _L != signal3XH1State.L {
sysEvent.Signal3XH1AspectChangedEventBus.Publish(w, createSignal3XH1AspectChangedEvent(w, e, signal3XH1State))
}
})
}
// 黄灯点灯电路
// 开放引导信号,黄灯亮且红灯亮
// 开放列车信号且开通侧向,只黄灯亮
func (me *Signal3XH1System) calculateU(w ecs.World, state *Signal3XH1State) {
//引导信号
isY := !state.DDJ && !state.LXJ && state.DJ && state.YXJ
//侧向行车信号
isLC := !state.DDJ && state.LXJ && !state.ZXJ
state.U = isY || isLC
}
// 绿灯点灯电路
// 开放正线行车信号,只亮绿灯
func (me *Signal3XH1System) calculateL(w ecs.World, state *Signal3XH1State) {
isL := !state.DDJ && state.LXJ && state.ZXJ
state.L = isL
}
// 红灯点灯电路
// 列车信号禁止时,亮红灯
func (me *Signal3XH1System) calculateH(w ecs.World, state *Signal3XH1State) {
isH := !state.DDJ && !state.LXJ
state.H = isH
}
// DJ 灯丝继电器电路
func (me *Signal3XH1System) calculateDJ(w ecs.World, e *ecs.Entry, state *Signal3XH1State) {
_DJ := state.DJ
//
isDj := !state.DDJ
state.DJ = isDj
//状态改变发送事件
if _DJ != state.DJ {
sysEvent.RelayStateChangedEventBus.Publish(w, createSignal3XH1DJChangedEvent(w, e, state))
}
}
// 2DJ 灯丝继电器电路
func (me *Signal3XH1System) calculate2DJ(w ecs.World, e *ecs.Entry, state *Signal3XH1State) {
_2DJ := state.EDJ
//
state.EDJ = state.U
//状态改变发送事件
if _2DJ != state.EDJ {
sysEvent.RelayStateChangedEventBus.Publish(w, createSignal3XH12DJChangedEvent(w, e, state))
}
}
func createSignal3XH1DJChangedEvent(w ecs.World, e *ecs.Entry, state *Signal3XH1State) *sysEvent.RelayStateChangedEvent {
return &sysEvent.RelayStateChangedEvent{
Id: "123",
Closed: state.DJ,
}
}
func createSignal3XH12DJChangedEvent(w ecs.World, e *ecs.Entry, state *Signal3XH1State) *sysEvent.RelayStateChangedEvent {
return &sysEvent.RelayStateChangedEvent{
Id: "123",
Closed: state.EDJ,
}
}
func createSignal3XH1AspectChangedEvent(w ecs.World, e *ecs.Entry, state *Signal3XH1State) *sysEvent.Signal3XH1AspectChangedEvent {
return &sysEvent.Signal3XH1AspectChangedEvent{
Id: "123",
U: state.U,
L: state.L,
H: state.H,
}
}