123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- package handler
- import (
- "city_chips/internal/model"
- "city_chips/internal/service"
- "city_chips/pkg/helper/resp"
- "encoding/json"
- "fmt"
- "math/rand"
- "time"
- "github.com/gin-gonic/gin"
- "github.com/spf13/viper"
- )
- type ElevatorHandler struct {
- *Handler
- elevatorService service.ElevatorService
- conf *viper.Viper
- }
- func NewElevatorHandler(handler *Handler, elevatorService service.ElevatorService, conf *viper.Viper) *ElevatorHandler {
- return &ElevatorHandler{
- Handler: handler,
- elevatorService: elevatorService,
- conf: conf,
- }
- }
- // GetElevator 获取电梯系统数据
- func (h *ElevatorHandler) GetElevator(ctx *gin.Context) {
- m := make(map[string]any)
- RunState := make(map[string]any)
- Overload := make(map[string]any)
- TrappedPerson := make(map[string]any)
- Abnormal := make(map[string]any)
- m["ElevatorCount"] = rand.Intn(1000) //电梯总量
- m["RunningNormally"] = rand.Intn(1000) //运行正常
- m["RunningAbnormal"] = rand.Intn(1000) //运行异常
- m["OverloadWarning"] = rand.Intn(1000) //超载报警
- m["TrappedPersonAlarm"] = rand.Intn(1000) //困人报警
- m["AbnormalVibration"] = rand.Intn(1000) //异常震动
- RunState["2025-5-1"] = rand.Intn(1000)
- RunState["2025-5-2"] = rand.Intn(1000)
- RunState["2025-5-3"] = rand.Intn(1000)
- RunState["2025-5-4"] = rand.Intn(1000)
- RunState["2025-5-5"] = rand.Intn(1000)
- RunState["2025-5-6"] = rand.Intn(1000)
- RunState["2025-5-7"] = rand.Intn(1000)
- Overload["2025-5-1"] = rand.Intn(1000)
- Overload["2025-5-2"] = rand.Intn(1000)
- Overload["2025-5-3"] = rand.Intn(1000)
- Overload["2025-5-4"] = rand.Intn(1000)
- Overload["2025-5-5"] = rand.Intn(1000)
- Overload["2025-5-6"] = rand.Intn(1000)
- Overload["2025-5-7"] = rand.Intn(1000)
- TrappedPerson["2025-5-1"] = rand.Intn(1000)
- TrappedPerson["2025-5-2"] = rand.Intn(1000)
- TrappedPerson["2025-5-3"] = rand.Intn(1000)
- TrappedPerson["2025-5-4"] = rand.Intn(1000)
- TrappedPerson["2025-5-5"] = rand.Intn(1000)
- TrappedPerson["2025-5-6"] = rand.Intn(1000)
- TrappedPerson["2025-5-7"] = rand.Intn(1000)
- Abnormal["2025-5-1"] = rand.Intn(1000)
- Abnormal["2025-5-2"] = rand.Intn(1000)
- Abnormal["2025-5-3"] = rand.Intn(1000)
- Abnormal["2025-5-4"] = rand.Intn(1000)
- Abnormal["2025-5-5"] = rand.Intn(1000)
- Abnormal["2025-5-6"] = rand.Intn(1000)
- Abnormal["2025-5-7"] = rand.Intn(1000)
- m["OverloadCount"] = Overload //超载预警
- m["RunState"] = RunState //运行状态
- m["AbnormalCount"] = Abnormal //异常告警
- m["TrappedPerson"] = TrappedPerson //困人报警
- resp.HandleSuccess(ctx, m)
- }
- // GetElevatorData 获取电梯数据
- func (h *ElevatorHandler) GetElevatorData(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.ElevatorFloors)
- data["电梯名称"] = model.GetRandomItem(model.ElevatorDeviceNames)
- 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)
- }
- }
- }
|