rts-sim-module/examples/test1/main.go

184 lines
6.2 KiB
Go
Raw Normal View History

2023-08-22 14:37:30 +08:00
package main
import (
"fmt"
"time"
"joylink.club/ecs"
"joylink.club/rtsssimulation/components"
2023-08-23 15:13:06 +08:00
"joylink.club/rtsssimulation/components/cstate"
2023-08-23 16:01:01 +08:00
"joylink.club/rtsssimulation/operate"
2023-08-23 15:23:30 +08:00
"joylink.club/rtsssimulation/storages/memory"
"joylink.club/rtsssimulation/storages/memory/wdcreator"
"joylink.club/rtsssimulation/storages/model"
2023-08-22 14:37:30 +08:00
"joylink.club/rtsssimulation/system"
"joylink.club/rtsssimulation/umi"
)
type Simulation struct {
FaceSystem *system.FaceSystem
World ecs.World
2023-08-23 15:13:06 +08:00
Status memory.StatusStorage
2023-08-22 14:37:30 +08:00
}
func main() {
var sim *Simulation = &Simulation{}
//
initModels()
initSimWorld(sim, 300)
//
sim.World.StartUp()
time.Sleep(2 * time.Second)
2023-08-22 15:04:46 +08:00
//time.Sleep(60 * time.Second)
2023-08-22 14:37:30 +08:00
//
2023-08-22 17:21:21 +08:00
//testSwitchTurn(sim.World, sim.FaceSystem)
2023-08-22 14:37:30 +08:00
//testSignalOpt(world, face)
//testPsdCellOpt(world, face)
//testPsdCellOpt(sim.World, sim.FaceSystem)
2023-08-22 17:21:21 +08:00
testButtonOpt(sim.World, sim.FaceSystem)
2023-08-22 14:37:30 +08:00
//
time.Sleep(300 * time.Second)
}
// 构建仿真world
func initSimWorld(sim *Simulation, tick int) {
sim.World = ecs.NewWorld(tick)
sim.FaceSystem = wdcreator.InitializeWorld(sim.World, time.Now())
//
mds := memory.DeviceModelStorage
//信号机相关组件
2023-08-23 16:52:01 +08:00
mds.ForEachModelsByType(umi.Signal, func(md memory.ModelData) {
2023-08-22 14:37:30 +08:00
entry := wdcreator.CreateSignalEntity(sim.World)
components.DeviceIdentityComponent.Set(entry, &cstate.DeviceIdentity{Id: md.GetId()})
components.SignalStateComponent.Set(entry, &cstate.SignalState{Display: cstate.SignalAspect_No})
})
//道岔相关组件
2023-08-23 16:52:01 +08:00
mds.ForEachModelsByType(umi.Switch, func(md memory.ModelData) {
2023-08-22 14:37:30 +08:00
entry := wdcreator.CreateSwitchEntity(sim.World)
components.DeviceIdentityComponent.Set(entry, &cstate.DeviceIdentity{Id: md.GetId()})
components.SwitchRelayStateComponent.Set(entry, &cstate.SwitchRelayState{DcJ: false, FcJ: false, DbJ: true, FbJ: false})
components.PercentageDeviceComponent.Set(entry, &cstate.PercentageDeviceState{Rate: system.SwitchNormalRate})
})
//屏蔽门相关组件
2023-08-23 16:52:01 +08:00
mds.ForEachModelsByType(umi.Psd, func(md memory.ModelData) {
2023-08-22 14:37:30 +08:00
psdEntry := wdcreator.CreatePsdEntity(sim.World)
components.DeviceIdentityComponent.Set(psdEntry, &cstate.DeviceIdentity{Id: md.GetId()})
components.PsdStateComponent.Set(psdEntry, &cstate.PsdState{AllClosed: true, AllOpened: false, InterlockReleased: false})
//
psd := md.(umi.IPsdModel)
for _, psdCell := range psd.AllDeviceCells() {
cellEntry := wdcreator.CreatePsdCellEntity(sim.World, psdEntry)
components.DeviceIdentityComponent.Set(cellEntry, &cstate.DeviceIdentity{Id: psdCell.GetId()})
components.PercentageDeviceComponent.Set(cellEntry, &cstate.PercentageDeviceState{Rate: system.PsdCellWholeCloseRate})
}
})
2023-08-22 17:21:21 +08:00
//按钮相关组件
2023-08-23 16:52:01 +08:00
mds.ForEachModelsByType(umi.TowPosButton, func(md memory.ModelData) {
2023-08-22 17:21:21 +08:00
entry := wdcreator.CreateTowPosButtonEntity(sim.World)
components.DeviceIdentityComponent.Set(entry, &cstate.DeviceIdentity{Id: md.GetId()})
components.TowPositionButtonStateComponent.Set(entry, &cstate.TowPositionButtonState{Pos1: false, Pos2: true})
})
2023-08-22 14:37:30 +08:00
//添加必要的系统
sim.World.AddSystem(system.NewSwitchSystem())
sim.World.AddSystem(system.NewPsdSystem())
2023-08-23 16:01:01 +08:00
sim.World.AddSystem(system.NewPercentageSystem())
2023-08-22 14:37:30 +08:00
}
// 构建模型
func initModels() {
mds := memory.DeviceModelStorage
//
link1 := model.NewLinkModel("link1")
link2 := model.NewLinkModel("link2")
mds.AddModel(link1)
mds.AddModel(link2)
//
signal1 := model.NewSignalModel("signal1")
signal2 := model.NewSignalModel("signal2")
mds.AddModel(signal1)
mds.AddModel(signal2)
//
switch1 := model.NewSwitchModel("switch1")
switch1.TurnTime = 3000
switch2 := model.NewSwitchModel("switch2")
switch2.TurnTime = 2500
mds.AddModel(switch1)
mds.AddModel(switch2)
//
psd1 := model.NewPsdModel("psd1")
psd1.MoveTime = 5000
psd1Cell1 := model.NewPsdCellModel("psd1Cell1", psd1)
psd1Cell2 := model.NewPsdCellModel("psd1Cell2", psd1)
psd2 := model.NewPsdModel("psd2")
psd2.MoveTime = 6000
psd2Cell1 := model.NewPsdCellModel("psd2Cell1", psd2)
psd2Cell2 := model.NewPsdCellModel("psd2Cell2", psd2)
mds.AddModel(psd1)
mds.AddModel(psd1Cell1)
mds.AddModel(psd1Cell2)
mds.AddModel(psd2)
mds.AddModel(psd2Cell1)
mds.AddModel(psd2Cell2)
//
2023-08-22 17:21:21 +08:00
button1 := model.NewTowPosButtonModel("button1")
mds.AddModel(button1)
2023-08-22 14:37:30 +08:00
}
////////////////////////////////////////////////////////////////
func testPsdOpt(world ecs.World, face *system.FaceSystem) {
face.Call(func(w ecs.World) any {
2023-08-23 16:01:01 +08:00
return operate.FirePsdOperation(w, "psd1", true)
2023-08-22 14:37:30 +08:00
})
time.Sleep(5 * time.Second)
face.Call(func(w ecs.World) any {
2023-08-23 16:01:01 +08:00
return operate.FirePsdOperation(w, "psd1", false)
2023-08-22 14:37:30 +08:00
})
}
func testPsdCellOpt(world ecs.World, face *system.FaceSystem) {
face.Call(func(w ecs.World) any {
2023-08-23 16:01:01 +08:00
return operate.FirePsdCellOperation(w, "psd1", "psd1Cell1", 0)
2023-08-22 14:37:30 +08:00
})
face.Call(func(w ecs.World) any {
2023-08-23 16:01:01 +08:00
return operate.FirePsdCellOperation(w, "psd1", "psd1Cell2", 0)
2023-08-22 14:37:30 +08:00
})
time.Sleep(10 * time.Second)
face.Call(func(w ecs.World) any {
2023-08-23 16:01:01 +08:00
return operate.FirePsdCellOperation(w, "psd1", "psd1Cell1", 100-20)
2023-08-22 14:37:30 +08:00
})
face.Call(func(w ecs.World) any {
2023-08-23 16:01:01 +08:00
return operate.FirePsdCellOperation(w, "psd1", "psd1Cell2", 100)
2023-08-22 14:37:30 +08:00
})
}
func testSwitchTurn(world ecs.World, face *system.FaceSystem) {
reslult, _ := face.Call(func(w ecs.World) any {
fmt.Println("==>>1触发转动道岔 ...")
2023-08-23 16:01:01 +08:00
return operate.FireSwitchTurn(w, "switch1", system.SwitchReverseRate)
2023-08-22 14:37:30 +08:00
})
fmt.Println("==>>1触发转动道岔 。。。", reslult)
time.Sleep(8 * time.Second)
reslult2, _ := face.Call(func(w ecs.World) any {
fmt.Println("==>>2触发转动道岔 ...")
2023-08-23 16:01:01 +08:00
return operate.FireSwitchTurn(w, "switch1", system.SwitchNormalRate)
2023-08-22 14:37:30 +08:00
})
fmt.Println("==>>2触发转动道岔 。。。", reslult2)
}
func testSignalOpt(world ecs.World, face *system.FaceSystem) {
face.Call(func(w ecs.World) any {
2023-08-23 16:01:01 +08:00
return operate.SetSignalDisplay(w, "siganl1", cstate.SignalAspect_B)
2023-08-22 14:37:30 +08:00
})
}
2023-08-22 17:21:21 +08:00
func testButtonOpt(world ecs.World, face *system.FaceSystem) {
face.Call(func(w ecs.World) any {
2023-08-23 16:01:01 +08:00
return operate.FireTowPositionButtonMoving(world, "button1")
2023-08-22 17:21:21 +08:00
})
time.Sleep(3 * time.Second)
face.Call(func(w ecs.World) any {
2023-08-23 16:01:01 +08:00
return operate.FireTowPositionButtonArrivedPos1(world, "button1")
2023-08-22 17:21:21 +08:00
})
time.Sleep(3 * time.Second)
face.Call(func(w ecs.World) any {
2023-08-23 16:01:01 +08:00
return operate.FireTowPositionButtonArrivedPos2(world, "button1")
2023-08-22 17:21:21 +08:00
})
}