intelligentbuildingcontrol.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package handler
  2. import (
  3. "city_chips/internal/model"
  4. "city_chips/internal/service"
  5. "city_chips/pkg/helper/resp"
  6. "math/rand"
  7. "time"
  8. "github.com/spf13/viper"
  9. "github.com/gin-gonic/gin"
  10. )
  11. type IntelligentBuildingControlHandler struct {
  12. *Handler
  13. intelligentBuildingControlService service.IntelligentBuildingControlService
  14. conf *viper.Viper
  15. }
  16. func NewIntelligentBuildingControlHandler(
  17. handler *Handler,
  18. intelligentBuildingControlService service.IntelligentBuildingControlService,
  19. conf *viper.Viper,
  20. ) *IntelligentBuildingControlHandler {
  21. return &IntelligentBuildingControlHandler{
  22. Handler: handler,
  23. intelligentBuildingControlService: intelligentBuildingControlService,
  24. conf: conf,
  25. }
  26. }
  27. // 智能楼宇控制系统
  28. // 示例:创建一个报警记录
  29. func generateAlarm() model.AlarmList {
  30. return model.AlarmList{
  31. Id: rand.Intn(100),
  32. Name: model.GetRandomItem(model.DeviceNames),
  33. State: rand.Intn(2), // 假设0为正常,1为报警
  34. Date: time.Now().Format("2006-01-02 15:04:05"),
  35. Location: model.GetRandomItem(model.Locations),
  36. AlarmContent: model.GetRandomItem(model.AlarmContents),
  37. }
  38. }
  39. func (h *IntelligentBuildingControlHandler) GetIntelligentBuildingControl(ctx *gin.Context) {
  40. m := make(map[string]any)
  41. var device []model.AlarmList
  42. for i := 0; i < 20; i++ {
  43. alarm := generateAlarm()
  44. device = append(device, alarm)
  45. }
  46. count, err := h.intelligentBuildingControlService.DeviceCount()
  47. if err != nil {
  48. resp.HandleError(ctx, 1201, "查询设备总数失败", nil)
  49. return
  50. }
  51. m["DeviceCount"] = count //设备总数
  52. m["StopState"] = rand.Intn(100) //停止状态
  53. m["RunState"] = rand.Intn(100) //运行状态
  54. m["FaultState"] = rand.Intn(1000) //故障状态
  55. m["DeviceList"] = device //设备列表
  56. resp.HandleSuccess(ctx, m)
  57. }
  58. // GetPoint 获取点位数据
  59. func (h *IntelligentBuildingControlHandler) GetPoint(ctx *gin.Context) {
  60. pointName := ctx.PostForm("pointName")
  61. deviceType := ctx.PostForm("deviceType")
  62. building := ctx.PostForm("building")
  63. floor := ctx.PostForm("floor")
  64. section := ctx.PostForm("section")
  65. device_name := ctx.PostForm("deviceName")
  66. conds := make(map[string]any)
  67. if pointName != "" {
  68. conds["point_name"] = pointName
  69. }
  70. if deviceType != "" {
  71. conds["device_type"] = deviceType
  72. }
  73. if building != "" {
  74. conds["building"] = building
  75. }
  76. if floor != "" {
  77. conds["floor"] = floor
  78. }
  79. if section != "" {
  80. conds["section"] = section
  81. }
  82. if device_name != "" {
  83. conds["device_name"] = device_name
  84. }
  85. baseUrl := h.conf.GetString("obix.baseUrl")
  86. println("obix baseUrl:", baseUrl)
  87. points, err := h.intelligentBuildingControlService.GetPoint(conds)
  88. if err != nil {
  89. resp.HandleError(ctx, 1201, "查询点位失败", nil)
  90. return
  91. }
  92. resp.HandleSuccess(ctx, points)
  93. }
  94. // GetPointType 获取点位类型
  95. func (h *IntelligentBuildingControlHandler) GetPointType(ctx *gin.Context) {
  96. points, err := h.intelligentBuildingControlService.GetPointType()
  97. if err != nil {
  98. resp.HandleError(ctx, 1201, "查询点位类型失败", nil)
  99. return
  100. }
  101. resp.HandleSuccess(ctx, points)
  102. }
  103. func (h *IntelligentBuildingControlHandler) GetDeviceType(ctx *gin.Context) {
  104. points, err := h.intelligentBuildingControlService.GetPointType()
  105. if err != nil {
  106. resp.HandleError(ctx, 1201, "查询设备类型失败", nil)
  107. return
  108. }
  109. m := make(map[string]string)
  110. for _, v := range points {
  111. m[v] = model.DeviceType[v]
  112. }
  113. resp.HandleSuccess(ctx, m)
  114. }