panlei
2019-06-22 7f33dadd11c44b872e4e0b0c43504c318f0d4d3c
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package insertdata
 
import (
    "errors"
    "fmt"
    "encoding/json"
    "net"
    "time"
 
    "basic.com/pubsub/protomsg.git"
    "basic.com/dbapi.git"
    "github.com/golang/protobuf/proto"
    "ruleprocess/ruleserver"
    "github.com/satori/go.uuid"
)
 
// 人脸的数据结构
type PerVideoPicture struct {
    Id              string `json:"id"`
    CameraId        string `json:"cameraId"`
    CameraAddr      string `json:"cameraAddr"`
    PicDate         string `json:"picDate"`
    PicMaxUrl       string `json:"picMaxUrl"`
    TaskId          string `json:"taskId"`
    TaskName        string `json:"taskName"`
    SdkName         string `json:"sdkName"`
    Content         string `json:"content"`
    LikeDate        string `json:"likeDate"`
    Sex             int32  `json:"sex"`
    Age             int32  `json:"age"`
    AgeDescription  string `json:"ageDescription"`
    Race            int32  `json:"race"`
    SmileLevel      int32  `json:"smileLevel"`
    BeautyLevel     int32  `json:"beautyLevel"`
    FaceFeature     string `json:"faceFeature"`
    PicSmUrl        string `json:"picSmUrl"`
    VideoUrl        string `json:"videoUrl"`
    AnalyServerId   string `json:"analyServerId"`
    AnalyServerName string `json:"analyServerName"`
    AnalyServerIp   string `json:"analyServerIp"`
    ClusterId       string `json:"clusterId"`
    IsAlarm         string `json:"isAlarm"`
    IsAckAlarm      string `json:"isAckAlarm"`
    IsCollect       string `json:"isCollect"`
    IsDelete        int    `json:"isDelete"`
    BaseInfo        Base   `json:"baseInfo"`
}
 
type Base struct {
    TableId      string  `json:"tableId"`
    TableName    string  `json:"tableName"`
    CompareScore float64 `json:"compareScore"`
    PersonId     string  `json:"personId"`
    PersonName   string  `json:"personName"`
    PersonPicUrl string  `json:"personPicUrl"`
    PhoneNum     string  `json:"phoneNum"`
    Sex          string  `json:"sex"`
    IdCard       string  `json:"idCard"`
    MonitorLevel string  `json:"monitorLevel"`
    Content      string  `json:"content"`
}
 
//  yolo行为的数据结构
type Personaction struct {
    Id              string `json:"id"`
    CameraId        string `json:"cameraId"`
    CameraName      string `json:"cameraName"`
    CameraAddr      string `json:"cameraAddr"`
    TaskId          string `json:"taskId"`
    TaskName        string `json:"taskName"`
    SdkName         string `json:"sdkName"`
    Content         string `json:"content"`
    AlarmRules      []AlarmRule
    AnalyServerId   string `json:"analyServerId"`
    AnalyServerName string `json:"analyServerName"`
    AnalyServerIp   string `json:"analyServerIp"`
    ClusterId       string `json:"clusterId"`
    PicUrl          string `json:"picUrl"`
    PicDate         string `json:"picDate"`
    VideoUrl        string `json:"videoUrl"`
    IsAlarm         string `json:"isAlarm"`
    IsAckAlarm      string `json:"isAckAlarm"`
    IsCollect       string `json:"isCollect"`
    IsDelete        int    `json:"isDelete"`
}
 
type AlarmRule struct {
    GroupId    string `json:"groupId"`
    AlarmLevel int32 `json:"alarmLevel"`
    RuleText   string `json:"ruleText"`
}
 
