|
@@ -9,8 +9,10 @@ import (
|
|
"fmt"
|
|
"fmt"
|
|
"go.uber.org/zap"
|
|
"go.uber.org/zap"
|
|
"math/rand"
|
|
"math/rand"
|
|
|
|
+ "net/http"
|
|
"regexp"
|
|
"regexp"
|
|
- "sync"
|
|
|
|
|
|
+ "strconv"
|
|
|
|
+ "strings"
|
|
"time"
|
|
"time"
|
|
|
|
|
|
"github.com/spf13/viper"
|
|
"github.com/spf13/viper"
|
|
@@ -76,8 +78,10 @@ func (h *IntelligentBuildingControlHandler) GetPoint(ctx *gin.Context) {
|
|
floor := ctx.PostForm("floor")
|
|
floor := ctx.PostForm("floor")
|
|
section := ctx.PostForm("section")
|
|
section := ctx.PostForm("section")
|
|
device_name := ctx.PostForm("deviceName")
|
|
device_name := ctx.PostForm("deviceName")
|
|
|
|
+ pageNum, err := strconv.Atoi(ctx.PostForm("pageNum"))
|
|
|
|
+ pageSize, err := strconv.Atoi(ctx.PostForm("pageSize"))
|
|
conds := make(map[string]any)
|
|
conds := make(map[string]any)
|
|
- var pointType []model.PointType
|
|
|
|
|
|
+ //var pointType []model.PointType
|
|
if pointName != "" {
|
|
if pointName != "" {
|
|
conds["point_name"] = pointName
|
|
conds["point_name"] = pointName
|
|
}
|
|
}
|
|
@@ -97,56 +101,126 @@ func (h *IntelligentBuildingControlHandler) GetPoint(ctx *gin.Context) {
|
|
conds["device_name"] = device_name
|
|
conds["device_name"] = device_name
|
|
}
|
|
}
|
|
|
|
|
|
- baseUrl := h.conf.GetString("obix.baseUrl")
|
|
|
|
- points, err := h.intelligentBuildingControlService.GetPoint(conds)
|
|
|
|
|
|
+ //baseUrl := h.conf.GetString("obix.baseUrl")
|
|
|
|
+ point, total, err := h.intelligentBuildingControlService.GetPoint(conds, pageNum, pageSize)
|
|
if err != nil {
|
|
if err != nil {
|
|
resp.HandleError(ctx, 1201, "查询点位失败", nil)
|
|
resp.HandleError(ctx, 1201, "查询点位失败", nil)
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
+ //
|
|
|
|
+ //var wg sync.WaitGroup
|
|
|
|
+ //var mutex sync.Mutex // 保护切片并发写入
|
|
|
|
+ //m := make(map[string]string)
|
|
|
|
+ //sem := make(chan struct{}, 10) // 最大并发数为10
|
|
|
|
+ //
|
|
|
|
+ //for _, v := range *points {
|
|
|
|
+ // url := baseUrl + v.FullPath
|
|
|
|
+ // wg.Add(1)
|
|
|
|
+ // sem <- struct{}{}
|
|
|
|
+ // go func(url string, pointName string) {
|
|
|
|
+ // defer func() {
|
|
|
|
+ // <-sem
|
|
|
|
+ // wg.Done()
|
|
|
|
+ // }()
|
|
|
|
+ //
|
|
|
|
+ // request, err := obix.SendSecureRequest(url, h.conf.GetString("obix.username"), h.conf.GetString("obix.password"))
|
|
|
|
+ // if err != nil {
|
|
|
|
+ // h.logger.Error("发送请求失败", zap.Error(err))
|
|
|
|
+ // return
|
|
|
|
+ // }
|
|
|
|
+ //
|
|
|
|
+ // re := regexp.MustCompile(`val="([^"]+)"`)
|
|
|
|
+ // matches := re.FindStringSubmatch(request)
|
|
|
|
+ //
|
|
|
|
+ // mutex.Lock()
|
|
|
|
+ // defer mutex.Unlock()
|
|
|
|
+ //
|
|
|
|
+ // if len(matches) > 1 {
|
|
|
|
+ // s := model.PointName[pointName]
|
|
|
|
+ // m[s] = matches[1]
|
|
|
|
+ // pointType = append(pointType, model.PointType{Type: m})
|
|
|
|
+ // } else {
|
|
|
|
+ // h.logger.Warn("未找到 val 值", zap.String("url", url))
|
|
|
|
+ // }
|
|
|
|
+ // }(url, v.PointName)
|
|
|
|
+ //}
|
|
|
|
+ //
|
|
|
|
+ //// 等待所有协程完成
|
|
|
|
+ //wg.Wait()
|
|
|
|
|
|
- var wg sync.WaitGroup
|
|
|
|
- var mutex sync.Mutex // 保护切片并发写入
|
|
|
|
- m := make(map[string]string)
|
|
|
|
- sem := make(chan struct{}, 10) // 最大并发数为10
|
|
|
|
|
|
+ // 统一返回结果
|
|
|
|
+ resp.PageHandleSuccess(ctx, point, total, pageNum, pageSize)
|
|
|
|
+}
|
|
|
|
|
|
|
|
+// GetGetPoint 获取点位设备数据
|
|
|
|
+func (h *IntelligentBuildingControlHandler) GetGetPoint(ctx *gin.Context) {
|
|
|
|
+ pointName := ctx.Query("pointName")
|
|
|
|
+ deviceType := ctx.Query("deviceType")
|
|
|
|
+ building := ctx.Query("building")
|
|
|
|
+ floor := ctx.Query("floor")
|
|
|
|
+ section := ctx.Query("section")
|
|
|
|
+ device_name := ctx.Query("deviceName")
|
|
|
|
+ conds := make(map[string]any)
|
|
|
|
+ if pointName != "" {
|
|
|
|
+ conds["point_name"] = pointName
|
|
|
|
+ }
|
|
|
|
+ if deviceType != "" {
|
|
|
|
+ conds["device_type"] = deviceType
|
|
|
|
+ }
|
|
|
|
+ if building != "" {
|
|
|
|
+ conds["building"] = building
|
|
|
|
+ }
|
|
|
|
+ if floor != "" {
|
|
|
|
+ conds["floor"] = floor
|
|
|
|
+ }
|
|
|
|
+ if section != "" {
|
|
|
|
+ conds["section"] = section
|
|
|
|
+ }
|
|
|
|
+ if device_name != "" {
|
|
|
|
+ conds["device_name"] = device_name
|
|
|
|
+ }
|
|
|
|
+ baseUrl := h.conf.GetString("obix.baseUrl")
|
|
|
|
+ m := make(map[string]any)
|
|
|
|
+ points, _, err := h.intelligentBuildingControlService.GetPoint(conds, 1, 100)
|
|
|
|
+ if err != nil {
|
|
|
|
+ resp.HandleError(ctx, 1201, "查询点位失败", nil)
|
|
|
|
+ }
|
|
for _, v := range *points {
|
|
for _, v := range *points {
|
|
url := baseUrl + v.FullPath
|
|
url := baseUrl + v.FullPath
|
|
- wg.Add(1)
|
|
|
|
- sem <- struct{}{}
|
|
|
|
- go func(url string, pointName string) {
|
|
|
|
- defer func() {
|
|
|
|
- <-sem
|
|
|
|
- wg.Done()
|
|
|
|
- }()
|
|
|
|
-
|
|
|
|
- request, err := obix.SendSecureRequest(url, h.conf.GetString("obix.username"), h.conf.GetString("obix.password"))
|
|
|
|
- if err != nil {
|
|
|
|
- h.logger.Error("发送请求失败", zap.Error(err))
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- re := regexp.MustCompile(`val="([^"]+)"`)
|
|
|
|
- matches := re.FindStringSubmatch(request)
|
|
|
|
-
|
|
|
|
- mutex.Lock()
|
|
|
|
- defer mutex.Unlock()
|
|
|
|
-
|
|
|
|
- if len(matches) > 1 {
|
|
|
|
- s := model.PointName[pointName]
|
|
|
|
- m[s] = matches[1]
|
|
|
|
- pointType = append(pointType, model.PointType{Type: m})
|
|
|
|
- } else {
|
|
|
|
- h.logger.Warn("未找到 val 值", zap.String("url", url))
|
|
|
|
|
|
+ request, err := obix.SendSecureRequest(url, h.conf.GetString("obix.username"), h.conf.GetString("obix.password"))
|
|
|
|
+ if err != nil {
|
|
|
|
+ h.logger.Error("发送请求失败", zap.Error(err))
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ re := regexp.MustCompile(`val="([^"]+)"`)
|
|
|
|
+ matches := re.FindStringSubmatch(request)
|
|
|
|
+ if len(matches) > 1 {
|
|
|
|
+ s := model.PointName[v.PointName]
|
|
|
|
+ if s != "" {
|
|
|
|
+ value := matches[1]
|
|
|
|
+ switch val := obix.DetectType(value).(type) {
|
|
|
|
+ case int:
|
|
|
|
+ m[s] = val
|
|
|
|
+ case float64:
|
|
|
|
+ m[s] = fmt.Sprintf("%.2f", val) // 保留两位小数输出
|
|
|
|
+ case bool:
|
|
|
|
+ if val {
|
|
|
|
+ m[s] = "是"
|
|
|
|
+ } else {
|
|
|
|
+ m[s] = "否"
|
|
|
|
+ }
|
|
|
|
+ default:
|
|
|
|
+ m[s] = value // 原样输出字符串
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- }(url, v.PointName)
|
|
|
|
|
|
+ } else {
|
|
|
|
+ h.logger.Warn("未找到 val 值", zap.String("url", url))
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+ resp.HandleSuccess(ctx, m)
|
|
|
|
|
|
- // 等待所有协程完成
|
|
|
|
- wg.Wait()
|
|
|
|
-
|
|
|
|
- // 统一返回结果
|
|
|
|
- resp.HandleSuccess(ctx, pointType)
|
|
|
|
}
|
|
}
|
|
|
|
+
|
|
func (h *IntelligentBuildingControlHandler) GetGetPointSSE(ctx *gin.Context) {
|
|
func (h *IntelligentBuildingControlHandler) GetGetPointSSE(ctx *gin.Context) {
|
|
// 设置响应头
|
|
// 设置响应头
|
|
ctx.Header("Content-Type", "text/event-stream")
|
|
ctx.Header("Content-Type", "text/event-stream")
|
|
@@ -198,7 +272,7 @@ func (h *IntelligentBuildingControlHandler) GetGetPointSSE(ctx *gin.Context) {
|
|
return
|
|
return
|
|
default:
|
|
default:
|
|
m := make(map[string]any)
|
|
m := make(map[string]any)
|
|
- points, err := h.intelligentBuildingControlService.GetPoint(conds)
|
|
|
|
|
|
+ points, _, err := h.intelligentBuildingControlService.GetPoint(conds, 1, 100)
|
|
if err != nil {
|
|
if err != nil {
|
|
resp.HandleError(ctx, 1201, "查询点位失败", nil)
|
|
resp.HandleError(ctx, 1201, "查询点位失败", nil)
|
|
conn = false
|
|
conn = false
|
|
@@ -249,29 +323,143 @@ func (h *IntelligentBuildingControlHandler) GetGetPointSSE(ctx *gin.Context) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+//func (h *IntelligentBuildingControlHandler) GetGetPointSSE(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 `json:"requestId,omitempty"`
|
|
|
|
+// Code int32 `json:"code,omitempty"`
|
|
|
|
+// Msg string `json:"msg,omitempty"`
|
|
|
|
+// Data any `json:"data"`
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// pointName := ctx.Query("pointName")
|
|
|
|
+// deviceType := ctx.Query("deviceType")
|
|
|
|
+// building := ctx.Query("building")
|
|
|
|
+// floor := ctx.Query("floor")
|
|
|
|
+// section := ctx.Query("section")
|
|
|
|
+// device_name := ctx.Query("deviceName")
|
|
|
|
+//
|
|
|
|
+// conds := make(map[string]any)
|
|
|
|
+// if pointName != "" {
|
|
|
|
+// conds["point_name"] = pointName
|
|
|
|
+// }
|
|
|
|
+// if deviceType != "" {
|
|
|
|
+// conds["device_type"] = deviceType
|
|
|
|
+// }
|
|
|
|
+// if building != "" {
|
|
|
|
+// conds["building"] = building
|
|
|
|
+// }
|
|
|
|
+// if floor != "" {
|
|
|
|
+// conds["floor"] = floor
|
|
|
|
+// }
|
|
|
|
+// if section != "" {
|
|
|
|
+// conds["section"] = section
|
|
|
|
+// }
|
|
|
|
+// if device_name != "" {
|
|
|
|
+// conds["device_name"] = device_name
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// baseUrl := h.conf.GetString("obix.baseUrl")
|
|
|
|
+//
|
|
|
|
+// var response Response
|
|
|
|
+// for conn {
|
|
|
|
+// select {
|
|
|
|
+// case <-notify:
|
|
|
|
+// conn = false
|
|
|
|
+// fmt.Println("客户端断开连接")
|
|
|
|
+// return
|
|
|
|
+// default:
|
|
|
|
+// m := make(map[string]any)
|
|
|
|
+// points, _, err := h.intelligentBuildingControlService.GetPoint(conds, 1, 100)
|
|
|
|
+// if err != nil {
|
|
|
|
+// resp.HandleError(ctx, 1201, "查询点位失败", nil)
|
|
|
|
+// conn = false
|
|
|
|
+// continue
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// var wg sync.WaitGroup
|
|
|
|
+// var mutex sync.Mutex
|
|
|
|
+// sem := make(chan struct{}, 10) // 控制最大并发数为10
|
|
|
|
+//
|
|
|
|
+// for _, v := range *points {
|
|
|
|
+// select {
|
|
|
|
+// case sem <- struct{}{}:
|
|
|
|
+// default:
|
|
|
|
+// <-sem
|
|
|
|
+// sem <- struct{}{}
|
|
|
|
+// }
|
|
|
|
+// wg.Add(1)
|
|
|
|
+// go func(v model.Point) {
|
|
|
|
+// defer func() {
|
|
|
|
+// <-sem
|
|
|
|
+// wg.Done()
|
|
|
|
+// }()
|
|
|
|
+//
|
|
|
|
+// url := baseUrl + v.FullPath
|
|
|
|
+// request, err := obix.SendSecureRequest(url, h.conf.GetString("obix.username"), h.conf.GetString("obix.password"))
|
|
|
|
+// if err != nil {
|
|
|
|
+// h.logger.Error("发送请求失败", zap.String("url", url), zap.Error(err))
|
|
|
|
+// return
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// re := regexp.MustCompile(`val="([^"]+)"`)
|
|
|
|
+// matches := re.FindStringSubmatch(request)
|
|
|
|
+//
|
|
|
|
+// if len(matches) > 1 {
|
|
|
|
+// s := model.PointName[v.PointName]
|
|
|
|
+// if s != "" {
|
|
|
|
+// value := matches[1]
|
|
|
|
+// result := obix.DetectType(value)
|
|
|
|
+//
|
|
|
|
+// mutex.Lock()
|
|
|
|
+// defer mutex.Unlock()
|
|
|
|
+//
|
|
|
|
+// switch val := result.(type) {
|
|
|
|
+// case int:
|
|
|
|
+// m[s] = val
|
|
|
|
+// case float64:
|
|
|
|
+// m[s] = fmt.Sprintf("%.2f", val)
|
|
|
|
+// case bool:
|
|
|
|
+// if val {
|
|
|
|
+// m[s] = "是"
|
|
|
|
+// } else {
|
|
|
|
+// m[s] = "否"
|
|
|
|
+// }
|
|
|
|
+// default:
|
|
|
|
+// m[s] = value
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+// } else {
|
|
|
|
+// h.logger.Warn("未找到 val 值", zap.String("url", url))
|
|
|
|
+// }
|
|
|
|
+// }(v)
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// wg.Wait() // 等待所有 goroutine 完成
|
|
|
|
+//
|
|
|
|
+// response.Code = 200
|
|
|
|
+// response.RequestId = ctx.ClientIP()
|
|
|
|
+// response.Msg = "success"
|
|
|
|
+// response.Data = m
|
|
|
|
+//
|
|
|
|
+// res, _ := json.Marshal(&response)
|
|
|
|
+// fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
|
|
|
|
+// ctx.Writer.Flush()
|
|
|
|
+// time.Sleep(10 * time.Second)
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+//}
|
|
|
|
+
|
|
// GetPointType 获取点位类型
|
|
// GetPointType 获取点位类型
|
|
func (h *IntelligentBuildingControlHandler) GetPointType(ctx *gin.Context) {
|
|
func (h *IntelligentBuildingControlHandler) GetPointType(ctx *gin.Context) {
|
|
- var tempS1 model.NumericPoint
|
|
|
|
- // var xmlData = `<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
- //<real val="27.49811553955078" display="27.50 °C {ok}" unit="obix:units/celsius">
|
|
|
|
- // <str name="facets" val="units=u:celsius;°C;(K);+273.15;" display="units=°C"/>
|
|
|
|
- // <ref name="proxyExt" display="analogInput:1:Present Value:-1:REAL"/>
|
|
|
|
- // <real name="out" val="27.49811553955078" display="27.50 °C {ok}"/>
|
|
|
|
- // <ref name="tag" display="Point Tag"/>
|
|
|
|
- //</real>`
|
|
|
|
- //err2 := ctx.ShouldBindXML(&tempS1)
|
|
|
|
- //err2 := xml.Unmarshal([]byte(xmlData), &tempS1)
|
|
|
|
- //if err2 != nil {
|
|
|
|
- // resp.HandleError(ctx, 1201, "绑定XML失败", nil)
|
|
|
|
- // return
|
|
|
|
- //}
|
|
|
|
- request, err2 := obix.SendSecureRequest("https://10.1.201.253/obix/config/Drivers/BacnetNetwork/DDC_9B_1F_1a/points/PAU/PAU_9B_1F_1/PAUAlr1", h.conf.GetString("obix.username"), h.conf.GetString("obix.password"))
|
|
|
|
- if err2 != nil {
|
|
|
|
- resp.HandleError(ctx, 1201, "发送请求失败", nil)
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- fmt.Println(request, "===========")
|
|
|
|
- fmt.Println(tempS1)
|
|
|
|
points, err := h.intelligentBuildingControlService.GetPointType()
|
|
points, err := h.intelligentBuildingControlService.GetPointType()
|
|
if err != nil {
|
|
if err != nil {
|
|
resp.HandleError(ctx, 1201, "查询点位类型失败", nil)
|
|
resp.HandleError(ctx, 1201, "查询点位类型失败", nil)
|
|
@@ -279,6 +467,8 @@ func (h *IntelligentBuildingControlHandler) GetPointType(ctx *gin.Context) {
|
|
}
|
|
}
|
|
resp.HandleSuccess(ctx, points)
|
|
resp.HandleSuccess(ctx, points)
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+// GetDeviceType 获取设备类型
|
|
func (h *IntelligentBuildingControlHandler) GetDeviceType(ctx *gin.Context) {
|
|
func (h *IntelligentBuildingControlHandler) GetDeviceType(ctx *gin.Context) {
|
|
points, err := h.intelligentBuildingControlService.DeviceType()
|
|
points, err := h.intelligentBuildingControlService.DeviceType()
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -291,3 +481,58 @@ func (h *IntelligentBuildingControlHandler) GetDeviceType(ctx *gin.Context) {
|
|
}
|
|
}
|
|
resp.HandleSuccess(ctx, m)
|
|
resp.HandleSuccess(ctx, m)
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+// GetDevices 获取设备列表
|
|
|
|
+func (h *IntelligentBuildingControlHandler) GetDevices(ctx *gin.Context) {
|
|
|
|
+ conds := make(map[string]any)
|
|
|
|
+ pageNum, err := strconv.Atoi(ctx.Query("pageNum"))
|
|
|
|
+ pageSize, err := strconv.Atoi(ctx.Query("pageSize"))
|
|
|
|
+ device_type := ctx.Query("device_type")
|
|
|
|
+ if len(device_type) == 0 || device_type == "" {
|
|
|
|
+ resp.HandleError(ctx, 1201, "设备类型不能为空", nil)
|
|
|
|
+ }
|
|
|
|
+ conds["device_type"] = device_type
|
|
|
|
+ if err != nil {
|
|
|
|
+ resp.HandleError(ctx, 1201, "获取分页参数失败", nil)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ devices, total, err := h.intelligentBuildingControlService.GetDevices(conds, pageNum, pageSize)
|
|
|
|
+ if err != nil {
|
|
|
|
+ resp.HandleError(ctx, 1201, "查询设备类型失败", nil)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ resp.PageHandleSuccess(ctx, devices, total, pageNum, pageSize)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// ConfigurationDiagram 获取设备组态图
|
|
|
|
+func (h *IntelligentBuildingControlHandler) ConfigurationDiagram(c *gin.Context) {
|
|
|
|
+ // 获取查询参数
|
|
|
|
+ //type-deviceName
|
|
|
|
+ id := c.Query("id")
|
|
|
|
+
|
|
|
|
+ if len(id) == 0 {
|
|
|
|
+ resp.HandleError(c, 1201, "设备名称不能为空", nil)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ split := strings.Split(id, "-")
|
|
|
|
+ if len(split) != 2 {
|
|
|
|
+ resp.HandleError(c, 1201, "设备名称格式错误", nil)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ deviceName := split[1]
|
|
|
|
+ types := split[0]
|
|
|
|
+ switch types {
|
|
|
|
+ case "PAU":
|
|
|
|
+ c.HTML(http.StatusOK, "PAU.html", gin.H{
|
|
|
|
+ "deviceName": deviceName,
|
|
|
|
+ })
|
|
|
|
+ case "JSK":
|
|
|
|
+ c.HTML(http.StatusOK, "JSK.html", gin.H{
|
|
|
|
+ "deviceName": deviceName,
|
|
|
|
+ })
|
|
|
|
+ case "PF":
|
|
|
|
+ c.HTML(http.StatusOK, "PF.html", gin.H{
|
|
|
|
+ "deviceName": deviceName,
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+}
|