zhangzengfei
2020-05-08 a9565c42d241ec6ab2bfcaef9fe97aac943a07a0
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package controllers
 
import (
    "basic.com/dbapi.git"
    "basic.com/pubsub/esutil.git"
    "basic.com/pubsub/protomsg.git"
    "encoding/json"
    "strconv"
    "time"
    "webserver/cache"
    "webserver/extend/config"
 
    "github.com/gin-gonic/gin"
    "webserver/extend/code"
    "webserver/extend/util"
)
 
type TaskController struct{}
 
type TaskVo struct {
    Taskid   string `json:"taskid"`
    Taskname string `json:"taskname" example:"任务一"`
    CreateAt time.Time `json:"create_at"`
    Createby string `json:"create_by"`
    UpdateAt time.Time `json:"update_at"`
    Enable   bool `json:"enable"`
    IsAlarm  bool `json:"is_alarm"`
    DelFlag  bool `json:"del_flag"`
}
 
type TaskSdkVo struct {
    Task TaskVo `json:"task"`
    Sdks []SdkVo `json:"sdks"`
}
 
var (
    FaceExtract_VirtualTaskId = "92496BDF-2BFA-98F2-62E8-96DD9866ABD2"//虚拟任务id
    FaceExtract_VirtualSdkId = "virtual-faceextract-sdk-pull"//结果输出的算法id
)
 
// @Security ApiKeyAuth
// @Summary 查找所有任务,包含任务信息和对应的算法信息
// @Description 查找所有任务
// @Produce json
// @Tags task
// @Success 200 {string} json "{"code":200, msg:"请求处理成功",data:"",success:true}"
// @Failure 500 {string} json "{"code":500, msg:"请求失败",data:"",success:false}"
// @Router /data/api-v/task/findAll [GET]
func (tc TaskController) FindAll(c *gin.Context) {
    // 显示所有任务: 获取信息
    var taskApi dbapi.TaskApi
 
    taskInfos := taskApi.FindAll()
    var arr []protomsg.TaskSdkInfo
    for _,ti :=range taskInfos{
        if ti.Task.Taskid != FaceExtract_VirtualTaskId{
            arr = append(arr,ti)
        }
    }
    var tasks []TaskSdkVo
    dataBytes, err := json.Marshal(arr)
    if err !=nil {
        util.ResponseFormat(c,code.ComError,[]TaskSdkVo{})
    } else {
        if err := json.Unmarshal(dataBytes, &tasks);err !=nil {
            util.ResponseFormat(c,code.ComError,[]TaskSdkVo{})
        } else {
            util.ResponseFormat(c,code.Success,tasks)
        }
    }
}
 
// @Security ApiKeyAuth
// @Summary 检索页面获取所有任务列表(包含已删除的任务)
// @Description 检索页面获取所有任务列表(包含已删除的任务)
// @Produce json
// @Tags task
// @Success 200 {string} json "{"code":200, msg:"请求处理成功",data:"",success:true}"
// @Failure 500 {string} json "{"code":500, msg:"请求失败",data:"",success:false}"
// @Router /data/api-v/task/aggregateTaskList [GET]
func (tc TaskController) AggregateTaskList(c *gin.Context) {
    // 显示所有任务: 获取信息
    var taskApi dbapi.TaskApi
 
    taskInfos := taskApi.FindAll()
    m := make(map[string]string)
    var arr []protomsg.TaskSdkInfo
    for _,ti :=range taskInfos{
        if ti.Task.Taskid != FaceExtract_VirtualTaskId{
            arr = append(arr,ti)
            m[ti.Task.Taskid] = ti.Task.Taskid
        }
    }
    var tasks []TaskSdkVo
    dataBytes, err := json.Marshal(arr)
    if err !=nil {
        util.ResponseFormat(c,code.ComError,[]TaskSdkVo{})
    } else {
        if err := json.Unmarshal(dataBytes, &tasks);err !=nil {
            util.ResponseFormat(c,code.ComError,[]TaskSdkVo{})
        } else {
            //处理已被删除的任务
            localConf, _ := cache.GetServerInfo()
            if localConf.AlarmIp != "" && localConf.ServerId != "" && localConf.AlarmPort>0 {
                indexName := config.EsInfo.EsIndex.AiOcean.IndexName
                esTaskM, e := esutil.AggregateTaskList(localConf.AlarmIp, strconv.Itoa(int(localConf.AlarmPort)), indexName, localConf.ServerId)
                if e==nil && esTaskM !=nil {
                    for _,tM :=range esTaskM {
                        if _,ok := m[tM["taskId"].(string)];!ok {//表示此任务已被删除
                            taskId := tM["taskId"].(string)
                            m[taskId] = taskId
                            tasks = append(tasks, TaskSdkVo{
                                Task:TaskVo{
                                    Taskid:    taskId,
                                    Taskname: tM["taskName"].(string),
                                    DelFlag: true,
                                },
                                Sdks:[]SdkVo{},
                            })
                        }
                    }
                }
            }
 
            util.ResponseFormat(c,code.Success,tasks)
        }
    }
}
 
