|
@@ -115,3 +115,91 @@ func (h *EnergyHandler) GetWaterMmeter(ctx *gin.Context) {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+// 电表数据
|
|
|
+func (h *EnergyHandler) GetAmmeter(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 = strconv.Itoa(rand.Intn(1000))
|
|
|
+ response.Msg = "success"
|
|
|
+ data["今日用电量"] = rand.Intn(1000)
|
|
|
+ data["昨日用电量"] = rand.Intn(1000)
|
|
|
+ data["本月用水量"] = rand.Intn(1000)
|
|
|
+ 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)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+func (h *EnergyHandler) GetTemperature(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 = strconv.Itoa(rand.Intn(1000))
|
|
|
+ response.Msg = "success"
|
|
|
+ data["温度"] = rand.Intn(100)
|
|
|
+ data["湿度"] = rand.Intn(100)
|
|
|
+ data["时间"] = time.Now().Format("2006-01-02 15:04:05")
|
|
|
+ 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)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|