rts-sim-module/system/signal_2xh1_system.go

117 lines
3.8 KiB
Go
Raw Normal View History

2023-09-18 13:16:31 +08:00
package system
import (
2023-09-18 13:26:01 +08:00
"fmt"
2023-09-18 13:16:31 +08:00
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
sysEvent "joylink.club/rtsssimulation/system/event"
"joylink.club/rtsssimulation/umi"
)
// Signal2XH1State 电路状态:**信号机2XH-1(红-绿) 出段(场)信号机 或 **出站区间阻挡信号机
type Signal2XH1State struct {
// 物理绿灯true-亮
L bool
// 物理红灯true-亮
H bool
// 点灯继电器true-吸合,常态落下表示逻辑点灯
DDJ bool
//灯丝继电器true-吸合
DJ bool
//列车信号继电器true-吸合
LXJ bool
}
// Signal2XH1FilamentState 信号机2XH-1 灯丝状态
type Signal2XH1FilamentState struct {
// 物理绿灯true-灯丝正常
Lf bool
// 物理红灯true-灯丝正常
Hf bool
}
func NewSignal2XH1FilamentState() *Signal2XH1FilamentState {
return &Signal2XH1FilamentState{Lf: true, Hf: true}
}
// NewSignal2XH1CdcState 出段(场)信号机
func NewSignal2XH1CdcState() *Signal2XH1State {
return &Signal2XH1State{DDJ: false, LXJ: false, DJ: true, L: false, H: true}
}
// NewSignal2XH1CzqzState 出站区间阻挡信号机
func NewSignal2XH1CzqzState() *Signal2XH1State {
return &Signal2XH1State{DDJ: true, LXJ: false, DJ: false, L: false, H: false}
}
var Signal2XH1StateComponent = ecs.NewComponentType[Signal2XH1State]()
var Signal2XH1FilamentStateComponent = ecs.NewComponentType[Signal2XH1FilamentState]()
var signal2XH1Query = ecs.NewQuery(filter.Contains(EntityIdentityComponent, Signal2XH1StateComponent))
2023-09-18 15:41:15 +08:00
type Signal2XH1System struct {
}
2023-09-18 17:34:06 +08:00
func NewSignal2XH1System() *Signal2XH1System {
return &Signal2XH1System{}
}
2023-09-18 13:16:31 +08:00
// Update world 执行
2023-09-18 15:41:15 +08:00
func (me *Signal2XH1System) Update(w ecs.World) {
2023-09-18 13:16:31 +08:00
signal2XH1Query.Each(w, func(entry *ecs.Entry) {
state := Signal2XH1StateComponent.Get(entry)
filament := Signal2XH1FilamentStateComponent.Get(entry)
me.calculateL(state, filament)
me.calculateH(state, filament)
me.calculateDJ(w, entry, state, filament)
})
}
2023-09-18 13:26:01 +08:00
// Signal2XH1RelayActionEventProcessor 继电器动作事件处理
// 将继电器动作的结果同步到系统
var Signal2XH1RelayActionEventProcessor = func(w ecs.World, event sysEvent.RelayActionEvent) {
2023-09-18 15:41:15 +08:00
//根据event来更新Signal2XH1State中对应继电器的状态
2023-09-18 13:26:01 +08:00
signal2XH1Query.Each(w, func(e *ecs.Entry) {
signalModel := FindModelStorage(w).FindById(EntityIdentityComponent.Get(e).Id)
roler, ok := signalModel.(umi.IRelayCRole)
if ok {
if relayGroup, relayName, find := roler.FindCircuitRoleById(event.Id); find {
if relayGroup == SIGNAL_2XH1 {
state := Signal2XH1StateComponent.Get(e)
switch relayName {
case SIGNAL_DDJ:
state.DDJ = event.Xh
case SIGNAL_DJ:
state.DJ = event.Xh
case SIGNAL_LXJ:
state.LXJ = event.Xh
default:
panic(fmt.Sprintf("Signal2XH1的模型[%s]中继电器功能名称[%s]无法识别", signalModel.GetId(), relayName))
}
} else {
panic(fmt.Sprintf("Signal2XH1的模型[%s]中继电器组合类型[%s]无法识别", signalModel.GetId(), relayGroup))
}
}
} else {
panic("Signal2XH1的模型未实现接口umi.IRelayCRoler")
}
})
}
2023-09-18 15:41:15 +08:00
func (me *Signal2XH1System) calculateL(state *Signal2XH1State, filament *Signal2XH1FilamentState) {
2023-09-18 13:16:31 +08:00
isL := !state.DDJ && state.LXJ && filament.Lf
state.L = isL
}
2023-09-18 15:41:15 +08:00
func (me *Signal2XH1System) calculateH(state *Signal2XH1State, filament *Signal2XH1FilamentState) {
2023-09-18 13:16:31 +08:00
isH := !state.DDJ && !state.LXJ && filament.Hf
state.H = isH
}
2023-09-18 15:41:15 +08:00
func (me *Signal2XH1System) calculateDJ(w ecs.World, e *ecs.Entry, state *Signal2XH1State, filament *Signal2XH1FilamentState) {
2023-09-18 13:16:31 +08:00
isDJ := !state.DDJ && state.LXJ && filament.Lf || !state.DDJ && !state.LXJ && filament.Hf
//通知继电器进行动作
if state.DJ != isDJ {
2023-09-18 16:11:16 +08:00
if event, ok := createRelayNeedChangeEvent(w, EntityIdentityComponent.Get(e).Id, SIGNAL_2XH1, SIGNAL_DJ, isDJ); ok {
sysEvent.RelayNeedChangeEventBus.Publish(w, event)
}
2023-09-18 13:16:31 +08:00
}
}