camera.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package v1
  4. import (
  5. "net/http"
  6. "smart-site-management-gateway/errors"
  7. param_v1 "smart-site-management-gateway/param/v1"
  8. "smart-site-management-gateway/pb"
  9. "smart-site-management-gateway/pb/v1"
  10. "smart-site-management-gateway/utils"
  11. "github.com/gin-gonic/gin"
  12. "github.com/jaryhe/gopkgs/logger"
  13. "github.com/jaryhe/gopkgs/tasker/httptasker"
  14. "github.com/jaryhe/gopkgs/util"
  15. "go.uber.org/zap"
  16. )
  17. //
  18. // @Summary 设备绑定摄像头
  19. // @Description 设备绑定摄像头
  20. // @Tags 摄像头后台设置
  21. // @Accept json
  22. // @Produce json
  23. // @Param token header string true " "
  24. // @Param body body v1.DeviceBindCameraBody false " "
  25. // @Success 200 {object} v1.DeviceBindCameraResponse
  26. // @Failure 500 {object} base.HTTPError
  27. // @Router /api/v1/camera/bind_device [put]
  28. func (c *Controller) DeviceBindCamera(ctx *gin.Context) {
  29. // 解析参数
  30. req := &param_v1.DeviceBindCameraRequest{}
  31. var loginUid int64
  32. var userName string
  33. parseParamTask := func() error {
  34. err := util.ShouldBind(ctx, nil, nil, nil, &req.DeviceBindCameraBody)
  35. if err != nil {
  36. logger.Error("func",
  37. zap.String("call", "util.ShouldBind"),
  38. zap.String("error", err.Error()))
  39. return errors.ParamsError
  40. }
  41. loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
  42. return nil
  43. }
  44. // 业务处理
  45. var pid int64
  46. handleServiceTask := func() error {
  47. _, projectId, err := utils.GetSubjectValue(ctx)
  48. if err != nil {
  49. return err
  50. }
  51. pid = projectId
  52. // 响应数据
  53. resp := param_v1.DeviceBindCameraResponse{}
  54. rpcReq := &v1.DeviceBindCameraRequest{
  55. DeviceId:req.DeviceId,
  56. ChannelId:req.ChannelId,
  57. }
  58. _, err = pb.Project.DeviceBindCamera(ctx, rpcReq)
  59. if err != nil {
  60. s, _ := json.MarshalToString(req)
  61. logger.Error("func",
  62. zap.String("call", "pb.Project.DeviceBindCamera"),
  63. zap.String("params", s),
  64. zap.String("error", err.Error()))
  65. return errors.ErrorTransForm(err)
  66. }
  67. ctx.JSON(http.StatusOK, resp)
  68. return nil
  69. }
  70. // 执行任务
  71. err := httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  72. s, _ := json.MarshalToString(req)
  73. utils.LogWrite("设备绑定摄像头", loginUid, userName, s, err, pid)
  74. }
  75. //
  76. // @Summary 设备解绑摄像头
  77. // @Description 设备解绑摄像头
  78. // @Tags 摄像头后台设置
  79. // @Accept json
  80. // @Produce json
  81. // @Param token header string true " "
  82. // @Param body body v1.DeviceUnbindCameraBody false " "
  83. // @Success 200 {object} v1.DeviceUnbindCameraResponse
  84. // @Failure 500 {object} base.HTTPError
  85. // @Router /api/v1/camera/unbind_device [put]
  86. func (c *Controller) DeviceUnbindCamera(ctx *gin.Context) {
  87. // 解析参数
  88. req := &param_v1.DeviceUnbindCameraRequest{}
  89. var loginUid int64
  90. var userName string
  91. parseParamTask := func() error {
  92. err := util.ShouldBind(ctx, nil, nil, nil, &req.DeviceUnbindCameraBody)
  93. if err != nil {
  94. logger.Error("func",
  95. zap.String("call", "util.ShouldBind"),
  96. zap.String("error", err.Error()))
  97. return errors.ParamsError
  98. }
  99. loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
  100. return nil
  101. }
  102. // 业务处理
  103. var pid int64
  104. handleServiceTask := func() error {
  105. _, projectId, err := utils.GetSubjectValue(ctx)
  106. if err != nil {
  107. return err
  108. }
  109. pid = projectId
  110. // 响应数据
  111. resp := param_v1.DeviceUnbindCameraResponse{}
  112. rpcReq := &v1.DeviceUnbindCameraRequest{
  113. DeviceId:req.DeviceId,
  114. ChannelId:req.ChannelId,
  115. }
  116. _, err = pb.Project.DeviceUnbindCamera(ctx, rpcReq)
  117. if err != nil {
  118. s, _ := json.MarshalToString(req)
  119. logger.Error("func",
  120. zap.String("call", "pb.Project.DeviceUnbindCamera"),
  121. zap.String("params", s),
  122. zap.String("error", err.Error()))
  123. return errors.ErrorTransForm(err)
  124. }
  125. ctx.JSON(http.StatusOK, resp)
  126. return nil
  127. }
  128. // 执行任务
  129. err := httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  130. s, _ := json.MarshalToString(req)
  131. utils.LogWrite("设备解绑摄像头", loginUid, userName, s, err, pid)
  132. }
  133. //
  134. // @Summary 获取实时播放地址
  135. // @Description 获取实时播放地址
  136. // @Tags
  137. // @Accept json
  138. // @Produce json
  139. // @Param token header string true " "
  140. // @Param channel_no query string true " "
  141. // @Success 200 {object} v1.RealPlayResponse
  142. // @Failure 500 {object} base.HTTPError
  143. // @Router /api/v1/camera/real_play [get]
  144. func (c *Controller) RealPlay(ctx *gin.Context) {
  145. // 解析参数
  146. req := &param_v1.RealPlayRequest{}
  147. var loginUid int64
  148. var userName string
  149. parseParamTask := func() error {
  150. err := util.ShouldBind(ctx, nil, nil, &req.RealPlayQuery, nil)
  151. if err != nil {
  152. logger.Error("func",
  153. zap.String("call", "util.ShouldBind"),
  154. zap.String("error", err.Error()))
  155. return errors.ParamsError
  156. }
  157. loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
  158. return nil
  159. }
  160. // 业务处理
  161. handleServiceTask := func() error {
  162. // 响应数据
  163. resp := param_v1.RealPlayResponse{}
  164. rpcReq := &v1.RealPlayRequest{
  165. ChannelNo:req.ChannelNo,
  166. }
  167. rpcRsp, err := pb.Project.RealPlay(ctx, rpcReq)
  168. if err != nil {
  169. s, _ := json.MarshalToString(req)
  170. logger.Error("func",
  171. zap.String("call", "pb.Project.RealPlay"),
  172. zap.String("params", s),
  173. zap.String("error", err.Error()))
  174. return errors.ErrorTransForm(err)
  175. }
  176. resp.Data = *rpcRsp
  177. ctx.JSON(http.StatusOK, resp)
  178. return nil
  179. }
  180. // 执行任务
  181. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  182. }
  183. //
  184. // @Summary ptz控制
  185. // @Description ptz控制
  186. // @Tags
  187. // @Accept json
  188. // @Produce json
  189. // @Param token header string true " "
  190. // @Param channel_no query string true " "
  191. // @Param ptz_type query string true " 上:up,下:down,左:left,右:right,左上:leftup,左下:leftdown,右上:rightup,右下:rightdown,镜头近:zoomin,镜头远:zoomout, 焦距远:focusfar,焦距近:focusnear, 设置预置位:setpos,调预置位:callpos,调预置位:delpos,停止:stop"
  192. // @Param ptz_param query int true "速度范围为1-255 "
  193. // @Success 200 {object} v1.PtzResponse
  194. // @Failure 500 {object} base.HTTPError
  195. // @Router /api/v1/camera/ptz [get]
  196. func (c *Controller) Ptz(ctx *gin.Context) {
  197. // 解析参数
  198. req := &param_v1.PtzRequest{}
  199. var loginUid int64
  200. var userName string
  201. parseParamTask := func() error {
  202. err := util.ShouldBind(ctx, nil, nil, &req.PtzQuery, nil)
  203. if err != nil {
  204. logger.Error("func",
  205. zap.String("call", "util.ShouldBind"),
  206. zap.String("error", err.Error()))
  207. return errors.ParamsError
  208. }
  209. loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
  210. return nil
  211. }
  212. // 业务处理
  213. handleServiceTask := func() error {
  214. // 响应数据
  215. resp := param_v1.PtzResponse{}
  216. rpcReq := &v1.PtzRequest{
  217. ChannelNo:req.ChannelNo,
  218. Ptzparam:req.PtzParam,
  219. Ptztype:req.PtzType,
  220. }
  221. _, err := pb.Project.Ptz(ctx, rpcReq)
  222. if err != nil {
  223. s, _ := json.MarshalToString(req)
  224. logger.Error("func",
  225. zap.String("call", "pb.Project.Ptz"),
  226. zap.String("params", s),
  227. zap.String("error", err.Error()))
  228. return errors.ErrorTransForm(err)
  229. }
  230. ctx.JSON(http.StatusOK, resp)
  231. return nil
  232. }
  233. // 执行任务
  234. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  235. }
  236. //
  237. // @Summary 摄像头ping
  238. // @Description 摄像头ping
  239. // @Tags camera
  240. // @Accept json
  241. // @Produce json
  242. // @Param session_id query string true " "
  243. // @Success 200 {object} v1.VedioPingResponse
  244. // @Failure 500 {object} base.HTTPError
  245. // @Router /api/v1/camera/ping [get]
  246. func (c *Controller) VedioPing(ctx *gin.Context) {
  247. // 解析参数
  248. req := &param_v1.VedioPingRequest{}
  249. var loginUid int64
  250. var userName string
  251. parseParamTask := func() error {
  252. err := util.ShouldBind(ctx, nil, nil, &req.VedioPingQuery, nil)
  253. if err != nil {
  254. logger.Error("func",
  255. zap.String("call", "util.ShouldBind"),
  256. zap.String("error", err.Error()))
  257. return errors.ParamsError
  258. }
  259. loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
  260. return nil
  261. }
  262. // 业务处理
  263. handleServiceTask := func() error {
  264. // 响应数据
  265. resp := param_v1.VedioPingResponse{}
  266. rpcReq := &v1.VedioPingRequest{
  267. Sessionid:req.SessionId,
  268. }
  269. rpcResp, err := pb.Project.VedioPing(ctx, rpcReq)
  270. if err != nil {
  271. s, _ := json.MarshalToString(req)
  272. logger.Error("func",
  273. zap.String("call", "pb.Project.VedioPing"),
  274. zap.String("params", s),
  275. zap.String("error", err.Error()))
  276. return errors.ErrorTransForm(err)
  277. }
  278. resp.Data = *rpcResp
  279. ctx.JSON(http.StatusOK, resp)
  280. return nil
  281. }
  282. // 执行任务
  283. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  284. }
  285. // 编辑通道
  286. // @Summary 编辑通道
  287. // @Description 编辑通道
  288. // @Tags 摄像头后台设置
  289. // @Accept json
  290. // @Produce json
  291. // @Param token header string true " "
  292. // @Param body body v1.ChannelUpdateBody true " "
  293. // @Success 200 {object} v1.ChannelUpdateResponse
  294. // @Failure 500 {object} base.HTTPError
  295. // @Router /api/v1/device/vedio_channel [put]
  296. func (c *Controller) ChannelUpdate(ctx *gin.Context) {
  297. // 解析参数
  298. req := &param_v1.ChannelUpdateRequest{}
  299. var loginUid int64
  300. var userName string
  301. parseParamTask := func() error {
  302. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.ChannelUpdateBody)
  303. if err != nil {
  304. logger.Error("func",
  305. zap.String("call", "util.ShouldBind"),
  306. zap.String("error", err.Error()))
  307. return errors.ParamsError
  308. }
  309. loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
  310. return nil
  311. }
  312. // 业务处理
  313. var projectId int64
  314. handleServiceTask := func() error {
  315. // 响应数据
  316. resp := param_v1.ChannelUpdateResponse{}
  317. rpcReq := &v1.ChannelUpdateRequest{
  318. Id:req.Id,
  319. Name:req.Name,
  320. }
  321. rpcRsp, err := pb.Project.ChannelUpdate(ctx, rpcReq)
  322. if err != nil {
  323. s, _ := json.MarshalToString(req)
  324. logger.Error("func",
  325. zap.String("call", "ChannelUpdate"),
  326. zap.String("params", s),
  327. zap.String("error", err.Error()))
  328. return errors.ErrorTransForm(err)
  329. }
  330. projectId = rpcRsp.ProjectId
  331. ctx.JSON(http.StatusOK, resp)
  332. return nil
  333. }
  334. // 执行任务
  335. err := httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  336. s, _ := json.MarshalToString(req)
  337. utils.LogWrite("编辑通道", loginUid, userName, s, err, projectId)
  338. }