From b6534bc5544404a73b4a2befc29a19cde307f6f7 Mon Sep 17 00:00:00 2001 From: weizhihong Date: Thu, 26 Oct 2023 18:00:17 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=8F=90=E5=87=BA=E7=AC=AC=E4=B8=89?= =?UTF-8?q?=E6=96=B9=E3=80=81world=E5=90=AF=E5=8A=A8=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=81=E4=BF=AE=E6=94=B9=E8=BF=90=E8=A1=8C=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E8=BF=94=E5=9B=9E=E7=BB=93=E6=9E=9C=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/projectRunConfig.go | 2 +- third_party/dynamics/dynamics.go | 3 +- third_party/interlock/interlock.go | 17 ++++-- .../wayside/memory/wayside_simulation.go | 60 +----------------- ts/test_simulation_manage.go | 61 ++++++++++++++++++- 5 files changed, 75 insertions(+), 68 deletions(-) diff --git a/service/projectRunConfig.go b/service/projectRunConfig.go index 0f10e6f..94210dc 100644 --- a/service/projectRunConfig.go +++ b/service/projectRunConfig.go @@ -21,7 +21,7 @@ func PageProjectRunConfigQuery(query *dto.PageProjectRunConfigReqDto) *dto.PageD if err != nil { panic(sys_error.New("查询失败,数据库错误请联系运维人员", err)) } - return &dto.PageDto{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records} + return &dto.PageDto{Total: int(total), PageQueryDto: query.PageQueryDto, Records: dto.ConvertToRunConfigFromSlice(records)} } // 查询项目运行环境列表 diff --git a/third_party/dynamics/dynamics.go b/third_party/dynamics/dynamics.go index f951394..18f8451 100644 --- a/third_party/dynamics/dynamics.go +++ b/third_party/dynamics/dynamics.go @@ -205,8 +205,7 @@ func (d *dynamics) initDynamics() { // 动力学运行所需数据 func (d *dynamics) initDynamicsRunRepository() error { // 动力学接口调用 - lineBaseInfo := d.manager.GetDynamicsRunRepository() - err := d.requestStartSimulation(lineBaseInfo) + err := d.requestStartSimulation(d.manager.GetDynamicsRunRepository()) if err != nil { return err } diff --git a/third_party/interlock/interlock.go b/third_party/interlock/interlock.go index 8487bc1..b7686a4 100644 --- a/third_party/interlock/interlock.go +++ b/third_party/interlock/interlock.go @@ -15,7 +15,7 @@ import ( // 联锁代理通信接口 type InterlockMessageManager interface { - CollectRelayInfo() *message.InterlockSendMsgPkg + CollectRelayInfo() []*message.InterlockSendMsgPkg HandleDriverInfo(b []byte) GetInterlockRunConfig() *config.InterlockConfig } @@ -78,10 +78,11 @@ func (i *interlockProxy) Start(manager InterlockMessageManager) { i.collectInfoTaskCancel = cancle } -const ( - // 采集电路状态发送间隔,单位ms - InterlockMessageSendInterval = 50 -) +// 采集电路状态发送间隔,单位ms +const InterlockMessageSendInterval = 50 + +// 序列号 +var serialNumber uint8 // 定时发送采集电路状态任务 func (i *interlockProxy) collectInfoStateTask(ctx context.Context) { @@ -98,7 +99,11 @@ func (i *interlockProxy) collectInfoStateTask(ctx context.Context) { default: } collectInfoStates := i.manager.CollectRelayInfo() - i.sendCollectUdpClient.SendMsg(collectInfoStates) + for _, state := range collectInfoStates { + serialNumber++ + state.Header.SerialNumber = serialNumber + i.sendCollectUdpClient.SendMsg(state) + } time.Sleep(time.Millisecond * InterlockMessageSendInterval) } } diff --git a/ts/simulation/wayside/memory/wayside_simulation.go b/ts/simulation/wayside/memory/wayside_simulation.go index 7ecdeef..46cf397 100644 --- a/ts/simulation/wayside/memory/wayside_simulation.go +++ b/ts/simulation/wayside/memory/wayside_simulation.go @@ -5,20 +5,16 @@ import ( "fmt" "log/slog" "math" - "runtime" "sort" "strconv" "strings" "sync" "time" - rtss_simulation "joylink.club/rtsssimulation" - "joylink.club/bj-rtsts-server/config" "joylink.club/bj-rtsts-server/dto" "joylink.club/bj-rtsts-server/sys_error" "joylink.club/bj-rtsts-server/third_party/dynamics" - "joylink.club/bj-rtsts-server/third_party/interlock" "joylink.club/bj-rtsts-server/third_party/message" "joylink.club/bj-rtsts-server/third_party/semi_physical_train" "joylink.club/bj-rtsts-server/ts/protos/graphicData" @@ -109,16 +105,6 @@ func CreateSimulation(projectId int32, mapIds []int32, runConfig string) (*Verif if err != nil { return nil, err } - // 创建world - err = verifySimulation.initWorld() - if err != nil { - return nil, err - } - // 运行第三方服务 - err = verifySimulation.runThirdParty() - if err != nil { - return nil, err - } return verifySimulation, nil } @@ -416,7 +402,7 @@ func (s *VerifySimulation) GetInterlockRunConfig() *config.InterlockConfig { } // 采集联锁中的继电器消息 -func (s *VerifySimulation) CollectRelayInfo() *message.InterlockSendMsgPkg { +func (s *VerifySimulation) CollectRelayInfo() []*message.InterlockSendMsgPkg { msg := &message.InterlockSendMsgPkg{} relayArr := make([]string, 256) for index, id := range relayArr { @@ -427,7 +413,7 @@ func (s *VerifySimulation) CollectRelayInfo() *message.InterlockSendMsgPkg { } } - return msg + return []*message.InterlockSendMsgPkg{} } // 初始化仿真运行配置 @@ -473,48 +459,6 @@ func (s *VerifySimulation) initRepository() error { return nil } -// 创建world -func (s *VerifySimulation) initWorld() error { - //创建仿真 - w, err := rtss_simulation.NewSimulation(s.Repo) - if err != nil { - return sys_error.New("仿真创建失败", err) - } - s.World = w - // 保证World关闭 - runtime.SetFinalizer(s, func(verifySimulation *VerifySimulation) { - slog.Info("---关闭仿真World---") - verifySimulation.World.Close() - }) - return nil -} - -// 运行仿真第三方模块 -func (s *VerifySimulation) runThirdParty() error { - // 动力学启动 - err := dynamics.Default().Start(s) - if err != nil { - return err - } - // 半实物启动 - semi_physical_train.Default().Start(s) - // 联锁启动 - interlock.Default().Start(s) - return nil -} - -// 停止仿真 -func (s *VerifySimulation) StopSimulation() { - // 停止ecs world - s.World.Close() - // 停止动力学接口功能 - dynamics.Default().Stop() - // 停止半实物 - semi_physical_train.Default().Stop() - // 联锁启动 - interlock.Default().Stop() -} - func buildProtoRepository(mapIds []int32) (*proto.Repository, error) { repo := &proto.Repository{} var exceptStationGiMapIds []int32 diff --git a/ts/test_simulation_manage.go b/ts/test_simulation_manage.go index e007972..8cc3be1 100644 --- a/ts/test_simulation_manage.go +++ b/ts/test_simulation_manage.go @@ -1,15 +1,21 @@ package ts import ( + "log/slog" + "runtime" "strconv" "sync" "joylink.club/bj-rtsts-server/config" "joylink.club/bj-rtsts-server/message_server" "joylink.club/bj-rtsts-server/sys_error" + "joylink.club/bj-rtsts-server/third_party/dynamics" + "joylink.club/bj-rtsts-server/third_party/interlock" + "joylink.club/bj-rtsts-server/third_party/semi_physical_train" "joylink.club/bj-rtsts-server/ts/simulation/wayside/memory" "joylink.club/bj-rtsts-server/dto" + rtss_simulation "joylink.club/rtsssimulation" ) // 仿真存储集合 @@ -38,6 +44,16 @@ func CreateSimulation(projectId int32, mapIds []int32, runConfig string) (string return "", err } verifySimulation.SimulationId = simulationId + // world构建 + err = initWorld(verifySimulation) + if err != nil { + return "", err + } + // 第三方服务处理 + err = runThirdParty(verifySimulation) + if err != nil { + return "", err + } simulationMap.Store(simulationId, verifySimulation) // 全部成功,启动仿真 verifySimulation.World.StartUp() @@ -55,8 +71,51 @@ func DestroySimulation(simulationId string) { } simulationMap.Delete(simulationId) simulationInfo := s.(*memory.VerifySimulation) + // 停止ecs world + simulationInfo.World.Close() + // 停止第三方 + stopThirdParty(simulationInfo) message_server.Close(simulationInfo) - simulationInfo.StopSimulation() +} + +// 创建world +func initWorld(s *memory.VerifySimulation) error { + //创建仿真 + w, err := rtss_simulation.NewSimulation(s.Repo) + if err != nil { + return sys_error.New("仿真创建失败", err) + } + s.World = w + // 保证World关闭 + runtime.SetFinalizer(s, func(verifySimulation *memory.VerifySimulation) { + slog.Info("---关闭仿真World---") + verifySimulation.World.Close() + }) + return nil +} + +// 运行仿真第三方模块 +func runThirdParty(s *memory.VerifySimulation) error { + // 动力学启动 + err := dynamics.Default().Start(s) + if err != nil { + return err + } + // 半实物启动 + semi_physical_train.Default().Start(s) + // 联锁启动 + interlock.Default().Start(s) + return nil +} + +// 停止仿真 +func stopThirdParty(s *memory.VerifySimulation) { + // 停止动力学接口功能 + dynamics.Default().Stop() + // 停止半实物 + semi_physical_train.Default().Stop() + // 联锁启动 + interlock.Default().Stop() } func createSimulationId(projectId int32) string {