package main import ( "fmt" "time" "joylink.club/ecs" "joylink.club/rtsssimulation/components" "joylink.club/rtsssimulation/cstate" "joylink.club/rtsssimulation/memory" "joylink.club/rtsssimulation/memory/wdcreator" "joylink.club/rtsssimulation/model" "joylink.club/rtsssimulation/system" "joylink.club/rtsssimulation/umi" ) type Simulation struct { FaceSystem *system.FaceSystem World ecs.World } func main() { var sim *Simulation = &Simulation{} // initModels() initSimWorld(sim, 300) // sim.World.StartUp() time.Sleep(2 * time.Second) se := sim.FaceSystem.FindEntity("signal1") fmt.Println("==>>fe : ", se) //time.Sleep(60 * time.Second) // testSwitchTurn(sim.World, sim.FaceSystem) //testSignalOpt(world, face) //testPsdCellOpt(world, face) //testPsdCellOpt(sim.World, sim.FaceSystem) // 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 //信号机相关组件 mds.ForEachModelsByType(cstate.DeviceType_Signal, func(md memory.ModelData) { entry := wdcreator.CreateSignalEntity(sim.World) components.DeviceIdentityComponent.Set(entry, &cstate.DeviceIdentity{Id: md.GetId()}) components.SignalStateComponent.Set(entry, &cstate.SignalState{Display: cstate.SignalAspect_No}) }) //道岔相关组件 mds.ForEachModelsByType(cstate.DeviceType_Switch, func(md memory.ModelData) { 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}) }) //屏蔽门相关组件 mds.ForEachModelsByType(cstate.DeviceType_Psd, func(md memory.ModelData) { 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}) } }) //添加必要的系统 sim.World.AddSystem(system.NewSwitchSystem()) sim.World.AddSystem(system.NewSignalSystem()) sim.World.AddSystem(system.NewPsdSystem()) } // 构建模型 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) // } //////////////////////////////////////////////////////////////// func testPsdOpt(world ecs.World, face *system.FaceSystem) { face.Call(func(w ecs.World) any { return system.FirePsdOperation(w, "psd1", true) }) time.Sleep(5 * time.Second) face.Call(func(w ecs.World) any { return system.FirePsdOperation(w, "psd1", false) }) } func testPsdCellOpt(world ecs.World, face *system.FaceSystem) { face.Call(func(w ecs.World) any { return system.FirePsdCellOperation(w, "psd1", "psd1Cell1", 0) }) face.Call(func(w ecs.World) any { return system.FirePsdCellOperation(w, "psd1", "psd1Cell2", 0) }) time.Sleep(10 * time.Second) face.Call(func(w ecs.World) any { return system.FirePsdCellOperation(w, "psd1", "psd1Cell1", 100-20) }) face.Call(func(w ecs.World) any { return system.FirePsdCellOperation(w, "psd1", "psd1Cell2", 100) }) } func testSwitchTurn(world ecs.World, face *system.FaceSystem) { reslult, _ := face.Call(func(w ecs.World) any { fmt.Println("==>>1触发转动道岔 ...") return system.FireSwitchTurn(w, "switch1", system.SwitchReverseRate) }) fmt.Println("==>>1触发转动道岔 。。。", reslult) time.Sleep(8 * time.Second) reslult2, _ := face.Call(func(w ecs.World) any { fmt.Println("==>>2触发转动道岔 ...") return system.FireSwitchTurn(w, "switch1", system.SwitchNormalRate) }) fmt.Println("==>>2触发转动道岔 。。。", reslult2) } func testSignalOpt(world ecs.World, face *system.FaceSystem) { face.Call(func(w ecs.World) any { return system.SetSignalDisplay(w, "siganl1", cstate.SignalAspect_B) }) }