126 lines
4.0 KiB
Go
126 lines
4.0 KiB
Go
package system
|
||
|
||
import (
|
||
"fmt"
|
||
"github.com/yohamta/donburi/filter"
|
||
"joylink.club/ecs"
|
||
sysEvent "joylink.club/rtsssimulation/system/event"
|
||
)
|
||
|
||
// Signal3XH4State 电路状态:信号机3XH-4(红-绿-黄) 出站兼道岔防护信号机(三显示不封灯、有单黄显示、无引导)
|
||
type Signal3XH4State struct {
|
||
// 物理黄灯,true-亮
|
||
U bool
|
||
// 物理绿灯,true-亮
|
||
L bool
|
||
// 物理红灯,true-亮
|
||
H bool
|
||
// 点灯继电器,true-吸合,常态落下表示逻辑点灯
|
||
DDJ bool
|
||
//灯丝继电器,true-吸合
|
||
DJ bool
|
||
//列车信号继电器,true-吸合
|
||
LXJ bool
|
||
//开通正线信号继电器,true-吸合
|
||
ZXJ bool
|
||
}
|
||
|
||
// Signal3XH4FilamentState 信号机3XH-2 灯丝状态
|
||
type Signal3XH4FilamentState struct {
|
||
// 物理黄灯,true-灯丝正常
|
||
Uf bool
|
||
// 物理绿灯,true-灯丝正常
|
||
Lf bool
|
||
// 物理红灯,true-灯丝正常
|
||
Hf bool
|
||
}
|
||
|
||
func NewSignal3XH4State() *Signal3XH4State {
|
||
return &Signal3XH4State{DDJ: true, DJ: false, LXJ: false, ZXJ: false, U: false, L: false, H: false}
|
||
}
|
||
func NewSignal3XH4FilamentState() *Signal3XH4FilamentState {
|
||
return &Signal3XH4FilamentState{Uf: false, Lf: false, Hf: false}
|
||
}
|
||
|
||
var Signal3XH4StateComponent = ecs.NewComponentType[Signal3XH4State]()
|
||
var Signal3XH4FilamentStateComponent = ecs.NewComponentType[Signal3XH4FilamentState]()
|
||
var signal3XH4Query = ecs.NewQuery(filter.Contains(EntityIdentityComponent, Signal3XH4StateComponent))
|
||
|
||
type Signal3XH4System struct {
|
||
}
|
||
|
||
func NewSignal3XH4System() *Signal3XH4System {
|
||
return &Signal3XH4System{}
|
||
}
|
||
|
||
// Update world 执行
|
||
func (me *Signal3XH4System) Update(w ecs.World) {
|
||
signal3XH4Query.Each(w, func(e *ecs.Entry) {
|
||
state := Signal3XH4StateComponent.Get(e)
|
||
filament := Signal3XH4FilamentStateComponent.Get(e)
|
||
//
|
||
me.calculateU(w, state, filament)
|
||
me.calculateL(w, state, filament)
|
||
me.calculateH(w, state, filament)
|
||
me.calculateDJ(w, e, state, filament)
|
||
})
|
||
}
|
||
|
||
// Signal3XH4RelayActionEventProcessor 继电器动作事件处理
|
||
var Signal3XH4RelayActionEventProcessor = func(w ecs.World, event sysEvent.RelayActionEvent) {
|
||
//根据event来更新Signal3XH4State中对应继电器的状态
|
||
signal3XH4Query.Each(w, func(e *ecs.Entry) {
|
||
signalModel := FindModelStorage(w).FindById(EntityIdentityComponent.Get(e).Id)
|
||
roler, ok := signalModel.(IRelayCRole)
|
||
if ok {
|
||
if relayGroup, relayName, find := roler.FindCircuitRoleById(event.Id); find {
|
||
if relayGroup == SIGNAL_3XH4 {
|
||
state := Signal3XH4StateComponent.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
|
||
case SIGNAL_ZXJ:
|
||
state.ZXJ = event.Xh
|
||
default:
|
||
panic(fmt.Sprintf("Signal3XH4的模型[%s]中继电器功能名称[%s]无法识别", signalModel.Id(), relayName))
|
||
}
|
||
} else {
|
||
panic(fmt.Sprintf("Signal3XH4的模型[%s]中继电器组合类型[%s]无法识别", signalModel.Id(), relayGroup))
|
||
}
|
||
}
|
||
} else {
|
||
panic("Signal3XH4的模型未实现接口umi.IRelayCRoler")
|
||
}
|
||
})
|
||
}
|
||
|
||
func (me *Signal3XH4System) calculateU(w ecs.World, state *Signal3XH4State, filament *Signal3XH4FilamentState) {
|
||
isU := filament.Uf && !state.DDJ && state.LXJ && !state.ZXJ
|
||
state.U = isU
|
||
}
|
||
|
||
func (me *Signal3XH4System) calculateL(w ecs.World, state *Signal3XH4State, filament *Signal3XH4FilamentState) {
|
||
isL := filament.Lf && !state.DDJ && state.LXJ && state.ZXJ
|
||
state.L = isL
|
||
}
|
||
|
||
func (me *Signal3XH4System) calculateH(w ecs.World, state *Signal3XH4State, filament *Signal3XH4FilamentState) {
|
||
isH := filament.Hf && !state.DDJ && !state.LXJ
|
||
state.H = isH
|
||
}
|
||
|
||
func (me *Signal3XH4System) calculateDJ(w ecs.World, e *ecs.Entry, state *Signal3XH4State, filament *Signal3XH4FilamentState) {
|
||
isDJ := filament.Uf && !state.DDJ && state.LXJ && !state.ZXJ ||
|
||
filament.Lf && !state.DDJ && state.LXJ && state.ZXJ ||
|
||
filament.Hf && !state.DDJ && !state.LXJ
|
||
if isDJ != state.DJ {
|
||
if event, ok := createRelayNeedChangeEvent(w, EntityIdentityComponent.Get(e).Id, SIGNAL_3XH3, SIGNAL_DJ, isDJ); ok {
|
||
sysEvent.RelayNeedChangeEventBus.Publish(w, event)
|
||
}
|
||
}
|
||
}
|