intelligentbuildingcontrol.go 4.1 KB

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