panlei
2019-12-13 2ed59d1db29aa1a58f0cca02346eb7ae722eab27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package ruleserver
 
import (
    "basic.com/pubsub/protomsg.git"
    "ruleprocess/cache"
    "basic.com/valib/logger.git"
    "ruleprocess/structure"
    "github.com/kirinlabs/HttpRequest"
    "strconv"
)
 
type CameraPolygon struct {
    CameraId string
    CameraAddr string
    Rtsp string
    Area []Polygon
}
type Polygon struct {
    CameraId string
    CameraAddr string
    Polygon []structure.Point
}
var camps []CameraPolygon
 
func Handle () {
    taskRules := cache.GetCameraTaskRulesAll()
    for _,taskRule := range taskRules {
        for _,groupRule := range taskRule.GroupRules {
            for _,rule := range groupRule.Rules {
                camp := GetCameraPolygon(rule,groupRule)
                camps = append(camps,*camp)
            }
        }
    }
}
 
func GetCameraPolygon(rule *protomsg.Rule,groupRule *protomsg.GroupRule) *CameraPolygon{
    if rule.SdkId == "标定算法的id" {
        // 得到标定算法的区域
        cameraPolygon := new(CameraPolygon)
        polygon := cache.GetPolygonsById(rule.PolygonId)
        cameraPolygon.CameraId = rule.CameraId
        camera,err := cache.GetCameraById(rule.CameraId)
        if err != nil {
            logger.Error("查询摄像机信息失败!",err)
        }
        cameraPolygon.CameraAddr = camera.Addr
        cameraPolygon.Rtsp = camera.Rtsp
        if rule.SdkArgAlias == "全景摄像机" { // 塞的是自己的区域
            polygon1 := new(Polygon)
            polygon1.Polygon = Json2points(polygon.Polygon)
            polygon1.CameraId = rule.CameraId
            polygon1.CameraAddr = camera.Addr
            cameraPolygon.Area = append(cameraPolygon.Area,*polygon1)
        } else { // 塞的是与之相关的全景摄像机的区域
            for _,rule := range groupRule.Rules {
                if rule.SdkArgAlias == "全景摄像机" {
                    polygon1 := new(Polygon)
                    polygon1.Polygon = Json2points(polygon.Polygon)
                    polygon1.CameraId = rule.CameraId
                    polygon1.CameraAddr = camera.Addr
                    cameraPolygon.Area = append(cameraPolygon.Area,*polygon1)
                }
            }
        }
        return cameraPolygon
    } else {
        return nil
    }
}
 
func Struct2JsonString(camps []CameraPolygon) string{
    str := "["
    for index,cam := range camps {
        str1 := "{\"CameraId\":\""+cam.CameraId+"\",\"CameraAddr\":"+cam.CameraAddr+"\",\"Rtsp\":\""+cam.Rtsp+"\",\"Area\":\"["
        for j,ar := range cam.Area {
            str2 := "{\""+ar.CameraId+"\": {\"area\": ["
            for i,point := range ar.Polygon {
                str3 := ""
                if i < len(ar.Polygon) - 1 {
                    str3 = "{\"coordinate\": {\"x\": \""+strconv.FormatFloat(point.X, 'f', -1, 64)+"\",\"y\": \""+strconv.FormatFloat(point.Y, 'f', -1, 64)+"\"}},"
                } else { // 最后一个结构体不加逗号
                    str3 = "{\"coordinate\": {\"x\": \""+strconv.FormatFloat(point.X, 'f', -1, 64)+"\",\"y\": \""+strconv.FormatFloat(point.Y, 'f', -1, 64)+"\"}}"
                }
                str2 += str3
            }
            if j < len(cam.Area) - 1{
                str2 += "]}},"
            } else {
                str2 += "]}}"
            }
            str1 += str2
        }
        str += str1
        if index < len(camps) - 1{
            str += "]},"
        } else {
            str += "]}"
        }
    }
    str += "]"
    return str
}
 
func PushPolygon() string{
    Handle()
    param := Struct2JsonString(camps)
    req := HttpRequest.NewRequest()
    res,err := req.JSON().Post("http://192.168.20.109:7011/tracking/get_region",param)
    body,err := res.Body()
    if err != nil {
        return "请求失败"+err.Error()
    }
    return string(body)
}