rts-sim-testing-service/api/simulation.go
walker c9eaef1d6f 添加业务错误结构
修改用户注册和启动测试仿真接口使用业务错误返回
调整一些接口返回error
2023-10-20 15:08:47 +08:00

412 lines
13 KiB
Go

package api
import (
"fmt"
"log/slog"
"net/http"
"sort"
"strconv"
jwt "github.com/appleboy/gin-jwt/v2"
"github.com/gin-gonic/gin"
"github.com/golang/protobuf/proto"
"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/memory"
"joylink.club/bj-rtsts-server/config"
"joylink.club/bj-rtsts-server/dto"
"joylink.club/bj-rtsts-server/dto/request_proto"
apiproto "joylink.club/bj-rtsts-server/grpcproto"
"joylink.club/bj-rtsts-server/middleware"
"joylink.club/bj-rtsts-server/service"
"joylink.club/bj-rtsts-server/sys_error"
)
func InitSimulationRouter(api *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
authed := api.Group("/v1/simulation").Use(authMiddleware.MiddlewareFunc(), middleware.PermissMiddleware)
authed.POST("/createByProject", createByProjectId)
authed.POST("/destroy/:id", destroy)
authed.GET("/list", findAllSimulations)
authed.POST("/check/data", checkSimMapData)
authed.POST("/train/add", addTrain)
authed.POST("/train/remove", removeTrain)
authed.POST("/switch/operation", switchOperation)
authed.GET("/getDataChannelName", getDataChannelName)
authed.POST("/relay/operation", relayOperation)
authed.POST("/signal/operation", signalOperation)
authed.POST("/esbBtn/operation", esbBtnOperation)
authed.POST("/ibp/operation", ibpBtnOperation)
// 初始化地图信息
initPublishMapInfo()
apiproto.InitClient()
apiproto.Register(&apiproto.SimulationServer{})
apiproto.Register(&apiproto.SimulationIBPServer{})
apiproto.Register(&apiproto.SimulationPSLServer{})
apiproto.Register(&apiproto.MemoryChangeServer{SimulationMap: make(map[string]*state.SimulationStatus)})
}
func initPublishMapInfo() {
mapArr, err := service.ListAllPublishedGi()
if err != nil {
panic("地图加载出错")
}
sort.SliceStable(mapArr, func(i, j int) bool { return mapArr[i].Type != 0 })
for _, v := range mapArr {
memory.PublishMapVerifyStructure(v)
}
}
// 创建ATS测试仿真通过项目ID
//
// @Summary 创建ATS测试仿真
//
// @Security JwtAuth
//
// @Description 创建ATS测试仿真通过项目ID
// @Tags ATS测试仿真Api
// @Accept json
// @Produce json
// @Param Authorization header string true "JWT Token"
// @Param SimulationCreateReqDto body dto.SimulationCreateReqDto true "创建仿真请求"
// @Success 200 {object} dto.SimulationCreateRspDto
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/simulation/createByProject [post]
func createByProjectId(c *gin.Context) {
req := dto.SimulationCreateReqDto{}
if err := c.ShouldBind(&req); nil != err {
// panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
panic(sys_error.New("测试启动失败,请求参数异常", err))
}
mapInfos := service.QueryProjectPublishedGi(req.ProjectId)
if len(mapInfos) == 0 {
// panic(dto.ErrorDto{Code: dto.DataNotExist, Message: "项目未关联地图"})
panic(sys_error.New("测试启动失败,项目未关联发布图"))
}
mapIds := make([]int32, len(mapInfos))
for i, mapInfo := range mapInfos {
mapIds[i] = mapInfo.ID
}
rsp := dto.SimulationCreateRspDto{ProjectId: req.ProjectId, MapId: mapIds[0], MapIds: mapIds}
simulationId, err := simulation.CreateSimulation(req.ProjectId, mapIds)
if err != nil {
panic(sys_error.New("测试启动失败", err))
}
rsp.SimulationId = simulationId
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 id path int true "仿真id"
// @Success 200 {object} string
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/simulation/destroy/{id} [post]
func destroy(c *gin.Context) {
simId := c.Param("id")
slog.Debug("ATS测试仿真-ATS仿真销毁 请求:", simId)
simulation.DestroySimulation(simId)
c.JSON(http.StatusOK, "ok")
}
// 获取ATS测试系统所有仿真实例的基本信息
//
// @Summary 获取ATS测试系统所有仿真实例的基本信息
//
// @Security JwtAuth
//
// @Description 获取ATS测试系统所有仿真实例的基本信息
// @Tags ATS测试仿真Api
// @Accept json
// @Produce json
// @Param Authorization header string true "JWT Token"
// @Success 200 {object} dto.SimulationInfoRspDtoArr
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/simulation/list [get]
func findAllSimulations(c *gin.Context) {
c.JSON(http.StatusOK, simulation.ListAllSimulations())
}
// ATS测试仿真地图数据校验
//
// @Summary ATS测试仿真地图数据校验
//
// @Security JwtAuth
//
// @Description 地图数据校验
// @Tags ATS测试仿真Api
// @Accept json
// @Produce json
// @Param Authorization header string true "JWT Token"
// @Param RemoveTrainDto body dto.CheckMapDataReqDto true "ATS测试仿真-地图数据"
//
// @Success 200 {object} dto.CheckMapDataRspDto
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/simulation/check/data [post]
func checkSimMapData(c *gin.Context) {
rt := &dto.CheckMapDataReqDto{}
if err := c.ShouldBind(&rt); nil != err {
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
}
err := proto.Unmarshal(rt.Data, &graphicData.RtssGraphicStorage{})
if err != nil {
c.JSON(http.StatusInternalServerError, "参数错误")
return
}
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) {
req := dto.AddTrainReqDto{}
if err := c.ShouldBind(&req); err != nil {
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
}
simulation := checkDeviceDataAndReturn(req.SimulationId)
id := getAddTrainPrimaryKey(simulation)
if id == -1 {
c.JSON(http.StatusBadRequest, "已存在在线列车")
return
}
rsp := &state.TrainState{
Id: strconv.Itoa(id),
HeadDeviceId: req.Id,
HeadOffset: req.HeadOffset,
DevicePort: req.DevicePort,
RunDirection: req.RunDirection,
TrainLength: req.TrainLength,
}
memory.AddTrainState(simulation, rsp, req.MapId)
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 RemoveTrainDto body dto.RemoveTrainDto true "ATS测试仿真-移除列车"
//
// @Success 200 {object} string
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/simulation/train/remove [post]
func removeTrain(c *gin.Context) {
rt := &dto.RemoveTrainDto{}
if err := c.ShouldBind(&rt); err != nil {
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
}
slog.Debug("ATS测试仿真-移除列车,请求:", rt)
simulation := checkDeviceDataAndReturn(rt.SimulationId)
memory.RemoveTrainState(simulation, rt.TrainId)
//TODO 后续调用列车删除操作
c.JSON(http.StatusOK, "ok")
}
// 获取ATS测试-操作道岔
//
// @Summary 获取ATS测试-操作道岔
//
// @Security JwtAuth
//
// @Description ATS测试-操作道岔
// @Tags ATS测试仿真Api
// @Accept json
// @Produce json
// @Param Authorization header string true "JWT Token"
// @Param TurnoutOperationReq body request_proto.TurnoutOperationReq true "ATS测试仿真-操作道岔"
//
// @Success 200 {object} string
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/simulation/switch/operation [post]
func switchOperation(c *gin.Context) {
req := &request_proto.TurnoutOperationReq{}
if err := c.ShouldBind(&req); err != nil {
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
}
simulation := checkDeviceDataAndReturn(req.SimulationId)
slog.Info("传入状态参数", "request", req)
memory.HandleTurnoutOperation(simulation, req)
c.JSON(http.StatusOK, "ok")
}
// ATS测试-信号机操作
//
// @Summary ATS测试-信号机操作
//
// @Security JwtAuth
//
// @Description ATS测试-信号机操作
// @Tags ATS测试仿真Api
// @Accept json
// @Produce json
// @Param Authorization header string true "JWT Token"
// @Param SignalOperationReqDto body dto.SignalOperationReqDto true "ATS测试仿真-操作信号机"
//
// @Success 200 {object} string
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/simulation/signal/operation [post]
func signalOperation(c *gin.Context) {
req := &dto.SignalOperationReqDto{}
if err := c.ShouldBind(&req); err != nil {
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
}
simulation := checkDeviceDataAndReturn(req.SimulationId)
slog.Info("传入状态参数", req)
memory.ChangeSignalState(simulation, req.MapId, req.DeviceId, req.Aspect)
c.JSON(http.StatusOK, "ok")
}
// ATS测试-ESB按钮操作
//
// @Summary ATS测试-ESB按钮操作
//
// @Security JwtAuth
//
// @Description ATS测试-ESB按钮操作
// @Tags ATS测试仿真Api
// @Accept json
// @Produce json
// @Param Authorization header string true "JWT Token"
// @Param ButtonOperationReqDto body dto.ButtonOperationReqDto true "ATS测试仿真-ESB按钮操作"
//
// @Success 200 {object} string
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/simulation/esbBtn/operation [post]
func esbBtnOperation(c *gin.Context) {
req := &dto.ButtonOperationReqDto{}
if err := c.ShouldBind(&req); err != nil {
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
}
simulation := checkDeviceDataAndReturn(req.SimulationId)
slog.Info("传入状态参数", req)
memory.ChangeEsbButtonState(simulation, req.MapId, req.Id, req.Down)
c.JSON(http.StatusOK, "ok")
}
// ATS测试-IBP按钮操作
//
// @Summary ATS测试-IBP按钮操作
//
// @Security JwtAuth
//
// @Description ATS测试-IBP按钮操作
// @Tags ATS测试仿真Api
// @Accept json
// @Produce json
// @Param Authorization header string true "JWT Token"
// @Param ButtonOperationReqDto body dto.ButtonOperationReqDto true "ATS测试仿真-IBP按钮操作"
//
// @Success 200 {object} string
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/simulation/ibp/operation [post]
func ibpBtnOperation(c *gin.Context) {
req := &dto.ButtonOperationReqDto{}
if err := c.ShouldBind(&req); err != nil {
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
}
simulation := checkDeviceDataAndReturn(req.SimulationId)
slog.Info("传入状态参数", req)
memory.ChangeIBPButtonState(simulation, req.MapId, req.Station, req.Id, req.Down)
c.JSON(http.StatusOK, "ok")
}
// 获取仿真信息更新通道名称
//
// @Summary 获取仿真信息更新通道名称
//
// @Security JwtAuth
//
// @Description 获取仿真信息更新通道名称
// @Tags ATS测试仿真Api
// @Accept json
// @Produce json
// @Param Authorization header string true "JWT Token"
//
// @Success 200 {object} string
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/simulation/getDataChannelName [get]
func getDataChannelName(c *gin.Context) {
c.JSON(http.StatusOK, config.SimulationId_prefix+apiproto.MemoryChangeServerSuffix)
}
// 获取ATS测试-操作继电器
//
// @Summary 获取ATS测试-操作继电器
//
// @Security JwtAuth
//
// @Description ATS测试-操作继电器
// @Tags ATS测试仿真Api
// @Accept json
// @Produce json
// @Param Authorization header string true "JWT Token"
// @Param RelayOperationReqDto body dto.RelayOperationReqDto true "ATS测试仿真-操作继电器"
//
// @Success 200 {object} string
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/simulation/relay/operation [post]
func relayOperation(c *gin.Context) {
req := &dto.RelayOperationReqDto{}
if err := c.ShouldBind(&req); err != nil {
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
}
simulation := checkDeviceDataAndReturn(req.SimulationId)
slog.Info("传入状态参数", req)
memory.ChangeRelayState(simulation, req.MapId, req.Id, req.Td)
c.JSON(http.StatusOK, "ok")
}
// 获取仿真设备数据并返回
func checkDeviceDataAndReturn(simId string) *memory.VerifySimulation {
deviceMemory := simulation.FindSimulation(simId)
if deviceMemory == nil {
panic(&dto.ErrorDto{Code: dto.DataNotExist, Message: fmt.Sprintf("仿真[%s]不存在", simId)})
}
return deviceMemory
}
// 获取列车主键
func getAddTrainPrimaryKey(simulation *memory.VerifySimulation) int {
trainMap := &simulation.Memory.Status.TrainStateMap
// 获取列车ID
i := 1
for {
_, ok := trainMap.Load(strconv.Itoa(i))
if !ok {
break
}
i++
}
return i
}