dust.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 type query int true " "
  25. // @Param time query int64 true " "
  26. // @Param sn query string true " "
  27. // @Success 200 {object} v1.DustMonitorHistoryResponse
  28. // @Failure 500 {object} base.HTTPError
  29. // @Router /api/v1/dust/history [get]
  30. func (c *Controller) DustMonitorHistory(ctx *gin.Context) {
  31. // 解析参数
  32. req := &param_v1.DustMonitorHistoryRequest{}
  33. var loginUid int64
  34. var userName string
  35. parseParamTask := func() error {
  36. err := util.ShouldBind(ctx, nil, nil, &req.DustMonitorHistoryQuery, nil)
  37. if err != nil {
  38. logger.Error("func",
  39. zap.String("call", "util.ShouldBind"),
  40. zap.String("error", err.Error()))
  41. return errors.ParamsError
  42. }
  43. loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
  44. return nil
  45. }
  46. // 业务处理
  47. handleServiceTask := func() error {
  48. // 响应数据
  49. resp := param_v1.DustMonitorHistoryResponse{}
  50. rpcReq := &v1.DustMonitorHistoryRequest{
  51. Sn: req.Sn,
  52. Type: req.Type,
  53. Time:req.Time,
  54. }
  55. rpcRsp, err := pb.Project.DustMonitorHistory(ctx, rpcReq)
  56. if err != nil {
  57. s, _ := json.MarshalToString(req)
  58. logger.Error("func",
  59. zap.String("call", "pb.Project.DustMonitorHistory"),
  60. zap.String("params", s),
  61. zap.String("error", err.Error()))
  62. return errors.ErrorTransForm(err)
  63. }
  64. resp.Data = *rpcRsp
  65. ctx.JSON(http.StatusOK, resp)
  66. return nil
  67. }
  68. // 执行任务
  69. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  70. }
  71. //
  72. // @Summary 扬尘最近数据
  73. // @Description 扬尘最近数据
  74. // @Tags 扬尘监控前台展示
  75. // @Accept json
  76. // @Produce json
  77. // @Param token header string true " "
  78. // @Param type query int false " "
  79. // @Param sn query string false " "
  80. // @Success 200 {object} v1.LatestDustMonitorDataResponse
  81. // @Failure 500 {object} base.HTTPError
  82. // @Router /api/v1/dust/last [get]
  83. func (c *Controller) LatestDustMonitorData(ctx *gin.Context) {
  84. // 解析参数
  85. req := &param_v1.LatestDustMonitorDataRequest{}
  86. var loginUid int64
  87. var userName string
  88. parseParamTask := func() error {
  89. err := util.ShouldBind(ctx, nil, nil, &req.LatestDustMonitorQuery, nil)
  90. if err != nil {
  91. logger.Error("func",
  92. zap.String("call", "util.ShouldBind"),
  93. zap.String("error", err.Error()))
  94. return errors.ParamsError
  95. }
  96. loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
  97. return nil
  98. }
  99. // 业务处理
  100. handleServiceTask := func() error {
  101. // 响应数据
  102. _, projectId, err := utils.GetSubjectValue(ctx)
  103. if err != nil {
  104. return err
  105. }
  106. resp := param_v1.LatestDustMonitorDataResponse{}
  107. rpcReq := &v1.LatestDustMonitorDataRequest{
  108. Sn: req.Sn,
  109. Type: req.Type,
  110. ProjectId:projectId,
  111. }
  112. rpcRsp, err := pb.Project.LatestDustMonitorData(ctx, rpcReq)
  113. if err != nil {
  114. s, _ := json.MarshalToString(req)
  115. logger.Error("func",
  116. zap.String("call", "pb.Project.LatestDustMonitorData"),
  117. zap.String("params", s),
  118. zap.String("error", err.Error()))
  119. return errors.ErrorTransForm(err)
  120. }
  121. resp.Data = *rpcRsp.Data
  122. ctx.JSON(http.StatusOK, resp)
  123. return nil
  124. }
  125. // 执行任务
  126. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  127. }
  128. //
  129. // @Summary 扬尘设备列表
  130. // @Description 扬尘历史
  131. // @Tags 扬尘监控前台展示
  132. // @Accept json
  133. // @Produce json
  134. // @Param token header string true " "
  135. // @Param page query int true " "
  136. // @Param page_size query int true " "
  137. // @Success 200 {object} v1.DustListResponse
  138. // @Failure 500 {object} base.HTTPError
  139. // @Router /api/v1/dust/list [get]
  140. func (c *Controller) DustList(ctx *gin.Context) {
  141. // 解析参数
  142. req := &param_v1.DustListRequest{}
  143. var loginUid int64
  144. var userName string
  145. parseParamTask := func() error {
  146. err := util.ShouldBind(ctx, nil, nil, &req.DustListQuery, nil)
  147. if err != nil {
  148. logger.Error("func",
  149. zap.String("call", "util.ShouldBind"),
  150. zap.String("error", err.Error()))
  151. return errors.ParamsError
  152. }
  153. loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
  154. return nil
  155. }
  156. // 业务处理
  157. handleServiceTask := func() error {
  158. _, projectId, err := utils.GetSubjectValue(ctx)
  159. if err != nil {
  160. return err
  161. }
  162. // 响应数据
  163. resp := param_v1.DustListResponse{}
  164. rpcReq := &v1.DustListRequest{
  165. PageSize:req.PageSize,
  166. Page:req.Page,
  167. ProjectId:projectId,
  168. }
  169. rpcRsp, err := pb.Project.DustList(ctx, rpcReq)
  170. if err != nil {
  171. s, _ := json.MarshalToString(req)
  172. logger.Error("func",
  173. zap.String("call", "pb.Project.DustList"),
  174. zap.String("params", s),
  175. zap.String("error", err.Error()))
  176. return errors.ErrorTransForm(err)
  177. }
  178. resp.Data = *rpcRsp
  179. if resp.Data.List == nil {
  180. resp.Data.List = []*v1.DustListItem{}
  181. }
  182. ctx.JSON(http.StatusOK, resp)
  183. return nil
  184. }
  185. // 执行任务
  186. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  187. }
  188. //
  189. // @Summary 扬尘aqi
  190. // @Description 扬尘aqi
  191. // @Tags 扬尘监控前台展示
  192. // @Accept json
  193. // @Produce json
  194. // @Param token header string true " "
  195. // @Param sn query string true " "
  196. // @Success 200 {object} v1.DustAqiResponse
  197. // @Failure 500 {object} base.HTTPError
  198. // @Router /api/v1/dust/aqi [get]
  199. func (c *Controller) DustAqi(ctx *gin.Context) {
  200. // 解析参数
  201. req := &param_v1.DustAqiRequest{}
  202. var loginUid int64
  203. var userName string
  204. parseParamTask := func() error {
  205. err := util.ShouldBind(ctx, nil, nil, &req.DustAqiQuery, nil)
  206. if err != nil {
  207. logger.Error("func",
  208. zap.String("call", "util.ShouldBind"),
  209. zap.String("error", err.Error()))
  210. return errors.ParamsError
  211. }
  212. loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
  213. return nil
  214. }
  215. // 业务处理
  216. handleServiceTask := func() error {
  217. // 响应数据
  218. resp := param_v1.DustAqiResponse{}
  219. rpcReq := &v1.DustAqiRequest{
  220. Sn:req.Sn,
  221. }
  222. rpcRsp, err := pb.Project.DustAqi(ctx, rpcReq)
  223. if err != nil {
  224. s, _ := json.MarshalToString(req)
  225. logger.Error("func",
  226. zap.String("call", "pb.Project.DustAqi"),
  227. zap.String("params", s),
  228. zap.String("error", err.Error()))
  229. return errors.ErrorTransForm(err)
  230. }
  231. resp.Data.Aqi = int(rpcRsp.Aqi)
  232. ctx.JSON(http.StatusOK, resp)
  233. return nil
  234. }
  235. // 执行任务
  236. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  237. }