This commit is contained in:
walker 2023-09-27 18:39:26 +08:00
commit f71c3092d8
13 changed files with 455 additions and 45 deletions

23
entities/button_entity.go Normal file
View File

@ -0,0 +1,23 @@
package entities
import (
"joylink.club/ecs"
"joylink.club/rtsssimulation/system"
)
// CreateSelfResetButtonEntity 创建自复位按钮实体
func CreateSelfResetButtonEntity(w ecs.World, buttonId string) *ecs.Entry {
e := w.Create(system.EntityIdentityComponent, system.ButtonStateComponent, system.ButtonSelfRestTag)
system.EntityIdentityComponent.Set(e, &system.EntityIdentity{Id: buttonId})
system.ButtonStateComponent.Set(e, system.NewButtonState())
system.ButtonSelfRestTag.Set(e, &system.ButtonSelfRestState{KeepAxTime: 0})
return e
}
// CreateNonResetButtonEntity 创建非自复位按钮实体
func CreateNonResetButtonEntity(w ecs.World, buttonId string) *ecs.Entry {
e := w.Create(system.EntityIdentityComponent, system.ButtonStateComponent, system.ButtonNonRestTag)
system.EntityIdentityComponent.Set(e, &system.EntityIdentity{Id: buttonId})
system.ButtonStateComponent.Set(e, system.NewButtonState())
return e
}

14
entities/dcb_entity.go Normal file
View File

@ -0,0 +1,14 @@
package entities
import (
"joylink.club/ecs"
"joylink.club/rtsssimulation/system"
)
// CreateDcbCircuitEntity 创建门控箱电路实体
func CreateDcbCircuitEntity(w ecs.World, dcbCircuitId string) *ecs.Entry {
e := w.Create(system.EntityIdentityComponent, system.DcbCircuitStateComponent)
system.EntityIdentityComponent.Set(e, &system.EntityIdentity{Id: dcbCircuitId})
system.DcbCircuitStateComponent.Set(e, system.NewDcbCircuitState())
return e
}

13
entities/emps_entity.go Normal file
View File

@ -0,0 +1,13 @@
package entities
import (
"joylink.club/ecs"
"joylink.club/rtsssimulation/system"
)
func CreateEmpsCircuitEntity(w ecs.World, empsCircuitId string) *ecs.Entry {
e := w.Create(system.EntityIdentityComponent, system.EmpsCircuitStateComponent)
system.EntityIdentityComponent.Set(e, &system.EntityIdentity{Id: empsCircuitId})
system.EmpsCircuitStateComponent.Set(e, system.NewEmpsCircuitState())
return e
}

View File

@ -3,9 +3,15 @@ package entities
import (
"joylink.club/ecs"
"joylink.club/rtsssimulation/repository"
"joylink.club/rtsssimulation/simulation"
"joylink.club/rtsssimulation/system"
)
type RelayState struct {
Id string
Xh bool
}
// CreateRelayEntity 创建继电器实体
func CreateRelayEntity(w ecs.World, relayId string) *ecs.Entry {
e := w.Create(system.EntityIdentityComponent, system.RelayStateComponent)
@ -19,3 +25,13 @@ func CreateRelayEntries(world ecs.World, relays []*repository.Relay) {
CreateRelayEntity(world, relay.Id())
}
}
func GetRelayState(worldId ecs.WorldId, relayId string) *RelayState {
sim := simulation.FindSimulation(worldId)
entry := sim.GetEntry(relayId)
if entry == nil {
return nil
}
state := system.RelayStateComponent.Get(entry)
return &RelayState{Id: relayId, Xh: state.Xh}
}

14
entities/spks_entity.go Normal file
View File

@ -0,0 +1,14 @@
package entities
import (
"joylink.club/ecs"
"joylink.club/rtsssimulation/system"
)
// CreateSpksCircuitEntity 创建人员防护按钮电路实体
func CreateSpksCircuitEntity(w ecs.World, spksCircuitId string) *ecs.Entry {
e := w.Create(system.EntityIdentityComponent, system.SpksCircuitStateComponent)
system.EntityIdentityComponent.Set(e, &system.EntityIdentity{Id: spksCircuitId})
system.SpksCircuitStateComponent.Set(e, system.NewSpksCircuitState())
return e
}

