package insertdata
|
|
import (
|
"errors"
|
"encoding/json"
|
"log"
|
"net"
|
"time"
|
|
"basic.com/pubsub/protomsg.git"
|
"basic.com/dbapi.git"
|
"ruleprocess/ruleserver"
|
"ruleprocess/util"
|
"github.com/golang/protobuf/proto"
|
"github.com/satori/go.uuid"
|
)
|
var weedfsUrl = "http://192.168.1.182:6333/submit"
|
// 人脸的数据结构
|
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"`
|
}
|
// 往ES插数据
|
func InsertToEs(msg ruleserver.ResultMsg) {
|
log.Println("往ES插数据")
|
|
for _, sdkinfo := range msg.Tasklab.Sdkinfos {
|
if sdkinfo.Sdktype == "FaceDetect" {
|
if len(sdkinfo.Sdkdata) > 1 {
|
|
faceParam := protomsg.ParamFacePos{}
|
err1 := proto.Unmarshal(sdkinfo.Sdkdata, &faceParam)
|
if err1 != nil {
|
log.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 {
|
log.Println("json parse error ", err)
|
return
|
|
}
|
err = EsReq("POST", "http://192.168.1.182:9200/videopersons/perVideoPicture", requstbody)
|
if err != nil {
|
log.Println("es can not execute right.")
|
}
|
}
|
} else {
|
continue
|
}
|
}
|
if sdkinfo.Sdktype == "Yolo" {
|
if len(sdkinfo.Sdkdata) > 1 {
|
|
yoloObj := protomsg.ParamYoloObj{}
|
err1 := proto.Unmarshal(sdkinfo.Sdkdata, &yoloObj)
|
if err1 != nil {
|
log.Println("解析YOLO sdk有误", err1)
|
//continue
|
}
|
alarmRules := []AlarmRule{}
|
for _,result := range msg.RuleResult {
|
alarmRules = append(alarmRules,AlarmRule{result.RuleGroupId,result.AlarmLevel,result.RuleText})
|
}
|
isAlarm := ""
|
resp := make(map[string]interface{})
|
resp["fileUrl"] = ""
|
if len(alarmRules) > 0 {
|
isAlarm = "1"
|
// 解压缩并上传图片
|
bdata, err := util.UnCompress(msg.Data)
|
if err != nil {
|
panic("解压缩图片时出现错误")
|
}
|
i := protomsg.Image{}
|
err = proto.Unmarshal(bdata, &i)
|
resp,err = util.PostFormBufferData(weedfsUrl,i,uuid.NewV4().String())
|
log.Println("已报警并上传改帧图片到服务器")
|
}else {
|
isAlarm = "0"
|
// 不是报警数据不存
|
continue
|
}
|
log.Println("图片上传返回值:",resp)
|
// 查询本机信息
|
flag,localConfig := dbapi.SysSetApi{}.GetServerInfo()
|
if !flag {
|
log.Println("查询本机信息失败!")
|
}
|
// 查询cameraName
|
camera,err := dbapi.CameraApi{}.GetCameraById(msg.Cid)
|
if err != nil {
|
log.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,
|
"",
|
resp["fileUrl"].(string),
|
time.Now().Format("2006-01-02 15:04:05"),
|
"",
|
isAlarm,
|
"",
|
"",
|
0,
|
}
|
requstbody, err := json.Marshal(peraction)
|
|
if err != nil {
|
log.Println("json parse error ", err)
|
return
|
|
}
|
err = EsReq("POST", "http://192.168.1.182:9200/personaction/perVideoAction", requstbody)
|
if err != nil {
|
log.Println("es can not execute right.")
|
}
|
} else {
|
continue
|
}
|
}
|
|
}
|
}
|
|
// 获取本机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
|
}
|