浏览代码

楼控设备多设备处理

huangyan 3 周之前
父节点
当前提交
9d461417c0
共有 2 个文件被更改,包括 52 次插入7 次删除
  1. 51 7
      internal/handler/intelligentbuildingcontrol.go
  2. 1 0
      internal/server/http.go

+ 51 - 7
internal/handler/intelligentbuildingcontrol.go

@@ -5,7 +5,10 @@ import (
 	"city_chips/internal/service"
 	"city_chips/pkg/helper/resp"
 	"fmt"
+	"go.uber.org/zap"
+	"io"
 	"math/rand"
+	"net/http"
 	"time"
 
 	"github.com/spf13/viper"
@@ -92,12 +95,53 @@ func (h *IntelligentBuildingControlHandler) GetPoint(ctx *gin.Context) {
 		conds["device_name"] = device_name
 	}
 	baseUrl := h.conf.GetString("obix.baseUrl")
-	println("obix baseUrl:", baseUrl)
 	points, err := h.intelligentBuildingControlService.GetPoint(conds)
 	if err != nil {
 		resp.HandleError(ctx, 1201, "查询点位失败", nil)
 		return
 	}
+	type result struct {
+		Body string
+		Err  error
+	}
+	resultChan := make(chan result, len(*points))
+	ctxReq := ctx.Request.Context()
+	//设置最大并发数
+	sem := make(chan struct{}, 10)
+	for _, v := range *points {
+		url := baseUrl + v.FullPath
+		sem <- struct{}{}
+		go func(url string) {
+			defer func() { <-sem }()
+			req, err := http.NewRequestWithContext(ctxReq, "GET", url, nil)
+			get, err := http.DefaultClient.Do(req)
+			if err != nil {
+				resultChan <- result{Err: err}
+				return
+			}
+			defer get.Body.Close()
+			body, err := io.ReadAll(get.Body)
+			if err != nil {
+				resultChan <- result{Err: err}
+				return
+			}
+			fmt.Println(string(body))
+			resultChan <- result{Body: string(body)}
+		}(url)
+	}
+	// 等待所有 goroutine 完成
+	go func() {
+		// 等待所有结果
+		for i := 0; i < cap(resultChan); i++ {
+			r := <-resultChan
+			if r.Err != nil {
+				h.logger.Error("获取设备数据失败", zap.Error(r.Err))
+				continue
+			}
+			//可以处理各个设备响应的数据并且处理
+			fmt.Println(r.Body)
+		}
+	}()
 	resp.HandleSuccess(ctx, points)
 }
 
@@ -111,12 +155,12 @@ func (h *IntelligentBuildingControlHandler) GetPointType(ctx *gin.Context) {
 	//	<real name="out" val="27.49811553955078" display="27.50 °C {ok}"/>
 	//	<ref name="tag" display="Point Tag"/>
 	//</real>`
-	err2 := ctx.ShouldBindXML(&tempS1)
+	//err2 := ctx.ShouldBindXML(&tempS1)
 	//err2 := xml.Unmarshal([]byte(xmlData), &tempS1)
-	if err2 != nil {
-		resp.HandleError(ctx, 1201, "绑定XML失败", nil)
-		return
-	}
+	//if err2 != nil {
+	//	resp.HandleError(ctx, 1201, "绑定XML失败", nil)
+	//	return
+	//}
 	fmt.Println(tempS1)
 	points, err := h.intelligentBuildingControlService.GetPointType()
 	if err != nil {
@@ -126,7 +170,7 @@ func (h *IntelligentBuildingControlHandler) GetPointType(ctx *gin.Context) {
 	resp.HandleSuccess(ctx, points)
 }
 func (h *IntelligentBuildingControlHandler) GetDeviceType(ctx *gin.Context) {
-	points, err := h.intelligentBuildingControlService.GetPointType()
+	points, err := h.intelligentBuildingControlService.DeviceType()
 	if err != nil {
 		resp.HandleError(ctx, 1201, "查询设备类型失败", nil)
 		return

+ 1 - 0
internal/server/http.go

@@ -115,6 +115,7 @@ func NewServerHTTP(
 		inte.GET("/count", intell.GetIntelligentBuildingControl)
 		inte.POST("/point", intell.GetPoint)
 		inte.GET("/pointType", intell.GetPointType)
+		inte.GET("/deviceType", intell.GetDeviceType)
 	}
 	return r
 }