---
panlei
2019-12-17 cd60b7e3a0bbc09f4501f66260b5d961d58aaf49
insertdata/insertDataToEs.go
@@ -1,263 +1,41 @@
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"
   "plugin"
   "ruleprocess/cache"
   "basic.com/valib/logger.git"
   "ruleprocess/structure"
   "strconv"
)
// 人脸的数据结构
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" {
         if len(sdkinfo.Sdkdata) > 1 {
            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.")
               }
            }
         } else {
            continue
         }
//var weedfsUrl, videoPersonUrl, personAction string
//var serverIp string
//var serverPort string
func InsertToEs(msg structure.ResultMsg) {
   defer func() {
      if err := recover(); err != nil {
         logger.Error("es模块儿的异常捕获:", err)
      }
      if sdkinfo.Sdktype == "Yolo" {
         if len(sdkinfo.Sdkdata) > 1 {
            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.")
            }
         } else {
            continue
         }
      }
   }()
   localConfig1, err := cache.GetServerInfo()
   if err != nil {
      panic("配置文件不合法")
   }
   weedfsUrl := "http://" + localConfig1.WebPicIp + ":" + strconv.Itoa(int(localConfig1.WebPicPort)) + "/submit"
   videoPersonUrl := "http://" + localConfig1.AlarmIp + ":" + strconv.Itoa(int(localConfig1.AlarmPort)) + "/" + structure.EsInfo.EsIndex.AIOcean.IndexName + "/" + structure.EsInfo.EsIndex.AIOcean.IndexType+"?refresh=true"
   serverIp := localConfig1.AlarmIp
   serverPort := strconv.Itoa(int(localConfig1.AlarmPort))
   handle(msg,weedfsUrl,videoPersonUrl,serverIp,serverPort)
}
// 获取本机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
func handle (msg structure.ResultMsg,weedfsUrl,videoPersonUrl,serverIp,serverPort string) {
   p,err :=  plugin.Open("./algorithm/middleware.so")
   if err != nil {
      panic(err)
   }
   // 取第一个非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
         }
      }
   f,err1 := p.Lookup("InsertToEs")
   if err1 != nil {
      panic("没有找到中间件的格式化数据函数")
   }
   err = errors.New("ipv4 not found")
   return
   f.(func(msg structure.ResultMsg,weedfsUrl,videoPersonUrl,serverIp,serverPort string))(msg,weedfsUrl,videoPersonUrl,serverIp,serverPort)
}