View File

@ -3,8 +3,6 @@ package entities
import (
"time"
"github.com/yohamta/donburi/component"
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
"joylink.club/rtsssimulation/repository"
"joylink.club/rtsssimulation/simulation"
@ -13,6 +11,13 @@ import (
type Position int
type TurnoutState struct {
Id string
Normal bool
Reverse bool
Turning bool
}
const (
SK Position = iota //四开(不会起名字)
D //定位
@ -44,13 +49,7 @@ func CreateTurnoutEntries(world ecs.World, turnouts []*repository.Turnout) []*ec
if len(turnout.RelayGroups()) == 0 {
continue
}
var components []component.IComponentType
components = append(components, system.EntityIdentityComponent)
components = append(components, system.Switch2jZdj9StateComponent)
entry := world.Create(components...)
entries = append(entries, entry)
system.EntityIdentityComponent.Set(entry, &system.EntityIdentity{Id: turnout.Id()})
system.Switch2jZdj9StateComponent.Set(entry, system.NewSwitch2jZdj9State())
entries = append(entries, CreateSwitch2jzdj9Entity(world, turnout.Id()))
}
return entries
}
@ -58,41 +57,31 @@ func CreateTurnoutEntries(world ecs.World, turnouts []*repository.Turnout) []*ec
func TurnToNormal(worldId ecs.WorldId, turnoutId string) {
sim := simulation.FindSimulation(worldId)
system.Switch2jZdj9DriveYc(sim.World(), turnoutId, true)
system.Switch2jZdj9DriveFc(sim.World(), turnoutId, true)
system.Switch2jZdj9DriveDc(sim.World(), turnoutId, true)
go func() {
<-time.After(1 * time.Second)
<-time.After(5 * time.Second)
system.Switch2jZdj9DriveYc(sim.World(), turnoutId, false)
system.Switch2jZdj9DriveFc(sim.World(), turnoutId, false)
system.Switch2jZdj9DriveDc(sim.World(), turnoutId, false)
}()
}
func TurnToReverse(worldId ecs.WorldId, turnoutId string) {
sim := simulation.FindSimulation(worldId)
system.Switch2jZdj9DriveYc(sim.World(), turnoutId, true)
system.Switch2jZdj9DriveDc(sim.World(), turnoutId, true)
system.Switch2jZdj9DriveFc(sim.World(), turnoutId, true)
go func() {
<-time.After(1 * time.Second)
<-time.After(5 * time.Second)
system.Switch2jZdj9DriveYc(sim.World(), turnoutId, false)
system.Switch2jZdj9DriveDc(sim.World(), turnoutId, false)
system.Switch2jZdj9DriveFc(sim.World(), turnoutId, false)
}()
}
func GetState(worldId ecs.WorldId, turnoutId string) Position {
func GetTurnoutState(worldId ecs.WorldId, turnoutId string) *TurnoutState {
sim := simulation.FindSimulation(worldId)
query := ecs.NewQuery(filter.Contains(system.EntityIdentityComponent, system.Switch2jZdj9StateComponent))
var position Position
// 查找道岔位置
query.Each(sim.World(), func(e *ecs.Entry) {
if turnoutId == system.EntityIdentityComponent.Get(e).Id {
state := system.Switch2jZdj9StateComponent.Get(e)
if state.J1_DB_K9 {
position = D
} else if state.J1_FB_K10 {
position = F
} else {
position = SK
}
}
})
return position
entry := sim.GetEntry(turnoutId)
if entry == nil {
return nil
}
state := system.Switch2jZdj9StateComponent.Get(entry)
return &TurnoutState{Id: turnoutId, Normal: state.IsNormal(), Reverse: state.IsReverse(), Turning: state.IsTurning()}
}

View File

@ -3,11 +3,12 @@ package repository
import (
"errors"
"fmt"
"math"
"strconv"
"go.uber.org/zap"
"joylink.club/rtsssimulation/repository/model/proto"
"joylink.club/rtsssimulation/util/number"
"math"
"strconv"
)
var repositoryMap = make(map[string]*Repository)
@ -421,7 +422,7 @@ func buildLinksAndRelate(repo *Repository) error {
func buildLinks(repo *Repository) error {
visitedTurnoutPortMap := make(map[string]bool)
allTurnouts := repo.TurnoutList()
linkIdGenerator := 10000
linkIdGenerator := 1
for {
//找一个未遍历过的道岔端口
startTp := getATurnoutPort(allTurnouts, visitedTurnoutPortMap)

View File

@ -1,6 +1,8 @@
package simulation
import (
"sync"
"joylink.club/ecs"
"joylink.club/rtsssimulation/repository"
)
@ -10,8 +12,9 @@ var (
)
type Simulation struct {
world ecs.World
repo *repository.Repository
world ecs.World
repo *repository.Repository
entityMap sync.Map
}
func (s *Simulation) World() ecs.World {
@ -22,12 +25,25 @@ func (s *Simulation) Repo() *repository.Repository {
return s.repo
}
func CreateSimulation(repo *repository.Repository, world ecs.World) {
func (s *Simulation) AddEntry(id string, entity *ecs.Entry) {
s.entityMap.Store(id, entity)
}
func (s *Simulation) GetEntry(id string) *ecs.Entry {
e, ok := s.entityMap.Load(id)
if ok {
return e.(*ecs.Entry)
}
return nil
}
func CreateSimulation(repo *repository.Repository, world ecs.World) *Simulation {
sim := &Simulation{
world: world,
repo: repo,
}
simulationManager[world.Id()] = sim
return sim
}
func DestroySimulation(id ecs.WorldId) {

View File

@ -1,27 +1,27 @@
package world
import (
"time"
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
"joylink.club/rtsssimulation/entities"
"joylink.club/rtsssimulation/repository"
"joylink.club/rtsssimulation/simulation"
"joylink.club/rtsssimulation/system"
"time"
)
func CreateSimulation(repo *repository.Repository) ecs.WorldId {
var systems []ecs.ISystem
wc := &WorldConfig{
Systems: systems,
Systems: []ecs.ISystem{system.NewSwitch2jZdj9System(), system.NewRelaySystem()},
Tick: 200,
InitTime: time.Now(),
}
w := InitializeWorld(wc)
simulation.CreateSimulation(repo, w)
sim := simulation.CreateSimulation(repo, w)
//添加实体
entities.CreateTurnoutEntries(w, repo.TurnoutList())
entities.CreateRelayEntries(w, repo.RelayList())
entities.CreateSystemTimerEntity(w, wc.InitTime)
initDeviceEntries(w, sim, repo)
//初始化组件
InitComponent(w, repo)
//添加监听器
@ -32,6 +32,20 @@ func CreateSimulation(repo *repository.Repository) ecs.WorldId {
return w.Id()
}
func initDeviceEntries(w ecs.World, sim *simulation.Simulation, repo *repository.Repository) {
// 初始化道岔
for _, turnout := range repo.TurnoutList() {
if len(turnout.RelayGroups()) == 0 {
continue
}
sim.AddEntry(turnout.Id(), entities.CreateSwitch2jzdj9Entity(w, turnout.Id()))
}
// 初始化继电器
for _, relay := range repo.RelayList() {
sim.AddEntry(relay.Id(), entities.CreateRelayEntity(w, relay.Id()))
}
}
// InitializeWorld 初始化仿真world
func InitializeWorld(config *WorldConfig) ecs.World {
world := ecs.NewWorld(config.Tick)

View File

@ -1,6 +1,9 @@
package system
import "joylink.club/ecs"
import (
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
)
// ButtonState 广义按钮开关状态,开关有三个接点,分别为公共接点、常开点和常闭点
type ButtonState struct {
@ -8,6 +11,49 @@ type ButtonState struct {
Ckd bool
}
// ButtonSelfRestState 广义自复位按钮,复位状态
type ButtonSelfRestState struct {
//自复位按钮保持按下剩余时间ms
KeepAxTime int64
}
var (
ButtonStateComponent = ecs.NewComponentType[ButtonState]()
ButtonSelfRestTag = ecs.NewComponentType[ButtonSelfRestState]() //自复位按钮标签
ButtonNonRestTag = ecs.NewTag() //非自复位按钮标签
)
type ButtonSystem struct {
selfRestQuery *ecs.Query
}
func NewButtonSystem() *ButtonSystem {
return &ButtonSystem{selfRestQuery: ecs.NewQuery(filter.Contains(ButtonStateComponent, ButtonSelfRestTag))}
}
func NewButtonState() *ButtonState {
return &ButtonState{Ckd: false}
}
// Update world 执行
func (me *ButtonSystem) Update(w ecs.World) {
me.selfRestQuery.Each(w, func(e *ecs.Entry) {
state := ButtonStateComponent.Get(e)
me.calculateSelfRest(w, e, state)
})
}
// 自复位按钮运算
func (me *ButtonSystem) calculateSelfRest(w ecs.World, e *ecs.Entry, state *ButtonState) {
if state.Ckd {
reset := ButtonSelfRestTag.Get(e)
if reset.KeepAxTime > 0 {
reset.KeepAxTime -= int64(w.Tick())
if reset.KeepAxTime < 0 {
reset.KeepAxTime = 0
}
}
if reset.KeepAxTime <= 0 {
state.Ckd = false
}
}
}

246
system/dcb_system.go Normal file
View File

@ -0,0 +1,246 @@
package system
import (
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
)
// DcbCircuitState 门控箱电路 A---按钮 J---继电器
type DcbCircuitState struct {
//下行站台门关闭继电器true-继电器吸起
XPCBJ bool
//ture-按钮开关接通常开点
MKX5_PCB5A bool
//ture-按钮开关接通常开点
MKX5_PCB5_MKXPL5A bool
//ture-按钮开关接通常开点
MKX3_PCB3A bool
//ture-按钮开关接通常开点
MKX3_PCB3_MKXPL3A bool
//ture-按钮开关接通常开点
MKX1_PCB1A bool
//ture-按钮开关接通常开点
MKX1_PCB1_MKXPL1A bool
//下行站台门打开继电器true-继电器吸起
XPOBJ bool
//ture-按钮开关接通常开点
MKX5_POB5A bool
//ture-按钮开关接通常开点
MKX5_POB5_MKXPL5A bool
//ture-按钮开关接通常开点
MKX3_POB3A bool
//ture-按钮开关接通常开点
MKX3_POB3_MKXPL3A bool
//ture-按钮开关接通常开点
MKX1_POB1A bool
//ture-按钮开关接通常开点
MKX1_POB1_MKXPL1A bool
//下行站台确认继电器true-继电器吸起
XPABJ bool
//ture-按钮开关接通常开点
MKX5_PAB5A bool
//ture-按钮开关接通常开点
MKX5_PAB5_MKXPL5A bool
//ture-按钮开关接通常开点
MKX3_PAB3A bool
//ture-按钮开关接通常开点
MKX3_PAB3_MKXPL3A bool
//ture-按钮开关接通常开点
MKX1_PAB1A bool
//ture-按钮开关接通常开点
MKX1_PAB1_MKXPL1A bool
//上行站台门关闭继电器true-继电器吸起
SPCBJ bool
//ture-按钮开关接通常开点
MKX6_PCB6A bool
//ture-按钮开关接通常开点
MKX6_PCB6_MKXPL6A bool
//ture-按钮开关接通常开点
MKX4_PCB4A bool
//ture-按钮开关接通常开点
MKX4_PCB4_MKXPL4A bool
//ture-按钮开关接通常开点
MKX2_PCB2A bool
//ture-按钮开关接通常开点
MKX2_PCB2_MKXPL2A bool
//上行站台门开门继电器true-继电器吸起
SPOBJ bool
//ture-按钮开关接通常开点
MKX6_POB6A bool
//ture-按钮开关接通常开点
MKX6_POB6_MKXPL6A bool
//ture-按钮开关接通常开点
MKX4_POB4A bool
//ture-按钮开关接通常开点
MKX4_POB4_MKXPL4A bool
//ture-按钮开关接通常开点
MKX2_POB2A bool
//ture-按钮开关接通常开点
MKX2_POB2_MKXPL2A bool
//上行站台门开门继电器true-继电器吸起
SPABJ bool
//ture-按钮开关接通常开点
MKX6_PAB6A bool
//ture-按钮开关接通常开点
MKX6_PAB6_MKXPL6A bool
//ture-按钮开关接通常开点
MKX4_PAB4A bool
//ture-按钮开关接通常开点
MKX4_PAB4_MKXPL4A bool
//ture-按钮开关接通常开点
MKX2_PAB2A bool
//ture-按钮开关接通常开点
MKX2_PAB2_MKXPL2A bool
}
var DcbCircuitStateComponent = ecs.NewComponentType[DcbCircuitState]()
type DcbCircuitSystem struct {
query *ecs.Query
}
func NewDcbCircuitState() *DcbCircuitState {
return &DcbCircuitState{
//下行站台门关闭继电器true-继电器吸起
XPCBJ: false,
//ture-按钮开关接通常开点
MKX5_PCB5A: false,
//ture-按钮开关接通常开点
MKX5_PCB5_MKXPL5A: true,
//ture-按钮开关接通常开点
MKX3_PCB3A: false,
//ture-按钮开关接通常开点
MKX3_PCB3_MKXPL3A: true,
//ture-按钮开关接通常开点
MKX1_PCB1A: false,
//ture-按钮开关接通常开点
MKX1_PCB1_MKXPL1A: true,
//下行站台门打开继电器true-继电器吸起
XPOBJ: false,
//ture-按钮开关接通常开点
MKX5_POB5A: false,
//ture-按钮开关接通常开点
MKX5_POB5_MKXPL5A: true,
//ture-按钮开关接通常开点
MKX3_POB3A: false,
//ture-按钮开关接通常开点
MKX3_POB3_MKXPL3A: true,
//ture-按钮开关接通常开点
MKX1_POB1A: false,
//ture-按钮开关接通常开点
MKX1_POB1_MKXPL1A: true,
//下行站台确认继电器true-继电器吸起
XPABJ: false,
//ture-按钮开关接通常开点
MKX5_PAB5A: false,
//ture-按钮开关接通常开点
MKX5_PAB5_MKXPL5A: true,
//ture-按钮开关接通常开点
MKX3_PAB3A: false,
//ture-按钮开关接通常开点
MKX3_PAB3_MKXPL3A: true,
//ture-按钮开关接通常开点
MKX1_PAB1A: false,
//ture-按钮开关接通常开点
MKX1_PAB1_MKXPL1A: true,
//上行站台门关闭继电器true-继电器吸起
SPCBJ: false,
//ture-按钮开关接通常开点
MKX6_PCB6A: false,
//ture-按钮开关接通常开点
MKX6_PCB6_MKXPL6A: true,
//ture-按钮开关接通常开点
MKX4_PCB4A: false,
//ture-按钮开关接通常开点
MKX4_PCB4_MKXPL4A: true,
//ture-按钮开关接通常开点
MKX2_PCB2A: false,
//ture-按钮开关接通常开点
MKX2_PCB2_MKXPL2A: true,
//上行站台门开门继电器true-继电器吸起
SPOBJ: false,
//ture-按钮开关接通常开点
MKX6_POB6A: false,
//ture-按钮开关接通常开点
MKX6_POB6_MKXPL6A: true,
//ture-按钮开关接通常开点
MKX4_POB4A: false,
//ture-按钮开关接通常开点
MKX4_POB4_MKXPL4A: true,
//ture-按钮开关接通常开点
MKX2_POB2A: false,
//ture-按钮开关接通常开点
MKX2_POB2_MKXPL2A: true,
//上行站台门开门继电器true-继电器吸起
SPABJ: false,
//ture-按钮开关接通常开点
MKX6_PAB6A: false,
//ture-按钮开关接通常开点
MKX6_PAB6_MKXPL6A: true,
//ture-按钮开关接通常开点
MKX4_PAB4A: false,
//ture-按钮开关接通常开点
MKX4_PAB4_MKXPL4A: true,
//ture-按钮开关接通常开点
MKX2_PAB2A: false,
//ture-按钮开关接通常开点
MKX2_PAB2_MKXPL2A: true,
}
}
func NewDcbCircuitSystem() *DcbCircuitSystem {
return &DcbCircuitSystem{query: ecs.NewQuery(filter.Contains(DcbCircuitStateComponent))}
}
// Update world 执行
func (me *DcbCircuitSystem) Update(w ecs.World) {
me.query.Each(w, func(e *ecs.Entry) {
state := DcbCircuitStateComponent.Get(e)
//
me.calculateXPCBJ(state)
me.calculateXPOBJ(state)
me.calculateXPABJ(state)
me.calculateSPCBJ(state)
me.calculateSPOBJ(state)
me.calculateSPABJ(state)
})
}
func (me *DcbCircuitSystem) calculateXPCBJ(state *DcbCircuitState) {
state.XPCBJ = state.MKX5_PCB5A && state.MKX5_PCB5_MKXPL5A ||
state.MKX3_PCB3A && state.MKX3_PCB3_MKXPL3A ||
state.MKX1_PCB1A && state.MKX1_PCB1_MKXPL1A
}
func (me *DcbCircuitSystem) calculateXPOBJ(state *DcbCircuitState) {
state.XPOBJ = state.MKX5_POB5A && state.MKX5_POB5_MKXPL5A ||
state.MKX3_POB3A && state.MKX3_POB3_MKXPL3A ||
state.MKX1_POB1A && state.MKX1_POB1_MKXPL1A
}
func (me *DcbCircuitSystem) calculateXPABJ(state *DcbCircuitState) {
state.XPABJ = state.MKX5_PAB5A && state.MKX5_PAB5_MKXPL5A ||
state.MKX3_PAB3A && state.MKX3_PAB3_MKXPL3A ||
state.MKX1_PAB1A && state.MKX1_PAB1_MKXPL1A
}
func (me *DcbCircuitSystem) calculateSPCBJ(state *DcbCircuitState) {
state.SPCBJ = state.MKX6_PCB6A && state.MKX6_PCB6_MKXPL6A ||
state.MKX4_PCB4A && state.MKX4_PCB4_MKXPL4A ||
state.MKX2_PCB2A && state.MKX2_PCB2_MKXPL2A
}
func (me *DcbCircuitSystem) calculateSPOBJ(state *DcbCircuitState) {
state.SPOBJ = state.MKX6_POB6A && state.MKX6_POB6_MKXPL6A ||
state.MKX4_POB4A && state.MKX4_POB4_MKXPL4A ||
state.MKX2_POB2A && state.MKX2_POB2_MKXPL2A
}
func (me *DcbCircuitSystem) calculateSPABJ(state *DcbCircuitState) {
state.SPABJ = state.MKX6_PAB6A && state.MKX6_PAB6_MKXPL6A ||
state.MKX4_PAB4A && state.MKX4_PAB4_MKXPL4A ||
state.MKX2_PAB2A && state.MKX2_PAB2_MKXPL2A
}

View File

@ -0,0 +1,3 @@
package sysEvent
//广义按钮相关事件

View File

@ -116,6 +116,21 @@ type Switch2jZdj9State struct {
J2_Traction int8
}
// IsTurning 道岔物理实际状态而非采集状态:道岔是否在转动过程中
func (me *Switch2jZdj9State) IsTurning() bool {
return !me.J1_DB_K9 && !me.J1_FB_K10 || !me.J2_DB_K9 && !me.J2_FB_K10
}
// IsNormal 道岔物理实际状态而非采集状态:道岔是否在定位
func (me *Switch2jZdj9State) IsNormal() bool {
return me.J1_DB_K9 && !me.J1_FB_K10 && me.J2_DB_K9 && !me.J2_FB_K10
}
// IsReverse 道岔物理实际状态而非采集状态:道岔是否在反位
func (me *Switch2jZdj9State) IsReverse() bool {
return !me.J1_DB_K9 && me.J1_FB_K10 && !me.J2_DB_K9 && me.J2_FB_K10
}
// NewSwitch2jZdj9State 创建ZDJ9道岔状态并初始化
func NewSwitch2jZdj9State() *Switch2jZdj9State {
return &Switch2jZdj9State{