// @Security ApiKeyAuth
// @Summary 添加任务
// @Description 查找所有任务
// @Accept json
// @Produce json
// @Tags task
// @Param reqMap body controllers.TaskVo false "任务AAA"
// @Success 200 {string} json "{"code":200, msg:"请求处理成功",data:"",success:true}"
// @Failure 500 {string} json "{"code":500, msg:"请求失败",data:"",success:false}"
// @Router /data/api-v/task/addTask [POST]
func (tc TaskController) AddTask(c *gin.Context) {
    var task TaskVo
    var taskApi dbapi.TaskApi
    err := c.BindJSON(&task)
    if err !=nil {
        util.ResponseFormat(c,code.RequestParamError,"参数有误")
        return
    }
    paramBody := util.Struct2Map(task)
    flag, data := taskApi.Add(paramBody)
    if !flag {
        util.ResponseFormat(c, code.ComError, data)
        return
    }
 
    util.ResponseFormat(c, code.Success, data)
}
 
type TaskSdkAdd struct {
    TaskId string `json:"taskId"`
    Sdks []SdkSort `json:"sdks"`
}
type SdkSort struct {
    SdkId string `json:"sdkId"`
    Sort int `json:"sort"`
}
 
// @Security ApiKeyAuth
// @Summary 给任务添加算法
// @Description 任务添加算法
// @Accept json
// @Produce json
// @Tags task
// @Param taskSdkAdd body controllers.TaskSdkAdd 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/task/addTaskSdk [post]
func (tc TaskController) AddTaskSdk(c *gin.Context) {
    var addVo TaskSdkAdd
    err := c.BindJSON(&addVo)
    if err !=nil || addVo.TaskId == "" || len(addVo.Sdks)==0 {
        util.ResponseFormat(c,code.RequestParamError,"参数有误")
        return
    }
    paramBody := util.Struct2Map(addVo)
    var taskApi dbapi.TaskApi
    flag,data := taskApi.AddTaskSdk(paramBody)
    if flag {
        util.ResponseFormat(c, code.Success, data)
    } else {
        util.ResponseFormat(c,code.ComError,data)
    }
}
 
// @Security ApiKeyAuth
// @Summary 任务删除算法
// @Description 根据taskid和sdkid删除
// @Produce json
// @Tags task
// @Param taskId query string true "任务id"
// @Param sdkId 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/task/delTaskSdk [GET]
func (tc TaskController) DeleteTaskSdk(c *gin.Context) {
    var taskApi dbapi.TaskApi
    taskId := c.Query("taskId")
    sdkId := c.Query("sdkId")
    if taskId == "" || sdkId == "" {
        util.ResponseFormat(c,code.RequestParamError,"参数有误")
        return
    }
    flag,data := taskApi.DeleteTaskSdk(taskId,sdkId)
    if flag {
        util.ResponseFormat(c, code.Success, data)
    } else {
        util.ResponseFormat(c,code.ComError, data)
    }
}
 
// @Security ApiKeyAuth
// @Summary 更新任务名称
// @Description 更新任务名称
// @Accept x-www-form-urlencoded
// @Produce json
// @Tags task
// @Param taskId formData string true "taskId"
// @Param taskName formData string true "taskName"
// @Success 200 {string} json "{"code":200, msg:"",data:"",success:true}"
// @Failure 500 {string} json "{"code":500, msg:"",data:"",success:false}"
// @Router /data/api-v/task/updateTaskName [POST]
func (tc TaskController) UpdateTaskName(c *gin.Context) {
    taskId := c.PostForm("taskId")
    taskName := c.PostForm("taskName")
    if taskId == "" || taskName == "" {
        util.ResponseFormat(c,code.RequestParamError,"参数有误")
        return
    }
    var api dbapi.TaskApi
    if api.UpdateTaskName(taskId, taskName) {
        util.ResponseFormat(c,code.UpdateSuccess,"更新成功")
    } else {
        util.ResponseFormat(c,code.ComError,"更新失败")
    }
 
}
 
type TaskStatusVo struct {
    TaskId string `json:"taskId"`
    Enable bool `json:"enable"`
}
 
