elevator.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package handler
  2. import (
  3. "city_chips/internal/model"
  4. "city_chips/internal/service"
  5. "city_chips/pkg/helper/resp"
  6. "encoding/json"
  7. "fmt"
  8. "math/rand"
  9. "time"
  10. "github.com/gin-gonic/gin"
  11. "github.com/spf13/viper"
  12. )
  13. type ElevatorHandler struct {
  14. *Handler
  15. elevatorService service.ElevatorService
  16. conf *viper.Viper
  17. }
  18. func NewElevatorHandler(handler *Handler, elevatorService service.ElevatorService, conf *viper.Viper) *ElevatorHandler {
  19. return &ElevatorHandler{
  20. Handler: handler,
  21. elevatorService: elevatorService,
  22. conf: conf,
  23. }
  24. }
  25. // GetElevator 获取电梯系统数据
  26. func (h *ElevatorHandler) GetElevator(ctx *gin.Context) {
  27. m := make(map[string]any)
  28. RunState := make(map[string]any)
  29. Overload := make(map[string]any)
  30. TrappedPerson := make(map[string]any)
  31. Abnormal := make(map[string]any)
  32. m["ElevatorCount"] = rand.Intn(1000) //电梯总量
  33. m["RunningNormally"] = rand.Intn(1000) //运行正常
  34. m["RunningAbnormal"] = rand.Intn(1000) //运行异常
  35. m["OverloadWarning"] = rand.Intn(1000) //超载报警
  36. m["TrappedPersonAlarm"] = rand.Intn(1000) //困人报警
  37. m["AbnormalVibration"] = rand.Intn(1000) //异常震动
  38. RunState["2025-5-1"] = rand.Intn(1000)
  39. RunState["2025-5-2"] = rand.Intn(1000)
  40. RunState["2025-5-3"] = rand.Intn(1000)
  41. RunState["2025-5-4"] = rand.Intn(1000)
  42. RunState["2025-5-5"] = rand.Intn(1000)
  43. RunState["2025-5-6"] = rand.Intn(1000)
  44. RunState["2025-5-7"] = rand.Intn(1000)
  45. Overload["2025-5-1"] = rand.Intn(1000)
  46. Overload["2025-5-2"] = rand.Intn(1000)
  47. Overload["2025-5-3"] = rand.Intn(1000)
  48. Overload["2025-5-4"] = rand.Intn(1000)
  49. Overload["2025-5-5"] = rand.Intn(1000)
  50. Overload["2025-5-6"] = rand.Intn(1000)
  51. Overload["2025-5-7"] = rand.Intn(1000)
  52. TrappedPerson["2025-5-1"] = rand.Intn(1000)
  53. TrappedPerson["2025-5-2"] = rand.Intn(1000)
  54. TrappedPerson["2025-5-3"] = rand.Intn(1000)
  55. TrappedPerson["2025-5-4"] = rand.Intn(1000)
  56. TrappedPerson["2025-5-5"] = rand.Intn(1000)
  57. TrappedPerson["2025-5-6"] = rand.Intn(1000)
  58. TrappedPerson["2025-5-7"] = rand.Intn(1000)
  59. Abnormal["2025-5-1"] = rand.Intn(1000)
  60. Abnormal["2025-5-2"] = rand.Intn(1000)
  61. Abnormal["2025-5-3"] = rand.Intn(1000)
  62. Abnormal["2025-5-4"] = rand.Intn(1000)
  63. Abnormal["2025-5-5"] = rand.Intn(1000)
  64. Abnormal["2025-5-6"] = rand.Intn(1000)
  65. Abnormal["2025-5-7"] = rand.Intn(1000)
  66. m["OverloadCount"] = Overload //超载预警
  67. m["RunState"] = RunState //运行状态
  68. m["AbnormalCount"] = Abnormal //异常告警
  69. m["TrappedPerson"] = TrappedPerson //困人报警
  70. resp.HandleSuccess(ctx, m)
  71. }
  72. // GetElevatorData 获取电梯数据
  73. func (h *ElevatorHandler) GetElevatorData(ctx *gin.Context) {
  74. // 设置响应头
  75. ctx.Header("Content-Type", "text/event-stream")
  76. ctx.Header("Cache-Control", "no-cache")
  77. ctx.Header("Connection", "keep-alive")
  78. // 监听客户端断开连接
  79. conn := true
  80. notify := ctx.Writer.CloseNotify()
  81. type Response struct {
  82. RequestId string `protobuf:"bytes,1,opt,name=requestId,proto3" json:"requestId,omitempty"`
  83. Code int32 `protobuf:"varint,2,opt,name=code,proto3" json:"code,omitempty"`
  84. Msg string `protobuf:"bytes,3,opt,name=msg,proto3" json:"msg,omitempty"`
  85. Data any `json:"data"`
  86. }
  87. stationNo := ctx.Query("id")
  88. if len(stationNo) == 0 {
  89. resp.HandleError(ctx, 1201, "缺少必要参数", nil)
  90. return
  91. }
  92. var response Response
  93. for conn {
  94. select {
  95. case <-notify:
  96. conn = false
  97. fmt.Println("断开连接")
  98. return
  99. default:
  100. // 模拟数据
  101. data := make(map[string]any)
  102. response.Code = 200
  103. response.RequestId = stationNo
  104. response.Msg = "success"
  105. data["state"] = rand.Intn(2) // // 1开 0关 2故障
  106. data["楼层"] = model.GetRandomItem(model.ElevatorFloors)
  107. data["电梯名称"] = model.GetRandomItem(model.ElevatorDeviceNames)
  108. response.Data = data
  109. res, _ := json.Marshal(&response)
  110. fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  111. ctx.Writer.Flush()
  112. time.Sleep(10 * time.Second)
  113. }
  114. }
  115. }