func InsertToEs(msg ruleserver.ResultMsg) {
    fmt.Println("往ES插数据")
 
    for _, sdkinfo := range msg.Tasklab.Sdkinfos {
        if sdkinfo.Sdktype == "FaceDetect" {
            faceParam := protomsg.ParamFacePos{}
            err1 := proto.Unmarshal(sdkinfo.Sdkdata, &faceParam)
            if err1 != nil {
                fmt.Println("解析FACE sdk数据时出现错误", err1)
                continue
            }
            for _, face := range faceParam.Faces {
                pervideo := PerVideoPicture{
                    uuid.NewV4().String(),
                    msg.Cid,
                    msg.Caddr,
                    time.Now().Format("2006-01-02 15:04:05"),
                    "",
                    msg.Tasklab.Taskid,
                    msg.Tasklab.Taskname,
                    sdkinfo.SdkName,
                    "",
                    "",
                    face.Result.Gender,
                    face.Result.Age,
                    "",
                    face.Result.Race,
                    face.Result.Smile,
                    face.Result.Beauty,
                    "不是每个人脸算法都有",
                    "---",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    0,
                    Base{
                        "是每个人脸算法都有吗",
                        "",
                        0,
                        "",
                        "",
                        "",
                        "",
                        "",
                        "",
                        "",
                        "",
                    },
                }
                requstbody, err := json.Marshal(pervideo)
 
                if err != nil {
                    fmt.Println("json parse error ", err)
                    return
 
                }
                err = EsReq("POST", "http://192.168.1.182:9200/videopersons/perVideoPicture", requstbody)
                if err != nil {
                    fmt.Println("es can not execute right.")
                }
            }
        }
        if sdkinfo.Sdktype == "Yolo" {
            yoloObj := protomsg.ParamYoloObj{}
            err1 := proto.Unmarshal(sdkinfo.Sdkdata, &yoloObj)
            if err1 != nil {
                fmt.Println("解析YOLO sdk数据时出现错误", err1)
                //continue
            }
            alarmRules := []AlarmRule{}
            for _,result := range msg.RuleResult {
                alarmRules = append(alarmRules,AlarmRule{result.RuleGroupId,result.AlarmLevel,result.RuleText})
            }
            isAlarm := ""
            if len(alarmRules) > 0 {
                isAlarm = "1"
            }else {
                isAlarm = "0"
                continue
            }
            // 查询本机信息
            flag,localConfig := dbapi.SysSetApi{}.GetServerInfo()
            if !flag {
                fmt.Println("查询本机信息失败!")
            }
            // 查询cameraName
            camera,err := dbapi.CameraApi{}.GetCameraById(msg.Cid)
            if err == nil {
                fmt.Println("查询摄像机信息失败")
            }
            serverIp,err := GetLocalIP()
            peraction := Personaction{
                uuid.NewV4().String(),
                msg.Cid,
                camera.Name,
                msg.Caddr,
                msg.Tasklab.Taskid,
                msg.Tasklab.Taskname,
                sdkinfo.SdkName,
                "",
                alarmRules,
                localConfig.ServerId,
                localConfig.ServerName,
                serverIp,
                "",
                "",
                time.Now().Format("2006-01-02 15:04:05"),
                "",
                isAlarm,
                "",
                "",
                0,
            }
            requstbody, err := json.Marshal(peraction)
 
            if err != nil {
                fmt.Println("json parse error ", err)
                return
 
            }
            err = EsReq("POST", "http://192.168.1.182:9200/personaction/perVideoAction", requstbody)
            if err != nil {
                fmt.Println("es can not execute right.")
            }
        }
 
    }
}
 
// 获取本机ip
func GetLocalIP() (ipv4 string, err error) {
    var (
        addrs   []net.Addr
        addr    net.Addr
        ipNet   *net.IPNet // IP地址
        isIpNet bool
    )
    // 获取所有网卡
    if addrs, err = net.InterfaceAddrs(); err != nil {
        return
    }
    // 取第一个非lo的网卡IP
    for _, addr = range addrs {
        // 这个网络地址是IP地址: ipv4, ipv6
        if ipNet, isIpNet = addr.(*net.IPNet); isIpNet && !ipNet.IP.IsLoopback() {
            // 跳过IPV6
            if ipNet.IP.To4() != nil {
                ipv4 = ipNet.IP.String() // 192.168.1.1
                return
            }
        }
    }
 
    err = errors.New("ipv4 not found")
    return
}