accesscontrol.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. "github.com/gin-gonic/gin"
  9. "github.com/spf13/viper"
  10. "math/rand"
  11. "strconv"
  12. "time"
  13. )
  14. type AccessControlHandler struct {
  15. *Handler
  16. accessControlService service.AccessControlService
  17. conf *viper.Viper
  18. }
  19. func NewAccessControlHandler(handler *Handler, accessControlService service.AccessControlService, conf *viper.Viper) *AccessControlHandler {
  20. return &AccessControlHandler{
  21. Handler: handler,
  22. accessControlService: accessControlService,
  23. conf: conf,
  24. }
  25. }
  26. // GetAccessControl 出入口控制
  27. func (h *AccessControlHandler) GetAccessControl(ctx *gin.Context) {
  28. m := make(map[string]any)
  29. var deviceRanking []model.DeviceRanking
  30. var device []model.Device
  31. for i := 0; i < 10; i++ {
  32. name := fmt.Sprintf("设备名称%v", i+1)
  33. ranking := model.DeviceRanking{
  34. Id: i + 1,
  35. DeviceName: name,
  36. Value: rand.Intn(100),
  37. }
  38. deviceRanking = append(deviceRanking, ranking)
  39. }
  40. for i := 0; i < 10; i++ {
  41. name := fmt.Sprintf("设备%v", i+1)
  42. m2 := model.Device{
  43. Id: i + 1,
  44. Name: name,
  45. State: rand.Intn(2),
  46. Date: time.Now().Format("2006-01-02 15:04:05"),
  47. }
  48. device = append(device, m2)
  49. }
  50. m["Online"] = rand.Intn(100) //在线
  51. m["Offline"] = rand.Intn(100) //离线
  52. m["DeviceRanking"] = deviceRanking //设备排名
  53. m["Out"] = rand.Intn(100) //出
  54. m["Into"] = rand.Intn(100) //入
  55. m["DeviceList"] = device //设备列表
  56. resp.HandleSuccess(ctx, m)
  57. }
  58. // GetParkingLotInfo 查询车场信息
  59. func (h *AccessControlHandler) GetParkingLotInfo(ctx *gin.Context) {
  60. // 设置响应头
  61. ctx.Header("Content-Type", "text/event-stream")
  62. ctx.Header("Cache-Control", "no-cache")
  63. ctx.Header("Connection", "keep-alive")
  64. // 监听客户端断开连接
  65. conn := true
  66. notify := ctx.Writer.CloseNotify()
  67. type Response struct {
  68. RequestId string `protobuf:"bytes,1,opt,name=requestId,proto3" json:"requestId,omitempty"`
  69. Code int32 `protobuf:"varint,2,opt,name=code,proto3" json:"code,omitempty"`
  70. Msg string `protobuf:"bytes,3,opt,name=msg,proto3" json:"msg,omitempty"`
  71. Data any `json:"data"`
  72. }
  73. stationNo := ctx.Query("stationNo")
  74. //GetParkingLotInfo := h.conf.GetString("lifang.baseurl") + h.conf.GetString("lifang.GetParkingLotInfo")
  75. m := make(map[string]any)
  76. m["stationNo"] = stationNo
  77. moni := make(map[string]any)
  78. moni["stationNo"] = stationNo
  79. var response Response
  80. for conn {
  81. select {
  82. case <-notify:
  83. conn = false
  84. fmt.Println("断开连接")
  85. return
  86. default:
  87. //request, err := h.accessControlService.SendRequest(http.DefaultClient, "POST", GetParkingLotInfo, m)
  88. //if err != nil {
  89. // resp.HandleError(ctx, 1203, "获取停车场信息失败", err)
  90. // return
  91. //}
  92. var parkingLot model.ParkingLot
  93. //if gjson.Get(string(request), "resCode").Int() == 0 {
  94. // err = json.Unmarshal(request, &parkingLot)
  95. // if err != nil {
  96. // h.logger.Error("json序列化失败")
  97. // response.Code = 1203
  98. // response.Msg = "json序列化失败"
  99. // response.Data = nil
  100. // res, _ := json.Marshal(&response)
  101. // fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  102. // ctx.Writer.Flush()
  103. // return
  104. // }
  105. //} else {
  106. // response.Code = 1203
  107. // response.Msg = "获取停车场信息失败"
  108. // response.Data = string(request)
  109. // res, _ := json.Marshal(&response)
  110. // fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  111. // ctx.Writer.Flush()
  112. // return
  113. //}
  114. parkingLot.ResCode = 0
  115. parkingLot.ResMsg = "查询成功"
  116. parkingLot.TotalNum = rand.Intn(100)
  117. parkingLot.TotalStopNum = rand.Intn(100)
  118. parkingLot.TotalRemainNum = rand.Intn(100)
  119. parkingLot.ParkID = strconv.Itoa(rand.Intn(100))
  120. parkingLot.ParkName = "测试停车场"
  121. parkingLot.ChargeRuleDesc = "2元/小时"
  122. for i := 0; i < 3; i++ {
  123. sprintf := fmt.Sprintf("设备%v", i+1)
  124. info := model.ParkingLotInfo{
  125. ParkingLotId: rand.Intn(100),
  126. ParkingLotName: sprintf,
  127. TotalNum: rand.Intn(100),
  128. TotalStopNum: rand.Intn(100),
  129. TotalRemainNum: rand.Intn(100),
  130. }
  131. parkingLot.ParkingLotInfo = append(parkingLot.ParkingLotInfo, info)
  132. }
  133. response.Code = 200
  134. response.Msg = "查询成功"
  135. response.Data = parkingLot
  136. res, _ := json.Marshal(&response)
  137. fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  138. ctx.Writer.Flush()
  139. time.Sleep(10 * time.Second)
  140. }
  141. }
  142. }