rts-sim-testing-service/api/projectRunConfig.go
2023-12-20 14:13:23 +08:00

282 lines
9.2 KiB
Go

package api
import (
"net/http"
"reflect"
"strconv"
jwt "github.com/appleboy/gin-jwt/v2"
"github.com/gin-gonic/gin"
"joylink.club/bj-rtsts-server/config"
"joylink.club/bj-rtsts-server/dto"
"joylink.club/bj-rtsts-server/middleware"
"joylink.club/bj-rtsts-server/service"
"joylink.club/bj-rtsts-server/sys_error"
iotdto "joylink.club/iot/dto"
)
func InitProjectRunConfigRouter(api *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
authed := api.Group("/v1/runconfig").Use(authMiddleware.MiddlewareFunc(), middleware.PermissMiddleware)
authed.GET("/paging", pageQueryProjectRunConfig)
authed.GET("/list", listQueryProjectRunConfig)
authed.POST("", createProjectRunConfig)
authed.GET("/:id", queryProjectRunConfig)
authed.PUT("/:id", updateProjectRunConfig)
authed.DELETE("/:id", deleteProjectRunConfig)
authed.GET("/description", getRunCofigDescription)
}
// 分页查询项目运行环境配置信息
//
// @Summary 分页查询项目运行环境配置信息
//
// @Security JwtAuth
//
// @Description 可以通过项目名称过滤,分页查询项目运行环境配置信息
// @Tags 项目运行环境配置Api
// @Accept json
// @Produce json
// @Param PageProjectRunConfigReqDto query dto.PageProjectRunConfigReqDto true "运行环境配置查询条件带分页信息"
// @Success 200 {object} dto.PageDto
// @Failure 401 {object} dto.ErrorDto
// @Failure 404 {object} dto.ErrorDto
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/runconfig/paging [get]
func pageQueryProjectRunConfig(c *gin.Context) {
req := dto.PageProjectRunConfigReqDto{}
if err := c.ShouldBind(&req); err != nil {
panic(sys_error.New("查询失败,参数格式错误", err))
}
c.JSON(http.StatusOK, service.PageProjectRunConfigQuery(&req))
}
// 查询项目运行环境配置信息列表
//
// @Summary 查询项目运行环境配置信息列表
//
// @Security JwtAuth
//
// @Description 无参数,查询项目运行环境配置列表
// @Tags 项目运行环境配置Api
// @Accept json
// @Produce json
// @Success 200 {object} dto.ProjectRunConfigDto
// @Failure 401 {object} dto.ErrorDto
// @Failure 404 {object} dto.ErrorDto
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/runconfig/list [get]
func listQueryProjectRunConfig(c *gin.Context) {
c.JSON(http.StatusOK, service.ListProjectRunConfigQuery())
}
// 创建项目运行环境配置信息
//
// @Summary 创建项目运行环境配置信息
//
// @Security JwtAuth
//
// @Description 创建项目运行环境配置数据
// @Tags 项目运行环境配置Api
// @Accept json
// @Produce json
// @Param ProjectRunConfigReqDto query dto.ProjectRunConfigReqDto true "创建的项目运行环境配置信息"
// @Success 200 {object} nil
// @Failure 401 {object} dto.ErrorDto
// @Failure 404 {object} dto.ErrorDto
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/runconfig [post]
func createProjectRunConfig(c *gin.Context) {
req := dto.ProjectRunConfigReqDto{}
if err := c.ShouldBind(&req); err != nil {
panic(sys_error.New("创建失败,参数格式错误", err))
}
c.JSON(http.StatusOK, service.CreateProjectRunConfig(&req))
}
// 查询项目运行环境信息
//
// @Summary 查询项目运行环境信息
//
// @Security JwtAuth
//
// @Description 查询项目运行环境信息
// @Tags 项目运行环境配置Api
// @Accept json
// @Produce json
// @Param id path int true "项目运行环境ID"
// @Success 200 {object} dto.ProjectRunConfigDto
// @Failure 401 {object} dto.ErrorDto
// @Failure 404 {object} dto.ErrorDto
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/runconfig/{id} [get]
func queryProjectRunConfig(c *gin.Context) {
id, exist := c.Params.Get("id")
if !exist {
panic(sys_error.New("查询失败,缺少主键"))
}
intId, _ := strconv.Atoi(id)
c.JSON(http.StatusOK, service.QueryProjectRunConfig(int32(intId)))
}
// 修改项目运行环境信息
//
// @Summary 修改项目运行环境信息
//
// @Security JwtAuth
//
// @Description 修改项目运行环境信息
// @Tags 项目运行环境配置Api
// @Accept json
// @Produce json
// @Param id path int true "项目运行环境信息ID"
// @Param ProjectDto query dto.ProjectDto true "修改的项目信息"
// @Success 200 {object} nil
// @Failure 401 {object} dto.ErrorDto
// @Failure 404 {object} dto.ErrorDto
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/runconfig/{id} [put]
func updateProjectRunConfig(c *gin.Context) {
id, exist := c.Params.Get("id")
if !exist {
panic(sys_error.New("更新失败,缺少主键"))
}
req := dto.ProjectRunConfigReqDto{}
if err := c.ShouldBind(&req); err != nil {
panic(sys_error.New("更新失败,参数格式错误", err))
}
intId, _ := strconv.Atoi(id)
c.JSON(http.StatusOK, service.UpdateProjectRunConfig(int32(intId), &req))
}
// 删除项目运行环境信息
//
// @Summary 删除项目运行环境信息
//
// @Security JwtAuth
//
// @Description 删除项目运行环境信息
// @Tags 项目运行环境配置Api
// @Accept json
// @Produce json
// @Param id path int true "项目运行环境信息ID"
// @Success 200 {object} nil
// @Failure 401 {object} dto.ErrorDto
// @Failure 404 {object} dto.ErrorDto
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/runconfig/{id} [delete]
func deleteProjectRunConfig(c *gin.Context) {
idStr := c.Param("id")
id, err := strconv.Atoi(idStr)
if err != nil {
panic(sys_error.New("删除失败,缺少主键"))
}
service.DeleteProjectRunConfigById(int32(id))
c.JSON(http.StatusOK, true)
}
// 获取项目运行环境信息结构说明
//
// @Summary 获取项目运行环境信息结构说明
//
// @Security JwtAuth
//
// @Description 获取项目运行环境信息结构说明
// @Tags 项目运行环境配置Api
// @Accept json
// @Produce json
// @Success 200 {object} nil
// @Failure 401 {object} dto.ErrorDto
// @Failure 404 {object} dto.ErrorDto
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/runconfig/description [get]
func getRunCofigDescription(c *gin.Context) {
c.JSON(http.StatusOK, parseRunCofigStruct(&config.ThridPartyConfig{}))
}
// 解析环境配置结构
func parseRunCofigStruct(m interface{}) []*dto.RunConfigDescription {
var cs []*dto.RunConfigDescription
t := reflect.TypeOf(m).Elem()
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if field.Tag.Get("description") == "" {
continue
}
c := &dto.RunConfigDescription{
FieldName: field.Tag.Get("json"),
Description: field.Tag.Get("description"),
DefaultValue: field.Tag.Get("default"),
}
k := field.Type.Kind()
switch k {
case reflect.Struct:
c.ItemTypeFields = parseRunCofigStruct(reflect.New(field.Type).Interface())
c.Type = "map"
case reflect.Slice:
e := field.Type.Elem()
c.ItemTypeFields = parseRunCofigStruct(reflect.New(e).Interface())
c.Type = "array"
default:
c.Type = k.String()
}
// slog.Warn("运行配置字段类型", "fieldType", field.Type, "fieldTypeName", field.Type.Name(), "fieldTypeString", field.Type.String())
switch field.Type.String() {
case "proto.Modbus_Endianness":
c.SelectOptions = modbus_EndiannessSelectOptions
case "proto.Modbus_Function":
c.SelectOptions = modbus_FunctionSelectOptions
// case "proto.Modbus_WriteStrategy":
// c.SelectOptions = modbus_WriteStrategySelectOptions
case "proto.DataType":
c.SelectOptions = dataTypeSelectOptions
default:
}
cs = append(cs, c)
}
return cs
}
var modbus_EndiannessSelectOptions = buildModbus_EndiannessSelectOptions()
func buildModbus_EndiannessSelectOptions() []*dto.RunConfigSelectOption {
return []*dto.RunConfigSelectOption{
{Label: "大端", Value: int32(iotdto.Modbus_BigEndian)},
{Label: "小端", Value: int32(iotdto.Modbus_LittleEndian)},
}
}
var modbus_FunctionSelectOptions = buildModbus_FunctionSelectOptions()
func buildModbus_FunctionSelectOptions() []*dto.RunConfigSelectOption {
return []*dto.RunConfigSelectOption{
{Label: "读线圈", Value: int32(iotdto.Modbus_ReadCoil)},
{Label: "读离散输入", Value: int32(iotdto.Modbus_ReadDiscreteInput)},
{Label: "读保持寄存器", Value: int32(iotdto.Modbus_ReadHoldingRegister)},
{Label: "读输入寄存器", Value: int32(iotdto.Modbus_ReadInputRegister)},
{Label: "写单个线圈", Value: int32(iotdto.Modbus_WriteCoil)},
{Label: "写多个线圈", Value: int32(iotdto.Modbus_WriteCoils)},
{Label: "写单个寄存器", Value: int32(iotdto.Modbus_WriteRegister)},
{Label: "写多个寄存器", Value: int32(iotdto.Modbus_WriteRegisters)},
{Label: "读写多个线圈", Value: int32(iotdto.Modbus_RWCoils)},
{Label: "读写多个寄存器", Value: int32(iotdto.Modbus_RWRegisters)},
}
}
// var modbus_WriteStrategySelectOptions = buildModbus_WriteStrategySelectOptions()
// func buildModbus_WriteStrategySelectOptions() []*dto.RunConfigSelectOption {
// return []*dto.RunConfigSelectOption{
// {Label: "数据更新时写", Value: int32(iotdto.Modbus_OnUpdate)},
// {Label: "定时写", Value: int32(iotdto.Modbus_OnScheduled)},
// }
// }
var dataTypeSelectOptions = buildDataTypeSelectOptions()
func buildDataTypeSelectOptions() []*dto.RunConfigSelectOption {
return []*dto.RunConfigSelectOption{
{Label: "采集数据", Value: int32(iotdto.DataType_CJ)},
{Label: "驱动数据", Value: int32(iotdto.DataType_QD)},
}
}