From b1e8a39fe330b1d3ebd3600fab6cffa1c2e8afa3 Mon Sep 17 00:00:00 2001 From: weizhihong Date: Mon, 31 Jul 2023 17:27:05 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E4=BB=BF=E7=9C=9F?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E9=80=BB=E8=BE=91=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/simulation.go | 153 +++++++++++++++++++++------------ dto/simulation.go | 4 +- grpcproto/simulation_server.go | 3 +- service/simulation.go | 1 + 4 files changed, 103 insertions(+), 58 deletions(-) diff --git a/api/simulation.go b/api/simulation.go index 267ee69..38a2e54 100644 --- a/api/simulation.go +++ b/api/simulation.go @@ -1,12 +1,19 @@ package api import ( + "fmt" "net/http" + "strconv" + "strings" jwt "github.com/appleboy/gin-jwt/v2" "github.com/gin-gonic/gin" + "github.com/golang/protobuf/proto" "go.uber.org/zap" + "joylink.club/bj-rtsts-server/ats/verify/protos/graphicData" + "joylink.club/bj-rtsts-server/ats/verify/protos/state" "joylink.club/bj-rtsts-server/ats/verify/simulation" + "joylink.club/bj-rtsts-server/ats/verify/simulation/wayside" "joylink.club/bj-rtsts-server/dto" apiproto "joylink.club/bj-rtsts-server/grpcproto" "joylink.club/bj-rtsts-server/service" @@ -17,8 +24,8 @@ func InitSimulationRouter(api *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddle authed.POST("/create", create) authed.POST("/destroy/:id", destroy) authed.GET("/list", findAllSimulations) - authed.POST("/train/add", addTrain) authed.POST("/check/data", checkSimMapData) + authed.POST("/train/add", addTrain) authed.POST("/train/remove", removeTrain) authed.POST("/switch/operation", switchOperation) @@ -47,39 +54,11 @@ func create(c *gin.Context) { panic(err) } zap.S().Debug("创建仿真请求:", req) - rsp := dto.SimulationCreateRspDto{} - rsp.MapId = req.MapId - //rsp.SimulationId = fmt.Sprintf("sim-%d", req.MapId) - mapData := service.QueryRtssGraphicStorage(req.MapId) - rsp.SimulationId = simulation.CreateSimulation(int(rsp.MapId), mapData) - c.JSON(http.StatusOK, &rsp) -} - -// ATS测试仿真-添加列车 -// -// @Summary ATS测试仿真-添加列车 -// -// @Security JwtAuth -// -// @Description ATS测试仿真-添加列车 -// @Tags ATS测试仿真Api -// @Accept json -// @Produce json -// @Param Authorization header string true "JWT Token" -// @Param AddTrainReqDto body dto.AddTrainReqDto true "ATS测试仿真-添加列车" -// @Success 200 {object} dto.AddTrainRspDto -// @Failure 500 {object} dto.ErrorDto -// @Router /api/v1/simulation/train/add [post] -func addTrain(c *gin.Context) { - //user,_:=c.Get(middleware.IdentityKey) - req := dto.AddTrainReqDto{} - if err := c.ShouldBind(&req); nil != err { - panic(err) + rsp := dto.SimulationCreateRspDto{ + MapId: req.MapId, } - zap.S().Debug("ATS测试仿真-添加列车,请求:", req) - rsp := dto.AddTrainRspDto{} - rsp.SimulationId = req.SimulationId - rsp.TrainId = "666" + mapData := service.QueryRtssGraphicStorage(req.MapId) + rsp.SimulationId = simulation.CreateSimulation(int(req.MapId), mapData) c.JSON(http.StatusOK, &rsp) } @@ -122,8 +101,16 @@ func destroy(c *gin.Context) { // @Router /api/v1/simulation/list [get] func findAllSimulations(c *gin.Context) { zap.S().Debug("ATS测试仿真-获取ATS测试系统所有仿真实例的基本信息,请求") + simArr := [...]dto.SimulationInfoRepDto{} + i := 0 + for v := range simulation.SimulationMap { + simArr[i] = dto.SimulationInfoRepDto{ + SimulationId: v, + MapId: strings.Split(v, "_")[0], + } + } //TODO 后续调用 - c.JSON(http.StatusOK, dto.SimulationInfoRepDtoArr{dto.SimulationInfoRepDto{"1", "123"}, dto.SimulationInfoRepDto{"2", "333"}}) + c.JSON(http.StatusOK, simArr) } // ATS测试仿真地图数据校验 @@ -143,17 +130,62 @@ func findAllSimulations(c *gin.Context) { // @Failure 500 {object} dto.ErrorDto // @Router /api/v1/simulation/check/data [post] func checkSimMapData(c *gin.Context) { - rt := &dto.CheckMapDataRspDto{} - err := c.Bind(rt) + rt := &dto.CheckMapDataReqDto{} + if err := c.ShouldBind(&rt); nil != err { + panic(err) + } + err := proto.Unmarshal(rt.Data, &graphicData.RtssGraphicStorage{}) if err != nil { c.JSON(http.StatusInternalServerError, "参数错误") - } else { - //TODO 后续调用 - c.JSON(http.StatusOK, dto.CheckMapDataRspDto{true, []string{"error"}}) + c.JSON(http.StatusOK, &dto.CheckMapDataRspDto{Success: true}) } } +// ATS测试仿真-添加列车 +// +// @Summary ATS测试仿真-添加列车 +// +// @Security JwtAuth +// +// @Description ATS测试仿真-添加列车 +// @Tags ATS测试仿真Api +// @Accept json +// @Produce json +// @Param Authorization header string true "JWT Token" +// @Param AddTrainReqDto body dto.AddTrainReqDto true "ATS测试仿真-添加列车" +// @Success 200 {object} dto.AddTrainRspDto +// @Failure 500 {object} dto.ErrorDto +// @Router /api/v1/simulation/train/add [post] +func addTrain(c *gin.Context) { + //user,_:=c.Get(middleware.IdentityKey) + req := dto.AddTrainReqDto{} + if err := c.ShouldBind(&req); err != nil { + panic(err) + } + zap.S().Debug("ATS测试仿真-添加列车,请求:", req) + deviceMemory := checkDeviceDataAndReturn(req.SimulationId) + trainMap := deviceMemory.Status.TrainStateMap + // 获取列车ID + i := 1 + for { + if trainMap[strconv.Itoa(i)] == nil { + break + } else { + i++ + } + } + id := strconv.Itoa(i) + rsp := &state.TrainState{ + Id: id, + HeadLinkId: req.HeadLinkId, + HeadLinkOffset: req.HeadLinkOffset, + Up: req.Up, + } + trainMap[id] = rsp + c.JSON(http.StatusOK, &rsp) +} + // ATS测试仿真-移除列车 // // @Summary ATS测试仿真-移除列车 @@ -172,15 +204,14 @@ func checkSimMapData(c *gin.Context) { // @Router /api/v1/simulation/train/remove [post] func removeTrain(c *gin.Context) { rt := &dto.RemoveTrainDto{} - err := c.Bind(rt) - if err != nil { - c.JSON(http.StatusInternalServerError, "参数错误") - - } else { - //TODO 后续调用列车删除操作 - c.JSON(http.StatusOK, "ok") + if err := c.ShouldBind(&rt); err != nil { + panic(err) } - + zap.S().Debug("ATS测试仿真-移除列车,请求:", rt) + deviceMemory := checkDeviceDataAndReturn(rt.SimulationId) + delete(deviceMemory.Status.TrainStateMap, rt.TrainId) + //TODO 后续调用列车删除操作 + c.JSON(http.StatusOK, "ok") } // 获取ATS测试-操作道岔 @@ -200,13 +231,25 @@ func removeTrain(c *gin.Context) { // @Failure 500 {object} dto.ErrorDto // @Router /api/v1/simulation/switch/operation [post] func switchOperation(c *gin.Context) { - switchOper := &dto.SwitchOperationReqDto{} - err := c.Bind(switchOper) - - if err != nil { - c.JSON(http.StatusInternalServerError, "参数错误") - } else { - //TODO 后续调用道岔操作 - c.JSON(http.StatusOK, "ok") + req := &dto.SwitchOperationReqDto{} + if err := c.ShouldBind(&req); err != nil { + panic(err) } + deviceMemory := checkDeviceDataAndReturn(req.SimulationId) + switchInfo := deviceMemory.Status.SwitchStateMap[req.SwitchIndex] + if switchInfo == nil { + panic(&dto.ErrorDto{Code: dto.DataNotExist, Message: fmt.Sprintf("仿真[%s]中索引为[%s]的道岔不存在", req.SimulationId, req.SwitchIndex)}) + } + switchInfo.Normal = req.TurnNormal + switchInfo.Reverse = req.TurnReverse + c.JSON(http.StatusOK, "ok") +} + +// 获取仿真设备数据并返回 +func checkDeviceDataAndReturn(simId string) *wayside.VerifyMemory { + deviceMemory := simulation.FindSimulation(simId) + if deviceMemory == nil { + panic(&dto.ErrorDto{Code: dto.DataNotExist, Message: fmt.Sprintf("仿真[%s]不存在", simId)}) + } + return deviceMemory } diff --git a/dto/simulation.go b/dto/simulation.go index bdf7d9b..3b2b8eb 100644 --- a/dto/simulation.go +++ b/dto/simulation.go @@ -46,8 +46,8 @@ type RemoveTrainDto AddTrainRspDto type SwitchOperationReqDto struct { SimulationId string `form:"simulationId" json:"simulationId" binding:"required"` SwitchIndex string `form:"switchIndex" json:"switchIndex" binding:"required"` - TurnNormal bool `form:"turnNormal" json:"turnNormal" binding:"required"` - TurnReverse bool `form:"turnReverse" json:"turnReverse" binding:"required"` + TurnNormal bool `form:"turnNormal" json:"turnNormal"` + TurnReverse bool `form:"turnReverse" json:"turnReverse"` } ///////////////////////////////////////////////////////////////////////////////// diff --git a/grpcproto/simulation_server.go b/grpcproto/simulation_server.go index 6d8df86..fc28408 100644 --- a/grpcproto/simulation_server.go +++ b/grpcproto/simulation_server.go @@ -67,10 +67,11 @@ func generateTestState(v *wayside.VerifyMemory) *state.PushedDevicesStatus { if len(switchMap) == 0 { for i := 0; i < 9; i++ { switchArr[i] = &state.SwitchState{ - Id: strconv.Itoa(i), + Id: strconv.Itoa(i + 1), Normal: true, Reverse: false, } + v.Status.SwitchStateMap[strconv.Itoa(i+1)] = switchArr[i] } } else { i := 0 diff --git a/service/simulation.go b/service/simulation.go index 4c0dbdd..7a7e12c 100644 --- a/service/simulation.go +++ b/service/simulation.go @@ -5,6 +5,7 @@ import ( "joylink.club/bj-rtsts-server/ats/verify/protos/graphicData" ) +// 查询地图数据 func QueryRtssGraphicStorage(mapId int32) *graphicData.RtssGraphicStorage { publishdata, err := GetPublishedGiById(int(mapId)) if err != nil {