tower.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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/parser"
  9. "smart-site-management-gateway/pb"
  10. "smart-site-management-gateway/pb/v1"
  11. "smart-site-management-gateway/utils"
  12. "github.com/gin-gonic/gin"
  13. "github.com/jaryhe/gopkgs/logger"
  14. "github.com/jaryhe/gopkgs/tasker/httptasker"
  15. "github.com/jaryhe/gopkgs/util"
  16. "go.uber.org/zap"
  17. )
  18. //
  19. // @Summary 塔机历史
  20. // @Description 塔机历史
  21. // @Tags 塔吊监控前台展示
  22. // @Accept json
  23. // @Produce json
  24. // @Param token header string true " "
  25. // @Param page query int false " "
  26. // @Param page_size query int false " "
  27. // @Param start query int64 false " "
  28. // @Param end query int64 false " "
  29. // @Param sn query string true " "
  30. // @Success 200 {object} v1.TowerMonitorHistoryResponse
  31. // @Failure 500 {object} base.HTTPError
  32. // @Router /api/v1/iot/tower_history [get]
  33. func (c *Controller) TowerMonitorHistory(ctx *gin.Context) {
  34. // 解析参数
  35. req := &param_v1.TowerMonitorHistoryRequest{}
  36. var loginUid int64
  37. var userName string
  38. parseParamTask := func() error {
  39. err := util.ShouldBind(ctx, nil, nil, &req.TowerMonitorHistoryQuery, nil)
  40. if err != nil {
  41. logger.Error("func",
  42. zap.String("call", "util.ShouldBind"),
  43. zap.String("error", err.Error()))
  44. return errors.ParamsError
  45. }
  46. loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
  47. return nil
  48. }
  49. // 业务处理
  50. handleServiceTask := func() error {
  51. // 响应数据
  52. resp := param_v1.TowerMonitorHistoryResponse{}
  53. rpcReq := &v1.TowerMonitorHistoryRequest{
  54. Sn: req.Sn,
  55. Start:req.Start,
  56. End:req.End,
  57. PageSize:req.PageSize,
  58. Page:req.Page,
  59. }
  60. rpcRsp, err := pb.Project.TowerMonitorHistory(ctx, rpcReq)
  61. if err != nil {
  62. s, _ := json.MarshalToString(req)
  63. logger.Error("func",
  64. zap.String("call", "pb.Project.TowerMonitorHistory"),
  65. zap.String("params", s),
  66. zap.String("error", err.Error()))
  67. return errors.ErrorTransForm(err)
  68. }
  69. resp.Data = *rpcRsp
  70. ctx.JSON(http.StatusOK, resp)
  71. return nil
  72. }
  73. // 执行任务
  74. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  75. }
  76. //
  77. // @Summary 塔吊最近数据
  78. // @Description 塔吊最近数据
  79. // @Tags 塔吊监控前台展示
  80. // @Accept json
  81. // @Produce json
  82. // @Param token header string true " "
  83. // @Param sn query string false " "
  84. // @Success 200 {object} v1.LatestTowerMonitorDataResponse
  85. // @Failure 500 {object} base.HTTPError
  86. // @Router /api/v1/iot/tower_last [get]
  87. func (c *Controller) LatestTowerMonitorData(ctx *gin.Context) {
  88. // 解析参数
  89. req := &param_v1.LatestTowerMonitorDataRequest{}
  90. var loginUid int64
  91. var userName string
  92. parseParamTask := func() error {
  93. err := util.ShouldBind(ctx, nil, nil, &req.LatestTowerMonitorQuery, nil)
  94. if err != nil {
  95. logger.Error("func",
  96. zap.String("call", "util.ShouldBind"),
  97. zap.String("error", err.Error()))
  98. return errors.ParamsError
  99. }
  100. loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
  101. return nil
  102. }
  103. // 业务处理
  104. handleServiceTask := func() error {
  105. // 响应数据
  106. _, projectId, err := utils.GetSubjectValue(ctx)
  107. if err != nil {
  108. return err
  109. }
  110. resp := param_v1.LatestTowerMonitorDataResponse{}
  111. rpcReq := &v1.LatestTowerMonitorDataRequest{
  112. Sn: req.Sn,
  113. ProjectId:projectId,
  114. }
  115. rpcRsp, err := pb.Project.LatestTowerMonitorData(ctx, rpcReq)
  116. if err != nil {
  117. s, _ := json.MarshalToString(req)
  118. logger.Error("func",
  119. zap.String("call", "pb.Project.LatestTowerMonitorData"),
  120. zap.String("params", s),
  121. zap.String("error", err.Error()))
  122. return errors.ErrorTransForm(err)
  123. }
  124. resp.Data = *rpcRsp.Data
  125. ctx.JSON(http.StatusOK, resp)
  126. return nil
  127. }
  128. // 执行任务
  129. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  130. }
  131. //
  132. // @Summary iot设备列表
  133. // @Description iot设备列表
  134. // @Tags 塔吊监控前台展示
  135. // @Accept json
  136. // @Produce json
  137. // @Param token header string true " "
  138. // @Param page query int false " "
  139. // @Param page_size query int false " "
  140. // @Param device_code query int true " "
  141. // @Success 200 {object} v1.IotListResponse
  142. // @Failure 500 {object} base.HTTPError
  143. // @Router /api/v1/iot/list [get]
  144. func (c *Controller) IotList(ctx *gin.Context) {
  145. // 解析参数
  146. req := &param_v1.IotListRequest{}
  147. var loginUid int64
  148. var userName string
  149. parseParamTask := func() error {
  150. err := util.ShouldBind(ctx, nil, nil, &req.IotListQuery, 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. _, projectId, err := utils.GetSubjectValue(ctx)
  163. if err != nil {
  164. return err
  165. }
  166. // 响应数据
  167. resp := param_v1.IotListResponse{}
  168. rpcReq := &v1.IotListRequest{
  169. PageSize:req.PageSize,
  170. Page:req.Page,
  171. ProjectId:projectId,
  172. DeviceCode:req.DeviceCode,
  173. }
  174. rpcRsp, err := pb.Project.IotList(ctx, rpcReq)
  175. if err != nil {
  176. s, _ := json.MarshalToString(req)
  177. logger.Error("func",
  178. zap.String("call", "pb.Project.IotList"),
  179. zap.String("params", s),
  180. zap.String("error", err.Error()))
  181. return errors.ErrorTransForm(err)
  182. }
  183. resp.Data = *rpcRsp
  184. if resp.Data.List == nil {
  185. resp.Data.List = []*v1.IotListItem{}
  186. }
  187. ctx.JSON(http.StatusOK, resp)
  188. return nil
  189. }
  190. // 执行任务
  191. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  192. }
  193. //
  194. // @Summary iot设备类型列表
  195. // @Description iot设备类型列表
  196. // @Tags 塔吊监控前台展示
  197. // @Accept json
  198. // @Produce json
  199. // @Param token header string true " "
  200. // @Success 200 {object} v1.IotTypeListResponse
  201. // @Failure 500 {object} base.HTTPError
  202. // @Router /api/v1/iot/type_list [get]
  203. func (c *Controller) IotTypeList(ctx *gin.Context) {
  204. // 解析参数
  205. req := &param_v1.IotTypeListRequest{}
  206. var loginUid int64
  207. var userName string
  208. parseParamTask := func() error {
  209. err := util.ShouldBind(ctx, nil, nil, nil, nil)
  210. if err != nil {
  211. logger.Error("func",
  212. zap.String("call", "util.ShouldBind"),
  213. zap.String("error", err.Error()))
  214. return errors.ParamsError
  215. }
  216. loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
  217. return nil
  218. }
  219. // 业务处理
  220. handleServiceTask := func() error {
  221. _, projectId, err := utils.GetSubjectValue(ctx)
  222. if err != nil {
  223. return err
  224. }
  225. // 响应数据
  226. resp := param_v1.IotTypeListResponse{}
  227. rpcReq := &v1.IotTypeListRequest{
  228. ProjectId:projectId,
  229. }
  230. rpcRsp, err := pb.Project.IotTypeList(ctx, rpcReq)
  231. if err != nil {
  232. s, _ := json.MarshalToString(req)
  233. logger.Error("func",
  234. zap.String("call", "pb.Project.IotList"),
  235. zap.String("params", s),
  236. zap.String("error", err.Error()))
  237. return errors.ErrorTransForm(err)
  238. }
  239. resp.Data = *rpcRsp
  240. if resp.Data.List == nil {
  241. resp.Data.List = []*v1.IotTypeListItem{}
  242. }
  243. ctx.JSON(http.StatusOK, resp)
  244. return nil
  245. }
  246. // 执行任务
  247. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  248. }
  249. //
  250. // @Summary iot最近数据
  251. // @Description iot最近数据
  252. // @Tags 塔吊监控前台展示
  253. // @Accept json
  254. // @Produce json
  255. // @Param token header string true " "
  256. // @Param sn query string true " "
  257. // @Param device_code query int true " "
  258. // @Success 200 {object} v1.IotLastResponse
  259. // @Failure 500 {object} base.HTTPError
  260. // @Router /api/v1/iot/last [get]
  261. func (c *Controller) IotLast(ctx *gin.Context) {
  262. // 解析参数
  263. req := &param_v1.IotLastRequest{}
  264. var loginUid int64
  265. var userName string
  266. parseParamTask := func() error {
  267. err := util.ShouldBind(ctx, nil, nil, &req.IotLastQuery, nil)
  268. if err != nil {
  269. logger.Error("func",
  270. zap.String("call", "util.ShouldBind"),
  271. zap.String("error", err.Error()))
  272. return errors.ParamsError
  273. }
  274. loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
  275. return nil
  276. }
  277. // 业务处理
  278. handleServiceTask := func() error {
  279. // 响应数据
  280. _, projectId, err := utils.GetSubjectValue(ctx)
  281. if err != nil {
  282. return err
  283. }
  284. resp := param_v1.IotLastResponse{}
  285. rpcReq := &v1.IotLastRequest{
  286. Sn: req.Sn,
  287. ProjectId:projectId,
  288. DeviceCode:req.DeviceCode,
  289. }
  290. rpcRsp, err := pb.Project.IotLast(ctx, rpcReq)
  291. if err != nil {
  292. s, _ := json.MarshalToString(req)
  293. logger.Error("func",
  294. zap.String("call", "pb.Project.IotLast"),
  295. zap.String("params", s),
  296. zap.String("error", err.Error()))
  297. return errors.ErrorTransForm(err)
  298. }
  299. resp.Data = *rpcRsp
  300. resp.Data.Pic = parser.Conf.TowerPicUrl
  301. ctx.JSON(http.StatusOK, resp)
  302. return nil
  303. }
  304. // 执行任务
  305. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  306. }