intelligentbuildingcontrol.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. m["DeviceCount"] = rand.Intn(100) //设备总数
  47. m["StopState"] = rand.Intn(100) //停止状态
  48. m["RunState"] = rand.Intn(100) //运行状态
  49. m["FaultState"] = rand.Intn(1000) //故障状态
  50. m["DeviceList"] = device //设备列表
  51. resp.HandleSuccess(ctx, m)
  52. }
  53. // 获取点位数据
  54. func (h *IntelligentBuildingControlHandler) GetPoint(ctx *gin.Context) {
  55. pointName := ctx.PostForm("pointName")
  56. deviceType := ctx.PostForm("deviceType")
  57. building := ctx.PostForm("building")
  58. floor := ctx.PostForm("floor")
  59. section := ctx.PostForm("section")
  60. device_name := ctx.PostForm("deviceName")
  61. conds := make(map[string]any)
  62. if pointName != "" {
  63. conds["point_name"] = pointName
  64. }
  65. if deviceType != "" {
  66. conds["device_type"] = deviceType
  67. }
  68. if building != "" {
  69. conds["building"] = building
  70. }
  71. if floor != "" {
  72. conds["floor"] = floor
  73. }
  74. if section != "" {
  75. conds["section"] = section
  76. }
  77. if device_name != "" {
  78. conds["device_name"] = device_name
  79. }
  80. baseUrl := h.conf.GetString("obix.baseUrl")
  81. println("obix baseUrl:", baseUrl)
  82. points, err := h.intelligentBuildingControlService.GetPoint(conds)
  83. if err != nil {
  84. resp.HandleError(ctx, 1201, "查询点位失败", nil)
  85. return
  86. }
  87. resp.HandleSuccess(ctx, points)
  88. }
  89. // 获取点位类型
  90. func (h *IntelligentBuildingControlHandler) GetPointType(ctx *gin.Context) {
  91. points, err := h.intelligentBuildingControlService.GetPointType()
  92. if err != nil {
  93. resp.HandleError(ctx, 1201, "查询点位类型失败", nil)
  94. return
  95. }
  96. resp.HandleSuccess(ctx, points)
  97. }