123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- 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)
- }
- }
- }
- // GetDriveway 获取车位
- func (h *AccessControlHandler) GetDriveway(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("id")
- if len(stationNo) == 0 {
- resp.HandleError(ctx, 1201, "缺少必要参数", nil)
- return
- }
- var response Response
- for conn {
- select {
- case <-notify:
- conn = false
- fmt.Println("断开连接")
- return
- default:
- // 模拟数据
- data := make(map[string]any)
- response.Code = 200
- response.RequestId = stationNo
- response.Msg = "success"
- data["state"] = rand.Intn(2) // // 1开 0关 2故障
- data["车位编号"] = model.GetRandomItem(model.Locations)
- response.Data = data
- res, _ := json.Marshal(&response)
- fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
- ctx.Writer.Flush()
- time.Sleep(10 * time.Second)
- }
- }
- }
|