ccamera.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. package controllers
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/astaxie/beego"
  10. validation "github.com/go-ozzo/ozzo-validation/v4"
  11. "lc/common/models"
  12. "lc/common/mqtt"
  13. "lc/common/protocol"
  14. "lc/common/util"
  15. )
  16. type CameraController struct {
  17. BaseController
  18. }
  19. type ReqCamera struct {
  20. Code string `json:"code"` //设备编号,禁止修改
  21. GID string `json:"gid"` //网关ID
  22. Tenant string `json:"tenant"` //租户ID
  23. Name string `json:"name"` //设备名称
  24. Brand int `json:"brand"` //品牌
  25. Model int `json:"model"` //型号
  26. DevType int `json:"devtype"` //设备类型
  27. State int `json:"state"` //1启用,0禁用
  28. }
  29. func (a ReqCamera) Validate() error {
  30. return validation.ValidateStruct(&a,
  31. validation.Field(&a.Code, validation.Required, validation.Length(4, 32)),
  32. validation.Field(&a.GID, validation.Required, validation.Length(4, 32)),
  33. validation.Field(&a.Tenant, validation.Required, validation.Length(4, 8)),
  34. validation.Field(&a.State, validation.In(0, 1)),
  35. )
  36. }
  37. type Camera struct {
  38. Code string `json:"code"` //设备编号,禁止修改
  39. GID string `json:"gid"` //网关ID
  40. Name string `json:"name"` //设备名称
  41. Brand int `json:"brand"` //品牌
  42. Model int `json:"model"` //型号
  43. DevType int `json:"devtype"` //设备类型
  44. State int `json:"state"` //1启用,0禁用
  45. }
  46. func (a Camera) Validate() error {
  47. return validation.ValidateStruct(&a,
  48. validation.Field(&a.Code, validation.Required, validation.Length(4, 32)),
  49. validation.Field(&a.GID, validation.Required, validation.Length(4, 32)),
  50. validation.Field(&a.State, validation.In(0, 1)),
  51. )
  52. }
  53. type ReqImportCamera struct {
  54. Tenant string `json:"tenant"` //租户ID
  55. List []Camera `json:"list"` //列表
  56. }
  57. func (a ReqImportCamera) Validate() error {
  58. var rules []*validation.FieldRules
  59. rules = append(rules, validation.Field(&a.Tenant, validation.Required, validation.Length(4, 8)))
  60. rules = append(rules, validation.Field(&a.List, validation.Required))
  61. return validation.ValidateStruct(&a, rules...)
  62. }
  63. type RespSnapshotUrl struct {
  64. Code string `json:"code"`
  65. Date string `json:"date"`
  66. URLs []string `json:"urls"`
  67. }
  68. type PresetInfo struct {
  69. Token string `json:"token"`
  70. Name string `json:"name"`
  71. File string `json:"file"`
  72. }
  73. type RespPresetInfo struct {
  74. Code string `json:"code"`
  75. Data []PresetInfo `json:"data"`
  76. }
  77. // CreateCamera @Title 创建摄像头或一键告警设备
  78. // @Description 创建摄像头或一键告警设备
  79. // @Param body controllers.ReqCamera true "数据"
  80. // @Success 0 {int} BaseResponse.Code "成功"
  81. // @Failure 1 {int} BaseResponse.Code "失败"
  82. // @router /v1/create [post]
  83. func (o *CameraController) CreateCamera() {
  84. var obj ReqCamera
  85. if err := json.Unmarshal(o.Ctx.Input.RequestBody, &obj); err != nil {
  86. beego.Debug(string(o.Ctx.Input.RequestBody))
  87. o.Response(Failure, fmt.Sprintf("数据包解析错误:%s", err.Error()), nil)
  88. return
  89. }
  90. if err := obj.Validate(); err != nil {
  91. beego.Error(fmt.Sprintf("请求参数验证失败:%s", err.Error()))
  92. o.Response(Failure, fmt.Sprintf("请求参数验证失败:%s", err.Error()), nil)
  93. return
  94. }
  95. oo := models.CameraDevice{
  96. ID: obj.Code, //设备ID,DID
  97. Name: obj.Name, //集控器名称
  98. GID: obj.GID, //网关ID
  99. DevType: obj.DevType,
  100. Tenant: obj.Tenant,
  101. State: obj.State, //1启用,0禁用
  102. }
  103. if err := oo.SaveFromWeb(); err != nil {
  104. o.Response(Failure, fmt.Sprintf("数据插入失败:%s", err.Error()), nil)
  105. return
  106. }
  107. o.Response(Success, "成功", oo.ID)
  108. }
  109. // UpdateCamera @Title 更新摄像头或一键告警设备
  110. // @Description 更新摄像头或一键告警设备
  111. // @Param body controllers.ReqCamera true "数据"
  112. // @Success 0 {int} BaseResponse.Code "成功"
  113. // @Failure 1 {int} BaseResponse.Code "失败"
  114. // @router /v1/update [post]
  115. func (o *CameraController) UpdateCamera() {
  116. var obj ReqCamera
  117. if err := json.Unmarshal(o.Ctx.Input.RequestBody, &obj); err != nil {
  118. beego.Debug(string(o.Ctx.Input.RequestBody))
  119. o.Response(Failure, fmt.Sprintf("数据包解析错误:%s", err.Error()), nil)
  120. return
  121. }
  122. if err := obj.Validate(); err != nil {
  123. beego.Error(fmt.Sprintf("请求参数验证失败:%s", err.Error()))
  124. o.Response(Failure, fmt.Sprintf("请求参数验证失败:%s", err.Error()), nil)
  125. return
  126. }
  127. oo := models.CameraDevice{
  128. ID: obj.Code, //设备ID,DID
  129. Name: obj.Name, //集控器名称
  130. GID: obj.GID, //网关ID
  131. DevType: obj.DevType,
  132. Tenant: obj.Tenant,
  133. State: obj.State, //1启用,0禁用
  134. }
  135. if err := oo.SaveFromWeb(); err != nil {
  136. o.Response(Failure, fmt.Sprintf("数据更新失败:%s", err.Error()), nil)
  137. return
  138. }
  139. o.Response(Success, "成功", oo.ID)
  140. }
  141. // DeleteCamera @Title 删除摄像头或一键告警设备
  142. // @Description 删除摄像头或一键告警设备
  143. // @Param code query string true "设备ID"
  144. // @Success 0 {int} BaseResponse.Code "成功"
  145. // @Failure 1 {int} BaseResponse.Code "失败"
  146. // @router /v1/delete [post]
  147. func (o *CameraController) DeleteCamera() {
  148. code := strings.Trim(o.GetString("code"), " ")
  149. if err := validation.Validate(code, validation.Required, validation.Length(4, 100)); err != nil {
  150. beego.Error(fmt.Sprintf("请求参数验证失败:%s", err.Error()))
  151. o.Response(Failure, fmt.Sprintf("请求参数验证失败:%s", err.Error()), nil)
  152. return
  153. }
  154. c := models.CameraDevice{
  155. ID: code,
  156. }
  157. if err := c.Delete(); err != nil {
  158. beego.Error(fmt.Sprintf("删除失败:%s", err.Error()))
  159. o.Response(Failure, fmt.Sprintf("数据删除失败:%s", err.Error()), nil)
  160. return
  161. }
  162. o.Response(Success, "成功", c.ID)
  163. }
  164. // ImportCamera @Title 导入摄像头或一键告警设备
  165. // @Description 导入摄像头或一键告警设备
  166. // @Param body controllers.ReqImportCamera true "数据"
  167. // @Success 0 {int} BaseResponse.Code "成功"
  168. // @Failure 1 {int} BaseResponse.Code "失败"
  169. // @router /v1/import [post]
  170. func (o *CameraController) ImportCamera() {
  171. var obj ReqImportCamera
  172. if err := json.Unmarshal(o.Ctx.Input.RequestBody, &obj); err != nil {
  173. beego.Debug(string(o.Ctx.Input.RequestBody))
  174. o.Response(Failure, fmt.Sprintf("数据包解析错误:%s", err.Error()), nil)
  175. return
  176. }
  177. if err := obj.Validate(); err != nil {
  178. beego.Error(fmt.Sprintf("请求参数验证失败:%s", err.Error()))
  179. o.Response(Failure, fmt.Sprintf("请求参数验证失败:%s", err.Error()), nil)
  180. return
  181. }
  182. var resp []RespImport
  183. for _, v := range obj.List {
  184. var aresp RespImport
  185. aresp.Code = v.Code
  186. oo := models.CameraDevice{
  187. ID: v.Code, //设备ID,DID
  188. Name: v.Name, //集控器名称
  189. GID: v.GID, //网关ID
  190. DevType: v.DevType,
  191. Tenant: obj.Tenant,
  192. State: v.State, //1启用,0禁用
  193. }
  194. err := oo.SaveFromWeb()
  195. if err != nil {
  196. aresp.Error = err.Error()
  197. beego.Error(fmt.Sprintf("数据导入失败,code=%s,失败原因:%s", v.Code, err.Error()))
  198. }
  199. resp = append(resp, aresp)
  200. }
  201. o.Response(Success, "成功", resp)
  202. }
  203. // UpdateState @Title 更新摄像机或一键告警设备启用禁用状态
  204. // @Description 更新摄像机或一键告警设备启用禁用状态
  205. // @Param body controllers.ProgramsState true "数据"
  206. // @Success 0 {int} BaseResponse.Code "成功"
  207. // @Failure 1 {int} BaseResponse.Code "失败"
  208. // @router /v1/update/state [POST]
  209. func (o *CameraController) UpdateState() {
  210. code := o.GetString("code")
  211. state, err := o.GetUint8("state", 0)
  212. if code == "" || state > 1 || err != nil {
  213. beego.Error("参数错误")
  214. o.Response(Failure, "参数错误", nil)
  215. return
  216. }
  217. err = models.CameraDeviceUpdateState(code, int(state))
  218. if err != nil {
  219. beego.Error("CameraController.UpdateState:更新设备状态错误:", err.Error())
  220. o.Response(Failure, "失败", err.Error())
  221. return
  222. }
  223. o.Response(Success, "成功", code)
  224. }
  225. // SelectPresets @Title 获取预置位信息
  226. // @Description 获取预置位信息
  227. // @Param body controllers.ProgramsState true "数据"
  228. // @Success 0 {int} BaseResponse.Code "成功"
  229. // @Failure 1 {int} BaseResponse.Code "失败"
  230. // @router /v1/select/presets [get]
  231. func (o *CameraController) SelectPresets() {
  232. code := o.GetString("code")
  233. if code == "" {
  234. beego.Error("参数错误")
  235. o.Response(Failure, "参数错误", nil)
  236. return
  237. }
  238. arr, err := models.GetPresetsByID(code)
  239. if err != nil {
  240. beego.Error("CameraController.SelectPresets:获取预置位错误:", err.Error())
  241. o.Response(Failure, "失败", err.Error())
  242. return
  243. }
  244. presetdir := BaseUrl + string(os.PathSeparator) + "file" + string(os.PathSeparator) + "preset" + string(os.PathSeparator)
  245. var ret RespPresetInfo
  246. ret.Code = code
  247. for _, v := range arr {
  248. ret.Data = append(ret.Data, PresetInfo{Token: v.Token, Name: v.Name, File: presetdir + v.File})
  249. }
  250. o.Response(Success, "成功", ret)
  251. }
  252. // ControlPTZ @Title 球机PTZ控制
  253. // @Description 球机PTZ控制
  254. // @router /v1/control/ptz [get]
  255. func (o *CameraController) ControlPTZ() {
  256. code := o.GetString("code") //设备编号
  257. direction, err0 := o.GetUint8("direction") //方向,0~6
  258. speed, err1 := o.GetUint8("speed") //速度
  259. if err0 != nil || err1 != nil || code == "" {
  260. beego.Error(fmt.Sprintf("请输入正确的参数"))
  261. o.Response(Failure, "参数错误", nil)
  262. return
  263. }
  264. if direction < 0 || direction > 6 || speed < 1 {
  265. beego.Error(fmt.Sprintf("请输入正确的参数"))
  266. o.Response(Failure, "参数错误", nil)
  267. return
  268. }
  269. ci, err := models.GetCameraInfoByID(code)
  270. if err != nil {
  271. beego.Error(fmt.Sprintf("找不到该摄像机的配置[ID=%s] err=%s", code, err))
  272. o.Response(Failure, fmt.Sprintf("找不到该摄像机的配置[ID=%s]", code), nil)
  273. return
  274. }
  275. var ptz protocol.Pack_PTZMoveInfo
  276. if str, err := ptz.EnCode(code, ci.GID, "", GetNextUint64(), direction, speed); err == nil {
  277. topic := GetTopic(ci.Tenant, protocol.DT_IPC, code, protocol.TP_ONVIF_PTZ)
  278. err := GetMqttHandler().PublishString(topic, str, mqtt.AtMostOnce)
  279. if err != nil {
  280. beego.Error(fmt.Sprintf("发布PTZ主题[%s]失败:%s", topic, err.Error()))
  281. o.Response(Failure, fmt.Sprintf("发布PTZ主题[%s]失败:%s", topic, err.Error()), nil)
  282. return
  283. } else {
  284. beego.Info(fmt.Sprintf("发布PTZ主题[%s]成功,内容:%s", topic, str))
  285. }
  286. }
  287. o.Response(Success, "成功", nil)
  288. }
  289. // ControlPTZHome @Title 球机HOME设置和跳转
  290. // @Description 球机HOME设置和跳转
  291. // @router /v1/control/ptzhome [get]
  292. func (o *CameraController) ControlPTZHome() {
  293. code := o.GetString("code") //设备编号
  294. flag, err0 := o.GetUint8("flag") //0跳转,1设置
  295. if err0 != nil || code == "" {
  296. beego.Error(fmt.Sprintf("请输入正确的参数"))
  297. o.Response(Failure, "参数错误", nil)
  298. return
  299. }
  300. if flag == 1 {
  301. flag = 4
  302. } else {
  303. flag = 5
  304. }
  305. ci, err := models.GetCameraInfoByID(code)
  306. if err != nil {
  307. beego.Error(fmt.Sprintf("找不到该摄像机的配置[ID=%s]", code))
  308. o.Response(Failure, fmt.Sprintf("找不到该摄像机的配置[ID=%s]", code), nil)
  309. return
  310. }
  311. var ptzc protocol.Pack_PTZCommonInfo
  312. if str, err := ptzc.EnCode(code, ci.GID, "", "1", "home", GetNextUint64(), int(flag)); err == nil {
  313. topic := GetTopic(ci.Tenant, protocol.DT_IPC, code, protocol.TP_ONVIF_PTZ_COMM)
  314. err := GetMqttHandler().PublishString(topic, str, mqtt.AtMostOnce)
  315. if err != nil {
  316. beego.Error(fmt.Sprintf("发布PTZ主题[%s]失败:%s", topic, err.Error()))
  317. o.Response(Failure, fmt.Sprintf("发布PTZ主题[%s]失败:%s", topic, err.Error()), nil)
  318. return
  319. }
  320. }
  321. o.Response(Success, "成功", nil)
  322. }
  323. //case 25: //设置home
  324. //var ptzc protocol.Pack_PTZCommonInfo
  325. //if str, err := ptzc.EnCode(SXTDID, GID, "", "10", "旁边的小路口", GetNextUint64(), 4); err == nil {
  326. //mc.Publish(GetTopic(SXTDID, protocol.DT_IPC, protocol.TP_ONVIF_PTZ_COMM), 0x00, false, str).Wait()
  327. //}
  328. //case 26: //跳转home
  329. //var ptzc protocol.Pack_PTZCommonInfo
  330. //if str, err := ptzc.EnCode(SXTDID, GID, "", "10", "旁边的小路口", GetNextUint64(), 5); err == nil {
  331. //mc.Publish(GetTopic(SXTDID, protocol.DT_IPC, protocol.TP_ONVIF_PTZ_COMM), 0x00, false, str).Wait()
  332. //}
  333. // ControlSnapshot @Title 抓图命令
  334. // @Description 抓图命令
  335. // @router /v1/control/snapshot [get]
  336. func (o *CameraController) ControlSnapshot() {
  337. code := o.GetString("code") //设备编号
  338. if code == "" {
  339. beego.Error(fmt.Sprintf("请输入正确的参数"))
  340. o.Response(Failure, "参数错误", nil)
  341. return
  342. }
  343. ci, err := models.GetCameraInfoByID(code)
  344. if err != nil {
  345. beego.Error(fmt.Sprintf("找不到该摄像机的配置[ID=%s]", code))
  346. o.Response(Failure, fmt.Sprintf("找不到该摄像机的配置[ID=%s]", code), nil)
  347. return
  348. }
  349. var pc protocol.Pack_MediaCommonInfo
  350. if str, err := pc.EnCode(code, ci.GID, "", GetNextUint64(), 0 /*0:抓图*/); err == nil {
  351. topic := GetTopic(ci.Tenant, protocol.DT_IPC, code, protocol.TP_ONVIF_SNAPSHOT)
  352. err := GetMqttHandler().PublishString(topic, str, mqtt.AtMostOnce)
  353. if err != nil {
  354. beego.Error(fmt.Sprintf("发布PTZ主题[%s]失败:%s", topic, err.Error()))
  355. o.Response(Failure, fmt.Sprintf("发布PTZ主题[%s]失败:%s", topic, err.Error()), nil)
  356. return
  357. }
  358. }
  359. o.Response(Success, "成功", nil)
  360. }
  361. // Video @Title 视频推流、停止推流
  362. // @Description 视频推流、停止推流
  363. // @router /v1/control/video [get]
  364. func (o *CameraController) Video() {
  365. code := o.GetString("code") //设备编号
  366. flag, err := o.GetUint8("flag") //方向,0~6
  367. if err != nil || code == "" {
  368. beego.Error(fmt.Sprintf("请输入正确的参数"))
  369. o.Response(Failure, "参数错误", nil)
  370. return
  371. }
  372. if flag != 1 && flag != 2 {
  373. beego.Error(fmt.Sprintf("请输入正确的参数"))
  374. o.Response(Failure, "参数错误", nil)
  375. return
  376. }
  377. ci, err := models.GetCameraInfoByID(code)
  378. if err != nil {
  379. beego.Error(fmt.Sprintf("找不到该摄像机的配置[ID=%s]", code))
  380. o.Response(Failure, fmt.Sprintf("找不到该摄像机的配置[ID=%s]", code), nil)
  381. return
  382. }
  383. //开启GB28181协议的设备,视频流直接走GB28181服务,不需要边缘端用ffmpeg拉流、推流
  384. if ci.Gb28181 {
  385. beego.Debug(fmt.Sprintf("该摄象机配置了GB28181,不需要ffmpeg推流[设备ID=%s]", code))
  386. o.Response(Success, "成功", nil)
  387. return
  388. }
  389. var pc protocol.Pack_MediaCommonInfo
  390. if str, err := pc.EnCode(code, ci.GID, "", GetNextUint64(), int(flag)); err == nil {
  391. topic := GetTopic(ci.Tenant, protocol.DT_IPC, code, protocol.TP_ONVIF_VIDEO)
  392. err := GetMqttHandler().PublishString(topic, str, mqtt.AtMostOnce)
  393. if err != nil {
  394. beego.Error(fmt.Sprintf("发布PTZ主题[%s]失败:%s", topic, err.Error()))
  395. o.Response(Failure, fmt.Sprintf("发布PTZ主题[%s]失败:%s", topic, err.Error()), nil)
  396. return
  397. }
  398. }
  399. o.Response(Success, "成功", nil)
  400. }
  401. // Snapshot @Title 抓图上报
  402. // @Description 摄像头抓图上报
  403. // @router /v1/snapshot [post]
  404. func (o *CameraController) Snapshot() {
  405. f, fh, err := o.Ctx.Request.FormFile("file")
  406. if err != nil {
  407. return
  408. }
  409. defer f.Close()
  410. f.Seek(0, 0)
  411. content, err := ioutil.ReadAll(f)
  412. if err != nil {
  413. return
  414. }
  415. strDate := util.MlNow().Format("2006-01-02")
  416. filepath := "file" + string(os.PathSeparator) + "snapshot" + string(os.PathSeparator)
  417. strList := strings.Split(fh.Filename, "_")
  418. if len(strList) >= 2 {
  419. filepath = filepath + strList[0] + string(os.PathSeparator)
  420. strList2 := strings.Split(strList[1], ".")
  421. if len(strList2) > 0 {
  422. i64, err := strconv.ParseInt(strList2[0], 10, 64)
  423. if err == nil {
  424. strDate = time.Unix(i64, 0).Format("2006-01-02")
  425. }
  426. }
  427. }
  428. filepath = filepath + strDate + string(os.PathSeparator)
  429. os.MkdirAll(filepath, os.ModePerm)
  430. filepath = filepath + fh.Filename
  431. err = ioutil.WriteFile(filepath, content, os.ModePerm)
  432. if err != nil {
  433. beego.Error("Snapshot存储抓图发生错误:", err)
  434. }
  435. }
  436. // PresetPicture @Title 预置位图片上传
  437. // @Description 预置位图片上传
  438. // @router /v1/preset/picture [post]
  439. func (o *CameraController) PresetPicture() {
  440. f, fh, err := o.Ctx.Request.FormFile("file")
  441. if err != nil {
  442. return
  443. }
  444. defer f.Close()
  445. f.Seek(0, 0)
  446. content, err := ioutil.ReadAll(f)
  447. if err != nil {
  448. return
  449. }
  450. filepath := "file" + string(os.PathSeparator) + "preset" + string(os.PathSeparator)
  451. os.MkdirAll(filepath, os.ModePerm)
  452. filepath = filepath + fh.Filename
  453. err = ioutil.WriteFile(filepath, content, os.ModePerm)
  454. if err != nil {
  455. beego.Error("Snapshot存储抓图发生错误:", err)
  456. }
  457. }
  458. // FLV @Title 上传MP4
  459. // @Description 上传MP4
  460. // @router /v1/flv [post]
  461. func (o *CameraController) FLV() {
  462. f, fh, err := o.Ctx.Request.FormFile("file")
  463. if err != nil {
  464. return
  465. }
  466. defer f.Close()
  467. f.Seek(0, 0)
  468. content, err := ioutil.ReadAll(f)
  469. if err != nil {
  470. return
  471. }
  472. strDate := util.MlNow().Format("2006-01-02")
  473. filepath := "file" + string(os.PathSeparator) + "flv" + string(os.PathSeparator)
  474. strList := strings.Split(fh.Filename, "_")
  475. if len(strList) >= 2 {
  476. filepath = filepath + strList[0] + string(os.PathSeparator)
  477. strList2 := strings.Split(strList[1], ".")
  478. if len(strList2) > 0 {
  479. i64, err := strconv.ParseInt(strList2[0], 10, 64)
  480. if err == nil {
  481. strDate = time.Unix(i64, 0).Format("2006-01-02")
  482. }
  483. }
  484. }
  485. filepath = filepath + strDate + string(os.PathSeparator)
  486. os.MkdirAll(filepath, os.ModePerm)
  487. filepath = filepath + fh.Filename
  488. err = ioutil.WriteFile(filepath, content, os.ModePerm)
  489. if err != nil {
  490. beego.Error("MP4存储发生错误:", err)
  491. }
  492. }
  493. // GetSnapshot @Title 获取抓取的图片
  494. // @Description 获取抓取的图片
  495. // @router /v1/select/snapshot [get]
  496. func (o *CameraController) GetSnapshot() {
  497. code := o.GetString("code") //设备编号
  498. date := o.GetString("date") //
  499. if code == "" {
  500. beego.Error(fmt.Sprintf("请输入正确的参数"))
  501. o.Response(Failure, "参数错误", nil)
  502. return
  503. }
  504. if date == "" {
  505. date = util.MlNow().Format("2006-01-02")
  506. }
  507. //dir := "file/snapshot/" + util.MlNow().Format("2006-01-02") + "/"
  508. filepath := "file" + string(os.PathSeparator) + "snapshot" + string(os.PathSeparator) +
  509. code + string(os.PathSeparator) + date + string(os.PathSeparator)
  510. fis, err := ioutil.ReadDir(filepath)
  511. if err != nil {
  512. beego.Error(fmt.Sprintf("未找到该路径[%s],请先抓图", err.Error()))
  513. o.Response(Failure, "未找到该路径,请先抓图", nil)
  514. return
  515. }
  516. var obj RespSnapshotUrl
  517. obj.Code = code
  518. obj.Date = date
  519. for _, v := range fis {
  520. if strings.HasSuffix(v.Name(), ".jpg") { //只返回jpg图片
  521. jpgUrl := BaseUrl + "/file/snapshot/" + code + "/" + date + "/" + v.Name()
  522. obj.URLs = append(obj.URLs, jpgUrl)
  523. }
  524. }
  525. o.Response(Success, "成功", obj)
  526. }
  527. // GetFLV @Title 获取录制的视频
  528. // @Description 获取录制的视频
  529. // @router /v1/select/flv [get]
  530. func (o *CameraController) GetFLV() {
  531. code := o.GetString("code") //设备编号
  532. date := o.GetString("date") //
  533. if code == "" {
  534. beego.Error(fmt.Sprintf("请输入正确的参数"))
  535. o.Response(Failure, "参数错误", nil)
  536. return
  537. }
  538. if date == "" {
  539. date = util.MlNow().Format("2006-01-02")
  540. }
  541. //dir := "file/snapshot/" + util.MlNow().Format("2006-01-02") + "/"
  542. filepath := "file" + string(os.PathSeparator) + "flv" + string(os.PathSeparator) +
  543. code + string(os.PathSeparator) + date + string(os.PathSeparator)
  544. fis, err := ioutil.ReadDir(filepath)
  545. if err != nil {
  546. beego.Error(fmt.Sprintf("未找到该路径[%s],请先录制mp4", err.Error()))
  547. o.Response(Failure, "未找到该路径,请先录制mp4", nil)
  548. return
  549. }
  550. var obj RespSnapshotUrl
  551. obj.Code = code
  552. obj.Date = date
  553. for _, v := range fis {
  554. if strings.HasSuffix(v.Name(), ".mp4") { //只返回jpg图片
  555. jpgUrl := BaseUrl + "/file/mp4/" + code + "/" + date + "/" + v.Name()
  556. obj.URLs = append(obj.URLs, jpgUrl)
  557. }
  558. }
  559. o.Response(Success, "成功", obj)
  560. }