// @Security ApiKeyAuth
// @Summary 更新任务状态
// @Description (算法不变,只更新任务状态)
// @Accept json
// @Produce json
// @Tags task
// @Param taskStatus body controllers.TaskStatusVo 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/task/updateTaskStatus [POST]
func (tc TaskController) UpdateTaskStatus(c *gin.Context) {
    var tsvo TaskStatusVo
    err := c.BindJSON(&tsvo)
    if err !=nil {
        util.ResponseFormat(c,code.RequestParamError,"参数有误")
        return
    }
    var taskApi dbapi.TaskApi
    paramBody := util.Struct2Map(tsvo)
    flag, data := taskApi.UpdateTaskStatus(paramBody)
    if flag {
        util.ResponseFormat(c,code.Success,data)
    } else {
        util.ResponseFormat(c,code.ComError,data)
    }
}
 
// @Security ApiKeyAuth
// @Summary 删除任务
// @Description 根据任务id删除任务
// @Produce json
// @Tags task
// @Param taskId 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/task/delete [GET]
func (tc TaskController) DeleteTask(c *gin.Context) {
    var taskApi dbapi.TaskApi
    taskId := c.Query("taskId")
    if taskId == "" {
        util.ResponseFormat(c,code.RequestParamError,"参数有误")
        return
    }
    flag, data := taskApi.Delete(taskId)
    if flag {
        util.ResponseFormat(c,code.Success,data)
    } else {
        util.ResponseFormat(c,code.ComError,data)
    }
 
}
 
type TaskSdkRules struct {
    TaskId string `json:"taskId"`
    SdkId string  `json:"sdkId"`
    Rules []TaskSdkRuleVo `json:"rules"`
}
 
type TaskSdkRuleVo struct {
    Id string `json:"id"`
    SdkArgAlias string `json:"sdk_arg_alias"`
    Operator string `json:"operator"`
    SdkArgValue string `json:"sdk_arg_value"`
}
 
// @Security ApiKeyAuth
// @Summary 保存算法规则
// @Description 保存算法规则
// @Accept json
// @Produce json
// @Tags 算法规则
// @Param sdkrules body controllers.TaskSdkRules true "算法参数规则"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/task/saveTaskSdkRule [post]
func (tc TaskController) SaveTaskSdkRule(c *gin.Context) {
    var rules TaskSdkRules
    err := c.BindJSON(&rules)
    if err !=nil || rules.TaskId == "" || rules.SdkId == "" || len(rules.Rules) == 0 {
        util.ResponseFormat(c,code.RequestParamError,"参数有误")
        return
    }
    paramBody := util.Struct2Map(rules)
    var api dbapi.TaskSdkRuleApi
    b,d := api.SaveTaskSdkRule(paramBody)
    if b {
        util.ResponseFormat(c,code.Success,d)
    } else {
        util.ResponseFormat(c,code.ComError,"保存失败")
    }
}
 
// @Security ApiKeyAuth
// @Summary 删除算法规则
// @Description 删除算法规则
// @Accept x-www-form-urlencoded
// @Produce json
// @Tags 算法规则
// @Param taskId formData string true "taskId"
// @Param sdkId formData string true "sdkId"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/task/deleteTaskSdkRule [post]
func (tc TaskController) DeleteTaskSdkRule(c *gin.Context) {
    taskId := c.PostForm("taskId")
    sdkId := c.PostForm("sdkId")
    if taskId == "" || sdkId == "" {
        util.ResponseFormat(c,code.RequestParamError,"参数有误")
        return
    }
    var api dbapi.TaskSdkRuleApi
    if api.DeleteTaskSdkRule(taskId, sdkId) {
        util.ResponseFormat(c,code.Success,"删除成功")
    } else {
        util.ResponseFormat(c,code.ComError, "删除失败")
    }
}
 
// @Security ApiKeyAuth
// @Summary 查询算法规则
// @Description 查询算法规则
// @Produce json
// @Tags 算法规则
// @Param taskId query string true "任务id"
// @Param sdkId query string true "算法id"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/task/getRulesByTaskSdk [get]
func (tc TaskController) GetRulesByTaskSdk(c *gin.Context) {
    taskId := c.Query("taskId")
    sdkId := c.Query("sdkId")
    if taskId == "" || sdkId == ""{
        util.ResponseFormat(c,code.RequestParamError,"参数有误")
        return
    }
    var taskSdkApi dbapi.TaskSdkRuleApi
    b, d := taskSdkApi.GetRulesByTaskSdk(taskId, sdkId)
    if b {
        util.ResponseFormat(c,code.Success,d)
    } else {
        util.ResponseFormat(c,code.ComError,"查询失败")
    }
}
 
// @Security ApiKeyAuth
// @Summary 统计每个任务的摄像机数量
// @Description 统计每个任务的摄像机数量
// @Produce json
// @Tags task
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/task/statisticTaskCamera [get]
func (tc TaskController) StatisticTaskCamera(c *gin.Context) {
    var taskApi dbapi.TaskApi
    b,d := taskApi.StatisticTaskCamera()
    if b {
        util.ResponseFormat(c,code.Success, d)
    } else {
        util.ResponseFormat(c,code.ComError, "")
    }
}