wisdomGatewayController.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "iot_manager_service/app/dao"
  5. "iot_manager_service/app/model"
  6. "iot_manager_service/app/service"
  7. "iot_manager_service/app/utils"
  8. "math"
  9. "net/http"
  10. "strconv"
  11. )
  12. // 智慧网关基本信息管理对象
  13. var WisdomGateway = new(wisdomGatewayCtl)
  14. type wisdomGatewayCtl struct{}
  15. func (c *wisdomGatewayCtl) Detail(ctx *gin.Context) {
  16. id, e := strconv.Atoi(ctx.Query("id"))
  17. if e != nil {
  18. ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(e.Error(), nil))
  19. return
  20. }
  21. device, err := service.GatewayService.Get(id)
  22. if err != nil {
  23. ctx.JSON(http.StatusOK, err)
  24. return
  25. }
  26. ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, device))
  27. }
  28. func (c *wisdomGatewayCtl) List(ctx *gin.Context) {
  29. searchValue := ctx.Query("searchValue")
  30. current, _ := strconv.Atoi(ctx.Query("current"))
  31. size, _ := strconv.Atoi(ctx.Query("size"))
  32. if current == 0 {
  33. current = 1
  34. }
  35. if size <= 0 || size > 100 {
  36. size = 10
  37. }
  38. devices, err := service.GatewayService.List(searchValue, current, size)
  39. if err != nil {
  40. ctx.JSON(http.StatusOK, err)
  41. return
  42. }
  43. pages := math.Ceil(float64(len(devices)) / float64(size))
  44. rsp := model.RspGatewayList{
  45. Current: current,
  46. Size: size,
  47. Total: len(devices),
  48. Pages: int(pages),
  49. Records: devices,
  50. }
  51. ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, rsp))
  52. }
  53. func (c *wisdomGatewayCtl) CreateOrUpdate(ctx *gin.Context) {
  54. var req *model.GatewayDetail
  55. if err := ctx.ShouldBindJSON(&req); err != nil {
  56. ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(err.Error(), nil))
  57. return
  58. }
  59. err := service.GatewayService.CreateOrUpdate(req)
  60. ctx.JSON(http.StatusOK, err)
  61. }
  62. func (c *wisdomGatewayCtl) Remove(ctx *gin.Context) {
  63. var req *model.ReqGatewayRemove
  64. if err := ctx.ShouldBindJSON(&req); err != nil {
  65. ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(err.Error(), nil))
  66. return
  67. }
  68. err := service.GatewayService.Remove(req.IDs)
  69. if err != nil {
  70. ctx.JSON(http.StatusOK, err)
  71. return
  72. }
  73. ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, nil))
  74. }
  75. func (c *wisdomGatewayCtl) ImportExcel(ctx *gin.Context) {
  76. }
  77. func (c *wisdomGatewayCtl) ExportExcel(ctx *gin.Context) {
  78. }
  79. func (c *wisdomGatewayCtl) ExportTemplate(ctx *gin.Context) {
  80. }
  81. func (c *wisdomGatewayCtl) GetList(ctx *gin.Context) {
  82. devices, err := service.GatewayService.GetList()
  83. if err != nil {
  84. ctx.JSON(http.StatusOK, err)
  85. return
  86. }
  87. ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, devices))
  88. }
  89. func (c *wisdomGatewayCtl) GetRelevanceDetail(ctx *gin.Context) {
  90. id, e := strconv.Atoi(ctx.Query("id"))
  91. if e != nil {
  92. ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(e.Error(), nil))
  93. return
  94. }
  95. device, err := service.GatewayService.GetRelevanceDetail(id)
  96. if err != nil {
  97. ctx.JSON(http.StatusOK, err)
  98. return
  99. }
  100. ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, device))
  101. }
  102. func gatewayDaoToModel(device dao.Gateway) model.GatewayDetail {
  103. return model.GatewayDetail{
  104. Gateway: device,
  105. CountLampPole: 0,
  106. EndLineTime: "",
  107. NetworkState: "",
  108. RunState: "",
  109. Cpu: "",
  110. Memory: "",
  111. AlarmTerminalList: nil,
  112. CameraList: nil,
  113. CaptureUnitList: nil,
  114. InfoBoardList: nil,
  115. IpBroadcastList: nil,
  116. LightControlList: nil,
  117. ZigbeeList: nil,
  118. OptoSensorList: nil,
  119. }
  120. }