zhangzengfei
2024-10-20 b916f7c68a3e16413eac4be13f0404267561f90c
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
package nvcs
 
import (
    "encoding/xml"
    "io/ioutil"
 
    "gat1400Exchange/pkg/logger"
 
    dac "github.com/xinsnake/go-http-digest-auth-client"
)
 
// 设置其他字符的第一个, 位置固定位7
// 摄像机信息暂时固定, 避免泄露
const (
    hikISAPIOverlay7SetUrl = "/ISAPI/System/Video/inputs/channels/1/overlays/text/7"
    cameraWebAddr          = "http://192.168.10.11:40080"
    cameraWebUser          = "admin"
    cameraWebPassword      = "a1234567"
)
 
var overlayText textOverlay
 
type textOverlay struct {
    Id               int64  `xml:"id"`
    Enabled          bool   `xml:"enabled"`
    PositionX        int64  `xml:"positionX"`
    PositionY        int64  `xml:"positionY"`
    DisplayText      string `xml:"displayText"`
    DirectAngle      string `xml:"directAngle"`
    IsPersistentText bool   `xml:"isPersistentText"`
}
 
func hikISAPIRequest(username, password, method, url, body string) ([]byte, error) {
    dr := dac.NewRequest(username, password, method, url, body)
    resp, err := dr.Execute()
    if err != nil {
        return nil, err
    }
 
    defer resp.Body.Close()
 
    return ioutil.ReadAll(resp.Body)
}
 
func addFloorToOSD(osdText string) {
    overlay7Url := cameraWebAddr + hikISAPIOverlay7SetUrl
 
    // 获取左下角第一个字符的位置
    if overlayText.DisplayText == "" {
        rsp, err := hikISAPIRequest(cameraWebUser, cameraWebPassword, "GET", overlay7Url, "")
        if err != nil {
            logger.Warn("Get osd info failure")
            return
        }
 
        err = xml.Unmarshal(rsp, &overlayText)
        if err != nil {
            logger.Warn("%s", err.Error())
            return
        }
    }
 
    overlayText.DisplayText = osdText
    body, _ := xml.Marshal(overlayText)
    _, err := hikISAPIRequest(cameraWebUser, cameraWebPassword, "PUT", overlay7Url, string(body))
    if err != nil {
        logger.Warn("Camera osd set failure!!")
        return
    }
}