2023-09-11 18:12:34 +08:00
|
|
|
|
package system
|
|
|
|
|
|
|
|
|
|
import (
|
2023-09-13 11:12:39 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
|
2023-09-11 18:12:34 +08:00
|
|
|
|
"github.com/yohamta/donburi/filter"
|
|
|
|
|
"joylink.club/ecs"
|
|
|
|
|
sysEvent "joylink.club/rtsssimulation/system/event"
|
2023-09-13 11:12:39 +08:00
|
|
|
|
"joylink.club/rtsssimulation/umi"
|
2023-09-11 18:12:34 +08:00
|
|
|
|
)
|
|
|
|
|
|
2023-09-13 15:52:39 +08:00
|
|
|
|
// 信号机电路继电器组合类型和功能名称常量
|
|
|
|
|
// 其他信号机的一并定义于此
|
|
|
|
|
const (
|
|
|
|
|
//继电器组合类型
|
|
|
|
|
SIGNAL_3XH1 = "3XH-1"
|
|
|
|
|
SIGNAL_3XH2 = "3XH-2"
|
|
|
|
|
SIGNAL_3XH3 = "3XH-3"
|
|
|
|
|
SIGNAL_3XH4 = "3XH-4"
|
|
|
|
|
SIGNAL_2XH1 = "2XH-1"
|
|
|
|
|
//继电器功能名称
|
|
|
|
|
SIGNAL_DDJ = "DDJ"
|
|
|
|
|
SIGNAL_DJ = "DJ"
|
|
|
|
|
SIGNAL_2DJ = "2DJ"
|
|
|
|
|
SIGNAL_LXJ = "LXJ"
|
|
|
|
|
SIGNAL_YXJ = "YXJ"
|
|
|
|
|
SIGNAL_ZXJ = "ZXJ"
|
|
|
|
|
)
|
|
|
|
|
|
2023-09-15 13:48:48 +08:00
|
|
|
|
// Signal3XH1State 电路状态:信号机3XH-1(红-绿-黄) 道岔防护信号机(三显示不封灯,有单黄显示、带引导)
|
2023-09-11 18:12:34 +08:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-18 10:37:17 +08:00
|
|
|
|
// Signal3XH1FilamentState 信号机3XH-1 灯丝状态
|
|
|
|
|
type Signal3XH1FilamentState struct {
|
|
|
|
|
// 物理黄灯,true-灯丝正常
|
|
|
|
|
Uf bool
|
|
|
|
|
// 物理绿灯,true-灯丝正常
|
|
|
|
|
Lf bool
|
|
|
|
|
// 物理红灯,true-灯丝正常
|
|
|
|
|
Hf bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewSignal3XH1FilamentState() *Signal3XH1FilamentState {
|
|
|
|
|
return &Signal3XH1FilamentState{Uf: true, Lf: true, Hf: true}
|
|
|
|
|
}
|
2023-09-11 18:12:34 +08:00
|
|
|
|
func NewSignal3XH1State() *Signal3XH1State {
|
|
|
|
|
return &Signal3XH1State{
|
|
|
|
|
U: false,
|
|
|
|
|
L: false,
|
|
|
|
|
H: false,
|
|
|
|
|
DDJ: true,
|
|
|
|
|
EDJ: false,
|
|
|
|
|
DJ: false,
|
|
|
|
|
LXJ: false,
|
|
|
|
|
YXJ: false,
|
|
|
|
|
ZXJ: false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-15 13:48:48 +08:00
|
|
|
|
// Signal3XH1StateComponent 信号机3XH-1电路状态组件
|
2023-09-11 18:12:34 +08:00
|
|
|
|
var Signal3XH1StateComponent = ecs.NewComponentType[Signal3XH1State]()
|
2023-09-18 10:37:17 +08:00
|
|
|
|
var Signal3XH1FilamentStateComponent = ecs.NewComponentType[Signal3XH1FilamentState]()
|
2023-09-13 11:12:39 +08:00
|
|
|
|
var signal3XH1Query = ecs.NewQuery(filter.Contains(EntityIdentityComponent, Signal3XH1StateComponent))
|
2023-09-11 18:12:34 +08:00
|
|
|
|
|
|
|
|
|
type Signal3XH1System struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewSignal3XH1System() *Signal3XH1System {
|
2023-09-13 11:12:39 +08:00
|
|
|
|
return &Signal3XH1System{}
|
2023-09-11 18:12:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-15 13:48:48 +08:00
|
|
|
|
// Signal3XH1RelayActionEventProcessor 继电器动作事件处理
|
2023-09-12 16:39:30 +08:00
|
|
|
|
// 将继电器动作的结果同步到系统
|
|
|
|
|
var Signal3XH1RelayActionEventProcessor = func(w ecs.World, event sysEvent.RelayActionEvent) {
|
|
|
|
|
//根据event来更新Signal3XH1State中对应继电器的状态
|
2023-09-13 11:12:39 +08:00
|
|
|
|
signal3XH1Query.Each(w, func(e *ecs.Entry) {
|
|
|
|
|
signalModel := FindModelStorage(w).FindById(EntityIdentityComponent.Get(e).Id)
|
2023-09-13 11:28:36 +08:00
|
|
|
|
roler, ok := signalModel.(umi.IRelayCRole)
|
2023-09-13 11:12:39 +08:00
|
|
|
|
if ok {
|
2023-09-13 15:52:39 +08:00
|
|
|
|
if relayGroup, relayName, find := roler.FindCircuitRoleById(event.Id); find {
|
|
|
|
|
if relayGroup == SIGNAL_3XH1 {
|
|
|
|
|
state := Signal3XH1StateComponent.Get(e)
|
|
|
|
|
switch relayName {
|
|
|
|
|
case SIGNAL_DDJ:
|
|
|
|
|
state.DDJ = event.Xh
|
|
|
|
|
case SIGNAL_DJ:
|
|
|
|
|
state.DJ = event.Xh
|
|
|
|
|
case SIGNAL_2DJ:
|
|
|
|
|
state.EDJ = event.Xh
|
|
|
|
|
case SIGNAL_LXJ:
|
|
|
|
|
state.LXJ = event.Xh
|
|
|
|
|
case SIGNAL_YXJ:
|
|
|
|
|
state.YXJ = event.Xh
|
|
|
|
|
case SIGNAL_ZXJ:
|
|
|
|
|
state.ZXJ = event.Xh
|
|
|
|
|
default:
|
|
|
|
|
panic(fmt.Sprintf("Signal3XH1的模型[%s]中继电器功能名称[%s]无法识别", signalModel.GetId(), relayName))
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
panic(fmt.Sprintf("Signal3XH1的模型[%s]中继电器组合类型[%s]无法识别", signalModel.GetId(), relayGroup))
|
2023-09-13 11:12:39 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
panic("Signal3XH1的模型未实现接口umi.IRelayCRoler")
|
|
|
|
|
}
|
|
|
|
|
})
|
2023-09-11 18:12:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-15 13:48:48 +08:00
|
|
|
|
// Update world 执行
|
2023-09-11 18:12:34 +08:00
|
|
|
|
func (me *Signal3XH1System) Update(w ecs.World) {
|
2023-09-13 11:12:39 +08:00
|
|
|
|
signal3XH1Query.Each(w, func(e *ecs.Entry) {
|
2023-09-11 18:12:34 +08:00
|
|
|
|
signal3XH1State := Signal3XH1StateComponent.Get(e)
|
2023-09-18 10:37:17 +08:00
|
|
|
|
filament := Signal3XH1FilamentStateComponent.Get(e)
|
2023-09-11 18:12:34 +08:00
|
|
|
|
//
|
2023-09-18 10:37:17 +08:00
|
|
|
|
me.calculateU(w, signal3XH1State, filament)
|
|
|
|
|
me.calculateL(w, signal3XH1State, filament)
|
|
|
|
|
me.calculateH(w, signal3XH1State, filament)
|
|
|
|
|
me.calculateDJ(w, e, signal3XH1State, filament)
|
|
|
|
|
me.calculate2DJ(w, e, signal3XH1State, filament)
|
2023-09-11 18:12:34 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 黄灯点灯电路
|
|
|
|
|
// 开放引导信号,黄灯亮且红灯亮
|
|
|
|
|
// 开放列车信号且开通侧向,只黄灯亮
|
2023-09-18 10:37:17 +08:00
|
|
|
|
func (me *Signal3XH1System) calculateU(w ecs.World, state *Signal3XH1State, filament *Signal3XH1FilamentState) {
|
2023-09-11 18:12:34 +08:00
|
|
|
|
//引导信号
|
|
|
|
|
isY := !state.DDJ && !state.LXJ && state.DJ && state.YXJ
|
|
|
|
|
//侧向行车信号
|
|
|
|
|
isLC := !state.DDJ && state.LXJ && !state.ZXJ
|
2023-09-18 10:37:17 +08:00
|
|
|
|
state.U = filament.Uf && (isY || isLC)
|
2023-09-11 18:12:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 绿灯点灯电路
|
|
|
|
|
// 开放正线行车信号,只亮绿灯
|
2023-09-18 10:37:17 +08:00
|
|
|
|
func (me *Signal3XH1System) calculateL(w ecs.World, state *Signal3XH1State, filament *Signal3XH1FilamentState) {
|
2023-09-11 18:12:34 +08:00
|
|
|
|
isL := !state.DDJ && state.LXJ && state.ZXJ
|
2023-09-18 10:37:17 +08:00
|
|
|
|
state.L = filament.Lf && isL
|
2023-09-11 18:12:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 红灯点灯电路
|
|
|
|
|
// 列车信号禁止时,亮红灯
|
2023-09-18 10:37:17 +08:00
|
|
|
|
func (me *Signal3XH1System) calculateH(w ecs.World, state *Signal3XH1State, filament *Signal3XH1FilamentState) {
|
2023-09-11 18:12:34 +08:00
|
|
|
|
isH := !state.DDJ && !state.LXJ
|
2023-09-18 10:37:17 +08:00
|
|
|
|
state.H = filament.Hf && isH
|
2023-09-11 18:12:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DJ 灯丝继电器电路
|
2023-09-18 10:37:17 +08:00
|
|
|
|
func (me *Signal3XH1System) calculateDJ(w ecs.World, e *ecs.Entry, state *Signal3XH1State, filament *Signal3XH1FilamentState) {
|
2023-09-11 18:12:34 +08:00
|
|
|
|
_DJ := state.DJ
|
2023-09-18 10:37:17 +08:00
|
|
|
|
isDj := filament.Lf && !state.DDJ && state.LXJ && state.ZXJ ||
|
|
|
|
|
filament.Uf && !state.DDJ && state.LXJ && !state.ZXJ ||
|
|
|
|
|
filament.Hf && !state.DDJ && !state.LXJ
|
2023-09-12 16:39:30 +08:00
|
|
|
|
//通知继电器进行动作
|
|
|
|
|
if _DJ != isDj {
|
2023-09-18 16:11:16 +08:00
|
|
|
|
if event, ok := createRelayNeedChangeEvent(w, EntityIdentityComponent.Get(e).Id, SIGNAL_3XH1, SIGNAL_DJ, isDj); ok {
|
|
|
|
|
sysEvent.RelayNeedChangeEventBus.Publish(w, event)
|
|
|
|
|
}
|
2023-09-11 18:12:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2DJ 灯丝继电器电路
|
2023-09-18 10:37:17 +08:00
|
|
|
|
func (me *Signal3XH1System) calculate2DJ(w ecs.World, e *ecs.Entry, state *Signal3XH1State, filament *Signal3XH1FilamentState) {
|
2023-09-11 18:12:34 +08:00
|
|
|
|
_2DJ := state.EDJ
|
|
|
|
|
//
|
2023-09-18 10:37:17 +08:00
|
|
|
|
is2DJ := filament.Uf && !state.DDJ && !state.LXJ && state.DJ && state.YXJ
|
2023-09-12 16:39:30 +08:00
|
|
|
|
//通知继电器进行动作
|
|
|
|
|
if _2DJ != is2DJ {
|
2023-09-18 16:11:16 +08:00
|
|
|
|
if event, ok := createRelayNeedChangeEvent(w, EntityIdentityComponent.Get(e).Id, SIGNAL_3XH1, SIGNAL_2DJ, is2DJ); ok {
|
|
|
|
|
sysEvent.RelayNeedChangeEventBus.Publish(w, event)
|
|
|
|
|
}
|
2023-09-11 18:12:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|