liuxiaolong
2019-11-12 4d0e5b1e9f59ebba7f875c9a39a6ab84c9dde4b6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package controllers
 
import (
    "basic.com/dbapi.git"
    uuid "github.com/satori/go.uuid"
    "webserver/extend/code"
    "webserver/extend/config"
    "basic.com/valib/logger.git"
    "webserver/extend/util"
    "github.com/gin-gonic/gin"
    "webserver/service"
)
 
type SdkController struct {
}
 
type SdkVo struct {
    Id         string `json:"id"`
    SdkType string `json:"sdk_type"`//人脸检测:FaceDetect,人脸提取:FaceExtract,人脸比对:FaceCompare,行为:Yolo
    SdkName string `json:"sdk_name"`    //算法名称
    Args    []SdkArgVo `json:"args"` //算法参数
    Icon    string `json:"icon"`       //算法图标
    Url     string `json:"url"`                       //算法下载地址
    CreateTime string `json:"create_time"`
    CreateBy string `json:"create_by"`
    UpdateTime string `json:"update_time"`
    Enable bool `json:"enable"`//是否启用
    DelFlag bool `json:"del_flag"`//逻辑删除
}
 
//算法参数定义
type SdkArgVo struct {
    Alias   string `json:"alias"`   //参数的别名
    Name  string `json:"name"`  //参数名称
    Type  string `json:"type"`  //参数类型(整数,字符串或数组)
    Must  bool   `json:"must"`  //是否必填
    Unit  string `json:"unit"`
    Range string `json:"range"` //值的范围,eg:0,100表示从0到100
    DefaultValue string `json:"default_value"`
    Sort  int    `json:"sort"`  //参数顺序
}
 
// @Security ApiKeyAuth
// @Summary 算法保存
// @Description 算法保存
// @Accept json
// @Produce json
// @Tags sdk
// @Param reqMap body controllers.SdkVo true "人脸检测"
// @Success 200 {string} json "{"code":200, msg:"请求处理成功",data:"",success:true}"
// @Failure 500 {string} json "{"code":500, msg:"请求失败",data:"",success:false}"
// @Router /data/api-v/sdk/save [POST]
func (sc SdkController) Save(c *gin.Context) {
    var sdk SdkVo
    var api dbapi.SdkApi
 
    if err := c.BindJSON(&sdk);err !=nil {
        util.ResponseFormat(c,code.RequestParamError,"参数错误")
        return
    }
    paramBody := util.Struct2Map(sdk)
    flag, data := api.Save(paramBody)
    if flag{
        util.ResponseFormat(c, code.Success, data)
    } else {
        util.ResponseFormat(c, code.ComError, data)
    }
}
 
// @Security ApiKeyAuth
// @Summary 查找所有算法
// @Description 查找所有算法
// @Produce json
// @Tags sdk
// @Param sdkName query string false "可选参数"
// @Success 200 {string} json "{"code":200, msg:"请求处理成功",data:"算法列表",success:true}"
// @Failure 500 {string} json "{"code":500, msg:"请求失败",data:"[]",success:false}"
// @Router /data/api-v/sdk/findAllSdk [GET]
func (sc SdkController) FindAllSdk(c *gin.Context) {
    var api dbapi.SdkApi
 
    sdkName := c.Query("sdkName")
    sdks := api.FindAll(sdkName)
    util.ResponseFormat(c, code.Success, sdks)
}
 
// @Security ApiKeyAuth
// @Summary 根据id获取算法信息
// @Description 根据id获取算法信息
// @Produce json
// @Tags sdk
// @Param id query string true "算法id,必填"
// @Success 200 {string} json "{"code":200, msg:"请求处理成功",data:"算法信息",success:true}"
// @Failure 500 {string} json "{"code":500, msg:"请求失败",data:"",success:false}"
// @Router /data/api-v/sdk/getById [GET]
func (sc SdkController) GetById(c *gin.Context) {
    var api dbapi.SdkApi
    sdkId := c.Query("id")
    if sdkId == ""{
        util.ResponseFormat(c,code.RequestParamError,"sdkId不能为空")
        return
    }
 
    flag, sdk := api.GetById(sdkId)
 
    if flag {
        util.ResponseFormat(c,code.Success,sdk)
    } else {
        util.ResponseFormat(c,code.ComError,sdk)
    }
}
 
// @Router /data/api-v/sdkArg/getSdkArgs [get]
func (sc SdkController) GetSdkArgs(c *gin.Context) {
    sdkId := c.Query("sdkId")
    scope := c.Query("scope")
    if sdkId== "" || scope == ""{
        util.ResponseFormat(c,code.RequestParamError,"参数有误")
        return
    }
    var api dbapi.SdkApi
    b,d := api.GetSdkArgs(sdkId, scope)
    if b{
        util.ResponseFormat(c,code.Success,d)
    } else {
        util.ResponseFormat(c,code.ComError,"查询失败")
    }
}
 
// @Security ApiKeyAuth
// @Summary 根据taskId获取算法信息
// @Description 根据taskId获取算法信息
// @Produce json
// @Tags sdk
// @Param taskId query string true "taskId,必填"
// @Success 200 {string} json "{"code":200, msg:"请求处理成功",data:"算法信息",success:true}"
// @Failure 500 {string} json "{"code":500, msg:"请求失败",data:"",success:false}"
// @Router /data/api-v/sdk/findByTaskId [GET]
func (sc SdkController) FindByTaskId(c *gin.Context) {
    var api dbapi.SdkApi
    taskId := c.Query("taskId")
    if taskId == "" {
        util.ResponseFormat(c,code.ComError,"任务id不能为空")
        return
    }
    flag,sdks := api.FindByTaskId(taskId)
    if flag {
        util.ResponseFormat(c,code.Success,sdks)
    } else {
        util.ResponseFormat(c,code.ComError,sdks)
    }
}
 
func (sc SdkController) SdkDownLoad(c *gin.Context) {
    path,exist := c.GetQuery("path")
    if !exist {
        util.ResponseFormat(c,code.ComError,"下载的算法参数有误")
    }
    logger.Info(path)
    flag := c.Query("needUpdateMiddle")
    // 下载算法(有时候也需要把中间件一起下载下来)
    if flag == "true" {
        // 下载并更新中间件,带上MD5校验
        flag1,err := service.DownSo("http://"+config.SoPath.Ip+":"+config.SoPath.Port+"/"+"middleware.so")
        logger.Debug("中间件路径:","http://"+config.SoPath.Ip+":"+config.SoPath.Port+"/"+"middleware.so")
        if err != nil {
            logger.Info(err)
        }
        if !flag1 {
            util.ResponseFormat(c,code.ComError,"请重新下载算法")
        }
    }
    // 下载算法,校验,并写入到目标目录下
    flag2,err2 := service.DownSo(path)
    if err2 != nil {
        logger.Info(err2)
    }
    if !flag2 {
        util.ResponseFormat(c,code.ComError,"请重新下载算法")
    }
    // 将算法和so名称存到规则私有的注册表
    var soApi dbapi.SoApi
    param := make(map[string]interface{})
    param["id"] = uuid.NewV4().String()
    sdkId := uuid.NewV4().String()
    param["sdkId"] = sdkId
    param["soName"] = service.GetFileNameFromUrl(path,true)
    flag3,_ := soApi.Add(param)
    if flag3 {
        util.ResponseFormat(c,code.Success,"下载算法成功!")
    }
}