package handler import ( "city_chips/internal/model" "city_chips/internal/service" "city_chips/pkg/helper/resp" "encoding/json" "fmt" "github.com/gin-gonic/gin" "github.com/spf13/viper" "math/rand" "strconv" "time" ) type AccessControlHandler struct { *Handler accessControlService service.AccessControlService conf *viper.Viper } func NewAccessControlHandler(handler *Handler, accessControlService service.AccessControlService, conf *viper.Viper) *AccessControlHandler { return &AccessControlHandler{ Handler: handler, accessControlService: accessControlService, conf: conf, } } // GetAccessControl 出入口控制 func (h *AccessControlHandler) GetAccessControl(ctx *gin.Context) { m := make(map[string]any) var deviceRanking []model.DeviceRanking var device []model.Device for i := 0; i < 10; i++ { name := fmt.Sprintf("设备名称%v", i+1) ranking := model.DeviceRanking{ Id: i + 1, DeviceName: name, Value: rand.Intn(100), } deviceRanking = append(deviceRanking, ranking) } for i := 0; i < 30; i++ { m2 := model.Device{ Id: i + 1, Car: model.GenerateLicensePlate(), Name: model.GetRandomItem(model.EntranceExitDeviceNames), State: rand.Intn(2), Date: time.Now().Format("2006-01-02 15:04:05"), } device = append(device, m2) } m["Online"] = rand.Intn(100) //在线 m["Offline"] = rand.Intn(100) //离线 m["DeviceRanking"] = deviceRanking //设备排名 m["Out"] = rand.Intn(100) //出 m["Into"] = rand.Intn(100) //入 m["DeviceList"] = device //设备列表 resp.HandleSuccess(ctx, m) } // GetParkingLotInfo 查询车场信息 func (h *AccessControlHandler) GetParkingLotInfo(ctx *gin.Context) { // 设置响应头 ctx.Header("Content-Type", "text/event-stream") ctx.Header("Cache-Control", "no-cache") ctx.Header("Connection", "keep-alive") // 监听客户端断开连接 conn := true notify := ctx.Writer.CloseNotify() type Response struct { RequestId string `protobuf:"bytes,1,opt,name=requestId,proto3" json:"requestId,omitempty"` Code int32 `protobuf:"varint,2,opt,name=code,proto3" json:"code,omitempty"` Msg string `protobuf:"bytes,3,opt,name=msg,proto3" json:"msg,omitempty"` Data any `json:"data"` } stationNo := ctx.Query("stationNo") //GetParkingLotInfo := h.conf.GetString("lifang.baseurl") + h.conf.GetString("lifang.GetParkingLotInfo") m := make(map[string]any) m["stationNo"] = stationNo moni := make(map[string]any) moni["stationNo"] = stationNo var response Response for conn { select { case <-notify: conn = false fmt.Println("断开连接") return default: //request, err := h.accessControlService.SendRequest(http.DefaultClient, "POST", GetParkingLotInfo, m) //if err != nil { // resp.HandleError(ctx, 1203, "获取停车场信息失败", err) // return //} var parkingLot model.ParkingLot //if gjson.Get(string(request), "resCode").Int() == 0 { // err = json.Unmarshal(request, &parkingLot) // if err != nil { // h.logger.Error("json序列化失败") // response.Code = 1203 // response.Msg = "json序列化失败" // response.Data = nil // res, _ := json.Marshal(&response) // fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res)) // ctx.Writer.Flush() // return // } //} else { // response.Code = 1203 // response.Msg = "获取停车场信息失败" // response.Data = string(request) // res, _ := json.Marshal(&response) // fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res)) // ctx.Writer.Flush() // return //} parkingLot.ResCode = 0 parkingLot.ResMsg = "查询成功" parkingLot.TotalNum = rand.Intn(100) parkingLot.TotalStopNum = rand.Intn(100) parkingLot.TotalRemainNum = rand.Intn(100) parkingLot.ParkID = strconv.Itoa(rand.Intn(100)) parkingLot.ParkName = "测试停车场" parkingLot.ChargeRuleDesc = "2元/小时" for i := 0; i < 3; i++ { sprintf := fmt.Sprintf("设备%v", i+1) info := model.ParkingLotInfo{ ParkingLotId: rand.Intn(100), ParkingLotName: sprintf, TotalNum: rand.Intn(100), TotalStopNum: rand.Intn(100), TotalRemainNum: rand.Intn(100), } parkingLot.ParkingLotInfo = append(parkingLot.ParkingLotInfo, info) } response.Code = 200 response.Msg = "查询成功" response.Data = parkingLot res, _ := json.Marshal(&response) fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res)) ctx.Writer.Flush() time.Sleep(10 * time.Second) } } }